diff --git a/ObjectiveGit/GTReference.h b/ObjectiveGit/GTReference.h index 1421fb322..1cc35dd4b 100644 --- a/ObjectiveGit/GTReference.h +++ b/ObjectiveGit/GTReference.h @@ -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; diff --git a/ObjectiveGit/GTReference.m b/ObjectiveGit/GTReference.m index c3100655e..bf51e6156 100644 --- a/ObjectiveGit/GTReference.m +++ b/ObjectiveGit/GTReference.m @@ -32,6 +32,7 @@ #import "GTSignature.h" #import "NSError+Git.h" #import "NSString+Git.h" +#import "GTRepository+References.h" #import "git2/errors.h" @@ -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); @@ -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; } @@ -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 { diff --git a/ObjectiveGit/GTRepository+References.h b/ObjectiveGit/GTRepository+References.h new file mode 100644 index 000000000..ed6f004aa --- /dev/null +++ b/ObjectiveGit/GTRepository+References.h @@ -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 diff --git a/ObjectiveGit/GTRepository+References.m b/ObjectiveGit/GTRepository+References.m new file mode 100644 index 000000000..1494d7539 --- /dev/null +++ b/ObjectiveGit/GTRepository+References.m @@ -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 diff --git a/ObjectiveGit/GTRepository+RemoteOperations.m b/ObjectiveGit/GTRepository+RemoteOperations.m index 000d3f88f..6f4f3b183 100644 --- a/ObjectiveGit/GTRepository+RemoteOperations.m +++ b/ObjectiveGit/GTRepository+RemoteOperations.m @@ -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" @@ -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]; diff --git a/ObjectiveGit/GTRepository.m b/ObjectiveGit/GTRepository.m index 999896050..a93d886ef 100644 --- a/ObjectiveGit/GTRepository.m +++ b/ObjectiveGit/GTRepository.m @@ -51,6 +51,7 @@ #import "NSArray+StringArray.h" #import "NSError+Git.h" #import "NSString+Git.h" +#import "GTRepository+References.h" #import "git2.h" @@ -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]; diff --git a/ObjectiveGit/ObjectiveGit.h b/ObjectiveGit/ObjectiveGit.h index 0108166a0..da4a672eb 100644 --- a/ObjectiveGit/ObjectiveGit.h +++ b/ObjectiveGit/ObjectiveGit.h @@ -37,6 +37,7 @@ FOUNDATION_EXPORT const unsigned char ObjectiveGitVersionString[]; #import #import #import +#import #import #import #import diff --git a/ObjectiveGitFramework.xcodeproj/project.pbxproj b/ObjectiveGitFramework.xcodeproj/project.pbxproj index d2260dae3..4ca80aa71 100644 --- a/ObjectiveGitFramework.xcodeproj/project.pbxproj +++ b/ObjectiveGitFramework.xcodeproj/project.pbxproj @@ -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 */; }; @@ -471,6 +475,8 @@ 88A994B916FCE7D400402C7B /* GTBranchSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTBranchSpec.m; sourceTree = ""; }; 88A994C916FCED1D00402C7B /* QuickSpec+GTFixtures.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "QuickSpec+GTFixtures.h"; sourceTree = ""; }; 88A994CA16FCED1D00402C7B /* QuickSpec+GTFixtures.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "QuickSpec+GTFixtures.m"; sourceTree = ""; }; + 88B2131A1B20E785005CF2C5 /* GTRepository+References.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+References.h"; sourceTree = ""; }; + 88B2131B1B20E785005CF2C5 /* GTRepository+References.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+References.m"; sourceTree = ""; }; 88BC0E4E18EF4F3600C7D0E6 /* GTRepository+Reset.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GTRepository+Reset.h"; sourceTree = ""; }; 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "GTRepository+Reset.m"; sourceTree = ""; }; 88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfigurationSpec.m; sourceTree = ""; }; @@ -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 */, @@ -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 */, @@ -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 */, @@ -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 */, @@ -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 */, diff --git a/ObjectiveGitTests/GTBranchSpec.m b/ObjectiveGitTests/GTBranchSpec.m index cbbffa02d..47ae25176 100644 --- a/ObjectiveGitTests/GTBranchSpec.m +++ b/ObjectiveGitTests/GTBranchSpec.m @@ -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()); diff --git a/ObjectiveGitTests/GTReferenceSpec.m b/ObjectiveGitTests/GTReferenceSpec.m index 2bf041d77..e54b741c8 100644 --- a/ObjectiveGitTests/GTReferenceSpec.m +++ b/ObjectiveGitTests/GTReferenceSpec.m @@ -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()); @@ -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()); @@ -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()); @@ -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()); @@ -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; diff --git a/ObjectiveGitTests/GTRepositorySpec.m b/ObjectiveGitTests/GTRepositorySpec.m index 589d2e11f..82b528f40 100644 --- a/ObjectiveGitTests/GTRepositorySpec.m +++ b/ObjectiveGitTests/GTRepositorySpec.m @@ -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];