Skip to content

Commit f9db304

Browse files
committed
Keep a branch two worktrees share marked as held (issue gitx#629)
Dropping our own checkout by branch name also dropped the other worktree holding that same branch, which `git worktree add --force` allows, so the one case the marking exists for went unmarked. The details pane had a second gap: refreshing a commit whose refs changed never refreshed which branch is current, so the label stayed on the branch HEAD had left. * Leave out our own worktree by the path it sits at, so a branch shared with another worktree is still reported as held there * Name the current branch again when the details pane refreshes in place, not only when it loads a different commit
1 parent e48132c commit f9db304

4 files changed

Lines changed: 63 additions & 17 deletions

File tree

‎Classes/Controllers/PBWebHistoryController.m‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ - (void)changeContentToCommit:(PBGitCommit *)commit
121121
PBGitRepository *repository = [historyController repository];
122122

123123
if ([currentOID isEqual:commit.OID]) {
124-
[[self script] callWebScriptMethod:@"reload" withArguments:@[ [repository refNamesHeldByOtherWorktrees] ]];
124+
NSArray *refreshed = @[ [[repository headRef] simpleRef] ?: @"", [repository refNamesHeldByOtherWorktrees] ];
125+
[[self script] callWebScriptMethod:@"reload" withArguments:refreshed];
125126
return;
126127
}
127128

‎Classes/git/PBGitRepository.m‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,23 +277,31 @@ - (void)reloadRefs
277277
[self didChangeValueForKey:@"stashes"];
278278
}
279279

280-
+ (NSDictionary<NSString *, NSString *> *)worktreePathsFromPorcelain:(NSString *)output excludingRef:(NSString *)ourRef
280+
+ (BOOL)worktreeAtPath:(NSString *)path isTheOneAt:(NSString *)ourPath
281+
{
282+
if (!path || !ourPath)
283+
return NO;
284+
285+
return [path.stringByStandardizingPath isEqualToString:ourPath.stringByStandardizingPath];
286+
}
287+
288+
+ (NSDictionary<NSString *, NSString *> *)worktreePathsFromPorcelain:(NSString *)output excludingWorktreeAtPath:(NSString *)ourPath
281289
{
282290
NSMutableDictionary *paths = [NSMutableDictionary dictionary];
283291
NSString *worktreePath = nil;
284292

285293
for (NSString *line in [output componentsSeparatedByString:@"\n"]) {
286-
if ([line hasPrefix:@"worktree "])
294+
if ([line hasPrefix:@"worktree "]) {
287295
worktreePath = [line substringFromIndex:[@"worktree " length]];
288-
else if ([line hasPrefix:@"branch "] && worktreePath)
289-
paths[[line substringFromIndex:[@"branch " length]]] = worktreePath;
290-
else if (line.length == 0)
296+
} else if ([line hasPrefix:@"branch "] && worktreePath) {
297+
NSString *ref = [line substringFromIndex:[@"branch " length]];
298+
if (![self worktreeAtPath:worktreePath isTheOneAt:ourPath] && !paths[ref])
299+
paths[ref] = worktreePath;
300+
} else if (line.length == 0) {
291301
worktreePath = nil;
302+
}
292303
}
293304

294-
if (ourRef)
295-
[paths removeObjectForKey:ourRef];
296-
297305
return paths;
298306
}
299307

@@ -309,7 +317,7 @@ - (void)reloadRefs
309317
if (!output)
310318
return @{};
311319

312-
_worktreePathsByRefName = [PBGitRepository worktreePathsFromPorcelain:output excludingRef:[[self headRef] simpleRef]];
320+
_worktreePathsByRefName = [PBGitRepository worktreePathsFromPorcelain:output excludingWorktreeAtPath:self.workingDirectory];
313321

314322
return _worktreePathsByRefName;
315323
}

‎GitXTests/PBGitRepositoryWorktreeTests.m‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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\nbare\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"}];

‎Resources/html/views/history/history.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ var selectCommit = function(a) {
130130
};
131131

132132
// Relead only refs
133-
var reload = function(worktreeRefs) {
133+
var reload = function(currentRef, worktreeRefs) {
134134
$("notification").classList.add("hidden");
135+
if (currentRef)
136+
commit.currentRef = currentRef;
135137
if (worktreeRefs)
136138
commit.worktreeRefs = worktreeRefs;
137139
commit.reloadRefs();

0 commit comments

Comments
 (0)