Skip to content

Commit 76d8454

Browse files
committed
Publish coherent index snapshots and reset paths from parent
gitx/gitx#421 gitx/gitx#301 gitx/gitx#530 gitx/gitx#428
1 parent 56f9134 commit 76d8454

2 files changed

Lines changed: 168 additions & 50 deletions

File tree

‎Classes/git/PBGitIndex.m‎

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

‎GitXTests/GitXCoreTests.m‎

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,11 @@ - (void)testRefreshRepresentsPartiallyStagedFileInBothSections
490490
NSError *error = nil;
491491
XCTAssertTrue([self.fixture writeText:@"first line\nstaged line\nunstaged line\n" toPath:@"tracked.txt" error:&error], @"%@", error);
492492
NSString *patch = @"diff --git a/tracked.txt b/tracked.txt\n"
493-
@"--- a/tracked.txt\n"
494-
@"+++ b/tracked.txt\n"
495-
@"@@ -1 +1,2 @@\n"
496-
@" first line\n"
497-
@"+staged line\n";
493+
@"--- a/tracked.txt\n"
494+
@"+++ b/tracked.txt\n"
495+
@"@@ -1 +1,2 @@\n"
496+
@" first line\n"
497+
@"+staged line\n";
498498
[self refreshIndexAfterPerforming:^{
499499
XCTAssertTrue([self.repository.index applyPatch:patch stage:YES reverse:NO]);
500500
}];
@@ -511,6 +511,33 @@ - (void)testRefreshRepresentsPartiallyStagedFileInBothSections
511511
XCTAssertTrue([unstagedDiff containsString:@"+unstaged line"]);
512512
}
513513

514+
- (void)testWholeFileUnstageAfterPartialStageIgnoresStaleIndexMetadata
515+
{
516+
NSError *error = nil;
517+
XCTAssertTrue([self.fixture writeText:@"first line\nstaged line\nunstaged line\n" toPath:@"tracked.txt" error:&error], @"%@", error);
518+
NSString *patch = @"diff --git a/tracked.txt b/tracked.txt\n"
519+
@"--- a/tracked.txt\n"
520+
@"+++ b/tracked.txt\n"
521+
@"@@ -1 +1,2 @@\n"
522+
@" first line\n"
523+
@"+staged line\n";
524+
[self refreshIndexAfterPerforming:^{
525+
XCTAssertTrue([self.repository.index applyPatch:patch stage:YES reverse:NO]);
526+
}];
527+
528+
PBChangedFile *tracked = [self changedFileAtPath:@"tracked.txt"];
529+
XCTAssertNotNil(tracked);
530+
tracked.commitBlobMode = @"100644";
531+
tracked.commitBlobSHA = [[self.fixture git:@[ @"rev-parse", @":tracked.txt" ] error:&error]
532+
stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
533+
XCTAssertTrue([self.repository.index unstageFiles:@[ tracked ]]);
534+
535+
NSString *stagedNames = [self.fixture git:@[ @"diff", @"--cached", @"--name-only" ] error:&error];
536+
NSString *workingText = [NSString stringWithContentsOfFile:[self.fixture.path stringByAppendingPathComponent:@"tracked.txt"] encoding:NSUTF8StringEncoding error:&error];
537+
XCTAssertEqualObjects(stagedNames, @"");
538+
XCTAssertEqualObjects(workingText, @"first line\nstaged line\nunstaged line\n");
539+
}
540+
514541
- (void)testAmendRefreshTreatsFileAddedByLastCommitAsNew
515542
{
516543
NSError *error = nil;
@@ -530,6 +557,62 @@ - (void)testAmendRefreshTreatsFileAddedByLastCommitAsNew
530557
XCTAssertFalse(added.hasUnstagedChanges);
531558
}
532559

560+
- (void)testAmendCanUnstageModifiedFileAddedByLastCommit
561+
{
562+
NSError *error = nil;
563+
NSString *path = @"folder/spaced ünicode.txt";
564+
XCTAssertTrue([self.fixture writeText:@"committed\n" toPath:path error:&error], @"%@", error);
565+
XCTAssertTrue([self.fixture commitAllWithMessage:@"add file" error:&error], @"%@", error);
566+
self.repository = [[PBGitRepository alloc] initWithURL:[NSURL fileURLWithPath:self.fixture.path] error:&error];
567+
XCTAssertNotNil(self.repository, @"%@", error);
568+
XCTAssertTrue([self.fixture writeText:@"committed\nmodified\n" toPath:path error:&error], @"%@", error);
569+
570+
[self refreshIndexAfterPerforming:^{
571+
[self.repository.index refresh];
572+
}];
573+
XCTAssertEqual([self changedFileAtPath:path].status, MODIFIED);
574+
[self refreshIndexAfterPerforming:^{
575+
self.repository.index.amend = YES;
576+
}];
577+
PBChangedFile *added = [self changedFileAtPath:path];
578+
XCTAssertNotNil(added);
579+
XCTAssertEqual(added.status, NEW);
580+
XCTAssertTrue(added.hasStagedChanges);
581+
XCTAssertTrue(added.hasUnstagedChanges);
582+
XCTAssertTrue([self.repository.index unstageFiles:@[ added ]]);
583+
584+
NSString *stagedNames = [self.fixture git:@[ @"diff", @"--cached", @"--name-only", @"HEAD^" ] error:&error];
585+
NSString *indexEntry = [self.fixture git:@[ @"ls-files", @"--stage", @"--", path ] error:&error];
586+
NSString *workingText = [NSString stringWithContentsOfFile:[self.fixture.path stringByAppendingPathComponent:path] encoding:NSUTF8StringEncoding error:&error];
587+
XCTAssertEqualObjects(stagedNames, @"");
588+
XCTAssertEqualObjects(indexEntry, @"");
589+
XCTAssertEqualObjects(workingText, @"committed\nmodified\n");
590+
}
591+
592+
- (void)testRefreshPublishesOneCoherentIndexUpdate
593+
{
594+
NSError *error = nil;
595+
XCTAssertTrue([self.fixture writeText:@"changed\n" toPath:@"tracked.txt" error:&error], @"%@", error);
596+
XCTAssertTrue([self.fixture writeText:@"untracked\n" toPath:@"untracked.txt" error:&error], @"%@", error);
597+
__block NSUInteger updateCount = 0;
598+
id token = [[NSNotificationCenter defaultCenter]
599+
addObserverForName:PBGitIndexIndexUpdated
600+
object:self.repository.index
601+
queue:NSOperationQueue.mainQueue
602+
usingBlock:^(__unused NSNotification *notification) {
603+
updateCount++;
604+
}];
605+
606+
[self refreshIndexAfterPerforming:^{
607+
[self.repository.index refresh];
608+
}];
609+
610+
[[NSNotificationCenter defaultCenter] removeObserver:token];
611+
XCTAssertEqual(updateCount, 1);
612+
XCTAssertNotNil([self changedFileAtPath:@"tracked.txt"]);
613+
XCTAssertNotNil([self changedFileAtPath:@"untracked.txt"]);
614+
}
615+
533616
- (void)testReadOnlyRefreshSucceedsWhileIndexLockExists
534617
{
535618
NSError *error = nil;
@@ -555,6 +638,25 @@ - (void)testReadOnlyRefreshSucceedsWhileIndexLockExists
555638
XCTAssertNotNil([self changedFileAtPath:@"tracked.txt"]);
556639
}
557640

641+
- (void)testWholeFileUnstageFailureLeavesIndexUntouched
642+
{
643+
NSError *error = nil;
644+
XCTAssertTrue([self.fixture writeText:@"changed\n" toPath:@"tracked.txt" error:&error], @"%@", error);
645+
[self refreshIndexAfterPerforming:^{
646+
[self.repository.index refresh];
647+
}];
648+
PBChangedFile *tracked = [self changedFileAtPath:@"tracked.txt"];
649+
XCTAssertTrue([self.repository.index stageFiles:@[ tracked ]]);
650+
NSString *lockPath = [self.fixture.path stringByAppendingPathComponent:@".git/index.lock"];
651+
XCTAssertTrue([NSData.data writeToFile:lockPath options:NSDataWritingAtomic error:&error], @"%@", error);
652+
653+
XCTAssertFalse([self.repository.index unstageFiles:@[ tracked ]]);
654+
655+
[[NSFileManager defaultManager] removeItemAtPath:lockPath error:nil];
656+
NSString *stagedNames = [self.fixture git:@[ @"diff", @"--cached", @"--name-only" ] error:&error];
657+
XCTAssertEqualObjects(stagedNames, @"tracked.txt\n");
658+
}
659+
558660
- (void)testDiscardRestoresTrackedFilesButLeavesUntrackedFiles
559661
{
560662
NSError *error = nil;

0 commit comments

Comments
 (0)