Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file rename handling in git #116

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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