Skip to content

Commit 7be9137

Browse files
authored
cu checkout: optional branch name flag (#126)
Signed-off-by: Andrea Luzzardi <[email protected]>
1 parent 6ae972e commit 7be9137

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

cmd/cu/checkout.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ var checkoutCmd = &cobra.Command{
2222
return err
2323
}
2424

25-
branch, err := repo.Checkout(ctx, envID)
25+
branchName, err := app.Flags().GetString("branch")
26+
if err != nil {
27+
return err
28+
}
29+
30+
branch, err := repo.Checkout(ctx, envID, branchName)
2631
if err != nil {
2732
return err
2833
}
@@ -33,5 +38,6 @@ var checkoutCmd = &cobra.Command{
3338
}
3439

3540
func init() {
41+
checkoutCmd.Flags().StringP("branch", "b", "", "Local branch name to use")
3642
rootCmd.AddCommand(checkoutCmd)
3743
}

environment/integration/repository_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func TestRepositoryCreate(t *testing.T) {
1717
WithRepository(t, "repository-create", SetupEmptyRepo, func(t *testing.T, repo *repository.Repository, user *UserActions) {
1818
// Create an environment
1919
env := user.CreateEnvironment("Test Create", "Testing repository create")
20-
20+
2121
// Verify environment was created properly
2222
assert.NotNil(t, env)
2323
assert.NotEmpty(t, env.ID)
2424
assert.Equal(t, "Test Create", env.State.Title)
2525
assert.NotEmpty(t, env.Worktree)
26-
26+
2727
// Verify worktree was created
2828
_, err := os.Stat(env.Worktree)
2929
assert.NoError(t, err)
@@ -35,17 +35,17 @@ func TestRepositoryGet(t *testing.T) {
3535
t.Parallel()
3636
WithRepository(t, "repository-get", SetupEmptyRepo, func(t *testing.T, repo *repository.Repository, user *UserActions) {
3737
ctx := context.Background()
38-
38+
3939
// Create an environment
4040
env := user.CreateEnvironment("Test Get", "Testing repository get")
41-
41+
4242
// Get the environment using repository directly
4343
retrieved, err := repo.Get(ctx, user.dag, env.ID)
4444
require.NoError(t, err)
4545
assert.NotNil(t, retrieved)
4646
assert.Equal(t, env.ID, retrieved.ID)
4747
assert.Equal(t, env.State.Title, retrieved.State.Title)
48-
48+
4949
// Test getting non-existent environment
5050
_, err = repo.Get(ctx, user.dag, "non-existent-env")
5151
assert.Error(t, err)
@@ -57,16 +57,16 @@ func TestRepositoryList(t *testing.T) {
5757
t.Parallel()
5858
WithRepository(t, "repository-list", SetupEmptyRepo, func(t *testing.T, repo *repository.Repository, user *UserActions) {
5959
ctx := context.Background()
60-
60+
6161
// Create two environments
6262
env1 := user.CreateEnvironment("Environment 1", "First test environment")
6363
env2 := user.CreateEnvironment("Environment 2", "Second test environment")
64-
64+
6565
// List should return at least 2
6666
envs, err := repo.List(ctx)
6767
require.NoError(t, err)
6868
assert.GreaterOrEqual(t, len(envs), 2)
69-
69+
7070
// Verify the environments are in the list
7171
var foundIDs []string
7272
for _, e := range envs {
@@ -82,20 +82,20 @@ func TestRepositoryDelete(t *testing.T) {
8282
t.Parallel()
8383
WithRepository(t, "repository-delete", SetupEmptyRepo, func(t *testing.T, repo *repository.Repository, user *UserActions) {
8484
ctx := context.Background()
85-
85+
8686
// Create an environment
8787
env := user.CreateEnvironment("Test Delete", "Testing repository delete")
8888
worktreePath := env.Worktree
8989
envID := env.ID
90-
90+
9191
// Delete it
9292
err := repo.Delete(ctx, envID)
9393
require.NoError(t, err)
94-
94+
9595
// Verify it's gone
9696
_, err = repo.Get(ctx, user.dag, envID)
9797
assert.Error(t, err)
98-
98+
9999
// Verify worktree is deleted
100100
_, err = os.Stat(worktreePath)
101101
assert.True(t, os.IsNotExist(err))
@@ -107,22 +107,22 @@ func TestRepositoryCheckout(t *testing.T) {
107107
t.Parallel()
108108
WithRepository(t, "repository-checkout", SetupEmptyRepo, func(t *testing.T, repo *repository.Repository, user *UserActions) {
109109
ctx := context.Background()
110-
110+
111111
// Create an environment and add content
112112
env := user.CreateEnvironment("Test Checkout", "Testing repository checkout")
113113
user.FileWrite(env.ID, "test.txt", "test content", "Add test file")
114-
114+
115115
// Checkout the environment branch in the source repo
116-
branch, err := repo.Checkout(ctx, env.ID)
116+
branch, err := repo.Checkout(ctx, env.ID, "")
117117
require.NoError(t, err)
118118
assert.NotEmpty(t, branch)
119-
119+
120120
// Verify we're on the correct branch
121121
currentBranch, err := repository.RunGitCommand(ctx, repo.SourcePath(), "branch", "--show-current")
122122
require.NoError(t, err)
123123
// Branch name could be either env.ID or cu-env.ID depending on the logic
124124
actualBranch := strings.TrimSpace(currentBranch)
125-
assert.True(t, actualBranch == env.ID || actualBranch == "cu-"+env.ID,
125+
assert.True(t, actualBranch == env.ID || actualBranch == "cu-"+env.ID,
126126
"Expected branch to be %s or cu-%s, got %s", env.ID, env.ID, actualBranch)
127127
})
128-
}
128+
}

repository/repository.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,14 @@ func (r *Repository) Delete(ctx context.Context, id string) error {
304304

305305
// Checkout changes the user's current branch to that of the identified environment.
306306
// It attempts to get the most recent commit from the environment without discarding any user changes.
307-
func (r *Repository) Checkout(ctx context.Context, id string) (string, error) {
307+
func (r *Repository) Checkout(ctx context.Context, id, branch string) (string, error) {
308308
if err := r.exists(ctx, id); err != nil {
309309
return "", err
310310
}
311311

312-
branch := "cu-" + id
312+
if branch == "" {
313+
branch = "cu-" + id
314+
}
313315

314316
// set up remote tracking branch if it's not already there
315317
_, err := RunGitCommand(ctx, r.userRepoPath, "show-ref", "--verify", "--quiet", fmt.Sprintf("refs/heads/%s", branch))
@@ -321,7 +323,7 @@ func (r *Repository) Checkout(ctx context.Context, id string) (string, error) {
321323
}
322324
}
323325

324-
_, err = RunGitCommand(ctx, r.userRepoPath, "checkout", id)
326+
_, err = RunGitCommand(ctx, r.userRepoPath, "checkout", branch)
325327
if err != nil {
326328
return "", err
327329
}

0 commit comments

Comments
 (0)