cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/git/changes.go
308lines · modecode
| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "fmt" |
| 7 | "log/slog" |
| 8 | "os" |
| 9 | "path" |
| 10 | "strings" |
| 11 | |
| 12 | "golang.org/x/exp/slices" |
| 13 | ) |
| 14 | |
| 15 | type FileStatus rune |
| 16 | |
| 17 | const ( |
| 18 | FileAdded FileStatus = 'A' |
| 19 | FileCopied FileStatus = 'C' |
| 20 | FileDeleted FileStatus = 'D' |
| 21 | FileRenamed FileStatus = 'R' |
| 22 | FileModified FileStatus = 'M' |
| 23 | FileTypeChanged FileStatus = 'T' |
| 24 | ) |
| 25 | |
| 26 | type PathType int |
| 27 | |
| 28 | const ( |
| 29 | Missing PathType = iota |
| 30 | Dir |
| 31 | File |
| 32 | Symlink |
| 33 | ) |
| 34 | |
| 35 | type TypeDiff struct { |
| 36 | Before PathType |
| 37 | After PathType |
| 38 | } |
| 39 | |
| 40 | type BodyDiff struct { |
| 41 | Before []byte |
| 42 | After []byte |
| 43 | ModifiedLines []int |
| 44 | } |
| 45 | |
| 46 | type Path struct { |
| 47 | Name string |
| 48 | SymlinkTarget string |
| 49 | Type PathType |
| 50 | } |
| 51 | |
| 52 | func (p Path) EffectivePath() string { |
| 53 | if p.SymlinkTarget != "" && p.Name != p.SymlinkTarget { |
| 54 | return p.SymlinkTarget |
| 55 | } |
| 56 | return p.Name |
| 57 | } |
| 58 | |
| 59 | type PathDiff struct { |
| 60 | Before Path |
| 61 | After Path |
| 62 | } |
| 63 | |
| 64 | type FileChange struct { |
| 65 | Commits []string |
| 66 | Path PathDiff |
| 67 | Body BodyDiff |
| 68 | } |
| 69 | |
| 70 | func Changes(cmd CommandRunner, cr CommitRangeResults, filter PathFilter) ([]*FileChange, error) { |
| 71 | out, err := cmd("log", "--reverse", "--no-merges", "--first-parent", "--format=%H", "--name-status", cr.String()) |
| 72 | if err != nil { |
| 73 | return nil, fmt.Errorf("failed to get the list of modified files from git: %w", err) |
| 74 | } |
| 75 | |
| 76 | var changes []*FileChange |
| 77 | var commit string |
| 78 | s := bufio.NewScanner(bytes.NewReader(out)) |
| 79 | for s.Scan() { |
| 80 | line := s.Text() |
| 81 | |
| 82 | parts := strings.Split(line, "\t") |
| 83 | |
| 84 | if len(parts) == 0 { |
| 85 | continue |
| 86 | } |
| 87 | |
| 88 | if len(parts) == 1 { |
| 89 | if parts[0] != "" { |
| 90 | commit = parts[0] |
| 91 | } |
| 92 | continue |
| 93 | } |
| 94 | |
| 95 | status := FileStatus(parts[0][0]) |
| 96 | srcPath := parts[1] |
| 97 | dstPath := parts[len(parts)-1] |
| 98 | slog.Debug("Git file change", slog.String("change", parts[0]), slog.String("path", dstPath), slog.String("commit", commit)) |
| 99 | |
| 100 | if !filter.IsPathAllowed(dstPath) { |
| 101 | slog.Debug("Skipping file due to include/exclude rules", slog.String("path", dstPath)) |
| 102 | continue |
| 103 | } |
| 104 | |
| 105 | // ignore directories |
| 106 | // FIXME move all files instead? |
| 107 | if isDir, _ := isDirectoryPath(dstPath); isDir { |
| 108 | slog.Debug("Skipping directory entry change", slog.String("path", dstPath)) |
| 109 | continue |
| 110 | } |
| 111 | |
| 112 | change := getChangeByPath(changes, dstPath) |
| 113 | if change == nil { |
| 114 | beforeType := getTypeForPath(cmd, commit+"^", srcPath) |
| 115 | change = &FileChange{ |
| 116 | Path: PathDiff{ |
| 117 | Before: Path{ |
| 118 | Name: srcPath, |
| 119 | Type: beforeType, |
| 120 | SymlinkTarget: resolveSymlinkTarget(cmd, commit+"^", srcPath, beforeType), |
| 121 | }, |
| 122 | After: Path{ |
| 123 | Name: dstPath, |
| 124 | }, |
| 125 | }, |
| 126 | } |
| 127 | switch status { |
| 128 | case FileAdded: |
| 129 | // newly added file, there's no "BEFORE" version |
| 130 | case FileCopied: |
| 131 | // file copied from other location, there's no "BEFORE" version |
| 132 | case FileDeleted: |
| 133 | // delete file, there's no "AFTER" version |
| 134 | change.Body.Before = getContentAtCommit(cmd, commit+"^", change.Path.Before.SymlinkTarget) |
| 135 | case FileModified: |
| 136 | // modified file, there's both "BEFORE" and "AFTER" |
| 137 | change.Body.Before = getContentAtCommit(cmd, commit+"^", change.Path.Before.SymlinkTarget) |
| 138 | case FileRenamed: |
| 139 | // rename could be only partial so there's both "BEFORE" and "AFTER" |
| 140 | change.Body.Before = getContentAtCommit(cmd, commit+"^", change.Path.Before.SymlinkTarget) |
| 141 | case FileTypeChanged: |
| 142 | // type change, could be file -> dir or symlink -> file |
| 143 | // so there's both "BEFORE" and "AFTER" |
| 144 | change.Body.Before = getContentAtCommit(cmd, commit+"^", change.Path.Before.SymlinkTarget) |
| 145 | default: |
| 146 | slog.Debug("Unknown git change", slog.String("path", dstPath), slog.String("commit", commit), slog.String("change", parts[0])) |
| 147 | } |
| 148 | changes = append(changes, change) |
| 149 | } |
| 150 | change.Commits = append(change.Commits, commit) |
| 151 | } |
| 152 | slog.Debug("Parsed git log", slog.Int("changes", len(changes))) |
| 153 | |
| 154 | for _, change := range changes { |
| 155 | lastCommit := change.Commits[len(change.Commits)-1] |
| 156 | |
| 157 | change.Path.After.Type = getTypeForPath(cmd, lastCommit, change.Path.After.Name) |
| 158 | change.Path.After.SymlinkTarget = resolveSymlinkTarget(cmd, lastCommit, change.Path.After.Name, change.Path.After.Type) |
| 159 | change.Body.After = getContentAtCommit(cmd, lastCommit, change.Path.After.EffectivePath()) |
| 160 | |
| 161 | switch { |
| 162 | case change.Path.Before.Type != Missing && change.Path.After.Type == Symlink: |
| 163 | // file was turned into a symlink, every source line is modification |
| 164 | change.Body.ModifiedLines = CountLines(change.Body.After) |
| 165 | case change.Path.Before.Type != Missing && change.Path.After.Type != Missing && change.Path.After.Type != Symlink: |
| 166 | change.Body.ModifiedLines, err = getModifiedLines(cmd, change.Commits, change.Path.After.EffectivePath(), lastCommit) |
| 167 | if err != nil { |
| 168 | return nil, fmt.Errorf("failed to run git blame for %s: %w", change.Path.After.EffectivePath(), err) |
| 169 | } |
| 170 | case change.Path.Before.Type == Symlink && change.Path.After.Type == Symlink: |
| 171 | // symlink was modified, every source line is modification |
| 172 | change.Body.ModifiedLines = CountLines(change.Body.After) |
| 173 | case change.Path.Before.Type == Missing && change.Path.After.Type != Missing: |
| 174 | // old file body is empty, meaning that every line was modified |
| 175 | change.Body.ModifiedLines = CountLines(change.Body.After) |
| 176 | case change.Path.Before.Type != Missing && change.Path.After.Type == Missing: |
| 177 | // new file body is empty, meaning that every line was modified |
| 178 | change.Body.ModifiedLines = CountLines(change.Body.Before) |
| 179 | default: |
| 180 | slog.Debug("Unhandled change", slog.String("change", fmt.Sprintf("+%v", change))) |
| 181 | } |
| 182 | |
| 183 | if change.Path.Before.Name == change.Path.Before.SymlinkTarget { |
| 184 | change.Path.Before.SymlinkTarget = "" |
| 185 | } |
| 186 | if change.Path.After.Name == change.Path.After.SymlinkTarget { |
| 187 | change.Path.After.SymlinkTarget = "" |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return changes, nil |
| 192 | } |
| 193 | |
| 194 | func getChangeByPath(changes []*FileChange, fpath string) *FileChange { |
| 195 | for _, c := range changes { |
| 196 | if c.Path.After.Name == fpath { |
| 197 | return c |
| 198 | } |
| 199 | } |
| 200 | return nil |
| 201 | } |
| 202 | |
| 203 | func getModifiedLines(cmd CommandRunner, commits []string, fpath, atCommit string) ([]int, error) { |
| 204 | slog.Debug("Getting list of modified lines", slog.String("commits", fmt.Sprint(commits)), slog.String("path", fpath)) |
| 205 | lines, err := Blame(cmd, fpath, atCommit) |
| 206 | if err != nil { |
| 207 | return nil, err |
| 208 | } |
| 209 | |
| 210 | modLines := make([]int, 0, len(lines)) |
| 211 | for _, line := range lines { |
| 212 | if !slices.Contains(commits, line.Commit) { |
| 213 | continue |
| 214 | } |
| 215 | modLines = append(modLines, line.Line) |
| 216 | } |
| 217 | return modLines, nil |
| 218 | } |
| 219 | |
| 220 | func getTypeForPath(cmd CommandRunner, commit, fpath string) PathType { |
| 221 | args := []string{"ls-tree", commit, fpath} |
| 222 | out, err := cmd(args...) |
| 223 | if err != nil { |
| 224 | slog.Debug("git command returned an error", slog.Any("err", err), slog.String("args", fmt.Sprint(args))) |
| 225 | return Missing |
| 226 | } |
| 227 | |
| 228 | s := bufio.NewScanner(bytes.NewReader(out)) |
| 229 | for s.Scan() { |
| 230 | parts := strings.SplitN(s.Text(), " ", 3) |
| 231 | if len(parts) != 3 { |
| 232 | continue |
| 233 | } |
| 234 | objmode := parts[0] |
| 235 | objtype := parts[1] |
| 236 | |
| 237 | parts = strings.SplitN(parts[2], "\t", 2) |
| 238 | if len(parts) != 2 { |
| 239 | continue |
| 240 | } |
| 241 | objpath := parts[1] |
| 242 | slog.Debug("ls-tree line", |
| 243 | slog.String("mode", objmode), |
| 244 | slog.String("type", objtype), |
| 245 | slog.String("path", objpath), |
| 246 | ) |
| 247 | |
| 248 | // not our file |
| 249 | if objpath != fpath { |
| 250 | continue |
| 251 | } |
| 252 | if objtype == "tree" { |
| 253 | return Dir |
| 254 | } |
| 255 | // not a blob - could be a tree or a tag |
| 256 | if objtype != "blob" { |
| 257 | continue |
| 258 | } |
| 259 | |
| 260 | if objmode == "120000" { |
| 261 | return Symlink |
| 262 | } |
| 263 | |
| 264 | return File |
| 265 | } |
| 266 | |
| 267 | return Missing |
| 268 | } |
| 269 | |
| 270 | // recursively find the final target of a symlink. |
| 271 | func resolveSymlinkTarget(cmd CommandRunner, commit, fpath string, typ PathType) string { |
| 272 | if typ != Symlink { |
| 273 | return fpath |
| 274 | } |
| 275 | raw := string(getContentAtCommit(cmd, commit, fpath)) |
| 276 | spath := path.Clean(path.Join(path.Dir(fpath), raw)) |
| 277 | stype := getTypeForPath(cmd, commit, spath) |
| 278 | return resolveSymlinkTarget(cmd, commit, spath, stype) |
| 279 | } |
| 280 | |
| 281 | func getContentAtCommit(cmd CommandRunner, commit, fpath string) []byte { |
| 282 | args := []string{"cat-file", "blob", fmt.Sprintf("%s:%s", commit, fpath)} |
| 283 | body, err := cmd(args...) |
| 284 | if err != nil { |
| 285 | slog.Debug("git command returned an error", slog.Any("err", err), slog.String("args", fmt.Sprint(args))) |
| 286 | return nil |
| 287 | } |
| 288 | return body |
| 289 | } |
| 290 | |
| 291 | func CountLines(body []byte) (lines []int) { |
| 292 | var line int |
| 293 | s := bufio.NewScanner(bytes.NewReader(body)) |
| 294 | for s.Scan() { |
| 295 | line++ |
| 296 | lines = append(lines, line) |
| 297 | } |
| 298 | return lines |
| 299 | } |
| 300 | |
| 301 | func isDirectoryPath(path string) (bool, error) { |
| 302 | fileInfo, err := os.Stat(path) |
| 303 | if err != nil { |
| 304 | return false, err |
| 305 | } |
| 306 | |
| 307 | return fileInfo.IsDir(), err |
| 308 | } |
| 309 | |