Diff Line Model Object #112

Merged
merged 8 commits into from Dec 21, 2012
View
14 Classes/GTDiffHunk.h
@@ -9,17 +9,7 @@
#import "git2.h"
@class GTDiffDelta;
-
-// A character representing the origin of a given line.
-//
-// See diff.h for individual documentation.
-typedef enum : git_diff_line_t {
- GTDiffHunkLineOriginContext = GIT_DIFF_LINE_CONTEXT,
- GTDiffHunkLineOriginAddition = GIT_DIFF_LINE_ADDITION,
- GTDiffHunkLineOriginDeletion = GIT_DIFF_LINE_DELETION,
- GTDiffHunkLineOriginAddEOFNewLine = GIT_DIFF_LINE_ADD_EOFNL,
- GTDiffHunkLineOriginDeleteEOFNewLine = GIT_DIFF_LINE_DEL_EOFNL,
-} GTDiffHunkLineOrigin;
+@class GTDiffLine;
// A class representing a hunk within a diff delta.
@interface GTDiffHunk : NSObject
@@ -42,6 +32,6 @@ typedef enum : git_diff_line_t {
//
// block - A block to execute on each line. Setting `stop` to `NO` will
// immediately stop the enumeration and return from the method.
-- (void)enumerateLinesInHunkUsingBlock:(void (^)(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop))block;
+- (void)enumerateLinesInHunkUsingBlock:(void (^)(GTDiffLine *line, BOOL *stop))block;
@end
View
6 Classes/GTDiffHunk.m
@@ -9,6 +9,7 @@
#import "GTDiffHunk.h"
#import "GTDiffDelta.h"
+#import "GTDiffLine.h"
@interface GTDiffHunk ()
@@ -38,7 +39,7 @@ - (instancetype)initWithDelta:(GTDiffDelta *)delta hunkIndex:(NSUInteger)hunkInd
return self;
}
-- (void)enumerateLinesInHunkUsingBlock:(void (^)(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop))block {
+- (void)enumerateLinesInHunkUsingBlock:(void (^)(GTDiffLine *line, BOOL *stop))block {
NSParameterAssert(block != nil);
for (NSUInteger idx = 0; idx < self.lineCount; idx ++) {
@@ -51,8 +52,9 @@ - (void)enumerateLinesInHunkUsingBlock:(void (^)(NSString *lineContent, NSUInteg
if (result != GIT_OK) continue;
NSString *lineString = [[[NSString alloc] initWithBytes:content length:contentLength encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:NSCharacterSet.newlineCharacterSet];
+ GTDiffLine *line = [[GTDiffLine alloc] initWithContent:lineString oldLineNumber:oldLineNumber newLineNumber:newLineNumber origin:lineOrigin];
BOOL stop = NO;
- block(lineString, (NSUInteger)oldLineNumber, (NSUInteger)newLineNumber, lineOrigin, &stop);
+ block(line, &stop);
if (stop) return;
}
}
View
44 Classes/GTDiffLine.h
@@ -0,0 +1,44 @@
+//
+// GTDiffLine.h
+// ObjectiveGitFramework
+//
+// Created by Danny Greg on 20/12/2012.
+// Copyright (c) 2012 GitHub, Inc. All rights reserved.
+//
+
+#import "git2.h"
+
+// A character representing the origin of a given line.
+//
+// See diff.h for individual documentation.
+typedef enum : git_diff_line_t {
+ GTDiffLineOriginContext = GIT_DIFF_LINE_CONTEXT,
+ GTDiffLineOriginAddition = GIT_DIFF_LINE_ADDITION,
+ GTDiffLineOriginDeletion = GIT_DIFF_LINE_DELETION,
+ GTDiffLineOriginAddEOFNewLine = GIT_DIFF_LINE_ADD_EOFNL,
+ GTDiffLineOriginDeleteEOFNewLine = GIT_DIFF_LINE_DEL_EOFNL,
+} GTDiffLineOrigin;
+
+// Represents an individual line in a diff hunk.
+@interface GTDiffLine : NSObject
+
+// The content string of the line.
+@property (nonatomic, readonly, copy) NSString *content;
+
+// The line number of this line in the left side of the diff.
+//
+// -1 if the line is an addition.
+@property (nonatomic, readonly) NSInteger oldLineNumber;
+
+// The line number of this line in the right side of the diff.
+//
+// -1 if the line is a deletion.
+@property (nonatomic, readonly) NSInteger newLineNumber;
+
+// The origin of the line, see the enum above for possible values.
+@property (nonatomic, readonly) GTDiffLineOrigin origin;
+
+// Designated initialiser.
+- (instancetype)initWithContent:(NSString *)content oldLineNumber:(NSInteger)oldLineNumber newLineNumber:(NSInteger)newLineNumber origin:(GTDiffLineOrigin)origin;
+
+@end
View
25 Classes/GTDiffLine.m
@@ -0,0 +1,25 @@
+//
+// GTDiffLine.m
+// ObjectiveGitFramework
+//
+// Created by Danny Greg on 20/12/2012.
+// Copyright (c) 2012 GitHub, Inc. All rights reserved.
+//
+
+#import "GTDiffLine.h"
+
+@implementation GTDiffLine
+
+- (instancetype)initWithContent:(NSString *)content oldLineNumber:(NSInteger)oldLineNumber newLineNumber:(NSInteger)newLineNumber origin:(GTDiffLineOrigin)origin {
+ self = [super init];
+ if (self == nil) return nil;
+
+ _content = [content copy];
+ _oldLineNumber = oldLineNumber;
+ _newLineNumber = newLineNumber;
+ _origin = origin;
+
+ return self;
+}
+
+@end
View
1 Classes/ObjectiveGit.h
@@ -51,6 +51,7 @@
#import <ObjectiveGit/GTDiffDelta.h>
#import <ObjectiveGit/GTDiffFile.h>
#import <ObjectiveGit/GTDiffHunk.h>
+#import <ObjectiveGit/GTDiffLine.h>
// This must be called before doing any ObjectiveGit work. Under normal
// circumstances, it will automatically be called on your behalf.
View
36 ObjectiveGitFramework.xcodeproj/project.pbxproj
@@ -79,6 +79,10 @@
30A3D6551667F11C00C49A39 /* GTDiff.h in Headers */ = {isa = PBXBuildFile; fileRef = 30A3D6521667F11C00C49A39 /* GTDiff.h */; settings = {ATTRIBUTES = (Public, ); }; };
30A3D6561667F11C00C49A39 /* GTDiff.m in Sources */ = {isa = PBXBuildFile; fileRef = 30A3D6531667F11C00C49A39 /* GTDiff.m */; };
30A3D6571667F11C00C49A39 /* GTDiff.m in Sources */ = {isa = PBXBuildFile; fileRef = 30A3D6531667F11C00C49A39 /* GTDiff.m */; };
+ 30FDC07F16835A8100654BF0 /* GTDiffLine.h in Headers */ = {isa = PBXBuildFile; fileRef = 30FDC07D16835A8100654BF0 /* GTDiffLine.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 30FDC08016835A8100654BF0 /* GTDiffLine.h in Headers */ = {isa = PBXBuildFile; fileRef = 30FDC07D16835A8100654BF0 /* GTDiffLine.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 30FDC08116835A8100654BF0 /* GTDiffLine.m in Sources */ = {isa = PBXBuildFile; fileRef = 30FDC07E16835A8100654BF0 /* GTDiffLine.m */; };
+ 30FDC08216835A8100654BF0 /* GTDiffLine.m in Sources */ = {isa = PBXBuildFile; fileRef = 30FDC07E16835A8100654BF0 /* GTDiffLine.m */; };
3E0A23E5159E0FDB00A6068F /* GTObjectDatabase.h in Headers */ = {isa = PBXBuildFile; fileRef = 55C8054C13861F34004DCB0F /* GTObjectDatabase.h */; settings = {ATTRIBUTES = (Public, ); }; };
55C8054F13861FE7004DCB0F /* GTObjectDatabase.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C8054D13861F34004DCB0F /* GTObjectDatabase.m */; };
55C8055013861FE7004DCB0F /* GTObjectDatabase.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C8054D13861F34004DCB0F /* GTObjectDatabase.m */; };
@@ -284,6 +288,8 @@
30865A90167F503400B1AB6E /* GTDiffSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTDiffSpec.m; sourceTree = "<group>"; };
30A3D6521667F11C00C49A39 /* GTDiff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTDiff.h; sourceTree = "<group>"; };
30A3D6531667F11C00C49A39 /* GTDiff.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTDiff.m; sourceTree = "<group>"; };
+ 30FDC07D16835A8100654BF0 /* GTDiffLine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTDiffLine.h; sourceTree = "<group>"; };
+ 30FDC07E16835A8100654BF0 /* GTDiffLine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTDiffLine.m; sourceTree = "<group>"; };
32DBCF5E0370ADEE00C91783 /* ObjectiveGitFramework_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjectiveGitFramework_Prefix.pch; sourceTree = "<group>"; };
55C8054C13861F34004DCB0F /* GTObjectDatabase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTObjectDatabase.h; sourceTree = "<group>"; };
55C8054D13861F34004DCB0F /* GTObjectDatabase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = GTObjectDatabase.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
@@ -469,6 +475,23 @@
name = "Other Frameworks";
sourceTree = "<group>";
};
+ 30FDC07C16835A6F00654BF0 /* Diff */ = {
+ isa = PBXGroup;
+ children = (
+ 30A3D6521667F11C00C49A39 /* GTDiff.h */,
+ 30A3D6531667F11C00C49A39 /* GTDiff.m */,
+ 3011D8691668E48500CE3409 /* GTDiffFile.h */,
+ 3011D86A1668E48500CE3409 /* GTDiffFile.m */,
+ 3011D86F1668E78500CE3409 /* GTDiffHunk.h */,
+ 3011D8701668E78500CE3409 /* GTDiffHunk.m */,
+ 3011D8751668F29600CE3409 /* GTDiffDelta.h */,
+ 3011D8761668F29600CE3409 /* GTDiffDelta.m */,
+ 30FDC07D16835A8100654BF0 /* GTDiffLine.h */,
+ 30FDC07E16835A8100654BF0 /* GTDiffLine.m */,
+ );
+ name = Diff;
+ sourceTree = "<group>";
+ };
32C88DFF0371C24200C91783 /* Other Sources */ = {
isa = PBXGroup;
children = (
@@ -607,14 +630,7 @@
88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */,
883CD6A91600EBC600F57354 /* GTRemote.h */,
883CD6AA1600EBC600F57354 /* GTRemote.m */,
- 30A3D6521667F11C00C49A39 /* GTDiff.h */,
- 30A3D6531667F11C00C49A39 /* GTDiff.m */,
- 3011D8691668E48500CE3409 /* GTDiffFile.h */,
- 3011D86A1668E48500CE3409 /* GTDiffFile.m */,
- 3011D86F1668E78500CE3409 /* GTDiffHunk.h */,
- 3011D8701668E78500CE3409 /* GTDiffHunk.m */,
- 3011D8751668F29600CE3409 /* GTDiffDelta.h */,
- 3011D8761668F29600CE3409 /* GTDiffDelta.m */,
+ 30FDC07C16835A6F00654BF0 /* Diff */,
);
path = Classes;
sourceTree = "<group>";
@@ -662,6 +678,7 @@
30A3D6551667F11C00C49A39 /* GTDiff.h in Headers */,
3011D86C1668E48500CE3409 /* GTDiffFile.h in Headers */,
3011D8721668E78500CE3409 /* GTDiffHunk.h in Headers */,
+ 30FDC08016835A8100654BF0 /* GTDiffLine.h in Headers */,
3011D8781668F29600CE3409 /* GTDiffDelta.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -695,6 +712,7 @@
30A3D6541667F11C00C49A39 /* GTDiff.h in Headers */,
3011D86B1668E48500CE3409 /* GTDiffFile.h in Headers */,
3011D8711668E78500CE3409 /* GTDiffHunk.h in Headers */,
+ 30FDC07F16835A8100654BF0 /* GTDiffLine.h in Headers */,
3011D8771668F29600CE3409 /* GTDiffDelta.h in Headers */,
8849C6A214AD81FF003890AF /* GTRepository+Private.h in Headers */,
);
@@ -941,6 +959,7 @@
3011D86E1668E48500CE3409 /* GTDiffFile.m in Sources */,
3011D8741668E78500CE3409 /* GTDiffHunk.m in Sources */,
3011D87A1668F29600CE3409 /* GTDiffDelta.m in Sources */,
+ 30FDC08216835A8100654BF0 /* GTDiffLine.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -995,6 +1014,7 @@
3011D86D1668E48500CE3409 /* GTDiffFile.m in Sources */,
3011D8731668E78500CE3409 /* GTDiffHunk.m in Sources */,
3011D8791668F29600CE3409 /* GTDiffDelta.m in Sources */,
+ 30FDC08116835A8100654BF0 /* GTDiffLine.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
14 ObjectiveGitTests/GTDiffSpec.m
@@ -93,14 +93,14 @@
NSUInteger subtractionLine = 3;
NSUInteger additionLine = 4;
__block NSUInteger lineIndex = 0;
- [hunk enumerateLinesInHunkUsingBlock:^(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop) {
- expect(lineContent).to.equal(expectedLines[lineIndex]);
+ [hunk enumerateLinesInHunkUsingBlock:^(GTDiffLine *line, BOOL *stop) {
+ expect(line.content).to.equal(expectedLines[lineIndex]);
if (lineIndex == subtractionLine) {
- expect((NSUInteger)lineOrigin).to.equal(GTDiffHunkLineOriginDeletion);
+ expect((NSUInteger)line.origin).to.equal(GTDiffLineOriginDeletion);
} else if (lineIndex == additionLine) {
- expect((NSUInteger)lineOrigin).to.equal(GTDiffHunkLineOriginAddition);
+ expect((NSUInteger)line.origin).to.equal(GTDiffLineOriginAddition);
} else {
- expect((NSUInteger)lineOrigin).to.equal(GTDiffHunkLineOriginContext);
+ expect((NSUInteger)line.origin).to.equal(GTDiffLineOriginContext);
}
lineIndex ++;
@@ -162,8 +162,8 @@
expect(delta.hunkCount).to.equal(1);
[delta enumerateHunksWithBlock:^(GTDiffHunk *hunk, BOOL *stop) {
__block NSUInteger contextCount = 0;
- [hunk enumerateLinesInHunkUsingBlock:^(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop) {
- if (lineOrigin == GTDiffHunkLineOriginContext) contextCount ++;
+ [hunk enumerateLinesInHunkUsingBlock:^(GTDiffLine *line, BOOL *stop) {
+ if (line.origin == GTDiffLineOriginContext) contextCount ++;
}];
expect(contextCount).to.equal(10);
*stop = YES;