Skip to content

Commit fcbcf23

Browse files
committed
Make the apply/pop machinery common
1 parent b4d5816 commit fcbcf23

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

ObjectiveGit/GTRepository+Stashing.m

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int stashApplyProgressCallback(git_stash_apply_progress_t progress, void
5959
return (stop ? GIT_EUSER : 0);
6060
}
6161

62-
- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags error:(NSError **)error progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock {
62+
- (BOOL)applyStashAtIndex:(NSUInteger)index popStash:(BOOL)pop flags:(GTRepositoryStashApplyFlag)flags error:(NSError **)error progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress, BOOL * __nonnull))progressBlock {
6363
git_stash_apply_options stash_options = GIT_STASH_APPLY_OPTIONS_INIT;
6464

6565
stash_options.flags = (git_stash_apply_flags)flags;
@@ -68,29 +68,28 @@ - (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)fl
6868
stash_options.progress_payload = (__bridge void *)progressBlock;
6969
}
7070

71-
int gitError = git_stash_apply(self.git_repository, index, &stash_options);
72-
if (gitError != GIT_OK) {
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];
74-
return NO;
71+
if (pop == NO) {
72+
int gitError = git_stash_apply(self.git_repository, index, &stash_options);
73+
if (gitError != GIT_OK) {
74+
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];
75+
return NO;
76+
}
77+
} else {
78+
int gitError = git_stash_pop(self.git_repository, index, &stash_options);
79+
if (gitError != GIT_OK) {
80+
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];
81+
return NO;
82+
}
7583
}
7684
return YES;
7785
}
7886

79-
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags error:(NSError **)error progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock {
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+
- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags error:(NSError **)error progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock {
88+
return [self applyStashAtIndex:index popStash:NO flags:flags error:error progressBlock:progressBlock];
89+
}
8790

88-
int gitError = git_stash_pop(self.git_repository, index, &stash_options);
89-
if (gitError != GIT_OK) {
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];
91-
return NO;
92-
}
93-
return YES;
91+
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryStashApplyFlag)flags error:(NSError **)error progressBlock:(nullable void (^)(GTRepositoryStashApplyProgress progress, BOOL *stop))progressBlock {
92+
return [self applyStashAtIndex:index popStash:YES flags:flags error:error progressBlock:progressBlock];
9493
}
9594

9695
- (BOOL)dropStashAtIndex:(NSUInteger)index error:(NSError **)error {

0 commit comments

Comments
 (0)