Skip to content

Commit 816a6cd

Browse files
committed
Add some methods to GTIndexEntry
1 parent 0b91bc4 commit 816a6cd

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

ObjectiveGit/GTIndexEntry.h

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
#import <Foundation/Foundation.h>
3131
#include "git2/index.h"
32+
#import "GTObject.h"
33+
34+
@class GTIndex;
3235

3336
typedef NS_ENUM(NSInteger, GTIndexEntryStatus) {
3437
GTIndexEntryStatusUpdated = 0,
@@ -40,6 +43,22 @@ typedef NS_ENUM(NSInteger, GTIndexEntryStatus) {
4043

4144
@interface GTIndexEntry : NSObject
4245

46+
/// Initializes the receiver with the given libgit2 index entry.
47+
///
48+
/// entry - The libgit2 index entry. Cannot be NULL.
49+
/// index - The index this entry belongs to.
50+
/// error - will be filled if an error occurs
51+
///
52+
/// Returns the initialized object.
53+
- (instancetype)initWithGitIndexEntry:(const git_index_entry *)entry index:(GTIndex *)index error:(NSError **)error NS_DESIGNATED_INITIALIZER;
54+
- (instancetype)initWithGitIndexEntry:(const git_index_entry *)entry;
55+
56+
/// The underlying `git_index_entry` object.
57+
- (const git_index_entry *)git_index_entry __attribute__((objc_returns_inner_pointer));
58+
59+
/// The entry's index. This may be nil if nil is passed in to -initWithGitIndexEntry:
60+
@property (nonatomic, strong, readonly) GTIndex *index;
61+
4362
/// The repository-relative path for the entry.
4463
@property (nonatomic, readonly, copy) NSString *path;
4564

@@ -49,14 +68,21 @@ typedef NS_ENUM(NSInteger, GTIndexEntryStatus) {
4968
/// What is the entry's status?
5069
@property (nonatomic, readonly) GTIndexEntryStatus status;
5170

52-
/// Initializes the receiver with the given libgit2 index entry.
71+
/// The OID of the entry.
72+
@property (nonatomic, strong, readonly) GTOID *OID;
73+
74+
/// Convert the entry into an GTObject
5375
///
54-
/// entry - The libgit2 index entry. Cannot be NULL.
76+
/// error - will be filled if an error occurs
5577
///
56-
/// Returns the initialized object.
57-
- (id)initWithGitIndexEntry:(const git_index_entry *)entry NS_DESIGNATED_INITIALIZER;
78+
/// Returns this entry as a GTObject or nil if an error occurred.
79+
- (GTObject *)GTObject:(NSError **)error;
5880

59-
/// The underlying `git_index_entry` object.
60-
- (const git_index_entry *)git_index_entry __attribute__((objc_returns_inner_pointer));
81+
@end
82+
83+
@interface GTObject (GTIndexEntry)
84+
85+
+ (instancetype)objectWithIndexEntry:(GTIndexEntry *)treeEntry error:(NSError **)error;
86+
- (instancetype)initWithIndexEntry:(GTIndexEntry *)treeEntry error:(NSError **)error;
6187

6288
@end

ObjectiveGit/GTIndexEntry.m

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
#import "GTIndexEntry.h"
3131
#import "NSError+Git.h"
3232
#import "NSString+Git.h"
33+
#import "GTOID.h"
34+
#import "GTRepository.h"
35+
#import "GTIndex.h"
36+
37+
#import "git2.h"
3338

3439
@interface GTIndexEntry ()
3540
@property (nonatomic, assign, readonly) const git_index_entry *git_index_entry;
@@ -45,17 +50,22 @@ - (NSString *)description {
4550

4651
#pragma mark Lifecycle
4752

48-
- (id)initWithGitIndexEntry:(const git_index_entry *)entry {
53+
- (instancetype)initWithGitIndexEntry:(const git_index_entry *)entry index:(GTIndex *)index error:(NSError **)error {
4954
NSParameterAssert(entry != NULL);
5055

5156
self = [super init];
5257
if (self == nil) return nil;
5358

5459
_git_index_entry = entry;
60+
_index = index;
5561

5662
return self;
5763
}
5864

65+
- (instancetype)initWithGitIndexEntry:(const git_index_entry *)entry {
66+
return [self initWithGitIndexEntry:entry index:nil error:NULL];
67+
}
68+
5969
#pragma mark Properties
6070

6171
- (NSString *)path {
@@ -86,4 +96,44 @@ - (GTIndexEntryStatus)status {
8696
return GTIndexEntryStatusUpToDate;
8797
}
8898

99+
- (GTOID *)OID {
100+
return [GTOID oidWithGitOid:&self.git_index_entry->id];
101+
}
102+
103+
#pragma mark API
104+
105+
- (GTRepository *)repository {
106+
return self.index.repository;
107+
}
108+
109+
- (GTObject *)GTObject:(NSError **)error {
110+
return [GTObject objectWithIndexEntry:self error:error];
111+
}
112+
113+
@end
114+
115+
@implementation GTObject (GTIndexEntry)
116+
117+
+ (instancetype)objectWithIndexEntry:(GTIndexEntry *)indexEntry error:(NSError **)error {
118+
return [[self alloc] initWithIndexEntry:indexEntry error:error];
119+
}
120+
121+
- (instancetype)initWithIndexEntry:(GTIndexEntry *)indexEntry error:(NSError **)error {
122+
123+
git_object *obj;
124+
int gitError = git_object_lookup(&obj, indexEntry.repository.git_repository, indexEntry.OID.git_oid, (git_otype)GTObjectTypeAny);
125+
126+
if (gitError < GIT_OK) {
127+
if (error != NULL) {
128+
*error = [NSError git_errorFor:gitError description:@"Failed to get object for index entry."];
129+
}
130+
131+
return nil;
132+
}
133+
134+
135+
return [self initWithObj:obj inRepository:indexEntry.repository];
136+
}
137+
89138
@end
139+

ObjectiveGitTests/GTIndexSpec.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,9 @@
295295
[index addData:data withName:@"bar/foo" error:&error];
296296
expect(error).to(beNil());
297297

298-
NSData *returnData = [index dataWithName:@"bar/foo" error:&error];
298+
GTIndexEntry *entry = [index entryWithName:@"bar/foo" error:&error];
299+
expect(entry).notTo(beNil());
299300
expect(error).to(beNil());
300-
// expect([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]).to(equal(@"foo"));
301-
expect(returnData).to(equal(data));
302301
});
303302
});
304303

0 commit comments

Comments
 (0)