-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap all the various way of doing checkouts in GTCheckoutOptions
Fixes #457
- Loading branch information
Showing
9 changed files
with
199 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// GTCheckoutOptions.h | ||
// ObjectiveGitFramework | ||
// | ||
// Created by Etienne on 10/04/2015. | ||
// Copyright (c) 2015 GitHub, Inc. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "GTRepository.h" | ||
|
||
/// Checkout strategies used by the various -checkout... methods | ||
/// See git_checkout_strategy_t | ||
typedef NS_OPTIONS(NSInteger, GTCheckoutStrategyType) { | ||
GTCheckoutStrategyNone = GIT_CHECKOUT_NONE, | ||
GTCheckoutStrategySafe = GIT_CHECKOUT_SAFE, | ||
GTCheckoutStrategyForce = GIT_CHECKOUT_FORCE, | ||
GTCheckoutStrategyAllowConflicts = GIT_CHECKOUT_ALLOW_CONFLICTS, | ||
GTCheckoutStrategyRemoveUntracked = GIT_CHECKOUT_REMOVE_UNTRACKED, | ||
GTCheckoutStrategyRemoveIgnored = GIT_CHECKOUT_REMOVE_IGNORED, | ||
GTCheckoutStrategyUpdateOnly = GIT_CHECKOUT_UPDATE_ONLY, | ||
GTCheckoutStrategyDontUpdateIndex = GIT_CHECKOUT_DONT_UPDATE_INDEX, | ||
GTCheckoutStrategyNoRefresh = GIT_CHECKOUT_NO_REFRESH, | ||
GTCheckoutStrategyDisablePathspecMatch = GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH, | ||
GTCheckoutStrategySkipLockedDirectories = GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES, | ||
}; | ||
|
||
/// Checkout notification flags used by the various -checkout... methods | ||
/// See git_checkout_notify_t | ||
typedef NS_OPTIONS(NSInteger, GTCheckoutNotifyFlags) { | ||
GTCheckoutNotifyNone = GIT_CHECKOUT_NOTIFY_NONE, | ||
GTCheckoutNotifyConflict = GIT_CHECKOUT_NOTIFY_CONFLICT, | ||
GTCheckoutNotifyDirty = GIT_CHECKOUT_NOTIFY_DIRTY, | ||
GTCheckoutNotifyUpdated = GIT_CHECKOUT_NOTIFY_UPDATED, | ||
GTCheckoutNotifyUntracked = GIT_CHECKOUT_NOTIFY_UNTRACKED, | ||
GTCheckoutNotifyIgnored = GIT_CHECKOUT_NOTIFY_IGNORED, | ||
|
||
GTCheckoutNotifyAll = GIT_CHECKOUT_NOTIFY_ALL, | ||
}; | ||
|
||
@interface GTCheckoutOptions : NSObject | ||
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy notifyFlags:(GTCheckoutNotifyFlags)notifyFlags progressBlock:(void (^)(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps))progressBlock notifyBlock:(int (^)(GTCheckoutNotifyFlags why, NSString *path, GTDiffFile *baseline, GTDiffFile *target, GTDiffFile *workdir))notifyBlock; | ||
|
||
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy progressBlock:(void (^)(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps))progressBlock; | ||
|
||
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy; | ||
|
||
- (git_checkout_options *)git_checkoutOptions NS_RETURNS_INNER_POINTER; | ||
|
||
@property (assign) GTCheckoutStrategyType strategy; | ||
@property (copy) void (^progressBlock)(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps); | ||
|
||
@property (assign) GTCheckoutNotifyFlags notifyFlags; | ||
@property (copy) int (^notifyBlock)(GTCheckoutNotifyFlags why, NSString *path, GTDiffFile *baseline, GTDiffFile *target, GTDiffFile *workdir); | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// GTCheckoutOptions.m | ||
// ObjectiveGitFramework | ||
// | ||
// Created by Etienne on 10/04/2015. | ||
// Copyright (c) 2015 GitHub, Inc. All rights reserved. | ||
// | ||
|
||
#import "GTCheckoutOptions.h" | ||
#import "GTDiffFile.h" | ||
#import "NSError+Git.h" | ||
#import "git2.h" | ||
|
||
// The type of block passed to -checkout:strategy:progressBlock:notifyBlock:notifyFlags:error: for progress reporting | ||
typedef void (^GTCheckoutProgressBlock)(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps); | ||
|
||
// The type of block passed to -checkout:strategy:progressBlock:notifyBlock:notifyFlags:error: for notification reporting | ||
typedef int (^GTCheckoutNotifyBlock)(GTCheckoutNotifyFlags why, NSString *path, GTDiffFile *baseline, GTDiffFile *target, GTDiffFile *workdir); | ||
|
||
|
||
@interface GTCheckoutOptions () { | ||
git_checkout_options _git_checkoutOptions; | ||
} | ||
@end | ||
|
||
@implementation GTCheckoutOptions | ||
|
||
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy notifyFlags:(GTCheckoutNotifyFlags)notifyFlags progressBlock:(GTCheckoutProgressBlock)progressBlock notifyBlock:(GTCheckoutNotifyBlock)notifyBlock { | ||
GTCheckoutOptions *options = [self checkoutOptionsWithStrategy:strategy progressBlock:progressBlock]; | ||
options.notifyFlags = notifyFlags; | ||
options.notifyBlock = notifyBlock; | ||
return options; | ||
} | ||
|
||
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy progressBlock:(GTCheckoutProgressBlock)progressBlock { | ||
GTCheckoutOptions *options = [self checkoutOptionsWithStrategy:strategy]; | ||
options.progressBlock = progressBlock; | ||
return options; | ||
} | ||
|
||
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy { | ||
GTCheckoutOptions *options = [[self alloc] init]; | ||
options.strategy = strategy; | ||
return options; | ||
} | ||
|
||
- (instancetype)init { | ||
self = [super init]; | ||
if (self == nil) return nil; | ||
|
||
_git_checkoutOptions.version = GIT_CHECKOUT_OPTIONS_VERSION; | ||
|
||
return self; | ||
} | ||
|
||
static void GTCheckoutProgressCallback(const char *path, size_t completedSteps, size_t totalSteps, void *payload) { | ||
if (payload == NULL) return; | ||
void (^block)(NSString *, NSUInteger, NSUInteger) = (__bridge id)payload; | ||
NSString *nsPath = (path != NULL ? [NSString stringWithUTF8String:path] : nil); | ||
block(nsPath, completedSteps, totalSteps); | ||
} | ||
|
||
static int GTCheckoutNotifyCallback(git_checkout_notify_t why, const char *path, const git_diff_file *baseline, const git_diff_file *target, const git_diff_file *workdir, void *payload) { | ||
if (payload == NULL) return 0; | ||
GTCheckoutNotifyBlock block = (__bridge id)payload; | ||
NSString *nsPath = (path != NULL ? @(path) : nil); | ||
GTDiffFile *gtBaseline = (baseline != NULL ? [[GTDiffFile alloc] initWithGitDiffFile:*baseline] : nil); | ||
GTDiffFile *gtTarget = (target != NULL ? [[GTDiffFile alloc] initWithGitDiffFile:*target] : nil); | ||
GTDiffFile *gtWorkdir = (workdir != NULL ? [[GTDiffFile alloc] initWithGitDiffFile:*workdir] : nil); | ||
return block((GTCheckoutNotifyFlags)why, nsPath, gtBaseline, gtTarget, gtWorkdir); | ||
} | ||
|
||
- (git_checkout_options *)git_checkoutOptions { | ||
_git_checkoutOptions.checkout_strategy = self.strategy; | ||
|
||
if (self.progressBlock) { | ||
_git_checkoutOptions.progress_cb = GTCheckoutProgressCallback; | ||
_git_checkoutOptions.progress_payload = (__bridge void *)self.progressBlock; | ||
} | ||
|
||
if (self.notifyBlock) { | ||
_git_checkoutOptions.notify_cb = GTCheckoutNotifyCallback; | ||
_git_checkoutOptions.notify_flags = self.notifyFlags; | ||
_git_checkoutOptions.notify_payload = (__bridge void *)self.notifyBlock; | ||
} | ||
|
||
return &_git_checkoutOptions; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.