Skip to content
Browse files

make it easy to diff between the wd and HEAD

  • Loading branch information...
1 parent 007d97e commit 1bb457a6f18d040f58789b815e3ba79060693b2a @joshaber joshaber committed Mar 20, 2013
Showing with 32 additions and 0 deletions.
  1. +10 −0 Classes/GTDiff.h
  2. +18 −0 Classes/GTDiff.m
  3. +4 −0 ObjectiveGitTests/GTDiffSpec.m
View
10 Classes/GTDiff.h
@@ -212,6 +212,16 @@ typedef enum : git_diff_find_t {
// Returns a newly created `GTDiff` object or nil on error.
+ (GTDiff *)diffWorkingDirectoryFromTree:(GTTree *)tree options:(NSDictionary *)options error:(NSError **)error;
+// Create a diff between the working directory and HEAD.
+//
+// repository - The repository to be used for the diff.
+// options - A dictionary containing any of the above options key constants,
+// or nil to use the defaults.
+// error - Populated if an error occurs.
+//
+// Returns a newly created GTDiff, or nil if an error occurred.
++ (GTDiff *)diffWorkingDirectoryToHEADInRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error;
+
// Designated initialiser.
- (instancetype)initWithGitDiffList:(git_diff_list *)diffList;
View
18 Classes/GTDiff.m
@@ -11,6 +11,7 @@
#import "GTDiffDelta.h"
#import "GTRepository.h"
#import "GTTree.h"
+#import "GTCommit.h"
#import "NSError+Git.h"
@@ -144,6 +145,23 @@ + (GTDiff *)diffWorkingDirectoryFromTree:(GTTree *)tree options:(NSDictionary *)
return newDiff;
}
++ (GTDiff *)diffWorkingDirectoryToHEADInRepository:(GTRepository *)repository options:(NSDictionary *)options error:(NSError **)error {
+ NSParameterAssert(repository != nil);
+
+ GTCommit *HEADCommit = (GTCommit *)[repository lookupObjectByRefspec:@"HEAD" error:error];
+ if (HEADCommit == nil) return nil;
+
+ GTDiff *HEADIndexDiff = [GTDiff diffIndexFromTree:HEADCommit.tree options:options error:error];
+ if (HEADIndexDiff == nil) return nil;
+
+ GTDiff *WDDiff = [GTDiff diffIndexToWorkingDirectoryInRepository:repository options:options error:error];
+ if (WDDiff == nil) return nil;
+
+ git_diff_merge(HEADIndexDiff.git_diff_list, WDDiff.git_diff_list);
+
+ return HEADIndexDiff;
+}
+
- (instancetype)initWithGitDiffList:(git_diff_list *)diffList {
NSParameterAssert(diffList != NULL);
View
4 ObjectiveGitTests/GTDiffSpec.m
@@ -42,6 +42,10 @@
it(@"should be able to initialse a diff against an index from a repo's working directory", ^{
expect([GTDiff diffIndexToWorkingDirectoryInRepository:repository options:nil error:NULL]).toNot.beNil();
});
+
+ it(@"should be able to initialize a diff between HEAD and the working directory", ^{
+ expect([GTDiff diffWorkingDirectoryToHEADInRepository:repository options:nil error:NULL]).notTo.beNil();
+ });
});
describe(@"GTDiff diffing", ^{

0 comments on commit 1bb457a

Please sign in to comment.
Something went wrong with that request. Please try again.