@@ -139,8 +139,6 @@ - (void)postIndexRefreshSuccess:(BOOL)success message:(nullable NSString *)messa
139139 userInfo: @{@" description" : message}];
140140 }
141141 });
142-
143- [self postIndexUpdated ];
144142}
145143
146144- (void )postIndexUpdated
@@ -168,6 +166,10 @@ - (void)refresh
168166 return ;
169167 }
170168
169+ __block NSMutableDictionary *untrackedDictionary = nil ;
170+ __block NSMutableDictionary *stagedDictionary = nil ;
171+ __block NSMutableDictionary *unstagedDictionary = nil ;
172+
171173 // Enter the group for ALL tasks upfront, before launching any of them.
172174 // This prevents dispatch_group_notify from firing prematurely if an
173175 // early task completes before later tasks have called group_enter.
@@ -182,6 +184,24 @@ - (void)refresh
182184 // This block runs once all tasks have called group_leave.
183185 dispatch_group_notify (_indexRefreshGroup, dispatch_get_main_queue (), ^{
184186 // At this point, all index operations have finished.
187+ // Merge the three snapshots in Git-semantic order. The staged snapshot
188+ // owns the parent-tree metadata used by whole-file unstage; the unstaged
189+ // snapshot must not replace it with the current index entry.
190+ if (stagedDictionary) {
191+ for (PBChangedFile *file in self.files )
192+ file.hasStagedChanges = NO ;
193+ }
194+ if (unstagedDictionary && untrackedDictionary) {
195+ for (PBChangedFile *file in self.files )
196+ file.hasUnstagedChanges = NO ;
197+ }
198+ if (stagedDictionary)
199+ [self addFilesFromDictionary: stagedDictionary staged: YES tracked: YES ];
200+ if (unstagedDictionary)
201+ [self addFilesFromDictionary: unstagedDictionary staged: NO tracked: YES ];
202+ if (untrackedDictionary)
203+ [self addFilesFromDictionary: untrackedDictionary staged: NO tracked: NO ];
204+
185205 // We need to find all files that don't have either
186206 // staged or unstaged files, and delete them
187207
@@ -198,6 +218,8 @@ - (void)refresh
198218 [self didChangeValueForKey: @" indexChanges" ];
199219 }
200220
221+ [self postIndexUpdated ];
222+
201223 __block BOOL shouldReplay = NO ;
202224 dispatch_sync (self->_indexRefreshQueue , ^{
203225 self->_refreshInProgress = NO ;
@@ -237,7 +259,7 @@ - (void)refresh
237259 [dictionary setObject: fileStatus forKey: path];
238260 }
239261
240- [ self addFilesFromDictionary: dictionary staged: NO tracked: NO ] ;
262+ untrackedDictionary = dictionary ;
241263 [self postIndexRefreshSuccess: YES message: @" ls-files success" ];
242264 }
243265
@@ -256,8 +278,7 @@ - (void)refresh
256278 [self postIndexRefreshSuccess: NO message: @" diff-index failed" ];
257279 } else {
258280 NSArray *lines = [self linesFromData: readData];
259- NSMutableDictionary *dic = [self dictionaryForLines: lines];
260- [self addFilesFromDictionary: dic staged: YES tracked: YES ];
281+ stagedDictionary = [self dictionaryForLines: lines];
261282 [self postIndexRefreshSuccess: YES message: @" diff-index success" ];
262283 }
263284
@@ -276,8 +297,7 @@ - (void)refresh
276297 [self postIndexRefreshSuccess: NO message: @" diff-files failed" ];
277298 } else {
278299 NSArray *lines = [self linesFromData: readData];
279- NSMutableDictionary *dic = [self dictionaryForLines: lines];
280- [self addFilesFromDictionary: dic staged: NO tracked: YES ];
300+ unstagedDictionary = [self dictionaryForLines: lines];
281301 [self postIndexRefreshSuccess: YES message: @" diff-files success" ];
282302 }
283303
@@ -532,47 +552,35 @@ - (BOOL)performStageOrUnstage:(BOOL)stage withFiles:(NSArray *)files
532552
533553 // Staging
534554 while (loopCount < filesCount) {
535- // Input string for update-index
536- // This will be a list of filenames that
537- // should be updated. It's similar to
538- // "git add -- <files>
539- NSMutableString *input = [NSMutableString string ];
540-
541- for (NSUInteger i = loopFrom; i < loopTo; i++) {
542- loopCount++;
543-
544- PBChangedFile *file = [files objectAtIndex: i];
545-
546- if (stage) {
555+ NSError *error = nil ;
556+ BOOL success = NO ;
557+ if (stage) {
558+ // Input is a NUL-delimited list of paths, equivalent to git add.
559+ NSMutableString *input = [NSMutableString string ];
560+ for (NSUInteger i = loopFrom; i < loopTo; i++) {
561+ PBChangedFile *file = [files objectAtIndex: i];
547562 [input appendFormat: @" %@ \0 " , file.path];
548- } else {
549- NSString *indexInfo;
550- if (file.status == NEW ) {
551- // Index info lies because the file is NEW
552- indexInfo = [NSString stringWithFormat: @" 0 0000000000000000000000000000000000000000\t %@ \0 " , file.path];
553- } else {
554- indexInfo = [file indexInfo ];
555- }
556- [input appendString: indexInfo];
557563 }
558- }
559-
560- NSArray *arguments = nil ;
561- if (stage) {
562- arguments = @[ @" update-index" , @" --add" , @" --remove" , @" -z" , @" --stdin" ];
564+ success = [self .repository launchTaskWithArguments: @[ @" update-index" , @" --add" , @" --remove" , @" -z" , @" --stdin" ]
565+ input: input
566+ error: &error];
563567 } else {
564- arguments = @[ @" update-index" , @" -z" , @" --index-info" ];
568+ // Reset paths from the comparison tree instead of trusting cached
569+ // PBChangedFile blob metadata, which can lag behind partial staging.
570+ NSMutableArray <NSString *> *arguments = [NSMutableArray arrayWithObjects: @" reset" , @" --quiet" , [self parentTree ], @" --" , nil ];
571+ for (NSUInteger i = loopFrom; i < loopTo; i++) {
572+ PBChangedFile *file = [files objectAtIndex: i];
573+ [arguments addObject: file.path];
574+ }
575+ success = [self .repository launchTaskWithArguments: arguments error: &error];
565576 }
566-
567- NSError *error = nil ;
568- BOOL success = [self .repository launchTaskWithArguments: arguments
569- input: input
570- error: &error];
571577 if (!success) {
572578 [self postOperationFailed: [NSString stringWithFormat: @" Error in %@ files. Return value: %@ " , (stage ? @" staging" : @" unstaging" ), error.userInfo[PBTaskTerminationStatusKey]]];
573579 return NO ;
574580 }
575581
582+ loopCount = loopTo;
583+
576584 for (NSUInteger i = loopFrom; i < loopTo; i++) {
577585 PBChangedFile *file = [files objectAtIndex: i];
578586 file.hasStagedChanges = stage;
@@ -707,17 +715,25 @@ - (void)addFilesFromDictionary:(NSMutableDictionary *)dictionary staged:(BOOL)st
707715 // Object found, this is still a cached / uncached thing
708716 if (fileStatus) {
709717 if (tracked) {
710- NSString *mode = [[fileStatus objectAtIndex: 0 ] substringFromIndex: 1 ];
711- NSString *sha = [fileStatus objectAtIndex: 2 ];
712- file.commitBlobSHA = sha;
713- file.commitBlobMode = mode;
718+ BOOL preserveStagedIdentity = !staged && file.hasStagedChanges ;
719+ if (!preserveStagedIdentity) {
720+ NSString *mode = [[fileStatus objectAtIndex: 0 ] substringFromIndex: 1 ];
721+ NSString *sha = [fileStatus objectAtIndex: 2 ];
722+ file.commitBlobSHA = sha;
723+ file.commitBlobMode = mode;
724+
725+ if ([[fileStatus objectAtIndex: 4 ] isEqualToString: @" D" ])
726+ file.status = DELETED ;
727+ else if ([[fileStatus objectAtIndex: 0 ] isEqualToString: @" :000000" ])
728+ file.status = NEW ;
729+ else
730+ file.status = MODIFIED ;
731+ }
714732
715733 if (staged)
716734 file.hasStagedChanges = YES ;
717735 else
718736 file.hasUnstagedChanges = YES ;
719- if ([[fileStatus objectAtIndex: 4 ] isEqualToString: @" D" ])
720- file.status = DELETED ;
721737 } else {
722738 // Untracked file, set status to NEW, only unstaged changes
723739 file.hasStagedChanges = NO ;
0 commit comments