Skip to content

Commit 18376bb

Browse files
committed
Merge pull request #184 from jphalip/master
Context menu item for deleting files
2 parents bab7599 + 14e30fc commit 18376bb

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

PBGitIndexController.m

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#import "PBChangedFile.h"
1111
#import "PBGitRepository.h"
1212
#import "PBGitIndex.h"
13+
#import "PBGitDefaults.h"
1314

1415
#define FileChangesTableViewType @"GitFileChangedType"
16+
#define kDialogDeleteFiles @"Delete Files"
1517

1618
@interface PBGitIndexController ()
1719
- (void)discardChangesForFiles:(NSArray *)files force:(BOOL)force;
@@ -105,12 +107,18 @@ - (NSMenu *) menuForTable:(NSTableView *)table
105107
[menu addItem:unstageItem];
106108
}
107109

108-
NSString *title = [selectedFiles count] == 1 ? @"Open file" : @"Open files";
109-
NSMenuItem *openItem = [[NSMenuItem alloc] initWithTitle:title action:@selector(openFilesAction:) keyEquivalent:@""];
110+
NSString *openTitle = [selectedFiles count] == 1 ? @"Open file" : @"Open files";
111+
NSMenuItem *openItem = [[NSMenuItem alloc] initWithTitle:openTitle action:@selector(openFilesAction:) keyEquivalent:@""];
110112
[openItem setTarget:self];
111113
[openItem setRepresentedObject:selectedFiles];
112114
[menu addItem:openItem];
113115

116+
NSString *deleteTitle = [selectedFiles count] == 1 ? @"Delete file…" : @"Delete files…";
117+
NSMenuItem *deleteItem = [[NSMenuItem alloc] initWithTitle:deleteTitle action:@selector(showDeleteFilesSheet:) keyEquivalent:@""];
118+
[deleteItem setTarget:self];
119+
[deleteItem setRepresentedObject:selectedFiles];
120+
[menu addItem:deleteItem];
121+
114122
// Attempt to ignore
115123
if ([self allSelectedCanBeIgnored:selectedFiles]) {
116124
NSString *ignoreText = [selectedFiles count] == 1 ? @"Ignore File": @"Ignore Files";
@@ -178,6 +186,60 @@ - (void) openFilesAction:(id) sender
178186
[[NSWorkspace sharedWorkspace] openFile:[workingDirectory stringByAppendingPathComponent:[file path]]];
179187
}
180188

189+
- (void) doDeleteFiles:(NSArray *)files {
190+
NSString *workingDirectory = [[commitController.repository workingDirectory] stringByAppendingString:@"/"];
191+
NSString *path;
192+
NSArray *filenamesArray;
193+
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
194+
195+
for (PBChangedFile *file in files) {
196+
path = [workingDirectory stringByAppendingPathComponent:[file path]];
197+
filenamesArray = [NSArray arrayWithObject:[path lastPathComponent]];
198+
[ws performFileOperation:NSWorkspaceRecycleOperation source:[path stringByDeletingLastPathComponent] destination:@"" files:filenamesArray tag:nil];
199+
}
200+
[commitController.index refresh];
201+
}
202+
203+
- (void)showDeleteFilesSheet:(id)sender
204+
{
205+
NSArray *files = [sender representedObject];
206+
NSMutableString *filesPathsString = [[NSMutableString alloc] init];
207+
208+
for (PBChangedFile *file in files) {
209+
[filesPathsString appendString:[file path]];
210+
[filesPathsString appendString:@"\n"];
211+
}
212+
213+
if ([PBGitDefaults isDialogWarningSuppressedForDialog:kDialogDeleteFiles]) {
214+
[self doDeleteFiles:files];
215+
return;
216+
}
217+
218+
NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:@"Delete files?"]
219+
defaultButton:@"Delete"
220+
alternateButton:@"Cancel"
221+
otherButton:nil
222+
informativeTextWithFormat:@"Are you sure you want to move the following files to the trash?\n\n%@\nYou cannot undo this operation.", filesPathsString];
223+
[alert setShowsSuppressionButton:YES];
224+
225+
[alert beginSheetModalForWindow:[[commitController view] window]
226+
modalDelegate:self
227+
didEndSelector:@selector(deleteFilesSheetDidEnd:returnCode:contextInfo:)
228+
contextInfo:(__bridge void *) files];
229+
}
230+
231+
- (void)deleteFilesSheetDidEnd:(NSAlert *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
232+
{
233+
if ([[sheet suppressionButton] state] == NSOnState)
234+
[PBGitDefaults suppressDialogWarningForDialog:kDialogDeleteFiles];
235+
236+
NSArray *files = (__bridge NSArray*) contextInfo;
237+
238+
if (returnCode == NSAlertDefaultReturn) {
239+
[self doDeleteFiles:files];
240+
}
241+
}
242+
181243
- (void) ignoreFilesAction:(id) sender
182244
{
183245
NSArray *selectedFiles = [sender representedObject];

0 commit comments

Comments
 (0)