Skip to content

cu checkout: optional branch name flag #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/cu/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ var checkoutCmd = &cobra.Command{
return err
}

branch, err := repo.Checkout(ctx, envID)
branchName, err := app.Flags().GetString("branch")
if err != nil {
return err
}

branch, err := repo.Checkout(ctx, envID, branchName)
if err != nil {
return err
}
Expand All @@ -32,5 +37,6 @@ var checkoutCmd = &cobra.Command{
}

func init() {
checkoutCmd.Flags().StringP("branch", "b", "", "Local branch name to use")
rootCmd.AddCommand(checkoutCmd)
}
8 changes: 5 additions & 3 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,14 @@ func (r *Repository) Delete(ctx context.Context, id string) error {
return nil
}

func (r *Repository) Checkout(ctx context.Context, id string) (string, error) {
func (r *Repository) Checkout(ctx context.Context, id, branch string) (string, error) {
if err := r.exists(ctx, id); err != nil {
return "", err
}

branch := "cu-" + id
if branch == "" {
branch = "cu-" + id
}

// set up remote tracking branch if it's not already there
_, err := runGitCommand(ctx, r.userRepoPath, "show-ref", "--verify", "--quiet", fmt.Sprintf("refs/heads/%s", branch))
Expand All @@ -230,7 +232,7 @@ func (r *Repository) Checkout(ctx context.Context, id string) (string, error) {
}
}

_, err = runGitCommand(ctx, r.userRepoPath, "checkout", id)
_, err = runGitCommand(ctx, r.userRepoPath, "checkout", branch)
if err != nil {
return "", err
}
Expand Down