diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2026-01-06 19:10:53 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2026-01-09 14:28:59 +0000 |
| commit | a45480436715f68eac7cc949da964ee2b73ef172 (patch) | |
| tree | a78ef367de0d6ccd99e45571eee6029d7c5dedae /pkg | |
| parent | 9ee25c60da3366005e31dedd1574732f63338151 (diff) | |
pkg/vcs: extend ParseGitDiff
Return not just the modified files, but also their blob hashes.
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/manager/diff.go | 4 | ||||
| -rw-r--r-- | pkg/vcs/git.go | 30 | ||||
| -rw-r--r-- | pkg/vcs/git_test.go | 27 |
3 files changed, 49 insertions, 12 deletions
diff --git a/pkg/manager/diff.go b/pkg/manager/diff.go index 4ad759244..88710a59b 100644 --- a/pkg/manager/diff.go +++ b/pkg/manager/diff.go @@ -870,7 +870,9 @@ func affectedFiles(cfg *mgrconfig.Config, gitPatches [][]byte) (direct, transiti transitiveMap := make(map[string]struct{}) var allFiles []string for _, patch := range gitPatches { - allFiles = append(allFiles, vcs.ParseGitDiff(patch)...) + for _, diff := range vcs.ParseGitDiff(patch) { + allFiles = append(allFiles, diff.Name) + } } for _, file := range allFiles { directMap[file] = struct{}{} diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index 72b4a32e6..a2975c034 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -578,15 +578,33 @@ func (git *gitRepo) PushCommit(repo, commit string) error { return nil } -var fileNameRe = regexp.MustCompile(`(?m)^diff.* b\/([^\s]+)`) +var ( + fileNameRe = regexp.MustCompile(`^diff.* b\/([^\s]+)$`) + indexRe = regexp.MustCompile(`^index (\w+)\.\.(?:\w+)(?:\s\d+)?$`) +) + +type ModifiedFile struct { + Name string + LeftHash string +} // ParseGitDiff extracts the files modified in the git patch. -func ParseGitDiff(patch []byte) []string { - var files []string - for _, match := range fileNameRe.FindAllStringSubmatch(string(patch), -1) { - files = append(files, match[1]) +func ParseGitDiff(patch []byte) []ModifiedFile { + var ret []ModifiedFile + scanner := bufio.NewScanner(bytes.NewReader(patch)) + for scanner.Scan() { + line := scanner.Text() + indexMatch := indexRe.FindStringSubmatch(line) + if indexMatch != nil && len(ret) > 0 { + ret[len(ret)-1].LeftHash = indexMatch[1] + } else { + fileNameMatch := fileNameRe.FindStringSubmatch(line) + if fileNameMatch != nil { + ret = append(ret, ModifiedFile{Name: fileNameMatch[1]}) + } + } } - return files + return ret } type Git struct { diff --git a/pkg/vcs/git_test.go b/pkg/vcs/git_test.go index 40207898c..f2d761713 100644 --- a/pkg/vcs/git_test.go +++ b/pkg/vcs/git_test.go @@ -455,7 +455,7 @@ func TestGitFetchShortHash(t *testing.T) { } func TestParseGitDiff(t *testing.T) { - files := ParseGitDiff([]byte(`diff --git a/a.txt b/a.txt + list := ParseGitDiff([]byte(`diff --git a/a.txt b/a.txt index 4c5fd91..8fe1e32 100644 --- a/a.txt +++ b/a.txt @@ -469,9 +469,26 @@ index 0000000..f8a9677 +++ b/b.txt @@ -0,0 +1 @@ +Second file. -diff --git a/c/c.txt b/c/c.txt -new file mode 100644 -index 0000000..e69de29 +diff --git a/c.txt b/c.txt +deleted file mode 100644 +index f70f10e..0000000 +--- a/c.txt ++++ /dev/null +@@ -1 +0,0 @@ +-A `)) - assert.ElementsMatch(t, files, []string{"a.txt", "b.txt", "c/c.txt"}) + assert.Equal(t, list, []ModifiedFile{ + { + Name: `a.txt`, + LeftHash: `4c5fd91`, + }, + { + Name: `b.txt`, + LeftHash: `0000000`, + }, + { + Name: `c.txt`, + LeftHash: `f70f10e`, + }, + }) } |
