diff --git a/PBGitHistoryController.m b/PBGitHistoryController.m index 541dcf6..0f6f578 100644 --- a/PBGitHistoryController.m +++ b/PBGitHistoryController.m @@ -266,6 +266,15 @@ } } +- (void) checkoutFiles:(id)sender +{ + NSMutableArray *files = [NSMutableArray array]; + for (NSString *filePath in [sender representedObject]) + [files addObject:[filePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]]; + + [repository checkoutFiles:files fromRefish:realCommit]; +} + - (NSMenu *)contextMenuForTreeView { @@ -286,7 +295,10 @@ BOOL multiple = [filePaths count] != 1; NSMenuItem *historyItem = [[NSMenuItem alloc] initWithTitle:multiple? @"Show history of files" : @"Show history of file" action:@selector(showCommitsFromTree:) - keyEquivalent:@""]; + keyEquivalent:@""]; + NSMenuItem *checkoutItem = [[NSMenuItem alloc] initWithTitle:multiple ? @"Checkout files" : @"Checkout file" + action:@selector(checkoutFiles:) + keyEquivalent:@""]; NSMenuItem *finderItem = [[NSMenuItem alloc] initWithTitle:@"Show in Finder" action:@selector(showInFinderAction:) keyEquivalent:@""]; @@ -294,7 +306,7 @@ action:@selector(openFilesAction:) keyEquivalent:@""]; - NSArray *menuItems = [NSArray arrayWithObjects:historyItem, finderItem, openFilesItem, nil]; + NSArray *menuItems = [NSArray arrayWithObjects:historyItem, checkoutItem, finderItem, openFilesItem, nil]; for (NSMenuItem *item in menuItems) { [item setTarget:self]; [item setRepresentedObject:filePaths]; diff --git a/PBGitRepository.h b/PBGitRepository.h index fc89755..18294d5 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -30,6 +30,7 @@ extern NSString* PBGitRepositoryErrorDomain; } - (BOOL) checkoutRefish:(id )ref; +- (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id )ref; - (BOOL) createBranch:(NSString *)branchName atRefish:(id )ref; - (BOOL) createTag:(NSString *)tagName message:(NSString *)message atRefish:(id )commitSHA; diff --git a/PBGitRepository.m b/PBGitRepository.m index ab0fa04..4d5413a 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -450,6 +450,30 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; return YES; } +- (BOOL) checkoutFiles:(NSArray *)files fromRefish:(id )ref +{ + if (!files || ([files count] == 0)) + return NO; + + NSString *refName = nil; + if ([ref refishType] == kGitXBranchType) + refName = [ref shortName]; + else + refName = [ref refishName]; + + int retValue = 1; + NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"checkout", refName, @"--", nil]; + [arguments addObjectsFromArray:files]; + NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue]; + if (retValue) { + NSString *message = [NSString stringWithFormat:@"There was an error checking out the file(s) from the %@ '%@'.\n\nPerhaps your working directory is not clean?", [ref refishType], [ref shortName]]; + [self.windowController showErrorSheetTitle:@"Checkout failed!" message:message arguments:arguments output:output]; + return NO; + } + + return YES; +} + - (BOOL) createBranch:(NSString *)branchName atRefish:(id )ref { if (!branchName || !ref)