diff --git a/ObjectiveGit/GTIndex.h b/ObjectiveGit/GTIndex.h index ba57ac2f3..d0df6e4ec 100644 --- a/ObjectiveGit/GTIndex.h +++ b/ObjectiveGit/GTIndex.h @@ -155,6 +155,13 @@ NS_ASSUME_NONNULL_BEGIN /// Returns whether reading the tree was successful. - (BOOL)addContentsOfTree:(GTTree *)tree error:(NSError **)error; +/// Add all the content of the working directory to the index. Like `git add -A` +/// +/// error - If not NULL, set to any error that occurs. +/// +/// Returns whether the operation was successful +- (BOOL)addAll:(NSError **)error; + /// Remove an entry (by relative path) from the index. /// Will fail if the receiver's repository is nil. /// diff --git a/ObjectiveGit/GTIndex.m b/ObjectiveGit/GTIndex.m index 863beb9cc..0acbf0c90 100644 --- a/ObjectiveGit/GTIndex.m +++ b/ObjectiveGit/GTIndex.m @@ -218,6 +218,16 @@ - (BOOL)addContentsOfTree:(GTTree *)tree error:(NSError **)error { return YES; } +- (BOOL)addAll:(NSError **)error { + int status = git_index_add_all(self.git_index, nil, GIT_INDEX_ADD_CHECK_PATHSPEC, nil, nil); + if (status != GIT_OK) { + if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add all the contents of the working tree to the index"]; + return NO; + } + + return YES; +} + - (BOOL)removeFile:(NSString *)file error:(NSError **)error { NSString *unicodeString = [self composedUnicodeStringWithString:file]; diff --git a/ObjectiveGitTests/GTIndexSpec.m b/ObjectiveGitTests/GTIndexSpec.m index 48b258e8a..1dddd2170 100644 --- a/ObjectiveGitTests/GTIndexSpec.m +++ b/ObjectiveGitTests/GTIndexSpec.m @@ -227,6 +227,33 @@ renamedFileURL = [NSURL fileURLWithPath:newPath isDirectory:NO]; }); + it(@"should add all files from the current file system to the index", ^{ + NSData *currentFileContent = [[NSFileManager defaultManager] contentsAtPath:fileURL.path]; + expect(currentFileContent).notTo(beNil()); + NSString *currentFileString = [[NSString alloc] initWithData:currentFileContent encoding:NSUTF8StringEncoding]; + currentFileString = [currentFileString stringByAppendingString:@"I would like to append this to the file"]; + currentFileContent = [currentFileString dataUsingEncoding:NSUTF8StringEncoding]; + expect(@([[NSFileManager defaultManager] createFileAtPath:fileURL.path contents:currentFileContent attributes:nil])).to(beTruthy()); + + NSString *newFileContent = @"This is a new file \n1 2 3"; + NSData *newFileData = [newFileContent dataUsingEncoding:NSUTF8StringEncoding]; + expect(newFileData).notTo(beNil()); + expect(@([[NSFileManager defaultManager] createFileAtPath:renamedFileURL.path contents:newFileData attributes:nil])).to(beTruthy()); + + GTIndexEntry *entry = [index entryWithPath:[filename decomposedStringWithCanonicalMapping]]; + expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeUnmodified, GTDeltaTypeModified))).to(beTruthy()); + entry = [index entryWithPath:[renamedFilename decomposedStringWithCanonicalMapping]]; + expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeUnmodified, GTDeltaTypeUntracked))).to(beTruthy()); + + expect(@([index addAll:NULL])).to(beTruthy()); + expect(@([index write:NULL])).to(beTruthy()); + + entry = [index entryWithPath:[filename decomposedStringWithCanonicalMapping]]; + expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeModified, GTDeltaTypeUnmodified))).to(beTruthy()); + entry = [index entryWithPath:[renamedFilename decomposedStringWithCanonicalMapping]]; + expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeAdded, GTDeltaTypeUnmodified))).to(beTruthy()); + }); + it(@"it preserves decomposed Unicode in index paths with precomposeunicode disabled", ^{ NSString *decomposedFilename = [filename decomposedStringWithCanonicalMapping]; GTIndexEntry *entry = [index entryWithPath:decomposedFilename error:NULL];