Skip to content

Commit

Permalink
Merge pull request cloudflare#116 from cloudflare/ci-rename
Browse files Browse the repository at this point in the history
Fix file rename handling in git
  • Loading branch information
prymitive authored Jan 17, 2022
2 parents 1834dbb + a6cd310 commit 697bfb6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.6.6

### Fixed

- File renames were not handled correctly when running `git ci` on branches with
multiple commits.

## v0.6.5

### Added
Expand Down
26 changes: 18 additions & 8 deletions internal/discovery/git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (gd GitBranchFileFinder) Find(pattern ...string) (FileFindResults, error) {

log.Debug().Str("from", cr.From).Str("to", cr.To).Msg("Got commit range from git")

out, err := gd.gitCmd("log", "--no-merges", "--pretty=format:%H", "--name-status", "--diff-filter=d", cr.String())
out, err := gd.gitCmd("log", "--reverse", "--no-merges", "--pretty=format:%H", "--name-status", "--diff-filter=d", cr.String())
if err != nil {
return nil, err
}
Expand All @@ -42,19 +42,29 @@ func (gd GitBranchFileFinder) Find(pattern ...string) (FileFindResults, error) {
if len(parts) == 1 && parts[0] != "" {
commit = parts[0]
} else if len(parts) >= 2 {
path := parts[len(parts)-1]
op := parts[0]
srcPath := parts[1]
dstPath := parts[len(parts)-1]
log.Debug().
Str("path", path).
Str("path", dstPath).
Str("commit", commit).
Bool("allowed", gd.isPathAllowed(path)).
Bool("allowed", gd.isPathAllowed(dstPath)).
Msg("Git file change")
if !gd.isPathAllowed(path) {
if !gd.isPathAllowed(dstPath) {
continue
}
if _, ok := results.pathCommits[path]; !ok {
results.pathCommits[path] = []string{}
if _, ok := results.pathCommits[dstPath]; !ok {
results.pathCommits[dstPath] = []string{}
}
results.pathCommits[path] = append(results.pathCommits[path], commit)
// check if we're dealing with a rename and if so we need to
// rename results in pathCommits
if strings.HasPrefix(op, "R") {
if v, ok := results.pathCommits[srcPath]; ok {
results.pathCommits[dstPath] = append(results.pathCommits[dstPath], v...)
delete(results.pathCommits, srcPath)
}
}
results.pathCommits[dstPath] = append(results.pathCommits[dstPath], commit)
}
}

Expand Down
25 changes: 25 additions & 0 deletions internal/discovery/git_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ C50 foo/bar/cp1.txt foo/cp1.del
{Path: "foo/1.txt", Commits: []string{"commit1"}},
},
},
{
detector: discovery.NewGitBranchFileFinder(func(args ...string) ([]byte, error) {
if args[0] == "rev-list" {
return []byte("commit1\ncommit3\n"), nil
}
content := `commit1
M file1
M file2
commit2
M file1
M file3
commit3
R100 file1 file4
M file2
`
return []byte([]byte(content)), nil
}, nil, "main"),
output: []discovery.File{
{Path: "file2", Commits: []string{"commit1", "commit3"}},
{Path: "file3", Commits: []string{"commit2"}},
{Path: "file4", Commits: []string{"commit1", "commit2", "commit3"}},
},
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 697bfb6

Please sign in to comment.