git-sync-web 自动提交 2026-09-09 18:02:17
This commit is contained in:
@@ -54,6 +54,29 @@ func isRepo(path string) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func ensureRepo(path, branch string) error {
|
||||
if path == "" {
|
||||
return fmt.Errorf("仓库路径为空")
|
||||
}
|
||||
path = normalizeWinPath(path)
|
||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if isRepo(path) {
|
||||
return nil
|
||||
}
|
||||
if branch == "" {
|
||||
branch = "main"
|
||||
}
|
||||
if _, err := gitRun(path, 30*time.Second, "init", "-b", branch); err != nil {
|
||||
if _, e2 := gitRun(path, 30*time.Second, "init"); e2 != nil {
|
||||
return e2
|
||||
}
|
||||
_, _ = gitRun(path, 15*time.Second, "checkout", "-b", branch)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func currentBranch(path string) (string, error) {
|
||||
return gitRun(path, 15*time.Second, "rev-parse", "--abbrev-ref", "HEAD")
|
||||
}
|
||||
@@ -210,5 +233,23 @@ func changedFiles(path string) map[string]string {
|
||||
m[p] = "M" // 含 UU/AU 冲突状态,统一显示为已修改
|
||||
}
|
||||
}
|
||||
for p, st := range m {
|
||||
if st == "?" && strings.HasSuffix(p, "/") {
|
||||
expandUntracked(path, p, m)
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func expandUntracked(repoPath, dir string, m map[string]string) {
|
||||
out, err := gitRun(repoPath, 15*time.Second, "ls-files", "--others", "--exclude-standard", "--", dir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, ln := range strings.Split(out, "\n") {
|
||||
ln = strings.TrimSpace(ln)
|
||||
if ln != "" {
|
||||
m[ln] = "?"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user