From 0c83ba3d0f3843f3b22d9f165f2f6047df73bd03 Mon Sep 17 00:00:00 2001 From: Kent Rancourt Date: Wed, 27 Mar 2024 11:05:28 -0400 Subject: [PATCH] remove unnecessary push when rendering to local path Signed-off-by: Kent Rancourt --- branches.go | 33 +++++++++++++++---- docs/docs/30-how-to-guides/30-docker-image.md | 2 +- pkg/git/git.go | 17 ++++++++++ pkg/git/git_test.go | 20 +++++++++-- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/branches.go b/branches.go index 12b58cd..bd274cc 100644 --- a/branches.go +++ b/branches.go @@ -80,12 +80,12 @@ func switchToTargetBranch(rc requestContext) error { logger := rc.logger.WithField("targetBranch", rc.request.TargetBranch) // Check if the target branch exists on the remote - targetBranchExists, err := rc.repo.RemoteBranchExists(rc.request.TargetBranch) + remoteTargetBranchExists, err := rc.repo.RemoteBranchExists(rc.request.TargetBranch) if err != nil { - return fmt.Errorf("error checking for existence of target branch: %w", err) + return fmt.Errorf("error checking for existence of remote target branch: %w", err) } - if targetBranchExists { + if remoteTargetBranchExists { logger.Debug("target branch exists on remote") if err = rc.repo.Fetch(); err != nil { return fmt.Errorf("error fetching from remote: %w", err) @@ -103,10 +103,31 @@ func switchToTargetBranch(rc requestContext) error { } logger.Debug("target branch does not exist on remote") - if err = rc.repo.CreateOrphanedBranch(rc.request.TargetBranch); err != nil { - return fmt.Errorf("error creating new target branch: %w", err) + + // Check if the target branch exists locally + localTargetBranchExists, err := rc.repo.LocalBranchExists(rc.request.TargetBranch) + if err != nil { + return fmt.Errorf("error checking for existence of local target branch: %w", err) } - logger.Debug("created target branch") + + if localTargetBranchExists { + logger.Debug("target branch exists locally") + if err = rc.repo.Checkout(rc.request.TargetBranch); err != nil { + return fmt.Errorf("error checking out target branch: %w", err) + } + logger.Debug("checked out target branch") + } else { + logger.Debug("target branch does not exist locally") + if err = rc.repo.CreateOrphanedBranch(rc.request.TargetBranch); err != nil { + return fmt.Errorf("error creating new target branch: %w", err) + } + logger.Debug("created target branch locally") + } + + if rc.request.LocalOutPath != "" { + return nil // There's no need to push the new branch to the remote + } + if err = rc.repo.Commit( "Initial commit", &git.CommitOptions{ diff --git a/docs/docs/30-how-to-guides/30-docker-image.md b/docs/docs/30-how-to-guides/30-docker-image.md index 58c553b..a5265b7 100644 --- a/docs/docs/30-how-to-guides/30-docker-image.md +++ b/docs/docs/30-how-to-guides/30-docker-image.md @@ -17,7 +17,7 @@ easiest option for experimenting locally with Kargo Render! Example usage: ```shell -docker run -it ghcr.io/akuity/kargo-render:v0.1.0-rc.38 \ +docker run -it ghcr.io/akuity/kargo-render:v0.1.0-rc.39 \ --repo https://github.com//kargo-render-demo-deploy \ --repo-username \ --repo-password \ diff --git a/pkg/git/git.go b/pkg/git/git.go index 59d50e2..e046e16 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -68,6 +68,8 @@ type Repo interface { // LastCommitID returns the ID (sha) of the most recent commit to the current // branch. LastCommitID() (string, error) + // LocalBranchExists returns a bool indicating if the specified branch exists. + LocalBranchExists(branch string) (bool, error) // CommitMessage returns the text of the most recent commit message associated // with the specified commit ID. CommitMessage(id string) (string, error) @@ -366,6 +368,21 @@ func (r *repo) LastCommitID() (string, error) { return strings.TrimSpace(string(shaBytes)), nil } +func (r *repo) LocalBranchExists(branch string) (bool, error) { + resBytes, err := libExec.Exec(r.buildCommand( + "branch", + "--list", + branch, + )) + if err != nil { + return false, + fmt.Errorf("error checking for existence of local branch %q: %w", branch, err) + } + return strings.TrimSpace( + strings.Replace(string(resBytes), "*", "", 1), + ) == branch, nil +} + func (r *repo) CommitMessage(id string) (string, error) { msgBytes, err := libExec.Exec( r.buildCommand("log", "-n", "1", "--pretty=format:%s", id), diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 7986f38..7c6e59a 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -168,12 +168,28 @@ func TestRepo(t *testing.T) { require.NoError(t, err) }) + testBranch := fmt.Sprintf("test-branch-%s", uuid.NewString()) + err = r.CreateChildBranch(testBranch) + require.NoError(t, err) + t.Run("can create a child branch", func(t *testing.T) { - testBranch := fmt.Sprintf("test-branch-%s", uuid.NewString()) - err = r.CreateChildBranch(testBranch) require.NoError(t, err) }) + t.Run("can check if local branch exists -- negative result", func(t *testing.T) { + var exists bool + exists, err = r.LocalBranchExists("branch-that-does-not-exist") + require.NoError(t, err) + require.False(t, exists) + }) + + t.Run("can check if local branch exists -- positive result", func(t *testing.T) { + var exists bool + exists, err = r.LocalBranchExists(testBranch) + require.NoError(t, err) + require.True(t, exists) + }) + err = os.WriteFile(fmt.Sprintf("%s/%s", r.WorkingDir(), "test.txt"), []byte("bar"), 0600) require.NoError(t, err)