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

Commit

Permalink
pull git calls into their own functions
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 4706fe0 commit a171577
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions pkg/vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,24 @@ func ImageInfoFromApp(ctx context.Context, app *apiv1.App, cloneDir string) (str
_, err = f.ReadDir(1)
if err != nil {
// Directory is empty, clone the repo
args := []string{"clone", remote, workdir}
cmd := exec.CommandContext(ctx, "git", args...)
err = cmd.Run()
err = gitClone(ctx, workdir, remote)
if err != nil {
fmt.Printf("failed to clone repository %q: %v", remote, err)
fmt.Printf("%v\n", 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()
err = gitFetch(ctx, workdir, remote)
if err != nil {
fmt.Printf("failed to fetch remote %q in repository %q: %v", remote, workdir, err)
fmt.Printf("%v\n", err)
continue
}
}
} 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()
err = gitClone(ctx, workdir, remote)
if err != nil {
fmt.Printf("failed to clone repository %q: %v", remote, err)
fmt.Printf("%v\n", err)
continue
}
} else {
Expand All @@ -143,11 +137,9 @@ 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 = gitCheckout(ctx, workdir, vcs.Revision)
if err != nil {
fmt.Printf("failed to checkout revision %q: %v", vcs.Revision, err)
fmt.Printf("%v\n", err)
continue
}

Expand All @@ -160,10 +152,7 @@ func ImageInfoFromApp(ctx context.Context, app *apiv1.App, cloneDir string) (str
}

// Determine if the Acornfile is dirty or not
args = []string{"-C", workdir, "diff", "--quiet"}
cmd = exec.CommandContext(ctx, "git", args...)
err = cmd.Run()
if err != nil {
if gitDirty(ctx, workdir) {
fmt.Printf("running with a dirty Acornfile %q\n", acornfile)
}

Expand All @@ -174,3 +163,43 @@ func ImageInfoFromApp(ctx context.Context, app *apiv1.App, cloneDir string) (str
}
return "", "", fmt.Errorf("failed to resolve an acornfile from the app")
}

func gitClone(ctx context.Context, workdir, remote string) (err error) {
args := []string{"clone", remote, workdir}
cmd := exec.CommandContext(ctx, "git", args...)
cmdErr := cmd.Run()
if cmdErr != nil {
err = fmt.Errorf("failed to clone repository %q: %v", remote, err)
}
return
}

func gitFetch(ctx context.Context, workdir, remote string) (err error) {
args := []string{"-C", workdir, "fetch", remote}
cmd := exec.CommandContext(ctx, "git", args...)
cmdErr := cmd.Run()
if cmdErr != nil {
err = fmt.Errorf("failed to fetch remote %q in repository %q: %v", remote, workdir, err)
}
return
}

func gitCheckout(ctx context.Context, workdir, revision string) (err error) {
args := []string{"-C", workdir, "checkout", revision}
cmd := exec.CommandContext(ctx, "git", args...)
cmdErr := cmd.Run()
if cmdErr != nil {
err = fmt.Errorf("failed to checkout revision %q: %v", revision, err)
}
return
}

func gitDirty(ctx context.Context, workdir string) bool {
args := []string{"-C", workdir, "diff", "--quiet"}
cmd := exec.CommandContext(ctx, "git", args...)
cmdErr := cmd.Run()
if cmdErr != nil {

Check failure on line 201 in pkg/vcs/vcs.go

View workflow job for this annotation

GitHub Actions / validate

S1008: should use 'return cmdErr != nil' instead of 'if cmdErr != nil { return true }; return false' (gosimple)
return true
}
return false
}

0 comments on commit a171577

Please sign in to comment.