@@ -85,37 +85,57 @@ func GetModifiedFiles(pr int) (files []ModifiedFile, err error) {
85
85
firstParentLines := strings .Split (string (firstParentOut ), "\n " )
86
86
firstParent := firstParentLines [len (firstParentLines )- 1 ]
87
87
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
+ }
96
91
}
97
- oldestAncestor := strings .TrimSpace (string (oldestAncestorOut ))
98
92
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
- }
105
93
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 ))
109
101
}
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
+ }
117
136
}
118
137
}
138
+
119
139
return modifiedFiles , nil
120
140
}
121
141
0 commit comments