Skip to content

Commit 183232f

Browse files
committed
Merge pull request #535 from phatblat/ben/movehead
Expose -moveHEAD
2 parents 452bdaf + 66339b5 commit 183232f

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

ObjectiveGit/GTRepository.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,22 @@ extern NSString * const GTRepositoryInitOptionsOriginURLString;
269269
/// Returns a GTReference or nil if an error occurs.
270270
- (nullable GTReference *)headReferenceWithError:(NSError **)error;
271271

272+
/// Move HEAD reference safely, since deleting and recreating HEAD is always wrong.
273+
///
274+
/// reference - The new target reference for HEAD.
275+
/// error - If not NULL, set to any error that occurs.
276+
///
277+
/// Returns NO if an error occurs.
278+
- (BOOL)moveHEADToReference:(GTReference *)reference error:(NSError **)error;
279+
280+
/// Move HEAD reference safely, since deleting and recreating HEAD is always wrong.
281+
///
282+
/// commit - The commit which HEAD should point to.
283+
/// error - If not NULL, set to any error that occurs.
284+
///
285+
/// Returns NO if an error occurs.
286+
- (BOOL)moveHEADToCommit:(GTCommit *)commit error:(NSError **)error;
287+
272288
/// Get the local branches.
273289
///
274290
/// error - If not NULL, set to any error that occurs.
@@ -286,7 +302,7 @@ extern NSString * const GTRepositoryInitOptionsOriginURLString;
286302
/// Get branches with names sharing a given prefix.
287303
///
288304
/// prefix - The prefix to use for filtering. Must not be nil.
289-
/// error - If not NULL, set to any error that occurs.
305+
/// error - If not NULL, set to any error that occurs.
290306
///
291307
/// Returns an array of GTBranches or nil if an error occurs.
292308
- (nullable NSArray<GTBranch *> *)branchesWithPrefix:(NSString *)prefix error:(NSError **)error;

ObjectiveGitTests/GTRepositorySpec.m

+52
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,58 @@
358358
});
359359
});
360360

361+
describe(@"move head", ^{
362+
beforeEach(^{
363+
repository = self.testAppFixtureRepository;
364+
});
365+
366+
//- (BOOL)moveHEADToReference:(GTReference *)reference error:(NSError **)error;
367+
it(@"should move to reference", ^{
368+
NSError *error = nil;
369+
GTReference *originalHead = [repository headReferenceWithError:NULL];
370+
371+
GTReference *targetReference = [repository lookUpReferenceWithName:@"refs/heads/other-branch" error:NULL];
372+
expect(targetReference).notTo(beNil());
373+
374+
// -> Test the move
375+
BOOL success = [repository moveHEADToReference:targetReference error:&error];
376+
expect(@(success)).to(beTruthy());
377+
expect(error).to(beNil());
378+
379+
// Verify
380+
GTReference *head = [repository headReferenceWithError:&error];
381+
expect(head).notTo(beNil());
382+
expect(head).notTo(equal(originalHead));
383+
expect(head.targetOID.SHA).to(equal(targetReference.targetOID.SHA));
384+
});
385+
386+
//- (BOOL)moveHEADToCommit:(GTCommit *)commit error:(NSError **)error;
387+
it(@"should move to commit", ^{
388+
NSError *error = nil;
389+
GTReference *originalHead = [repository headReferenceWithError:NULL];
390+
NSString *targetCommitSHA = @"f7ecd8f4404d3a388efbff6711f1bdf28ffd16a0";
391+
392+
GTCommit *commit = [repository lookUpObjectBySHA:targetCommitSHA error:NULL];
393+
expect(commit).notTo(beNil());
394+
395+
GTCommit *originalHeadCommit = [repository lookUpObjectByOID:originalHead.targetOID error:NULL];
396+
expect(originalHeadCommit).notTo(beNil());
397+
398+
// -> Test the move
399+
BOOL success = [repository moveHEADToCommit:commit error:&error];
400+
expect(@(success)).to(beTruthy());
401+
expect(error).to(beNil());
402+
403+
// Test for detached?
404+
405+
// Verify
406+
GTReference *head = [repository headReferenceWithError:&error];
407+
expect(head).notTo(beNil());
408+
expect(head.targetOID.SHA).to(equal(targetCommitSHA));
409+
});
410+
});
411+
412+
361413
describe(@"-checkout:strategy:error:progressBlock:", ^{
362414
it(@"should allow references", ^{
363415
NSError *error = nil;

0 commit comments

Comments
 (0)