Skip to content

Commit 67fc1e9

Browse files
committed
Bring API in line with current libgit2 version
1 parent bde2d8a commit 67fc1e9

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

ObjectiveGit/GTRepository+Stashing.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,23 @@ typedef NS_OPTIONS(NSInteger, GTRepositoryStashFlag) {
2121

2222
/// Flags for -applyStashAtIndex:flags:error: and
2323
/// -popStashAtIndex:flags:error.
24-
/// Those can be ORed together. See git_apply_flags for additional information.
25-
typedef NS_OPTIONS(NSInteger, GTRepositoryApplyFlag) {
26-
GTRepositoryApplyFlagDefault = GIT_APPLY_DEFAULT,
27-
GTRepositoryApplyFlagReinstateIndex = GIT_APPLY_REINSTATE_INDEX,
24+
/// Those can be ORed together. See git_stash_apply_flags for additional information.
25+
typedef NS_OPTIONS(NSInteger, GTRepositoryStashApplyFlag) {
26+
GTRepositoryStashApplyFlagDefault = GIT_STASH_APPLY_DEFAULT,
27+
GTRepositoryStashApplyFlagReinstateIndex = GIT_STASH_APPLY_REINSTATE_INDEX,
28+
};
29+
30+
/// Enum representing the current state of a stash apply/pop operation.
31+
/// See git_stash_apply_progress_t for additional information.
32+
typedef NS_ENUM(NSInteger, GTRepositoryStashApplyProgress) {
33+
GTRepositoryStashApplyProgressNone = GIT_STASH_APPLY_PROGRESS_NONE,
34+
GTRepositoryStashApplyProgressLoadingStash = GIT_STASH_APPLY_PROGRESS_LOADING_STASH,
35+
GTRepositoryStashApplyProgressAnalyzeIndex = GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX,
36+
GTRepositoryStashApplyProgressAnalyzeModified = GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED,
37+
GTRepositoryStashApplyProgressAnalyzeUntracked = GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED,
38+
GTRepositoryStashApplyProgressGheckoutUntracked = GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED,
39+
GTRepositoryStashApplyProgressCheckoutModified = GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED,
40+
GTRepositoryStashApplyProgressDone = GIT_STASH_APPLY_PROGRESS_DONE,
2841
};
2942

3043
NS_ASSUME_NONNULL_BEGIN
@@ -56,7 +69,7 @@ NS_ASSUME_NONNULL_BEGIN
5669
/// error - If not NULL, set to any error that occurred.
5770
///
5871
/// Returns YES if the requested stash was successfully applied, NO otherwise.
59-
- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error;
72+
- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock error:(NSError **)error;
6073

6174
/// Pop stashed changes.
6275
///
@@ -65,7 +78,7 @@ NS_ASSUME_NONNULL_BEGIN
6578
/// error - If not NULL, set to any error that occurred.
6679
///
6780
/// Returns YES if the requested stash was successfully applied, NO otherwise.
68-
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error;
81+
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock error:(NSError **)error;
6982

7083
/// Drop a stash from the repository's list of stashes.
7184
///

ObjectiveGit/GTRepository+Stashing.m

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,44 @@ - (void)enumerateStashesUsingBlock:(GTRepositoryStashEnumerationBlock)block {
5050
git_stash_foreach(self.git_repository, &stashEnumerationCallback, (__bridge void *)block);
5151
}
5252

53-
- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error {
54-
int gitError = git_stash_apply(self.git_repository, index, flags);
53+
static int stashApplyProgressCallback(git_stash_apply_progress_t progress, void *payload) {
54+
void (^block)(GTRepositoryStashApplyProgress, BOOL *) = (__bridge id)payload;
55+
56+
BOOL stop = NO;
57+
block((GTRepositoryStashApplyProgress)progress, &stop);
58+
59+
return (stop ? GIT_EUSER : 0);
60+
}
61+
62+
- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock error:(NSError **)error {
63+
git_stash_apply_options stash_options = GIT_STASH_APPLY_OPTIONS_INIT;
64+
65+
stash_options.flags = (git_stash_apply_flags)flags;
66+
if (progressBlock != nil) {
67+
stash_options.progress_cb = stashApplyProgressCallback;
68+
stash_options.progress_payload = (__bridge void *)progressBlock;
69+
}
70+
71+
int gitError = git_stash_apply(self.git_repository, index, &stash_options);
5572
if (gitError != GIT_OK) {
56-
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to apply stash" failureReason:@"The stash at index %ld couldn't be applied.", index];
73+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Stash apply failed" failureReason:@"The stash at index %ld couldn't be applied.", (unsigned long)index];
5774
return NO;
5875
}
5976
return YES;
6077
}
6178

62-
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error {
63-
int gitError = git_stash_pop(self.git_repository, index, flags);
79+
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock error:(NSError **)error {
80+
git_stash_apply_options stash_options = GIT_STASH_APPLY_OPTIONS_INIT;
81+
82+
stash_options.flags = (git_stash_apply_flags)flags;
83+
if (progressBlock != nil) {
84+
stash_options.progress_cb = stashApplyProgressCallback;
85+
stash_options.progress_payload = (__bridge void *)progressBlock;
86+
}
87+
88+
int gitError = git_stash_pop(self.git_repository, index, &stash_options);
6489
if (gitError != GIT_OK) {
65-
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to pop stash" failureReason:@"The stash at index %ld couldn't be applied.", index];
90+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Stash pop failed" failureReason:@"The stash at index %ld couldn't be applied.", (unsigned long)index];
6691
return NO;
6792
}
6893
return YES;
@@ -71,7 +96,7 @@ - (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags err
7196
- (BOOL)dropStashAtIndex:(NSUInteger)index error:(NSError **)error {
7297
int gitError = git_stash_drop(self.git_repository, index);
7398
if (gitError != GIT_OK) {
74-
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to drop stash."];
99+
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Stash drop failed" failureReason:@"The stash at index %ld couldn't be dropped", (unsigned long)index];
75100
return NO;
76101
}
77102

0 commit comments

Comments
 (0)