Skip to content

Commit eb69c7e

Browse files
jolheiserlunny
andauthored
Allow default branch to be inferred on compare page (#17908)
* Allow default branch to be inferred Signed-off-by: jolheiser <[email protected]> * Add test for inferred default branch Signed-off-by: jolheiser <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 12a42ba commit eb69c7e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

integrations/compare_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ func TestCompareTag(t *testing.T) {
2727
resp = session.MakeRequest(t, req, http.StatusNotFound)
2828
assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500")
2929
}
30+
31+
// Compare with inferred default branch (master)
32+
func TestCompareDefault(t *testing.T) {
33+
defer prepareTestEnv(t)()
34+
35+
session := loginUser(t, "user2")
36+
req := NewRequest(t, "GET", "/user2/repo1/compare/v1.1")
37+
resp := session.MakeRequest(t, req, http.StatusOK)
38+
htmlDoc := NewHTMLParser(t, resp.Body)
39+
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
40+
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
41+
}

routers/web/repo/compare.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
186186
// 1. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headBranch}
187187
// 2. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}:{:headBranch}
188188
// 3. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}/{:headRepoName}:{:headBranch}
189+
// 4. /{:baseOwner}/{:baseRepoName}/compare/{:headBranch}
190+
// 5. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}:{:headBranch}
191+
// 6. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}/{:headRepoName}:{:headBranch}
189192
//
190193
// Here we obtain the infoPath "{:baseBranch}...[{:headOwner}/{:headRepoName}:]{:headBranch}" as ctx.Params("*")
191194
// with the :baseRepo in ctx.Repo.
@@ -213,9 +216,12 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
213216
infos := strings.SplitN(infoPath, "...", 2)
214217

215218
if len(infos) != 2 {
216-
infos = strings.SplitN(infoPath, "..", 2)
217-
ci.DirectComparison = true
218-
ctx.Data["PageIsComparePull"] = false
219+
infos = []string{baseRepo.DefaultBranch, infoPath}
220+
if strings.Contains(infoPath, "..") {
221+
infos = strings.SplitN(infoPath, "..", 2)
222+
ci.DirectComparison = true
223+
ctx.Data["PageIsComparePull"] = false
224+
}
219225
}
220226

221227
if len(infos) != 2 {

0 commit comments

Comments
 (0)