1010// Reading the worktrees and answering for one ref are separate steps, so the
1111// test names both rather than launching git for them.
1212@interface PBGitRepository (WorktreeTesting)
13- + (NSDictionary <NSString *, NSString *> *)worktreePathsFromPorcelain : (NSString *)output excludingRef : (NSString *)ourRef ;
13+ + (NSDictionary <NSString *, NSString *> *)worktreePathsFromPorcelain : (NSString *)output excludingWorktreeAtPath : (NSString *)ourPath ;
1414@end
1515
1616// What `git worktree list --porcelain` prints for a repository whose own
@@ -56,7 +56,7 @@ - (void)useWorktreePaths:(NSDictionary *)paths
5656
5757- (void )testEveryBranchInAWorktreeIsRead
5858{
59- NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingRef :nil ];
59+ NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingWorktreeAtPath :nil ];
6060
6161 XCTAssertEqualObjects (paths[@" refs/heads/master" ], @" /repos/gitx" );
6262 XCTAssertEqualObjects (paths[@" refs/heads/feature" ], @" /repos/gitx-feature" );
@@ -65,7 +65,7 @@ - (void)testEveryBranchInAWorktreeIsRead
6565
6666- (void )testAWorktreeOnADetachedHeadHoldsNoBranch
6767{
68- NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingRef :nil ];
68+ NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingWorktreeAtPath :nil ];
6969
7070 XCTAssertEqual (paths.count , 3u , @" %@ " , paths);
7171 XCTAssertFalse ([[paths allValues ] containsObject: @" /repos/gitx-detached" ]);
@@ -75,24 +75,59 @@ - (void)testABareRepositoryHoldsNoBranch
7575{
7676 NSString *porcelain = @" worktree /repos/gitx.git\n bare\n " ;
7777
78- XCTAssertEqualObjects ([PBGitRepository worktreePathsFromPorcelain: porcelain excludingRef :nil ], @{});
78+ XCTAssertEqualObjects ([PBGitRepository worktreePathsFromPorcelain: porcelain excludingWorktreeAtPath :nil ], @{});
7979}
8080
8181- (void )testNothingIsReadFromEmptyOutput
8282{
83- XCTAssertEqualObjects ([PBGitRepository worktreePathsFromPorcelain: @" " excludingRef :nil ], @{});
83+ XCTAssertEqualObjects ([PBGitRepository worktreePathsFromPorcelain: @" " excludingWorktreeAtPath :nil ], @{});
8484}
8585
8686// The main working tree is a worktree too, so the branch this window has
8787// checked out would answer for itself without this.
8888- (void )testTheBranchWeHaveCheckedOutOurselvesIsNotHeldElsewhere
8989{
90- NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingRef :@" refs/heads/master " ];
90+ NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingWorktreeAtPath :@" /repos/gitx " ];
9191
9292 XCTAssertNil (paths[@" refs/heads/master" ]);
9393 XCTAssertEqualObjects (paths[@" refs/heads/feature" ], @" /repos/gitx-feature" );
9494}
9595
96+ // `git worktree add --force` puts one branch in two worktrees. Dropping our own
97+ // by name used to take the other holder with it.
98+ - (void )testABranchOurWorktreeSharesWithAnotherIsStillHeldThere
99+ {
100+ NSString *porcelain =
101+ @" worktree /repos/gitx\n "
102+ @" HEAD 0000000000000000000000000000000000000001\n "
103+ @" branch refs/heads/shared\n "
104+ @" \n "
105+ @" worktree /repos/gitx-second\n "
106+ @" HEAD 0000000000000000000000000000000000000001\n "
107+ @" branch refs/heads/shared\n " ;
108+
109+ NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: porcelain excludingWorktreeAtPath: @" /repos/gitx" ];
110+
111+ XCTAssertEqualObjects (paths[@" refs/heads/shared" ], @" /repos/gitx-second" );
112+ }
113+
114+ // Our own entry is the one to drop wherever it appears in the listing.
115+ - (void )testOurOwnWorktreeIsDroppedEvenWhenItIsNotListedFirst
116+ {
117+ NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingWorktreeAtPath: @" /repos/gitx-feature" ];
118+
119+ XCTAssertNil (paths[@" refs/heads/feature" ]);
120+ XCTAssertEqualObjects (paths[@" refs/heads/master" ], @" /repos/gitx" );
121+ }
122+
123+ // git prints the resolved path, which need not match the spelling we hold.
124+ - (void )testOurOwnWorktreeIsRecognisedThroughAnUntidyPath
125+ {
126+ NSDictionary *paths = [PBGitRepository worktreePathsFromPorcelain: kPorcelain excludingWorktreeAtPath: @" /repos/./gitx" ];
127+
128+ XCTAssertNil (paths[@" refs/heads/master" ]);
129+ }
130+
96131- (void )testTheWorktreeHoldingABranchIsFoundByItsRef
97132{
98133 [self useWorktreePaths: @{@" refs/heads/feature" : @" /repos/gitx-feature" }];
0 commit comments