From fc00f6c087a31a827adb43dfe671aa9db7041286 Mon Sep 17 00:00:00 2001 From: Mark Bestley Date: Mon, 1 Dec 2008 17:51:35 +0000 Subject: [PATCH] CommitView: Add option to add files to .gitignore In the commit view, add an option in the context menu to have git ignore them by adding the file names to .gitignore file in the root of the workarea. --- PBGitIndexController.m | 68 +++++++++++++++++++++++++++++++++++++++++- PBGitRepository.h | 1 + PBGitRepository.m | 7 +++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/PBGitIndexController.m b/PBGitIndexController.m index 45ca2a7..e0b72a7 100644 --- a/PBGitIndexController.m +++ b/PBGitIndexController.m @@ -8,6 +8,7 @@ #import "PBGitIndexController.h" #import "PBChangedFile.h" +#import "PBGitRepository.h" #define FileChangesTableViewType @"GitFileChangedType" @@ -76,6 +77,43 @@ } } +- (void) ignoreFiles:(NSArray *)files +{ + // Build output string + NSMutableArray *fileList = [NSMutableArray array]; + for (PBChangedFile *file in files) { + NSString *name = file.path; + if ([name length] > 0) + [fileList addObject:name]; + } + NSString *filesAsString = [fileList componentsJoinedByString:@"\n"]; + + // Write to the file + NSString *gitIgnoreName = [commitController.repository gitIgnoreFilename]; + + NSStringEncoding enc; + NSError *error = nil; + NSMutableString *ignoreFile; + + if (![[NSFileManager defaultManager] fileExistsAtPath:gitIgnoreName]) { + ignoreFile = [filesAsString mutableCopy]; + } else { + ignoreFile = [NSMutableString stringWithContentsOfFile:gitIgnoreName usedEncoding:&enc error:&error]; + if (error) { + [[NSAlert alertWithError:error] runModal]; + return; + } + // Add a newline if not yet present + if ([ignoreFile characterAtIndex:([ignoreFile length] - 1)] != '\n') + [ignoreFile appendString:@"\n"]; + [ignoreFile appendString:filesAsString]; + } + + [ignoreFile writeToFile:gitIgnoreName atomically:YES encoding:enc error:&error]; + if (error) + [[NSAlert alertWithError:error] runModal]; +} + # pragma mark Displaying diffs - (NSString *) stagedChangesForFile:(PBChangedFile *)file @@ -124,6 +162,16 @@ # pragma mark Context Menu methods +- (BOOL) allSelectedCanBeIgnored:(NSArray *)selectedFiles +{ + for (PBChangedFile *selectedItem in selectedFiles) { + if (selectedItem.status != NEW) { + return NO; + } + } + return YES; +} + - (NSMenu *) menuForTable:(NSTableView *)table { NSMenu *menu = [[NSMenu alloc] init]; @@ -150,7 +198,15 @@ [openItem setRepresentedObject:selectedFiles]; [menu addItem:openItem]; - + // Attempt to ignore + if ([self allSelectedCanBeIgnored:selectedFiles]) { + NSString *ignoreText = [selectedFiles count] == 1 ? @"Ignore File": @"Ignore Files"; + NSMenuItem *ignoreItem = [[NSMenuItem alloc] initWithTitle:ignoreText action:@selector(ignoreFilesAction:) keyEquivalent:@""]; + [ignoreItem setTarget:self]; + [ignoreItem setRepresentedObject:selectedFiles]; + [menu addItem:ignoreItem]; + } + // Do not add "revert" options for untracked files // if (selectedItem.status == NEW) // return a; @@ -187,6 +243,16 @@ [[NSWorkspace sharedWorkspace] openFile:[workingDirectory stringByAppendingPathComponent:[file path]]]; } +- (void) ignoreFilesAction:(id) sender +{ + NSArray *selectedFiles = [sender representedObject]; + if ([selectedFiles count] > 0) { + [self ignoreFiles:selectedFiles]; + } + [commitController refresh:NULL]; +} + + # pragma mark TableView icon delegate - (void)tableView:(NSTableView*)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn row:(int)rowIndex { diff --git a/PBGitRepository.h b/PBGitRepository.h index 04aa5b0..da87e86 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -37,6 +37,7 @@ extern NSString* PBGitRepositoryErrorDomain; - (NSString *)outputInWorkdirForArguments:(NSArray*) arguments retValue:(int *)ret; - (NSString *)workingDirectory; +- (NSString *)gitIgnoreFilename; - (BOOL) reloadRefs; - (void) addRef:(PBGitRef *)ref fromParameters:(NSArray *)params; diff --git a/PBGitRepository.m b/PBGitRepository.m index a5d3a92..d526a59 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -155,6 +155,13 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; return displayName; } +// Get the .gitignore file at the root of the repository +- (NSString*)gitIgnoreFilename +{ + NSString *dir = [self workingDirectory]; + return [dir stringByAppendingString:@"/.gitignore"]; +} + // Overridden to create our custom window controller - (void)makeWindowControllers {