Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
handle the case of the directory existing but being empty before clone
Browse files Browse the repository at this point in the history
Signed-off-by: Oscar Ward <[email protected]>
  • Loading branch information
Oscar Ward committed Oct 13, 2023
1 parent eb9f6c9 commit 4706fe0
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pkg/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,48 @@ 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
}

// 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
Expand Down

0 comments on commit 4706fe0

Please sign in to comment.