Skip to content

Commit

Permalink
CommitView: Add option to add files to .gitignore
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mark Bestley authored and pieter committed Dec 22, 2008
1 parent cbf2608 commit fc00f6c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
68 changes: 67 additions & 1 deletion PBGitIndexController.m
Expand Up @@ -8,6 +8,7 @@

#import "PBGitIndexController.h"
#import "PBChangedFile.h"
#import "PBGitRepository.h"

#define FileChangesTableViewType @"GitFileChangedType"

Expand Down Expand Up @@ -76,6 +77,43 @@ - (void) unstageFiles:(NSArray *)files
}
}

- (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
Expand Down Expand Up @@ -124,6 +162,16 @@ - (NSString *)unstagedChangesForFile:(PBChangedFile *)file


# 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];
Expand All @@ -150,7 +198,15 @@ - (NSMenu *) menuForTable:(NSTableView *)table
[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;
Expand Down Expand Up @@ -187,6 +243,16 @@ - (void) openFilesAction:(id) sender
[[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
{
Expand Down
1 change: 1 addition & 0 deletions PBGitRepository.h
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions PBGitRepository.m
Expand Up @@ -155,6 +155,13 @@ - (NSString*)displayName
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
{
Expand Down

0 comments on commit fc00f6c

Please sign in to comment.