| 1 | package gitrepo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Change is één bestand in een diff, zonder de difftekst zelf. |
| 11 | // Status is added, modified, removed of renamed. |
| 12 | type Change struct { |
| 13 | Path string |
| 14 | OldPath string |
| 15 | Status string |
| 16 | Added int |
| 17 | Deleted int |
| 18 | Binary bool |
| 19 | } |
| 20 | |
| 21 | // CommitFileChanges geeft de bestanden die sha tegen zijn eerste parent wijzigt. |
| 22 | // Een root-commit wordt met de lege boom vergeleken. |
| 23 | func CommitFileChanges(ctx context.Context, gitBin, dir, sha string) ([]Change, error) { |
| 24 | base, err := diffBase(ctx, gitBin, dir, sha) |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | return diffChanges(ctx, gitBin, dir, base, sha) |
| 29 | } |
| 30 | |
| 31 | // CompareChanges is de three-dot diff van from...to, zonder de difftekst. |
| 32 | func CompareChanges(ctx context.Context, gitBin, dir, from, to string) ([]Change, error) { |
| 33 | if from == "" || to == "" { |
| 34 | return nil, fmt.Errorf("lege vergelijking") |
| 35 | } |
| 36 | return diffChanges(ctx, gitBin, dir, from+"..."+to) |
| 37 | } |
| 38 | |
| 39 | // CommitDiff is de unified diff van sha tegen zijn eerste parent. |
| 40 | func CommitDiff(ctx context.Context, gitBin, dir, sha string) (string, error) { |
| 41 | base, err := diffBase(ctx, gitBin, dir, sha) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | return unifiedDiff(ctx, gitBin, dir, base, sha) |
| 46 | } |
| 47 | |
| 48 | // CompareDiff is de unified three-dot diff van from...to. |
| 49 | func CompareDiff(ctx context.Context, gitBin, dir, from, to string) (string, error) { |
| 50 | if from == "" || to == "" { |
| 51 | return "", fmt.Errorf("lege vergelijking") |
| 52 | } |
| 53 | return unifiedDiff(ctx, gitBin, dir, from+"..."+to) |
| 54 | } |
| 55 | |
| 56 | func diffBase(ctx context.Context, gitBin, dir, sha string) (string, error) { |
| 57 | out, err := run(ctx, gitBin, nil, "-C", dir, "rev-list", "--parents", "-n", "1", sha) |
| 58 | if err != nil { |
| 59 | return "", err |
| 60 | } |
| 61 | fields := strings.Fields(out) |
| 62 | if len(fields) == 0 { |
| 63 | return "", fmt.Errorf("commit niet gevonden") |
| 64 | } |
| 65 | if len(fields) > 1 { |
| 66 | return fields[1], nil |
| 67 | } |
| 68 | empty, err := run(ctx, gitBin, nil, "hash-object", "-t", "tree", "--stdin") |
| 69 | if err != nil { |
| 70 | return "", err |
| 71 | } |
| 72 | return strings.TrimSpace(empty), nil |
| 73 | } |
| 74 | |
| 75 | func diffChanges(ctx context.Context, gitBin, dir string, revs ...string) ([]Change, error) { |
| 76 | nameRaw, err := gitDiff(ctx, gitBin, dir, append([]string{"--name-status", "--find-renames", "-z"}, revs...)...) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | numRaw, err := gitDiff(ctx, gitBin, dir, append([]string{"--numstat", "--find-renames", "-z"}, revs...)...) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | files := parseNameStatus(nameRaw) |
| 85 | counts, binary := parseNumstat(numRaw) |
| 86 | for i := range files { |
| 87 | key := files[i].Path |
| 88 | if n, ok := counts[key]; ok { |
| 89 | files[i].Added, files[i].Deleted = n[0], n[1] |
| 90 | } |
| 91 | if binary[key] { |
| 92 | files[i].Binary = true |
| 93 | files[i].Added, files[i].Deleted = 0, 0 |
| 94 | } |
| 95 | } |
| 96 | if files == nil { |
| 97 | files = []Change{} |
| 98 | } |
| 99 | return files, nil |
| 100 | } |
| 101 | |
| 102 | func unifiedDiff(ctx context.Context, gitBin, dir string, revs ...string) (string, error) { |
| 103 | return gitDiff(ctx, gitBin, dir, append([]string{"--no-color", "--find-renames", "--unified=3"}, revs...)...) |
| 104 | } |
| 105 | |
| 106 | func gitDiff(ctx context.Context, gitBin, dir string, flags ...string) (string, error) { |
| 107 | args := []string{"-c", "core.quotepath=false", "-C", dir, "diff"} |
| 108 | args = append(args, flags...) |
| 109 | return runAllow(ctx, gitBin, nil, 1, args...) |
| 110 | } |
| 111 | |
| 112 | func parseNameStatus(raw string) []Change { |
| 113 | parts := strings.Split(raw, "\x00") |
| 114 | var out []Change |
| 115 | for i := 0; i < len(parts); { |
| 116 | status := parts[i] |
| 117 | if status == "" { |
| 118 | i++ |
| 119 | continue |
| 120 | } |
| 121 | i++ |
| 122 | if i >= len(parts) { |
| 123 | break |
| 124 | } |
| 125 | if strings.HasPrefix(status, "R") || strings.HasPrefix(status, "C") { |
| 126 | old := parts[i] |
| 127 | i++ |
| 128 | if i >= len(parts) { |
| 129 | break |
| 130 | } |
| 131 | path := parts[i] |
| 132 | i++ |
| 133 | st := "renamed" |
| 134 | if strings.HasPrefix(status, "C") { |
| 135 | st = "modified" |
| 136 | } |
| 137 | out = append(out, Change{Path: path, OldPath: old, Status: st}) |
| 138 | continue |
| 139 | } |
| 140 | out = append(out, Change{Path: parts[i], Status: changeStatus(status)}) |
| 141 | i++ |
| 142 | } |
| 143 | return out |
| 144 | } |
| 145 | |
| 146 | func changeStatus(status string) string { |
| 147 | switch status { |
| 148 | case "A": |
| 149 | return "added" |
| 150 | case "D": |
| 151 | return "removed" |
| 152 | default: |
| 153 | return "modified" |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func parseNumstat(raw string) (map[string][2]int, map[string]bool) { |
| 158 | counts := map[string][2]int{} |
| 159 | binary := map[string]bool{} |
| 160 | for _, rec := range strings.Split(raw, "\x00") { |
| 161 | rec = strings.TrimRight(rec, "\n") |
| 162 | if rec == "" { |
| 163 | continue |
| 164 | } |
| 165 | added, deleted, path, ok := splitNumstat(rec) |
| 166 | if !ok || path == "" { |
| 167 | continue |
| 168 | } |
| 169 | path = numstatPath(path) |
| 170 | if added == "-" || deleted == "-" { |
| 171 | binary[path] = true |
| 172 | continue |
| 173 | } |
| 174 | a, aErr := strconv.Atoi(added) |
| 175 | d, dErr := strconv.Atoi(deleted) |
| 176 | if aErr != nil || dErr != nil { |
| 177 | continue |
| 178 | } |
| 179 | counts[path] = [2]int{a, d} |
| 180 | } |
| 181 | return counts, binary |
| 182 | } |
| 183 | |
| 184 | func splitNumstat(rec string) (added, deleted, path string, ok bool) { |
| 185 | tab := strings.IndexByte(rec, '\t') |
| 186 | if tab < 0 { |
| 187 | return "", "", "", false |
| 188 | } |
| 189 | tab2 := strings.IndexByte(rec[tab+1:], '\t') |
| 190 | if tab2 < 0 { |
| 191 | return "", "", "", false |
| 192 | } |
| 193 | tab2 += tab + 1 |
| 194 | return rec[:tab], rec[tab+1 : tab2], rec[tab2+1:], true |
| 195 | } |
| 196 | |
| 197 | func numstatPath(path string) string { |
| 198 | brace := strings.Index(path, "{") |
| 199 | arrow := strings.Index(path, " => ") |
| 200 | end := strings.Index(path, "}") |
| 201 | if brace >= 0 && arrow > brace && end > arrow { |
| 202 | return path[:brace] + path[arrow+4:end] + path[end+1:] |
| 203 | } |
| 204 | if arrow >= 0 { |
| 205 | return path[arrow+4:] |
| 206 | } |
| 207 | return path |
| 208 | } |