tools: cope with PRs with 1 renamed file
The show-pr command panicked when run against which has only a
renamed file without any modifications.
@@ -85,9 +85,14 @@ func GetModifiedFiles(pr int) (files []ModifiedFile, err error) {
firstParentLines := strings.Split(string(firstParentOut), "\n")
firstParent := firstParentLines[len(firstParentLines)-1]
if firstParent == "" {
+ if len(firstParentLines) > 1 {
firstParent = firstParentLines[len(firstParentLines)-2]
}
+ }
+
+ modifiedFiles := []ModifiedFile{}
+ if firstParent != "" {
oldestAncestorOut, err := exec.Command("git", "rev-list", firstParent+"^^!").Output()
if err != nil {
exitError := err.(*exec.ExitError)
@@ -102,7 +107,6 @@ func GetModifiedFiles(pr int) (files []ModifiedFile, err error) {
return nil, errors.Wrap(err, fmt.Sprintf("could not get files changed in %s: %s",
ref, exitError.Stderr))
}
- modifiedFiles := []ModifiedFile{}
for _, line := range strings.Split(string(out), "\n") {
if line == "" {
continue
@@ -116,6 +120,22 @@ func GetModifiedFiles(pr int) (files []ModifiedFile, err error) {
modifiedFiles = append(modifiedFiles, ModifiedFile{Name: trimmed, Mode: mode})
}
}
+ } else {
+ // Resort to using `git show` to find the changed files
+ out, err := exec.Command("git", "show", "--pretty=", "--name-only", ref).Output()
+ if err != nil {
+ exitError := err.(*exec.ExitError)
+ return nil, errors.Wrap(err, fmt.Sprintf("could not get files changed in %s: %s",
+ ref, exitError.Stderr))
+ }
+ for _, name := range strings.Split(string(out), "\n") {
+ trimmed := strings.TrimSpace(name)
+ if trimmed != "" {
+ modifiedFiles = append(modifiedFiles, ModifiedFile{Name: trimmed, Mode: "?"})
+ }
+ }
+ }
+
return modifiedFiles, nil
}
