@@ -14,6 +14,7 @@ import {
1414 getGitHistory ,
1515 getGitStatus ,
1616 runGit ,
17+ runGitCheckout ,
1718 runGitFetch ,
1819 runGitPull ,
1920 runGitPush ,
@@ -609,7 +610,7 @@ describe("runGitListBranches", () => {
609610 await rm ( testDir , { recursive : true } ) ;
610611 } ) ;
611612
612- it ( "strips linked worktree markers from local branch names " , async ( ) => {
613+ it ( "omits local branches that are checked out in another worktree " , async ( ) => {
613614 await writeFile ( join ( testDir , "README.md" ) , "test" ) ;
614615 await execFileAsync ( "git" , [ "add" , "." ] , { cwd : testDir } ) ;
615616 await execFileAsync ( "git" , [ "commit" , "-m" , "initial" ] , { cwd : testDir } ) ;
@@ -624,11 +625,11 @@ describe("runGitListBranches", () => {
624625 const result = await runGitListBranches ( testDir ) ;
625626
626627 expect ( result . current ) . toBe ( defaultBranch ) ;
627- expect ( result . branches ) . toContainEqual ( {
628- name : "feature/worktree-branch" ,
629- isRemote : false ,
630- isCurrent : false ,
631- } ) ;
628+ expect ( result . branches ) . not . toContainEqual (
629+ expect . objectContaining ( {
630+ name : "feature/worktree-branch" ,
631+ } )
632+ ) ;
632633 expect ( result . branches ) . not . toContainEqual (
633634 expect . objectContaining ( {
634635 name : expect . stringMatching ( / ^ \+ \s / ) ,
@@ -716,6 +717,42 @@ describe("getGitStatus", () => {
716717 } ) ;
717718} ) ;
718719
720+ describe ( "runGitCheckout" , ( ) => {
721+ it ( "switches to an existing local branch when selecting a remote branch with the same short name" , async ( ) => {
722+ const testDir = await mkdtemp ( join ( tmpdir ( ) , "git-checkout-remote-existing-local-" ) ) ;
723+ const remoteDir = await mkdtemp ( join ( tmpdir ( ) , "git-checkout-remote-existing-local-remote-" ) ) ;
724+
725+ await execFileAsync ( "git" , [ "init" ] , { cwd : testDir } ) ;
726+ await execFileAsync ( "git" , [ "config" , "user.name" , "Test" ] , { cwd : testDir } ) ;
727+ await execFileAsync ( "git" , [ "config" , "user.email" , "test@example.com" ] , { cwd : testDir } ) ;
728+ await writeFile ( join ( testDir , "README.md" ) , "test" ) ;
729+ await execFileAsync ( "git" , [ "add" , "." ] , { cwd : testDir } ) ;
730+ await execFileAsync ( "git" , [ "commit" , "-m" , "initial" ] , { cwd : testDir } ) ;
731+
732+ await execFileAsync ( "git" , [ "init" , "--bare" ] , { cwd : remoteDir } ) ;
733+ await execFileAsync ( "git" , [ "remote" , "add" , "origin" , remoteDir ] , { cwd : testDir } ) ;
734+
735+ await execFileAsync ( "git" , [ "checkout" , "-b" , "feature/login" ] , { cwd : testDir } ) ;
736+ await execFileAsync ( "git" , [ "push" , "-u" , "origin" , "feature/login" ] , { cwd : testDir } ) ;
737+ await execFileAsync ( "git" , [ "checkout" , "master" ] , { cwd : testDir } ) ;
738+
739+ const result = await runGitCheckout ( testDir , "origin/feature/login" ) ;
740+
741+ expect ( result ) . toMatchObject ( {
742+ success : true ,
743+ branch : "feature/login" ,
744+ } ) ;
745+ await expect (
746+ execFileAsync ( "git" , [ "rev-parse" , "--abbrev-ref" , "HEAD" ] , { cwd : testDir } )
747+ ) . resolves . toMatchObject ( {
748+ stdout : "feature/login\n" ,
749+ } ) ;
750+
751+ await rm ( testDir , { recursive : true } ) ;
752+ await rm ( remoteDir , { recursive : true } ) ;
753+ } ) ;
754+ } ) ;
755+
719756describe ( "getGitHistory" , ( ) => {
720757 it ( "returns recent commits in reverse chronological order with author and timestamp" , async ( ) => {
721758 const testDir = await mkdtemp ( join ( tmpdir ( ) , "git-history-" ) ) ;
0 commit comments