Skip to content

Commit

Permalink
improve how new branches are created (#266)
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com>
  • Loading branch information
krancour authored Mar 25, 2024
1 parent bbd4546 commit 89fd5f9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
13 changes: 7 additions & 6 deletions branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/30-how-to-guides/30-docker-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<your GitHub handle>/kargo-render-demo-deploy \
--repo-username <your GitHub handle> \
--repo-password <a GitHub personal access token> \
Expand Down
19 changes: 15 additions & 4 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 17 additions & 6 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 89fd5f9

Please sign in to comment.