diff --git a/services/pull/merge_prepare.go b/services/pull/merge_prepare.go index fc70da10a4..4598d57b7a 100644 --- a/services/pull/merge_prepare.go +++ b/services/pull/merge_prepare.go @@ -249,6 +249,11 @@ func rebaseTrackingOnToBase(ctx *mergeContext, mergeStyle repo_model.MergeStyle) ctx.outbuf.Reset() ctx.errbuf.Reset() + // If the pull request is zero commits behind, then no rebasing needs to be done. + if ctx.pr.CommitsBehind == 0 { + return nil + } + // Check git version for availability of git-replay. If it is available, we use // it for performance and to preserve unknown commit headers like the // "change-id" header used by Jujutsu and GitButler to track changes across diff --git a/tests/integration/pull_merge_test.go b/tests/integration/pull_merge_test.go index cca2381fd4..ab3d1604de 100644 --- a/tests/integration/pull_merge_test.go +++ b/tests/integration/pull_merge_test.go @@ -1135,3 +1135,32 @@ func TestPullDeleteBranchPerms(t *testing.T) { user4Session.MakeRequest(t, req, http.StatusOK) }) } + +// Test that rebasing only happens when its necessary. +func TestRebaseWhenNecessary(t *testing.T) { + onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { + session := loginUser(t, "user1") + testRepoFork(t, session, "user2", "repo1", "user1", "repo1") + testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n") + + resp := testPullCreate(t, session, "user1", "repo1", false, "master", "master", "This is a pull title") + pullLink := test.RedirectURL(resp) + + resp = session.MakeRequest(t, NewRequest(t, "GET", test.RedirectURL(resp)+"/commits"), http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + commitLinkBefore, ok := htmlDoc.Find("a.sha").Attr("href") + assert.True(t, ok) + commitBefore := commitLinkBefore[strings.LastIndexByte(commitLinkBefore, '/'):] + + elem := strings.Split(pullLink, "/") + testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleRebase, false) + + resp = session.MakeRequest(t, NewRequest(t, "GET", "/user2/repo1"), http.StatusOK) + htmlDoc = NewHTMLParser(t, resp.Body) + commitLinkAfter, ok := htmlDoc.Find(".latest-commit a.sha").Attr("href") + assert.True(t, ok) + commitAfter := commitLinkAfter[strings.LastIndexByte(commitLinkAfter, '/'):] + + assert.Equal(t, commitBefore, commitAfter) + }) +}