Skip to content
This repository

Diff Line Model Object #112

Merged
merged 8 commits into from over 1 year ago

4 participants

Danny Greg Alan Rogers Justin Spahr-Summers Josh Abernathy
Danny Greg
Collaborator

When using the new diff API it was pretty inconvenient that we didn't actually represent a line as a model object at any point, but rather just passed all of it's components separately into the enumeration block.

This changes that, representing a line as an object.

Alan Rogers
Collaborator

Looks pretty good to me. Could do with some unit tests for the new class though. (yes I'm back :dancer:)

Classes/GTDiffLine.h
((11 lines not shown))
  11 +// A character representing the origin of a given line.
  12 +//
  13 +// See diff.h for individual documentation.
  14 +typedef enum : git_diff_line_t {
  15 + GTDiffLineOriginContext = GIT_DIFF_LINE_CONTEXT,
  16 + GTDiffLineOriginAddition = GIT_DIFF_LINE_ADDITION,
  17 + GTDiffLineOriginDeletion = GIT_DIFF_LINE_DELETION,
  18 + GTDiffLineOriginAddEOFNewLine = GIT_DIFF_LINE_ADD_EOFNL,
  19 + GTDiffLineOriginDeleteEOFNewLine = GIT_DIFF_LINE_DEL_EOFNL,
  20 +} GTDiffLineOrigin;
  21 +
  22 +// Represents an individual line in a diff hunk.
  23 +@interface GTDiffLine : NSObject
  24 +
  25 +// The content string of the line.
  26 +@property (nonatomic, readonly) NSString *content;
2
Danny Greg Collaborator

Nice catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Danny Greg
Collaborator

@alanjrogers it isn't split out, but this is unit tested in the diff unit tests. Although the more the merrier I guess :p

Danny Greg
Collaborator

:u55b6:

Justin Spahr-Summers

Looks good to me.

@alanjrogers?

Danny Greg
Collaborator

I presumed he was a :+1: with his above comment.

Justin Spahr-Summers

I'd like to give him the opportunity to reply about the tests.

Danny Greg
Collaborator

I feel the need for :shipit:

Justin Spahr-Summers

You can just pin to this branch from your other work. :P

Danny Greg
Collaborator

Yeah I already am but it's not quite like a PR getting merged man. It's my crack. :p

Alan Rogers alanjrogers merged commit 0bbc548 into from
Alan Rogers alanjrogers deleted the branch
Alan Rogers
Collaborator

Crack administered :grinning:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
14 Classes/GTDiffHunk.h
@@ -9,17 +9,7 @@
9 9 #import "git2.h"
10 10
11 11 @class GTDiffDelta;
12   -
13   -// A character representing the origin of a given line.
14   -//
15   -// See diff.h for individual documentation.
16   -typedef enum : git_diff_line_t {
17   - GTDiffHunkLineOriginContext = GIT_DIFF_LINE_CONTEXT,
18   - GTDiffHunkLineOriginAddition = GIT_DIFF_LINE_ADDITION,
19   - GTDiffHunkLineOriginDeletion = GIT_DIFF_LINE_DELETION,
20   - GTDiffHunkLineOriginAddEOFNewLine = GIT_DIFF_LINE_ADD_EOFNL,
21   - GTDiffHunkLineOriginDeleteEOFNewLine = GIT_DIFF_LINE_DEL_EOFNL,
22   -} GTDiffHunkLineOrigin;
  12 +@class GTDiffLine;
23 13
24 14 // A class representing a hunk within a diff delta.
25 15 @interface GTDiffHunk : NSObject
@@ -42,6 +32,6 @@ typedef enum : git_diff_line_t {
42 32 //
43 33 // block - A block to execute on each line. Setting `stop` to `NO` will
44 34 // immediately stop the enumeration and return from the method.
45   -- (void)enumerateLinesInHunkUsingBlock:(void (^)(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop))block;
  35 +- (void)enumerateLinesInHunkUsingBlock:(void (^)(GTDiffLine *line, BOOL *stop))block;
46 36
47 37 @end
6 Classes/GTDiffHunk.m
@@ -9,6 +9,7 @@
9 9 #import "GTDiffHunk.h"
10 10
11 11 #import "GTDiffDelta.h"
  12 +#import "GTDiffLine.h"
12 13
13 14 @interface GTDiffHunk ()
14 15
@@ -38,7 +39,7 @@ - (instancetype)initWithDelta:(GTDiffDelta *)delta hunkIndex:(NSUInteger)hunkInd
38 39 return self;
39 40 }
40 41
41   -- (void)enumerateLinesInHunkUsingBlock:(void (^)(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop))block {
  42 +- (void)enumerateLinesInHunkUsingBlock:(void (^)(GTDiffLine *line, BOOL *stop))block {
42 43 NSParameterAssert(block != nil);
43 44
44 45 for (NSUInteger idx = 0; idx < self.lineCount; idx ++) {
@@ -51,8 +52,9 @@ - (void)enumerateLinesInHunkUsingBlock:(void (^)(NSString *lineContent, NSUInteg
51 52 if (result != GIT_OK) continue;
52 53
53 54 NSString *lineString = [[[NSString alloc] initWithBytes:content length:contentLength encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:NSCharacterSet.newlineCharacterSet];
  55 + GTDiffLine *line = [[GTDiffLine alloc] initWithContent:lineString oldLineNumber:oldLineNumber newLineNumber:newLineNumber origin:lineOrigin];
54 56 BOOL stop = NO;
55   - block(lineString, (NSUInteger)oldLineNumber, (NSUInteger)newLineNumber, lineOrigin, &stop);
  57 + block(line, &stop);
56 58 if (stop) return;
57 59 }
58 60 }
44 Classes/GTDiffLine.h
... ... @@ -0,0 +1,44 @@
  1 +//
  2 +// GTDiffLine.h
  3 +// ObjectiveGitFramework
  4 +//
  5 +// Created by Danny Greg on 20/12/2012.
  6 +// Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7 +//
  8 +
  9 +#import "git2.h"
  10 +
  11 +// A character representing the origin of a given line.
  12 +//
  13 +// See diff.h for individual documentation.
  14 +typedef enum : git_diff_line_t {
  15 + GTDiffLineOriginContext = GIT_DIFF_LINE_CONTEXT,
  16 + GTDiffLineOriginAddition = GIT_DIFF_LINE_ADDITION,
  17 + GTDiffLineOriginDeletion = GIT_DIFF_LINE_DELETION,
  18 + GTDiffLineOriginAddEOFNewLine = GIT_DIFF_LINE_ADD_EOFNL,
  19 + GTDiffLineOriginDeleteEOFNewLine = GIT_DIFF_LINE_DEL_EOFNL,
  20 +} GTDiffLineOrigin;
  21 +
  22 +// Represents an individual line in a diff hunk.
  23 +@interface GTDiffLine : NSObject
  24 +
  25 +// The content string of the line.
  26 +@property (nonatomic, readonly, copy) NSString *content;
  27 +
  28 +// The line number of this line in the left side of the diff.
  29 +//
  30 +// -1 if the line is an addition.
  31 +@property (nonatomic, readonly) NSInteger oldLineNumber;
  32 +
  33 +// The line number of this line in the right side of the diff.
  34 +//
  35 +// -1 if the line is a deletion.
  36 +@property (nonatomic, readonly) NSInteger newLineNumber;
  37 +
  38 +// The origin of the line, see the enum above for possible values.
  39 +@property (nonatomic, readonly) GTDiffLineOrigin origin;
  40 +
  41 +// Designated initialiser.
  42 +- (instancetype)initWithContent:(NSString *)content oldLineNumber:(NSInteger)oldLineNumber newLineNumber:(NSInteger)newLineNumber origin:(GTDiffLineOrigin)origin;
  43 +
  44 +@end
25 Classes/GTDiffLine.m
... ... @@ -0,0 +1,25 @@
  1 +//
  2 +// GTDiffLine.m
  3 +// ObjectiveGitFramework
  4 +//
  5 +// Created by Danny Greg on 20/12/2012.
  6 +// Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7 +//
  8 +
  9 +#import "GTDiffLine.h"
  10 +
  11 +@implementation GTDiffLine
  12 +
  13 +- (instancetype)initWithContent:(NSString *)content oldLineNumber:(NSInteger)oldLineNumber newLineNumber:(NSInteger)newLineNumber origin:(GTDiffLineOrigin)origin {
  14 + self = [super init];
  15 + if (self == nil) return nil;
  16 +
  17 + _content = [content copy];
  18 + _oldLineNumber = oldLineNumber;
  19 + _newLineNumber = newLineNumber;
  20 + _origin = origin;
  21 +
  22 + return self;
  23 +}
  24 +
  25 +@end
1  Classes/ObjectiveGit.h
@@ -51,6 +51,7 @@
51 51 #import <ObjectiveGit/GTDiffDelta.h>
52 52 #import <ObjectiveGit/GTDiffFile.h>
53 53 #import <ObjectiveGit/GTDiffHunk.h>
  54 +#import <ObjectiveGit/GTDiffLine.h>
54 55
55 56 // This must be called before doing any ObjectiveGit work. Under normal
56 57 // circumstances, it will automatically be called on your behalf.
36 ObjectiveGitFramework.xcodeproj/project.pbxproj
@@ -79,6 +79,10 @@
79 79 30A3D6551667F11C00C49A39 /* GTDiff.h in Headers */ = {isa = PBXBuildFile; fileRef = 30A3D6521667F11C00C49A39 /* GTDiff.h */; settings = {ATTRIBUTES = (Public, ); }; };
80 80 30A3D6561667F11C00C49A39 /* GTDiff.m in Sources */ = {isa = PBXBuildFile; fileRef = 30A3D6531667F11C00C49A39 /* GTDiff.m */; };
81 81 30A3D6571667F11C00C49A39 /* GTDiff.m in Sources */ = {isa = PBXBuildFile; fileRef = 30A3D6531667F11C00C49A39 /* GTDiff.m */; };
  82 + 30FDC07F16835A8100654BF0 /* GTDiffLine.h in Headers */ = {isa = PBXBuildFile; fileRef = 30FDC07D16835A8100654BF0 /* GTDiffLine.h */; settings = {ATTRIBUTES = (Public, ); }; };
  83 + 30FDC08016835A8100654BF0 /* GTDiffLine.h in Headers */ = {isa = PBXBuildFile; fileRef = 30FDC07D16835A8100654BF0 /* GTDiffLine.h */; settings = {ATTRIBUTES = (Public, ); }; };
  84 + 30FDC08116835A8100654BF0 /* GTDiffLine.m in Sources */ = {isa = PBXBuildFile; fileRef = 30FDC07E16835A8100654BF0 /* GTDiffLine.m */; };
  85 + 30FDC08216835A8100654BF0 /* GTDiffLine.m in Sources */ = {isa = PBXBuildFile; fileRef = 30FDC07E16835A8100654BF0 /* GTDiffLine.m */; };
82 86 3E0A23E5159E0FDB00A6068F /* GTObjectDatabase.h in Headers */ = {isa = PBXBuildFile; fileRef = 55C8054C13861F34004DCB0F /* GTObjectDatabase.h */; settings = {ATTRIBUTES = (Public, ); }; };
83 87 55C8054F13861FE7004DCB0F /* GTObjectDatabase.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C8054D13861F34004DCB0F /* GTObjectDatabase.m */; };
84 88 55C8055013861FE7004DCB0F /* GTObjectDatabase.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C8054D13861F34004DCB0F /* GTObjectDatabase.m */; };
@@ -284,6 +288,8 @@
284 288 30865A90167F503400B1AB6E /* GTDiffSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTDiffSpec.m; sourceTree = "<group>"; };
285 289 30A3D6521667F11C00C49A39 /* GTDiff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTDiff.h; sourceTree = "<group>"; };
286 290 30A3D6531667F11C00C49A39 /* GTDiff.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTDiff.m; sourceTree = "<group>"; };
  291 + 30FDC07D16835A8100654BF0 /* GTDiffLine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTDiffLine.h; sourceTree = "<group>"; };
  292 + 30FDC07E16835A8100654BF0 /* GTDiffLine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTDiffLine.m; sourceTree = "<group>"; };
287 293 32DBCF5E0370ADEE00C91783 /* ObjectiveGitFramework_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjectiveGitFramework_Prefix.pch; sourceTree = "<group>"; };
288 294 55C8054C13861F34004DCB0F /* GTObjectDatabase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTObjectDatabase.h; sourceTree = "<group>"; };
289 295 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 @@
469 475 name = "Other Frameworks";
470 476 sourceTree = "<group>";
471 477 };
  478 + 30FDC07C16835A6F00654BF0 /* Diff */ = {
  479 + isa = PBXGroup;
  480 + children = (
  481 + 30A3D6521667F11C00C49A39 /* GTDiff.h */,
  482 + 30A3D6531667F11C00C49A39 /* GTDiff.m */,
  483 + 3011D8691668E48500CE3409 /* GTDiffFile.h */,
  484 + 3011D86A1668E48500CE3409 /* GTDiffFile.m */,
  485 + 3011D86F1668E78500CE3409 /* GTDiffHunk.h */,
  486 + 3011D8701668E78500CE3409 /* GTDiffHunk.m */,
  487 + 3011D8751668F29600CE3409 /* GTDiffDelta.h */,
  488 + 3011D8761668F29600CE3409 /* GTDiffDelta.m */,
  489 + 30FDC07D16835A8100654BF0 /* GTDiffLine.h */,
  490 + 30FDC07E16835A8100654BF0 /* GTDiffLine.m */,
  491 + );
  492 + name = Diff;
  493 + sourceTree = "<group>";
  494 + };
472 495 32C88DFF0371C24200C91783 /* Other Sources */ = {
473 496 isa = PBXGroup;
474 497 children = (
@@ -607,14 +630,7 @@
607 630 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */,
608 631 883CD6A91600EBC600F57354 /* GTRemote.h */,
609 632 883CD6AA1600EBC600F57354 /* GTRemote.m */,
610   - 30A3D6521667F11C00C49A39 /* GTDiff.h */,
611   - 30A3D6531667F11C00C49A39 /* GTDiff.m */,
612   - 3011D8691668E48500CE3409 /* GTDiffFile.h */,
613   - 3011D86A1668E48500CE3409 /* GTDiffFile.m */,
614   - 3011D86F1668E78500CE3409 /* GTDiffHunk.h */,
615   - 3011D8701668E78500CE3409 /* GTDiffHunk.m */,
616   - 3011D8751668F29600CE3409 /* GTDiffDelta.h */,
617   - 3011D8761668F29600CE3409 /* GTDiffDelta.m */,
  633 + 30FDC07C16835A6F00654BF0 /* Diff */,
618 634 );
619 635 path = Classes;
620 636 sourceTree = "<group>";
@@ -662,6 +678,7 @@
662 678 30A3D6551667F11C00C49A39 /* GTDiff.h in Headers */,
663 679 3011D86C1668E48500CE3409 /* GTDiffFile.h in Headers */,
664 680 3011D8721668E78500CE3409 /* GTDiffHunk.h in Headers */,
  681 + 30FDC08016835A8100654BF0 /* GTDiffLine.h in Headers */,
665 682 3011D8781668F29600CE3409 /* GTDiffDelta.h in Headers */,
666 683 );
667 684 runOnlyForDeploymentPostprocessing = 0;
@@ -695,6 +712,7 @@
695 712 30A3D6541667F11C00C49A39 /* GTDiff.h in Headers */,
696 713 3011D86B1668E48500CE3409 /* GTDiffFile.h in Headers */,
697 714 3011D8711668E78500CE3409 /* GTDiffHunk.h in Headers */,
  715 + 30FDC07F16835A8100654BF0 /* GTDiffLine.h in Headers */,
698 716 3011D8771668F29600CE3409 /* GTDiffDelta.h in Headers */,
699 717 8849C6A214AD81FF003890AF /* GTRepository+Private.h in Headers */,
700 718 );
@@ -941,6 +959,7 @@
941 959 3011D86E1668E48500CE3409 /* GTDiffFile.m in Sources */,
942 960 3011D8741668E78500CE3409 /* GTDiffHunk.m in Sources */,
943 961 3011D87A1668F29600CE3409 /* GTDiffDelta.m in Sources */,
  962 + 30FDC08216835A8100654BF0 /* GTDiffLine.m in Sources */,
944 963 );
945 964 runOnlyForDeploymentPostprocessing = 0;
946 965 };
@@ -995,6 +1014,7 @@
995 1014 3011D86D1668E48500CE3409 /* GTDiffFile.m in Sources */,
996 1015 3011D8731668E78500CE3409 /* GTDiffHunk.m in Sources */,
997 1016 3011D8791668F29600CE3409 /* GTDiffDelta.m in Sources */,
  1017 + 30FDC08116835A8100654BF0 /* GTDiffLine.m in Sources */,
998 1018 );
999 1019 runOnlyForDeploymentPostprocessing = 0;
1000 1020 };
14 ObjectiveGitTests/GTDiffSpec.m
@@ -93,14 +93,14 @@
93 93 NSUInteger subtractionLine = 3;
94 94 NSUInteger additionLine = 4;
95 95 __block NSUInteger lineIndex = 0;
96   - [hunk enumerateLinesInHunkUsingBlock:^(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop) {
97   - expect(lineContent).to.equal(expectedLines[lineIndex]);
  96 + [hunk enumerateLinesInHunkUsingBlock:^(GTDiffLine *line, BOOL *stop) {
  97 + expect(line.content).to.equal(expectedLines[lineIndex]);
98 98 if (lineIndex == subtractionLine) {
99   - expect((NSUInteger)lineOrigin).to.equal(GTDiffHunkLineOriginDeletion);
  99 + expect((NSUInteger)line.origin).to.equal(GTDiffLineOriginDeletion);
100 100 } else if (lineIndex == additionLine) {
101   - expect((NSUInteger)lineOrigin).to.equal(GTDiffHunkLineOriginAddition);
  101 + expect((NSUInteger)line.origin).to.equal(GTDiffLineOriginAddition);
102 102 } else {
103   - expect((NSUInteger)lineOrigin).to.equal(GTDiffHunkLineOriginContext);
  103 + expect((NSUInteger)line.origin).to.equal(GTDiffLineOriginContext);
104 104 }
105 105
106 106 lineIndex ++;
@@ -162,8 +162,8 @@
162 162 expect(delta.hunkCount).to.equal(1);
163 163 [delta enumerateHunksWithBlock:^(GTDiffHunk *hunk, BOOL *stop) {
164 164 __block NSUInteger contextCount = 0;
165   - [hunk enumerateLinesInHunkUsingBlock:^(NSString *lineContent, NSUInteger oldLineNumber, NSUInteger newLineNumber, GTDiffHunkLineOrigin lineOrigin, BOOL *stop) {
166   - if (lineOrigin == GTDiffHunkLineOriginContext) contextCount ++;
  165 + [hunk enumerateLinesInHunkUsingBlock:^(GTDiffLine *line, BOOL *stop) {
  166 + if (line.origin == GTDiffLineOriginContext) contextCount ++;
167 167 }];
168 168 expect(contextCount).to.equal(10);
169 169 *stop = YES;

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.