Skip to content

Commit c491c22

Browse files
typelesstechknowlogick
authored andcommitted
Fix pull creation with empty changes (#7920) (#7926)
* Logs the stderr of git-apply * Add an integration test * Skip testPatch when patch is empty
1 parent 5649f0d commit c491c22

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

Diff for: integrations/pull_status_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"path"
11+
"strings"
1112
"testing"
1213

1314
"code.gitea.io/gitea/models"
@@ -93,3 +94,28 @@ func TestPullCreate_CommitStatus(t *testing.T) {
9394
}
9495
})
9596
}
97+
98+
func TestPullCreate_EmptyChangesWithCommits(t *testing.T) {
99+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
100+
session := loginUser(t, "user1")
101+
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
102+
testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
103+
testEditFileToNewBranch(t, session, "user1", "repo1", "status1", "status1", "README.md", "# repo1\n\nDescription for repo1")
104+
105+
url := path.Join("user1", "repo1", "compare", "master...status1")
106+
req := NewRequestWithValues(t, "POST", url,
107+
map[string]string{
108+
"_csrf": GetCSRF(t, session, url),
109+
"title": "pull request from status1",
110+
},
111+
)
112+
session.MakeRequest(t, req, http.StatusFound)
113+
114+
req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
115+
resp := session.MakeRequest(t, req, http.StatusOK)
116+
doc := NewHTMLParser(t, resp.Body)
117+
118+
text := strings.TrimSpace(doc.doc.Find(".item.text.green").Text())
119+
assert.EqualValues(t, "This pull request can be merged automatically.", text)
120+
})
121+
}

Diff for: models/pull.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func (pr *PullRequest) testPatch(e Engine) (err error) {
598598
if err != nil {
599599
for i := range patchConflicts {
600600
if strings.Contains(stderr, patchConflicts[i]) {
601-
log.Trace("PullRequest[%d].testPatch (apply): has conflict", pr.ID)
601+
log.Trace("PullRequest[%d].testPatch (apply): has conflict: %s", pr.ID, stderr)
602602
const prefix = "error: patch failed:"
603603
pr.Status = PullRequestStatusConflict
604604
pr.ConflictedFiles = make([]string, 0, 5)
@@ -661,13 +661,16 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
661661
}
662662

663663
pr.Index = pull.Index
664-
if err = repo.savePatch(sess, pr.Index, patch); err != nil {
665-
return fmt.Errorf("SavePatch: %v", err)
666-
}
667-
668664
pr.BaseRepo = repo
669-
if err = pr.testPatch(sess); err != nil {
670-
return fmt.Errorf("testPatch: %v", err)
665+
pr.Status = PullRequestStatusChecking
666+
if len(patch) > 0 {
667+
if err = repo.savePatch(sess, pr.Index, patch); err != nil {
668+
return fmt.Errorf("SavePatch: %v", err)
669+
}
670+
671+
if err = pr.testPatch(sess); err != nil {
672+
return fmt.Errorf("testPatch: %v", err)
673+
}
671674
}
672675
// No conflict appears after test means mergeable.
673676
if pr.Status == PullRequestStatusChecking {

0 commit comments

Comments
 (0)