From 8d729dae4cc8d3b564d149d6c9bb96922ee1bafa Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Mon, 22 Dec 2008 21:31:05 +0100 Subject: [PATCH] CommitView: Don't change selection when refreshing We used to read in a completely new array when refreshing the index. The problem with this is that the selection changes when reading in the new array. We avoid this by changing the current array, rather than loading in a completely new one. --- PBChangedFile.h | 4 ++-- PBChangedFile.m | 2 +- PBGitCommitController.m | 40 +++++++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/PBChangedFile.h b/PBChangedFile.h index 4641a4d..f4d53fd 100644 --- a/PBChangedFile.h +++ b/PBChangedFile.h @@ -19,6 +19,7 @@ typedef enum { NSString *path; BOOL hasCachedChanges; BOOL hasUnstagedChanges; + BOOL shouldBeDeleted; // Index and HEAD stuff, to be used to revert changes NSString *commitBlobSHA; @@ -30,8 +31,7 @@ typedef enum { @property (copy) NSString *path, *commitBlobSHA, *commitBlobMode; @property (assign) PBChangedFileStatus status; -@property (assign) BOOL hasCachedChanges; -@property (assign) BOOL hasUnstagedChanges; +@property (assign) BOOL hasCachedChanges, hasUnstagedChanges, shouldBeDeleted; - (NSImage *)icon; - (NSString *)indexInfo; diff --git a/PBChangedFile.m b/PBChangedFile.m index e2bb7e8..bbea198 100644 --- a/PBChangedFile.m +++ b/PBChangedFile.m @@ -11,7 +11,7 @@ @implementation PBChangedFile -@synthesize path, status, hasCachedChanges, hasUnstagedChanges, commitBlobSHA, commitBlobMode; +@synthesize path, status, hasCachedChanges, hasUnstagedChanges, commitBlobSHA, commitBlobMode, shouldBeDeleted; - (id) initWithPath:(NSString *)p { diff --git a/PBGitCommitController.m b/PBGitCommitController.m index deb31e1..4d54ade 100644 --- a/PBGitCommitController.m +++ b/PBGitCommitController.m @@ -16,6 +16,7 @@ - (void)awakeFromNib { + self.files = [NSMutableArray array]; [super awakeFromNib]; [self refresh:self]; @@ -80,9 +81,11 @@ - (void) refresh:(id) sender { + for (PBChangedFile *file in files) + file.shouldBeDeleted = YES; + self.status = @"Refreshing index…"; self.busy++; - self.files = [NSMutableArray array]; [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"update-index", @"-q", @"--unmerged", @"--ignore-missing", @"--refresh", nil]]; self.busy--; @@ -108,8 +111,6 @@ [nc addObserver:self selector:@selector(readCachedFiles:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle]; self.busy++; [handle readToEndOfFileInBackgroundAndNotify]; - - self.files = files; } - (void) updateView @@ -119,10 +120,16 @@ - (void) doneProcessingIndex { - if (!--self.busy) + [self willChangeValueForKey:@"files"]; + if (!--self.busy) { self.status = @"Ready"; - [unstagedFilesController didChangeArrangementCriteria]; - [cachedFilesController didChangeArrangementCriteria]; + NSArray *filesToBeDeleted = [files filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"shouldBeDeleted == 1"]]; + for (PBChangedFile *file in filesToBeDeleted) { + NSLog(@"Deleting file: %@", [file path]); + [files removeObject:file]; + } + } + [self didChangeValueForKey:@"files"]; } - (void) readOtherFiles:(NSNotification *)notification; @@ -131,13 +138,31 @@ for (NSString *line in lines) { if ([line length] == 0) continue; + + BOOL added = NO; + // Check if file is already in our index + for (PBChangedFile *file in files) { + if ([[file path] isEqualToString:line]) { + file.shouldBeDeleted = NO; + added = YES; + file.status = NEW; + file.hasCachedChanges = NO; + file.hasUnstagedChanges = YES; + continue; + } + } + + if (added) + continue; + + // File does not exist yet, so add it PBChangedFile *file =[[PBChangedFile alloc] initWithPath:line]; file.status = NEW; file.hasCachedChanges = NO; file.hasUnstagedChanges = YES; - [files addObject: file]; } + [self doneProcessingIndex]; } @@ -161,6 +186,7 @@ // but rather update it to incorporate our changes for (PBChangedFile *file in files) { if ([file.path isEqualToString:line]) { + file.shouldBeDeleted = NO; if (cached) { file.commitBlobSHA = sha; file.commitBlobMode = mode;