@@ -432,6 +432,7 @@ func pullCmd() *cobra.Command {
432
432
Short : "Pull remote changes and merge them with local changes." ,
433
433
RunE : func (cmd * cobra.Command , args []string ) (err error ) {
434
434
defer err2 .Handle (& err )
435
+ ctx := cmd .Context ()
435
436
436
437
dot := try .To1 (dotlekko .ReadDotLekko ("" ))
437
438
nativeMetadata , nativeLang := try .To2 (native .DetectNativeLang ("" ))
@@ -475,7 +476,7 @@ func pullCmd() *cobra.Command {
475
476
return fmt .Errorf ("please commit or stash changes in '%s' before pulling" , lekkoPath )
476
477
}
477
478
}
478
- try .To (gen .GenNative (cmd . Context () , nativeLang , dot .LekkoPath , repoPath , gen.GenOptions {NativeMetadata : nativeMetadata }))
479
+ try .To (gen .GenNative (ctx , nativeLang , dot .LekkoPath , repoPath , gen.GenOptions {NativeMetadata : nativeMetadata }))
479
480
480
481
dot .LockSHA = newHead .Hash ().String ()
481
482
if err := dot .WriteBack (); err != nil {
@@ -500,12 +501,18 @@ func pullCmd() *cobra.Command {
500
501
return errors .Wrap (err , "ts pull" )
501
502
}
502
503
case native .GO :
503
- files , err := sync .BisyncGo (cmd . Context () , lekkoPath , lekkoPath , repoPath )
504
+ files , err := sync .BisyncGo (ctx , lekkoPath , lekkoPath , repoPath )
504
505
if err != nil {
505
506
return errors .Wrap (err , "go bisync" )
506
507
}
508
+ hasConflicts := false
507
509
for _ , f := range files {
508
- try .To (mergeFile (cmd .Context (), f , dot , nativeMetadata ))
510
+ hasConflicts = hasConflicts && try .To1 (mergeFile (ctx , f , dot , nativeMetadata ))
511
+ }
512
+ if ! hasConflicts {
513
+ if _ , err := sync .BisyncGo (ctx , lekkoPath , lekkoPath , repoPath ); err != nil {
514
+ return errors .Wrap (err , "post-merge go bisync" )
515
+ }
509
516
}
510
517
default :
511
518
return fmt .Errorf ("unsupported language: %s" , nativeLang )
@@ -523,54 +530,54 @@ func pullCmd() *cobra.Command {
523
530
return cmd
524
531
}
525
532
526
- func mergeFile (ctx context.Context , filename string , dot * dotlekko.DotLekko , nativeMetadata native.Metadata ) (err error ) {
533
+ func mergeFile (ctx context.Context , filename string , dot * dotlekko.DotLekko , nativeMetadata native.Metadata ) (hasConflicts bool , err error ) {
527
534
defer err2 .Handle (& err )
528
535
nativeLang , err := native .NativeLangFromExt (filename )
529
536
if err != nil {
530
- return err
537
+ return false , err
531
538
}
532
539
ns := try .To1 (nativeLang .GetNamespace (filename ))
533
540
534
541
fileBytes , err := os .ReadFile (filename )
535
542
if err != nil {
536
- return errors .Wrap (err , "read file" )
543
+ return false , errors .Wrap (err , "read file" )
537
544
}
538
545
if bytes .Contains (fileBytes , []byte ("<<<<<<<" )) {
539
- return fmt .Errorf ("%s has unresolved merge conflicts" , filename )
546
+ return false , fmt .Errorf ("%s has unresolved merge conflicts" , filename )
540
547
}
541
548
542
549
if len (dot .LockSHA ) == 0 {
543
- return errors .New ("no Lekko lock information found" )
550
+ return false , errors .New ("no Lekko lock information found" )
544
551
}
545
552
546
553
repoPath , err := repo .PrepareGithubRepo ()
547
554
if err != nil {
548
- return err
555
+ return false , err
549
556
}
550
557
gitRepo , err := git .PlainOpen (repoPath )
551
558
if err != nil {
552
- return errors .Wrap (err , "open git repo" )
559
+ return false , errors .Wrap (err , "open git repo" )
553
560
}
554
561
err = repo .ResetAndClean (gitRepo )
555
562
if err != nil {
556
- return err
563
+ return false , err
557
564
}
558
565
worktree , err := gitRepo .Worktree ()
559
566
if err != nil {
560
- return errors .Wrap (err , "get worktree" )
567
+ return false , errors .Wrap (err , "get worktree" )
561
568
}
562
569
563
570
// base
564
571
err = worktree .Checkout (& git.CheckoutOptions {
565
572
Hash : plumbing .NewHash (dot .LockSHA ),
566
573
})
567
574
if err != nil {
568
- return errors .Wrap (err , fmt .Sprintf ("checkout %s" , dot .LockSHA ))
575
+ return false , errors .Wrap (err , fmt .Sprintf ("checkout %s" , dot .LockSHA ))
569
576
}
570
577
571
578
baseDir , err := os .MkdirTemp ("" , "lekko-merge-base-" )
572
579
if err != nil {
573
- return errors .Wrap (err , "create temp dir" )
580
+ return false , errors .Wrap (err , "create temp dir" )
574
581
}
575
582
defer os .RemoveAll (baseDir )
576
583
err = gen .GenNative (ctx , nativeLang , dot .LekkoPath , repoPath , gen.GenOptions {
@@ -579,7 +586,7 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nat
579
586
NativeMetadata : nativeMetadata ,
580
587
})
581
588
if err != nil {
582
- return errors .Wrap (err , "gen native" )
589
+ return false , errors .Wrap (err , "gen native" )
583
590
}
584
591
585
592
getCommitInfo := func () (string , error ) {
@@ -598,19 +605,19 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nat
598
605
599
606
baseCommitInfo , err := getCommitInfo ()
600
607
if err != nil {
601
- return errors .Wrap (err , "get commit info" )
608
+ return false , errors .Wrap (err , "get commit info" )
602
609
}
603
610
604
611
// remote
605
612
err = worktree .Checkout (& git.CheckoutOptions {
606
613
Branch : plumbing .NewBranchReferenceName ("main" ),
607
614
})
608
615
if err != nil {
609
- return errors .Wrap (err , "checkout main" )
616
+ return false , errors .Wrap (err , "checkout main" )
610
617
}
611
618
remoteDir , err := os .MkdirTemp ("" , "lekko-merge-remote-" )
612
619
if err != nil {
613
- return errors .Wrap (err , "create temp dir" )
620
+ return false , errors .Wrap (err , "create temp dir" )
614
621
}
615
622
defer os .RemoveAll (remoteDir )
616
623
err = gen .GenNative (ctx , nativeLang , dot .LekkoPath , repoPath , gen.GenOptions {
@@ -619,12 +626,12 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nat
619
626
NativeMetadata : nativeMetadata ,
620
627
})
621
628
if err != nil {
622
- return errors .Wrap (err , "gen native" )
629
+ return false , errors .Wrap (err , "gen native" )
623
630
}
624
631
625
632
remoteCommitInfo , err := getCommitInfo ()
626
633
if err != nil {
627
- return errors .Wrap (err , "get commit info" )
634
+ return false , errors .Wrap (err , "get commit info" )
628
635
}
629
636
630
637
baseFilename := filepath .Join (baseDir , filename )
@@ -644,12 +651,13 @@ func mergeFile(ctx context.Context, filename string, dot *dotlekko.DotLekko, nat
644
651
// positive error code is fine, it signals number of conflicts
645
652
if ok && exitErr .ExitCode () > 0 {
646
653
fmt .Printf ("CONFLICT (content): Merge conflict in %s\n " , filename )
654
+ return true , nil
647
655
} else {
648
- return errors .Wrap (err , "git merge-file" )
656
+ return false , errors .Wrap (err , "git merge-file" )
649
657
}
650
658
}
651
659
652
- return nil
660
+ return false , nil
653
661
}
654
662
655
663
func mergeFileCmd () * cobra.Command {
@@ -664,7 +672,8 @@ func mergeFileCmd() *cobra.Command {
664
672
if err != nil {
665
673
return errors .Wrap (err , "open Lekko configuration file" )
666
674
}
667
- return mergeFile (cmd .Context (), tsFilename , dot , nil )
675
+ _ , err = mergeFile (cmd .Context (), tsFilename , dot , nil )
676
+ return err
668
677
},
669
678
}
670
679
cmd .Flags ().StringVarP (& tsFilename , "filename" , "f" , "" , "path to ts file to pull changes into" )
0 commit comments