From 4706fe03a50a847672466d32df4ca2e4c9c8eca3 Mon Sep 17 00:00:00 2001 From: Oscar Ward Date: Fri, 13 Oct 2023 11:43:56 -0700 Subject: [PATCH] handle the case of the directory existing but being empty before clone Signed-off-by: Oscar Ward --- pkg/vcs/vcs.go | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go index 4a72f95ea..7249bf427 100644 --- a/pkg/vcs/vcs.go +++ b/pkg/vcs/vcs.go @@ -104,26 +104,40 @@ func ImageInfoFromApp(ctx context.Context, app *apiv1.App, cloneDir string) (str } workdir := filepath.Join(cloneDir, strings.TrimSuffix(remote[idx+1:], ".git")) - // Check if we've cloned the repo already - if _, err := os.Stat(workdir); os.IsNotExist(err) { - // Clone the repo - args := []string{"clone", remote, workdir} - cmd := exec.CommandContext(ctx, "git", args...) - err = cmd.Run() + // Check for the directory we want to use + f, err := os.Open(workdir) + if err == nil { + // Directory exists, check if empty + _, err = f.ReadDir(1) if err != nil { - fmt.Printf("failed to clone repository %q: %v", remote, err) - continue + // Directory is empty, clone the repo + args := []string{"clone", remote, workdir} + cmd := exec.CommandContext(ctx, "git", args...) + err = cmd.Run() + if err != nil { + fmt.Printf("failed to clone repository %q: %v", remote, err) + continue + } + } else { + // Directory is not empty, try to fetch + args := []string{"-C", workdir, "fetch", remote} + cmd := exec.CommandContext(ctx, "git", args...) + err = cmd.Run() + if err != nil { + fmt.Printf("failed to fetch remote %q in repository %q: %v", remote, workdir, err) + continue + } } - } else if os.IsExist(err) { - // Fetch the remote in the repo - args := []string{"-C", workdir, "fetch", remote} + } else if os.IsNotExist(err) { + // Directory does not exist, just clone to create it + args := []string{"clone", remote, workdir} cmd := exec.CommandContext(ctx, "git", args...) err = cmd.Run() if err != nil { - fmt.Printf("failed to fetch remote %q in repository %q: %v", remote, workdir, err) + fmt.Printf("failed to clone repository %q: %v", remote, err) continue } - } else if err != nil { + } else { fmt.Printf("failed to check for the existence of directory %q: %v", workdir, err) continue } @@ -131,7 +145,7 @@ func ImageInfoFromApp(ctx context.Context, app *apiv1.App, cloneDir string) (str // Try to checkout the revision args := []string{"-C", workdir, "checkout", vcs.Revision} cmd := exec.CommandContext(ctx, "git", args...) - err := cmd.Run() + err = cmd.Run() if err != nil { fmt.Printf("failed to checkout revision %q: %v", vcs.Revision, err) continue