Skip to content

Commit 0903ca4

Browse files
committed
[FIX] fixes a race condition in refreshing index which spawns hundreds of external "git" processes and slows down host machine
1 parent 679764a commit 0903ca4

1 file changed

Lines changed: 88 additions & 56 deletions

File tree

‎Classes/git/PBGitIndex.m‎

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ @interface PBGitIndex () {
4545
dispatch_queue_t _indexRefreshQueue;
4646
dispatch_group_t _indexRefreshGroup;
4747
BOOL _amend;
48+
BOOL _refreshInProgress;
4849
}
4950

5051
@property (retain) NSDictionary *amendEnvironment;
@@ -65,6 +66,7 @@ - (id)initWithRepository:(PBGitRepository *)theRepository
6566
_files = [NSMutableArray array];
6667

6768
_indexRefreshGroup = dispatch_group_create();
69+
_indexRefreshQueue = dispatch_queue_create("org.gitx.indexRefresh", DISPATCH_QUEUE_SERIAL);
6870

6971
return self;
7072
}
@@ -149,26 +151,34 @@ - (void)postIndexUpdated
149151

150152
- (void)refresh
151153
{
152-
dispatch_group_enter(_indexRefreshGroup);
153-
154-
// Ask Git to refresh the index
155-
[PBTask launchTask:[PBGitBinary path]
156-
arguments:@[ @"update-index", @"-q", @"--unmerged", @"--ignore-missing", @"--refresh" ]
157-
inDirectory:self.repository.workingDirectoryURL.path
158-
completionHandler:^(NSData *readData, NSError *error) {
159-
if (error) {
160-
[self postIndexRefreshSuccess:NO message:@"update-index failed"];
161-
} else {
162-
[self postIndexRefreshSuccess:YES message:@"update-index success"];
163-
}
154+
//
155+
// Guard against concurrent refresh calls — skip if one is already running
156+
__block BOOL shouldSkip = NO;
157+
dispatch_sync(_indexRefreshQueue, ^{
158+
if (self->_refreshInProgress) {
159+
shouldSkip = YES;
160+
return;
161+
}
162+
self->_refreshInProgress = YES;
163+
});
164164

165-
dispatch_group_leave(self->_indexRefreshGroup);
166-
}];
165+
if (shouldSkip) {
166+
return;
167+
}
167168

169+
// Enter the group for ALL tasks upfront, before launching any of them.
170+
// This prevents dispatch_group_notify from firing prematurely if an
171+
// early task completes before later tasks have called group_enter.
172+
dispatch_group_enter(_indexRefreshGroup); // update-index
173+
BOOL isBare = [self.repository isBareRepository];
174+
if (!isBare) {
175+
dispatch_group_enter(_indexRefreshGroup); // ls-files
176+
dispatch_group_enter(_indexRefreshGroup); // diff-index
177+
dispatch_group_enter(_indexRefreshGroup); // diff-files
178+
}
168179

169-
// This block is called when each of the other blocks scheduled are done,
170-
// which means we can delete all files previously marked as deletable.
171-
// Note, there are scheduled blocks *below* this one ;-).
180+
// Register the notify block AFTER all group_enter calls.
181+
// This block runs once all tasks have called group_leave.
172182
dispatch_group_notify(_indexRefreshGroup, dispatch_get_main_queue(), ^{
173183
// At this point, all index operations have finished.
174184
// We need to find all files that don't have either
@@ -187,74 +197,96 @@ - (void)refresh
187197
[self didChangeValueForKey:@"indexChanges"];
188198
}
189199

200+
dispatch_async(self->_indexRefreshQueue, ^{
201+
self->_refreshInProgress = NO;
202+
});
203+
190204
[self postIndexRefreshFinished];
191205
});
192206

193-
if ([self.repository isBareRepository]) {
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+
221+
if (isBare) {
194222
return;
195223
}
196224

197-
// Other files
198-
dispatch_group_enter(_indexRefreshGroup);
225+
// Other files (untracked)
199226
[PBTask launchTask:[PBGitBinary path]
200227
arguments:@[ @"ls-files", @"--others", @"--exclude-standard", @"-z" ]
201228
inDirectory:self.repository.workingDirectoryURL.path
202229
completionHandler:^(NSData *readData, NSError *error) {
203-
if (error) {
204-
[self postIndexRefreshSuccess:NO message:@"ls-files failed"];
205-
} else {
206-
NSArray *lines = [self linesFromData:readData];
207-
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:[lines count]];
208-
// Other files are untracked, so we don't have any real index information. Instead, we can just fake it.
209-
// The line below is not used at all, as for these files the commitBlob isn't set
210-
NSArray *fileStatus = [NSArray arrayWithObjects:@":000000", @"100644", @"0000000000000000000000000000000000000000", @"0000000000000000000000000000000000000000", @"A", nil];
211-
for (NSString *path in lines) {
212-
if ([path length] == 0)
213-
continue;
214-
[dictionary setObject:fileStatus forKey:path];
230+
// Serialize access to self.files via _indexRefreshQueue
231+
dispatch_sync(self->_indexRefreshQueue, ^{
232+
if (error) {
233+
[self postIndexRefreshSuccess:NO message:@"ls-files failed"];
234+
} else {
235+
NSArray *lines = [self linesFromData:readData];
236+
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:[lines count]];
237+
// Other files are untracked, so we don't have any real index information. Instead, we can just fake it.
238+
// The line below is not used at all, as for these files the commitBlob isn't set
239+
NSArray *fileStatus = [NSArray arrayWithObjects:@":000000", @"100644", @"0000000000000000000000000000000000000000", @"0000000000000000000000000000000000000000", @"A", nil];
240+
for (NSString *path in lines) {
241+
if ([path length] == 0)
242+
continue;
243+
[dictionary setObject:fileStatus forKey:path];
244+
}
245+
246+
[self addFilesFromDictionary:dictionary staged:NO tracked:NO];
247+
[self postIndexRefreshSuccess:YES message:@"ls-files success"];
215248
}
249+
});
216250

217-
[self addFilesFromDictionary:dictionary staged:NO tracked:NO];
218-
}
219-
220-
[self postIndexRefreshSuccess:YES message:@"ls-files success"];
221251
dispatch_group_leave(self->_indexRefreshGroup);
222252
}];
223253

224254
// Staged files
225-
dispatch_group_enter(_indexRefreshGroup);
226255
[PBTask launchTask:[PBGitBinary path]
227256
arguments:@[ @"diff-index", @"--cached", @"-z", [self parentTree] ]
228257
inDirectory:self.repository.workingDirectoryURL.path
229258
completionHandler:^(NSData *readData, NSError *error) {
230-
if (error) {
231-
[self postIndexRefreshSuccess:NO message:@"diff-index failed"];
232-
} else {
233-
NSArray *lines = [self linesFromData:readData];
234-
NSMutableDictionary *dic = [self dictionaryForLines:lines];
235-
[self addFilesFromDictionary:dic staged:YES tracked:YES];
236-
}
237-
238-
[self postIndexRefreshSuccess:YES message:@"diff-index success"];
259+
// Serialize access to self.files via _indexRefreshQueue
260+
dispatch_sync(self->_indexRefreshQueue, ^{
261+
if (error) {
262+
[self postIndexRefreshSuccess:NO message:@"diff-index failed"];
263+
} else {
264+
NSArray *lines = [self linesFromData:readData];
265+
NSMutableDictionary *dic = [self dictionaryForLines:lines];
266+
[self addFilesFromDictionary:dic staged:YES tracked:YES];
267+
[self postIndexRefreshSuccess:YES message:@"diff-index success"];
268+
}
269+
});
239270

240271
dispatch_group_leave(self->_indexRefreshGroup);
241272
}];
242273

243-
244274
// Unstaged files
245-
dispatch_group_enter(_indexRefreshGroup);
246275
[PBTask launchTask:[PBGitBinary path]
247276
arguments:@[ @"diff-files", @"-z" ]
248277
inDirectory:self.repository.workingDirectoryURL.path
249278
completionHandler:^(NSData *readData, NSError *error) {
250-
if (error) {
251-
[self postIndexRefreshSuccess:NO message:@"diff-files failed"];
252-
} else {
253-
NSArray *lines = [self linesFromData:readData];
254-
NSMutableDictionary *dic = [self dictionaryForLines:lines];
255-
[self addFilesFromDictionary:dic staged:NO tracked:YES];
256-
}
257-
[self postIndexRefreshSuccess:YES message:@"diff-files success"];
279+
// Serialize access to self.files via _indexRefreshQueue
280+
dispatch_sync(self->_indexRefreshQueue, ^{
281+
if (error) {
282+
[self postIndexRefreshSuccess:NO message:@"diff-files failed"];
283+
} else {
284+
NSArray *lines = [self linesFromData:readData];
285+
NSMutableDictionary *dic = [self dictionaryForLines:lines];
286+
[self addFilesFromDictionary:dic staged:NO tracked:YES];
287+
[self postIndexRefreshSuccess:YES message:@"diff-files success"];
288+
}
289+
});
258290

259291
dispatch_group_leave(self->_indexRefreshGroup);
260292
}];

0 commit comments

Comments
 (0)