diff --git a/GitUpKit/Extensions/GCLiveRepository+Utilities-Tests.m b/GitUpKit/Extensions/GCLiveRepository+Utilities-Tests.m new file mode 100644 index 00000000..455b074c --- /dev/null +++ b/GitUpKit/Extensions/GCLiveRepository+Utilities-Tests.m @@ -0,0 +1,423 @@ +// +// GCLiveRepository+Utilities-Tests.m +// Tests +// + +#if !__has_feature(objc_arc) +#error This file requires ARC +#endif + +#import +#import + +#import "GCTestCase.h" +#import "GCLiveRepository+Utilities.h" // Not a public header of GitUpKit +#import + +static NSString* const kLocalChangesAlertTitle = @"Local changes would be overwritten by checkout"; +static NSString* const kDiscardButtonTitle = @"Discard Changes and Continue"; +static NSString* const kCancelButtonTitle = @"Cancel"; +static NSString* const kCreateLocalBranchButtonTitle = @"Create Local Branch"; +static NSString* const kCheckoutCommitButtonTitle = @"Checkout Commit"; +static NSString* const kLocalEdit = @"local edit\n"; + +// Alert the code asked to show as a sheet, with the handler AppKit calls when one of its buttons is clicked +@interface GCSheetAlert : NSObject +@property(nonatomic) NSAlert* alert; +@property(nonatomic, copy) void (^completionHandler)(NSModalResponse returnCode); +@end + +@implementation GCSheetAlert +@end + +// Records the errors it is asked to present, because presenting one runs a modal alert that would block the test +@interface GCErrorRecordingWindow : NSWindow +@property(nonatomic) NSArray* presentedErrors; +@end + +@implementation GCErrorRecordingWindow + +- (BOOL)presentError:(NSError*)error { + self.presentedErrors = [(self.presentedErrors ?: @[]) arrayByAddingObject:error]; + return NO; +} + +@end + +/* + c0 -> c1 (master) + |\ + | \-> cT (topic) + \ + \-> cF (origin/feature, no local branch) + + "conflict.txt" is different in every commit, so a local edit of it blocks any checkout from master + "staged.txt" is the same in every commit, so a local edit of it never blocks a checkout + "new.txt" only exists in cT +*/ +@interface GCLiveRepositoryUtilitiesTests : GCTestCase +@property(nonatomic) NSString* rootPath; +@property(nonatomic) GCLiveRepository* liveRepository; +@property(nonatomic) GCErrorRecordingWindow* window; +@property(nonatomic) NSMutableArray* pendingSheetAlerts; +@property(nonatomic) IMP originalBeginSheetImplementation; +@property(nonatomic) GCCommit* baseCommit; +@property(nonatomic) GCCommit* topicCommit; +@property(nonatomic) GCCommit* featureCommit; +@property(nonatomic) GCCommit* submoduleLocalBranchCommit; +@property(nonatomic) GCCommit* submoduleCommitWithoutBranches; +@property(nonatomic) GCCommit* submoduleRemoteBranchCommit; +@end + +@implementation GCLiveRepositoryUtilitiesTests + +- (void)setUp { + [super setUp]; + + // xctest has no application object, but GitUp always has one and NSAlert gives the Return key to its first button only then + [NSApplication sharedApplication]; + + [self startRecordingSheetAlerts]; + self.window = [[GCErrorRecordingWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 300) styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:YES]; + + self.rootPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]; + XCTAssertTrue([[NSFileManager defaultManager] createDirectoryAtPath:self.rootPath withIntermediateDirectories:YES attributes:nil error:NULL]); + self.liveRepository = [self createLiveRepositoryAtPath:[self.rootPath stringByAppendingPathComponent:@"repository"]]; + [self makeHistory]; +} + +- (void)tearDown { + [self stopRecordingSheetAlerts]; + self.pendingSheetAlerts = nil; // Their completion handlers keep the repository alive + self.window = nil; + self.liveRepository = nil; + [[NSFileManager defaultManager] removeItemAtPath:self.rootPath error:NULL]; + + [super tearDown]; +} + +#pragma mark - Tests + +- (void)testCheckoutBlockedByLocalChangesOffersToDiscardThem { + [self writeString:kLocalEdit toFile:@"conflict.txt"]; + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.topicCommit] window:self.window]; + + XCTAssertEqual(self.pendingSheetAlerts.count, 1); + NSAlert* alert = self.pendingSheetAlerts.firstObject.alert; + XCTAssertEqualObjects(alert.messageText, kLocalChangesAlertTitle); + NSMutableArray* buttonTitles = [[NSMutableArray alloc] init]; + for (NSButton* button in alert.buttons) { + [buttonTitles addObject:button.title]; + } + XCTAssertEqual(buttonTitles.count, 2); + XCTAssertEqualObjects([NSSet setWithArray:buttonTitles], ([NSSet setWithObjects:kDiscardButtonTitle, kCancelButtonTitle, nil])); + NSButton* discardButton = [self buttonWithTitle:kDiscardButtonTitle inAlert:alert]; + XCTAssertTrue(discardButton.hasDestructiveAction); // Drawn in red + XCTAssertNotEqualObjects(discardButton.keyEquivalent, @"\r"); // Return does not discard the changes + NSString* alertText = [NSString stringWithFormat:@"%@\n%@", alert.messageText, alert.informativeText]; + XCTAssertTrue([alertText localizedCaseInsensitiveContainsString:@"undo"]); // Warns that discarding cannot be undone +} + +- (void)testDiscardChangesAndContinueChecksOutBranchWithoutStagedOrUnstagedChanges { + [self writeString:kLocalEdit toFile:@"conflict.txt"]; + [self writeString:@"staged edit\n" toFile:@"staged.txt"]; + XCTAssertTrue([self.liveRepository addFileToIndex:@"staged.txt" error:NULL]); + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.topicCommit] window:self.window]; + [self clickButtonWithTitle:kDiscardButtonTitle]; + + XCTAssertEqualObjects([self headBranch].name, @"topic"); + XCTAssertEqualObjects([self contentsOfFile:@"conflict.txt"], @"topic\n"); + XCTAssertEqualObjects([self contentsOfFile:@"staged.txt"], @"same\n"); + NSError* error; + XCTAssertTrue([self.liveRepository checkClean:kGCCleanCheckOption_IgnoreUntrackedFiles error:&error], @"%@", error); // Nothing is left staged either +} + +- (void)testDiscardChangesAndContinueOverwritesUntrackedFileInTheWayAndKeepsOtherUntrackedFiles { + [self writeString:@"untracked file\n" toFile:@"new.txt"]; + [self writeString:@"untracked notes\n" toFile:@"notes.txt"]; // In no commit, so it is never in the way of a checkout + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.topicCommit] window:self.window]; + [self clickButtonWithTitle:kDiscardButtonTitle]; + + XCTAssertEqualObjects([self headBranch].name, @"topic"); + XCTAssertEqualObjects([self contentsOfFile:@"new.txt"], @"topic new\n"); + XCTAssertEqualObjects([self contentsOfFile:@"notes.txt"], @"untracked notes\n"); +} + +- (void)testDiscardChangesAndContinueChecksOutCommitWithDetachedHEAD { + // Commit without branches + [self writeString:kLocalEdit toFile:@"conflict.txt"]; + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.baseCommit] window:self.window]; + [self clickButtonWithTitle:kDiscardButtonTitle]; + + GCLocalBranch* branch; + XCTAssertEqualObjects([self.liveRepository lookupHEAD:&branch error:NULL], self.baseCommit); + XCTAssertNil(branch); + XCTAssertEqualObjects([self contentsOfFile:@"conflict.txt"], @"base\n"); + + // Tip of a remote branch, with "Checkout Commit" chosen instead of creating a local branch + [self writeString:kLocalEdit toFile:@"conflict.txt"]; + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.featureCommit] window:self.window]; + [self clickButtonWithTitle:kCheckoutCommitButtonTitle]; + [self clickButtonWithTitle:kDiscardButtonTitle]; + + XCTAssertEqualObjects([self.liveRepository lookupHEAD:&branch error:NULL], self.featureCommit); + XCTAssertNil(branch); + XCTAssertEqualObjects([self contentsOfFile:@"conflict.txt"], @"feature\n"); +} + +- (void)testDiscardChangesAndContinueCreatesLocalBranchTrackingRemoteBranch { + [self writeString:kLocalEdit toFile:@"conflict.txt"]; + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.featureCommit] window:self.window]; + [self clickButtonWithTitle:kCreateLocalBranchButtonTitle]; + [self clickButtonWithTitle:kDiscardButtonTitle]; + + GCLocalBranch* branch = [self headBranch]; + XCTAssertEqualObjects(branch.name, @"feature"); + XCTAssertEqualObjects([self.liveRepository lookupUpstreamForLocalBranch:branch error:NULL].name, @"origin/feature"); + XCTAssertEqualObjects([self contentsOfFile:@"conflict.txt"], @"feature\n"); +} + +- (void)testCancelKeepsLocalChangesAndCurrentBranch { + [self writeString:kLocalEdit toFile:@"conflict.txt"]; + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.topicCommit] window:self.window]; + [self clickButtonWithTitle:kCancelButtonTitle]; + + XCTAssertEqualObjects([self headBranch].name, @"master"); + XCTAssertEqualObjects([self contentsOfFile:@"conflict.txt"], kLocalEdit); + XCTAssertEqual(self.pendingSheetAlerts.count, 0); + XCTAssertEqual(self.window.presentedErrors.count, 0); +} + +- (void)testCheckoutErrorNotCausedByLocalChangesDoesNotOfferToDiscardThem { + [self writeString:kLocalEdit toFile:@"conflict.txt"]; + [self addWorktreeForBranchWithName:@"topic"]; // A branch checked out in another worktree cannot be checked out here, whatever the local changes + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.topicCommit] window:self.window]; + + XCTAssertEqual(self.pendingSheetAlerts.count, 0); + XCTAssertEqual(self.window.presentedErrors.count, 1); +} + +- (void)testCheckoutBlockedOnlyBySubmoduleChangesShowsErrorWithoutOfferingToDiscardThem { + [self makeHistoryWithSubmodule]; + [self writeString:kLocalEdit toFile:@"sub/file.txt"]; // The checkout does not look inside the submodule, so it moves HEAD and only the submodule update after it fails + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.submoduleLocalBranchCommit] window:self.window]; + [self assertCheckoutConflictErrorShownWithoutDiscardOffer:@"Local branch"]; + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.submoduleCommitWithoutBranches] window:self.window]; + [self assertCheckoutConflictErrorShownWithoutDiscardOffer:@"Commit without branches"]; + + [self.liveRepository smartCheckoutCommit:[self historyCommitForCommit:self.submoduleRemoteBranchCommit] window:self.window]; + [self clickButtonWithTitle:kCreateLocalBranchButtonTitle]; + [self assertCheckoutConflictErrorShownWithoutDiscardOffer:@"Create Local Branch"]; +} + +// The checkout error is shown as before, with only an OK button, instead of the alert offering to discard the local changes +- (void)assertCheckoutConflictErrorShownWithoutDiscardOffer:(NSString*)scenario { + XCTAssertEqual(self.pendingSheetAlerts.count, 0, @"%@", scenario); + XCTAssertEqual(self.window.presentedErrors.count, 1, @"%@", scenario); + NSError* error = self.window.presentedErrors.firstObject; + XCTAssertEqualObjects(error.domain, GCErrorDomain, @"%@", scenario); + XCTAssertEqual(error.code, kGCErrorCode_CheckoutConflicts, @"%@", scenario); // The local changes blocked the checkout, it did not fail for another reason + + // Next checkout starts with nothing shown + [self.pendingSheetAlerts removeAllObjects]; + self.window.presentedErrors = nil; +} + +#pragma mark - Repository + +- (GCLiveRepository*)createLiveRepositoryAtPath:(NSString*)path { + GCLiveRepository* repository = [[GCLiveRepository alloc] initWithNewLocalRepository:path bare:NO defaultBranchName:@"master" error:NULL]; + XCTAssertNotNil(repository); + [self useTestConfigForRepository:repository]; + return repository; +} + +// Only the repository config and a test identity apply, so the global git config of the machine cannot change checkouts or commits +- (void)useTestConfigForRepository:(GCRepository*)repository { + NSString* configPath = [self.rootPath stringByAppendingPathComponent:[repository.workingDirectoryPath.lastPathComponent stringByAppendingPathExtension:@"config"]]; + XCTAssertTrue([@"[user]\n\tname = Bot\n\temail = bot@example.com\n" writeToFile:configPath atomically:YES encoding:NSUTF8StringEncoding error:NULL]); + git_config* config; + XCTAssertEqual(git_config_new(&config), GIT_OK); + XCTAssertEqual(git_config_add_file_ondisk(config, [[repository.repositoryPath stringByAppendingPathComponent:@"config"] fileSystemRepresentation], GIT_CONFIG_LEVEL_LOCAL, repository.private, true), GIT_OK); + XCTAssertEqual(git_config_add_file_ondisk(config, configPath.fileSystemRepresentation, GIT_CONFIG_LEVEL_APP, repository.private, true), GIT_OK); + git_repository_set_config(repository.private, config); + git_config_free(config); +} + +- (void)makeHistory { + self.baseCommit = [self commitFiles:@{@"conflict.txt" : @"base\n", @"staged.txt" : @"same\n"} message:@"Base"]; + GCLocalBranch* masterBranch = [self headBranch]; + GCLocalBranch* topicBranch = [self.liveRepository createLocalBranchFromCommit:self.baseCommit withName:@"topic" force:NO error:NULL]; + GCLocalBranch* featureBranch = [self.liveRepository createLocalBranchFromCommit:self.baseCommit withName:@"feature" force:NO error:NULL]; + XCTAssertNotNil(topicBranch); + XCTAssertNotNil(featureBranch); + [self commitFiles:@{@"conflict.txt" : @"master\n"} message:@"Master"]; + + XCTAssertTrue([self.liveRepository checkoutLocalBranch:topicBranch options:(GCCheckoutOptions)0 error:NULL]); + self.topicCommit = [self commitFiles:@{@"conflict.txt" : @"topic\n", @"new.txt" : @"topic new\n"} message:@"Topic"]; + + XCTAssertTrue([self.liveRepository checkoutLocalBranch:featureBranch options:(GCCheckoutOptions)0 error:NULL]); + self.featureCommit = [self commitFiles:@{@"conflict.txt" : @"feature\n"} message:@"Feature"]; + + XCTAssertTrue([self.liveRepository checkoutLocalBranch:masterBranch options:(GCCheckoutOptions)0 error:NULL]); + + // Keep "feature" only as a remote branch + NSURL* remoteURL = [NSURL fileURLWithPath:[self.rootPath stringByAppendingPathComponent:@"origin.git"]]; + XCTAssertNotNil([self.liveRepository addRemoteWithName:@"origin" url:remoteURL error:NULL]); + XCTAssertNotNil([self.liveRepository createDirectReferenceWithFullName:@"refs/remotes/origin/feature" target:self.featureCommit force:NO error:NULL]); + XCTAssertTrue([self.liveRepository deleteLocalBranch:featureBranch error:NULL]); + + [self.liveRepository notifyRepositoryChanged]; // The live repository reloads its history only when notified +} + +/* + c1 -> cA (master, "sub" at s2) + \ + \-> cB ("sub" at s1) -> cC (subtopic) + \ + \-> cD (origin/subfeature, no local branch) + + "file.txt" of the submodule is different in s1 and s2, so a local edit of it blocks updating the submodule to s1 after a checkout from master +*/ +- (void)makeHistoryWithSubmodule { + // Cloned from a local repository, so no network is needed + NSString* originPath = [self.rootPath stringByAppendingPathComponent:@"submodule-origin"]; + GCRepository* origin = [[GCRepository alloc] initWithNewLocalRepository:originPath bare:NO defaultBranchName:@"master" error:NULL]; + XCTAssertNotNil(origin); + [self useTestConfigForRepository:origin]; + GCCommit* originCommit1 = [self commitFiles:@{@"file.txt" : @"s1\n"} message:@"s1" inRepository:origin]; + [self commitFiles:@{@"file.txt" : @"s2\n"} message:@"s2" inRepository:origin]; + + GCSubmodule* submodule = [self.liveRepository addSubmoduleWithURL:[NSURL fileURLWithPath:originPath] atPath:@"sub" recursive:NO error:NULL]; + XCTAssertNotNil(submodule); + GCCommit* masterCommit = [self.liveRepository createCommitFromHEADWithMessage:@"Add submodule" error:NULL]; + XCTAssertNotNil(masterCommit); + GCLocalBranch* masterBranch = [self headBranch]; + + GCLocalBranch* topicBranch = [self.liveRepository createLocalBranchFromCommit:masterCommit withName:@"subtopic" force:NO error:NULL]; + XCTAssertNotNil(topicBranch); + XCTAssertTrue([self.liveRepository checkoutLocalBranch:topicBranch options:(GCCheckoutOptions)0 error:NULL]); + GCRepository* submoduleRepository = [[GCRepository alloc] initWithSubmodule:submodule error:NULL]; + XCTAssertNotNil(submoduleRepository); + GCCommit* submoduleCommit1 = [submoduleRepository findCommitWithSHA1:originCommit1.SHA1 error:NULL]; + XCTAssertNotNil(submoduleCommit1); + XCTAssertTrue([submoduleRepository checkoutCommit:submoduleCommit1 options:(GCCheckoutOptions)0 error:NULL]); + XCTAssertTrue([self.liveRepository addSubmoduleToRepositoryIndex:submodule error:NULL]); + self.submoduleCommitWithoutBranches = [self.liveRepository createCommitFromHEADWithMessage:@"Submodule at s1" error:NULL]; + XCTAssertNotNil(self.submoduleCommitWithoutBranches); + self.submoduleLocalBranchCommit = [self commitFiles:@{@"subtopic.txt" : @"subtopic\n"} message:@"Subtopic"]; + + GCLocalBranch* featureBranch = [self.liveRepository createLocalBranchFromCommit:self.submoduleCommitWithoutBranches withName:@"subfeature" force:NO error:NULL]; + XCTAssertNotNil(featureBranch); + XCTAssertTrue([self.liveRepository checkoutLocalBranch:featureBranch options:(GCCheckoutOptions)0 error:NULL]); + self.submoduleRemoteBranchCommit = [self commitFiles:@{@"subfeature.txt" : @"subfeature\n"} message:@"Subfeature"]; + XCTAssertNotNil([self.liveRepository createDirectReferenceWithFullName:@"refs/remotes/origin/subfeature" target:self.submoduleRemoteBranchCommit force:NO error:NULL]); + + XCTAssertTrue([self.liveRepository checkoutLocalBranch:masterBranch options:kGCCheckoutOption_UpdateSubmodulesRecursively error:NULL]); // Also puts the submodule back to s2 + XCTAssertTrue([self.liveRepository deleteLocalBranch:featureBranch error:NULL]); + + [self.liveRepository notifyRepositoryChanged]; +} + +- (GCCommit*)commitFiles:(NSDictionary*)files message:(NSString*)message { + return [self commitFiles:files message:message inRepository:self.liveRepository]; +} + +- (GCCommit*)commitFiles:(NSDictionary*)files message:(NSString*)message inRepository:(GCRepository*)repository { + for (NSString* path in files) { + XCTAssertTrue([files[path] writeToFile:[repository.workingDirectoryPath stringByAppendingPathComponent:path] atomically:YES encoding:NSUTF8StringEncoding error:NULL]); + XCTAssertTrue([repository addFileToIndex:path error:NULL]); + } + GCCommit* commit = [repository createCommitFromHEADWithMessage:message error:NULL]; + XCTAssertNotNil(commit); + return commit; +} + +- (void)addWorktreeForBranchWithName:(NSString*)name { + GCLocalBranch* branch = [self.liveRepository findLocalBranchWithName:name error:NULL]; + XCTAssertNotNil(branch); + git_worktree_add_options options = GIT_WORKTREE_ADD_OPTIONS_INIT; + options.ref = branch.private; + git_worktree* worktree = NULL; + NSString* path = [self.rootPath stringByAppendingPathComponent:@"worktree"]; + XCTAssertEqual(git_worktree_add(&worktree, self.liveRepository.private, "worktree", path.fileSystemRepresentation, &options), GIT_OK); + git_worktree_free(worktree); +} + +- (GCHistoryCommit*)historyCommitForCommit:(GCCommit*)commit { + GCHistoryCommit* historyCommit = [self.liveRepository.history historyCommitForCommit:commit]; + XCTAssertNotNil(historyCommit); + return historyCommit; +} + +- (GCLocalBranch*)headBranch { + GCLocalBranch* branch; + XCTAssertNotNil([self.liveRepository lookupHEAD:&branch error:NULL]); + return branch; +} + +- (void)writeString:(NSString*)string toFile:(NSString*)path { + XCTAssertTrue([string writeToFile:[self.liveRepository.workingDirectoryPath stringByAppendingPathComponent:path] atomically:YES encoding:NSUTF8StringEncoding error:NULL]); +} + +- (NSString*)contentsOfFile:(NSString*)path { + return [NSString stringWithContentsOfFile:[self.liveRepository.workingDirectoryPath stringByAppendingPathComponent:path] encoding:NSUTF8StringEncoding error:NULL]; +} + +#pragma mark - Alerts + +// The code shows its alerts as sheets with -beginSheetModalForWindow:completionHandler:, so the test keeps them instead of showing them +- (void)startRecordingSheetAlerts { + NSMutableArray* pendingSheetAlerts = [[NSMutableArray alloc] init]; + self.pendingSheetAlerts = pendingSheetAlerts; + Method method = class_getInstanceMethod([NSAlert class], @selector(beginSheetModalForWindow:completionHandler:)); + IMP recordingImplementation = imp_implementationWithBlock(^(NSAlert* alert, NSWindow* sheetWindow, void (^completionHandler)(NSModalResponse returnCode)) { + (void)sheetWindow; + GCSheetAlert* sheetAlert = [[GCSheetAlert alloc] init]; + sheetAlert.alert = alert; + sheetAlert.completionHandler = completionHandler; + [pendingSheetAlerts addObject:sheetAlert]; + }); + self.originalBeginSheetImplementation = method_setImplementation(method, recordingImplementation); +} + +- (void)stopRecordingSheetAlerts { + Method method = class_getInstanceMethod([NSAlert class], @selector(beginSheetModalForWindow:completionHandler:)); + IMP recordingImplementation = method_setImplementation(method, self.originalBeginSheetImplementation); + imp_removeBlock(recordingImplementation); +} + +// Clicks a button of the oldest alert still shown: NSAlert ends its sheet with the tag of the clicked button as the return code +- (void)clickButtonWithTitle:(NSString*)title { + GCSheetAlert* sheetAlert = self.pendingSheetAlerts.firstObject; + if (sheetAlert == nil) { + XCTFail(@"No alert is shown to click \"%@\"", title); + return; + } + [self.pendingSheetAlerts removeObjectAtIndex:0]; + NSButton* button = [self buttonWithTitle:title inAlert:sheetAlert.alert]; + if (button == nil) { + XCTFail(@"No \"%@\" button in the alert \"%@\"", title, sheetAlert.alert.messageText); + return; + } + sheetAlert.completionHandler(button.tag); +} + +- (NSButton*)buttonWithTitle:(NSString*)title inAlert:(NSAlert*)alert { + for (NSButton* button in alert.buttons) { + if ([button.title isEqualToString:title]) { + return button; + } + } + return nil; +} + +@end diff --git a/GitUpKit/Extensions/GCLiveRepository+Utilities.m b/GitUpKit/Extensions/GCLiveRepository+Utilities.m index 06d22b7b..f85f5bb6 100644 --- a/GitUpKit/Extensions/GCLiveRepository+Utilities.m +++ b/GitUpKit/Extensions/GCLiveRepository+Utilities.m @@ -16,7 +16,7 @@ - (void)smartCheckoutCommit:(GCHistoryCommit*)commit window:(NSWindow*)window { } id target = [self smartCheckoutTarget:commit]; if ([target isKindOfClass:[GCLocalBranch class]]) { - [self _checkoutLocalBranch:target window:window]; + [self _checkoutLocalBranch:target force:NO window:window]; } else { GCHistoryRemoteBranch* branch = commit.remoteBranches.firstObject; if (branch && ![self.history historyLocalBranchWithName:branch.branchName]) { @@ -32,18 +32,56 @@ - (void)smartCheckoutCommit:(GCHistoryCommit*)commit window:(NSWindow*)window { if (returnCode == NSAlertFirstButtonReturn) { [self checkoutRemoteBranch:branch window:window]; } else if (returnCode == NSAlertSecondButtonReturn) { - [self _checkoutCommit:target window:window]; + [self _checkoutCommit:target force:NO window:window]; } }]; } else { - [self _checkoutCommit:target window:window]; + [self _checkoutCommit:target force:NO window:window]; } } } -// This will abort on conflicts in workdir or index so there's no need to require a clean repo +// Full name of the branch HEAD points to or SHA1 of the commit if HEAD is detached +- (NSString*)_HEADTarget { + GCLocalBranch* branch; + GCCommit* commit = [self lookupHEAD:&branch error:NULL]; + return branch ? branch.fullName : commit.SHA1; +} + +// Checkout aborts when local changes would be overwritten so offer to discard them and checkout again like "git checkout -f", unless HEAD has moved: submodules are updated after moving it so the retry could not be undone back to the previous HEAD +- (void)_presentCheckoutError:(NSError*)error previousHEADTarget:(NSString*)previousHEADTarget window:(NSWindow*)window discardChangesBlock:(dispatch_block_t)block { + if (!block || ![error.domain isEqualToString:GCErrorDomain] || (error.code != kGCErrorCode_CheckoutConflicts) || ![[self _HEADTarget] isEqualToString:previousHEADTarget]) { + [window presentError:error]; + return; + } + NSAlert* alert = [[NSAlert alloc] init]; + alert.type = kGIAlertType_Danger; + alert.messageText = NSLocalizedString(@"Local changes would be overwritten by checkout", nil); + alert.informativeText = NSLocalizedString(@"Discarding them reverts every uncommitted change to tracked files, including in submodules, and overwrites untracked files that are in the way. Other untracked files are kept.\n\nThis action cannot be undone.", nil); + NSButton* discardButton = [alert addButtonWithTitle:NSLocalizedString(@"Discard Changes and Continue", nil)]; + discardButton.keyEquivalent = @""; + discardButton.hasDestructiveAction = YES; + [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)]; + [alert beginSheetModalForWindow:window + completionHandler:^(NSModalResponse returnCode) { + if (returnCode == NSAlertFirstButtonReturn) { + block(); + } + }]; +} + - (void)checkoutRemoteBranch:(GCHistoryRemoteBranch*)remoteBranch window:(NSWindow*)window { + [self _checkoutRemoteBranch:remoteBranch force:NO window:window]; +} + +// This will abort on conflicts in workdir or index so there's no need to require a clean repo +- (void)_checkoutRemoteBranch:(GCHistoryRemoteBranch*)remoteBranch force:(BOOL)force window:(NSWindow*)window { NSError* error; + GCCheckoutOptions options = kGCCheckoutOption_UpdateSubmodulesRecursively; + if (force) { + options |= kGCCheckoutOption_Force; + } + NSString* HEADTarget = [self _HEADTarget]; [self setUndoActionName:[NSString stringWithFormat:NSLocalizedString(@"Checkout Remote Branch \"%@\"", nil), remoteBranch.name]]; if (![self performOperationWithReason:@"checkout_remote_branch" argument:remoteBranch.name @@ -57,43 +95,68 @@ - (void)checkoutRemoteBranch:(GCHistoryRemoteBranch*)remoteBranch window:(NSWind if (![repository setUpstream:remoteBranch forLocalBranch:localBranch error:outError]) { return NO; } - if (![repository checkoutLocalBranch:localBranch options:kGCCheckoutOption_UpdateSubmodulesRecursively error:outError]) { + if (![repository checkoutLocalBranch:localBranch options:options error:outError]) { [repository deleteLocalBranch:localBranch error:NULL]; // Ignore errors return NO; } return YES; }]) { - [window presentError:error]; + [self _presentCheckoutError:error + previousHEADTarget:HEADTarget + window:window + discardChangesBlock:(force ? nil : ^{ + [self _checkoutRemoteBranch:remoteBranch force:YES window:window]; + })]; } } // This will preemptively abort on conflicts in workdir or index so there's no need to require a clean repo -- (void)_checkoutLocalBranch:(GCHistoryLocalBranch*)branch window:(NSWindow*)window { +- (void)_checkoutLocalBranch:(GCHistoryLocalBranch*)branch force:(BOOL)force window:(NSWindow*)window { NSError* error; + GCCheckoutOptions options = kGCCheckoutOption_UpdateSubmodulesRecursively; + if (force) { + options |= kGCCheckoutOption_Force; + } + NSString* HEADTarget = [self _HEADTarget]; [self setUndoActionName:[NSString stringWithFormat:NSLocalizedString(@"Checkout Branch \"%@\"", nil), branch.name]]; if (![self performOperationWithReason:@"checkout_branch" argument:branch.name skipCheckoutOnUndo:NO error:&error usingBlock:^BOOL(GCLiveRepository* repository, NSError** outError) { - return [repository checkoutLocalBranch:branch options:kGCCheckoutOption_UpdateSubmodulesRecursively error:outError]; + return [repository checkoutLocalBranch:branch options:options error:outError]; }]) { - [window presentError:error]; + [self _presentCheckoutError:error + previousHEADTarget:HEADTarget + window:window + discardChangesBlock:(force ? nil : ^{ + [self _checkoutLocalBranch:branch force:YES window:window]; + })]; } } // This will preemptively abort on conflicts in workdir or index so there's no need to require a clean repo -- (void)_checkoutCommit:(GCHistoryCommit*)commit window:(NSWindow*)window { +- (void)_checkoutCommit:(GCHistoryCommit*)commit force:(BOOL)force window:(NSWindow*)window { NSError* error; + GCCheckoutOptions options = kGCCheckoutOption_UpdateSubmodulesRecursively; + if (force) { + options |= kGCCheckoutOption_Force; + } + NSString* HEADTarget = [self _HEADTarget]; [self setUndoActionName:NSLocalizedString(@"Checkout Commit", nil)]; if (![self performOperationWithReason:@"checkout_commit" argument:commit.SHA1 skipCheckoutOnUndo:NO error:&error usingBlock:^BOOL(GCLiveRepository* repository, NSError** outError) { - return [repository checkoutCommit:commit options:kGCCheckoutOption_UpdateSubmodulesRecursively error:outError]; + return [repository checkoutCommit:commit options:options error:outError]; }]) { - [window presentError:error]; + [self _presentCheckoutError:error + previousHEADTarget:HEADTarget + window:window + discardChangesBlock:(force ? nil : ^{ + [self _checkoutCommit:commit force:YES window:window]; + })]; } } diff --git a/GitUpKit/Extensions/TestHeaders/GitUpKit/GCLiveRepository+Utilities.h b/GitUpKit/Extensions/TestHeaders/GitUpKit/GCLiveRepository+Utilities.h new file mode 100644 index 00000000..e0bb8f22 --- /dev/null +++ b/GitUpKit/Extensions/TestHeaders/GitUpKit/GCLiveRepository+Utilities.h @@ -0,0 +1,10 @@ +// +// GCLiveRepository+Utilities.h +// Tests +// + +// Used only by the Tests target to build GCLiveRepository+Utilities.m: its header is not public in GitUpKit and that target builds without the AppKit part of GitUpKit.h +#import +#import + +#import "../../GCLiveRepository+Utilities.h" diff --git a/GitUpKit/GitUpKit.xcodeproj/project.pbxproj b/GitUpKit/GitUpKit.xcodeproj/project.pbxproj index 04e8bb89..44f01b21 100644 --- a/GitUpKit/GitUpKit.xcodeproj/project.pbxproj +++ b/GitUpKit/GitUpKit.xcodeproj/project.pbxproj @@ -34,6 +34,7 @@ 1DADC0E225A25D63008C2C35 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = DBBEE64C256B094000F96DAF /* libz.tbd */; }; 1DF371CD22F5262300EF7BD9 /* GCLiveRepository+Utilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 1DF371CB22F5262300EF7BD9 /* GCLiveRepository+Utilities.h */; }; 1DF371CE22F5262300EF7BD9 /* GCLiveRepository+Utilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 1DF371CC22F5262300EF7BD9 /* GCLiveRepository+Utilities.m */; }; + 447CD596AEDB4B7794B35C28 /* GIAppKit.m in Sources */ = {isa = PBXBuildFile; fileRef = E22941D41AC11E31000A83AF /* GIAppKit.m */; }; 6D8E3F0B25D90E1300AAFC17 /* GIImageDiffView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6D8E3F0A25D90E1300AAFC17 /* GIImageDiffView.m */; }; 6D8E3F1025D90E3400AAFC17 /* GIImageDiffView.h in Headers */ = {isa = PBXBuildFile; fileRef = 6D8E3F0F25D90E3400AAFC17 /* GIImageDiffView.h */; settings = {ATTRIBUTES = (Public, ); }; }; 743BF1841B871C0200E1CA49 /* GCOrderedSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 74EDB5D01B84D4C400F00E79 /* GCOrderedSet.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -61,6 +62,7 @@ DC040FCB2BCB21D000DF54D5 /* GCLiveRepository+Conflicts.h in Headers */ = {isa = PBXBuildFile; fileRef = DC040FC82BCB21D000DF54D5 /* GCLiveRepository+Conflicts.h */; settings = {ATTRIBUTES = (Public, ); }; }; DC040FCC2BCB22FC00DF54D5 /* GCLiveRepository+Conflicts.m in Sources */ = {isa = PBXBuildFile; fileRef = DC040FC92BCB21D000DF54D5 /* GCLiveRepository+Conflicts.m */; }; DC040FCD2BCB417A00DF54D5 /* GCLiveRepository+Conflicts.m in Sources */ = {isa = PBXBuildFile; fileRef = DC040FC92BCB21D000DF54D5 /* GCLiveRepository+Conflicts.m */; }; + DC7BB8520B0E4EFD995AF4EF /* GCLiveRepository+Utilities-Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3713F8DD4AE14FA9A3083EEE /* GCLiveRepository+Utilities-Tests.m */; }; E200A3BA1B02DDA100C4E39D /* GCPrivate.m in Sources */ = {isa = PBXBuildFile; fileRef = E200A3B81B02DDA100C4E39D /* GCPrivate.m */; }; E20EB08C19FC75CA0031A075 /* GCRepository+Reset.m in Sources */ = {isa = PBXBuildFile; fileRef = E20EB08A19FC75CA0031A075 /* GCRepository+Reset.m */; }; E20EB09019FC76160031A075 /* GCRepository+Status.m in Sources */ = {isa = PBXBuildFile; fileRef = E20EB08E19FC76160031A075 /* GCRepository+Status.m */; }; @@ -363,6 +365,7 @@ E2F5C2831A81C53A00C30739 /* GCRepository+Reflog-Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2F5C2821A81C53A00C30739 /* GCRepository+Reflog-Tests.m */; }; E2FEED491AEAA6B500CBED80 /* GCCommitDatabase-Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = E2FEED481AEAA6B500CBED80 /* GCCommitDatabase-Tests.m */; }; E2FEED4A1AEAA75F00CBED80 /* GCCommitDatabase.m in Sources */ = {isa = PBXBuildFile; fileRef = E2FEED451AEAA6AD00CBED80 /* GCCommitDatabase.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + EC48BBACE0324E4EB1CF506A /* GCLiveRepository+Utilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 1DF371CC22F5262300EF7BD9 /* GCLiveRepository+Utilities.m */; settings = {COMPILER_FLAGS = "-I$(SRCROOT)/Extensions/TestHeaders"; }; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -381,6 +384,7 @@ 1D841F772B955C3800E4FCCC /* GIRemappingExplanationViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = GIRemappingExplanationViewController.xib; sourceTree = ""; }; 1DF371CB22F5262300EF7BD9 /* GCLiveRepository+Utilities.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "GCLiveRepository+Utilities.h"; sourceTree = ""; }; 1DF371CC22F5262300EF7BD9 /* GCLiveRepository+Utilities.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "GCLiveRepository+Utilities.m"; sourceTree = ""; }; + 3713F8DD4AE14FA9A3083EEE /* GCLiveRepository+Utilities-Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "GCLiveRepository+Utilities-Tests.m"; sourceTree = ""; }; 6D8E3F0A25D90E1300AAFC17 /* GIImageDiffView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GIImageDiffView.m; sourceTree = ""; }; 6D8E3F0F25D90E3400AAFC17 /* GIImageDiffView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GIImageDiffView.h; sourceTree = ""; }; 74EDB5D01B84D4C400F00E79 /* GCOrderedSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GCOrderedSet.h; sourceTree = ""; }; @@ -783,6 +787,7 @@ E259C2D61A64FAEA0079616B /* GCRepository+Utilities-Tests.m */, E218A58C1A56706600DFF1DF /* GCRepository+Utilities.h */, E218A58D1A56706600DFF1DF /* GCRepository+Utilities.m */, + 3713F8DD4AE14FA9A3083EEE /* GCLiveRepository+Utilities-Tests.m */, 1DF371CB22F5262300EF7BD9 /* GCLiveRepository+Utilities.h */, 1DF371CC22F5262300EF7BD9 /* GCLiveRepository+Utilities.m */, ); @@ -1474,6 +1479,9 @@ E27E43061A74A96000D04ED1 /* GILayer.m in Sources */, E2C338F219F85C8600063D95 /* GCRemote.m in Sources */, E21739F41A4FE39E00EC6777 /* GCFunctions.m in Sources */, + EC48BBACE0324E4EB1CF506A /* GCLiveRepository+Utilities.m in Sources */, + 447CD596AEDB4B7794B35C28 /* GIAppKit.m in Sources */, + DC7BB8520B0E4EFD995AF4EF /* GCLiveRepository+Utilities-Tests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };