Skip to content

Commit bdb32c9

Browse files
committed
ensure that there is no chance for conflicts
1 parent 29758ec commit bdb32c9

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

modules/pull/merge.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,17 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
6666

6767
headRepoPath := models.RepoPath(pr.HeadUserName, pr.HeadRepo.Name)
6868

69-
if err := git.Clone(baseGitRepo.Path, tmpBasePath, git.CloneRepoOptions{
70-
Shared: true,
71-
NoCheckout: true,
72-
Branch: pr.BaseBranch,
73-
}); err != nil {
74-
return fmt.Errorf("git clone: %v", err)
69+
if err := git.InitRepository(tmpBasePath, false); err != nil {
70+
return fmt.Errorf("git init: %v", err)
7571
}
7672

7773
remoteRepoName := "head_repo"
74+
baseBranch := "base"
7875

7976
// Add head repo remote.
8077
addCacheRepo := func(staging, cache string) error {
8178
p := filepath.Join(staging, ".git", "objects", "info", "alternates")
82-
f, err := os.OpenFile(p, os.O_APPEND|os.O_WRONLY, 0600)
79+
f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
8380
if err != nil {
8481
return err
8582
}
@@ -91,11 +88,31 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
9188
return nil
9289
}
9390

94-
if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
91+
if err := addCacheRepo(tmpBasePath, baseGitRepo.Path); err != nil {
9592
return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
9693
}
9794

9895
var errbuf strings.Builder
96+
if err := git.NewCommand("remote", "add", "-t", pr.BaseBranch, "-m", pr.BaseBranch, "origin", baseGitRepo.Path).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
97+
return fmt.Errorf("git remote add [%s -> %s]: %s", baseGitRepo.Path, tmpBasePath, errbuf.String())
98+
}
99+
100+
if err := git.NewCommand("fetch", "origin", pr.BaseBranch+":"+baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
101+
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
102+
}
103+
104+
if err := git.NewCommand("fetch", "origin", pr.BaseBranch+":original_"+baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
105+
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
106+
}
107+
108+
if err := git.NewCommand("symbolic-ref", "HEAD", git.BranchPrefix+baseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
109+
return fmt.Errorf("git symbolic-ref HEAD base [%s]: %s", tmpBasePath, errbuf.String())
110+
}
111+
112+
if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
113+
return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
114+
}
115+
99116
if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
100117
return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
101118
}
@@ -109,7 +126,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
109126
stagingBranch := "staging"
110127

111128
// Enable sparse-checkout
112-
sparseCheckoutList, err := getDiffTree(tmpBasePath, pr.BaseBranch, trackingBranch)
129+
sparseCheckoutList, err := getDiffTree(tmpBasePath, baseBranch, trackingBranch)
113130
if err != nil {
114131
return fmt.Errorf("getDiffTree: %v", err)
115132
}
@@ -228,7 +245,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
228245
if err != nil {
229246
return fmt.Errorf("Failed to get full commit id for HEAD: %v", err)
230247
}
231-
mergeBaseSHA, err := git.GetFullCommitID(tmpBasePath, "origin/"+pr.BaseBranch)
248+
mergeBaseSHA, err := git.GetFullCommitID(tmpBasePath, "original_"+baseBranch)
232249
if err != nil {
233250
return fmt.Errorf("Failed to get full commit id for origin/%s: %v", pr.BaseBranch, err)
234251
}
@@ -261,7 +278,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
261278
)
262279

263280
// Push back to upstream.
264-
if err := git.NewCommand("push", "origin", pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
281+
if err := git.NewCommand("push", "origin", baseBranch+":"+pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
265282
return fmt.Errorf("git push: %s", errbuf.String())
266283
}
267284

0 commit comments

Comments
 (0)