chore: update repo pull way

This commit is contained in:
engigu
2026-05-06 23:55:59 +08:00
parent 92a42bb926
commit 09b2675c65
+27 -4
View File
@@ -172,11 +172,23 @@ func syncGit(cfg Config) {
defer restore() defer restore()
if pathExists(gitDir) { if pathExists(gitDir) {
fmt.Println("检测到已存在仓库,执行 git pull") fmt.Println("检测到已存在仓库,正在更新...")
if cfg.Branch != "" { runCmd([]string{"git", "fetch", "--all"}, dest, env)
runCmd([]string{"git", "checkout", cfg.Branch}, dest, env)
targetBranch := cfg.Branch
if targetBranch != "" {
// 如果切换了分支,或者当前分支偏离,强制切换并对齐远程
runCmd([]string{"git", "checkout", "-B", targetBranch, "origin/" + targetBranch}, dest, env)
} else {
targetBranch = getCurrentBranch(dest, env)
}
if targetBranch != "" {
fmt.Printf("执行强制同步 (reset --hard origin/%s)\n", targetBranch)
runCmd([]string{"git", "reset", "--hard", "origin/" + targetBranch}, dest, env)
} else {
runCmd([]string{"git", "pull", "--rebase"}, dest, env)
} }
runCmd([]string{"git", "pull"}, dest, env)
} else { } else {
fmt.Println("执行 git clone") fmt.Println("执行 git clone")
parentDir := filepath.Dir(dest) parentDir := filepath.Dir(dest)
@@ -478,6 +490,17 @@ func isDirEmpty(path string) bool {
return err == io.EOF return err == io.EOF
} }
func getCurrentBranch(dir string, env []string) string {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
cmd.Dir = dir
cmd.Env = env
out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
// preserve moves specified paths to a temporary location and returns a function to restore them // preserve moves specified paths to a temporary location and returns a function to restore them
func preserve(baseDir string, paths string) func() { func preserve(baseDir string, paths string) func() {
if paths == "" || !pathExists(baseDir) { if paths == "" || !pathExists(baseDir) {