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/vcs/git.go | |
| parent | 9ee25c60da3366005e31dedd1574732f63338151 (diff) | |
pkg/vcs: extend ParseGitDiff
Return not just the modified files, but also their blob hashes.
Diffstat (limited to 'pkg/vcs/git.go')
| -rw-r--r-- | pkg/vcs/git.go | 30 |
1 files changed, 24 insertions, 6 deletions
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 { |
