-
Notifications
You must be signed in to change notification settings - Fork 602
/
PBGitIndexController.m
297 lines (248 loc) · 9.92 KB
/
PBGitIndexController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
// PBGitIndexController.m
// GitX
//
// Created by Pieter de Bie on 18-11-08.
// Copyright 2008 Pieter de Bie. All rights reserved.
//
#import "PBGitIndexController.h"
#import "PBChangedFile.h"
#import "PBGitRepository.h"
#import "PBGitIndex.h"
#define FileChangesTableViewType @"GitFileChangedType"
@interface PBGitIndexController ()
- (void)discardChangesForFiles:(NSArray *)files force:(BOOL)force;
@end
@implementation PBGitIndexController
- (void)awakeFromNib
{
[unstagedTable setDoubleAction:@selector(tableClicked:)];
[stagedTable setDoubleAction:@selector(tableClicked:)];
[unstagedTable setTarget:self];
[stagedTable setTarget:self];
[unstagedTable registerForDraggedTypes: [NSArray arrayWithObject:FileChangesTableViewType]];
[stagedTable registerForDraggedTypes: [NSArray arrayWithObject:FileChangesTableViewType]];
}
// FIXME: Find a proper place for this method -- this is not it.
- (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 = NSUTF8StringEncoding;
NSError *error = nil;
NSMutableString *ignoreFile;
if (![[NSFileManager defaultManager] fileExistsAtPath:gitIgnoreName]) {
ignoreFile = [filesAsString mutableCopy];
} else {
ignoreFile = [NSMutableString stringWithContentsOfFile:gitIgnoreName usedEncoding:&enc error:&error];
if (error) {
[[commitController.repository windowController] showErrorSheet:error];
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)
[[commitController.repository windowController] showErrorSheet:error];
}
# 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];
id controller = [table tag] == 0 ? unstagedFilesController : stagedFilesController;
NSArray *selectedFiles = [controller selectedObjects];
// Unstaged changes
if ([table tag] == 0) {
NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:@"Stage Changes" action:@selector(stageFilesAction:) keyEquivalent:@""];
[stageItem setTarget:self];
[stageItem setRepresentedObject:selectedFiles];
[menu addItem:stageItem];
}
else if ([table tag] == 1) {
NSMenuItem *unstageItem = [[NSMenuItem alloc] initWithTitle:@"Unstage Changes" action:@selector(unstageFilesAction:) keyEquivalent:@""];
[unstageItem setTarget:self];
[unstageItem setRepresentedObject:selectedFiles];
[menu addItem:unstageItem];
}
NSString *title = [selectedFiles count] == 1 ? @"Open file" : @"Open files";
NSMenuItem *openItem = [[NSMenuItem alloc] initWithTitle:title action:@selector(openFilesAction:) keyEquivalent:@""];
[openItem setTarget:self];
[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];
}
if ([selectedFiles count] == 1) {
NSMenuItem *showInFinderItem = [[NSMenuItem alloc] initWithTitle:@"Show in Finder" action:@selector(showInFinderAction:) keyEquivalent:@""];
[showInFinderItem setTarget:self];
[showInFinderItem setRepresentedObject:selectedFiles];
[menu addItem:showInFinderItem];
}
for (PBChangedFile *file in selectedFiles)
if (!file.hasUnstagedChanges)
return menu;
NSMenuItem *discardItem = [[NSMenuItem alloc] initWithTitle:@"Discard changes…" action:@selector(discardFilesAction:) keyEquivalent:@""];
[discardItem setTarget:self];
[discardItem setAlternate:NO];
[discardItem setRepresentedObject:selectedFiles];
[menu addItem:discardItem];
NSMenuItem *discardForceItem = [[NSMenuItem alloc] initWithTitle:@"Discard changes" action:@selector(forceDiscardFilesAction:) keyEquivalent:@""];
[discardForceItem setTarget:self];
[discardForceItem setAlternate:YES];
[discardForceItem setRepresentedObject:selectedFiles];
[discardForceItem setKeyEquivalentModifierMask:NSAlternateKeyMask];
[menu addItem:discardForceItem];
return menu;
}
- (void) stageFilesAction:(id) sender
{
[commitController.index stageFiles:[sender representedObject]];
}
- (void) unstageFilesAction:(id) sender
{
[commitController.index unstageFiles:[sender representedObject]];
}
- (void) openFilesAction:(id) sender
{
NSArray *files = [sender representedObject];
NSString *workingDirectory = [commitController.repository workingDirectory];
for (PBChangedFile *file in files)
[[NSWorkspace sharedWorkspace] openFile:[workingDirectory stringByAppendingPathComponent:[file path]]];
}
- (void) ignoreFilesAction:(id) sender
{
NSArray *selectedFiles = [sender representedObject];
if ([selectedFiles count] == 0)
return;
[self ignoreFiles:selectedFiles];
[commitController.index refresh];
}
- (void)discardFilesAction:(id) sender
{
NSArray *selectedFiles = [sender representedObject];
if ([selectedFiles count] > 0)
[self discardChangesForFiles:selectedFiles force:FALSE];
}
- (void)forceDiscardFilesAction:(id) sender
{
NSArray *selectedFiles = [sender representedObject];
if ([selectedFiles count] > 0)
[self discardChangesForFiles:selectedFiles force:TRUE];
}
- (void) showInFinderAction:(id) sender
{
NSArray *selectedFiles = [sender representedObject];
if ([selectedFiles count] == 0)
return;
NSString *workingDirectory = [[commitController.repository workingDirectory] stringByAppendingString:@"/"];
NSString *path = [workingDirectory stringByAppendingPathComponent:[[selectedFiles objectAtIndex:0] path]];
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
[ws selectFile: path inFileViewerRootedAtPath:nil];
}
- (void)discardChangesForFiles:(NSArray *)files force:(BOOL)force
{
if (!force) {
int ret = [[NSAlert alertWithMessageText:@"Discard changes"
defaultButton:nil
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"Are you sure you wish to discard the changes to this file?\n\nYou cannot undo this operation."] runModal];
if (ret != NSAlertDefaultReturn)
return;
}
[commitController.index discardChangesForFiles:files];
}
# pragma mark TableView icon delegate
- (void)tableView:(NSTableView*)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)rowIndex
{
id controller = [tableView tag] == 0 ? unstagedFilesController : stagedFilesController;
[[tableColumn dataCell] setImage:[[[controller arrangedObjects] objectAtIndex:rowIndex] icon]];
}
- (void) tableClicked:(NSTableView *) tableView
{
NSArrayController *controller = [tableView tag] == 0 ? unstagedFilesController : stagedFilesController;
NSIndexSet *selectionIndexes = [tableView selectedRowIndexes];
NSArray *files = [[controller arrangedObjects] objectsAtIndexes:selectionIndexes];
if ([tableView tag] == 0)
[commitController.index stageFiles:files];
else
[commitController.index unstageFiles:files];
}
- (void) rowClicked:(NSCell *)sender
{
NSTableView *tableView = (NSTableView *)[sender controlView];
if([tableView numberOfSelectedRows] != 1)
return;
[self tableClicked: tableView];
}
- (BOOL)tableView:(NSTableView *)tv
writeRowsWithIndexes:(NSIndexSet *)rowIndexes
toPasteboard:(NSPasteboard*)pboard
{
// Copy the row numbers to the pasteboard.
[pboard declareTypes:[NSArray arrayWithObjects:FileChangesTableViewType, NSFilenamesPboardType, nil] owner:self];
// Internal, for dragging from one tableview to the other
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard setData:data forType:FileChangesTableViewType];
// External, to drag them to for example XCode or Textmate
NSArrayController *controller = [tv tag] == 0 ? unstagedFilesController : stagedFilesController;
NSArray *files = [[controller arrangedObjects] objectsAtIndexes:rowIndexes];
NSString *workingDirectory = [commitController.repository workingDirectory];
NSMutableArray *filenames = [NSMutableArray arrayWithCapacity:[rowIndexes count]];
for (PBChangedFile *file in files)
[filenames addObject:[workingDirectory stringByAppendingPathComponent:[file path]]];
[pboard setPropertyList:filenames forType:NSFilenamesPboardType];
return YES;
}
- (NSDragOperation)tableView:(NSTableView*)tableView
validateDrop:(id <NSDraggingInfo>)info
proposedRow:(NSInteger)row
proposedDropOperation:(NSTableViewDropOperation)operation
{
if ([info draggingSource] == tableView)
return NSDragOperationNone;
[tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
return NSDragOperationCopy;
}
- (BOOL)tableView:(NSTableView *)aTableView
acceptDrop:(id <NSDraggingInfo>)info
row:(NSInteger)row
dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboard dataForType:FileChangesTableViewType];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
NSArrayController *controller = [aTableView tag] == 0 ? stagedFilesController : unstagedFilesController;
NSArray *files = [[controller arrangedObjects] objectsAtIndexes:rowIndexes];
if ([aTableView tag] == 0)
[commitController.index unstageFiles:files];
else
[commitController.index stageFiles:files];
return YES;
}
@end