Skip to content

Commit f021ff4

Browse files
committed
+[GTDiff diffOldIndex:withNewIndex:…]
Wrapper for git_diff_index_to_index to create diffs from two in-memory indeces.
1 parent 53c4f5f commit f021ff4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

ObjectiveGit/GTDiff.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,23 @@ typedef NS_OPTIONS(NSInteger, GTDiffFindOptionsFlags) {
218218
/// Returns a newly created `GTDiff` object or nil on error.
219219
+ (nullable instancetype)diffOldTree:(nullable GTTree *)oldTree withNewIndex:(nullable GTIndex *)newIndex inRepository:(GTRepository *)repository options:(nullable NSDictionary *)options error:(NSError **)error;
220220

221+
/// Create a diff between two `GTIndex`es.
222+
///
223+
/// Both instances must be from the same repository, or an exception will be thrown.
224+
///
225+
/// oldIndex - The "left" side of the diff. May be nil to represent an empty
226+
/// index.
227+
/// newIndex - The "right" side of the diff. May be nil to represent an empty
228+
/// index.
229+
/// repository - The repository to be used for the diff. Cannot be nil.
230+
/// options - A dictionary containing any of the above options key constants, or
231+
/// nil to use the defaults.
232+
/// error - Populated with an `NSError` object on error, if information is
233+
/// available.
234+
///
235+
/// Returns a newly created `GTDiff` object or nil on error.
236+
+ (nullable instancetype)diffOldIndex:(nullable GTIndex *)oldIndex withNewIndex:(nullable GTIndex *)newIndex inRepository:(GTRepository *)repository options:(nullable NSDictionary *)options error:(NSError **)error;
237+
221238
/// Create a diff between a repository's current index.
222239
///
223240
/// 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
@@ -110,6 +110,22 @@ + (instancetype)diffOldTree:(GTTree *)oldTree withNewIndex:(GTIndex *)newIndex i
110110
return [[self alloc] initWithGitDiff:diff repository:repository];
111111
}
112112

113+
+ (nullable instancetype)diffOldIndex:(GTIndex *)oldIndex withNewIndex:(GTIndex *)newIndex inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error
114+
{
115+
NSParameterAssert(repository != nil);
116+
117+
__block git_diff *diff;
118+
int status = [self handleParsedOptionsDictionary:options usingBlock:^(git_diff_options *optionsStruct) {
119+
return git_diff_index_to_index(&diff, repository.git_repository, oldIndex.git_index, newIndex.git_index, optionsStruct);
120+
}];
121+
if (status != GIT_OK) {
122+
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to create diff between %@ and %@", oldIndex, newIndex];
123+
return nil;
124+
}
125+
126+
return [[self alloc] initWithGitDiff:diff repository:repository];
127+
}
128+
113129
+ (instancetype)diffIndexFromTree:(GTTree *)tree inRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
114130
NSParameterAssert(repository != nil);
115131
NSParameterAssert(tree == nil || [tree.repository isEqual:repository]);

0 commit comments

Comments
 (0)