-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathGTTreeEntry.m
152 lines (120 loc) · 4.28 KB
/
GTTreeEntry.m
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// GTTreeEntry.m
// ObjectiveGitFramework
//
// Created by Timothy Clem on 2/22/11.
//
// The MIT License
//
// Copyright (c) 2011 Tim Clem
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "GTTreeEntry.h"
#import "GTObject.h"
#import "GTTree.h"
#import "GTRepository.h"
#import "NSError+Git.h"
#import "NSString+Git.h"
#import "GTOID.h"
#import "git2/errors.h"
@interface GTTreeEntry ()
@property (nonatomic, assign, readonly) git_tree_entry *git_tree_entry;
@end
@implementation GTTreeEntry
#pragma mark NSObject
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p> name: %@, type: %@, sha: %@, attributes: %lu", NSStringFromClass(self.class), self, self.name, self.typeString, self.SHA, (unsigned long)self.attributes];
}
- (NSUInteger)hash {
return self.OID.hash;
}
- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:[self class]]) {
return [self isEqualToEntry:object];
}
return [super isEqual:object];
}
- (BOOL)isEqualToEntry:(GTTreeEntry *)treeEntry {
return git_tree_entry_cmp(self.git_tree_entry, treeEntry.git_tree_entry) == 0 ? YES : NO;
}
- (void)dealloc {
git_tree_entry_free(_git_tree_entry);
}
#pragma mark API
- (instancetype)initWithEntry:(const git_tree_entry *)theEntry parentTree:(GTTree *)parent error:(NSError **)error {
NSParameterAssert(theEntry != NULL);
self = [super init];
if (self == nil) return nil;
git_tree_entry *copyOfEntry = nil;
int gitError = git_tree_entry_dup(©OfEntry, theEntry);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to duplicate tree entry."];
return nil;
}
_git_tree_entry = copyOfEntry;
_tree = parent;
return self;
}
+ (instancetype)entryWithEntry:(const git_tree_entry *)theEntry parentTree:(GTTree *)parent error:(NSError **)error {
return [[self alloc] initWithEntry:theEntry parentTree:parent error:error];
}
- (NSString *)name {
NSString *name = @(git_tree_entry_name(self.git_tree_entry));
NSAssert(name, @"name was nil");
return name;
}
- (NSInteger)attributes {
return git_tree_entry_filemode(self.git_tree_entry);
}
- (GTOID *)OID {
return [GTOID oidWithGitOid:git_tree_entry_id(self.git_tree_entry)];
}
- (NSString *)SHA {
return self.OID.SHA;
}
- (GTObjectType)type {
return (GTObjectType)git_tree_entry_type(self.git_tree_entry);
}
- (NSString *)typeString {
return @(git_object_type2string(git_tree_entry_type(self.git_tree_entry)));
}
- (GTRepository *)repository {
return self.tree.repository;
}
- (GTObject *)GTObject:(NSError **)error {
return [GTObject objectWithTreeEntry:self error:error];
}
@end
@implementation GTObject (GTTreeEntry)
+ (instancetype)objectWithTreeEntry:(GTTreeEntry *)treeEntry error:(NSError **)error {
return [[self alloc] initWithTreeEntry:treeEntry error:error];
}
- (instancetype)initWithTreeEntry:(GTTreeEntry *)treeEntry error:(NSError **)error {
git_object *obj;
int gitError = git_tree_entry_to_object(&obj, treeEntry.repository.git_repository, treeEntry.git_tree_entry);
if (gitError < GIT_OK) {
if (error != NULL) {
*error = [NSError git_errorFor:gitError description:@"Failed to get object for tree entry."];
}
return nil;
}
return [self initWithObj:obj inRepository:treeEntry.repository];
}
@end