| 1 |
package gitfeedback |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"os/exec" |
| 7 |
"path/filepath" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
maxFiles = 50 |
| 13 |
maxDiffSize = 12_000 |
| 14 |
diffTruncatedMarker = "... diff truncated ..." |
| 15 |
) |
| 16 |
|
| 17 |
// Summary captures git-backed host feedback for the last command. |
| 18 |
type Summary struct { |
| 19 |
ChangedFiles []string |
| 20 |
Diff string |
| 21 |
} |
| 22 |
|
| 23 |
// Baseline records whether a command started from a clean git worktree. |
| 24 |
type Baseline struct { |
| 25 |
root string |
| 26 |
clean bool |
| 27 |
} |
| 28 |
|
| 29 |
// Detect records the git baseline for a working directory. |
| 30 |
func Detect(dir string) Baseline { |
| 31 |
root, ok := gitTopLevel(dir) |
| 32 |
if !ok { |
| 33 |
return Baseline{} |
| 34 |
} |
| 35 |
status, ok := gitStatusPorcelain(root) |
| 36 |
if !ok { |
| 37 |
return Baseline{} |
| 38 |
} |
| 39 |
return Baseline{root: root, clean: len(status) == 0} |
| 40 |
} |
| 41 |
|
| 42 |
// Collect returns rename-aware feedback relative to finalCwd. |
| 43 |
func (b Baseline) Collect(finalCwd string) (Summary, bool) { |
| 44 |
if b.root == "" || !b.clean { |
| 45 |
return Summary{}, false |
| 46 |
} |
| 47 |
|
| 48 |
status, ok := gitStatusPorcelain(b.root) |
| 49 |
if !ok { |
| 50 |
return Summary{}, false |
| 51 |
} |
| 52 |
diff, ok := gitCombinedDiff(b.root) |
| 53 |
if !ok { |
| 54 |
return Summary{}, false |
| 55 |
} |
| 56 |
|
| 57 |
files := parseStatusChangedFiles(status, b.root, finalCwd) |
| 58 |
files, diff = truncateSummary(files, diff) |
| 59 |
|
| 60 |
if len(files) == 0 && diff == "" { |
| 61 |
return Summary{}, false |
| 62 |
} |
| 63 |
return Summary{ChangedFiles: files, Diff: diff}, true |
| 64 |
} |
| 65 |
|
| 66 |
func gitTopLevel(dir string) (string, bool) { |
| 67 |
out, ok := gitOutput(dir, "rev-parse", "--show-toplevel") |
| 68 |
if !ok { |
| 69 |
return "", false |
| 70 |
} |
| 71 |
return strings.TrimSpace(string(out)), true |
| 72 |
} |
| 73 |
|
| 74 |
func gitStatusPorcelain(dir string) ([]byte, bool) { |
| 75 |
return gitOutput(dir, "status", "--porcelain", "-z", "--untracked-files=all") |
| 76 |
} |
| 77 |
|
| 78 |
func gitCombinedDiff(dir string) (string, bool) { |
| 79 |
unstaged, ok := gitOutput(dir, "diff", "--no-ext-diff", "--unified=3") |
| 80 |
if !ok { |
| 81 |
return "", false |
| 82 |
} |
| 83 |
cached, ok := gitOutput(dir, "diff", "--cached", "--no-ext-diff", "--unified=3") |
| 84 |
if !ok { |
| 85 |
return "", false |
| 86 |
} |
| 87 |
|
| 88 |
var parts []string |
| 89 |
if len(unstaged) > 0 { |
| 90 |
parts = append(parts, string(unstaged)) |
| 91 |
} |
| 92 |
if len(cached) > 0 { |
| 93 |
parts = append(parts, string(cached)) |
| 94 |
} |
| 95 |
return strings.Join(parts, "\n"), true |
| 96 |
} |
| 97 |
|
| 98 |
func gitOutput(dir string, args ...string) ([]byte, bool) { |
| 99 |
out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).Output() |
| 100 |
return out, err == nil |
| 101 |
} |
| 102 |
|
| 103 |
func parseStatusChangedFiles(status []byte, repoRoot, finalCwd string) []string { |
| 104 |
var files []string |
| 105 |
seen := make(map[string]bool) |
| 106 |
entries := bytes.Split(status, []byte{0}) |
| 107 |
|
| 108 |
for i := 0; i < len(entries); i++ { |
| 109 |
entry := entries[i] |
| 110 |
if len(entry) == 0 || len(entry) < 4 { |
| 111 |
continue |
| 112 |
} |
| 113 |
|
| 114 |
code := string(entry[:2]) |
| 115 |
paths := []string{string(entry[3:])} |
| 116 |
if (strings.Contains(code, "R") || strings.Contains(code, "C")) && i+1 < len(entries) && |
| 117 |
len(entries[i+1]) > 0 { |
| 118 |
paths = append(paths, string(entries[i+1])) |
| 119 |
i++ |
| 120 |
} |
| 121 |
|
| 122 |
for _, repoRel := range paths { |
| 123 |
normalized := normalizePath(repoRoot, finalCwd, repoRel) |
| 124 |
if normalized == "" || seen[normalized] { |
| 125 |
continue |
| 126 |
} |
| 127 |
seen[normalized] = true |
| 128 |
files = append(files, normalized) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return files |
| 133 |
} |
| 134 |
|
| 135 |
func normalizePath(repoRoot, finalCwd, repoRel string) string { |
| 136 |
absolute := filepath.Join(repoRoot, filepath.FromSlash(repoRel)) |
| 137 |
relative, err := filepath.Rel(finalCwd, absolute) |
| 138 |
if err != nil { |
| 139 |
return filepath.Clean(absolute) |
| 140 |
} |
| 141 |
return filepath.Clean(relative) |
| 142 |
} |
| 143 |
|
| 144 |
func truncateSummary(files []string, diff string) ([]string, string) { |
| 145 |
note := "" |
| 146 |
if len(files) > maxFiles { |
| 147 |
remaining := len(files) - maxFiles |
| 148 |
files = append([]string(nil), files[:maxFiles]...) |
| 149 |
note = fmt.Sprintf("... (%d more files omitted)", remaining) |
| 150 |
} |
| 151 |
if note != "" { |
| 152 |
diff = appendDiffNote(diff, note) |
| 153 |
} else if len(diff) > maxDiffSize { |
| 154 |
diff = diff[:maxDiffSize] + "\n" + diffTruncatedMarker |
| 155 |
} |
| 156 |
return files, diff |
| 157 |
} |
| 158 |
|
| 159 |
func appendDiffNote(diff, note string) string { |
| 160 |
if diff == "" { |
| 161 |
return note |
| 162 |
} |
| 163 |
|
| 164 |
suffix := "\n" + note |
| 165 |
if len(diff)+len(suffix) <= maxDiffSize { |
| 166 |
return diff + suffix |
| 167 |
} |
| 168 |
|
| 169 |
budget := maxDiffSize - len("\n"+diffTruncatedMarker+suffix) |
| 170 |
if budget < 0 { |
| 171 |
budget = 0 |
| 172 |
} |
| 173 |
diff = strings.TrimRight(diff[:budget], "\n") |
| 174 |
if diff == "" { |
| 175 |
return diffTruncatedMarker + suffix |
| 176 |
} |
| 177 |
return diff + "\n" + diffTruncatedMarker + suffix |
| 178 |
} |