Skip to content

Commit 683f6de

Browse files
committed
Revert "Faster multiple file staging (#629)"
3f5abaf
1 parent 58fb0f9 commit 683f6de

File tree

9 files changed

+39
-211
lines changed

9 files changed

+39
-211
lines changed

GitUpKit/Core/GCDiff-Tests.m

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,6 @@ - (void)testUnifiedDiff {
165165
XCTAssertTrue([self.repository addFileToIndex:@"renamed1.txt" error:NULL]);
166166
[self updateFileAtPath:@"type-changed.txt" withString:@""];
167167
XCTAssertTrue([self.repository addFileToIndex:@"type-changed.txt" error:NULL]);
168-
169-
NSArray* files = @[ @".gitignore", @"modified.txt", @"deleted.txt", @"renamed1.txt", @"type-changed.txt" ];
170-
171-
// Test adding and removing multiple files
172-
XCTAssertTrue([self.repository removeFilesFromIndex:files error:NULL]);
173-
XCTAssertEqual([self.repository checkIndexStatus:NULL].deltas.count, 0);
174-
175-
XCTAssertTrue([self.repository addFilesToIndex:files error:NULL]);
176-
XCTAssertEqual([self.repository checkIndexStatus:NULL].deltas.count, 5);
177-
178168
XCTAssertNotNil([self.repository createCommitFromHEADWithMessage:@"Update" error:NULL]);
179169

180170
// Touch files
@@ -189,8 +179,8 @@ - (void)testUnifiedDiff {
189179

190180
// Stage some files
191181
XCTAssertTrue([self.repository addFileToIndex:@"modified.txt" error:NULL]);
192-
files = @[ @"deleted.txt", @"renamed1.txt" ];
193-
XCTAssertTrue([self.repository removeFilesFromIndex:files error:NULL]);
182+
XCTAssertTrue([self.repository removeFileFromIndex:@"deleted.txt" error:NULL]);
183+
XCTAssertTrue([self.repository removeFileFromIndex:@"renamed1.txt" error:NULL]);
194184
XCTAssertTrue([self.repository addFileToIndex:@"renamed2.txt" error:NULL]);
195185
XCTAssertTrue([self.repository addFileToIndex:@"added.txt" error:NULL]);
196186

GitUpKit/Core/GCIndex.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ typedef BOOL (^GCIndexLineFilter)(GCLineDiffChange change, NSUInteger oldLineNum
6666
- (BOOL)resetLinesInFile:(NSString*)path index:(GCIndex*)index toCommit:(GCCommit*)commit error:(NSError**)error usingFilter:(GCIndexLineFilter)filter;
6767

6868
- (BOOL)checkoutFileToWorkingDirectory:(NSString*)path fromIndex:(GCIndex*)index error:(NSError**)error;
69-
- (BOOL)checkoutFilesToWorkingDirectory:(NSArray<NSString*>*)paths fromIndex:(GCIndex*)index error:(NSError**)error;
7069
- (BOOL)checkoutLinesInFileToWorkingDirectory:(NSString*)path fromIndex:(GCIndex*)index error:(NSError**)error usingFilter:(GCIndexLineFilter)filter;
7170

7271
- (BOOL)clearConflictForFile:(NSString*)path inIndex:(GCIndex*)index error:(NSError**)error;

GitUpKit/Core/GCIndex.m

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -485,24 +485,12 @@ - (BOOL)resetLinesInFile:(NSString*)path index:(GCIndex*)index toCommit:(GCCommi
485485
}
486486

487487
- (BOOL)checkoutFileToWorkingDirectory:(NSString*)path fromIndex:(GCIndex*)index error:(NSError**)error {
488-
return [self checkoutFilesToWorkingDirectory:@[ path ]
489-
fromIndex:index
490-
error:error];
491-
}
492-
493-
- (BOOL)checkoutFilesToWorkingDirectory:(NSArray<NSString*>*)paths fromIndex:(GCIndex*)index error:(NSError**)error {
494488
git_checkout_options options = GIT_CHECKOUT_OPTIONS_INIT;
495489
options.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH; // There's no reason to update the index
496-
options.paths.count = paths.count;
497-
char** pathStrings = malloc(paths.count * sizeof(char*));
498-
options.paths.strings = pathStrings;
499-
for (NSUInteger i = 0; i < paths.count; i++) {
500-
const char* filePath = GCGitPathFromFileSystemPath(paths[i]);
501-
options.paths.strings[i] = (char*)filePath;
502-
}
503-
490+
options.paths.count = 1;
491+
const char* filePath = GCGitPathFromFileSystemPath(path);
492+
options.paths.strings = (char**)&filePath;
504493
CALL_LIBGIT2_FUNCTION_RETURN(NO, git_checkout_index, self.private, index.private, &options);
505-
free(pathStrings);
506494
return YES;
507495
}
508496

GitUpKit/Extensions/GCRepository+Index-Tests.m

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,9 @@ - (void)testIndex {
6969
XCTAssertTrue([self.repository addFileToIndex:@"test.txt" error:NULL]);
7070
[self assertGitCLTOutputEqualsString:@"A test.txt\n" withRepository:self.repository command:@"status", @"--ignored", @"--porcelain", nil];
7171

72-
// Add multiple files to working directory
73-
NSMutableArray* filePaths = [[NSMutableArray alloc] init];
74-
NSString* expectedGitCLTOutput = [[NSString alloc] init];
75-
for (int i = 0; i < 50; i++) {
76-
NSString* filePath = [NSString stringWithFormat:@"hello_world%02d.txt", i];
77-
[self updateFileAtPath:filePath withString:@"Bonjour le monde!\n"];
78-
[filePaths addObject:filePath];
79-
expectedGitCLTOutput = [expectedGitCLTOutput stringByAppendingFormat:@"A %@\n", filePath];
80-
}
81-
expectedGitCLTOutput = [expectedGitCLTOutput stringByAppendingString:@"A test.txt\n"];
82-
83-
// Add multiple files to index
84-
XCTAssertTrue([self.repository addFilesToIndex:filePaths error:NULL]);
85-
[self assertGitCLTOutputEqualsString:expectedGitCLTOutput withRepository:self.repository command:@"status", @"--ignored", @"--porcelain", nil];
86-
87-
// Add remove multiple files from index
88-
XCTAssertTrue([self.repository removeFilesFromIndex:filePaths error:NULL]);
89-
expectedGitCLTOutput = [expectedGitCLTOutput stringByReplacingOccurrencesOfString:@"A test.txt\n" withString:@""];
90-
expectedGitCLTOutput = [expectedGitCLTOutput stringByReplacingOccurrencesOfString:@"A hello_world" withString:@"?? hello_world"];
91-
expectedGitCLTOutput = [@"A test.txt\n" stringByAppendingString:expectedGitCLTOutput];
92-
[self assertGitCLTOutputEqualsString:expectedGitCLTOutput withRepository:self.repository command:@"status", @"--ignored", @"--porcelain", nil];
93-
9472
// Reset index
9573
XCTAssertTrue([self.repository resetIndexToHEAD:NULL]);
96-
expectedGitCLTOutput = [expectedGitCLTOutput stringByReplacingOccurrencesOfString:@"A test.txt\n" withString:@""];
97-
expectedGitCLTOutput = [expectedGitCLTOutput stringByAppendingString:@"?? test.txt\n"];
98-
[self assertGitCLTOutputEqualsString:expectedGitCLTOutput withRepository:self.repository command:@"status", @"--ignored", @"--porcelain", nil];
74+
[self assertGitCLTOutputEqualsString:@"?? test.txt\n" withRepository:self.repository command:@"status", @"--ignored", @"--porcelain", nil];
9975
}
10076

10177
- (void)testIndex_Lines {

GitUpKit/Extensions/GCRepository+Index.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@
1919
- (BOOL)resetIndexToHEAD:(NSError**)error; // Like git reset --mixed HEAD but does not update reflog
2020

2121
- (BOOL)removeFileFromIndex:(NSString*)path error:(NSError**)error; // git rm --cached {file} - Delete file from index
22-
- (BOOL)removeFilesFromIndex:(NSArray<NSString*>*)paths error:(NSError**)error; // git rm --cached {file} - Delete files from index
2322

2423
- (BOOL)addFileToIndex:(NSString*)path error:(NSError**)error; // git add {file} - Copy file from workdir to index (aka stage file)
25-
- (BOOL)addFilesToIndex:(NSArray<NSString*>*)paths error:(NSError**)error;
2624
- (BOOL)resetFileInIndexToHEAD:(NSString*)path error:(NSError**)error; // git reset --mixed {file} - Copy file from HEAD to index (aka unstage file)
27-
- (BOOL)resetFilesInIndexToHEAD:(NSArray<NSString*>*)paths error:(NSError**)error;
2825
- (BOOL)checkoutFileFromIndex:(NSString*)path error:(NSError**)error; // git checkout {file} - Copy file from index to workdir (aka discard file)
29-
- (BOOL)checkoutFilesFromIndex:(NSArray<NSString*>*)paths error:(NSError**)error;
3026

3127
- (BOOL)addLinesFromFileToIndex:(NSString*)path error:(NSError**)error usingFilter:(GCIndexLineFilter)filter; // git add -p {file} - Copy only some lines of file from workdir to index (aka stage lines)
3228
- (BOOL)resetLinesFromFileInIndexToHEAD:(NSString*)path error:(NSError**)error usingFilter:(GCIndexLineFilter)filter; // git reset -p {file} - Copy only some lines of file from HEAD to index (aka unstage lines)

GitUpKit/Extensions/GCRepository+Index.m

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -39,61 +39,22 @@ - (BOOL)resetIndexToHEAD:(NSError**)error {
3939
}
4040

4141
- (BOOL)removeFileFromIndex:(NSString*)path error:(NSError**)error {
42-
return [self removeFilesFromIndex:@[ path ] error:error];
43-
}
44-
45-
- (BOOL)removeFilesFromIndex:(NSArray<NSString*>*)paths error:(NSError**)error {
4642
GCIndex* index = [self readRepositoryIndex:error];
4743
if (index == nil) {
4844
return NO;
4945
}
50-
51-
for (NSString* path in paths) {
52-
if (![self removeFile:path fromIndex:index error:error] || (error && *error != nil)) {
53-
[self writeRepositoryIndex:index error:error];
54-
return NO;
55-
}
56-
}
57-
58-
return [self writeRepositoryIndex:index error:error];
46+
return [self removeFile:path fromIndex:index error:error] && [self writeRepositoryIndex:index error:error];
5947
}
6048

6149
- (BOOL)addFileToIndex:(NSString*)path error:(NSError**)error {
62-
return [self addFilesToIndex:@[ path ] error:error];
63-
}
64-
65-
- (BOOL)addFilesToIndex:(NSArray<NSString*>*)paths error:(NSError**)error {
6650
GCIndex* index = [self readRepositoryIndex:error];
6751
if (index == nil) {
6852
return NO;
6953
}
70-
71-
BOOL failed = NO;
72-
BOOL shouldWriteRepository = NO;
73-
for (NSString* path in paths) {
74-
if (![self addFileInWorkingDirectory:path toIndex:index error:error] || (error && *error != nil)) {
75-
failed = YES;
76-
break;
77-
}
78-
79-
shouldWriteRepository = YES;
80-
}
81-
82-
if (failed && shouldWriteRepository) {
83-
if (shouldWriteRepository) {
84-
[self writeRepositoryIndex:index error:NULL];
85-
}
86-
return NO;
87-
}
88-
89-
return [self writeRepositoryIndex:index error:error];
54+
return [self addFileInWorkingDirectory:path toIndex:index error:error] && [self writeRepositoryIndex:index error:error];
9055
}
9156

9257
- (BOOL)resetFileInIndexToHEAD:(NSString*)path error:(NSError**)error {
93-
return [self resetFilesInIndexToHEAD:@[ path ] error:error];
94-
}
95-
96-
- (BOOL)resetFilesInIndexToHEAD:(NSArray<NSString*>*)paths error:(NSError**)error {
9758
GCIndex* index = [self readRepositoryIndex:error];
9859
if (index == nil) {
9960
return NO;
@@ -102,34 +63,24 @@ - (BOOL)resetFilesInIndexToHEAD:(NSArray<NSString*>*)paths error:(NSError**)erro
10263
if (![self lookupHEADCurrentCommit:&headCommit branch:NULL error:error]) {
10364
return NO;
10465
}
105-
106-
for (NSString* path in paths) {
107-
if (headCommit) {
108-
if (![self resetFile:path inIndex:index toCommit:headCommit error:error]) {
109-
[self writeRepositoryIndex:index error:error];
110-
return NO;
111-
}
112-
} else {
113-
if (![self removeFile:path fromIndex:index error:error]) {
114-
[self writeRepositoryIndex:index error:error];
115-
return NO;
116-
}
66+
if (headCommit) {
67+
if (![self resetFile:path inIndex:index toCommit:headCommit error:error]) {
68+
return NO;
69+
}
70+
} else {
71+
if (![self removeFile:path fromIndex:index error:error]) {
72+
return NO;
11773
}
11874
}
119-
12075
return [self writeRepositoryIndex:index error:error];
12176
}
12277

12378
- (BOOL)checkoutFileFromIndex:(NSString*)path error:(NSError**)error {
124-
return [self checkoutFilesFromIndex:@[ path ] error:error];
125-
}
126-
127-
- (BOOL)checkoutFilesFromIndex:(NSArray<NSString*>*)paths error:(NSError**)error {
12879
GCIndex* index = [self readRepositoryIndex:error];
12980
if (index == nil) {
13081
return NO;
13182
}
132-
return [self checkoutFilesToWorkingDirectory:paths fromIndex:index error:error];
83+
return [self checkoutFileToWorkingDirectory:path fromIndex:index error:error];
13384
}
13485

13586
- (BOOL)addLinesFromFileToIndex:(NSString*)path error:(NSError**)error usingFilter:(GCIndexLineFilter)filter {

GitUpKit/Utilities/GIViewController+Utilities.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@
3333
- (void)discardSubmoduleAtPath:(NSString*)path resetIndex:(BOOL)resetIndex; // Prompts user
3434

3535
- (void)stageAllChangesForFile:(NSString*)path;
36-
- (void)stageAllChangesForFiles:(NSArray<NSString*>*)paths;
3736
- (void)stageSelectedChangesForFile:(NSString*)path oldLines:(NSIndexSet*)oldLines newLines:(NSIndexSet*)newLines;
3837
- (void)unstageAllChangesForFile:(NSString*)path;
39-
- (void)unstageAllChangesForFiles:(NSArray<NSString*>*)paths;
4038
- (void)unstageSelectedChangesForFile:(NSString*)path oldLines:(NSIndexSet*)oldLines newLines:(NSIndexSet*)newLines;
4139

4240
- (BOOL)discardAllChangesForFile:(NSString*)path resetIndex:(BOOL)resetIndex error:(NSError**)error;
43-
- (BOOL)discardAllChangesForFiles:(NSArray<NSString*>*)paths resetIndex:(BOOL)resetIndex error:(NSError**)error;
4441
- (void)discardAllChangesForFile:(NSString*)path resetIndex:(BOOL)resetIndex; // Prompts user
4542
- (BOOL)discardSelectedChangesForFile:(NSString*)path oldLines:(NSIndexSet*)oldLines newLines:(NSIndexSet*)newLines resetIndex:(BOOL)resetIndex error:(NSError**)error;
4643
- (void)discardSelectedChangesForFile:(NSString*)path oldLines:(NSIndexSet*)oldLines newLines:(NSIndexSet*)newLines resetIndex:(BOOL)resetIndex; // Prompts user

GitUpKit/Utilities/GIViewController+Utilities.m

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,13 @@ - (void)discardSubmoduleAtPath:(NSString*)path resetIndex:(BOOL)resetIndex {
108108
}
109109

110110
- (void)stageAllChangesForFile:(NSString*)path {
111-
return [self stageAllChangesForFiles:@[ path ]];
112-
}
113-
114-
- (void)stageAllChangesForFiles:(NSArray<NSString*>*)paths {
115111
NSError* error;
116-
NSMutableArray* existingFiles = [NSMutableArray array];
117-
NSMutableArray* nonExistingFiles = [NSMutableArray array];
118-
for (NSString* path in paths) {
119-
if ([[NSFileManager defaultManager] fileExistsAtPath:[self.repository absolutePathForFile:path]]) {
120-
[existingFiles addObject:path];
121-
} else {
122-
[nonExistingFiles addObject:path];
123-
}
124-
}
125-
126-
if (existingFiles.count > 0) {
127-
if (![self.repository addFilesToIndex:existingFiles error:&error]) {
128-
[self presentError:error];
129-
}
130-
}
131-
132-
if (nonExistingFiles.count > 0) {
133-
if (![self.repository removeFilesFromIndex:nonExistingFiles error:&error]) {
134-
[self presentError:error];
135-
}
112+
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[self.repository absolutePathForFile:path] followLastSymlink:NO];
113+
if ((fileExists && [self.repository addFileToIndex:path error:&error]) || (!fileExists && [self.repository removeFileFromIndex:path error:&error])) {
114+
[self.repository notifyRepositoryChanged];
115+
} else {
116+
[self presentError:error];
136117
}
137-
138-
[self.repository notifyRepositoryChanged];
139118
}
140119

141120
- (void)stageSelectedChangesForFile:(NSString*)path oldLines:(NSIndexSet*)oldLines newLines:(NSIndexSet*)newLines {
@@ -158,12 +137,8 @@ - (void)stageSelectedChangesForFile:(NSString*)path oldLines:(NSIndexSet*)oldLin
158137
}
159138

160139
- (void)unstageAllChangesForFile:(NSString*)path {
161-
[self unstageAllChangesForFiles:@[ path ]];
162-
}
163-
164-
- (void)unstageAllChangesForFiles:(NSArray<NSString*>*)paths {
165140
NSError* error;
166-
if ([self.repository resetFilesInIndexToHEAD:paths error:&error]) {
141+
if ([self.repository resetFileInIndexToHEAD:path error:&error]) {
167142
[self.repository notifyWorkingDirectoryChanged];
168143
} else {
169144
[self presentError:error];
@@ -190,36 +165,18 @@ - (void)unstageSelectedChangesForFile:(NSString*)path oldLines:(NSIndexSet*)oldL
190165
}
191166

192167
- (BOOL)discardAllChangesForFile:(NSString*)path resetIndex:(BOOL)resetIndex error:(NSError**)error {
193-
return [self discardAllChangesForFiles:@[ path ]
194-
resetIndex:resetIndex
195-
error:error];
196-
}
197-
198-
- (BOOL)discardAllChangesForFiles:(NSArray<NSString*>*)paths resetIndex:(BOOL)resetIndex error:(NSError**)error {
199168
BOOL success = NO;
200169
if (resetIndex) {
201170
GCCommit* commit;
202-
if ([self.repository lookupHEADCurrentCommit:&commit branch:NULL error:error] && [self.repository resetFilesInIndexToHEAD:paths error:error]) {
203-
success = YES;
204-
for (NSString* path in paths) {
205-
if (commit && [self.repository checkTreeForCommit:commit containsFile:path error:NULL]) {
206-
if (![self.repository safeDeleteFileIfExists:path error:error] && [self.repository checkoutFileFromIndex:path error:error]) {
207-
return NO;
208-
}
209-
} else {
210-
if (![self.repository safeDeleteFileIfExists:path error:error]) {
211-
return NO;
212-
}
213-
}
171+
if ([self.repository lookupHEADCurrentCommit:&commit branch:NULL error:error] && [self.repository resetFileInIndexToHEAD:path error:error]) {
172+
if (commit && [self.repository checkTreeForCommit:commit containsFile:path error:NULL]) {
173+
success = [self.repository safeDeleteFileIfExists:path error:error] && [self.repository checkoutFileFromIndex:path error:error];
174+
} else {
175+
success = [self.repository safeDeleteFile:path error:error];
214176
}
215177
}
216178
} else {
217-
for (NSString* path in paths) {
218-
if (![self.repository safeDeleteFileIfExists:path error:error]) {
219-
return NO;
220-
}
221-
}
222-
success = [self.repository checkoutFilesFromIndex:paths error:error];
179+
success = [self.repository safeDeleteFileIfExists:path error:error] && [self.repository checkoutFileFromIndex:path error:error];
223180
}
224181
return success;
225182
}

0 commit comments

Comments
 (0)