@@ -563,17 +563,18 @@ func prepareLocalCopyGitLocked(
563
563
return "" , false , errors .Trace (err )
564
564
}
565
565
566
+ cloneOpts := ourgit.CloneOptions {
567
+ Depth : cloneDepth ,
568
+ }
569
+ // We specify the revision to clone if only depth is limited; otherwise,
570
+ // we'll clone at master and checkout the needed revision afterwards,
571
+ // because this use case is faster for go-git.
572
+ if cloneDepth > 0 {
573
+ cloneOpts .Ref = version
574
+ }
575
+
566
576
if ! repoExists {
567
577
freportf (logWriter , "%s: Does not exist, cloning from %q..." , name , origin )
568
- cloneOpts := ourgit.CloneOptions {
569
- Depth : cloneDepth ,
570
- }
571
- // We specify the revision to clone if only depth is limited; otherwise,
572
- // we'll clone at master and checkout the needed revision afterwards,
573
- // because this use case is faster for go-git.
574
- if cloneDepth > 0 {
575
- cloneOpts .Ref = version
576
- }
577
578
err := gitinst .Clone (origin , targetDir , cloneOpts )
578
579
if err != nil {
579
580
return "" , false , errors .Trace (err )
@@ -587,9 +588,24 @@ func prepareLocalCopyGitLocked(
587
588
}
588
589
curHash , _ := gitinst .GetCurrentHash (targetDir )
589
590
if ! isClean {
590
- freportf (logWriter , "%s exists and is dirty, leaving it intact\n " , targetDir )
591
+ freportf (logWriter , "%s: %s exists and is dirty, leaving it intact" , name , targetDir )
591
592
return curHash , true , nil
592
593
}
594
+ // Verify that origin matches.
595
+ if curOrigin , err := gitinst .GetOriginURL (targetDir ); err != nil {
596
+ return "" , false , errors .Trace (err )
597
+ } else {
598
+ if curOrigin != origin {
599
+ freportf (logWriter , "%s: Origin changed from %q to %q, re-cloning..." , name , curOrigin , origin )
600
+ if err = os .RemoveAll (targetDir ); err != nil {
601
+ return "" , false , errors .Annotatef (err , "%s: failed to delete %q" , name , targetDir )
602
+ }
603
+ err := gitinst .Clone (origin , targetDir , cloneOpts )
604
+ if err != nil {
605
+ return "" , false , errors .Trace (err )
606
+ }
607
+ }
608
+ }
593
609
}
594
610
595
611
// Now, we'll try to checkout the desired version.
0 commit comments