forked from dgrijalva/gitx
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathPBStashController.m
101 lines (74 loc) · 2.8 KB
/
PBStashController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// PBStashController.m
// GitX
//
// Created by Tomasz Krasnyk on 10-11-27.
// Copyright (c) 2010 __MyCompanyName__. All rights reserved.
//
#import "PBStashController.h"
#import "PBGitRepository.h"
#import "PBCommand.h"
#import "PBCommandWithParameter.h"
static NSString * const kCommandName = @"stash";
@interface PBStashController()
@property (nonatomic, retain) NSArray *stashes;
@end
@implementation PBStashController
@synthesize stashes;
- (id) initWithRepository:(PBGitRepository *) repo {
if ((self = [super init])){
repository = repo;
}
return self;
}
- (void) reload {
NSArray *arguments = [NSArray arrayWithObjects:kCommandName, @"list", nil];
NSString *output = [repository outputInWorkdirForArguments:arguments];
NSArray *lines = [output componentsSeparatedByString:@"\n"];
NSMutableArray *loadedStashes = [[NSMutableArray alloc] initWithCapacity:[lines count]];
for (NSString *stashLine in lines) {
if ([stashLine length] == 0)
continue;
PBGitStash *stash = [[PBGitStash alloc] initWithRawStashLine:stashLine];
if (stash != nil)
[loadedStashes addObject:stash];
}
self.stashes = loadedStashes;
}
#pragma mark Actions
- (void) stashLocalChanges {
NSArray *args = [NSArray arrayWithObject:kCommandName];
PBCommand *command = [[PBCommand alloc] initWithDisplayName:@"Stash local changes..." parameters:args repository:repository];
command.commandTitle = command.displayName;
command.commandDescription = @"Stashing local changes";
PBCommandWithParameter *cmd = [[PBCommandWithParameter alloc] initWithCommand:command parameterName:@"save" parameterDisplayName:@"Stash message (optional)"];
[cmd invoke];
}
- (void) clearAllStashes {
PBCommand *command = [[PBCommand alloc] initWithDisplayName:@"Clear stashes" parameters:[NSArray arrayWithObjects:kCommandName, @"clear", nil] repository:repository];
command.commandTitle = command.displayName;
command.commandDescription = @"Clearing stashes";
[command invoke];
}
#pragma mark Menu
- (NSArray *) menu {
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMenuItem *stashChanges = [[NSMenuItem alloc] initWithTitle:@"Stash local changes..." action:@selector(stashLocalChanges) keyEquivalent:@""];
[stashChanges setTarget:self];
NSMenuItem *clearStashes = [[NSMenuItem alloc] initWithTitle:@"Clear stashes" action:@selector(clearAllStashes) keyEquivalent:@""];
[clearStashes setTarget:self];
[array addObject:stashChanges];
[array addObject:clearStashes];
return array;
}
- (BOOL) validateMenuItem:(NSMenuItem *) item {
SEL action = [item action];
BOOL shouldBeEnabled = YES;
if (action == @selector(stashLocalChanges)) {
//TODO: check if we have unstaged changes
} else if (action == @selector(clearAllStashes)) {
shouldBeEnabled = [self.stashes count] > 0;
}
return shouldBeEnabled;
}
@end