diff --git a/branches.go b/branches.go index 303260c..407a54b 100644 --- a/branches.go +++ b/branches.go @@ -9,6 +9,7 @@ import ( "github.com/ghodss/yaml" "github.com/akuity/kargo-render/internal/file" + "github.com/akuity/kargo-render/pkg/git" ) // branchMetadata encapsulates details about an environment-specific branch for @@ -104,12 +105,12 @@ func switchToTargetBranch(rc requestContext) error { return fmt.Errorf("error creating new target branch: %w", err) } logger.Debug("created target branch") - if err = - writeBranchMetadata(branchMetadata{}, rc.repo.WorkingDir()); err != nil { - return fmt.Errorf("error writing blank target branch metadata: %w", err) - } - logger.Debug("wrote blank target branch metadata") - if err = rc.repo.AddAllAndCommit("Initial commit"); err != nil { + if err = rc.repo.Commit( + "Initial commit", + &git.CommitOptions{ + AllowEmpty: true, + }, + ); err != nil { return fmt.Errorf("error making initial commit to new target branch: %w", err) } logger.Debug("made initial commit to new target branch") 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 f735f76..f886cad 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.36 \ +docker run -it ghcr.io/akuity/kargo-render:v0.1.0-rc.37 \ --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 fb963ab..dab504f 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -47,7 +47,7 @@ type Repo interface { // Checkout checks out the specified branch. Checkout(branch string) error // Commit commits staged changes to the current branch. - Commit(message string) error + Commit(message string, opts *CommitOptions) error // CreateChildBranch creates a new branch that is a child of the current // branch. CreateChildBranch(branch string) error @@ -216,7 +216,7 @@ func (r *repo) AddAllAndCommit(message string) error { if err := r.AddAll(); err != nil { return err } - return r.Commit(message) + return r.Commit(message, nil) } func (r *repo) Clean() error { @@ -266,8 +266,19 @@ func (r *repo) Checkout(branch string) error { return nil } -func (r *repo) Commit(message string) error { - if _, err := libExec.Exec(r.buildCommand("commit", "-m", message)); err != nil { +type CommitOptions struct { + AllowEmpty bool +} + +func (r *repo) Commit(message string, opts *CommitOptions) error { + if opts == nil { + opts = &CommitOptions{} + } + cmdTokens := []string{"commit", "-m", message} + if opts.AllowEmpty { + cmdTokens = append(cmdTokens, "--allow-empty") + } + if _, err := libExec.Exec(r.buildCommand(cmdTokens...)); err != nil { return fmt.Errorf( "error committing changes to branch %q: %w", r.currentBranch, diff --git a/service.go b/service.go index 74676b5..c491dd8 100644 --- a/service.go +++ b/service.go @@ -206,13 +206,24 @@ func (s *service) RenderManifests( return res, fmt.Errorf("error loading branch metadata: %w", err) } if oldTargetBranchMetadata == nil { - return res, fmt.Errorf( - "target branch %q already exists, but does not appear to be managed by "+ - "Kargo Render; refusing to overwrite branch contents", - rc.request.TargetBranch, - ) + // The target branch doesn't appear to already be managed by Kargo Render. + // We'll let this slide if the branch is 100% empty, but we'll refuse to + // proceed otherwise. + var fileInfos []os.DirEntry + if fileInfos, err = os.ReadDir(rc.repo.WorkingDir()); err != nil { + return res, fmt.Errorf("error reading directory contents: %w", err) + } + if len(fileInfos) != 1 && fileInfos[0].Name() != ".git" { + return res, fmt.Errorf( + "target branch %q already exists, but does not appear to be managed by "+ + "Kargo Render; refusing to overwrite branch contents", + rc.request.TargetBranch, + ) + } + rc.target.oldBranchMetadata = branchMetadata{} + } else { + rc.target.oldBranchMetadata = *oldTargetBranchMetadata } - rc.target.oldBranchMetadata = *oldTargetBranchMetadata if rc.target.commit.branch, err = switchToCommitBranch(rc); err != nil { return res, fmt.Errorf("error switching to commit branch: %w", err)