From 46778bc227f35d83dcfadbd6644285b637369f27 Mon Sep 17 00:00:00 2001 From: wonder Date: Sat, 20 Jun 2026 00:06:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20services/git.g?= =?UTF-8?q?o=20=E4=B8=AD=E7=9A=84=E6=97=A0=E7=94=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/git.go | 85 +++++++++++++------------------------------------ 1 file changed, 23 insertions(+), 62 deletions(-) diff --git a/services/git.go b/services/git.go index d2fb4b7..9ff11ff 100644 --- a/services/git.go +++ b/services/git.go @@ -1,8 +1,8 @@ package services import ( - "bufio" "fmt" + "log" "os" "path/filepath" "sort" @@ -121,19 +121,29 @@ func Clone(opts CloneOptions, progressFn func(event string, data interface{})) ( result.CommitNum++ return nil }) + } else { + log.Printf("warning: failed to count commits: %v", err) } - branches, _ := repo.Branches() - _ = branches.ForEach(func(ref *plumbing.Reference) error { - result.Branches = append(result.Branches, ref.Name().Short()) - return nil - }) + branches, err := repo.Branches() + if err == nil { + _ = branches.ForEach(func(ref *plumbing.Reference) error { + result.Branches = append(result.Branches, ref.Name().Short()) + return nil + }) + } else { + log.Printf("warning: failed to list branches: %v", err) + } - tags, _ := repo.Tags() - _ = tags.ForEach(func(ref *plumbing.Reference) error { - result.Tags = append(result.Tags, ref.Name().Short()) - return nil - }) + tags, err := repo.Tags() + if err == nil { + _ = tags.ForEach(func(ref *plumbing.Reference) error { + result.Tags = append(result.Tags, ref.Name().Short()) + return nil + }) + } else { + log.Printf("warning: failed to list tags: %v", err) + } result.SizeBytes = dirSize(opts.Dir) @@ -446,34 +456,6 @@ remote, err := repo.Remote("origin") }) } -// CloneSSEEvent formats an SSE event for the clone progress stream. -func CloneSSEEvent(event string, data interface{}) string { - return fmt.Sprintf("event: %s\ndata: %s\n\n", event, toJSON(data)) -} - -// toJSON is a simple JSON encoder for SSE data. -func toJSON(v interface{}) string { - switch d := v.(type) { - case map[string]interface{}: - var parts []string - for k, val := range d { - switch vv := val.(type) { - case string: - parts = append(parts, fmt.Sprintf("%q:%q", k, vv)) - case int: - parts = append(parts, fmt.Sprintf("%q:%d", k, vv)) - case int64: - parts = append(parts, fmt.Sprintf("%q:%d", k, vv)) - default: - parts = append(parts, fmt.Sprintf("%q:%q", k, fmt.Sprintf("%v", vv))) - } - } - return "{" + strings.Join(parts, ",") + "}" - default: - return fmt.Sprintf("%q", fmt.Sprintf("%v", v)) - } -} - // dirSize returns the total size of files in a directory. func dirSize(path string) int64 { var size int64 @@ -489,29 +471,6 @@ func dirSize(path string) int64 { return size } -// EstimateSize estimates repo size without cloning (for display). -func EstimateSize(url string) int64 { - // We can't know without cloning, return 0 - return 0 -} - -// GetCloneProgressReader wraps a git clone with progress reporting. -type CloneProgressReader struct { - scanner *bufio.Scanner - progressFn func(event string, data interface{}) -} - -func NewCloneProgressReader(progressFn func(event string, data interface{})) *CloneProgressReader { - return &CloneProgressReader{ - progressFn: progressFn, - } -} - -func (r *CloneProgressReader) Read(p []byte) (n int, err error) { - // This is a simplified version - real implementation would parse git protocol - return 0, nil -} - // CompareCommits returns the diff between two commits. func CompareCommits(repoPath, base, head string) (string, error) { repo, err := OpenRepo(repoPath) @@ -575,6 +534,8 @@ func CloneWithProgress(opts CloneOptions, progressFn func(event string, data int return Clone(opts, progressFn) } +// GetDefaultBranch returns the default branch name (main or master). + // GetDefaultBranch returns the default branch name (main or master). func GetDefaultBranch(repoPath string) (string, error) { repo, err := OpenRepo(repoPath)