|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// nativeRepo implements Repository by shelling out to the native git CLI. |
| 14 | +// This is dramatically faster than go-git for large repositories because |
| 15 | +// native git has optimized packfile handling and memory-mapped I/O. |
| 16 | +type nativeRepo struct { |
| 17 | + path string |
| 18 | +} |
| 19 | + |
| 20 | +// NativeOpen opens an existing git repository using the native git CLI. |
| 21 | +func NativeOpen(path string) (Repository, error) { |
| 22 | + cmd := exec.Command("git", "-C", path, "rev-parse", "--git-dir") |
| 23 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 24 | + return nil, fmt.Errorf("not a git repository (or git not installed): %s", strings.TrimSpace(string(out))) |
| 25 | + } |
| 26 | + return &nativeRepo{path: path}, nil |
| 27 | +} |
| 28 | + |
| 29 | +func (r *nativeRepo) RepoName() string { |
| 30 | + return filepath.Base(r.path) |
| 31 | +} |
| 32 | + |
| 33 | +func (r *nativeRepo) CurrentBranch() string { |
| 34 | + cmd := exec.Command("git", "-C", r.path, "symbolic-ref", "--short", "HEAD") |
| 35 | + out, err := cmd.Output() |
| 36 | + if err != nil { |
| 37 | + return "HEAD" |
| 38 | + } |
| 39 | + return strings.TrimSpace(string(out)) |
| 40 | +} |
| 41 | + |
| 42 | +func (r *nativeRepo) HeadHash() (string, error) { |
| 43 | + cmd := exec.Command("git", "-C", r.path, "rev-parse", "HEAD") |
| 44 | + out, err := cmd.Output() |
| 45 | + if err != nil { |
| 46 | + return "", fmt.Errorf("rev-parse HEAD: %w", err) |
| 47 | + } |
| 48 | + return strings.TrimSpace(string(out)), nil |
| 49 | +} |
| 50 | + |
| 51 | +func (r *nativeRepo) Log(sinceHash string) (CommitIter, error) { |
| 52 | + args := []string{ |
| 53 | + "-C", r.path, "log", |
| 54 | + "--format=GITANALYTICS_COMMIT%n%H%n%aN%n%aE%n%aI%n%s", |
| 55 | + "--numstat", |
| 56 | + } |
| 57 | + if sinceHash != "" { |
| 58 | + args = append(args, sinceHash+"..HEAD") |
| 59 | + } |
| 60 | + |
| 61 | + cmd := exec.Command("git", args...) |
| 62 | + stdout, err := cmd.StdoutPipe() |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("creating stdout pipe: %w", err) |
| 65 | + } |
| 66 | + if err := cmd.Start(); err != nil { |
| 67 | + return nil, fmt.Errorf("starting git log: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + return &nativeCommitIter{ |
| 71 | + scanner: bufio.NewScanner(stdout), |
| 72 | + cmd: cmd, |
| 73 | + }, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (r *nativeRepo) Close() error { |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +// nativeCommitIter parses streaming output from git log --numstat. |
| 81 | +type nativeCommitIter struct { |
| 82 | + scanner *bufio.Scanner |
| 83 | + cmd *exec.Cmd |
| 84 | + peeked bool // true if we've already scanned a line that needs re-reading |
| 85 | + peekLine string // the line we peeked at |
| 86 | + exhausted bool |
| 87 | +} |
| 88 | + |
| 89 | +func (it *nativeCommitIter) nextLine() (string, bool) { |
| 90 | + if it.peeked { |
| 91 | + it.peeked = false |
| 92 | + return it.peekLine, true |
| 93 | + } |
| 94 | + if it.scanner.Scan() { |
| 95 | + return it.scanner.Text(), true |
| 96 | + } |
| 97 | + return "", false |
| 98 | +} |
| 99 | + |
| 100 | +func (it *nativeCommitIter) unread(line string) { |
| 101 | + it.peeked = true |
| 102 | + it.peekLine = line |
| 103 | +} |
| 104 | + |
| 105 | +func (it *nativeCommitIter) Next() (*Commit, error) { |
| 106 | + if it.exhausted { |
| 107 | + return nil, nil |
| 108 | + } |
| 109 | + |
| 110 | + // Scan until we find the sentinel line. |
| 111 | + for { |
| 112 | + line, ok := it.nextLine() |
| 113 | + if !ok { |
| 114 | + it.exhausted = true |
| 115 | + return nil, nil |
| 116 | + } |
| 117 | + if line == "GITANALYTICS_COMMIT" { |
| 118 | + break |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Read 5 metadata lines: hash, name, email, date, subject. |
| 123 | + meta := make([]string, 5) |
| 124 | + for i := range meta { |
| 125 | + line, ok := it.nextLine() |
| 126 | + if !ok { |
| 127 | + return nil, fmt.Errorf("unexpected end of git log output (expected metadata line %d)", i) |
| 128 | + } |
| 129 | + meta[i] = line |
| 130 | + } |
| 131 | + |
| 132 | + date, err := time.Parse(time.RFC3339, meta[3]) |
| 133 | + if err != nil { |
| 134 | + return nil, fmt.Errorf("parsing date %q: %w", meta[3], err) |
| 135 | + } |
| 136 | + |
| 137 | + // Read numstat lines until next sentinel or EOF. |
| 138 | + var files []FileStat |
| 139 | + for { |
| 140 | + line, ok := it.nextLine() |
| 141 | + if !ok { |
| 142 | + it.exhausted = true |
| 143 | + break |
| 144 | + } |
| 145 | + if line == "GITANALYTICS_COMMIT" { |
| 146 | + it.unread(line) |
| 147 | + break |
| 148 | + } |
| 149 | + if line == "" { |
| 150 | + continue |
| 151 | + } |
| 152 | + |
| 153 | + fs, err := parseNumstatLine(line) |
| 154 | + if err != nil { |
| 155 | + continue // skip unparseable lines |
| 156 | + } |
| 157 | + files = append(files, fs) |
| 158 | + } |
| 159 | + |
| 160 | + return &Commit{ |
| 161 | + Hash: meta[0], |
| 162 | + AuthorName: meta[1], |
| 163 | + AuthorEmail: meta[2], |
| 164 | + Date: date, |
| 165 | + Message: meta[4], |
| 166 | + FilesChanged: files, |
| 167 | + }, nil |
| 168 | +} |
| 169 | + |
| 170 | +func (it *nativeCommitIter) Close() { |
| 171 | + if it.cmd != nil && it.cmd.Process != nil { |
| 172 | + it.cmd.Process.Kill() |
| 173 | + it.cmd.Wait() |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +// parseNumstatLine parses a single --numstat output line. |
| 178 | +// Format: "additions\tdeletions\tpath" |
| 179 | +// Binary files show "-\t-\tpath" — treated as 0/0. |
| 180 | +func parseNumstatLine(line string) (FileStat, error) { |
| 181 | + parts := strings.SplitN(line, "\t", 3) |
| 182 | + if len(parts) != 3 { |
| 183 | + return FileStat{}, fmt.Errorf("expected 3 tab-separated fields, got %d", len(parts)) |
| 184 | + } |
| 185 | + |
| 186 | + var additions, deletions int |
| 187 | + if parts[0] != "-" { |
| 188 | + var err error |
| 189 | + additions, err = strconv.Atoi(parts[0]) |
| 190 | + if err != nil { |
| 191 | + return FileStat{}, fmt.Errorf("parsing additions %q: %w", parts[0], err) |
| 192 | + } |
| 193 | + } |
| 194 | + if parts[1] != "-" { |
| 195 | + var err error |
| 196 | + deletions, err = strconv.Atoi(parts[1]) |
| 197 | + if err != nil { |
| 198 | + return FileStat{}, fmt.Errorf("parsing deletions %q: %w", parts[1], err) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return FileStat{ |
| 203 | + Path: parts[2], |
| 204 | + Additions: additions, |
| 205 | + Deletions: deletions, |
| 206 | + }, nil |
| 207 | +} |
0 commit comments