Skip to content

Added -resetPathspecs:... #351

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 8 commits into from
Apr 7, 2014
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
38 changes: 38 additions & 0 deletions Classes/GTRepository+Reset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// GTRepository+Reset.h
// ObjectiveGitFramework
//
// Created by Josh Abernathy on 4/4/14.
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
//

#import "GTRepository.h"

/// The reset types. See the libgit2 documentation for more info.
typedef enum {
GTRepositoryResetTypeSoft = GIT_RESET_SOFT,
GTRepositoryResetTypeMixed = GIT_RESET_MIXED,
GTRepositoryResetTypeHard = GIT_RESET_HARD,
} GTRepositoryResetType;

@interface GTRepository (Reset)

/// Reset the repository's HEAD to the given commit.
///
/// commit - The commit the HEAD is to be reset to. Must not be nil.
/// resetType - The type of reset to be used.
/// error - The error if one occurred.
///
/// Returns whether the reset was succcessful.
- (BOOL)resetToCommit:(GTCommit *)commit resetType:(GTRepositoryResetType)resetType error:(NSError **)error;

/// Resets the given pathspecs in the index to the tree entries from the commit.
///
/// pathspecs - The pathspecs to reset. Cannot be nil.
/// commit - The commit whose tree should be used to reset. Cannot be nil.
/// error - The error if one occurred.
///
/// Returns whether the reset was successful.
- (BOOL)resetPathspecs:(NSArray *)pathspecs toCommit:(GTCommit *)commit error:(NSError **)error;

@end
49 changes: 49 additions & 0 deletions Classes/GTRepository+Reset.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// GTRepository+Reset.m
// ObjectiveGitFramework
//
// Created by Josh Abernathy on 4/4/14.
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
//

#import "GTRepository+Reset.h"
#import "GTCommit.h"
#import "NSArray+StringArray.h"
#import "NSError+Git.h"
#import "GTSignature.h"

@implementation GTRepository (Reset)

- (BOOL)resetToCommit:(GTCommit *)commit resetType:(GTRepositoryResetType)resetType error:(NSError **)error {
NSParameterAssert(commit != nil);

int gitError = git_reset(self.git_repository, commit.git_object, (git_reset_t)resetType, (git_signature *)[self userSignatureForNow].git_signature, NULL);
if (gitError != GIT_OK) {
if (error != NULL) {
*error = [NSError git_errorFor:gitError description:@"Failed to reset repository to commit %@.", commit.SHA];
}

return NO;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Weird indentation afoot.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're afoot.


return YES;
}

- (BOOL)resetPathspecs:(NSArray *)paths toCommit:(GTCommit *)commit error:(NSError **)error {
NSParameterAssert(paths != nil);
NSParameterAssert(commit != nil);

git_strarray array = paths.git_strarray;
int gitError = git_reset_default(self.git_repository, commit.git_object, &array);
if (gitError != GIT_OK) {
if (error != NULL) {
*error = [NSError git_errorFor:gitError description:@"Failed resetting paths (%@) to %@", paths, commit];
}

return NO;
}

return YES;
}

@end
15 changes: 0 additions & 15 deletions Classes/GTRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@
@class GTTag;
@class GTTree;

typedef enum {
GTRepositoryResetTypeSoft = GIT_RESET_SOFT,
GTRepositoryResetTypeMixed = GIT_RESET_MIXED,
GTRepositoryResetTypeHard = GIT_RESET_HARD
} GTRepositoryResetType;

// Checkout strategies used by the various -checkout... methods
// See git_checkout_strategy_t
typedef enum {
Expand Down Expand Up @@ -277,15 +271,6 @@ extern NSString *const GTRepositoryCloneOptionsCredentialProvider;
// returns the local commits, an empty array if there is no remote branch, or nil if an error occurred
- (NSArray *)localCommitsRelativeToRemoteBranch:(GTBranch *)remoteBranch error:(NSError **)error;

// Reset the repository's HEAD to the given commit.
//
// commit - the commit the HEAD is to be reset to. Must not be nil.
// resetType - The type of reset to be used.
// error(out) - in the event of an error this may be set.
//
// Returns `YES` if successful, `NO` if not.
- (BOOL)resetToCommit:(GTCommit *)commit withResetType:(GTRepositoryResetType)resetType error:(NSError **)error;

// Retrieves git's "prepared message" for the next commit, like the default
// message pre-filled when committing after a conflicting merge.
//
Expand Down
11 changes: 0 additions & 11 deletions Classes/GTRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -526,17 +526,6 @@ - (BOOL)isHEADUnborn {
return (BOOL)git_repository_head_unborn(self.git_repository);
}

- (BOOL)resetToCommit:(GTCommit *)commit withResetType:(GTRepositoryResetType)resetType error:(NSError **)error {
NSParameterAssert(commit != nil);

int result = git_reset(self.git_repository, commit.git_object, (git_reset_t)resetType, (git_signature *)[self userSignatureForNow].git_signature, NULL);
if (result == GIT_OK) return YES;

if (error != NULL) *error = [NSError git_errorFor:result description:@"Failed to reset repository to commit %@.", commit.SHA];

return NO;
}

- (NSString *)preparedMessageWithError:(NSError **)error {
void (^setErrorFromCode)(int) = ^(int errorCode) {
if (errorCode == 0 || errorCode == GIT_ENOTFOUND) {
Expand Down
1 change: 1 addition & 0 deletions Classes/ObjectiveGit.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import <ObjectiveGit/GTRepository+Stashing.h>
#import <ObjectiveGit/GTRepository+Committing.h>
#import <ObjectiveGit/GTRepository+Status.h>
#import <ObjectiveGit/GTRepository+Reset.h>
#import <ObjectiveGit/GTEnumerator.h>
#import <ObjectiveGit/GTCommit.h>
#import <ObjectiveGit/GTCredential.h>
Expand Down
16 changes: 16 additions & 0 deletions ObjectiveGitFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
8821547F17147B3600D76B76 /* GTOID.m in Sources */ = {isa = PBXBuildFile; fileRef = 8821547C17147B3600D76B76 /* GTOID.m */; };
8821548017147B3600D76B76 /* GTOID.m in Sources */ = {isa = PBXBuildFile; fileRef = 8821547C17147B3600D76B76 /* GTOID.m */; };
88215483171499BE00D76B76 /* GTReflogSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88215482171499BE00D76B76 /* GTReflogSpec.m */; };
88234B2618F2FE260039972E /* GTRepositoryResetSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88234B2518F2FE260039972E /* GTRepositoryResetSpec.m */; };
8832811F173D8816006D7DCF /* GTIndexSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 8832811E173D8816006D7DCF /* GTIndexSpec.m */; };
88328128173D8A64006D7DCF /* GTTreeSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88328127173D8A64006D7DCF /* GTTreeSpec.m */; };
883CD6AB1600EBC600F57354 /* GTRemote.h in Headers */ = {isa = PBXBuildFile; fileRef = 883CD6A91600EBC600F57354 /* GTRemote.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -212,6 +213,10 @@
88948AC91779243600809CDA /* GTObjectDatabaseSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88948AC81779243600809CDA /* GTObjectDatabaseSpec.m */; };
88A994BA16FCE7D400402C7B /* GTBranchSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88A994B916FCE7D400402C7B /* GTBranchSpec.m */; };
88A994CB16FCED1D00402C7B /* GTTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 88A994CA16FCED1D00402C7B /* GTTestCase.m */; };
88BC0E5018EF4F3600C7D0E6 /* GTRepository+Reset.h in Headers */ = {isa = PBXBuildFile; fileRef = 88BC0E4E18EF4F3600C7D0E6 /* GTRepository+Reset.h */; settings = {ATTRIBUTES = (Public, ); }; };
88BC0E5118EF4F3600C7D0E6 /* 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 */; };
88BC0E5318EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */ = {isa = PBXBuildFile; fileRef = 88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */; };
88C0BC5917038CF3009E99AA /* GTConfigurationSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 88C0BC5817038CF3009E99AA /* GTConfigurationSpec.m */; };
88EB7E4D14AEBA600046FEA4 /* GTConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
88EB7E4E14AEBA600046FEA4 /* GTConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */; };
Expand Down Expand Up @@ -430,6 +435,7 @@
8821547B17147B3600D76B76 /* GTOID.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTOID.h; sourceTree = "<group>"; };
8821547C17147B3600D76B76 /* GTOID.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTOID.m; sourceTree = "<group>"; };
88215482171499BE00D76B76 /* GTReflogSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTReflogSpec.m; sourceTree = "<group>"; };
88234B2518F2FE260039972E /* GTRepositoryResetSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTRepositoryResetSpec.m; sourceTree = "<group>"; };
8832811E173D8816006D7DCF /* GTIndexSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTIndexSpec.m; sourceTree = "<group>"; };
88328127173D8A64006D7DCF /* GTTreeSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTTreeSpec.m; sourceTree = "<group>"; };
883CD6A91600EBC600F57354 /* GTRemote.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTRemote.h; sourceTree = "<group>"; };
Expand All @@ -447,6 +453,8 @@
88A994B916FCE7D400402C7B /* GTBranchSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTBranchSpec.m; sourceTree = "<group>"; };
88A994C916FCED1D00402C7B /* GTTestCase.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTTestCase.h; sourceTree = "<group>"; };
88A994CA16FCED1D00402C7B /* GTTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTTestCase.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>"; };
88EB7E4B14AEBA600046FEA4 /* GTConfiguration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTConfiguration.h; sourceTree = "<group>"; };
88EB7E4C14AEBA600046FEA4 /* GTConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTConfiguration.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -733,6 +741,7 @@
D015F7D417F6965400AD5E1F /* GTRepositoryStashingSpec.m */,
886E623618AECD86000611A0 /* GTFilterSpec.m */,
D0751CD818BE520400134314 /* GTFilterListSpec.m */,
88234B2518F2FE260039972E /* GTRepositoryResetSpec.m */,
);
path = ObjectiveGitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -763,6 +772,8 @@
BDE4C063130EFE2C00851650 /* GTRepository.m */,
30DCBA6117B45A78009B0EBD /* GTRepository+Status.h */,
30DCBA6217B45A78009B0EBD /* GTRepository+Status.m */,
88BC0E4E18EF4F3600C7D0E6 /* GTRepository+Reset.h */,
88BC0E4F18EF4F3600C7D0E6 /* GTRepository+Reset.m */,
30DCBA5A17B45213009B0EBD /* GTStatusDelta.h */,
30DCBA5B17B45213009B0EBD /* GTStatusDelta.m */,
D015F7C817F695E800AD5E1F /* GTRepository+Stashing.h */,
Expand Down Expand Up @@ -935,6 +946,7 @@
886E622B18AEBF75000611A0 /* GTFilterSource.h in Headers */,
306123B217EA5261006591D4 /* metamacros.h in Headers */,
3E0A23E5159E0FDB00A6068F /* GTObjectDatabase.h in Headers */,
88BC0E5118EF4F3600C7D0E6 /* GTRepository+Reset.h in Headers */,
04DB464E133AB5E200D9C624 /* NSError+Git.h in Headers */,
55C8057F13875C62004DCB0F /* NSData+Git.h in Headers */,
55C8058013875C6E004DCB0F /* NSString+Git.h in Headers */,
Expand Down Expand Up @@ -998,6 +1010,7 @@
BDD627991318391200DE34D1 /* GTBlob.h in Headers */,
886E622A18AEBF75000611A0 /* GTFilterSource.h in Headers */,
BDD62924131C03D600DE34D1 /* GTTag.h in Headers */,
88BC0E5018EF4F3600C7D0E6 /* GTRepository+Reset.h in Headers */,
BDFAF9C3131C1845000508BC /* GTIndex.h in Headers */,
BDFAF9C9131C1868000508BC /* GTIndexEntry.h in Headers */,
BD441E08131ED0C300187010 /* GTReference.h in Headers */,
Expand Down Expand Up @@ -1253,6 +1266,7 @@
3011D87A1668F29600CE3409 /* GTDiffDelta.m in Sources */,
DD3D951E182AB3BD004AF532 /* GTBlame.m in Sources */,
880EE66418AE700500B82455 /* GTFilter.m in Sources */,
88BC0E5318EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */,
30FDC08216835A8100654BF0 /* GTDiffLine.m in Sources */,
30DCBA6017B45213009B0EBD /* GTStatusDelta.m in Sources */,
30B1E7F11703522100D0814D /* NSDate+GTTimeAdditions.m in Sources */,
Expand Down Expand Up @@ -1298,6 +1312,7 @@
8832811F173D8816006D7DCF /* GTIndexSpec.m in Sources */,
D0F4E28A17C7F24200BBDE30 /* NSErrorGitSpec.m in Sources */,
88328128173D8A64006D7DCF /* GTTreeSpec.m in Sources */,
88234B2618F2FE260039972E /* GTRepositoryResetSpec.m in Sources */,
5BE612931745EEBC00266D8C /* GTTreeBuilderSpec.m in Sources */,
D06D9E011755D10000558C17 /* GTEnumeratorSpec.m in Sources */,
D03B7C411756AB370034A610 /* GTSubmoduleSpec.m in Sources */,
Expand Down Expand Up @@ -1329,6 +1344,7 @@
BDD62925131C03D600DE34D1 /* GTTag.m in Sources */,
BDFAF9C4131C1845000508BC /* GTIndex.m in Sources */,
BDFAF9CA131C1868000508BC /* GTIndexEntry.m in Sources */,
88BC0E5218EF4F3600C7D0E6 /* GTRepository+Reset.m in Sources */,
BD441E09131ED0C300187010 /* GTReference.m in Sources */,
88F50F5A132054D800584FBE /* GTBranch.m in Sources */,
D0CE552318B6C58F008EB8E0 /* GTFilterList.m in Sources */,
Expand Down
89 changes: 89 additions & 0 deletions ObjectiveGitTests/GTRepositoryResetSpec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// GTRepositoryResetSpec.m
// ObjectiveGitFramework
//
// Created by Josh Abernathy on 4/7/14.
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
//

#import "GTRepository+Reset.h"
#import "GTRepository.h"
#import "GTIndex.h"

SpecBegin(GTRepositoryReset)

__block GTRepository *repository;

describe(@"-resetPathspecs:toCommit:error:", ^{
__block NSUInteger (^countStagedFiles)(void);

beforeEach(^{
repository = [self testAppFixtureRepository];

countStagedFiles = ^{
__block NSUInteger count = 0;
[repository enumerateFileStatusWithOptions:nil error:NULL usingBlock:^(GTStatusDelta *headToIndex, GTStatusDelta *indexToWorkingDirectory, BOOL *stop) {
if (headToIndex.status != GTStatusDeltaStatusUnmodified) count++;
}];

return count;
};
});

it(@"should reset the path's index entry", ^{
static NSString * const fileName = @"README.md";
NSURL *fileURL = [repository.fileURL URLByAppendingPathComponent:fileName];
BOOL success = [@"blahahaha" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:NULL];
expect(success).to.beTruthy();

GTIndex *index = [repository indexWithError:NULL];
expect(index).notTo.beNil();

success = [index addFile:fileName error:NULL];
expect(success).to.beTruthy();

expect(countStagedFiles()).to.equal(1);

GTCommit *HEAD = [repository lookUpObjectByRevParse:@"HEAD" error:NULL];
expect(HEAD).notTo.beNil();

success = [repository resetPathspecs:@[ fileName ] toCommit:HEAD error:NULL];
expect(success).to.beTruthy();

expect(countStagedFiles()).to.equal(0);
});
});

describe(@"-resetToCommit:resetType:error:", ^{
beforeEach(^{
repository = [self bareFixtureRepository];
});

it(@"should move HEAD when used", ^{
NSError *error = nil;
GTReference *originalHead = [repository headReferenceWithError:NULL];
NSString *resetTargetSHA = @"8496071c1b46c854b31185ea97743be6a8774479";

GTCommit *commit = [repository lookUpObjectBySHA:resetTargetSHA error:NULL];
expect(commit).notTo.beNil();
GTCommit *originalHeadCommit = [repository lookUpObjectBySHA:originalHead.targetSHA error:NULL];
expect(originalHeadCommit).notTo.beNil();

BOOL success = [repository resetToCommit:commit resetType:GTRepositoryResetTypeSoft error:&error];
expect(success).to.beTruthy();
expect(error).to.beNil();

GTReference *head = [repository headReferenceWithError:&error];
expect(head).notTo.beNil();
expect(head.targetSHA).to.equal(resetTargetSHA);

success = [repository resetToCommit:originalHeadCommit resetType:GTRepositoryResetTypeSoft error:&error];
expect(success).to.beTruthy();
expect(error).to.beNil();

head = [repository headReferenceWithError:&error];
expect(head.targetSHA).to.equal(originalHead.targetSHA);
});
});

SpecEnd
32 changes: 0 additions & 32 deletions ObjectiveGitTests/GTRepositorySpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -327,38 +327,6 @@
});
});

describe(@"-resetToCommit:withResetType:error:", ^{
beforeEach(^{
repository = self.bareFixtureRepository;
});

it(@"should move HEAD when used", ^{
NSError *error = nil;
GTReference *originalHead = [repository headReferenceWithError:NULL];
NSString *resetTargetSHA = @"8496071c1b46c854b31185ea97743be6a8774479";

GTCommit *commit = [repository lookUpObjectBySHA:resetTargetSHA error:NULL];
expect(commit).notTo.beNil();
GTCommit *originalHeadCommit = [repository lookUpObjectBySHA:originalHead.targetSHA error:NULL];
expect(originalHeadCommit).notTo.beNil();

BOOL success = [repository resetToCommit:commit withResetType:GTRepositoryResetTypeSoft error:&error];
expect(success).to.beTruthy();
expect(error).to.beNil();

GTReference *head = [repository headReferenceWithError:&error];
expect(head).notTo.beNil();
expect(head.targetSHA).to.equal(resetTargetSHA);

success = [repository resetToCommit:originalHeadCommit withResetType:GTRepositoryResetTypeSoft error:&error];
expect(success).to.beTruthy();
expect(error).to.beNil();

head = [repository headReferenceWithError:&error];
expect(head.targetSHA).to.equal(originalHead.targetSHA);
});
});

describe(@"-lookUpBranchWithName:type:error:", ^{
it(@"should look up a local branch", ^{
NSError *error = nil;
Expand Down
2 changes: 1 addition & 1 deletion ObjectiveGitTests/GTSubmoduleSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@

GTCommit *newHEAD = (id)[submoduleRepo lookUpObjectBySHA:@"82dc47f6ba3beecab33080a1136d8913098e1801" objectType:GTObjectTypeCommit error:NULL];
expect(newHEAD).notTo.beNil();
expect([submoduleRepo resetToCommit:newHEAD withResetType:GTRepositoryResetTypeHard error:NULL]).to.beTruthy();
expect([submoduleRepo resetToCommit:newHEAD resetType:GTRepositoryResetTypeHard error:NULL]).to.beTruthy();

expect(submodule.workingDirectoryOID.SHA).notTo.equal(newHEAD.SHA);

Expand Down