Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 78 lines (56 sloc) 1.827 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
//
// GTDiffDelta.m
// ObjectiveGitFramework
//
// Created by Danny Greg on 30/11/2012.
// Copyright (c) 2012 GitHub, Inc. All rights reserved.
//

#import "GTDiffDelta.h"

#import "GTDiff.h"
#import "GTDiffFile.h"
#import "GTDiffPatch.h"
#import "NSError+Git.h"

@interface GTDiffDelta ()

// The index of this delta within its parent `diff`.
@property (nonatomic, assign, readonly) NSUInteger deltaIndex;

@end

@implementation GTDiffDelta

#pragma mark Properties

- (git_diff_delta)git_diff_delta {
return *(git_diff_get_delta(self.diff.git_diff, self.deltaIndex));
}

- (GTDiffFileFlag)flags {
return (GTDiffFileFlag)self.git_diff_delta.flags;
}

- (GTDiffFile *)oldFile {
return [[GTDiffFile alloc] initWithGitDiffFile:self.git_diff_delta.old_file];
}

- (GTDiffFile *)newFile {
return [[GTDiffFile alloc] initWithGitDiffFile:self.git_diff_delta.new_file];
}

- (GTDiffDeltaType)type {
return (GTDiffDeltaType)self.git_diff_delta.status;
}

#pragma mark Lifecycle

- (instancetype)initWithDiff:(GTDiff *)diff deltaIndex:(NSUInteger)deltaIndex {
self = [super init];
if (self == nil) return nil;

_diff = diff;
_deltaIndex = deltaIndex;

return self;
}

#pragma mark Patch Generation

- (GTDiffPatch *)generatePatch:(NSError **)error {
git_patch *patch = NULL;
int gitError = git_patch_from_diff(&patch, self.diff.git_diff, self.deltaIndex);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Patch generation failed for delta %@", self];
return nil;
}

return [[GTDiffPatch alloc] initWithGitPatch:patch delta:self];
}

#pragma mark NSObject

- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p>{ flags: %u, oldFile: %@, newFile: %@ }", self.class, self, (unsigned)self.git_diff_delta.flags, self.oldFile, self.newFile];
}

@end
Something went wrong with that request. Please try again.