Skip to content

Commit cbf1bd1

Browse files
feat: improve git clone mecanism (#1186)
Signed-off-by: gabriel-farache <[email protected]>
1 parent 72ceb7b commit cbf1bd1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

common/vcs/git.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,21 @@ func (gvcsrepo *GitVCSRepo) Clone(cloneOptions VCSCloneOptions) (string, error)
257257
}
258258
gvcsrepo.GitRepository, err = git.Clone(limitStorer, repoDirWt, &cloneOpts)
259259
if err != nil {
260-
logrus.Debugf("failed to clone the given branch '%s' . Will clone the entire repo and try again.", gvcsrepo.Branch)
260+
logrus.Warningf("failed to clone the given branch '%s': %v . Will clone the entire repo and try again.", gvcsrepo.Branch, err)
261261
cloneOpts := git.CloneOptions{
262262
URL: gvcsrepo.URL,
263263
Depth: commitDepth,
264264
}
265+
logrus.Infof("Removing previous cloned repository folder and recreating storer: %q", repoPath)
266+
267+
if err := os.RemoveAll(repoPath); err != nil {
268+
return "", fmt.Errorf("failed to remove the files/directories at '%s' . error: %w", repoPath, err)
269+
}
270+
repoDirWt = osfs.New(repoPath)
271+
dotGitDir, _ = repoDirWt.Chroot(git.GitDirName)
272+
fStorer := filesystem.NewStorage(dotGitDir, cache.NewObjectLRUDefault())
273+
limitStorer := Limit(fStorer, cloneOptions.MaxSize)
274+
265275
gvcsrepo.GitRepository, err = git.Clone(limitStorer, repoDirWt, &cloneOpts)
266276
if err != nil {
267277
return "", fmt.Errorf("failed to perform clone operation using git. Error: %w", err)
@@ -273,7 +283,7 @@ func (gvcsrepo *GitVCSRepo) Clone(cloneOptions VCSCloneOptions) (string, error)
273283
return "", fmt.Errorf("failed return a worktree for the repostiory. Error: %w", err)
274284
}
275285
if err := w.Checkout(&git.CheckoutOptions{Create: false, Force: false, Branch: b}); err != nil {
276-
logrus.Debugf("failed to checkout the branch '%s', creating it...", b)
286+
logrus.Warningf("failed to checkout the branch '%s', creating it...", b)
277287
if err := w.Checkout(&git.CheckoutOptions{Create: true, Force: false, Branch: b}); err != nil {
278288
return "", fmt.Errorf("failed checkout a new branch. Error : %+v", err)
279289
}

0 commit comments

Comments
 (0)