diff --git a/ObjectiveGit/GTRepository+Stashing.h b/ObjectiveGit/GTRepository+Stashing.h index 32d04f8d5..709fffe08 100644 --- a/ObjectiveGit/GTRepository+Stashing.h +++ b/ObjectiveGit/GTRepository+Stashing.h @@ -19,6 +19,14 @@ typedef NS_OPTIONS(NSInteger, GTRepositoryStashFlag) { GTRepositoryStashFlagIncludeIgnored = GIT_STASH_INCLUDE_IGNORED }; +/// Flags for -applyStashAtIndex:flags:error: and +/// -popStashAtIndex:flags:error. +/// Those can be ORed together. See git_apply_flags for additional information. +typedef NS_OPTIONS(NSInteger, GTRepositoryApplyFlag) { + GTRepositoryApplyFlagDefault = GIT_APPLY_DEFAULT, + GTRepositoryApplyFlagReinstateIndex = GIT_APPLY_REINSTATE_INDEX, +}; + NS_ASSUME_NONNULL_BEGIN @interface GTRepository (Stashing) @@ -41,6 +49,24 @@ NS_ASSUME_NONNULL_BEGIN /// will cause enumeration to stop after the block returns. Must not be nil. - (void)enumerateStashesUsingBlock:(void (^)(NSUInteger index, NSString * __nullable message, GTOID * __nullable oid, BOOL *stop))block; +/// Apply stashed changes. +/// +/// index - The index of the stash to apply. 0 is the latest one. +/// flags - The flags to use when applying the stash. +/// error - If not NULL, set to any error that occurred. +/// +/// Returns YES if the requested stash was successfully applied, NO otherwise. +- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error; + +/// Pop stashed changes. +/// +/// index - The index of the stash to apply. 0 is the most recent stash. +/// flags - The flags to use when applying the stash. +/// error - If not NULL, set to any error that occurred. +/// +/// Returns YES if the requested stash was successfully applied, NO otherwise. +- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error; + /// Drop a stash from the repository's list of stashes. /// /// index - The index of the stash to drop, where 0 is the most recent stash. diff --git a/ObjectiveGit/GTRepository+Stashing.m b/ObjectiveGit/GTRepository+Stashing.m index 1db98875a..86eb61735 100644 --- a/ObjectiveGit/GTRepository+Stashing.m +++ b/ObjectiveGit/GTRepository+Stashing.m @@ -50,6 +50,24 @@ - (void)enumerateStashesUsingBlock:(GTRepositoryStashEnumerationBlock)block { git_stash_foreach(self.git_repository, &stashEnumerationCallback, (__bridge void *)block); } +- (BOOL)applyStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error { + int gitError = git_stash_apply(self.git_repository, index, flags); + if (gitError != GIT_OK) { + if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to apply stash" failureReason:@"The stash at index %ld couldn't be applied.", index]; + return NO; + } + return YES; +} + +- (BOOL)popStashAtIndex:(NSUInteger)index flags:(GTRepositoryApplyFlag)flags error:(NSError **)error { + int gitError = git_stash_pop(self.git_repository, index, flags); + if (gitError != GIT_OK) { + if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to pop stash" failureReason:@"The stash at index %ld couldn't be applied.", index]; + return NO; + } + return YES; +} + - (BOOL)dropStashAtIndex:(NSUInteger)index error:(NSError **)error { int gitError = git_stash_drop(self.git_repository, index); if (gitError != GIT_OK) {