Skip to content

Commit ef1a9c9

Browse files
committed
Handle conflicts
1 parent 1508322 commit ef1a9c9

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

services/pull/patch.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ func DownloadDiffOrPatch(pr *models.PullRequest, w io.Writer, patch bool) error
6161
return nil
6262
}
6363

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+
6471
// TestPatch will test whether a simple patch will apply
6572
func TestPatch(pr *models.PullRequest) error {
6673
// Clone base repo.
@@ -150,22 +157,36 @@ func TestPatch(pr *models.PullRequest) error {
150157
_ = stderrReader.Close()
151158
_ = stderrWriter.Close()
152159
}()
160+
conflict := false
153161
err = git.NewCommand(args...).
154162
RunInDirTimeoutEnvFullPipelineFunc(
155163
nil, -1, tmpBasePath,
156164
nil, stderrWriter, nil,
157165
func(ctx context.Context, cancel context.CancelFunc) {
158166
_ = stderrWriter.Close()
159167
const prefix = "error: patch failed:"
168+
const errorPrefix = "error: "
160169
conflictMap := map[string]bool{}
161170

162171
scanner := bufio.NewScanner(stderrReader)
163172
for scanner.Scan() {
164173
line := scanner.Text()
165-
174+
fmt.Printf("%s\n", line)
166175
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])
168178
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+
}
169190
}
170191
// only list 10 conflicted files
171192
if len(conflictMap) >= 10 {
@@ -177,17 +198,16 @@ func TestPatch(pr *models.PullRequest) error {
177198
for key := range conflictMap {
178199
pr.ConflictedFiles = append(pr.ConflictedFiles, key)
179200
}
180-
pr.Status = models.PullRequestStatusConflict
181201
}
182202
_ = stderrReader.Close()
183203
})
184204

185205
if err != nil {
186-
if len(pr.ConflictedFiles) > 0 {
206+
if conflict {
207+
pr.Status = models.PullRequestStatusConflict
187208
log.Trace("Found %d files conflicted: %v", len(pr.ConflictedFiles), pr.ConflictedFiles)
188209
return nil
189210
}
190-
191211
return fmt.Errorf("git apply --check: %v", err)
192212
}
193213
pr.Status = models.PullRequestStatusMergeable

0 commit comments

Comments
 (0)