Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added git_repository_open_ext wrapper #593

Merged
merged 3 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ObjectiveGit/GTRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ extern NSString * const GTRepositoryCloneOptionsCloneLocal;
/// A NSURL pointing to a local file that contains PEM-encoded certificate chain.
extern NSString *const GTRepositoryCloneOptionsServerCertificateURL;

/// Repository extended open control flags for
/// +initWithURL:flags:ceilingDirs:error:.
///
/// See respository.h for documentation of each individual flag.
typedef NS_OPTIONS(NSInteger, GTRepositoryOpenFlags) {
GTRepositoryOpenNoSearch = GIT_REPOSITORY_OPEN_NO_SEARCH,
GTRepositoryOpenCrossFS = GIT_REPOSITORY_OPEN_CROSS_FS,
GTRepositoryOpenBare = GIT_REPOSITORY_OPEN_BARE,
};

/// Initialization flags associated with `GTRepositoryInitOptionsFlags` for
/// +initializeEmptyRepositoryAtFileURL:options:error:.
///
Expand Down Expand Up @@ -209,6 +219,17 @@ typedef NS_ENUM(NSInteger, GTRepositoryStateType) {
/// Returns the initialized repository, or nil if an error occurred.
- (nullable instancetype)initWithURL:(NSURL *)localFileURL error:(NSError **)error;

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

- (instancetype)init NS_UNAVAILABLE;

/// Initializes the receiver to wrap the given repository object. Designated initializer.
Expand Down
29 changes: 29 additions & 0 deletions ObjectiveGit/GTRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,35 @@ - (instancetype)initWithURL:(NSURL *)localFileURL error:(NSError **)error {
return [self initWithGitRepository:r];
}

- (instancetype)initWithURL:(NSURL *)localFileURL flags:(NSInteger)flags ceilingDirs:(NSArray<NSURL *> *)ceilingDirURLs error:(NSError **)error {
if (!localFileURL.isFileURL || localFileURL.path == nil) {
if (error != NULL) *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadUnsupportedSchemeError userInfo:@{ NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid file path URL to open.", @"") }];
return nil;
}

// Concatenate URL paths.
NSMutableString *ceilingDirsString;
if (ceilingDirURLs.count > 0) {
ceilingDirsString = [[NSMutableString alloc] init];
[ceilingDirURLs enumerateObjectsUsingBlock:^(NSURL * _Nonnull url, NSUInteger idx, BOOL * _Nonnull stop) {
if (idx < ceilingDirURLs.count - 1) {
[ceilingDirsString appendString:[NSString stringWithFormat:@"%@%c", url.path, GIT_PATH_LIST_SEPARATOR]];
} else {
[ceilingDirsString appendString:url.path];
}
}];
}

git_repository *r;
int gitError = git_repository_open_ext(&r, localFileURL.path.fileSystemRepresentation, (unsigned int)flags, ceilingDirsString.fileSystemRepresentation);
if (gitError < GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to open repository at URL %@.", localFileURL];
return nil;
}

return [self initWithGitRepository:r];
}


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

Expand Down