Skip to content

Commit 038e330

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 the commit list 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 commit list label, and name the worktree holding them in the row's tooltip * Offer that worktree in place of Checkout, and leave Remove out of the menu, both decided 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 3a4a41d commit 038e330

16 files changed

Lines changed: 364 additions & 8 deletions

‎Classes/Controllers/PBGitHistoryController.m‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,11 +1111,18 @@ @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 “%@”", @"Contextual Menu Item to open the worktree holding the selected branch"), worktreePath.lastPathComponent];
1120+
[items addObject:[NSMenuItem pb_itemWithTitle:openWorktreeTitle action:@selector(openWorktree:) enabled:YES]];
1121+
} else {
1122+
// checkout ref
1123+
NSString *checkoutTitle = [NSString stringWithFormat:NSLocalizedString(@"Checkout “%@”", @"Contextual Menu Item to check out the selected ref"), refName];
1124+
[items addObject:[NSMenuItem pb_itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
1125+
}
11191126
[items addObject:[NSMenuItem separatorItem]];
11201127

11211128
// create branch
@@ -1200,7 +1207,7 @@ @implementation PBGitHistoryController (PBContextMenu)
12001207
// delete ref
12011208
[items addObject:[NSMenuItem separatorItem]];
12021209
BOOL isStash = [[ref ref] hasPrefix:@"refs/stash"];
1203-
BOOL isDeleteEnabled = !(isDetachedHead || isHead || isStash);
1210+
BOOL isDeleteEnabled = !(isDetachedHead || isHead || isStash || worktreePath);
12041211
if (isDeleteEnabled) {
12051212
NSString *deleteItemTitle = nil;
12061213
if (ref.isRemoteBranch) {

‎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/Views/PBGitRevisionCell.m‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ - (NSColor *)colorForRef:(PBGitRef *)ref
221221

222222
NSString *type = [ref type];
223223
if ([type isEqualToString:@"head"]) {
224+
if ([[controller repository] isRefHeldByAnotherWorktree:ref]) {
225+
return [NSColor colorWithCalibratedRed:0X76 / 256.0 green:0Xd9 / 256.0 blue:0Xcd / 256.0 alpha:1.0];
226+
}
227+
224228
return [NSColor colorWithCalibratedRed:0X9a / 256.0 green:0Xe2 / 256.0 blue:0X84 / 256.0 alpha:1.0];
225229
} else if ([type isEqualToString:@"remote"]) {
226230
return [NSColor colorWithCalibratedRed:0xa2 / 256.0 green:0Xcf / 256.0 blue:0Xef / 256.0 alpha:1.0];

‎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

‎Classes/Views/PBSourceViewItem.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ NS_ASSUME_NONNULL_BEGIN
3434

3535
- (nullable PBGitRef *)ref;
3636

37+
+ (NSImage *)iconNamed:(NSString *)name;
38+
3739
@property (readonly) NSString *title;
3840
@property (readonly) NSArray *sortedChildren;
3941
@property (getter=isGroupItem) BOOL groupItem;

‎Classes/Views/PBSourceViewItem.m‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,19 @@ - (PBSourceViewItem *)findRev:(PBGitRevSpecifier *)rev
141141
return nil;
142142
}
143143

144-
- (NSImage *)icon
144+
+ (NSImage *)iconNamed:(NSString *)name
145145
{
146-
NSImage *iconImage = [NSImage imageNamed:self.iconName];
146+
NSImage *iconImage = [NSImage imageNamed:name];
147147
[iconImage setSize:NSMakeSize(16, 16)];
148148
[iconImage setCacheMode:NSImageCacheAlways];
149149
return iconImage;
150150
}
151151

152+
- (NSImage *)icon
153+
{
154+
return [PBSourceViewItem iconNamed:self.iconName];
155+
}
156+
152157
- (NSString *)title
153158
{
154159
if (_title)

‎Classes/git/PBGitRepository.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ typedef NS_ENUM(NSInteger, PBGitConfigScope) {
7070
- (BOOL)pullBranch:(PBGitRef *)branchRef fromRemote:(PBGitRef *)remoteRef rebase:(BOOL)rebase error:(NSError **)error;
7171
- (BOOL)pushBranch:(PBGitRef *)branchRef toRemote:(PBGitRef *)remoteRef error:(NSError **)error;
7272

73+
- (nullable NSString *)pathOfWorktreeHoldingRef:(nullable PBGitRef *)ref;
74+
- (BOOL)isRefHeldByAnotherWorktree:(nullable PBGitRef *)ref;
75+
7376
- (BOOL)checkoutRefish:(id<PBGitRefish>)ref error:(NSError **)error;
7477
- (BOOL)checkoutFiles:(NSArray *)files fromRefish:(id<PBGitRefish>)ref error:(NSError **)error;
7578
- (BOOL)mergeWithRefish:(id<PBGitRefish>)ref error:(NSError **)error;

0 commit comments

Comments
 (0)