Skip to content

Expose -moveHEAD #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: could you align the - for the args?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this one was missed 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😲

/// 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