Skip to content

Commit 9575a7e

Browse files
committed
Add stash apply & pop support
1 parent d7eb46c commit 9575a7e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

ObjectiveGit/GTRepository+Stashing.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ typedef NS_OPTIONS(NSInteger, GTRepositoryStashFlag) {
1919
GTRepositoryStashFlagIncludeIgnored = GIT_STASH_INCLUDE_IGNORED
2020
};
2121

22+
/// Flags for -applyStashAtIndex:flags:error: and
23+
/// -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,
28+
};
29+
2230
NS_ASSUME_NONNULL_BEGIN
2331

2432
@interface GTRepository (Stashing)
@@ -41,6 +49,24 @@ NS_ASSUME_NONNULL_BEGIN
4149
/// will cause enumeration to stop after the block returns. Must not be nil.
4250
- (void)enumerateStashesUsingBlock:(void (^)(NSUInteger index, NSString * __nullable message, GTOID * __nullable oid, BOOL *stop))block;
4351

52+
/// Apply stashed changes.
53+
///
54+
/// index - The index of the stash to apply. 0 is the latest one.
55+
/// flags - The flags to use when applying the stash.
56+
/// error - If not NULL, set to any error that occurred.
57+
///
58+
/// Returns YES if the requested stash was successfully applied, NO otherwise.
59+
- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error;
60+
61+
/// Pop stashed changes.
62+
///
63+
/// index - The index of the stash to apply. 0 is the most recent stash.
64+
/// flags - The flags to use when applying the stash.
65+
/// error - If not NULL, set to any error that occurred.
66+
///
67+
/// Returns YES if the requested stash was successfully applied, NO otherwise.
68+
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error;
69+
4470
/// Drop a stash from the repository's list of stashes.
4571
///
4672
/// index - The index of the stash to drop, where 0 is the most recent stash.

ObjectiveGit/GTRepository+Stashing.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ - (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);
55+
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];
57+
return NO;
58+
}
59+
return YES;
60+
}
61+
62+
- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error {
63+
int gitError = git_stash_pop(self.git_repository, index, flags);
64+
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];
66+
return NO;
67+
}
68+
return YES;
69+
}
70+
5371
- (BOOL)dropStashAtIndex:(NSUInteger)index error:(NSError **)error {
5472
int gitError = git_stash_drop(self.git_repository, index);
5573
if (gitError != GIT_OK) {

0 commit comments

Comments
 (0)