Skip to content

Commit f47fd3c

Browse files
slazyklaullon
authored andcommitted
Feature: reset current branch to here
1 parent 00bb148 commit f47fd3c

9 files changed

+1248
-33
lines changed

Controller/PBGitResetController.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
//
88

99
#import <Cocoa/Cocoa.h>
10+
#import "PBResetSheet.h"
1011

1112
@class PBGitRepository;
13+
@protocol PBGitRefish;
1214

1315
@interface PBGitResetController : NSObject {
1416
PBGitRepository *repository;
@@ -17,8 +19,8 @@
1719

1820
- (NSArray *) menuItems;
1921

20-
2122
// actions
23+
- (void) resetToRefish: (id<PBGitRefish>) spec type: (PBResetType) type;
2224
- (void) resetHardToHead;
2325

2426
@end

Controller/PBGitResetController.m

+7-32
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#import "PBGitResetController.h"
1010
#import "PBGitRepository.h"
1111
#import "PBCommand.h"
12+
#import "PBGitRefish.h"
13+
#import "PBResetSheet.h"
1214

13-
static NSString * const kCommandKey = @"command";
1415

1516
@implementation PBGitResetController
1617

@@ -22,23 +23,11 @@ - (id) initWithRepository:(PBGitRepository *) repo {
2223
}
2324

2425
- (void) resetHardToHead {
25-
NSAlert *alert = [NSAlert alertWithMessageText:@"Reseting working copy and index"
26-
defaultButton:@"Cancel"
27-
alternateButton:nil
28-
otherButton:@"Reset"
29-
informativeTextWithFormat:@"Are you sure you want to reset your working copy and index? All changes to them will be gone!"];
30-
31-
NSArray *arguments = [NSArray arrayWithObjects:@"reset", @"--hard", @"HEAD", nil];
32-
PBCommand *cmd = [[PBCommand alloc] initWithDisplayName:@"Reset hard to HEAD" parameters:arguments repository:repository];
33-
cmd.commandTitle = cmd.displayName;
34-
cmd.commandDescription = @"Reseting head";
35-
36-
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithObject:cmd forKey:kCommandKey];
37-
38-
[alert beginSheetModalForWindow:[repository.windowController window]
39-
modalDelegate:self
40-
didEndSelector:@selector(confirmResetSheetDidEnd:returnCode:contextInfo:)
41-
contextInfo:info];
26+
[self resetToRefish: [PBGitRef refFromString: @"HEAD"] type: PBResetTypeHard];
27+
}
28+
29+
- (void) resetToRefish:(id<PBGitRefish>) refish type:(PBResetType)type {
30+
[PBResetSheet beginResetSheetForRepository: repository refish: refish andType: type];
4231
}
4332

4433
- (void) reset {
@@ -70,18 +59,4 @@ - (void) dealloc {
7059
[super dealloc];
7160
}
7261

73-
#pragma mark -
74-
#pragma mark Confirm Window
75-
76-
- (void) confirmResetSheetDidEnd:(NSAlert *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
77-
{
78-
[[sheet window] orderOut:nil];
79-
80-
if (returnCode != NSAlertDefaultReturn) {
81-
PBCommand *cmd = [(NSDictionary *)contextInfo objectForKey:kCommandKey];
82-
[cmd invoke];
83-
}
84-
}
85-
86-
8762
@end

GitX.xcodeproj/project.pbxproj

+10
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
47DBDBCA0E95016F00671A1E /* PBNSURLPathUserDefaultsTransfomer.m in Sources */ = {isa = PBXBuildFile; fileRef = 47DBDBC90E95016F00671A1E /* PBNSURLPathUserDefaultsTransfomer.m */; };
7373
551BF11E112F376C00265053 /* gitx_askpasswd_main.m in Sources */ = {isa = PBXBuildFile; fileRef = 551BF11D112F376C00265053 /* gitx_askpasswd_main.m */; };
7474
551BF176112F3F4B00265053 /* gitx_askpasswd in Resources */ = {isa = PBXBuildFile; fileRef = 551BF111112F371800265053 /* gitx_askpasswd */; };
75+
65D58BC4132D27A8003F7290 /* PBResetSheet.xib in Resources */ = {isa = PBXBuildFile; fileRef = 65D58BC3132D27A7003F7290 /* PBResetSheet.xib */; };
76+
65D58BC7132D48C2003F7290 /* PBResetSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = 65D58BC6132D48C0003F7290 /* PBResetSheet.m */; };
7577
770B37ED0679A11B001EADE2 /* GitTest_DataModel.xcdatamodel in Sources */ = {isa = PBXBuildFile; fileRef = 770B37EC0679A11B001EADE2 /* GitTest_DataModel.xcdatamodel */; };
7678
77C8280E06725ACE000B614F /* ApplicationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 77C8280C06725ACE000B614F /* ApplicationController.m */; };
7779
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
@@ -365,6 +367,9 @@
365367
47DBDBC90E95016F00671A1E /* PBNSURLPathUserDefaultsTransfomer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBNSURLPathUserDefaultsTransfomer.m; sourceTree = "<group>"; };
366368
551BF111112F371800265053 /* gitx_askpasswd */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = gitx_askpasswd; sourceTree = BUILT_PRODUCTS_DIR; };
367369
551BF11D112F376C00265053 /* gitx_askpasswd_main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = gitx_askpasswd_main.m; sourceTree = "<group>"; };
370+
65D58BC3132D27A7003F7290 /* PBResetSheet.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PBResetSheet.xib; sourceTree = "<group>"; };
371+
65D58BC5132D48BF003F7290 /* PBResetSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBResetSheet.h; sourceTree = "<group>"; };
372+
65D58BC6132D48C0003F7290 /* PBResetSheet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBResetSheet.m; sourceTree = "<group>"; };
368373
770B37EC0679A11B001EADE2 /* GitTest_DataModel.xcdatamodel */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = wrapper.xcdatamodel; path = GitTest_DataModel.xcdatamodel; sourceTree = "<group>"; };
369374
77C82804067257F0000B614F /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
370375
77C8280B06725ACE000B614F /* ApplicationController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ApplicationController.h; sourceTree = "<group>"; };
@@ -846,6 +851,7 @@
846851
21230ED11285EB5A0046E5A1 /* PBArgumentPicker.xib */,
847852
F58DB55F10566E3900CFDF4A /* PBGitSidebarView.xib */,
848853
D8022FE711E124A0003C21F6 /* PBGitXMessageSheet.xib */,
854+
65D58BC3132D27A7003F7290 /* PBResetSheet.xib */,
849855
);
850856
name = Resources;
851857
sourceTree = "<group>";
@@ -937,6 +943,8 @@
937943
D8083E02111FA33700337480 /* PBCloneRepositoryPanel.m */,
938944
D8022FEB11E124C8003C21F6 /* PBGitXMessageSheet.h */,
939945
D8022FEC11E124C8003C21F6 /* PBGitXMessageSheet.m */,
946+
65D58BC5132D48BF003F7290 /* PBResetSheet.h */,
947+
65D58BC6132D48C0003F7290 /* PBResetSheet.m */,
940948
);
941949
name = Sheets;
942950
sourceTree = "<group>";
@@ -1437,6 +1445,7 @@
14371445
31DAA7ED1317737100463846 /* gitx_l_pub.pem in Resources */,
14381446
316E7202131EE9C600AFBB36 /* list_Template.png in Resources */,
14391447
316E7203131EE9C600AFBB36 /* sidebar_Template.png in Resources */,
1448+
65D58BC4132D27A8003F7290 /* PBResetSheet.xib in Resources */,
14401449
);
14411450
runOnlyForDeploymentPostprocessing = 0;
14421451
};
@@ -1606,6 +1615,7 @@
16061615
217FF0BA12A1CB3300785A65 /* PBSubmoduleController.m in Sources */,
16071616
217FF0BB12A1CB3300785A65 /* PBGitResetController.m in Sources */,
16081617
217FF0BE12A1CB3E00785A65 /* PBRevealWithFinderCommand.m in Sources */,
1618+
65D58BC7132D48C2003F7290 /* PBResetSheet.m in Sources */,
16091619
);
16101620
runOnlyForDeploymentPostprocessing = 0;
16111621
};

PBRefController.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- (void) showConfirmPushRefSheet:(PBGitRef *)ref remote:(PBGitRef *)remoteRef;
3232

3333
- (void) checkout:(PBRefMenuItem *)sender;
34+
- (void) reset:(PBRefMenuItem *)sender;
3435
- (void) merge:(PBRefMenuItem *)sender;
3536
- (void) cherryPick:(PBRefMenuItem *)sender;
3637
- (void) rebaseHeadBranch:(PBRefMenuItem *)sender;

PBRefController.m

+7
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ - (void) checkout:(PBRefMenuItem *)sender
149149
[historyController.repository checkoutRefish:refish];
150150
}
151151

152+
#pragma mark Reset
153+
154+
- (void) reset:(PBRefMenuItem *)sender
155+
{
156+
id <PBGitRefish> refish = [sender refish];
157+
[historyController.repository.resetController resetToRefish: refish type: PBResetTypeMixed];
158+
}
152159

153160
#pragma mark Cherry Pick
154161

PBRefMenuItem.m

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ + (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitReposito
6161
[items addObject:[PBRefMenuItem itemWithTitle:checkoutTitle action:@selector(checkout:) enabled:!isHead]];
6262
[items addObject:[PBRefMenuItem separatorItem]];
6363

64+
NSString *resetTitle = [NSString stringWithFormat:@"Reset %@ to %@", headRefName, targetRefName];
65+
[items addObject:[PBRefMenuItem itemWithTitle: resetTitle action:@selector(reset:) enabled:YES]];
66+
[items addObject:[PBRefMenuItem separatorItem]];
67+
6468
// create branch
6569
NSString *createBranchTitle = [ref isRemoteBranch] ? [NSString stringWithFormat:@"Create branch that tracks %@", targetRefName] : @"Create branch…";
6670
[items addObject:[PBRefMenuItem itemWithTitle:createBranchTitle action:@selector(createBranch:) enabled:YES]];
@@ -156,7 +160,12 @@ + (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target
156160

157161
[items addObject:[PBRefMenuItem itemWithTitle:@"Checkout Commit" action:@selector(checkout:) enabled:YES]];
158162
[items addObject:[PBRefMenuItem separatorItem]];
163+
164+
NSString *resetTitle = [NSString stringWithFormat:@"Reset %@ to here", headBranchName];
165+
[items addObject:[PBRefMenuItem itemWithTitle: resetTitle action:@selector(reset:) enabled:YES]];
166+
[items addObject:[PBRefMenuItem separatorItem]];
159167

168+
160169
[items addObject:[PBRefMenuItem itemWithTitle:@"Create Branch…" action:@selector(createBranch:) enabled:YES]];
161170
[items addObject:[PBRefMenuItem itemWithTitle:@"Create Tag…" action:@selector(createTag:) enabled:YES]];
162171
[items addObject:[PBRefMenuItem separatorItem]];

PBResetSheet.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// PBResetSheet.h
3+
// GitX
4+
//
5+
// Created by Leszek Slazynski on 11-03-13.
6+
// Copyright 2011 LSL. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
11+
@protocol PBGitRefish;
12+
@class PBGitRepository;
13+
14+
typedef enum PBResetType {
15+
PBResetTypeNone,
16+
PBResetTypeSoft,
17+
PBResetTypeMixed,
18+
PBResetTypeHard,
19+
PBResetTypeMerge,
20+
PBResetTypeKeep
21+
} PBResetType;
22+
23+
@interface PBResetSheet : NSWindowController {
24+
IBOutlet NSSegmentedControl* resetType;
25+
IBOutlet NSTabView* resetDesc;
26+
PBResetType defaultType;
27+
id<PBGitRefish> targetRefish;
28+
PBGitRepository* repository;
29+
}
30+
31+
+ (void) beginResetSheetForRepository:(PBGitRepository*) repo refish:(id<PBGitRefish>)refish andType:(PBResetType)type;
32+
- (IBAction)resetBranch:(id)sender;
33+
- (IBAction)cancel:(id)sender;
34+
35+
@end

PBResetSheet.m

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// PBResetSheet.m
3+
// GitX
4+
//
5+
// Created by Leszek Slazynski on 11-03-13.
6+
// Copyright 2011 LSL. All rights reserved.
7+
//
8+
9+
#import "PBResetSheet.h"
10+
#import "PBGitRefish.h"
11+
#import "PBCommand.h"
12+
#import "PBGitRepository.h"
13+
14+
static const char* StringFromResetType(PBResetType type) {
15+
static const char* resetTypes[] = {
16+
"none", "soft", "mixed", "hard", "merge", "keep"
17+
};
18+
return resetTypes[type];
19+
}
20+
21+
@implementation PBResetSheet
22+
23+
- (void) beginResetSheetForRepository:(PBGitRepository*) repo refish:(id<PBGitRefish>)refish andType:(PBResetType)type {
24+
defaultType = type;
25+
targetRefish = refish;
26+
repository = repo;
27+
[NSApp beginSheet: [self window]
28+
modalForWindow: [[repository windowController] window]
29+
modalDelegate: self
30+
didEndSelector: nil
31+
contextInfo: NULL];
32+
}
33+
34+
+ (void) beginResetSheetForRepository:(PBGitRepository*) repo refish:(id<PBGitRefish>)refish andType:(PBResetType)type {
35+
PBResetSheet* sheet = [[self alloc] initWithWindowNibName: @"PBResetSheet"];
36+
[sheet beginResetSheetForRepository: repo refish: refish andType: type];
37+
}
38+
39+
- (id) init {
40+
if ( (self = [super initWithWindowNibName: @"PBResetSheet"]) ) {
41+
defaultType = PBResetTypeMixed;
42+
}
43+
return self;
44+
}
45+
46+
- (void) windowDidLoad {
47+
[resetType setSelectedSegment: defaultType - 1];
48+
[resetDesc selectTabViewItemAtIndex: defaultType - 1];
49+
}
50+
51+
- (IBAction)resetBranch:(id)sender {
52+
[NSApp endSheet:[self window]];
53+
[[self window] orderOut:self];
54+
PBResetType type = [resetType selectedSegment] + 1;
55+
56+
NSString* type_arg = [NSString stringWithFormat: @"--%s", StringFromResetType(type)];
57+
NSArray *arguments = [NSArray arrayWithObjects:@"reset", type_arg, [targetRefish refishName], nil];
58+
PBCommand *cmd = [[PBCommand alloc] initWithDisplayName:@"Reset branch" parameters:arguments repository:repository];
59+
[cmd invoke];
60+
}
61+
62+
- (IBAction)cancel:(id)sender {
63+
[NSApp endSheet:[self window]];
64+
[[self window] orderOut:self];
65+
}
66+
67+
@end

0 commit comments

Comments
 (0)