Skip to content

Commit 69d6782

Browse files
committed
Add Checkout
Checkout: - PBGitRepository: add checkoutRefish: - PBRefController: renamed checkoutRef: to checkout: and use new method in PBGitRepository - add to the contextual menus for commits (it was already in the menu for refs) - add "Checkout branch" checkbox to the Create Branch sheet - store the state in the defaults - tell the commitController to rearrangeObjects so that the refs highlight correctly (seems to only be a problem when going from a branch to a detached HEAD at the same commit)
1 parent aa4a24a commit 69d6782

10 files changed

+134
-29
lines changed

English.lproj/PBCreateBranchSheet.xib

+65-13
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,33 @@
198198
</object>
199199
</object>
200200
</object>
201+
<object class="NSButton" id="502264956">
202+
<reference key="NSNextResponder" ref="1006"/>
203+
<int key="NSvFlags">268</int>
204+
<string key="NSFrame">{{122, 58}, {129, 18}}</string>
205+
<reference key="NSSuperview" ref="1006"/>
206+
<bool key="NSEnabled">YES</bool>
207+
<object class="NSButtonCell" key="NSCell" id="742397723">
208+
<int key="NSCellFlags">-2080244224</int>
209+
<int key="NSCellFlags2">0</int>
210+
<string key="NSContents">Checkout branch</string>
211+
<reference key="NSSupport" ref="850782281"/>
212+
<reference key="NSControlView" ref="502264956"/>
213+
<int key="NSButtonFlags">1211912703</int>
214+
<int key="NSButtonFlags2">2</int>
215+
<object class="NSCustomResource" key="NSNormalImage">
216+
<string key="NSClassName">NSImage</string>
217+
<string key="NSResourceName">NSSwitch</string>
218+
</object>
219+
<object class="NSButtonImageSource" key="NSAlternateImage">
220+
<string key="NSImageName">NSSwitch</string>
221+
</object>
222+
<string key="NSAlternateContents"/>
223+
<string key="NSKeyEquivalent"/>
224+
<int key="NSPeriodicDelay">200</int>
225+
<int key="NSPeriodicInterval">25</int>
226+
</object>
227+
</object>
201228
</object>
202229
<string key="NSFrameSize">{480, 167}</string>
203230
<reference key="NSSuperview"/>
@@ -258,6 +285,22 @@
258285
</object>
259286
<int key="connectionID">47</int>
260287
</object>
288+
<object class="IBConnectionRecord">
289+
<object class="IBBindingConnection" key="connection">
290+
<string key="label">value: shouldCheckoutBranch</string>
291+
<reference key="source" ref="502264956"/>
292+
<reference key="destination" ref="1001"/>
293+
<object class="NSNibBindingConnector" key="connector">
294+
<reference key="NSSource" ref="502264956"/>
295+
<reference key="NSDestination" ref="1001"/>
296+
<string key="NSLabel">value: shouldCheckoutBranch</string>
297+
<string key="NSBinding">value</string>
298+
<string key="NSKeyPath">shouldCheckoutBranch</string>
299+
<int key="NSNibBindingConnectorVersion">2</int>
300+
</object>
301+
</object>
302+
<int key="connectionID">59</int>
303+
</object>
261304
</object>
262305
<object class="IBMutableOrderedSet" key="objectRecords">
263306
<object class="NSArray" key="orderedObjects">
@@ -306,6 +349,7 @@
306349
<reference ref="616021219"/>
307350
<reference ref="900626215"/>
308351
<reference ref="599625527"/>
352+
<reference ref="502264956"/>
309353
</object>
310354
<reference key="parent" ref="1005"/>
311355
</object>
@@ -393,6 +437,20 @@
393437
<reference key="object" ref="296663936"/>
394438
<reference key="parent" ref="900626215"/>
395439
</object>
440+
<object class="IBObjectRecord">
441+
<int key="objectID">56</int>
442+
<reference key="object" ref="502264956"/>
443+
<object class="NSMutableArray" key="children">
444+
<bool key="EncodedWithXMLCoder">YES</bool>
445+
<reference ref="742397723"/>
446+
</object>
447+
<reference key="parent" ref="1006"/>
448+
</object>
449+
<object class="IBObjectRecord">
450+
<int key="objectID">57</int>
451+
<reference key="object" ref="742397723"/>
452+
<reference key="parent" ref="502264956"/>
453+
</object>
396454
</object>
397455
</object>
398456
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -417,6 +475,8 @@
417475
<string>3.IBPluginDependency</string>
418476
<string>4.IBPluginDependency</string>
419477
<string>5.IBPluginDependency</string>
478+
<string>56.IBPluginDependency</string>
479+
<string>57.IBPluginDependency</string>
420480
<string>6.IBPluginDependency</string>
421481
<string>7.IBPluginDependency</string>
422482
<string>8.IBPluginDependency</string>
@@ -444,6 +504,8 @@
444504
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
445505
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
446506
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
507+
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
508+
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
447509
</object>
448510
</object>
449511
<object class="NSMutableDictionary" key="unlocalizedProperties">
@@ -462,7 +524,7 @@
462524
</object>
463525
</object>
464526
<nil key="sourceID"/>
465-
<int key="maxID">55</int>
527+
<int key="maxID">59</int>
466528
</object>
467529
<object class="IBClassDescriber" key="IBDocument.Classes">
468530
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -489,15 +551,13 @@
489551
<bool key="EncodedWithXMLCoder">YES</bool>
490552
<string>branchNameField</string>
491553
<string>errorMessageField</string>
492-
<string>startingAtMenu</string>
493-
<string>startingAtPopup</string>
554+
<string>startRefish</string>
494555
</object>
495556
<object class="NSMutableArray" key="dict.values">
496557
<bool key="EncodedWithXMLCoder">YES</bool>
497558
<string>NSTextField</string>
498559
<string>NSTextField</string>
499-
<string>NSMenu</string>
500-
<string>NSPopUpButton</string>
560+
<string>id</string>
501561
</object>
502562
</object>
503563
<object class="IBClassDescriptionSource" key="sourceIdentifier">
@@ -935,14 +995,6 @@
935995
<string key="minorKey">WebKit.framework/Headers/WebUIDelegate.h</string>
936996
</object>
937997
</object>
938-
<object class="IBPartialClassDescription">
939-
<string key="className">NSPopUpButton</string>
940-
<string key="superclassName">NSButton</string>
941-
<object class="IBClassDescriptionSource" key="sourceIdentifier">
942-
<string key="majorKey">IBFrameworkSource</string>
943-
<string key="minorKey">AppKit.framework/Headers/NSPopUpButton.h</string>
944-
</object>
945-
</object>
946998
<object class="IBPartialClassDescription">
947999
<string key="className">NSResponder</string>
9481000
<object class="IBClassDescriptionSource" key="sourceIdentifier">

PBCreateBranchSheet.h

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
PBGitRepository *repository;
1818
id <PBGitRefish> startRefish;
1919

20+
BOOL shouldCheckoutBranch;
21+
2022
NSTextField *branchNameField;
2123
NSTextField *errorMessageField;
2224
}
@@ -31,6 +33,8 @@
3133
@property (retain) PBGitRepository *repository;
3234
@property (retain) id <PBGitRefish> startRefish;
3335

36+
@property (assign) BOOL shouldCheckoutBranch;
37+
3438
@property (assign) IBOutlet NSTextField *branchNameField;
3539
@property (assign) IBOutlet NSTextField *errorMessageField;
3640

PBCreateBranchSheet.m

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "PBCreateBranchSheet.h"
1010
#import "PBGitRepository.h"
11+
#import "PBGitDefaults.h"
1112
#import "PBGitCommit.h"
1213
#import "PBGitRef.h"
1314

@@ -24,6 +25,8 @@ @implementation PBCreateBranchSheet
2425
@synthesize repository;
2526
@synthesize startRefish;
2627

28+
@synthesize shouldCheckoutBranch;
29+
2730
@synthesize branchNameField;
2831
@synthesize errorMessageField;
2932

@@ -42,10 +45,11 @@ + (void) beginCreateBranchSheetAtRefish:(id <PBGitRefish>)ref inRepository:(PBGi
4245
- (void) beginCreateBranchSheetAtRefish:(id <PBGitRefish>)ref inRepository:(PBGitRepository *)repo
4346
{
4447
self.repository = repo;
45-
self.startRefish = ref;
48+
self.startRefish = ref;
4649

4750
[self window]; // loads the window (if it wasn't already)
4851
[self.errorMessageField setStringValue:@""];
52+
self.shouldCheckoutBranch = [PBGitDefaults shouldCheckoutBranch];
4953

5054
[NSApp beginSheet:[self window] modalForWindow:[self.repository.windowController window] modalDelegate:self didEndSelector:nil contextInfo:NULL];
5155
}
@@ -57,14 +61,14 @@ - (void) beginCreateBranchSheetAtRefish:(id <PBGitRefish>)ref inRepository:(PBGi
5761
- (IBAction) createBranch:(id)sender
5862
{
5963
NSString *name = [self.branchNameField stringValue];
60-
PBGitRef *ref = [PBGitRef refFromString:[@"refs/heads/" stringByAppendingString:name]];
64+
PBGitRef *ref = [PBGitRef refFromString:[kGitXBranchRefPrefix stringByAppendingString:name]];
6165

6266
if (![self.repository checkRefFormat:[ref ref]]) {
6367
[self.errorMessageField setStringValue:@"Invalid name"];
6468
[self.errorMessageField setHidden:NO];
6569
return;
6670
}
67-
71+
6872
if ([self.repository refExists:ref]) {
6973
[self.errorMessageField setStringValue:@"Branch already exists"];
7074
[self.errorMessageField setHidden:NO];
@@ -74,6 +78,11 @@ - (IBAction) createBranch:(id)sender
7478
[self closeCreateBranchSheet:self];
7579

7680
[self.repository createBranch:name atRefish:self.startRefish];
81+
82+
[PBGitDefaults setShouldCheckoutBranch:self.shouldCheckoutBranch];
83+
84+
if (self.shouldCheckoutBranch)
85+
[self.repository checkoutRefish:ref];
7786
}
7887

7988

PBGitDefaults.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@
1919
+ (BOOL)showWhitespaceDifferences;
2020
+ (BOOL)openCurDirOnLaunch;
2121
+ (BOOL)showOpenPanelOnLaunch;
22+
+ (BOOL) shouldCheckoutBranch;
23+
+ (void) setShouldCheckoutBranch:(BOOL)shouldCheckout;
2224

2325
@end

PBGitDefaults.m

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define kShowWhitespaceDifferences @"PBShowWhitespaceDifferences"
1818
#define kOpenCurDirOnLaunch @"PBOpenCurDirOnLaunch"
1919
#define kShowOpenPanelOnLaunch @"PBShowOpenPanelOnLaunch"
20+
#define kShouldCheckoutBranch @"PBShouldCheckoutBranch"
2021

2122
@implementation PBGitDefaults
2223

@@ -39,6 +40,8 @@ + (void)initialize
3940
forKey:kOpenCurDirOnLaunch];
4041
[defaultValues setObject:[NSNumber numberWithBool:YES]
4142
forKey:kShowOpenPanelOnLaunch];
43+
[defaultValues setObject:[NSNumber numberWithBool:YES]
44+
forKey:kShouldCheckoutBranch];
4245
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
4346
}
4447

@@ -82,4 +85,14 @@ + (BOOL)showOpenPanelOnLaunch
8285
return [[NSUserDefaults standardUserDefaults] boolForKey:kShowOpenPanelOnLaunch];
8386
}
8487

88+
+ (BOOL) shouldCheckoutBranch
89+
{
90+
return [[NSUserDefaults standardUserDefaults] boolForKey:kShouldCheckoutBranch];
91+
}
92+
93+
+ (void) setShouldCheckoutBranch:(BOOL)shouldCheckout
94+
{
95+
[[NSUserDefaults standardUserDefaults] setBool:shouldCheckout forKey:kShouldCheckoutBranch];
96+
}
97+
8598
@end

PBGitRepository.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern NSString* PBGitRepositoryErrorDomain;
2929
PBGitRevSpecifier *_headRef; // Caching
3030
}
3131

32+
- (BOOL) checkoutRefish:(id <PBGitRefish>)ref;
3233
- (BOOL) createBranch:(NSString *)branchName atRefish:(id <PBGitRefish>)ref;
3334
- (BOOL) createTag:(NSString *)tagName message:(NSString *)message atRefish:(id <PBGitRefish>)commitSHA;
3435

PBGitRepository.m

+22
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,28 @@ - (NSString *) workingDirectory
428428

429429
#pragma mark Repository commands
430430

431+
- (BOOL) checkoutRefish:(id <PBGitRefish>)ref
432+
{
433+
NSString *refName = nil;
434+
if ([ref refishType] == kGitXBranchType)
435+
refName = [ref shortName];
436+
else
437+
refName = [ref refishName];
438+
439+
int retValue = 1;
440+
NSArray *arguments = [NSArray arrayWithObjects:@"checkout", refName, nil];
441+
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
442+
if (retValue) {
443+
NSString *message = [NSString stringWithFormat:@"There was an error checking out the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]];
444+
[self.windowController showErrorSheetTitle:@"Checkout failed!" message:message arguments:arguments output:output];
445+
return NO;
446+
}
447+
448+
[self reloadRefs];
449+
[self readCurrentBranch];
450+
return YES;
451+
}
452+
431453
- (BOOL) createBranch:(NSString *)branchName atRefish:(id <PBGitRefish>)ref
432454
{
433455
if (!branchName || !ref)

PBRefController.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
IBOutlet NSPopUpButton *branchPopUp;
2424
}
2525

26+
- (void) checkout:(PBRefMenuItem *)sender;
2627
- (void) createBranch:(PBRefMenuItem *)sender;
2728
- (void) copySHA:(PBRefMenuItem *)sender;
2829
- (void) copyPatch:(PBRefMenuItem *)sender;

PBRefController.m

+10-12
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(
3131
}
3232
else if ([(NSString *)context isEqualToString:@"currentBranchChange"]) {
3333
[self selectCurrentBranch];
34+
[commitController rearrangeObjects];
3435
}
3536
else {
3637
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
3738
}
3839
}
3940

4041

42+
#pragma mark Checkout
43+
44+
- (void) checkout:(PBRefMenuItem *)sender
45+
{
46+
id <PBGitRefish> refish = [sender refish];
47+
[historyController.repository checkoutRefish:refish];
48+
}
49+
50+
4151
#pragma mark Create Branch
4252

4353
- (void) createBranch:(PBRefMenuItem *)sender
@@ -125,18 +135,6 @@ - (void) removeRef:(PBRefMenuItem *)sender
125135
NSBeginAlertSheet([NSString stringWithFormat:@"Delete %@?", ref_desc], @"Delete", @"Cancel", nil, [[historyController view] window], self, @selector(removeRefSheetDidEnd:returnCode:contextInfo:), NULL, sender, question);
126136
}
127137

128-
- (void) checkoutRef:(PBRefMenuItem *)sender
129-
{
130-
int ret = 1;
131-
[historyController.repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", [[sender refish] shortName], nil] retValue: &ret];
132-
if (ret) {
133-
[[historyController.repository windowController] showMessageSheet:@"Checking out branch failed" infoText:@"There was an error checking out the branch. Perhaps your working directory is not clean?"];
134-
return;
135-
}
136-
[historyController.repository reloadRefs];
137-
[commitController rearrangeObjects];
138-
}
139-
140138

141139
#pragma mark Contextual menus
142140

PBRefMenuItem.m

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ + (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitReposito
4545

4646
// checkout ref
4747
NSString *checkoutTitle = [@"Checkout " stringByAppendingString:targetRefName];
48-
[items addObject:[PBRefMenuItem itemWithTitle:checkoutTitle action:@selector(checkoutRef:) enabled:!isHead]];
48+
[items addObject:[PBRefMenuItem itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
4949
[items addObject:[PBRefMenuItem separatorItem]];
5050

5151
// create branch
@@ -76,6 +76,9 @@ + (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target
7676
{
7777
NSMutableArray *items = [NSMutableArray array];
7878

79+
[items addObject:[PBRefMenuItem itemWithTitle:@"Checkout Commit" action:@selector(checkout:) enabled:YES]];
80+
[items addObject:[PBRefMenuItem separatorItem]];
81+
7982
[items addObject:[PBRefMenuItem itemWithTitle:@"Create Branch…" action:@selector(createBranch:) enabled:YES]];
8083
[items addObject:[PBRefMenuItem itemWithTitle:@"Create Tag…" action:@selector(createTag:) enabled:YES]];
8184
[items addObject:[PBRefMenuItem separatorItem]];

0 commit comments

Comments
 (0)