mirror of
https://github.com/kennethreitz-archive/gitx.git
synced 2026-06-05 23:40:18 +00:00
Merge branch 'pu/mb/add_gitignore'
* pu/mb/add_gitignore: CommitView: Add option to add files to .gitignore
This commit is contained in:
+67
-1
@@ -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
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user