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.
This commit is contained in:
Mark Bestley
2008-12-01 17:51:35 +00:00
committed by Pieter de Bie
parent cbf2608dfa
commit fc00f6c087
3 changed files with 75 additions and 1 deletions
+67 -1
View File
@@ -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
{
+1
View File
@@ -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;
+7
View File
@@ -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
{