Skip to content

Commit

Permalink
Merge pull request #549 from slavikus/master
Browse files Browse the repository at this point in the history
Moar wrapper functions
  • Loading branch information
pietbrauer committed Feb 17, 2016
2 parents 6a18a81 + 53e79e2 commit 8b46e45
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ObjectiveGit/GTCommit.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
@class GTSignature;
@class GTTree;
@class GTOID;
@class GTIndex;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -54,6 +55,17 @@ NS_ASSUME_NONNULL_BEGIN
/// The underlying `git_object` as a `git_commit` object.
- (git_commit *)git_commit __attribute__((objc_returns_inner_pointer));

/// Merges the given commit into the receiver in memory and produces the result as
/// an index.
///
/// otherCommit - The commit with which the receiver should be merged with. Cannot be
/// nil.
/// error - The error if one occurred.
///
/// Returns an index which represents the result of the merge, or nil if an error
/// occurred.
- (nullable GTIndex *)merge:(GTCommit *)otherCommit error:(NSError **)error;

@end

NS_ASSUME_NONNULL_END
18 changes: 18 additions & 0 deletions ObjectiveGit/GTCommit.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
#import "NSString+Git.h"
#import "NSDate+GTTimeAdditions.h"
#import "GTOID.h"
#import "GTIndex.h"

#import "git2/commit.h"
#import "git2/errors.h"
#import "git2/merge.h"

@implementation GTCommit

Expand Down Expand Up @@ -130,4 +132,20 @@ - (NSArray *)parents {
return parents;
}

#pragma mark Merging

- (GTIndex *)merge:(GTCommit *)otherCommit error:(NSError **)error {
NSParameterAssert(otherCommit != nil);

git_index *index;

int result = git_merge_commits(&index, self.repository.git_repository, self.git_commit, otherCommit.git_commit, NULL);
if (result != GIT_OK || index == NULL) {
if (error != NULL) *error = [NSError git_errorFor:result description:@"Failed to merge commit %@ with commit %@", self.SHA, otherCommit.SHA];
return nil;
}

return [[GTIndex alloc] initWithGitIndex:index repository:self.repository];
}

@end
17 changes: 17 additions & 0 deletions ObjectiveGit/GTDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ typedef NS_OPTIONS(NSInteger, GTDiffFindOptionsFlags) {
/// Returns a newly created `GTDiff` object or nil on error.
+ (nullable instancetype)diffOldTree:(nullable GTTree *)oldTree withNewIndex:(nullable GTIndex *)newIndex inRepository:(GTRepository *)repository options:(nullable NSDictionary *)options error:(NSError **)error;

/// Create a diff between two `GTIndex`es.
///
/// Both instances must be from the same repository, or an exception will be thrown.
///
/// oldIndex - The "left" side of the diff. May be nil to represent an empty
/// index.
/// newIndex - The "right" side of the diff. May be nil to represent an empty
/// index.
/// repository - The repository to be used for the diff. Cannot be nil.
/// options - A dictionary containing any of the above options key constants, or
/// nil to use the defaults.
/// error - Populated with an `NSError` object on error, if information is
/// available.
///
/// Returns a newly created `GTDiff` object or nil on error.
+ (nullable instancetype)diffOldIndex:(nullable GTIndex *)oldIndex withNewIndex:(nullable GTIndex *)newIndex inRepository:(GTRepository *)repository options:(nullable NSDictionary *)options error:(NSError **)error;

/// Create a diff between a repository's current index.
///
/// This is equivalent to `git diff --cached <treeish>` or if you pass the HEAD
Expand Down
16 changes: 16 additions & 0 deletions ObjectiveGit/GTDiff.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ + (instancetype)diffOldTree:(GTTree *)oldTree withNewIndex:(GTIndex *)newIndex i
return [[self alloc] initWithGitDiff:diff repository:repository];
}

+ (nullable instancetype)diffOldIndex:(GTIndex *)oldIndex withNewIndex:(GTIndex *)newIndex inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error
{
NSParameterAssert(repository != nil);

__block git_diff *diff;
int status = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
return git_diff_index_to_index(&diff, repository.git_repository, oldIndex.git_index, newIndex.git_index, optionsStruct);
}];
if (status != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to create diff between %@ and %@", oldIndex, newIndex];
return nil;
}

return [[self alloc] initWithGitDiff:diff repository:repository];
}

+ (instancetype)diffIndexFromTree:(GTTree *)tree inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
NSParameterAssert(repository != nil);
NSParameterAssert(tree == nil || [tree.repository isEqual:repository]);
Expand Down

0 comments on commit 8b46e45

Please sign in to comment.