Skip to content

Commit

Permalink
Add stash apply & pop support
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennou committed Aug 9, 2015
1 parent d7eb46c commit 9575a7e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ObjectiveGit/GTRepository+Stashing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions ObjectiveGit/GTRepository+Stashing.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9575a7e

Please sign in to comment.