-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathGTSubmodule.m
204 lines (153 loc) · 5.42 KB
/
GTSubmodule.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// GTSubmodule.m
// ObjectiveGitFramework
//
// Created by Justin Spahr-Summers on 2013-05-29.
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
//
#import "GTSubmodule.h"
#import "GTOID.h"
#import "GTRepository.h"
#import "NSError+Git.h"
#import "git2/errors.h"
@interface GTSubmodule ()
@property (nonatomic, assign, readonly) git_submodule *git_submodule;
@end
@implementation GTSubmodule
#pragma mark Properties
- (GTSubmoduleIgnoreRule)ignoreRule {
return (GTSubmoduleIgnoreRule)git_submodule_ignore(self.git_submodule);
}
- (GTSubmodule *)submoduleByUpdatingIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error {
int result = git_submodule_set_ignore(self.parentRepository.git_repository, git_submodule_name(self.git_submodule), (git_submodule_ignore_t)ignoreRule);
if (result != GIT_OK) {
if (error != NULL) {
*error = [NSError git_errorFor:result description:@"Couldn't set submodule ignore rule."];
}
return nil;
}
return [self.parentRepository submoduleWithName:self.name error:error];
}
- (GTOID *)indexOID {
const git_oid *oid = git_submodule_index_id(self.git_submodule);
if (oid == NULL) return nil;
return [[GTOID alloc] initWithGitOid:oid];
}
- (GTOID *)HEADOID {
const git_oid *oid = git_submodule_head_id(self.git_submodule);
if (oid == NULL) return nil;
return [[GTOID alloc] initWithGitOid:oid];
}
- (GTOID *)workingDirectoryOID {
const git_oid *oid = git_submodule_wd_id(self.git_submodule);
if (oid == NULL) return nil;
return [[GTOID alloc] initWithGitOid:oid];
}
- (NSString *)name {
const char *cName = git_submodule_name(self.git_submodule);
NSAssert(cName != NULL, @"Unexpected nil submodule name");
NSString *name = @(cName);
NSAssert(name, @"name is nil");
return name;
}
- (NSString *)path {
const char *cPath = git_submodule_path(self.git_submodule);
NSAssert(cPath != NULL, @"Unexpected nil submodule path");
NSString *path = @(cPath);
NSAssert(path, @"message is nil");
return path;
}
- (NSString *)URLString {
const char *cURL = git_submodule_url(self.git_submodule);
NSAssert(cURL != NULL, @"Unexpected nil submodule URL");
NSString *URL = @(cURL);
NSAssert(URL, @"URL is nil");
return URL;
}
#pragma mark Lifecycle
- (void)dealloc {
if (_git_submodule != NULL) {
git_submodule_free(_git_submodule);
}
}
- (instancetype)init {
NSAssert(NO, @"Call to an unavailable initializer.");
return nil;
}
- (instancetype)initWithGitSubmodule:(git_submodule *)submodule parentRepository:(GTRepository *)repository {
NSParameterAssert(submodule != NULL);
NSParameterAssert(repository != nil);
self = [super init];
if (self == nil) return nil;
_parentRepository = repository;
_git_submodule = submodule;
return self;
}
#pragma mark Inspection
- (GTSubmoduleStatus)statusWithIgnoreRule:(GTSubmoduleIgnoreRule)ignoreRule error:(NSError **)error {
unsigned status;
int gitError = git_submodule_status(&status, self.parentRepository.git_repository, git_submodule_name(self.git_submodule), (git_submodule_ignore_t)ignoreRule);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to get submodule %@ status.", self.name];
return GTSubmoduleStatusUnknown;
}
return status;
}
- (GTSubmoduleStatus)status:(NSError **)error {
return [self statusWithIgnoreRule:self.ignoreRule error:error];
}
#pragma mark Manipulation
- (BOOL)reload:(NSError **)error {
int gitError = git_submodule_reload(self.git_submodule, 0);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to reload submodule %@.", self.name];
return NO;
}
return YES;
}
- (BOOL)sync:(NSError **)error {
int gitError = git_submodule_sync(self.git_submodule);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to synchronize submodule %@.", self.name];
return NO;
}
return YES;
}
- (GTRepository *)submoduleRepository:(NSError **)error {
git_repository *repo;
int gitError = git_submodule_open(&repo, self.git_submodule);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to open submodule %@ repository.", self.name];
return nil;
}
return [[GTRepository alloc] initWithGitRepository:repo];
}
- (BOOL)writeToParentConfigurationDestructively:(BOOL)overwrite error:(NSError **)error {
int gitError = git_submodule_init(self.git_submodule, (overwrite ? 1 : 0));
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to initialize submodule %@.", self.name];
return NO;
}
return YES;
}
- (BOOL)addToIndex:(NSError **)error {
int gitError = git_submodule_add_to_index(self.git_submodule, 0);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to add submodule %@ to its parent's index.", self.name];
return NO;
}
return YES;
}
#pragma mark NSObject
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p>{ name: %@, indexOID: %@, HEADOID: %@, workingDirectoryOID: %@ }", self.class, self, self.name, self.indexOID, self.HEADOID, self.workingDirectoryOID];
}
- (NSUInteger)hash {
return self.name.hash;
}
- (BOOL)isEqual:(GTSubmodule *)submodule {
if (self == submodule) return YES;
if (![submodule isKindOfClass:GTSubmodule.class]) return NO;
return [self.parentRepository isEqual:submodule.parentRepository] && [self.name isEqual:submodule.name];
}
@end