@@ -66,20 +66,17 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
66
66
67
67
headRepoPath := models .RepoPath (pr .HeadUserName , pr .HeadRepo .Name )
68
68
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 )
75
71
}
76
72
77
73
remoteRepoName := "head_repo"
74
+ baseBranch := "base"
78
75
79
76
// Add head repo remote.
80
77
addCacheRepo := func (staging , cache string ) error {
81
78
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 )
83
80
if err != nil {
84
81
return err
85
82
}
@@ -91,11 +88,31 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
91
88
return nil
92
89
}
93
90
94
- if err := addCacheRepo (tmpBasePath , headRepoPath ); err != nil {
91
+ if err := addCacheRepo (tmpBasePath , baseGitRepo . Path ); err != nil {
95
92
return fmt .Errorf ("addCacheRepo [%s -> %s]: %v" , headRepoPath , tmpBasePath , err )
96
93
}
97
94
98
95
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
+
99
116
if err := git .NewCommand ("remote" , "add" , remoteRepoName , headRepoPath ).RunInDirPipeline (tmpBasePath , nil , & errbuf ); err != nil {
100
117
return fmt .Errorf ("git remote add [%s -> %s]: %s" , headRepoPath , tmpBasePath , errbuf .String ())
101
118
}
@@ -109,7 +126,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
109
126
stagingBranch := "staging"
110
127
111
128
// Enable sparse-checkout
112
- sparseCheckoutList , err := getDiffTree (tmpBasePath , pr . BaseBranch , trackingBranch )
129
+ sparseCheckoutList , err := getDiffTree (tmpBasePath , baseBranch , trackingBranch )
113
130
if err != nil {
114
131
return fmt .Errorf ("getDiffTree: %v" , err )
115
132
}
@@ -228,7 +245,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
228
245
if err != nil {
229
246
return fmt .Errorf ("Failed to get full commit id for HEAD: %v" , err )
230
247
}
231
- mergeBaseSHA , err := git .GetFullCommitID (tmpBasePath , "origin/" + pr . BaseBranch )
248
+ mergeBaseSHA , err := git .GetFullCommitID (tmpBasePath , "original_" + baseBranch )
232
249
if err != nil {
233
250
return fmt .Errorf ("Failed to get full commit id for origin/%s: %v" , pr .BaseBranch , err )
234
251
}
@@ -261,7 +278,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
261
278
)
262
279
263
280
// 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 {
265
282
return fmt .Errorf ("git push: %s" , errbuf .String ())
266
283
}
267
284
0 commit comments