Skip to content

Commit f95c90e

Browse files
committed
Fix index.lock contention caused by git update-index on every FSEvents notification
The commit view triggered git update-index --refresh on every file-system event via the FSEvents -> PBGitRepositoryEventNotification -> refresh chain. This held .git/index.lock for the duration of each run, causing concurrent git operations (git add, git commit, git stash, etc.) from terminals or editors to fail with "Unable to create index.lock: File exists". - Remove git update-index --refresh from the PBGitIndex refresh path; the remaining reads (diff-index, diff-files, ls-files) are read-only and never acquire the index lock - Add -refreshStatCache, which runs update-index --refresh as a separate fire-and-forget call followed by a refresh, to clear phantom stat-dirty entries (files that appear modified due to mtime mismatch but have unchanged content) - Call refreshStatCache on NSApplicationDidBecomeActiveNotification so the stat cache is reconciled when the user switches back to GitX, rather than on every file-system event Fixes gitx#164
1 parent f8d5703 commit f95c90e

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

‎Classes/Controllers/PBGitCommitController.m‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ - (id)initWithRepository:(PBGitRepository *)theRepository superController:(PBGit
6565
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(indexChanged:) name:PBGitIndexIndexUpdated object:index];
6666
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(indexOperationFailed:) name:PBGitIndexOperationFailed object:index];
6767
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(repositoryUpdatedNotification:) name:PBGitRepositoryEventNotification object:theRepository];
68+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:nil];
6869

6970
return self;
7071
}
@@ -121,6 +122,11 @@ - (void)awakeFromNib
121122
stagedTable.menu = [unstagedTable.menu copy];
122123
}
123124

125+
- (void)applicationDidBecomeActive:(NSNotification *)notification
126+
{
127+
[self.repository.index refreshStatCache];
128+
}
129+
124130
- (void)repositoryUpdatedNotification:(NSNotification *)notification
125131
{
126132
PBGitRepositoryWatcherEventType eventType = [(NSNumber *)[[notification userInfo] objectForKey:kPBGitRepositoryEventTypeUserInfoKey] unsignedIntValue];

‎Classes/git/PBGitIndex.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ extern NSString *PBGitIndexOperationFailed;
5959
// Refresh the index
6060
- (void)refresh;
6161

62+
// Update the stat cache (git update-index --refresh). Clears phantom "modified"
63+
// entries caused by stat mismatches. Call on app activation, not on every change.
64+
- (void)refreshStatCache;
65+
6266
// Run the prepare-git-msg hook and return the result
6367
- (nullable NSString *)createPrepareCommitMessage;
6468

‎Classes/git/PBGitIndex.m‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ - (void)refresh
169169
// Enter the group for ALL tasks upfront, before launching any of them.
170170
// This prevents dispatch_group_notify from firing prematurely if an
171171
// early task completes before later tasks have called group_enter.
172-
dispatch_group_enter(_indexRefreshGroup); // update-index
173172
BOOL isBare = [self.repository isBareRepository];
174173
if (!isBare) {
175174
dispatch_group_enter(_indexRefreshGroup); // ls-files
@@ -204,20 +203,6 @@ - (void)refresh
204203
[self postIndexRefreshFinished];
205204
});
206205

207-
// Ask Git to refresh the index
208-
[PBTask launchTask:[PBGitBinary path]
209-
arguments:@[ @"update-index", @"-q", @"--unmerged", @"--ignore-missing", @"--refresh" ]
210-
inDirectory:self.repository.workingDirectoryURL.path
211-
completionHandler:^(NSData *readData, NSError *error) {
212-
if (error) {
213-
[self postIndexRefreshSuccess:NO message:@"update-index failed"];
214-
} else {
215-
[self postIndexRefreshSuccess:YES message:@"update-index success"];
216-
}
217-
218-
dispatch_group_leave(self->_indexRefreshGroup);
219-
}];
220-
221206
if (isBare) {
222207
return;
223208
}
@@ -292,6 +277,22 @@ - (void)refresh
292277
}];
293278
}
294279

280+
// Refreshes the stat cache in the index by running git update-index --refresh.
281+
// This clears phantom "modified" entries caused by stat mismatches (same content,
282+
// different mtime). Called on app activation rather than every FSEvents notification
283+
// to avoid holding index.lock constantly.
284+
- (void)refreshStatCache
285+
{
286+
if ([self.repository isBareRepository]) return;
287+
288+
[PBTask launchTask:[PBGitBinary path]
289+
arguments:@[ @"update-index", @"-q", @"--unmerged", @"--ignore-missing", @"--refresh" ]
290+
inDirectory:self.repository.workingDirectoryURL.path
291+
completionHandler:^(NSData *readData, NSError *error) {
292+
[self refresh];
293+
}];
294+
}
295+
295296
// Returns the tree to compare the index to, based
296297
// on whether amend is set or not.
297298
- (NSString *)parentTree

0 commit comments

Comments
 (0)