Skip to content

Commit deba78e

Browse files
committed
Add basic support for worktree branches (issue gitx#629)
A branch checked out somewhere else cannot be checked out here. GitX showed such a branch like any other, in the sidebar and in both commit panes alike, and went on to offer actions that git refuses or, worse, that `update-ref -d` carries out and leaves the other worktree pointing at a branch that is gone. * Read `git worktree list --porcelain` and answer which worktree holds a given ref, our own excluded, re-read whenever the refs are reloaded * Give those branches their own sidebar icon and a teal label in both commit panes, and name the worktree holding them in the row's tooltip * Offer that worktree in place of Checkout, and grey out Remove with a tooltip saying which worktree holds it, both settled while the menu is built * Refuse the removal in the repository itself, so a caller that never asked cannot walk into it either
1 parent bdadbad commit deba78e

21 files changed

Lines changed: 420 additions & 12 deletions

‎Classes/Controllers/PBGitHistoryController.m‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,11 +1111,20 @@ @implementation PBGitHistoryController (PBContextMenu)
11111111
BOOL hasRemote = (remoteName ? YES : NO);
11121112
BOOL isRemote = (ref.isRemote && !ref.isRemoteBranch);
11131113

1114+
NSString *worktreePath = [self.repository pathOfWorktreeHoldingRef:ref];
1115+
11141116
NSMutableArray *items = [NSMutableArray array];
11151117
if (!isRemote) {
1116-
// checkout ref
1117-
NSString *checkoutTitle = [NSString stringWithFormat:NSLocalizedString(@"Checkout “%@”", @"Contextual Menu Item to check out the selected ref"), refName];
1118-
[items addObject:[NSMenuItem pb_itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
1118+
if (worktreePath) {
1119+
NSString *openWorktreeTitle = [NSString stringWithFormat:NSLocalizedString(@"Open Worktree of Branch “%@”", @"Contextual Menu Item to open the worktree holding the selected branch"), refName];
1120+
NSMenuItem *openWorktreeItem = [NSMenuItem pb_itemWithTitle:openWorktreeTitle action:@selector(openWorktree:) enabled:YES];
1121+
openWorktreeItem.toolTip = worktreePath;
1122+
[items addObject:openWorktreeItem];
1123+
} else {
1124+
// checkout ref
1125+
NSString *checkoutTitle = [NSString stringWithFormat:NSLocalizedString(@"Checkout “%@”", @"Contextual Menu Item to check out the selected ref"), refName];
1126+
[items addObject:[NSMenuItem pb_itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
1127+
}
11191128
[items addObject:[NSMenuItem separatorItem]];
11201129

11211130
// create branch
@@ -1210,7 +1219,8 @@ @implementation PBGitHistoryController (PBContextMenu)
12101219
} else {
12111220
deleteItemTitle = [NSString stringWithFormat:NSLocalizedString(@"Remove “%@”…", @"Contextual Menu Item to remove a local ref (e.g. branch)"), refName];
12121221
}
1213-
NSMenuItem *deleteItem = [NSMenuItem pb_itemWithTitle:deleteItemTitle action:@selector(deleteRef:) enabled:YES];
1222+
NSMenuItem *deleteItem = [NSMenuItem pb_itemWithTitle:deleteItemTitle action:@selector(deleteRef:) enabled:!worktreePath];
1223+
deleteItem.toolTip = worktreePath ? [NSString stringWithFormat:NSLocalizedString(@"Checked out in the worktree at %@", @"Contextual Menu Item tooltip for a branch that cannot be removed because a worktree holds it"), worktreePath] : nil;
12141224
[items addObject:deleteItem];
12151225
}
12161226

‎Classes/Controllers/PBGitSidebarController.m‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,13 @@ - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTable
359359
{
360360
PBSidebarTableViewCell *cell = [outlineView makeViewWithIdentifier:PBSidebarCellIdentifier owner:outlineView];
361361

362+
BOOL isCheckedOut = [item.revSpecifier isEqual:[self.repository headRef]];
363+
NSString *worktreePath = [self.repository pathOfWorktreeHoldingRef:item.ref];
364+
362365
cell.textField.stringValue = [[item title] copy];
363-
cell.imageView.image = item.icon;
364-
cell.isCheckedOut = [item.revSpecifier isEqual:[self.repository headRef]];
366+
cell.imageView.image = worktreePath.length ? [PBSourceViewItem iconNamed:@"WorktreeBranchTemplate"] : item.icon;
367+
cell.isCheckedOut = isCheckedOut;
368+
cell.worktreePath = worktreePath;
365369

366370
return cell;
367371
}

‎Classes/Controllers/PBGitWindowController.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
5151
- (IBAction)refresh:(id)sender;
5252

5353
- (IBAction)checkout:(id)sender;
54+
- (IBAction)openWorktree:(id)sender;
5455
- (IBAction)createBranch:(id)sender;
5556
- (IBAction)createTag:(id)sender;
5657
- (IBAction)merge:(id)sender;

‎Classes/Controllers/PBGitWindowController.m‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,21 @@ - (IBAction)checkout:(id)sender
708708
}
709709
}
710710

711+
- (IBAction)openWorktree:(id)sender
712+
{
713+
id<PBGitRefish> refish = [self refishForSender:sender refishTypes:@[ kGitXBranchType ]];
714+
NSString *worktreePath = [self.repository pathOfWorktreeHoldingRef:(PBGitRef *)refish];
715+
if (!worktreePath) return;
716+
717+
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:worktreePath]
718+
display:YES
719+
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) {
720+
if (error) {
721+
[self showErrorSheet:error];
722+
}
723+
}];
724+
}
725+
711726
- (IBAction)merge:(id)sender
712727
{
713728
id<PBGitRefish> refish = [self refishForSender:sender refishTypes:@[ kGitXBranchType, kGitXRemoteBranchType, kGitXCommitType, kGitXTagType ]];

‎Classes/Controllers/PBWebHistoryController.m‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,14 @@ static NSUInteger reallyGetFileSize(GTRepository *repo, GTDiffFile *file)
118118
- (void)changeContentToCommit:(PBGitCommit *)commit
119119
{
120120
// The sha is the same, but refs may have changed. reload it lazy
121+
PBGitRepository *repository = [historyController repository];
122+
121123
if ([currentOID isEqual:commit.OID]) {
122-
[[self script] callWebScriptMethod:@"reload" withArguments:nil];
124+
[[self script] callWebScriptMethod:@"reload" withArguments:@[ [repository refNamesHeldByOtherWorktrees] ]];
123125
return;
124126
}
125127

126-
NSArray *arguments = @[ commit, [[[historyController repository] headRef] simpleRef] ];
128+
NSArray *arguments = @[ commit, [[repository headRef] simpleRef], [repository refNamesHeldByOtherWorktrees] ];
127129
id scriptResult = [[self script] callWebScriptMethod:@"loadCommit" withArguments:arguments];
128130
if (!scriptResult) {
129131
// the web view is not really ready for scripting???

‎Classes/Views/PBGitRefLabelColors.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
1313

1414
+ (NSColor *)colorForRefType:(nullable NSString *)type;
1515
+ (NSColor *)currentBranchColor;
16+
+ (NSColor *)worktreeBranchColor;
1617

1718
+ (NSDictionary<NSString *, NSString *> *)CSSColors;
1819

‎Classes/Views/PBGitRefLabelColors.m‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ @implementation PBGitRefLabelColors
1515
@"head" : @"#9ae284",
1616
@"remote" : @"#a2cfef",
1717
@"tag" : @"#fced6f",
18+
@"worktree" : @"#8fe0d5",
1819
@"currentBranch" : @"#fca64f",
1920
};
2021
}
@@ -44,6 +45,11 @@ + (NSColor *)currentBranchColor
4445
return [self colorNamed:@"currentBranch"];
4546
}
4647

48+
+ (NSColor *)worktreeBranchColor
49+
{
50+
return [self colorNamed:@"worktree"];
51+
}
52+
4753
@end
4854

4955
NS_ASSUME_NONNULL_END

‎Classes/Views/PBGitRevisionCell.m‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ - (NSColor *)colorForRef:(PBGitRef *)ref
219219
if (isHEAD)
220220
return [PBGitRefLabelColors currentBranchColor];
221221

222+
if ([[controller repository] isRefHeldByAnotherWorktree:ref])
223+
return [PBGitRefLabelColors worktreeBranchColor];
224+
222225
return [PBGitRefLabelColors colorForRefType:[ref type]];
223226
}
224227

‎Classes/Views/PBSidebarTableViewCell.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
1414
}
1515

1616
@property (assign, nonatomic) BOOL isCheckedOut;
17+
@property (copy, nonatomic, nullable) NSString *worktreePath;
1718

1819
NS_ASSUME_NONNULL_END
1920

‎Classes/Views/PBSidebarTableViewCell.m‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ - (void)setIsCheckedOut:(BOOL)isCheckedOut
2121
[self updateCheckmarkImage];
2222
}
2323

24+
- (void)setWorktreePath:(nullable NSString *)worktreePath
25+
{
26+
_worktreePath = [worktreePath copy];
27+
[self updateCheckmarkImage];
28+
}
29+
2430
- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle
2531
{
2632
[super setBackgroundStyle:backgroundStyle];
@@ -38,6 +44,8 @@ - (void)updateCheckmarkImage
3844
}
3945

4046
[checkedOutImageView setHidden:!_isCheckedOut];
47+
48+
self.toolTip = _worktreePath.length ? [NSString stringWithFormat:NSLocalizedString(@"Checked out in the worktree at %@", @"Sidebar tooltip for a branch held by another worktree"), _worktreePath] : nil;
4149
}
4250

4351
@end

0 commit comments

Comments
 (0)