@@ -6,28 +6,13 @@ package pull
6
6
7
7
import (
8
8
"fmt"
9
- "io/ioutil"
10
- "os"
11
- "path/filepath"
12
- "strings"
13
- "time"
14
9
15
10
"code.gitea.io/gitea/models"
16
- "code.gitea.io/gitea/modules/git"
17
11
"code.gitea.io/gitea/modules/log"
18
- "code.gitea.io/gitea/modules/setting"
19
-
20
- "github.com/mcuadros/go-version"
21
12
)
22
13
23
14
// Update ToDo wip ...
24
15
func Update (pull * models.PullRequest , doer * models.User , message string ) (err error ) {
25
- binVersion , err := git .BinVersion ()
26
- if err != nil {
27
- log .Error ("git.BinVersion: %v" , err )
28
- return fmt .Errorf ("Unable to get git version: %v" , err )
29
- }
30
-
31
16
//use merge functions but switch repo's and branches
32
17
pr := & models.PullRequest {
33
18
HeadRepoID : pull .BaseRepoID ,
@@ -44,187 +29,9 @@ func Update(pull *models.PullRequest, doer *models.User, message string) (err er
44
29
return fmt .Errorf ("LoadBaseRepo: %v" , err )
45
30
}
46
31
47
- // Clone base repo.
48
- tmpBasePath , err := createTemporaryRepo (pr )
49
- if err != nil {
50
- log .Error ("CreateTemporaryPath: %v" , err )
32
+ if err := rawMerge (pr , doer , models .MergeStyleMerge , message ); err != nil {
51
33
return err
52
34
}
53
- defer func () {
54
- if err := models .RemoveTemporaryPath (tmpBasePath ); err != nil {
55
- log .Error ("Merge: RemoveTemporaryPath: %s" , err )
56
- }
57
- }()
58
-
59
- baseBranch := "base"
60
- trackingBranch := "tracking"
61
-
62
- var outbuf , errbuf strings.Builder
63
-
64
- // Enable sparse-checkout
65
- sparseCheckoutList , err := getDiffTree (tmpBasePath , baseBranch , trackingBranch )
66
- if err != nil {
67
- log .Error ("getDiffTree(%s, %s, %s): %v" , tmpBasePath , baseBranch , trackingBranch , err )
68
- return fmt .Errorf ("getDiffTree: %v" , err )
69
- }
70
-
71
- infoPath := filepath .Join (tmpBasePath , ".git" , "info" )
72
- if err := os .MkdirAll (infoPath , 0700 ); err != nil {
73
- log .Error ("Unable to create .git/info in %s: %v" , tmpBasePath , err )
74
- return fmt .Errorf ("Unable to create .git/info in tmpBasePath: %v" , err )
75
- }
76
-
77
- sparseCheckoutListPath := filepath .Join (infoPath , "sparse-checkout" )
78
- if err := ioutil .WriteFile (sparseCheckoutListPath , []byte (sparseCheckoutList ), 0600 ); err != nil {
79
- log .Error ("Unable to write .git/info/sparse-checkout file in %s: %v" , tmpBasePath , err )
80
- return fmt .Errorf ("Unable to write .git/info/sparse-checkout file in tmpBasePath: %v" , err )
81
- }
82
-
83
- var gitConfigCommand func () * git.Command
84
- if version .Compare (binVersion , "1.8.0" , ">=" ) {
85
- gitConfigCommand = func () * git.Command {
86
- return git .NewCommand ("config" , "--local" )
87
- }
88
- } else {
89
- gitConfigCommand = func () * git.Command {
90
- return git .NewCommand ("config" )
91
- }
92
- }
93
-
94
- // Switch off LFS process (set required, clean and smudge here also)
95
- if err := gitConfigCommand ().AddArguments ("filter.lfs.process" , "" ).RunInDirPipeline (tmpBasePath , & outbuf , & errbuf ); err != nil {
96
- log .Error ("git config [filter.lfs.process -> <> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
97
- return fmt .Errorf ("git config [filter.lfs.process -> <> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
98
- }
99
- outbuf .Reset ()
100
- errbuf .Reset ()
101
-
102
- if err := gitConfigCommand ().AddArguments ("filter.lfs.required" , "false" ).RunInDirPipeline (tmpBasePath , & outbuf , & errbuf ); err != nil {
103
- log .Error ("git config [filter.lfs.required -> <false> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
104
- return fmt .Errorf ("git config [filter.lfs.required -> <false> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
105
- }
106
- outbuf .Reset ()
107
- errbuf .Reset ()
108
-
109
- if err := gitConfigCommand ().AddArguments ("filter.lfs.clean" , "" ).RunInDirPipeline (tmpBasePath , & outbuf , & errbuf ); err != nil {
110
- log .Error ("git config [filter.lfs.clean -> <> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
111
- return fmt .Errorf ("git config [filter.lfs.clean -> <> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
112
- }
113
- outbuf .Reset ()
114
- errbuf .Reset ()
115
-
116
- if err := gitConfigCommand ().AddArguments ("filter.lfs.smudge" , "" ).RunInDirPipeline (tmpBasePath , & outbuf , & errbuf ); err != nil {
117
- log .Error ("git config [filter.lfs.smudge -> <> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
118
- return fmt .Errorf ("git config [filter.lfs.smudge -> <> ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
119
- }
120
- outbuf .Reset ()
121
- errbuf .Reset ()
122
-
123
- if err := gitConfigCommand ().AddArguments ("core.sparseCheckout" , "true" ).RunInDirPipeline (tmpBasePath , & outbuf , & errbuf ); err != nil {
124
- log .Error ("git config [core.sparseCheckout -> true ]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
125
- return fmt .Errorf ("git config [core.sparsecheckout -> true]: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
126
- }
127
- outbuf .Reset ()
128
- errbuf .Reset ()
129
-
130
- // Read base branch index
131
- if err := git .NewCommand ("read-tree" , "HEAD" ).RunInDirPipeline (tmpBasePath , & outbuf , & errbuf ); err != nil {
132
- log .Error ("git read-tree HEAD: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
133
- return fmt .Errorf ("Unable to read base branch in to the index: %v\n %s\n %s" , err , outbuf .String (), errbuf .String ())
134
- }
135
- outbuf .Reset ()
136
- errbuf .Reset ()
137
-
138
- // Determine if we should sign
139
- signArg := ""
140
- if version .Compare (binVersion , "1.7.9" , ">=" ) {
141
- sign , keyID , _ := pr .SignMerge (doer , tmpBasePath , "HEAD" , trackingBranch )
142
- if sign {
143
- signArg = "-S" + keyID
144
- } else if version .Compare (binVersion , "2.0.0" , ">=" ) {
145
- signArg = "--no-gpg-sign"
146
- }
147
- }
148
-
149
- sig := doer .NewGitSig ()
150
- commitTimeStr := time .Now ().Format (time .RFC3339 )
151
-
152
- // Because this may call hooks we should pass in the environment
153
- env := append (os .Environ (),
154
- "GIT_AUTHOR_NAME=" + sig .Name ,
155
- "GIT_AUTHOR_EMAIL=" + sig .Email ,
156
- "GIT_AUTHOR_DATE=" + commitTimeStr ,
157
- "GIT_COMMITTER_NAME=" + sig .Name ,
158
- "GIT_COMMITTER_EMAIL=" + sig .Email ,
159
- "GIT_COMMITTER_DATE=" + commitTimeStr ,
160
- )
161
-
162
- // Merge commits.
163
- cmd := git .NewCommand ("merge" , "--no-ff" , "--no-commit" , trackingBranch )
164
- if err := runMergeCommand (pr , models .MergeStyleMerge , cmd , tmpBasePath ); err != nil {
165
- log .Error ("Unable to merge tracking into base: %v" , err )
166
- return err
167
- }
168
-
169
- if err := commitAndSignNoAuthor (pr , message , signArg , tmpBasePath , env ); err != nil {
170
- log .Error ("Unable to make final commit: %v" , err )
171
- return err
172
- }
173
-
174
- // OK we should cache our current head and origin/headbranch
175
- mergeHeadSHA , err := git .GetFullCommitID (tmpBasePath , "HEAD" )
176
- if err != nil {
177
- return fmt .Errorf ("Failed to get full commit id for HEAD: %v" , err )
178
- }
179
- mergeBaseSHA , err := git .GetFullCommitID (tmpBasePath , "original_" + baseBranch )
180
- if err != nil {
181
- return fmt .Errorf ("Failed to get full commit id for origin/%s: %v" , pr .BaseBranch , err )
182
- }
183
-
184
- // Now it's questionable about where this should go - either after or before the push
185
- // I think in the interests of data safety - failures to push to the lfs should prevent
186
- // the merge as you can always remerge.
187
- if setting .LFS .StartServer {
188
- if err := LFSPush (tmpBasePath , mergeHeadSHA , mergeBaseSHA , pr ); err != nil {
189
- return err
190
- }
191
- }
192
-
193
- var headUser * models.User
194
- err = pr .HeadRepo .GetOwner ()
195
- if err != nil {
196
- if ! models .IsErrUserNotExist (err ) {
197
- log .Error ("Can't find user: %d for head repository - %v" , pr .HeadRepo .OwnerID , err )
198
- return err
199
- }
200
- log .Error ("Can't find user: %d for head repository - defaulting to doer: %s - %v" , pr .HeadRepo .OwnerID , doer .Name , err )
201
- headUser = doer
202
- } else {
203
- headUser = pr .HeadRepo .Owner
204
- }
205
-
206
- env = models .FullPushingEnvironment (
207
- headUser ,
208
- doer ,
209
- pr .BaseRepo ,
210
- pr .BaseRepo .Name ,
211
- pr .ID ,
212
- )
213
-
214
- // Push back to upstream.
215
- if err := git .NewCommand ("push" , "origin" , baseBranch + ":" + pr .BaseBranch ).RunInDirTimeoutEnvPipeline (env , - 1 , tmpBasePath , & outbuf , & errbuf ); err != nil {
216
- if strings .Contains (errbuf .String (), "non-fast-forward" ) {
217
- return models.ErrMergePushOutOfDate {
218
- Style : models .MergeStyleMerge ,
219
- StdOut : outbuf .String (),
220
- StdErr : errbuf .String (),
221
- Err : err ,
222
- }
223
- }
224
- return fmt .Errorf ("git push: %s" , errbuf .String ())
225
- }
226
- outbuf .Reset ()
227
- errbuf .Reset ()
228
35
229
36
//notification.NotifyPullRequestUpdated(pr, doer)
230
37
//trigger hooks and co ..
0 commit comments