@@ -61,6 +61,13 @@ func DownloadDiffOrPatch(pr *models.PullRequest, w io.Writer, patch bool) error
61
61
return nil
62
62
}
63
63
64
+ var patchErrorSuffices = []string {
65
+ ": already exists in index" ,
66
+ ": patch does not apply" ,
67
+ ": already exists in working directory" ,
68
+ "unrecognized input" ,
69
+ }
70
+
64
71
// TestPatch will test whether a simple patch will apply
65
72
func TestPatch (pr * models.PullRequest ) error {
66
73
// Clone base repo.
@@ -150,22 +157,36 @@ func TestPatch(pr *models.PullRequest) error {
150
157
_ = stderrReader .Close ()
151
158
_ = stderrWriter .Close ()
152
159
}()
160
+ conflict := false
153
161
err = git .NewCommand (args ... ).
154
162
RunInDirTimeoutEnvFullPipelineFunc (
155
163
nil , - 1 , tmpBasePath ,
156
164
nil , stderrWriter , nil ,
157
165
func (ctx context.Context , cancel context.CancelFunc ) {
158
166
_ = stderrWriter .Close ()
159
167
const prefix = "error: patch failed:"
168
+ const errorPrefix = "error: "
160
169
conflictMap := map [string ]bool {}
161
170
162
171
scanner := bufio .NewScanner (stderrReader )
163
172
for scanner .Scan () {
164
173
line := scanner .Text ()
165
-
174
+ fmt . Printf ( "%s \n " , line )
166
175
if strings .HasPrefix (line , prefix ) {
167
- var filepath = strings .TrimSpace (strings .Split (line [len (prefix ):], ":" )[0 ])
176
+ conflict = true
177
+ filepath := strings .TrimSpace (strings .Split (line [len (prefix ):], ":" )[0 ])
168
178
conflictMap [filepath ] = true
179
+ } else if strings .HasPrefix (line , errorPrefix ) {
180
+ for _ , suffix := range patchErrorSuffices {
181
+ if strings .HasSuffix (line , suffix ) {
182
+ conflict = true
183
+ filepath := strings .TrimSpace (strings .TrimSuffix (line [len (errorPrefix ):], suffix ))
184
+ if filepath != "" {
185
+ conflictMap [filepath ] = true
186
+ }
187
+ break
188
+ }
189
+ }
169
190
}
170
191
// only list 10 conflicted files
171
192
if len (conflictMap ) >= 10 {
@@ -177,17 +198,16 @@ func TestPatch(pr *models.PullRequest) error {
177
198
for key := range conflictMap {
178
199
pr .ConflictedFiles = append (pr .ConflictedFiles , key )
179
200
}
180
- pr .Status = models .PullRequestStatusConflict
181
201
}
182
202
_ = stderrReader .Close ()
183
203
})
184
204
185
205
if err != nil {
186
- if len (pr .ConflictedFiles ) > 0 {
206
+ if conflict {
207
+ pr .Status = models .PullRequestStatusConflict
187
208
log .Trace ("Found %d files conflicted: %v" , len (pr .ConflictedFiles ), pr .ConflictedFiles )
188
209
return nil
189
210
}
190
-
191
211
return fmt .Errorf ("git apply --check: %v" , err )
192
212
}
193
213
pr .Status = models .PullRequestStatusMergeable
0 commit comments