Skip to content

Commit 614bce9

Browse files
authored
Merge pull request #593 from glegrain/git_repository_open_ext
Added git_repository_open_ext wrapper
2 parents caffad9 + 367ad42 commit 614bce9

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

ObjectiveGit/GTRepository.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ extern NSString * const GTRepositoryCloneOptionsCloneLocal;
110110
/// A NSURL pointing to a local file that contains PEM-encoded certificate chain.
111111
extern NSString *const GTRepositoryCloneOptionsServerCertificateURL;
112112

113+
/// Repository extended open control flags for
114+
/// +initWithURL:flags:ceilingDirs:error:.
115+
///
116+
/// See respository.h for documentation of each individual flag.
117+
typedef NS_OPTIONS(NSInteger, GTRepositoryOpenFlags) {
118+
GTRepositoryOpenNoSearch = GIT_REPOSITORY_OPEN_NO_SEARCH,
119+
GTRepositoryOpenCrossFS = GIT_REPOSITORY_OPEN_CROSS_FS,
120+
GTRepositoryOpenBare = GIT_REPOSITORY_OPEN_BARE,
121+
};
122+
113123
/// Initialization flags associated with `GTRepositoryInitOptionsFlags` for
114124
/// +initializeEmptyRepositoryAtFileURL:options:error:.
115125
///
@@ -209,6 +219,17 @@ typedef NS_ENUM(NSInteger, GTRepositoryStateType) {
209219
/// Returns the initialized repository, or nil if an error occurred.
210220
- (nullable instancetype)initWithURL:(NSURL *)localFileURL error:(NSError **)error;
211221

222+
/// Convenience initializer to find and open a repository with extended controls.
223+
///
224+
/// localFileURL - The file URL for the new repository. Cannot be nil.
225+
/// flags - A combination of the `GTRepositoryOpenFlags` flags.
226+
/// ceilingDirURLs - An array of URLs at which the search for a containing
227+
/// repository should terminate. Can be NULL.
228+
/// error - The error if one occurs.
229+
///
230+
/// Returns the initialized repository, or nil if an error occurred.
231+
- (nullable instancetype)initWithURL:(NSURL *)localFileURL flags:(NSInteger)flags ceilingDirs:(nullable NSArray<NSURL *> *)ceilingDirURLs error:(NSError **)error;
232+
212233
- (instancetype)init NS_UNAVAILABLE;
213234

214235
/// Initializes the receiver to wrap the given repository object. Designated initializer.

ObjectiveGit/GTRepository.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ - (instancetype)initWithURL:(NSURL *)localFileURL error:(NSError **)error {
177177
return [self initWithGitRepository:r];
178178
}
179179

180+
- (instancetype)initWithURL:(NSURL *)localFileURL flags:(NSInteger)flags ceilingDirs:(NSArray<NSURL *> *)ceilingDirURLs error:(NSError **)error {
181+
if (!localFileURL.isFileURL || localFileURL.path == nil) {
182+
if (error != NULL) *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnsupportedSchemeError userInfo:@{ NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid file path URL to open.", @"") }];
183+
return nil;
184+
}
185+
186+
// Concatenate URL paths.
187+
NSMutableString *ceilingDirsString;
188+
if (ceilingDirURLs.count > 0) {
189+
ceilingDirsString = [[NSMutableString alloc] init];
190+
[ceilingDirURLs enumerateObjectsUsingBlock:^(NSURL * _Nonnull url, NSUInteger idx, BOOL * _Nonnull stop) {
191+
if (idx < ceilingDirURLs.count - 1) {
192+
[ceilingDirsString appendString:[NSString stringWithFormat:@"%@%c", url.path, GIT_PATH_LIST_SEPARATOR]];
193+
} else {
194+
[ceilingDirsString appendString:url.path];
195+
}
196+
}];
197+
}
198+
199+
git_repository *r;
200+
int gitError = git_repository_open_ext(&r, localFileURL.path.fileSystemRepresentation, (unsigned int)flags, ceilingDirsString.fileSystemRepresentation);
201+
if (gitError < GIT_OK) {
202+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to open repository at URL %@.", localFileURL];
203+
return nil;
204+
}
205+
206+
return [self initWithGitRepository:r];
207+
}
208+
180209

181210
typedef void(^GTTransferProgressBlock)(const git_transfer_progress *progress, BOOL *stop);
182211

0 commit comments

Comments
 (0)