Skip to content

Commit d4396ac

Browse files
algernonearl-warren
authored andcommitted
[GITEA] Don't consider orphan branches as recently pushed
When displaying the recently pushed branches banner, don't display branches that have no common history with the default branch. These branches are usually not meant to be merged, so the banner is just noise in this case. Refs: https://codeberg.org/forgejo/forgejo/pulls/2196 Signed-off-by: Gergely Nagy <[email protected]> (cherry picked from commit e1fba517f4c28c3027feaea73561045264f1f591) (cherry picked from commit 2d3c81d4f2676c58e026e5a06cfc8d84ad0d48fa) (cherry picked from commit 624a61b3b8660d53fc66f8ab3a1b0bff7a9fcb6c)
1 parent 578ab6a commit d4396ac

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

routers/web/repo/view.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,43 @@ func renderCode(ctx *context.Context) {
10681068
}
10691069
}
10701070

1071-
ctx.Data["RecentlyPushedNewBranches"] = branches
1071+
// Filter out branches that have no relation to the default branch of
1072+
// the repository.
1073+
var filteredBranches []*git_model.Branch
1074+
for _, branch := range branches {
1075+
repo, err := branch.GetRepo(ctx)
1076+
if err != nil {
1077+
continue
1078+
}
1079+
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
1080+
if err != nil {
1081+
continue
1082+
}
1083+
defer gitRepo.Close()
1084+
head, err := gitRepo.GetCommit(branch.CommitID)
1085+
if err != nil {
1086+
continue
1087+
}
1088+
defaultBranch, err := gitRepo.GetDefaultBranch()
1089+
if err != nil {
1090+
continue
1091+
}
1092+
defaultBranchHead, err := gitRepo.GetCommit(defaultBranch)
1093+
if err != nil {
1094+
continue
1095+
}
1096+
1097+
hasMergeBase, err := head.HasPreviousCommit(defaultBranchHead.ID)
1098+
if err != nil {
1099+
continue
1100+
}
1101+
1102+
if hasMergeBase {
1103+
filteredBranches = append(filteredBranches, branch)
1104+
}
1105+
}
1106+
1107+
ctx.Data["RecentlyPushedNewBranches"] = filteredBranches
10721108
}
10731109

10741110
PostRecentBranchCheck:

tests/integration/pull_create_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ import (
1616
"code.gitea.io/gitea/models/db"
1717
repo_model "code.gitea.io/gitea/models/repo"
1818
unit_model "code.gitea.io/gitea/models/unit"
19+
"code.gitea.io/gitea/models/unittest"
20+
user_model "code.gitea.io/gitea/models/user"
21+
"code.gitea.io/gitea/modules/git"
22+
"code.gitea.io/gitea/modules/graceful"
1923
"code.gitea.io/gitea/modules/test"
2024
repo_service "code.gitea.io/gitea/services/repository"
25+
files_service "code.gitea.io/gitea/services/repository/files"
2126
"code.gitea.io/gitea/tests"
2227

2328
"github.com/stretchr/testify/assert"
@@ -344,5 +349,58 @@ func TestRecentlyPushed(t *testing.T) {
344349
// PR link compares against the correct rep, and qualified branch name
345350
assert.Equal(t, "/user2/repo1/compare/master...user1/repo1:recent-push", forkPRLink)
346351
})
352+
353+
t.Run("unrelated branches are not shown", func(t *testing.T) {
354+
defer tests.PrintCurrentTest(t)()
355+
356+
adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{IsAdmin: true})
357+
358+
// Create a new branch with no relation to the default branch.
359+
// 1. Create a new Tree object
360+
cmd := git.NewCommand(db.DefaultContext, "write-tree")
361+
treeID, _, gitErr := cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
362+
assert.NoError(t, gitErr)
363+
treeID = strings.TrimSpace(treeID)
364+
// 2. Create a new (empty) commit
365+
cmd = git.NewCommand(db.DefaultContext, "commit-tree", "-m", "Initial orphan commit").AddDynamicArguments(treeID)
366+
commitID, _, gitErr := cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
367+
assert.NoError(t, gitErr)
368+
commitID = strings.TrimSpace(commitID)
369+
// 3. Create a new ref pointing to the orphaned commit
370+
cmd = git.NewCommand(db.DefaultContext, "update-ref", "refs/heads/orphan1").AddDynamicArguments(commitID)
371+
_, _, gitErr = cmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
372+
assert.NoError(t, gitErr)
373+
// 4. Sync the git repo to the database
374+
syncErr := repo_service.AddAllRepoBranchesToSyncQueue(graceful.GetManager().ShutdownContext(), adminUser.ID)
375+
assert.NoError(t, syncErr)
376+
// 5. Add a fresh commit, so that FindRecentlyPushedBranches has
377+
// something to find.
378+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
379+
changeResp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner,
380+
&files_service.ChangeRepoFilesOptions{
381+
Files: []*files_service.ChangeRepoFile{
382+
{
383+
Operation: "create",
384+
TreePath: "README.md",
385+
ContentReader: strings.NewReader("a readme file"),
386+
},
387+
},
388+
Message: "Add README.md",
389+
OldBranch: "orphan1",
390+
NewBranch: "orphan1",
391+
})
392+
assert.NoError(t, err)
393+
assert.NotEmpty(t, changeResp)
394+
395+
// Check that we only have 1 message on the main repo, the orphaned
396+
// one is not shown.
397+
req := NewRequest(t, "GET", "/user1/repo1")
398+
resp := session.MakeRequest(t, req, http.StatusOK)
399+
htmlDoc := NewHTMLParser(t, resp.Body)
400+
401+
htmlDoc.AssertElement(t, ".ui.message", true)
402+
link, _ := htmlDoc.Find(".ui.message a[href*='/src/branch/']").Attr("href")
403+
assert.Equal(t, "/user1/repo1/src/branch/recent-push", link)
404+
})
347405
})
348406
}

0 commit comments

Comments
 (0)