Skip to content

Commit 53c4f5f

Browse files
committed
+[GTDiff diffOldTree:withNewIndex:…]
Adding support for diffing a tree and an arbitrary GTIndex (for in-mem comparisons of pending merges)
1 parent 991cd22 commit 53c4f5f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

ObjectiveGit/GTDiff.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@class GTDiffDelta;
1414
@class GTRepository;
1515
@class GTTree;
16+
@class GTIndex;
1617

1718
NS_ASSUME_NONNULL_BEGIN
1819

@@ -200,6 +201,23 @@ typedef NS_OPTIONS(NSInteger, GTDiffFindOptionsFlags) {
200201
/// Returns a newly created `GTDiff` object or nil on error.
201202
+ (nullable instancetype)diffOldTree:(nullable GTTree *)oldTree withNewTree:(nullable GTTree *)newTree inRepository:(GTRepository *)repository options:(nullable NSDictionary *)options error:(NSError **)error;
202203

204+
/// Create a diff between `GTTree` and `GTIndex`.
205+
///
206+
/// Both instances must be from the same repository, or an exception will be thrown.
207+
///
208+
/// oldTree - The "left" side of the diff. May be nil to represent an empty
209+
/// tree.
210+
/// newIndex - The "right" side of the diff. May be nil to represent an empty
211+
/// index.
212+
/// repository - The repository to be used for the diff. Cannot be nil.
213+
/// options - A dictionary containing any of the above options key constants, or
214+
/// nil to use the defaults.
215+
/// error - Populated with an `NSError` object on error, if information is
216+
/// available.
217+
///
218+
/// Returns a newly created `GTDiff` object or nil on error.
219+
+ (nullable instancetype)diffOldTree:(nullable GTTree *)oldTree withNewIndex:(nullable GTIndex *)newIndex inRepository:(GTRepository *)repository options:(nullable NSDictionary *)options error:(NSError **)error;
220+
203221
/// Create a diff between a repository's current index.
204222
///
205223
/// This is equivalent to `git diff --cached <treeish>` or if you pass the HEAD

ObjectiveGit/GTDiff.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "GTCommit.h"
1212
#import "GTRepository.h"
1313
#import "GTTree.h"
14+
#import "GTIndex.h"
1415
#import "NSArray+StringArray.h"
1516
#import "NSError+Git.h"
1617

@@ -94,6 +95,21 @@ + (instancetype)diffOldTree:(GTTree *)oldTree withNewTree:(GTTree *)newTree inRe
9495
return [[self alloc] initWithGitDiff:diff repository:repository];
9596
}
9697

98+
+ (instancetype)diffOldTree:(GTTree *)oldTree withNewIndex:(GTIndex *)newIndex inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
99+
NSParameterAssert(repository != nil);
100+
101+
__block git_diff *diff;
102+
int status = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
103+
return git_diff_tree_to_index(&diff, repository.git_repository, oldTree.git_tree, newIndex.git_index, optionsStruct);
104+
}];
105+
if (status != GIT_OK) {
106+
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to create diff between %@ and %@", oldTree.SHA, newIndex];
107+
return nil;
108+
}
109+
110+
return [[self alloc] initWithGitDiff:diff repository:repository];
111+
}
112+
97113
+ (instancetype)diffIndexFromTree:(GTTree *)tree inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
98114
NSParameterAssert(repository != nil);
99115
NSParameterAssert(tree == nil || [tree.repository isEqual:repository]);

0 commit comments

Comments
 (0)