@@ -482,6 +482,125 @@ func TestE2E_CreateWorktree(t *testing.T) {
482482 t .Errorf ("second worktree path = %q, want %q" , wt2Path , expectedWt2Path )
483483 }
484484 })
485+
486+ t .Run ("branch_flag_new_branch" , func (t * testing.T ) {
487+ t .Parallel ()
488+ repo := testutil .NewTestRepo (t )
489+ repo .CreateFile ("README.md" , "# Test" )
490+ repo .Commit ("initial commit" )
491+
492+ stdout , stderr , err := runGitWtStdout (t , binPath , repo .Root , "-b" , "glasser/my-feature" , "my-feature" )
493+ if err != nil {
494+ t .Fatalf ("git-wt -b failed: %v\n stderr: %s" , err , stderr )
495+ }
496+
497+ wtPath := strings .TrimSpace (stdout )
498+ // Worktree directory should be named "my-feature", not "glasser/my-feature"
499+ expectedPath := filepath .Join (repo .Root , ".wt" , "my-feature" )
500+ if wtPath != expectedPath {
501+ t .Errorf ("worktree path = %q, want %q" , wtPath , expectedPath )
502+ }
503+ if _ , err := os .Stat (wtPath ); os .IsNotExist (err ) {
504+ t .Fatalf ("worktree directory was not created at %s" , wtPath )
505+ }
506+
507+ // Verify the branch name is "glasser/my-feature"
508+ restore := repo .Chdir ()
509+ defer restore ()
510+ cmd := exec .Command ("git" , "branch" , "--list" , "glasser/my-feature" )
511+ branchOut , err := cmd .Output ()
512+ if err != nil {
513+ t .Fatalf ("git branch --list failed: %v" , err )
514+ }
515+ if ! strings .Contains (string (branchOut ), "glasser/my-feature" ) {
516+ t .Errorf ("branch glasser/my-feature should exist, got: %s" , branchOut )
517+ }
518+ })
519+
520+ t .Run ("branch_flag_with_start_point" , func (t * testing.T ) {
521+ t .Parallel ()
522+ repo := testutil .NewTestRepo (t )
523+ repo .CreateFile ("README.md" , "# Test" )
524+ repo .Commit ("initial commit" )
525+
526+ repo .CreateFile ("main-file.txt" , "main content" )
527+ repo .Commit ("main commit" )
528+
529+ repo .Git ("branch" , "old-base" , "HEAD~1" )
530+
531+ stdout , stderr , err := runGitWtStdout (t , binPath , repo .Root , "-b" , "glasser/from-old" , "from-old" , "old-base" )
532+ if err != nil {
533+ t .Fatalf ("git-wt -b with start-point failed: %v\n stderr: %s" , err , stderr )
534+ }
535+
536+ wtPath := strings .TrimSpace (stdout )
537+ expectedPath := filepath .Join (repo .Root , ".wt" , "from-old" )
538+ if wtPath != expectedPath {
539+ t .Errorf ("worktree path = %q, want %q" , wtPath , expectedPath )
540+ }
541+
542+ // Verify the worktree is based on old-base (should NOT have main-file.txt)
543+ mainFilePath := filepath .Join (wtPath , "main-file.txt" )
544+ if _ , err := os .Stat (mainFilePath ); ! os .IsNotExist (err ) {
545+ t .Error ("worktree should NOT have main-file.txt (should be based on old-base)" )
546+ }
547+ })
548+
549+ t .Run ("branch_flag_existing_branch" , func (t * testing.T ) {
550+ t .Parallel ()
551+ repo := testutil .NewTestRepo (t )
552+ repo .CreateFile ("README.md" , "# Test" )
553+ repo .Commit ("initial commit" )
554+
555+ // Create an existing branch
556+ repo .Git ("branch" , "glasser/existing" )
557+
558+ stdout , stderr , err := runGitWtStdout (t , binPath , repo .Root , "-b" , "glasser/existing" , "existing" )
559+ if err != nil {
560+ t .Fatalf ("git-wt -b with existing branch failed: %v\n stderr: %s" , err , stderr )
561+ }
562+
563+ wtPath := strings .TrimSpace (stdout )
564+ expectedPath := filepath .Join (repo .Root , ".wt" , "existing" )
565+ if wtPath != expectedPath {
566+ t .Errorf ("worktree path = %q, want %q" , wtPath , expectedPath )
567+ }
568+ if _ , err := os .Stat (wtPath ); os .IsNotExist (err ) {
569+ t .Fatalf ("worktree directory was not created at %s" , wtPath )
570+ }
571+ })
572+
573+ t .Run ("branch_flag_switch_by_branch" , func (t * testing.T ) {
574+ t .Parallel ()
575+ repo := testutil .NewTestRepo (t )
576+ repo .CreateFile ("README.md" , "# Test" )
577+ repo .Commit ("initial commit" )
578+
579+ // Create worktree with -b
580+ stdout1 , stderr , err := runGitWtStdout (t , binPath , repo .Root , "-b" , "glasser/feat" , "feat" )
581+ if err != nil {
582+ t .Fatalf ("git-wt -b create failed: %v\n stderr: %s" , err , stderr )
583+ }
584+ wtPath := strings .TrimSpace (stdout1 )
585+
586+ // Switch to it by branch name
587+ stdout2 , stderr , err := runGitWtStdout (t , binPath , repo .Root , "glasser/feat" )
588+ if err != nil {
589+ t .Fatalf ("git-wt switch by branch failed: %v\n stderr: %s" , err , stderr )
590+ }
591+ if strings .TrimSpace (stdout2 ) != wtPath {
592+ t .Errorf ("switch by branch returned %q, want %q" , strings .TrimSpace (stdout2 ), wtPath )
593+ }
594+
595+ // Switch to it by directory name
596+ stdout3 , stderr , err := runGitWtStdout (t , binPath , repo .Root , "feat" )
597+ if err != nil {
598+ t .Fatalf ("git-wt switch by dir failed: %v\n stderr: %s" , err , stderr )
599+ }
600+ if strings .TrimSpace (stdout3 ) != wtPath {
601+ t .Errorf ("switch by dir returned %q, want %q" , strings .TrimSpace (stdout3 ), wtPath )
602+ }
603+ })
485604}
486605
487606func TestE2E_SwitchWorktree (t * testing.T ) {
0 commit comments