Skip to content

Commit c33886f

Browse files
committed
improvements
1 parent ea9d0ad commit c33886f

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

models/issues/pull.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ func (pr *PullRequest) LoadHeadRepo(ctx context.Context) (err error) {
256256
return fmt.Errorf("pr[%d].LoadHeadRepo[%d]: %w", pr.ID, pr.HeadRepoID, err)
257257
}
258258
pr.isHeadRepoLoaded = true
259+
if pr.IsSameRepo() && pr.BaseRepo == nil {
260+
pr.BaseRepo = pr.HeadRepo
261+
}
259262
}
260263
return nil
261264
}
@@ -322,6 +325,9 @@ func (pr *PullRequest) LoadBaseRepo(ctx context.Context) (err error) {
322325
if err != nil {
323326
return fmt.Errorf("pr[%d].LoadBaseRepo[%d]: %w", pr.ID, pr.BaseRepoID, err)
324327
}
328+
if pr.IsSameRepo() && pr.HeadRepo == nil {
329+
pr.HeadRepo = pr.BaseRepo
330+
}
325331
return nil
326332
}
327333

models/migrations/v1_12/v136.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package v1_12
66
import (
77
"fmt"
88
"math"
9-
"strings"
109
"time"
1110

1211
repo_model "code.gitea.io/gitea/models/repo"
@@ -18,19 +17,15 @@ import (
1817
"xorm.io/xorm"
1918
)
2019

21-
type Repository struct {
22-
ID int64 `xorm:"pk autoincr"`
23-
OwnerID int64 `xorm:"UNIQUE(s) index"`
24-
OwnerName string
25-
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
26-
Name string `xorm:"INDEX NOT NULL"`
27-
}
28-
29-
func (r *Repository) RelativePath() string {
30-
return fmt.Sprintf("%s/%s.git", strings.ToLower(r.OwnerName), strings.ToLower(r.Name))
31-
}
32-
3320
func AddCommitDivergenceToPulls(x *xorm.Engine) error {
21+
type Repository struct {
22+
ID int64 `xorm:"pk autoincr"`
23+
OwnerID int64 `xorm:"UNIQUE(s) index"`
24+
OwnerName string
25+
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
26+
Name string `xorm:"INDEX NOT NULL"`
27+
}
28+
3429
type PullRequest struct {
3530
ID int64 `xorm:"pk autoincr"`
3631
IssueID int64 `xorm:"INDEX"`

modules/gitrepo/fetch.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package gitrepo
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"code.gitea.io/gitea/modules/git/gitcmd"
11+
)
12+
13+
// FetchRemoteBranch fetches a remote branch into a local branch
14+
func FetchRemoteBranch(ctx context.Context, repo Repository, localBranch string, remoteRepo Repository, remoteBranch string, args ...string) error {
15+
_, _, err := gitcmd.NewCommand("fetch", "--no-tags", "--refmap=").
16+
AddDynamicArguments(repoPath(remoteRepo)).
17+
// + means force fetch
18+
AddDynamicArguments(fmt.Sprintf("+refs/heads/%s:%s", remoteBranch, localBranch)).
19+
RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
20+
return err
21+
}
22+
23+
func FetchRemoteCommit(ctx context.Context, repo, remoteRepo Repository, commitID string) error {
24+
_, _, err := gitcmd.NewCommand("fetch", "--no-tags").
25+
AddDynamicArguments(repoPath(remoteRepo)).
26+
AddDynamicArguments(commitID).
27+
RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath(repo)})
28+
return err
29+
}

services/pull/pull.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ func UpdatePullsRefs(ctx context.Context, repo *repo_model.Repository, update *r
560560
func UpdatePullRequestAgitFlowHead(ctx context.Context, pr *issues_model.PullRequest, commitID string) error {
561561
log.Trace("UpdateAgitPullRequestHead[%d]: update pull request head in base repo '%s'", pr.ID, pr.GetGitHeadRefName())
562562

563-
_, _, err := gitcmd.NewCommand("update-ref").AddDynamicArguments(pr.GetGitHeadRefName(), commitID).RunStdString(ctx, &gitcmd.RunOpts{Dir: pr.BaseRepo.RepoPath()})
564-
return err
563+
return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), commitID)
565564
}
566565

567566
// UpdatePullRequestHeadRef updates the head reference of a pull request
@@ -572,21 +571,17 @@ func UpdatePullRequestGithubFlowHead(ctx context.Context, pr *issues_model.PullR
572571
return err
573572
}
574573

575-
if err := gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadCommitID); err != nil {
576-
log.Error("Unable to update ref in base repository for PR[%d] Error: %v", pr.ID, err)
577-
}
574+
if !pr.IsSameRepo() { // for cross repository pull request
575+
if err := pr.LoadHeadRepo(ctx); err != nil {
576+
return err
577+
}
578578

579-
// for cross repository pull request
580-
if err := pr.LoadHeadRepo(ctx); err != nil {
581-
return err
579+
if err := gitrepo.FetchRemoteBranch(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadRepo, pr.HeadBranch); err != nil {
580+
return err
581+
}
582582
}
583583

584-
_, _, err := gitcmd.NewCommand("fetch", "--no-tags", "--refmap=").
585-
AddDynamicArguments(pr.HeadRepo.RepoPath()).
586-
// + means force fetch
587-
AddDynamicArguments(fmt.Sprintf("+refs/heads/%s:%s", pr.HeadBranch, pr.GetGitHeadRefName())).
588-
RunStdString(ctx, &gitcmd.RunOpts{Dir: pr.BaseRepo.RepoPath()})
589-
return err
584+
return gitrepo.UpdateRef(ctx, pr.BaseRepo, pr.GetGitHeadRefName(), pr.HeadCommitID)
590585
}
591586

592587
// retargetBranchPulls change target branch for all pull requests whose base branch is the branch

services/repository/branch.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
user_model "code.gitea.io/gitea/models/user"
2020
"code.gitea.io/gitea/modules/cache"
2121
"code.gitea.io/gitea/modules/git"
22-
"code.gitea.io/gitea/modules/git/gitcmd"
2322
"code.gitea.io/gitea/modules/gitrepo"
2423
"code.gitea.io/gitea/modules/graceful"
2524
"code.gitea.io/gitea/modules/json"
@@ -719,10 +718,8 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repo
719718

720719
// we need fetch the necessary commits from the head repo first if it's not the same repository
721720
if baseRepo.ID != headRepo.ID {
722-
if _, _, err := gitcmd.NewCommand("fetch", "--no-tags").
723-
AddDynamicArguments(headRepo.RepoPath()).
724-
AddDynamicArguments(headGitBranch.CommitID).
725-
RunStdString(ctx, &gitcmd.RunOpts{Dir: baseRepo.RepoPath()}); err != nil {
721+
// git default will gc the commit in 2 weeks, so it's safe to do the compare
722+
if err := gitrepo.FetchRemoteCommit(ctx, baseRepo, headRepo, headGitBranch.CommitID); err != nil {
726723
return nil, err
727724
}
728725
}

0 commit comments

Comments
 (0)