@@ -253,25 +253,78 @@ func (self *WorkingTreeHelper) commitPrefixConfigsForRepo() []config.CommitPrefi
253
253
return self .c .UserConfig ().Git .CommitPrefix
254
254
}
255
255
256
- func (self * WorkingTreeHelper ) CreateMergeConflictMenu ( selectedFilepaths [] string ) error {
257
- onMergeStrategySelected := func ( strategy string ) error {
258
- for _ , filepath := range selectedFilepaths {
259
- baseID , err := self . c . Git (). WorkingTree . ObjectIDAtStage ( filepath , 1 )
260
- if err != nil {
261
- return err
262
- }
256
+ func (self * WorkingTreeHelper ) mergeFile ( filepath string , strategy string ) ( string , error ) {
257
+ if self . c . Git (). Version . IsOlderThan ( 2 , 43 , 0 ) {
258
+ return self . mergeFileWithTempFiles ( filepath , strategy )
259
+ } else {
260
+ return self . mergeFileWithObjectIDs ( filepath , strategy )
261
+ }
262
+ }
263
263
264
- oursID , err := self .c .Git ().WorkingTree .ObjectIDAtStage (filepath , 2 )
265
- if err != nil {
266
- return err
267
- }
264
+ func (self * WorkingTreeHelper ) mergeFileWithTempFiles (filepath string , strategy string ) (string , error ) {
265
+ showToTempFile := func (stage int , label string ) (string , error ) {
266
+ output , err := self .c .Git ().WorkingTree .ShowFileAtStage (filepath , stage )
267
+ if err != nil {
268
+ return "" , err
269
+ }
268
270
269
- theirsID , err := self .c .Git ().WorkingTree .ObjectIDAtStage (filepath , 3 )
270
- if err != nil {
271
- return err
272
- }
271
+ f , err := os .CreateTemp ("" , "mergefile-" + label + "-*" )
272
+ if err != nil {
273
+ return "" , err
274
+ }
275
+ defer f .Close ()
273
276
274
- output , err := self .c .Git ().WorkingTree .MergeFile (strategy , oursID , baseID , theirsID )
277
+ if _ , err := f .Write ([]byte (output )); err != nil {
278
+ return "" , err
279
+ }
280
+
281
+ return f .Name (), nil
282
+ }
283
+
284
+ baseFilepath , err := showToTempFile (1 , "base" )
285
+ if err != nil {
286
+ return "" , err
287
+ }
288
+ defer os .Remove (baseFilepath )
289
+
290
+ oursFilepath , err := showToTempFile (2 , "ours" )
291
+ if err != nil {
292
+ return "" , err
293
+ }
294
+ defer os .Remove (oursFilepath )
295
+
296
+ theirsFilepath , err := showToTempFile (3 , "theirs" )
297
+ if err != nil {
298
+ return "" , err
299
+ }
300
+ defer os .Remove (theirsFilepath )
301
+
302
+ return self .c .Git ().WorkingTree .MergeFileForFiles (strategy , oursFilepath , baseFilepath , theirsFilepath )
303
+ }
304
+
305
+ func (self * WorkingTreeHelper ) mergeFileWithObjectIDs (filepath , strategy string ) (string , error ) {
306
+ baseID , err := self .c .Git ().WorkingTree .ObjectIDAtStage (filepath , 1 )
307
+ if err != nil {
308
+ return "" , err
309
+ }
310
+
311
+ oursID , err := self .c .Git ().WorkingTree .ObjectIDAtStage (filepath , 2 )
312
+ if err != nil {
313
+ return "" , err
314
+ }
315
+
316
+ theirsID , err := self .c .Git ().WorkingTree .ObjectIDAtStage (filepath , 3 )
317
+ if err != nil {
318
+ return "" , err
319
+ }
320
+
321
+ return self .c .Git ().WorkingTree .MergeFileForObjectIDs (strategy , oursID , baseID , theirsID )
322
+ }
323
+
324
+ func (self * WorkingTreeHelper ) CreateMergeConflictMenu (selectedFilepaths []string ) error {
325
+ onMergeStrategySelected := func (strategy string ) error {
326
+ for _ , filepath := range selectedFilepaths {
327
+ output , err := self .mergeFile (filepath , strategy )
275
328
if err != nil {
276
329
return err
277
330
}
0 commit comments