2727NSString *PBGitIndexAmendMessageAvailable = @" PBGitIndexAmendMessageAvailable" ;
2828NSString *PBGitIndexOperationFailed = @" PBGitIndexOperationFailed" ;
2929
30+ NS_ENUM (NSUInteger , PBGitIndexOperation) {
31+ PBGitIndexStageFiles,
32+ PBGitIndexUnstageFiles,
33+ };
34+
3035@interface PBGitIndex (IndexRefreshMethods)
3136
3237- (NSMutableDictionary *)dictionaryForLines : (NSArray *)lines ;
@@ -371,20 +376,21 @@ - (void)postOperationFailed:(NSString *)description
371376 userInfo: [NSDictionary dictionaryWithObject: description forKey: @" description" ]];
372377}
373378
374- - (BOOL )stageFiles : ( NSArray *)stageFiles
379+ - (BOOL )performStageOrUnstage : ( BOOL ) stage withFiles : ( NSArray *)files
375380{
376381 // Do staging files by chunks of 1000 files each, to prevent program freeze (because NSPipe has limited capacity)
377-
378- int filesCount = [stageFiles count ];
379-
382+
383+ int filesCount = [files count ];
384+ const int MAX_FILES_PER_STAGE = 1000 ;
385+
380386 // Prepare first iteration
381387 int loopFrom = 0 ;
382- int loopTo = 1000 ;
388+ int loopTo = MAX_FILES_PER_STAGE ;
383389 if (loopTo > filesCount)
384390 loopTo = filesCount;
385391 int loopCount = 0 ;
386392 int i = 0 ;
387-
393+
388394 // Staging
389395 while (loopCount < filesCount) {
390396 // Input string for update-index
@@ -395,104 +401,71 @@ - (BOOL)stageFiles:(NSArray *)stageFiles
395401
396402 for (i = loopFrom; i < loopTo; i++) {
397403 loopCount++;
398-
399- PBChangedFile *file = [stageFiles objectAtIndex: i];
400-
401- [input appendFormat: @" %@ \0 " , file.path];
404+
405+ PBChangedFile *file = [files objectAtIndex: i];
406+
407+ if (stage) {
408+ [input appendFormat: @" %@ \0 " , file.path];
409+ } else {
410+ NSString *indexInfo;
411+ if (file.status == NEW) {
412+ // Index info lies because the file is NEW
413+ indexInfo = [NSString stringWithFormat: @" 0 0000000000000000000000000000000000000000\t\" %@ \" " , file.path];
414+ } else {
415+ indexInfo = [file indexInfo ];
416+ }
417+ [input appendString: indexInfo];
418+ }
402419 }
403-
404-
420+
421+
405422 int ret = 1 ;
406- [self .repository outputForArguments: [NSArray arrayWithObjects: @" update-index" , @" --add" , @" --remove" , @" -z" , @" --stdin" , nil ]
407- inputString: input
408- retValue: &ret];
423+ if (stage) {
424+ [self .repository outputForArguments: [NSArray arrayWithObjects: @" update-index" , @" --add" , @" --remove" , @" -z" , @" --stdin" , nil ]
425+ inputString: input
426+ retValue: &ret];
427+ } else {
428+ [self .repository outputForArguments: [NSArray arrayWithObjects: @" update-index" , @" -z" , @" --index-info" , nil ]
429+ inputString: input
430+ retValue: &ret];
431+ }
409432
410433 if (ret) {
411- [self postOperationFailed: [NSString stringWithFormat: @" Error in staging files. Return value: %i " , ret]];
434+ [self postOperationFailed: [NSString stringWithFormat: @" Error in %@ files. Return value: %i " , (stage ? @" staging " : @" unstaging " ) , ret]];
412435 return NO ;
413436 }
414437
415438 for (i = loopFrom; i < loopTo; i++) {
416- PBChangedFile *file = [stageFiles objectAtIndex: i];
417-
418- file.hasUnstagedChanges = NO ;
419- file.hasStagedChanges = YES ;
420- }
421-
422- // Prepare next iteration
423- loopFrom = loopCount;
424- loopTo = loopFrom + 1000 ;
425- if (loopTo > filesCount)
426- loopTo = filesCount;
427- }
439+ PBChangedFile *file = [files objectAtIndex: i];
428440
429- [self postIndexChange ];
430- return YES ;
431- }
432-
433- // TODO: Refactor with above. What's a better name for this?
434- - (BOOL )unstageFiles : (NSArray *)unstageFiles
435- {
436- // Do unstaging files by chunks of 1000 files each, to prevent program freeze (because NSPipe has limited capacity)
437-
438- int filesCount = [unstageFiles count ];
439-
440- // Prepare first iteration
441- int loopFrom = 0 ;
442- int loopTo = 1000 ;
443- if (loopTo > filesCount)
444- loopTo = filesCount;
445- int loopCount = 0 ;
446- int i = 0 ;
447-
448- // Unstaging
449- while (loopCount < filesCount) {
450- NSMutableString *input = [NSMutableString string ];
451-
452- for (i = loopFrom; i < loopTo; i++) {
453- loopCount++;
454-
455- PBChangedFile *file = [unstageFiles objectAtIndex: i];
456-
457- NSString *indexInfo = nil ;
458- if (file.status == NEW) {
459- // Index info lies because the file is NEW
460- indexInfo = [NSString stringWithFormat: @" 0 0000000000000000000000000000000000000000\t\" %@ \" " , file.path];
441+ if (stage) {
442+ file.hasUnstagedChanges = NO ;
443+ file.hasStagedChanges = YES ;
461444 } else {
462- indexInfo = [file indexInfo ];
445+ file.hasUnstagedChanges = YES ;
446+ file.hasStagedChanges = NO ;
463447 }
464- [input appendString: indexInfo];
465448 }
466-
467- int ret = 1 ;
468- [self .repository outputForArguments: [NSArray arrayWithObjects: @" update-index" , @" -z" , @" --index-info" , nil ]
469- inputString: input
470- retValue: &ret];
471449
472- if (ret)
473- {
474- [self postOperationFailed: [NSString stringWithFormat: @" Error in unstaging files. Return value: %i " , ret]];
475- return NO ;
476- }
477-
478- for (i = loopFrom; i < loopTo; i++) {
479- PBChangedFile *file = [unstageFiles objectAtIndex: i];
480-
481- file.hasUnstagedChanges = YES ;
482- file.hasStagedChanges = NO ;
483- }
484-
485450 // Prepare next iteration
486451 loopFrom = loopCount;
487- loopTo = loopFrom + 1000 ;
452+ loopTo = loopFrom + MAX_FILES_PER_STAGE ;
488453 if (loopTo > filesCount)
489454 loopTo = filesCount;
490455 }
491-
492- [self postIndexChange ];
493456 return YES ;
494457}
495458
459+ - (BOOL )stageFiles : (NSArray *)stageFiles
460+ {
461+ return [self performStageOrUnstage: YES withFiles: stageFiles];
462+ }
463+
464+ - (BOOL )unstageFiles : (NSArray *)unstageFiles
465+ {
466+ return [self performStageOrUnstage: NO withFiles: unstageFiles];
467+ }
468+
496469- (void )discardChangesForFiles : (NSArray *)discardFiles
497470{
498471 NSArray *paths = [discardFiles valueForKey: @" path" ];
0 commit comments