-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathGTCheckoutOptions.m
More file actions
100 lines (79 loc) · 3.85 KB
/
GTCheckoutOptions.m
File metadata and controls
100 lines (79 loc) · 3.85 KB
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
//
// 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 "NSArray+StringArray.h"
#import "git2.h"
// The type of block set in progressBlock for progress reporting
typedef void (^GTCheckoutProgressBlock)(NSString *path, NSUInteger completedSteps, NSUInteger totalSteps);
// The type of block set in notifyBlock for notification reporting
typedef int (^GTCheckoutNotifyBlock)(GTCheckoutNotifyFlags why, NSString *path, GTDiffFile * _Nullable baseline, GTDiffFile * _Nullable target, GTDiffFile * _Nullable workdir);
@interface GTCheckoutOptions () {
git_checkout_options _git_checkoutOptions;
}
@end
@implementation GTCheckoutOptions
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy notifyFlags:(GTCheckoutNotifyFlags)notifyFlags progressBlock:(GTCheckoutProgressBlock _Nullable)progressBlock notifyBlock:( GTCheckoutNotifyBlock _Nullable)notifyBlock {
GTCheckoutOptions *options = [self checkoutOptionsWithStrategy:strategy];
options.notifyFlags = notifyFlags;
options.notifyBlock = notifyBlock;
options.progressBlock = progressBlock;
return options;
}
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy progressBlock:(GTCheckoutProgressBlock)progressBlock {
NSParameterAssert(progressBlock != nil);
GTCheckoutOptions *options = [self checkoutOptionsWithStrategy:strategy];
options.progressBlock = progressBlock;
return options;
}
+ (instancetype)checkoutOptionsWithStrategy:(GTCheckoutStrategyType)strategy notifyFlags:(GTCheckoutNotifyFlags)notifyFlags notifyBlock:(GTCheckoutNotifyBlock)notifyBlock {
NSParameterAssert(notifyBlock != nil);
return [self checkoutOptionsWithStrategy:strategy notifyFlags:notifyFlags progressBlock:nil notifyBlock:notifyBlock];
}
+ (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 ? @(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 = (unsigned int)self.strategy;
if (self.progressBlock != nil) {
_git_checkoutOptions.progress_cb = GTCheckoutProgressCallback;
_git_checkoutOptions.progress_payload = (__bridge void *)self.progressBlock;
}
if (self.notifyBlock != nil) {
_git_checkoutOptions.notify_cb = GTCheckoutNotifyCallback;
_git_checkoutOptions.notify_flags = (unsigned int)self.notifyFlags;
_git_checkoutOptions.notify_payload = (__bridge void *)self.notifyBlock;
}
_git_checkoutOptions.paths = self.pathSpecs.git_strarray;
return &_git_checkoutOptions;
}
@end