Skip to content

Commit

Permalink
Allow default branch to be inferred on compare page (go-gitea#17908)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
2 people authored and Stelios Malathouras committed Mar 28, 2022
1 parent 9b2887c commit cc16648
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 12 additions & 0 deletions integrations/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ func TestCompareTag(t *testing.T) {
resp = session.MakeRequest(t, req, http.StatusNotFound)
assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500")
}

// Compare with inferred default branch (master)
func TestCompareDefault(t *testing.T) {
defer prepareTestEnv(t)()

session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/user2/repo1/compare/v1.1")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
}
12 changes: 9 additions & 3 deletions routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
// 1. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headBranch}
// 2. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}:{:headBranch}
// 3. /{:baseOwner}/{:baseRepoName}/compare/{:baseBranch}...{:headOwner}/{:headRepoName}:{:headBranch}
// 4. /{:baseOwner}/{:baseRepoName}/compare/{:headBranch}
// 5. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}:{:headBranch}
// 6. /{:baseOwner}/{:baseRepoName}/compare/{:headOwner}/{:headRepoName}:{:headBranch}
//
// Here we obtain the infoPath "{:baseBranch}...[{:headOwner}/{:headRepoName}:]{:headBranch}" as ctx.Params("*")
// with the :baseRepo in ctx.Repo.
Expand Down Expand Up @@ -213,9 +216,12 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
infos := strings.SplitN(infoPath, "...", 2)

if len(infos) != 2 {
infos = strings.SplitN(infoPath, "..", 2)
ci.DirectComparison = true
ctx.Data["PageIsComparePull"] = false
infos = []string{baseRepo.DefaultBranch, infoPath}
if strings.Contains(infoPath, "..") {
infos = strings.SplitN(infoPath, "..", 2)
ci.DirectComparison = true
ctx.Data["PageIsComparePull"] = false
}
}

if len(infos) != 2 {
Expand Down

0 comments on commit cc16648

Please sign in to comment.