From daf10ff84cfcfa8a6d384ed49d6eb7cebb22b79d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 22 Feb 2026 09:14:53 -0800 Subject: [PATCH] Fix push time bug (#36693) When display or search branch's pushed time, we should use `updated_unix` rather than `commit_time`. Fix #36633 --------- Signed-off-by: silverwind Co-authored-by: silverwind --- models/git/branch.go | 14 ++++---- models/git/branch_test.go | 33 +++++++++++++++++++ .../code/recently_pushed_new_branches.tmpl | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 1d6eeb6868..bc6e2a1748 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -450,7 +450,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str type FindRecentlyPushedNewBranchesOptions struct { Repo *repo_model.Repository BaseRepo *repo_model.Repository - CommitAfterUnix int64 + PushedAfterUnix int64 MaxCount int } @@ -460,11 +460,11 @@ type RecentlyPushedNewBranch struct { BranchDisplayName string BranchLink string BranchCompareURL string - CommitTime timeutil.TimeStamp + PushedTime timeutil.TimeStamp } // FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 2 hours which has no opened PRs created -// if opts.CommitAfterUnix is 0, we will find the branches that were committed to in the last 2 hours +// if opts.PushedAfterUnix is 0, we will find the branches that were pushed in the last 2 hours // if opts.ListOptions is not set, we will only display top 2 latest branches. // Protected branches will be skipped since they are unlikely to be used to create new PRs. func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) { @@ -492,8 +492,8 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o } repoIDs := builder.Select("id").From("repository").Where(repoCond) - if opts.CommitAfterUnix == 0 { - opts.CommitAfterUnix = time.Now().Add(-time.Hour * 2).Unix() + if opts.PushedAfterUnix == 0 { + opts.PushedAfterUnix = time.Now().Add(-time.Hour * 2).Unix() } var ignoredCommitIDs []string @@ -522,7 +522,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o "pusher_id": doer.ID, "is_deleted": false, }, - builder.Gte{"commit_time": opts.CommitAfterUnix}, + builder.Gte{"updated_unix": opts.PushedAfterUnix}, builder.In("repo_id", repoIDs), // newly created branch have no changes, so skip them builder.NotIn("commit_id", ignoredCommitIDs), @@ -573,7 +573,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o BranchName: branch.Name, BranchLink: fmt.Sprintf("%s/src/branch/%s", branch.Repo.Link(), util.PathEscapeSegments(branch.Name)), BranchCompareURL: branch.Repo.ComposeBranchCompareURL(opts.BaseRepo, baseTargetBranchName, branch.Name), - CommitTime: branch.CommitTime, + PushedTime: branch.UpdatedUnix, }) } if len(newBranches) == opts.MaxCount { diff --git a/models/git/branch_test.go b/models/git/branch_test.go index 6de3ea552c..9e6148946d 100644 --- a/models/git/branch_test.go +++ b/models/git/branch_test.go @@ -6,14 +6,17 @@ package git_test import ( "context" "testing" + "time" "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/optional" + "code.gitea.io/gitea/modules/timeutil" "github.com/stretchr/testify/assert" ) @@ -63,6 +66,36 @@ func TestGetDeletedBranch(t *testing.T) { assert.NotNil(t, getDeletedBranch(t, firstBranch)) } +func TestFindRecentlyPushedNewBranchesUsesPushTime(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 12}) + branch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "outdated-new-branch"}) + + commitUnix := time.Now().Add(-3 * time.Hour).Unix() + pushUnix := time.Now().Add(-30 * time.Minute).Unix() + _, err := db.GetEngine(t.Context()).Exec( + "UPDATE branch SET commit_time = ?, updated_unix = ? WHERE id = ?", + commitUnix, + pushUnix, + branch.ID, + ) + assert.NoError(t, err) + + branches, err := git_model.FindRecentlyPushedNewBranches(t.Context(), doer, git_model.FindRecentlyPushedNewBranchesOptions{ + Repo: repo, + BaseRepo: repo, + PushedAfterUnix: time.Now().Add(-time.Hour).Unix(), + MaxCount: 1, + }) + assert.NoError(t, err) + if assert.Len(t, branches, 1) { + assert.Equal(t, branch.Name, branches[0].BranchName) + assert.Equal(t, timeutil.TimeStamp(pushUnix), branches[0].PushedTime) + } +} + func TestDeletedBranchLoadUser(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) diff --git a/templates/repo/code/recently_pushed_new_branches.tmpl b/templates/repo/code/recently_pushed_new_branches.tmpl index 8569bd6c13..e62c5ad480 100644 --- a/templates/repo/code/recently_pushed_new_branches.tmpl +++ b/templates/repo/code/recently_pushed_new_branches.tmpl @@ -6,7 +6,7 @@ {{range $recentBranch := $data.RecentlyPushedNewBranches}}
- {{$timeSince := DateUtils.TimeSince $recentBranch.CommitTime}} + {{$timeSince := DateUtils.TimeSince $recentBranch.PushedTime}} {{$branchLink := HTMLFormat `%s` $recentBranch.BranchLink .BranchDisplayName}} {{ctx.Locale.Tr "repo.pulls.recently_pushed_new_branches" $branchLink $timeSince}}