forked from laullon/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBGitSubmodule.m
116 lines (96 loc) · 3.19 KB
/
PBGitSubmodule.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// PBGitSubmodule.m
// GitX
//
// Created by Tomasz Krasnyk on 10-11-07.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "PBGitSubmodule.h"
@implementation PBGitSubmodule
@synthesize name;
@synthesize path;
@synthesize checkedOutCommit;
@synthesize submoduleState;
@synthesize submodules;
- (NSMutableArray *) submodules {
if (!submodules) {
submodules = [[NSMutableArray alloc] init];
}
return submodules;
}
- (id) initWithRawSubmoduleStatusString:(NSString *) submoduleStatusString {
NSParameterAssert([submoduleStatusString length] > 0);
if ((self = [super init])) {
unichar status = [submoduleStatusString characterAtIndex:0];
submoduleState = [PBGitSubmodule submoduleStateFromCharacter:status];
if (submoduleState == PBGitSubmoduleStateFailed) {
DLog(@"Submodule status failed:\n %@", submoduleStatusString);
return nil;
}
NSScanner *scanner = [NSScanner scannerWithString:[submoduleStatusString substringFromIndex:1]];
NSString *sha1 = nil;
NSString *fullPath = nil;
NSString *coName = nil;
BOOL shouldContinue = [scanner scanUpToString:@" " intoString:&sha1];
if (shouldContinue) {
shouldContinue = [scanner scanUpToString:@"(" intoString:&fullPath];
}
if (shouldContinue) {
shouldContinue = [scanner scanString:@"(" intoString:NULL];
}
if (shouldContinue) {
[scanner scanUpToString:@")" intoString:&coName];
}
path = [fullPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
coName = [coName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
checkedOutCommit = [coName length] > 0 ? coName : nil;
name = [self.path lastPathComponent];
}
return self;
}
- (void) addSubmodule:(PBGitSubmodule *) submodule {
[self.submodules addObject:submodule];
}
#pragma mark -
#pragma mark Presentable
- (NSImage *) icon {
return [PBGitSubmodule imageForSubmoduleState:self.submoduleState];
}
- (NSString *) displayDescription {
NSMutableString *result = [[NSMutableString alloc] initWithString:self.name];
if (self.checkedOutCommit) {
[result appendFormat:@" (%@)", self.checkedOutCommit];
}
return result;
}
- (NSString *) popupDescription {
return [self description];
}
#pragma mark -
#pragma mark Private
+ (NSImage *) imageForSubmoduleState:(PBGitSubmoduleState) state {
NSString *imageName = nil;
if (state == PBGitSubmoduleStateMatchingIndex) {
imageName = @"submodule-matching-index.png";
} else if (state == PBGitSubmoduleStateNotInitialized) {
imageName = @"submodule-empty.png";
} else if (state == PBGitSubmoduleStateDoesNotMatchIndex) {
imageName = @"submodule-notmatching-index.png";
}
return [NSImage imageNamed:imageName];
}
+ (PBGitSubmoduleState) submoduleStateFromCharacter:(unichar) character {
PBGitSubmoduleState state = PBGitSubmoduleStateMatchingIndex;
if (character == '-') {
state = PBGitSubmoduleStateNotInitialized;
} else if (character == '+') {
state = PBGitSubmoduleStateDoesNotMatchIndex;
} else if (character != ' ') {
return PBGitSubmoduleStateFailed;
}
return state;
}
- (NSString *) description {
return [NSString stringWithFormat:@"[SUBMODULE] %@(%@) %@", self.name, self.path, self.checkedOutCommit];
}
@end