Skip to content

Commit

Permalink
Merge pull request #463 from libgit2/look-up-ref
Browse files Browse the repository at this point in the history
Look up ref
  • Loading branch information
mdiep committed Jun 4, 2015
2 parents 5373acf + 93ce73b commit d4e18a9
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 34 deletions.
3 changes: 0 additions & 3 deletions ObjectiveGit/GTReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ typedef NS_OPTIONS(NSInteger, GTReferenceType) {
@property (nonatomic, readonly, strong) GTReflog *reflog;

/// Convenience initializers
+ (id)referenceByLookingUpReferencedNamed:(NSString *)refName inRepository:(GTRepository *)theRepo error:(NSError **)error;
- (id)initByLookingUpReferenceNamed:(NSString *)refName inRepository:(GTRepository *)theRepo error:(NSError **)error;

+ (id)referenceByResolvingSymbolicReference:(GTReference *)symbolicRef error:(NSError **)error;
- (id)initByResolvingSymbolicReference:(GTReference *)symbolicRef error:(NSError **)error;

Expand Down
23 changes: 3 additions & 20 deletions ObjectiveGit/GTReference.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#import "GTSignature.h"
#import "NSError+Git.h"
#import "NSString+Git.h"
#import "GTRepository+References.h"

#import "git2/errors.h"

Expand Down Expand Up @@ -69,28 +70,10 @@ - (BOOL)isRemote {
return git_reference_is_remote(self.git_reference) != 0;
}

+ (id)referenceByLookingUpReferencedNamed:(NSString *)refName inRepository:(GTRepository *)theRepo error:(NSError **)error {
return [[self alloc] initByLookingUpReferenceNamed:refName inRepository:theRepo error:error];
}

+ (id)referenceByResolvingSymbolicReference:(GTReference *)symbolicRef error:(NSError **)error {
return [[self alloc] initByResolvingSymbolicReference:symbolicRef error:error];
}

- (id)initByLookingUpReferenceNamed:(NSString *)refName inRepository:(GTRepository *)repo error:(NSError **)error {
NSParameterAssert(refName != nil);
NSParameterAssert(repo != nil);

git_reference *ref = NULL;
int gitError = git_reference_lookup(&ref, repo.git_repository, refName.UTF8String);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to lookup reference %@.", refName];
return nil;
}

return [self initWithGitReference:ref repository:repo];
}

- (id)initByResolvingSymbolicReference:(GTReference *)symbolicRef error:(NSError **)error {
NSParameterAssert(symbolicRef != nil);

Expand Down Expand Up @@ -151,7 +134,7 @@ - (id)unresolvedTarget {
NSString *refName = @(git_reference_symbolic_target(self.git_reference));
if (refName == NULL) return nil;

return [self.class referenceByLookingUpReferencedNamed:refName inRepository:self.repository error:NULL];
return [self.repository lookUpReferenceWithName:refName error:NULL];
}
return nil;
}
Expand Down Expand Up @@ -221,7 +204,7 @@ - (GTOID *)OID {
}

- (GTReference *)reloadedReferenceWithError:(NSError **)error {
return [[self.class alloc] initByLookingUpReferenceNamed:self.name inRepository:self.repository error:error];
return [self.repository lookUpReferenceWithName:self.name error:error];
}

+ (NSError *)invalidReferenceError {
Expand Down
23 changes: 23 additions & 0 deletions ObjectiveGit/GTRepository+References.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// GTRepository+References.h
// ObjectiveGitFramework
//
// Created by Josh Abernathy on 6/4/15.
// Copyright (c) 2015 GitHub, Inc. All rights reserved.
//

#import "GTrepository.h"

@class GTReference;

@interface GTRepository (References)

/// Look up a reference by name.
///
/// name - The name of the reference to look up. Cannot be nil.
/// error - The error if one occurs. May be NULL.
///
/// Returns the reference or nil if look up failed.
- (GTReference *)lookUpReferenceWithName:(NSString *)name error:(NSError **)error;

@end
30 changes: 30 additions & 0 deletions ObjectiveGit/GTRepository+References.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// GTRepository+References.m
// ObjectiveGitFramework
//
// Created by Josh Abernathy on 6/4/15.
// Copyright (c) 2015 GitHub, Inc. All rights reserved.
//

#import "GTRepository+References.h"
#import "GTReference.h"
#import "NSError+Git.h"

#import "git2/errors.h"

@implementation GTRepository (References)

- (GTReference *)lookUpReferenceWithName:(NSString *)name error:(NSError **)error {
NSParameterAssert(name != nil);

git_reference *ref = NULL;
int gitError = git_reference_lookup(&ref, self.git_repository, name.UTF8String);
if (gitError != GIT_OK) {
if (error != NULL) *error = [NSError git_errorFor:gitError description:@"Failed to lookup reference %@.", name];
return nil;
}

return [[GTReference alloc] initWithGitReference:ref repository:self];
}

@end
3 changes: 2 additions & 1 deletion ObjectiveGit/GTRepository+RemoteOperations.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "GTSignature.h"
#import "NSArray+StringArray.h"
#import "NSError+Git.h"
#import "GTRepository+References.h"

#import "git2/errors.h"
#import "git2/remote.h"
Expand Down Expand Up @@ -120,7 +121,7 @@ int GTFetchHeadEntriesCallback(const char *ref_name, const char *remote_url, con
GTRepository *repository = entriesPayload->repository;
GTRemoteEnumerateFetchHeadEntryBlock enumerationBlock = entriesPayload->enumerationBlock;

GTReference *reference = [GTReference referenceByLookingUpReferencedNamed:@(ref_name) inRepository:repository error:NULL];
GTReference *reference = [repository lookUpReferenceWithName:@(ref_name) error:NULL];

GTFetchHeadEntry *entry = [[GTFetchHeadEntry alloc] initWithReference:reference remoteURLString:@(remote_url) targetOID:[GTOID oidWithGitOid:oid] isMerge:(BOOL)is_merge];

Expand Down
3 changes: 2 additions & 1 deletion ObjectiveGit/GTRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#import "NSArray+StringArray.h"
#import "NSError+Git.h"
#import "NSString+Git.h"
#import "GTRepository+References.h"

#import "git2.h"

Expand Down Expand Up @@ -401,7 +402,7 @@ - (NSArray *)branchesWithPrefix:(NSString *)prefix error:(NSError **)error {
for (NSString *refName in references) {
if (![refName hasPrefix:prefix]) continue;

GTReference *ref = [[GTReference alloc] initByLookingUpReferenceNamed:refName inRepository:self error:error];
GTReference *ref = [self lookUpReferenceWithName:refName error:error];
if (ref == nil) continue;

GTBranch *branch = [[GTBranch alloc] initWithReference:ref repository:self];
Expand Down
1 change: 1 addition & 0 deletions ObjectiveGit/ObjectiveGit.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ FOUNDATION_EXPORT const unsigned char ObjectiveGitVersionString[];
#import <ObjectiveGit/GTRepository+Stashing.h>
#import <ObjectiveGit/GTRepository+Committing.h>
#import <ObjectiveGit/GTRepository+Status.h>
#import <ObjectiveGit/GTRepository+References.h>
#import <ObjectiveGit/GTRepository+RemoteOperations.h>
#import <ObjectiveGit/GTRepository+Reset.h>
#import <ObjectiveGit/GTEnumerator.h>
Expand Down
12 changes: 12 additions & 0 deletions ObjectiveGitFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
889923FB19FF5DD40092A9A6 /* git2 in Headers */ = {isa = PBXBuildFile; fileRef = 889923F919FF5DD40092A9A6 /* git2 */; settings = {ATTRIBUTES = (Public, ); }; };
88A994BA16FCE7D400402C7B /* GTBranchSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88A994B916FCE7D400402C7B /* GTBranchSpec.m */; };
88A994CB16FCED1D00402C7B /* QuickSpec+GTFixtures.m in Sources */ = {isa = PBXBuildFile; fileRef = 88A994CA16FCED1D00402C7B /* QuickSpec+GTFixtures.m */; };
88B2131C1B20E785005CF2C5 /* GTRepository+References.h in Headers */ = {isa = PBXBuildFile; fileRef = 88B2131A1B20E785005CF2C5 /* GTRepository+References.h */; settings = {ATTRIBUTES = (Public, ); }; };
88B2131D1B20E785005CF2C5 /* GTRepository+References.h in Headers */ = {isa = PBXBuildFile; fileRef = 88B2131A1B20E785005CF2C5 /* GTRepository+References.h */; settings = {ATTRIBUTES = (Public, ); }; };
88B2131E1B20E785005CF2C5 /* GTRepository+References.m in Sources */ = {isa = PBXBuildFile; fileRef = 88B2131B1B20E785005CF2C5 /* GTRepository+References.m */; };
88B2131F1B20E785005CF2C5 /* GTRepository+References.m in Sources */ = {isa = PBXBuildFile; fileRef = 88B2131B1B20E785005CF2C5 /* GTRepository+References.m */; };
88BC0E5018EF4F3600C7D0E6 /* GTRepository+Reset.h in Headers */ = {isa = PBXBuildFile; fileRef = 88BC0E4E18EF4F3600C7D0E6 /* GTRepository+Reset.h */; settings = {ATTRIBUTES = (Public, ); }; };
88BC0E5218EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */ = {isa = PBXBuildFile; fileRef = 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */; };
88C0BC5917038CF3009E99AA /* GTConfigurationSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */; };
Expand Down Expand Up @@ -471,6 +475,8 @@
88A994B916FCE7D400402C7B /* GTBranchSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTBranchSpec.m; sourceTree = "<group>"; };
88A994C916FCED1D00402C7B /* QuickSpec+GTFixtures.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "QuickSpec+GTFixtures.h"; sourceTree = "<group>"; };
88A994CA16FCED1D00402C7B /* QuickSpec+GTFixtures.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "QuickSpec+GTFixtures.m"; sourceTree = "<group>"; };
88B2131A1B20E785005CF2C5 /* GTRepository+References.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+References.h"; sourceTree = "<group>"; };
88B2131B1B20E785005CF2C5 /* GTRepository+References.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+References.m"; sourceTree = "<group>"; };
88BC0E4E18EF4F3600C7D0E6 /* GTRepository+Reset.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Reset.h"; sourceTree = "<group>"; };
88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Reset.m"; sourceTree = "<group>"; };
88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfigurationSpec.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -779,6 +785,8 @@
88746CC317FA1C950005888A /* GTRepository+Committing.m */,
4DFFB159183AA8D600D1565E /* GTRepository+RemoteOperations.h */,
4DFFB15A183AA8D600D1565E /* GTRepository+RemoteOperations.m */,
88B2131A1B20E785005CF2C5 /* GTRepository+References.h */,
88B2131B1B20E785005CF2C5 /* GTRepository+References.m */,
BDD8AE6D13131B8800CB5D40 /* GTEnumerator.h */,
BDD8AE6E13131B8800CB5D40 /* GTEnumerator.m */,
BD6C22A71314625800992935 /* GTObject.h */,
Expand Down Expand Up @@ -964,6 +972,7 @@
6EEB51A1199D62B9001D72C0 /* GTFetchHeadEntry.h in Headers */,
BD441E08131ED0C300187010 /* GTReference.h in Headers */,
88F6D9D91320451F00CC0BA8 /* ObjectiveGit.h in Headers */,
88B2131C1B20E785005CF2C5 /* GTRepository+References.h in Headers */,
88F6D9FA1320467100CC0BA8 /* GTCommit.h in Headers */,
88F6D9FB1320467500CC0BA8 /* GTObject.h in Headers */,
AA046112134F4D2000DF526B /* GTOdbObject.h in Headers */,
Expand Down Expand Up @@ -1015,6 +1024,7 @@
D01B6F1519F82F7B00D411BC /* NSData+Git.h in Headers */,
D01B6F6119F82FA600D411BC /* GTFilterSource.h in Headers */,
D0E0171519F9AD820019930C /* ObjectiveGit.h in Headers */,
88B2131D1B20E785005CF2C5 /* GTRepository+References.h in Headers */,
D01B6F4919F82F8700D411BC /* GTOdbObject.h in Headers */,
D01B6F3919F82F8700D411BC /* GTTreeEntry.h in Headers */,
D01B6F5B19F82FA600D411BC /* GTSubmodule.h in Headers */,
Expand Down Expand Up @@ -1318,6 +1328,7 @@
3011D8731668E78500CE3409 /* GTDiffHunk.m in Sources */,
3011D8791668F29600CE3409 /* GTDiffDelta.m in Sources */,
6EEB51A2199D62B9001D72C0 /* GTFetchHeadEntry.m in Sources */,
88B2131E1B20E785005CF2C5 /* GTRepository+References.m in Sources */,
30FDC08116835A8100654BF0 /* GTDiffLine.m in Sources */,
886E622C18AEBF75000611A0 /* GTFilterSource.m in Sources */,
DD3D9513182A81E1004AF532 /* GTBlame.m in Sources */,
Expand Down Expand Up @@ -1375,6 +1386,7 @@
D01B6F1A19F82F7B00D411BC /* NSString+Git.m in Sources */,
D01B6F2619F82F8700D411BC /* GTStatusDelta.m in Sources */,
D01B6F2019F82F8700D411BC /* GTRepository.m in Sources */,
88B2131F1B20E785005CF2C5 /* GTRepository+References.m in Sources */,
D01B6F7019F82FB300D411BC /* GTDiffHunk.m in Sources */,
D01B6F4819F82F8700D411BC /* GTObjectDatabase.m in Sources */,
D01B6F2C19F82F8700D411BC /* GTRepository+RemoteOperations.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ObjectiveGitTests/GTBranchSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@

it(@"should return itself for a remote branch", ^{
NSError *error = nil;
GTReference *remoteRef = [GTReference referenceByLookingUpReferencedNamed:@"refs/remotes/origin/master" inRepository:repository error:&error];
GTReference *remoteRef = [repository lookUpReferenceWithName:@"refs/remotes/origin/master" error:&error];
expect(remoteRef).notTo(beNil());
expect(error).to(beNil());

Expand Down
14 changes: 7 additions & 7 deletions ObjectiveGitTests/GTReferenceSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
});

it(@"should compare equal to the same reference", ^{
expect([[GTReference alloc] initByLookingUpReferenceNamed:@"refs/heads/master" inRepository:repository error:NULL]).to(equal([[GTReference alloc] initByLookingUpReferenceNamed:@"refs/heads/master" inRepository:repository error:NULL]));
expect([repository lookUpReferenceWithName:@"refs/heads/master" error:NULL]).to(equal([repository lookUpReferenceWithName:@"refs/heads/master" error:NULL]));
});

it(@"should compare unequal to a different reference", ^{
expect([[GTReference alloc] initByLookingUpReferenceNamed:@"refs/heads/master" inRepository:repository error:NULL]).notTo(equal([[GTReference alloc] initByLookingUpReferenceNamed:@"refs/remotes/origin/master" inRepository:repository error:NULL]));
expect([repository lookUpReferenceWithName:@"refs/heads/master" error:NULL]).notTo(equal([repository lookUpReferenceWithName:@"refs/remotes/origin/master" error:NULL]));
});

describe(@"remote property", ^{
it(@"should be YES for a remote-tracking branch", ^{
NSError *error = nil;
GTReference *ref = [[GTReference alloc] initByLookingUpReferenceNamed:@"refs/remotes/origin/master" inRepository:repository error:&error];
GTReference *ref = [repository lookUpReferenceWithName:@"refs/remotes/origin/master" error:&error];
expect(ref).notTo(beNil());
expect(error).to(beNil());

Expand All @@ -42,7 +42,7 @@

it(@"should be NO for a local branch", ^{
NSError *error = nil;
GTReference *ref = [[GTReference alloc] initByLookingUpReferenceNamed:@"refs/heads/master" inRepository:repository error:&error];
GTReference *ref = [repository lookUpReferenceWithName:@"refs/heads/master" error:&error];
expect(ref).notTo(beNil());
expect(error).to(beNil());

Expand Down Expand Up @@ -131,7 +131,7 @@
describe(@"+referenceByLookingUpReferenceNamed:inRepository:error:", ^{
it(@"should return a valid reference to a branch", ^{
NSError *error = nil;
GTReference *ref = [GTReference referenceByLookingUpReferencedNamed:@"refs/heads/master" inRepository:bareRepository error:&error];
GTReference *ref = [bareRepository lookUpReferenceWithName:@"refs/heads/master" error:&error];
expect(ref).notTo(beNil());
expect(error).to(beNil());

Expand All @@ -140,7 +140,7 @@

it(@"should return a valid reference to a tag", ^{
NSError *error = nil;
GTReference *ref = [GTReference referenceByLookingUpReferencedNamed:@"refs/tags/v0.9" inRepository:bareRepository error:&error];
GTReference *ref = [bareRepository lookUpReferenceWithName:@"refs/tags/v0.9" error:&error];
expect(ref).notTo(beNil());
expect(error).to(beNil());

Expand All @@ -150,7 +150,7 @@

describe(@"creating", ^{
it(@"can create a reference from a symbolic reference", ^{
GTReference *target = [[GTReference alloc] initByLookingUpReferenceNamed:@"refs/heads/master" inRepository:bareRepository error:NULL];
GTReference *target = [bareRepository lookUpReferenceWithName:@"refs/heads/master" error:NULL];
expect(target).notTo(beNil());

NSError *error = nil;
Expand Down
2 changes: 1 addition & 1 deletion ObjectiveGitTests/GTRepositorySpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@
describe(@"-checkout:strategy:error:progressBlock:", ^{
it(@"should allow references", ^{
NSError *error = nil;
GTReference *ref = [GTReference referenceByLookingUpReferencedNamed:@"refs/heads/other-branch" inRepository:repository error:&error];
GTReference *ref = [repository lookUpReferenceWithName:@"refs/heads/other-branch" error:&error];
expect(ref).notTo(beNil());
expect(error.localizedDescription).to(beNil());
BOOL result = [repository checkoutReference:ref strategy:GTCheckoutStrategyAllowConflicts error:&error progressBlock:nil];
Expand Down

0 comments on commit d4e18a9

Please sign in to comment.