Skip to content

Commit cef8989

Browse files
Merge pull request #760 from dhellmann/twie-fix-summary-panic
tools: cope with PRs with 1 renamed file
2 parents be76e85 + c82f119 commit cef8989

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

tools/enhancements/summary.go

+45-25
Original file line numberDiff line numberDiff line change
@@ -85,37 +85,57 @@ func GetModifiedFiles(pr int) (files []ModifiedFile, err error) {
8585
firstParentLines := strings.Split(string(firstParentOut), "\n")
8686
firstParent := firstParentLines[len(firstParentLines)-1]
8787
if firstParent == "" {
88-
firstParent = firstParentLines[len(firstParentLines)-2]
89-
}
90-
91-
oldestAncestorOut, err := exec.Command("git", "rev-list", firstParent+"^^!").Output()
92-
if err != nil {
93-
exitError := err.(*exec.ExitError)
94-
return nil, errors.Wrap(err, fmt.Sprintf("could not get files changed in %s: %s",
95-
ref, exitError.Stderr))
88+
if len(firstParentLines) > 1 {
89+
firstParent = firstParentLines[len(firstParentLines)-2]
90+
}
9691
}
97-
oldestAncestor := strings.TrimSpace(string(oldestAncestorOut))
9892

99-
out, err := exec.Command("git", "diff", "--name-status", oldestAncestor+".."+ref).Output()
100-
if err != nil {
101-
exitError := err.(*exec.ExitError)
102-
return nil, errors.Wrap(err, fmt.Sprintf("could not get files changed in %s: %s",
103-
ref, exitError.Stderr))
104-
}
10593
modifiedFiles := []ModifiedFile{}
106-
for _, line := range strings.Split(string(out), "\n") {
107-
if line == "" {
108-
continue
94+
95+
if firstParent != "" {
96+
oldestAncestorOut, err := exec.Command("git", "rev-list", firstParent+"^^!").Output()
97+
if err != nil {
98+
exitError := err.(*exec.ExitError)
99+
return nil, errors.Wrap(err, fmt.Sprintf("could not get files changed in %s: %s",
100+
ref, exitError.Stderr))
109101
}
110-
// The diff output shows the operation (mode) and filename:
111-
// A name-of-new-file
112-
// C name-of-changed-file
113-
mode := line[0:1]
114-
trimmed := strings.TrimSpace(line[1:])
115-
if trimmed != "" {
116-
modifiedFiles = append(modifiedFiles, ModifiedFile{Name: trimmed, Mode: mode})
102+
oldestAncestor := strings.TrimSpace(string(oldestAncestorOut))
103+
104+
out, err := exec.Command("git", "diff", "--name-status", oldestAncestor+".."+ref).Output()
105+
if err != nil {
106+
exitError := err.(*exec.ExitError)
107+
return nil, errors.Wrap(err, fmt.Sprintf("could not get files changed in %s: %s",
108+
ref, exitError.Stderr))
109+
}
110+
for _, line := range strings.Split(string(out), "\n") {
111+
if line == "" {
112+
continue
113+
}
114+
// The diff output shows the operation (mode) and filename:
115+
// A name-of-new-file
116+
// C name-of-changed-file
117+
mode := line[0:1]
118+
trimmed := strings.TrimSpace(line[1:])
119+
if trimmed != "" {
120+
modifiedFiles = append(modifiedFiles, ModifiedFile{Name: trimmed, Mode: mode})
121+
}
122+
}
123+
} else {
124+
// Resort to using `git show` to find the changed files
125+
out, err := exec.Command("git", "show", "--pretty=", "--name-only", ref).Output()
126+
if err != nil {
127+
exitError := err.(*exec.ExitError)
128+
return nil, errors.Wrap(err, fmt.Sprintf("could not get files changed in %s: %s",
129+
ref, exitError.Stderr))
130+
}
131+
for _, name := range strings.Split(string(out), "\n") {
132+
trimmed := strings.TrimSpace(name)
133+
if trimmed != "" {
134+
modifiedFiles = append(modifiedFiles, ModifiedFile{Name: trimmed, Mode: "?"})
135+
}
117136
}
118137
}
138+
119139
return modifiedFiles, nil
120140
}
121141

0 commit comments

Comments
 (0)