From 53e2e3356b43b9f6da948d45360fc6d909668863 Mon Sep 17 00:00:00 2001 From: changchaishi Date: Fri, 10 Jan 2025 10:31:09 +0000 Subject: [PATCH 01/11] add support --- models/git/branch.go | 2 + routers/web/repo/view_home.go | 23 ++++++- services/repository/branch.go | 57 +++++++++++++++++ services/repository/merge_upstream.go | 61 +------------------ .../repo/code/upstream_diverging_info.tmpl | 2 +- 5 files changed, 84 insertions(+), 61 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index e683ce47e657e..eb27a55592cee 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -440,6 +440,7 @@ type FindRecentlyPushedNewBranchesOptions struct { } type RecentlyPushedNewBranch struct { + BranchName string BranchDisplayName string BranchLink string BranchCompareURL string @@ -541,6 +542,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o } newBranches = append(newBranches, &RecentlyPushedNewBranch{ BranchDisplayName: branchDisplayName, + BranchName: branch.Name, BranchLink: fmt.Sprintf("%s/src/branch/%s", branch.Repo.Link(), util.PathEscapeSegments(branch.Name)), BranchCompareURL: branch.Repo.ComposeBranchCompareURL(opts.BaseRepo, branch.Name), CommitTime: branch.CommitTime, diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index 8c9f54656b0c0..109384c417775 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -215,10 +215,31 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) { if !opts.Repo.IsMirror && !opts.BaseRepo.IsMirror && opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) && baseRepoPerm.CanRead(unit_model.TypePullRequests) { - ctx.Data["RecentlyPushedNewBranches"], err = git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts) + var finalBranches []*git_model.RecentlyPushedNewBranch + branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts) if err != nil { log.Error("FindRecentlyPushedNewBranches failed: %v", err) } + + log.Info("Branches %v", branches) + + for _, branch := range branches { + log.Info("Info %v %v %v %v", opts.Repo, opts.BaseRepo, branch.BranchName, opts.BaseRepo.DefaultBranch) + + divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx, opts.Repo, opts.BaseRepo, branch.BranchName, opts.BaseRepo.DefaultBranch) + if err != nil { + continue + } + log.Info("divergingInfo %v", divergingInfo) + // Base is the pushed branch (for fork branch or local pushed branch perspective) + if divergingInfo.BaseIsNewer || divergingInfo.CommitsAhead > 0 { + finalBranches = append(finalBranches, branch) + } + } + + log.Info("FinalBranches %v", finalBranches) + + ctx.Data["RecentlyPushedNewBranches"] = finalBranches } } } diff --git a/services/repository/branch.go b/services/repository/branch.go index 302cfff62e4b7..e36a3a0fbb145 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -26,6 +26,7 @@ import ( "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/queue" repo_module "code.gitea.io/gitea/modules/repository" + "code.gitea.io/gitea/modules/reqctx" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" webhook_module "code.gitea.io/gitea/modules/webhook" @@ -642,3 +643,59 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR return nil } + +type BranchDivergingInfo struct { + BaseIsNewer bool + CommitsBehind int + CommitsAhead int +} + +// getBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch. +func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_model.Repository, baseBranch, headbranch string) (*BranchDivergingInfo, error) { + headGitBranch, err := git_model.GetBranch(ctx, headRepo.ID, headbranch) + if err != nil { + return nil, err + } + + baseGitBranch, err := git_model.GetBranch(ctx, baseRepo.ID, baseBranch) + if err != nil { + return nil, err + } + + info := &BranchDivergingInfo{} + if headGitBranch.CommitID == baseGitBranch.CommitID { + return info, nil + } + + // if the fork repo has new commits, this call will fail because they are not in the base repo + // exit status 128 - fatal: Invalid symmetric difference expression aaaaaaaaaaaa...bbbbbbbbbbbb + // so at the moment, we first check the update time, then check whether the fork branch has base's head + diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID) + if err != nil { + info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix + if info.BaseIsNewer { + return info, nil + } + + // if the base's update time is before the fork, check whether the base's head is in the fork + headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo) + if err != nil { + return nil, err + } + + baseCommitID, err := headGitRepo.ConvertToGitID(baseGitBranch.CommitID) + if err != nil { + return nil, err + } + headCommit, err := headGitRepo.GetCommit(headGitBranch.CommitID) + if err != nil { + return nil, err + } + hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) + info.BaseIsNewer = !hasPreviousCommit + return info, nil + } + + info.CommitsBehind, info.CommitsAhead = diff.Behind, diff.Ahead + return info, nil +} diff --git a/services/repository/merge_upstream.go b/services/repository/merge_upstream.go index ef161889c09d5..3e78b1d8527e4 100644 --- a/services/repository/merge_upstream.go +++ b/services/repository/merge_upstream.go @@ -7,24 +7,16 @@ import ( "context" "fmt" - git_model "code.gitea.io/gitea/models/git" issue_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/gitrepo" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/reqctx" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/pull" ) -type UpstreamDivergingInfo struct { - BaseHasNewCommits bool - CommitsBehind int - CommitsAhead int -} - // MergeUpstream merges the base repository's default branch into the fork repository's current branch. func MergeUpstream(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, branch string) (mergeStyle string, err error) { if err = repo.MustNotBeArchived(); err != nil { @@ -78,7 +70,7 @@ func MergeUpstream(ctx context.Context, doer *user_model.User, repo *repo_model. } // GetUpstreamDivergingInfo returns the information about the divergence between the fork repository's branch and the base repository's default branch. -func GetUpstreamDivergingInfo(ctx reqctx.RequestContext, repo *repo_model.Repository, branch string) (*UpstreamDivergingInfo, error) { +func GetUpstreamDivergingInfo(ctx reqctx.RequestContext, repo *repo_model.Repository, branch string) (*BranchDivergingInfo, error) { if !repo.IsFork { return nil, util.NewInvalidArgumentErrorf("repo is not a fork") } @@ -91,54 +83,5 @@ func GetUpstreamDivergingInfo(ctx reqctx.RequestContext, repo *repo_model.Reposi return nil, err } - forkBranch, err := git_model.GetBranch(ctx, repo.ID, branch) - if err != nil { - return nil, err - } - - baseBranch, err := git_model.GetBranch(ctx, repo.BaseRepo.ID, repo.BaseRepo.DefaultBranch) - if err != nil { - return nil, err - } - - info := &UpstreamDivergingInfo{} - if forkBranch.CommitID == baseBranch.CommitID { - return info, nil - } - - // if the fork repo has new commits, this call will fail because they are not in the base repo - // exit status 128 - fatal: Invalid symmetric difference expression aaaaaaaaaaaa...bbbbbbbbbbbb - // so at the moment, we first check the update time, then check whether the fork branch has base's head - diff, err := git.GetDivergingCommits(ctx, repo.BaseRepo.RepoPath(), baseBranch.CommitID, forkBranch.CommitID) - if err != nil { - info.BaseHasNewCommits = baseBranch.UpdatedUnix > forkBranch.UpdatedUnix - if info.BaseHasNewCommits { - return info, nil - } - - // if the base's update time is before the fork, check whether the base's head is in the fork - baseGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, repo.BaseRepo) - if err != nil { - return nil, err - } - headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, repo) - if err != nil { - return nil, err - } - - baseCommitID, err := baseGitRepo.ConvertToGitID(baseBranch.CommitID) - if err != nil { - return nil, err - } - headCommit, err := headGitRepo.GetCommit(forkBranch.CommitID) - if err != nil { - return nil, err - } - hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) - info.BaseHasNewCommits = !hasPreviousCommit - return info, nil - } - - info.CommitsBehind, info.CommitsAhead = diff.Behind, diff.Ahead - return info, nil + return GetBranchDivergingInfo(ctx, repo.BaseRepo, repo, repo.BaseRepo.DefaultBranch, branch) } diff --git a/templates/repo/code/upstream_diverging_info.tmpl b/templates/repo/code/upstream_diverging_info.tmpl index bdcd99a7f7e22..e361cf263b8d6 100644 --- a/templates/repo/code/upstream_diverging_info.tmpl +++ b/templates/repo/code/upstream_diverging_info.tmpl @@ -1,4 +1,4 @@ -{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseHasNewCommits .UpstreamDivergingInfo.CommitsBehind)}} +{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseIsNewer .UpstreamDivergingInfo.CommitsBehind)}}
{{$upstreamLink := printf "%s/src/branch/%s" .Repository.BaseRepo.Link (.Repository.BaseRepo.DefaultBranch|PathEscapeSegments)}} From 6e105e4582153e2ae732ff8501fa3804af66d694 Mon Sep 17 00:00:00 2001 From: iknowright Date: Sat, 11 Jan 2025 16:03:59 +0800 Subject: [PATCH 02/11] fix --- models/git/branch.go | 2 ++ routers/web/repo/view_home.go | 10 +--------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index eb27a55592cee..a264f555d9dd4 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -440,6 +440,7 @@ type FindRecentlyPushedNewBranchesOptions struct { } type RecentlyPushedNewBranch struct { + BranchRepo *repo_model.Repository BranchName string BranchDisplayName string BranchLink string @@ -541,6 +542,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o branchDisplayName = fmt.Sprintf("%s:%s", branch.Repo.FullName(), branchDisplayName) } newBranches = append(newBranches, &RecentlyPushedNewBranch{ + BranchRepo: branch.Repo, BranchDisplayName: branchDisplayName, BranchName: branch.Name, BranchLink: fmt.Sprintf("%s/src/branch/%s", branch.Repo.Link(), util.PathEscapeSegments(branch.Name)), diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index 109384c417775..cc4ea1dca8385 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -221,24 +221,16 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) { log.Error("FindRecentlyPushedNewBranches failed: %v", err) } - log.Info("Branches %v", branches) - for _, branch := range branches { - log.Info("Info %v %v %v %v", opts.Repo, opts.BaseRepo, branch.BranchName, opts.BaseRepo.DefaultBranch) - - divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx, opts.Repo, opts.BaseRepo, branch.BranchName, opts.BaseRepo.DefaultBranch) + divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx, branch.BranchRepo, opts.BaseRepo, branch.BranchName, opts.BaseRepo.DefaultBranch) if err != nil { continue } - log.Info("divergingInfo %v", divergingInfo) // Base is the pushed branch (for fork branch or local pushed branch perspective) if divergingInfo.BaseIsNewer || divergingInfo.CommitsAhead > 0 { finalBranches = append(finalBranches, branch) } } - - log.Info("FinalBranches %v", finalBranches) - ctx.Data["RecentlyPushedNewBranches"] = finalBranches } } From 845c70c40dbdf7452d77aa3c72cb9289e5c406cc Mon Sep 17 00:00:00 2001 From: iknowright Date: Sat, 11 Jan 2025 20:23:37 +0800 Subject: [PATCH 03/11] fix --- routers/web/repo/view_home.go | 2 +- services/repository/branch.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index cc4ea1dca8385..1bd67e0ba0a33 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -227,7 +227,7 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) { continue } // Base is the pushed branch (for fork branch or local pushed branch perspective) - if divergingInfo.BaseIsNewer || divergingInfo.CommitsAhead > 0 { + if divergingInfo.BaseIsNewer || divergingInfo.CommitsBehind > 0 { finalBranches = append(finalBranches, branch) } } diff --git a/services/repository/branch.go b/services/repository/branch.go index e36a3a0fbb145..5cb0528613fc2 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -651,8 +651,8 @@ type BranchDivergingInfo struct { } // getBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch. -func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_model.Repository, baseBranch, headbranch string) (*BranchDivergingInfo, error) { - headGitBranch, err := git_model.GetBranch(ctx, headRepo.ID, headbranch) +func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_model.Repository, baseBranch, headBranch string) (*BranchDivergingInfo, error) { + headGitBranch, err := git_model.GetBranch(ctx, headRepo.ID, headBranch) if err != nil { return nil, err } From b26c3566b4f7fd1140a81a7d71de70529324ae3e Mon Sep 17 00:00:00 2001 From: changchaishi Date: Mon, 13 Jan 2025 08:51:10 +0000 Subject: [PATCH 04/11] fix test case --- services/repository/branch.go | 12 +- tests/integration/repo_branch_test.go | 116 ++++++++---------- tests/integration/repo_merge_upstream_test.go | 29 +++-- 3 files changed, 73 insertions(+), 84 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 5cb0528613fc2..fe411ae2bc2b0 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -672,17 +672,11 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ // so at the moment, we first check the update time, then check whether the fork branch has base's head diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID) if err != nil { - info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix - if info.BaseIsNewer { - return info, nil - } - // if the base's update time is before the fork, check whether the base's head is in the fork headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo) if err != nil { return nil, err } - baseCommitID, err := headGitRepo.ConvertToGitID(baseGitBranch.CommitID) if err != nil { return nil, err @@ -691,8 +685,12 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ if err != nil { return nil, err } - hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) + hasPreviousCommit, err := headCommit.HasPreviousCommit(baseCommitID) info.BaseIsNewer = !hasPreviousCommit + // make update time as last resort for divergence comparison + if err != nil { + info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix + } return info, nil } diff --git a/tests/integration/repo_branch_test.go b/tests/integration/repo_branch_test.go index 2b4c417334bb7..f9cf13112a652 100644 --- a/tests/integration/repo_branch_test.go +++ b/tests/integration/repo_branch_test.go @@ -11,13 +11,8 @@ import ( "strings" "testing" - auth_model "code.gitea.io/gitea/models/auth" - org_model "code.gitea.io/gitea/models/organization" - "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" - "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unittest" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/tests" @@ -142,19 +137,51 @@ func TestCreateBranchInvalidCSRF(t *testing.T) { assert.Contains(t, resp.Body.String(), "Invalid CSRF token") } -func prepareBranch(t *testing.T, session *TestSession, repo *repo_model.Repository) { - baseRefSubURL := fmt.Sprintf("branch/%s", repo.DefaultBranch) - +func prepareRecentlyPushedBranchTest(t *testing.T, headSession *TestSession, baseRepo, headRepo *repo_model.Repository) { + refSubURL := fmt.Sprintf("branch/%s", headRepo.DefaultBranch) + baseRepoPath := baseRepo.OwnerName + "/" + baseRepo.Name + headRepoPath := headRepo.OwnerName + "/" + headRepo.Name + // Case 1: Normal branch changeset to display pushed message // create branch with no new commit - testCreateBranch(t, session, repo.OwnerName, repo.Name, baseRefSubURL, "no-commit", http.StatusSeeOther) + testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, refSubURL, "no-commit", http.StatusSeeOther) // create branch with commit - testCreateBranch(t, session, repo.OwnerName, repo.Name, baseRefSubURL, "new-commit", http.StatusSeeOther) - testAPINewFile(t, session, repo.OwnerName, repo.Name, "new-commit", "new-commit.txt", "new-commit") + testAPINewFile(t, headSession, headRepo.OwnerName, headRepo.Name, "new-commit", fmt.Sprintf("new-file-%s.txt", headRepo.Name), "new-commit") + + // create a branch then delete it + testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, "branch/new-commit", "deleted-branch", http.StatusSeeOther) + testUIDeleteBranch(t, headSession, headRepo.OwnerName, headRepo.Name, "deleted-branch") + + // only `new-commit` branch has commits ahead the base branch + checkRecentlyPushedNewBranches(t, headSession, headRepoPath, []string{"new-commit"}) + if baseRepo.RepoPath() != headRepo.RepoPath() { + checkRecentlyPushedNewBranches(t, headSession, baseRepoPath, []string{fmt.Sprintf("%v:new-commit", headRepo.FullName())}) + } + + // Case 2: Create PR so that `new-commit` branch will not show + testCreatePullToDefaultBranch(t, headSession, baseRepo, headRepo, "new-commit", "merge new-commit to default branch") + // No push message show because of active PR + checkRecentlyPushedNewBranches(t, headSession, headRepoPath, []string{}) + if baseRepo.RepoPath() != headRepo.RepoPath() { + checkRecentlyPushedNewBranches(t, headSession, baseRepoPath, []string{}) + } +} + +func prepareRecentlyPushedBranchSpecialTest(t *testing.T, session *TestSession, baseRepo, headRepo *repo_model.Repository) { + refSubURL := fmt.Sprintf("branch/%s", headRepo.DefaultBranch) + baseRepoPath := baseRepo.OwnerName + "/" + baseRepo.Name + headRepoPath := headRepo.OwnerName + "/" + headRepo.Name + // create branch with no new commit + testCreateBranch(t, session, headRepo.OwnerName, headRepo.Name, refSubURL, "no-commit-special", http.StatusSeeOther) + + // update base (default) branch before head branch is updated + testAPINewFile(t, session, baseRepo.OwnerName, baseRepo.Name, baseRepo.DefaultBranch, fmt.Sprintf("new-file-special-%s.txt", headRepo.Name), "new-commit") - // create deleted branch - testCreateBranch(t, session, repo.OwnerName, repo.Name, "branch/new-commit", "deleted-branch", http.StatusSeeOther) - testUIDeleteBranch(t, session, repo.OwnerName, repo.Name, "deleted-branch") + // Though we have new `no-commit` branch, but the headBranch is not newer or commits ahead baseBranch. No message show. + checkRecentlyPushedNewBranches(t, session, headRepoPath, []string{}) + if baseRepo.RepoPath() != headRepo.RepoPath() { + checkRecentlyPushedNewBranches(t, session, baseRepoPath, []string{}) + } } func testCreatePullToDefaultBranch(t *testing.T, session *TestSession, baseRepo, headRepo *repo_model.Repository, headBranch, title string) string { @@ -169,6 +196,9 @@ func testCreatePullToDefaultBranch(t *testing.T, session *TestSession, baseRepo, } func prepareRepoPR(t *testing.T, baseSession, headSession *TestSession, baseRepo, headRepo *repo_model.Repository) { + refSubURL := fmt.Sprintf("branch/%s", headRepo.DefaultBranch) + testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, refSubURL, "new-commit", http.StatusSeeOther) + // create opening PR testCreateBranch(t, headSession, headRepo.OwnerName, headRepo.Name, "branch/new-commit", "opening-pr", http.StatusSeeOther) testCreatePullToDefaultBranch(t, baseSession, baseRepo, headRepo, "opening-pr", "opening pr") @@ -210,65 +240,19 @@ func checkRecentlyPushedNewBranches(t *testing.T, session *TestSession, repoPath func TestRecentlyPushedNewBranches(t *testing.T) { onGiteaRun(t, func(t *testing.T, u *url.URL) { - user1Session := loginUser(t, "user1") - user2Session := loginUser(t, "user2") user12Session := loginUser(t, "user12") - user13Session := loginUser(t, "user13") - // prepare branch and PRs in original repo + // Same reposioty check repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) - prepareBranch(t, user12Session, repo10) prepareRepoPR(t, user12Session, user12Session, repo10, repo10) - - // outdated new branch should not be displayed - checkRecentlyPushedNewBranches(t, user12Session, "user12/repo10", []string{"new-commit"}) + prepareRecentlyPushedBranchTest(t, user12Session, repo10, repo10) + prepareRecentlyPushedBranchSpecialTest(t, user12Session, repo10, repo10) // create a fork repo in public org - testRepoFork(t, user12Session, repo10.OwnerName, repo10.Name, "org25", "org25_fork_repo10", "new-commit") + testRepoFork(t, user12Session, repo10.OwnerName, repo10.Name, "org25", "org25_fork_repo10", repo10.DefaultBranch) orgPublicForkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 25, Name: "org25_fork_repo10"}) prepareRepoPR(t, user12Session, user12Session, repo10, orgPublicForkRepo) - - // user12 is the owner of the repo10 and the organization org25 - // in repo10, user12 has opening/closed/merged pr and closed/merged pr with deleted branch - checkRecentlyPushedNewBranches(t, user12Session, "user12/repo10", []string{"org25/org25_fork_repo10:new-commit", "new-commit"}) - - userForkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11}) - testCtx := NewAPITestContext(t, repo10.OwnerName, repo10.Name, auth_model.AccessTokenScopeWriteRepository) - t.Run("AddUser13AsCollaborator", doAPIAddCollaborator(testCtx, "user13", perm.AccessModeWrite)) - prepareBranch(t, user13Session, userForkRepo) - prepareRepoPR(t, user13Session, user13Session, repo10, userForkRepo) - - // create branch with same name in different repo by user13 - testCreateBranch(t, user13Session, repo10.OwnerName, repo10.Name, "branch/new-commit", "same-name-branch", http.StatusSeeOther) - testCreateBranch(t, user13Session, userForkRepo.OwnerName, userForkRepo.Name, "branch/new-commit", "same-name-branch", http.StatusSeeOther) - testCreatePullToDefaultBranch(t, user13Session, repo10, userForkRepo, "same-name-branch", "same name branch pr") - - // user13 pushed 2 branches with the same name in repo10 and repo11 - // and repo11's branch has a pr, but repo10's branch doesn't - // in this case, we should get repo10's branch but not repo11's branch - checkRecentlyPushedNewBranches(t, user13Session, "user12/repo10", []string{"same-name-branch", "user13/repo11:new-commit"}) - - // create a fork repo in private org - testRepoFork(t, user1Session, repo10.OwnerName, repo10.Name, "private_org35", "org35_fork_repo10", "new-commit") - orgPrivateForkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: 35, Name: "org35_fork_repo10"}) - prepareRepoPR(t, user1Session, user1Session, repo10, orgPrivateForkRepo) - - // user1 is the owner of private_org35 and no write permission to repo10 - // so user1 can only see the branch in org35_fork_repo10 - checkRecentlyPushedNewBranches(t, user1Session, "user12/repo10", []string{"private_org35/org35_fork_repo10:new-commit"}) - - // user2 push a branch in private_org35 - testCreateBranch(t, user2Session, orgPrivateForkRepo.OwnerName, orgPrivateForkRepo.Name, "branch/new-commit", "user-read-permission", http.StatusSeeOther) - // convert write permission to read permission for code unit - token := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeWriteOrganization) - req := NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/teams/%d", 24), &api.EditTeamOption{ - Name: "team24", - UnitsMap: map[string]string{"repo.code": "read"}, - }).AddTokenAuth(token) - MakeRequest(t, req, http.StatusOK) - teamUnit := unittest.AssertExistsAndLoadBean(t, &org_model.TeamUnit{TeamID: 24, Type: unit.TypeCode}) - assert.Equal(t, perm.AccessModeRead, teamUnit.AccessMode) - // user2 can see the branch as it is created by user2 - checkRecentlyPushedNewBranches(t, user2Session, "user12/repo10", []string{"private_org35/org35_fork_repo10:user-read-permission"}) + prepareRecentlyPushedBranchTest(t, user12Session, repo10, orgPublicForkRepo) + prepareRecentlyPushedBranchSpecialTest(t, user12Session, repo10, orgPublicForkRepo) }) } diff --git a/tests/integration/repo_merge_upstream_test.go b/tests/integration/repo_merge_upstream_test.go index e3e423c51d5d2..19992dc19d358 100644 --- a/tests/integration/repo_merge_upstream_test.go +++ b/tests/integration/repo_merge_upstream_test.go @@ -4,6 +4,7 @@ package integration import ( + "encoding/base64" "fmt" "net/http" "net/url" @@ -12,8 +13,6 @@ import ( "time" auth_model "code.gitea.io/gitea/models/auth" - "code.gitea.io/gitea/models/db" - git_model "code.gitea.io/gitea/models/git" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -37,6 +36,7 @@ func TestRepoMergeUpstream(t *testing.T) { require.Equal(t, exp, resp.Body.String()) } + baseSession := loginUser(t, baseUser.Name) session := loginUser(t, forkUser.Name) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) @@ -60,7 +60,7 @@ func TestRepoMergeUpstream(t *testing.T) { t.Run("HeadBeforeBase", func(t *testing.T) { // add a file in base repo - require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-1")) + testAPINewFile(t, baseSession, baseRepo.OwnerName, baseRepo.Name, "master", "new-file.txt", "test-content-1") // the repo shows a prompt to "sync fork" var mergeUpstreamLink string @@ -83,14 +83,21 @@ func TestRepoMergeUpstream(t *testing.T) { t.Run("BaseChangeAfterHeadChange", func(t *testing.T) { // update the files: base first, head later, and check the prompt - require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-2")) - require.NoError(t, createOrReplaceFileInBranch(forkUser, forkRepo, "new-file-other.txt", "fork-branch", "test-content-other")) - - // make sure the base branch's update time is before the fork, to make it test the complete logic - baseBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: baseRepo.ID, Name: "master"}) - forkBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: forkRepo.ID, Name: "fork-branch"}) - _, err := db.GetEngine(db.DefaultContext).ID(forkBranch.ID).Update(&git_model.Branch{UpdatedUnix: baseBranch.UpdatedUnix + 1}) - require.NoError(t, err) + testAPINewFile(t, session, forkRepo.OwnerName, forkRepo.Name, "fork-branch", "new-file-other.txt", "test-content-other") + baseUserToken := getTokenForLoggedInUser(t, baseSession, auth_model.AccessTokenScopeWriteRepository) + req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", baseRepo.OwnerName, baseRepo.Name, "new-file.txt"), &api.UpdateFileOptions{ + DeleteFileOptions: api.DeleteFileOptions{ + FileOptions: api.FileOptions{ + BranchName: "master", + NewBranchName: "master", + Message: "Update new-file.txt", + }, + SHA: "a4007b6679563f949751ed31bb371fdfb3194446", + }, + ContentBase64: base64.StdEncoding.EncodeToString([]byte("test-content-2")), + }). + AddTokenAuth(baseUserToken) + MakeRequest(t, req, http.StatusOK) // the repo shows a prompt to "sync fork" require.Eventually(t, func() bool { From e4e7766ea5036f6254ac90a3bdcc52cade44183c Mon Sep 17 00:00:00 2001 From: changchaishi Date: Mon, 13 Jan 2025 10:30:45 +0000 Subject: [PATCH 05/11] update check timer for tests --- services/repository/branch.go | 16 ++++++---- tests/integration/repo_merge_upstream_test.go | 29 +++++++------------ 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index fe411ae2bc2b0..280efb8ee3983 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -672,12 +672,20 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ // so at the moment, we first check the update time, then check whether the fork branch has base's head diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID) if err != nil { + info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix + if headRepo.IsFork && info.BaseIsNewer { + return info, nil + } // if the base's update time is before the fork, check whether the base's head is in the fork + baseGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, baseRepo) + if err != nil { + return nil, err + } headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo) if err != nil { return nil, err } - baseCommitID, err := headGitRepo.ConvertToGitID(baseGitBranch.CommitID) + baseCommitID, err := baseGitRepo.ConvertToGitID(baseGitBranch.CommitID) if err != nil { return nil, err } @@ -685,12 +693,8 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ if err != nil { return nil, err } - hasPreviousCommit, err := headCommit.HasPreviousCommit(baseCommitID) + hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) info.BaseIsNewer = !hasPreviousCommit - // make update time as last resort for divergence comparison - if err != nil { - info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix - } return info, nil } diff --git a/tests/integration/repo_merge_upstream_test.go b/tests/integration/repo_merge_upstream_test.go index 19992dc19d358..e3e423c51d5d2 100644 --- a/tests/integration/repo_merge_upstream_test.go +++ b/tests/integration/repo_merge_upstream_test.go @@ -4,7 +4,6 @@ package integration import ( - "encoding/base64" "fmt" "net/http" "net/url" @@ -13,6 +12,8 @@ import ( "time" auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" + git_model "code.gitea.io/gitea/models/git" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -36,7 +37,6 @@ func TestRepoMergeUpstream(t *testing.T) { require.Equal(t, exp, resp.Body.String()) } - baseSession := loginUser(t, baseUser.Name) session := loginUser(t, forkUser.Name) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) @@ -60,7 +60,7 @@ func TestRepoMergeUpstream(t *testing.T) { t.Run("HeadBeforeBase", func(t *testing.T) { // add a file in base repo - testAPINewFile(t, baseSession, baseRepo.OwnerName, baseRepo.Name, "master", "new-file.txt", "test-content-1") + require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-1")) // the repo shows a prompt to "sync fork" var mergeUpstreamLink string @@ -83,21 +83,14 @@ func TestRepoMergeUpstream(t *testing.T) { t.Run("BaseChangeAfterHeadChange", func(t *testing.T) { // update the files: base first, head later, and check the prompt - testAPINewFile(t, session, forkRepo.OwnerName, forkRepo.Name, "fork-branch", "new-file-other.txt", "test-content-other") - baseUserToken := getTokenForLoggedInUser(t, baseSession, auth_model.AccessTokenScopeWriteRepository) - req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", baseRepo.OwnerName, baseRepo.Name, "new-file.txt"), &api.UpdateFileOptions{ - DeleteFileOptions: api.DeleteFileOptions{ - FileOptions: api.FileOptions{ - BranchName: "master", - NewBranchName: "master", - Message: "Update new-file.txt", - }, - SHA: "a4007b6679563f949751ed31bb371fdfb3194446", - }, - ContentBase64: base64.StdEncoding.EncodeToString([]byte("test-content-2")), - }). - AddTokenAuth(baseUserToken) - MakeRequest(t, req, http.StatusOK) + require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-2")) + require.NoError(t, createOrReplaceFileInBranch(forkUser, forkRepo, "new-file-other.txt", "fork-branch", "test-content-other")) + + // make sure the base branch's update time is before the fork, to make it test the complete logic + baseBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: baseRepo.ID, Name: "master"}) + forkBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: forkRepo.ID, Name: "fork-branch"}) + _, err := db.GetEngine(db.DefaultContext).ID(forkBranch.ID).Update(&git_model.Branch{UpdatedUnix: baseBranch.UpdatedUnix + 1}) + require.NoError(t, err) // the repo shows a prompt to "sync fork" require.Eventually(t, func() bool { From f46b1a68ed5980ccb85afdacd3077a8fd2bc27c5 Mon Sep 17 00:00:00 2001 From: changchaishi Date: Wed, 15 Jan 2025 05:58:28 +0000 Subject: [PATCH 06/11] revert the name of divergence info --- routers/web/repo/view_home.go | 2 +- services/repository/branch.go | 12 ++++++------ templates/repo/code/upstream_diverging_info.tmpl | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index 1bd67e0ba0a33..48c9131ecfca3 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -227,7 +227,7 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) { continue } // Base is the pushed branch (for fork branch or local pushed branch perspective) - if divergingInfo.BaseIsNewer || divergingInfo.CommitsBehind > 0 { + if divergingInfo.BaseHasNewCommits || divergingInfo.CommitsBehind > 0 { finalBranches = append(finalBranches, branch) } } diff --git a/services/repository/branch.go b/services/repository/branch.go index 280efb8ee3983..ef956093780b9 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -645,9 +645,9 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR } type BranchDivergingInfo struct { - BaseIsNewer bool - CommitsBehind int - CommitsAhead int + BaseHasNewCommits bool + CommitsBehind int + CommitsAhead int } // getBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch. @@ -672,8 +672,8 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ // so at the moment, we first check the update time, then check whether the fork branch has base's head diff, err := git.GetDivergingCommits(ctx, baseRepo.RepoPath(), baseGitBranch.CommitID, headGitBranch.CommitID) if err != nil { - info.BaseIsNewer = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix - if headRepo.IsFork && info.BaseIsNewer { + info.BaseHasNewCommits = baseGitBranch.UpdatedUnix > headGitBranch.UpdatedUnix + if headRepo.IsFork && info.BaseHasNewCommits { return info, nil } // if the base's update time is before the fork, check whether the base's head is in the fork @@ -694,7 +694,7 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ return nil, err } hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) - info.BaseIsNewer = !hasPreviousCommit + info.BaseHasNewCommits = !hasPreviousCommit return info, nil } diff --git a/templates/repo/code/upstream_diverging_info.tmpl b/templates/repo/code/upstream_diverging_info.tmpl index e361cf263b8d6..bdcd99a7f7e22 100644 --- a/templates/repo/code/upstream_diverging_info.tmpl +++ b/templates/repo/code/upstream_diverging_info.tmpl @@ -1,4 +1,4 @@ -{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseIsNewer .UpstreamDivergingInfo.CommitsBehind)}} +{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseHasNewCommits .UpstreamDivergingInfo.CommitsBehind)}}
{{$upstreamLink := printf "%s/src/branch/%s" .Repository.BaseRepo.Link (.Repository.BaseRepo.DefaultBranch|PathEscapeSegments)}} From 85e2cce842f18a24eac0467e11b272f8303188b9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 16 Jan 2025 08:59:25 +0800 Subject: [PATCH 07/11] try to remove unnecessary code --- services/repository/branch.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index ef956093780b9..fd5dc24ed74fd 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -685,14 +685,6 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ if err != nil { return nil, err } - baseCommitID, err := baseGitRepo.ConvertToGitID(baseGitBranch.CommitID) - if err != nil { - return nil, err - } - headCommit, err := headGitRepo.GetCommit(headGitBranch.CommitID) - if err != nil { - return nil, err - } hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) info.BaseHasNewCommits = !hasPreviousCommit return info, nil From 3d12ff30fb39f2101f133aa360fc027f91fb2660 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 16 Jan 2025 09:01:22 +0800 Subject: [PATCH 08/11] try to remove unused code --- services/repository/branch.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index fd5dc24ed74fd..418fa44feb73e 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -677,11 +677,11 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ return info, nil } // if the base's update time is before the fork, check whether the base's head is in the fork - baseGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, baseRepo) + headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo) if err != nil { return nil, err } - headGitRepo, err := gitrepo.RepositoryFromRequestContextOrOpen(ctx, headRepo) + headCommit, err := headGitRepo.GetCommit(headGitBranch.CommitID) if err != nil { return nil, err } From ab7751b6d69757fe086ad0376a358aa4ba8a5498 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 16 Jan 2025 09:02:07 +0800 Subject: [PATCH 09/11] fix --- services/repository/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 418fa44feb73e..8b069c446427d 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -685,7 +685,7 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ if err != nil { return nil, err } - hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) + hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseGitBranch.CommitID) info.BaseHasNewCommits = !hasPreviousCommit return info, nil } From f62e13e8fea1aa7ceb92f36b15d1d99095270873 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 16 Jan 2025 11:21:13 +0800 Subject: [PATCH 10/11] fix --- services/repository/branch.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 8b069c446427d..5d01717b35f49 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -685,7 +685,11 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ if err != nil { return nil, err } - hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseGitBranch.CommitID) + baseCommitID, err := git.NewIDFromString(baseGitBranch.CommitID) + if err != nil { + return nil, err + } + hasPreviousCommit, _ := headCommit.HasPreviousCommit(baseCommitID) info.BaseHasNewCommits = !hasPreviousCommit return info, nil } From 0646fc1c04f1ceb577e6ad362d348324bcb24718 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Thu, 16 Jan 2025 20:08:30 +0800 Subject: [PATCH 11/11] clarify names, add error log --- routers/web/repo/view_home.go | 11 ++++++++--- services/repository/branch.go | 12 +++++++----- services/repository/merge_upstream.go | 10 +++++----- templates/repo/code/upstream_diverging_info.tmpl | 6 +++--- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index 78baac69c5324..d141df181cea3 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -222,12 +222,17 @@ func prepareRecentlyPushedNewBranches(ctx *context.Context) { } for _, branch := range branches { - divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx, branch.BranchRepo, opts.BaseRepo, branch.BranchName, opts.BaseRepo.DefaultBranch) + divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx, + branch.BranchRepo, branch.BranchName, // "base" repo for diverging info + opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info + ) if err != nil { + log.Error("GetBranchDivergingInfo failed: %v", err) continue } - // Base is the pushed branch (for fork branch or local pushed branch perspective) - if divergingInfo.BaseHasNewCommits || divergingInfo.CommitsBehind > 0 { + branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits + baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind + if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 { finalBranches = append(finalBranches, branch) } } diff --git a/services/repository/branch.go b/services/repository/branch.go index f36ec88adba4c..08c53bbb6a8fd 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -667,14 +667,16 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR return nil } +// BranchDivergingInfo contains the information about the divergence of a head branch to the base branch. +// This struct is also used in templates, so it needs to search for all references before changing it. type BranchDivergingInfo struct { BaseHasNewCommits bool - CommitsBehind int - CommitsAhead int + HeadCommitsBehind int + HeadCommitsAhead int } -// getBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch. -func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_model.Repository, baseBranch, headBranch string) (*BranchDivergingInfo, error) { +// GetBranchDivergingInfo returns the information about the divergence of a patch branch to the base branch. +func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repository, baseBranch string, headRepo *repo_model.Repository, headBranch string) (*BranchDivergingInfo, error) { headGitBranch, err := git_model.GetBranch(ctx, headRepo.ID, headBranch) if err != nil { return nil, err @@ -717,6 +719,6 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo, headRepo *repo_ return info, nil } - info.CommitsBehind, info.CommitsAhead = diff.Behind, diff.Ahead + info.HeadCommitsBehind, info.HeadCommitsAhead = diff.Behind, diff.Ahead return info, nil } diff --git a/services/repository/merge_upstream.go b/services/repository/merge_upstream.go index 3e78b1d8527e4..008e7de3857e2 100644 --- a/services/repository/merge_upstream.go +++ b/services/repository/merge_upstream.go @@ -70,18 +70,18 @@ func MergeUpstream(ctx context.Context, doer *user_model.User, repo *repo_model. } // GetUpstreamDivergingInfo returns the information about the divergence between the fork repository's branch and the base repository's default branch. -func GetUpstreamDivergingInfo(ctx reqctx.RequestContext, repo *repo_model.Repository, branch string) (*BranchDivergingInfo, error) { - if !repo.IsFork { +func GetUpstreamDivergingInfo(ctx reqctx.RequestContext, forkRepo *repo_model.Repository, forkBranch string) (*BranchDivergingInfo, error) { + if !forkRepo.IsFork { return nil, util.NewInvalidArgumentErrorf("repo is not a fork") } - if repo.IsArchived { + if forkRepo.IsArchived { return nil, util.NewInvalidArgumentErrorf("repo is archived") } - if err := repo.GetBaseRepo(ctx); err != nil { + if err := forkRepo.GetBaseRepo(ctx); err != nil { return nil, err } - return GetBranchDivergingInfo(ctx, repo.BaseRepo, repo, repo.BaseRepo.DefaultBranch, branch) + return GetBranchDivergingInfo(ctx, forkRepo.BaseRepo, forkRepo.BaseRepo.DefaultBranch, forkRepo, forkBranch) } diff --git a/templates/repo/code/upstream_diverging_info.tmpl b/templates/repo/code/upstream_diverging_info.tmpl index a1f37b8c05d51..8d6e55959f588 100644 --- a/templates/repo/code/upstream_diverging_info.tmpl +++ b/templates/repo/code/upstream_diverging_info.tmpl @@ -1,10 +1,10 @@ -{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseHasNewCommits .UpstreamDivergingInfo.CommitsBehind)}} +{{if and .UpstreamDivergingInfo (or .UpstreamDivergingInfo.BaseHasNewCommits .UpstreamDivergingInfo.HeadCommitsBehind)}}
{{$upstreamLink := printf "%s/src/branch/%s" .Repository.BaseRepo.Link (.Repository.BaseRepo.DefaultBranch|PathEscapeSegments)}} {{$upstreamHtml := HTMLFormat `%s:%s` $upstreamLink .Repository.BaseRepo.FullName .Repository.BaseRepo.DefaultBranch}} - {{if .UpstreamDivergingInfo.CommitsBehind}} - {{ctx.Locale.TrN .UpstreamDivergingInfo.CommitsBehind "repo.pulls.upstream_diverging_prompt_behind_1" "repo.pulls.upstream_diverging_prompt_behind_n" .UpstreamDivergingInfo.CommitsBehind $upstreamHtml}} + {{if .UpstreamDivergingInfo.HeadCommitsBehind}} + {{ctx.Locale.TrN .UpstreamDivergingInfo.HeadCommitsBehind "repo.pulls.upstream_diverging_prompt_behind_1" "repo.pulls.upstream_diverging_prompt_behind_n" .UpstreamDivergingInfo.HeadCommitsBehind $upstreamHtml}} {{else}} {{ctx.Locale.Tr "repo.pulls.upstream_diverging_prompt_base_newer" $upstreamHtml}} {{end}}