新增远程项目克隆功能
This commit is contained in:
@@ -97,6 +97,31 @@ func (s *AppService) GetProjectTree() []TreeNode {
|
||||
|
||||
// --- 项目管理 ---
|
||||
|
||||
// CloneProject 克隆远程仓库到本地目录,并添加到项目树
|
||||
func (s *AppService) CloneProject(platform, username, repoURL, parentDir, name string) error {
|
||||
if repoURL == "" {
|
||||
return fmt.Errorf("仓库地址不能为空")
|
||||
}
|
||||
if parentDir == "" {
|
||||
return fmt.Errorf("目标目录不能为空")
|
||||
}
|
||||
if name == "" {
|
||||
return fmt.Errorf("项目名称不能为空")
|
||||
}
|
||||
|
||||
// 目标路径: parentDir/name
|
||||
targetPath := parentDir + "/" + name
|
||||
|
||||
// 执行 git clone
|
||||
_, err := s.gitClient.Clone(repoURL, targetPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("克隆失败: %w", err)
|
||||
}
|
||||
|
||||
// 克隆成功后添加到项目树
|
||||
return s.AddProject(platform, username, name, targetPath)
|
||||
}
|
||||
|
||||
// AddProject 添加项目到指定平台/用户下
|
||||
func (s *AppService) AddProject(platform, username, name, path string) error {
|
||||
p, ok := s.config.Platforms[platform]
|
||||
|
||||
@@ -65,7 +65,28 @@ func (g *GitClient) Status(path string) (string, error) {
|
||||
}
|
||||
|
||||
func (g *GitClient) Clone(repoURL, path string) (string, error) {
|
||||
return g.Run(".", "clone", repoURL, path)
|
||||
// Clone 操作可能需要较长时间,使用独立的超时设置(10 分钟)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
args := []string{"-c", "core.quotePath=false"}
|
||||
if g.Enabled {
|
||||
args = append(args, "-c", "http.proxy="+g.Proxy, "-c", "https.proxy="+g.Proxy)
|
||||
}
|
||||
args = append(args, "clone", "--progress", repoURL, path)
|
||||
log.Println(args)
|
||||
cmd := exec.CommandContext(ctx, "git", args...)
|
||||
hideWindow(cmd)
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
return "", fmt.Errorf("git clone timeout")
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("git clone error: %v, stderr: %s", err, stderr.String())
|
||||
}
|
||||
return stdout.String(), nil
|
||||
}
|
||||
|
||||
func (g *GitClient) Fetch(path string) (string, error) {
|
||||
|
||||
Reference in New Issue
Block a user