Skip to content

Commit edf3eff

Browse files
committed
Added git_repository_open_ext wrapper
1 parent caffad9 commit edf3eff

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

ObjectiveGit/GTRepository.h

Lines changed: 34 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(UInt32, 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
///
@@ -201,6 +211,18 @@ typedef NS_ENUM(NSInteger, GTRepositoryStateType) {
201211
/// Returns the initialized repository, or nil if an error occurred.
202212
+ (nullable instancetype)repositoryWithURL:(NSURL *)localFileURL error:(NSError **)error;
203213

214+
/// Convenience class initializer to find and open a repository with extended controls.
215+
///
216+
/// localFileURL - The file URL for the new repository. Cannot be nil.
217+
/// flags - A combination of the `GTRepositoryOpenFlags` flags.
218+
/// ceilingDirs - A GIT_PATH_LIST_SEPARATOR delimited list of path prefixes at
219+
/// which the search for a containing repository should terminate.
220+
/// Can be NULL.
221+
/// error - The error if one occurs.
222+
///
223+
/// Returns the initialized repository, or nil if an error occurred.
224+
+ (nullable instancetype)initWithURL:(NSURL *)localFileURL flags:(UInt32)flags ceilingDirs:(nullable const char *)ceilingDirs error:(NSError **)error;
225+
204226
/// Convenience initializer which uses the default options.
205227
///
206228
/// localFileURL - The file URL for the new repository. Cannot be nil.
@@ -209,6 +231,18 @@ typedef NS_ENUM(NSInteger, GTRepositoryStateType) {
209231
/// Returns the initialized repository, or nil if an error occurred.
210232
- (nullable instancetype)initWithURL:(NSURL *)localFileURL error:(NSError **)error;
211233

234+
/// Convenience initializer to find and open a repository with extended controls.
235+
///
236+
/// localFileURL - The file URL for the new repository. Cannot be nil.
237+
/// flags - A combination of the `GTRepositoryOpenFlags` flags.
238+
/// ceilingDirs - A GIT_PATH_LIST_SEPARATOR delimited list of path prefixes at
239+
/// which the search for a containing repository should terminate.
240+
/// Can be NULL.
241+
/// error - The error if one occurs.
242+
///
243+
/// Returns the initialized repository, or nil if an error occurred.
244+
- (nullable instancetype)initWithURL:(NSURL *)localFileURL flags:(UInt32)flags ceilingDirs:(nullable const char *)ceilingDirs error:(NSError **)error;
245+
212246
- (instancetype)init NS_UNAVAILABLE;
213247

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

ObjectiveGit/GTRepository.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ + (instancetype)repositoryWithURL:(NSURL *)localFileURL error:(NSError **)error
145145
return [[self alloc] initWithURL:localFileURL error:error];
146146
}
147147

148+
+ (instancetype)initWithURL:(NSURL *)localFileURL flags:(UInt32)flags ceilingDirs:(const char *)ceilingDirs error:(NSError **)error {
149+
return [[self alloc] initWithURL:localFileURL flags:flags ceilingDirs:ceilingDirs error:error];
150+
}
151+
148152
- (instancetype)init {
149153
NSAssert(NO, @"Call to an unavailable initializer.");
150154
return nil;
@@ -177,6 +181,22 @@ - (instancetype)initWithURL:(NSURL *)localFileURL error:(NSError **)error {
177181
return [self initWithGitRepository:r];
178182
}
179183

184+
- (instancetype)initWithURL:(NSURL *)localFileURL flags:(UInt32)flags ceilingDirs:(const char *)ceilingDirs error:(NSError **)error {
185+
if (!localFileURL.isFileURL || localFileURL.path == nil) {
186+
if (error != NULL) *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileWriteUnsupportedSchemeError userInfo:@{ NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid file path URL to initialize repository.", @"") }];
187+
return nil;
188+
}
189+
190+
git_repository *r;
191+
int gitError = git_repository_open_ext(&r, localFileURL.path.fileSystemRepresentation, flags, ceilingDirs);
192+
if (gitError < GIT_OK) {
193+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to open repository at URL %@.", localFileURL];
194+
return nil;
195+
}
196+
197+
return [self initWithGitRepository:r];
198+
}
199+
180200

181201
typedef void(^GTTransferProgressBlock)(const git_transfer_progress *progress, BOOL *stop);
182202

0 commit comments

Comments
 (0)