Skip to content

Commit

Permalink
Merge pull request #535 from phatblat/ben/movehead
Browse files Browse the repository at this point in the history
Expose -moveHEAD
  • Loading branch information
joshaber committed Nov 11, 2015
2 parents 452bdaf + 66339b5 commit 183232f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ObjectiveGit/GTRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ extern NSString * const GTRepositoryInitOptionsOriginURLString;
/// Returns a GTReference or nil if an error occurs.
- (nullable GTReference *)headReferenceWithError:(NSError **)error;

/// Move HEAD reference safely, since deleting and recreating HEAD is always wrong.
///
/// reference - The new target reference for HEAD.
/// error - If not NULL, set to any error that occurs.
///
/// Returns NO if an error occurs.
- (BOOL)moveHEADToReference:(GTReference *)reference error:(NSError **)error;

/// Move HEAD reference safely, since deleting and recreating HEAD is always wrong.
///
/// commit - The commit which HEAD should point to.
/// error - If not NULL, set to any error that occurs.
///
/// Returns NO if an error occurs.
- (BOOL)moveHEADToCommit:(GTCommit *)commit error:(NSError **)error;

/// Get the local branches.
///
/// error - If not NULL, set to any error that occurs.
Expand All @@ -286,7 +302,7 @@ extern NSString * const GTRepositoryInitOptionsOriginURLString;
/// Get branches with names sharing a given prefix.
///
/// prefix - The prefix to use for filtering. Must not be nil.
/// error - If not NULL, set to any error that occurs.
/// error - If not NULL, set to any error that occurs.
///
/// Returns an array of GTBranches or nil if an error occurs.
- (nullable NSArray<GTBranch *> *)branchesWithPrefix:(NSString *)prefix error:(NSError **)error;
Expand Down
52 changes: 52 additions & 0 deletions ObjectiveGitTests/GTRepositorySpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,58 @@
});
});

describe(@"move head", ^{
beforeEach(^{
repository = self.testAppFixtureRepository;
});

//- (BOOL)moveHEADToReference:(GTReference *)reference error:(NSError **)error;
it(@"should move to reference", ^{
NSError *error = nil;
GTReference *originalHead = [repository headReferenceWithError:NULL];

GTReference *targetReference = [repository lookUpReferenceWithName:@"refs/heads/other-branch" error:NULL];
expect(targetReference).notTo(beNil());

// -> Test the move
BOOL success = [repository moveHEADToReference:targetReference error:&error];
expect(@(success)).to(beTruthy());
expect(error).to(beNil());

// Verify
GTReference *head = [repository headReferenceWithError:&error];
expect(head).notTo(beNil());
expect(head).notTo(equal(originalHead));
expect(head.targetOID.SHA).to(equal(targetReference.targetOID.SHA));
});

//- (BOOL)moveHEADToCommit:(GTCommit *)commit error:(NSError **)error;
it(@"should move to commit", ^{
NSError *error = nil;
GTReference *originalHead = [repository headReferenceWithError:NULL];
NSString *targetCommitSHA = @"f7ecd8f4404d3a388efbff6711f1bdf28ffd16a0";

GTCommit *commit = [repository lookUpObjectBySHA:targetCommitSHA error:NULL];
expect(commit).notTo(beNil());

GTCommit *originalHeadCommit = [repository lookUpObjectByOID:originalHead.targetOID error:NULL];
expect(originalHeadCommit).notTo(beNil());

// -> Test the move
BOOL success = [repository moveHEADToCommit:commit error:&error];
expect(@(success)).to(beTruthy());
expect(error).to(beNil());

// Test for detached?

// Verify
GTReference *head = [repository headReferenceWithError:&error];
expect(head).notTo(beNil());
expect(head.targetOID.SHA).to(equal(targetCommitSHA));
});
});


describe(@"-checkout:strategy:error:progressBlock:", ^{
it(@"should allow references", ^{
NSError *error = nil;
Expand Down

0 comments on commit 183232f

Please sign in to comment.