Skip to content

Commit eece2fc

Browse files
authored
Merge pull request laullon#90 from ksuther/close-repository-on-move
Use NSFileCoordinator to listen see if the repository is deleted or moved
2 parents add4acb + 0682362 commit eece2fc

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

Diff for: Classes/Controllers/PBGitRepositoryDocument.m

+31-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSEr
2727
return PBReturnError(outError, @"Unable to find git", [PBGitBinary notFoundError], nil);
2828
}
2929

30-
BOOL isDirectory = FALSE;
31-
[[NSFileManager defaultManager] fileExistsAtPath:[absoluteURL path] isDirectory:&isDirectory];
32-
if (!isDirectory) {
30+
NSNumber *isDirectory;
31+
32+
if (![absoluteURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:outError] || ![isDirectory boolValue]) {
3333
return PBReturnError(outError, @"Unable to read files", @"Reading files is not supported", nil);
3434
}
3535

@@ -52,6 +52,8 @@ - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSEr
5252
}
5353

5454

55+
[NSFileCoordinator addFilePresenter:self];
56+
5557
return YES;
5658
}
5759

@@ -60,6 +62,8 @@ - (void)close
6062
/* FIXME: Check that this deallocs the repo */
6163
// [revisionList cleanup];
6264

65+
[NSFileCoordinator removeFilePresenter:self];
66+
6367
[super close];
6468
}
6569

@@ -223,4 +227,28 @@ - (void)findInModeScriptCommand:(NSScriptCommand *)command
223227
}
224228
}
225229

230+
#pragma mark - NSFilePresenter
231+
232+
- (void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *errorOrNil))completionHandler
233+
{
234+
// The repository was deleted, close the document
235+
dispatch_async(dispatch_get_main_queue(), ^{
236+
[self close];
237+
238+
if (completionHandler) {
239+
completionHandler(nil);
240+
}
241+
});
242+
}
243+
244+
- (void)presentedItemDidMoveToURL:(NSURL *)newURL
245+
{
246+
// Close the document if the repository gets moved
247+
// It would be better to automatically update the document, but that's a bit tricky with the current architecture
248+
// If PBGitRepositoryDocument and PBGitRepository get unified then it would make this much easier
249+
dispatch_async(dispatch_get_main_queue(), ^{
250+
[self close];
251+
});
252+
}
253+
226254
@end

0 commit comments

Comments
 (0)