Skip to content

Commit 42d0b93

Browse files
committed
Re-clone repo if origin changes
1 parent c81bb05 commit 42d0b93

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

cli/build/swmodule.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -563,17 +563,18 @@ func prepareLocalCopyGitLocked(
563563
return "", false, errors.Trace(err)
564564
}
565565

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+
566576
if !repoExists {
567577
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-
}
577578
err := gitinst.Clone(origin, targetDir, cloneOpts)
578579
if err != nil {
579580
return "", false, errors.Trace(err)
@@ -587,9 +588,24 @@ func prepareLocalCopyGitLocked(
587588
}
588589
curHash, _ := gitinst.GetCurrentHash(targetDir)
589590
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)
591592
return curHash, true, nil
592593
}
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+
}
593609
}
594610

595611
// Now, we'll try to checkout the desired version.

0 commit comments

Comments
 (0)