Skip to content

Commit 2279630

Browse files
authored
Merge pull request #628 from alehed/index_add_all
Wrap git_index_add_all
2 parents 0fc525b + 5aab68a commit 2279630

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

ObjectiveGit/GTIndex.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ NS_ASSUME_NONNULL_BEGIN
155155
/// Returns whether reading the tree was successful.
156156
- (BOOL)addContentsOfTree:(GTTree *)tree error:(NSError **)error;
157157

158+
/// Add all the content of the working directory to the index. Like `git add -A`
159+
///
160+
/// error - If not NULL, set to any error that occurs.
161+
///
162+
/// Returns whether the operation was successful
163+
- (BOOL)addAll:(NSError **)error;
164+
158165
/// Remove an entry (by relative path) from the index.
159166
/// Will fail if the receiver's repository is nil.
160167
///

ObjectiveGit/GTIndex.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ - (BOOL)addContentsOfTree:(GTTree *)tree error:(NSError **)error {
218218
return YES;
219219
}
220220

221+
- (BOOL)addAll:(NSError **)error {
222+
int status = git_index_add_all(self.git_index, nil, GIT_INDEX_ADD_CHECK_PATHSPEC, nil, nil);
223+
if (status != GIT_OK) {
224+
if (error != NULL) *error = [NSError git_errorFor:status description:@"Failed to add all the contents of the working tree to the index"];
225+
return NO;
226+
}
227+
228+
return YES;
229+
}
230+
221231
- (BOOL)removeFile:(NSString *)file error:(NSError **)error {
222232
NSString *unicodeString = [self composedUnicodeStringWithString:file];
223233

ObjectiveGitTests/GTIndexSpec.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,33 @@
227227
renamedFileURL = [NSURL fileURLWithPath:newPath isDirectory:NO];
228228
});
229229

230+
it(@"should add all files from the current file system to the index", ^{
231+
NSData *currentFileContent = [[NSFileManager defaultManager] contentsAtPath:fileURL.path];
232+
expect(currentFileContent).notTo(beNil());
233+
NSString *currentFileString = [[NSString alloc] initWithData:currentFileContent encoding:NSUTF8StringEncoding];
234+
currentFileString = [currentFileString stringByAppendingString:@"I would like to append this to the file"];
235+
currentFileContent = [currentFileString dataUsingEncoding:NSUTF8StringEncoding];
236+
expect(@([[NSFileManager defaultManager] createFileAtPath:fileURL.path contents:currentFileContent attributes:nil])).to(beTruthy());
237+
238+
NSString *newFileContent = @"This is a new file \n1 2 3";
239+
NSData *newFileData = [newFileContent dataUsingEncoding:NSUTF8StringEncoding];
240+
expect(newFileData).notTo(beNil());
241+
expect(@([[NSFileManager defaultManager] createFileAtPath:renamedFileURL.path contents:newFileData attributes:nil])).to(beTruthy());
242+
243+
GTIndexEntry *entry = [index entryWithPath:[filename decomposedStringWithCanonicalMapping]];
244+
expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeUnmodified, GTDeltaTypeModified))).to(beTruthy());
245+
entry = [index entryWithPath:[renamedFilename decomposedStringWithCanonicalMapping]];
246+
expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeUnmodified, GTDeltaTypeUntracked))).to(beTruthy());
247+
248+
expect(@([index addAll:NULL])).to(beTruthy());
249+
expect(@([index write:NULL])).to(beTruthy());
250+
251+
entry = [index entryWithPath:[filename decomposedStringWithCanonicalMapping]];
252+
expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeModified, GTDeltaTypeUnmodified))).to(beTruthy());
253+
entry = [index entryWithPath:[renamedFilename decomposedStringWithCanonicalMapping]];
254+
expect(@(fileStatusEqualsExpected(entry.path, GTDeltaTypeAdded, GTDeltaTypeUnmodified))).to(beTruthy());
255+
});
256+
230257
it(@"it preserves decomposed Unicode in index paths with precomposeunicode disabled", ^{
231258
NSString *decomposedFilename = [filename decomposedStringWithCanonicalMapping];
232259
GTIndexEntry *entry = [index entryWithPath:decomposedFilename error:NULL];

0 commit comments

Comments
 (0)