Skip to content

Commit 957aed4

Browse files
committed
fix sync fork
1 parent 1240798 commit 957aed4

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

routers/web/repo/view_home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func prepareUpstreamDivergingInfo(ctx *context.Context) {
181181
if !ctx.Repo.Repository.IsFork || !ctx.Repo.IsViewBranch || ctx.Repo.TreePath != "" {
182182
return
183183
}
184-
upstreamDivergingInfo, err := repo_service.GetUpstreamDivergingInfo(ctx, ctx.Repo.Repository, ctx.Repo.BranchName)
184+
upstreamDivergingInfo, err := repo_service.GetUpstreamDivergingInfo(ctx, ctx.Repo.GitRepo, ctx.Repo.Repository, ctx.Repo.BranchName)
185185
if err != nil {
186186
if !errors.Is(err, util.ErrNotExist) && !errors.Is(err, util.ErrInvalidArgument) {
187187
log.Error("GetUpstreamDivergingInfo: %v", err)

services/repository/merge_upstream.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ package repository
66
import (
77
"context"
88
"fmt"
9+
"strconv"
10+
"time"
911

1012
git_model "code.gitea.io/gitea/models/git"
1113
issue_model "code.gitea.io/gitea/models/issues"
1214
repo_model "code.gitea.io/gitea/models/repo"
1315
user_model "code.gitea.io/gitea/models/user"
1416
"code.gitea.io/gitea/modules/git"
17+
"code.gitea.io/gitea/modules/log"
1518
repo_module "code.gitea.io/gitea/modules/repository"
1619
"code.gitea.io/gitea/modules/util"
1720
"code.gitea.io/gitea/services/pull"
@@ -74,7 +77,7 @@ func MergeUpstream(ctx context.Context, doer *user_model.User, repo *repo_model.
7477
return "merge", nil
7578
}
7679

77-
func GetUpstreamDivergingInfo(ctx context.Context, repo *repo_model.Repository, branch string) (*UpstreamDivergingInfo, error) {
80+
func GetUpstreamDivergingInfo(ctx context.Context, gitRepo *git.Repository, repo *repo_model.Repository, branch string) (*UpstreamDivergingInfo, error) {
7881
if !repo.IsFork {
7982
return nil, util.NewInvalidArgumentErrorf("repo is not a fork")
8083
}
@@ -102,10 +105,29 @@ func GetUpstreamDivergingInfo(ctx context.Context, repo *repo_model.Repository,
102105
return info, nil
103106
}
104107

105-
// TODO: if the fork repo has new commits, this call will fail:
106-
// exit status 128 - fatal: Invalid symmetric difference expression aaaaaaaaaaaa...bbbbbbbbbbbb
107-
// so at the moment, we are not able to handle this case, should be improved in the future
108-
diff, err := git.GetDivergingCommits(ctx, repo.BaseRepo.RepoPath(), baseBranch.CommitID, forkBranch.CommitID)
108+
// Add a temporary remote
109+
tmpRemote := strconv.FormatInt(time.Now().UnixNano(), 10)
110+
if err = gitRepo.AddRemote(tmpRemote, repo.BaseRepo.RepoPath(), false); err != nil {
111+
log.Error("GetUpstreamDivergingInfo: AddRemote: %v", err)
112+
}
113+
defer func() {
114+
if err := gitRepo.RemoveRemote(tmpRemote); err != nil {
115+
log.Error("GetUpstreamDivergingInfo: RemoveRemote: %v", err)
116+
}
117+
}()
118+
119+
var remoteBranch string
120+
_, remoteBranch, err = gitRepo.GetMergeBase(tmpRemote, baseBranch.CommitID, forkBranch.CommitID)
121+
if err != nil {
122+
log.Error("GetMergeBase: %v", err)
123+
}
124+
125+
baseBranch.CommitID, err = git.GetFullCommitID(gitRepo.Ctx, gitRepo.Path, remoteBranch)
126+
if err != nil {
127+
baseBranch.CommitID = remoteBranch
128+
}
129+
130+
diff, err := git.GetDivergingCommits(gitRepo.Ctx, gitRepo.Path, baseBranch.CommitID, forkBranch.CommitID)
109131
if err != nil {
110132
info.BaseIsNewer = baseBranch.UpdatedUnix > forkBranch.UpdatedUnix
111133
return info, nil

0 commit comments

Comments
 (0)