-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAppController.m
334 lines (289 loc) · 8.77 KB
/
AppController.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* AppController.m
*
* Copyright (c) 2002 Pierre-Yves Rivaille <[email protected]>
* Copyright (c) 2002-2012, GNUstep Project
*
* This file is part of EasyDiff.app.
*
* EasyDiff.app is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* EasyDiff.app is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyDiff.app; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#import <AppKit/AppKit.h>
#import "AppController.h"
#import "DiffWindowController.h"
#import "DiffFileChooser.h"
#include <math.h>
@implementation AppController
- (void)dealloc
{
[vcExecPaths release];
[super dealloc];
}
- (void) awakeFromNib
{
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotif
{
NSUserDefaults *defaults;
NSDictionary *dict;
Class stringClass;
/* read the user preferences */
defaults = [NSUserDefaults standardUserDefaults];
dict = [defaults dictionaryForKey: @"VcExecutablePaths"];
if (dict == nil)
{
vcExecPaths = [NSMutableDictionary new];
}
else
{
vcExecPaths = [dict mutableCopy];
}
stringClass = [NSString class];
if (![[vcExecPaths objectForKey: @"CVS"] isKindOfClass: stringClass])
{
// Backward compatibility
NSString *str = [defaults stringForKey: @"CvsExecutablePath"];
if (str == nil)
str = @"cvs";
[vcExecPaths setObject: str forKey: @"CVS"];
}
if (![[vcExecPaths objectForKey: @"Subversion"] isKindOfClass: stringClass])
{
[vcExecPaths setObject: @"svn" forKey: @"Subversion"];
}
if (![[vcExecPaths objectForKey: @"Git"] isKindOfClass: stringClass])
{
[vcExecPaths setObject: @"git" forKey: @"Git"];
}
if (![[vcExecPaths objectForKey: @"Mercurial"] isKindOfClass: stringClass])
{
[vcExecPaths setObject: @"hg" forKey: @"Mercurial"];
}
if (![[vcExecPaths objectForKey: @"Darcs"] isKindOfClass: stringClass])
{
[vcExecPaths setObject: @"darcs" forKey: @"Darcs"];
}
}
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
if ([filenames count] == 2)
{
[diffFileChooser showWindow:self];
[diffFileChooser setLeftFileName:[filenames objectAtIndex:0]];
[diffFileChooser setRightFileName:[filenames objectAtIndex:1]];
}
}
- (NSString *)getVCPath: (NSString *)fileName
{
NSUInteger i, n;
BOOL isDir;
NSString *vcDir, *dir;
NSFileManager *fm;
NSArray *vcs;
fm = [NSFileManager defaultManager];
dir = [fileName stringByDeletingLastPathComponent];
// CVS (and SVN up to version 1.6) saves a meta directory in every version
// controlled directory, hence we only need to look in the current directory
vcDir = @"CVS";
if ([fm fileExistsAtPath: vcDir isDirectory: &isDir] && isDir)
return vcDir;
// SVN (since version 1.7), git, Mercurial, and Darcs use a single meta
// directory at the root of of a version controlled tree, hence we may
// have to travel all the way up to the root to find that directory
vcs = [NSArray arrayWithObjects: @".svn", @".git", @".hg", @"_darcs", nil];
for (n = [[dir pathComponents] count]; n > 0; n--)
{
for (i = 0; i < [vcs count]; i++)
{
vcDir = [dir stringByAppendingPathComponent: [vcs objectAtIndex: i]];
if ([fm fileExistsAtPath: vcDir isDirectory: &isDir] && isDir)
return vcDir;
}
dir = [dir stringByDeletingLastPathComponent];
}
// no VC directory found
return nil;
}
- (IBAction)compareFileToVC: (id)sender
{
NSOpenPanel *oPanel;
NSString *filename1, *filename2;
NSInteger result;
NSString *path = [[NSUserDefaults standardUserDefaults]
objectForKey:@"OpenDirectory"];
oPanel = [NSOpenPanel openPanel];
[oPanel setAllowsMultipleSelection:NO];
result = [oPanel runModalForDirectory: path
file:nil types:nil];
if (result != NSOKButton)
return;
path = [oPanel directory];
filename1 = [[oPanel filenames] objectAtIndex: 0];
[[NSUserDefaults standardUserDefaults]
setObject: path
forKey:@"OpenDirectory"];
{
NSTask *taskVC;
NSString *vcPath, *vcDir, *vcFile;
NSFileHandle *fh;
filename2 = [NSString stringWithFormat:@"%@/%d_%@_%@",
NSTemporaryDirectory(),
[[NSProcessInfo processInfo]
processIdentifier],
[[NSDate date]
descriptionWithCalendarFormat:
@"%j%H%M%S%F" timeZone:nil
locale:nil],
[filename1 lastPathComponent]];
// determine the version control system and the root of the version
// controlled tree
vcPath = [self getVCPath: filename1];
if (vcPath == nil)
{
NSRunAlertPanel(@"No VC system",
@"File %@ is not under version control",
@"OK", nil, nil, filename1);
return;
}
path = [vcPath stringByDeletingLastPathComponent];
vcFile = [filename1 substringFromIndex: [path length] + 1];
// set up the VC task
taskVC = [[NSTask alloc] init];
[taskVC setCurrentDirectoryPath: path];
vcDir = [vcPath lastPathComponent];
if ([vcDir isEqualToString: @"CVS"])
{
[taskVC setLaunchPath: [vcExecPaths objectForKey: @"CVS"]];
[taskVC setArguments:
[NSArray arrayWithObjects: @"update", @"-p", vcFile, nil]];
}
else if ([vcDir isEqualToString: @".svn"])
{
[taskVC setLaunchPath: [vcExecPaths objectForKey: @"Subversion"]];
[taskVC setArguments: [NSArray arrayWithObjects: @"cat", vcFile, nil]];
}
else if ([vcDir isEqualToString: @".git"])
{
[taskVC setLaunchPath: [vcExecPaths objectForKey: @"Git"]];
[taskVC setArguments:
[NSArray arrayWithObjects: @"show",
[@"HEAD:" stringByAppendingString: vcFile],
nil]];
}
else if ([vcDir isEqualToString: @".hg"])
{
[taskVC setLaunchPath: [vcExecPaths objectForKey: @"Mercurial"]];
[taskVC setArguments: [NSArray arrayWithObjects: @"cat", vcFile, nil]];
}
else if ([vcDir isEqualToString: @"_darcs"])
{
[taskVC setLaunchPath: [vcExecPaths objectForKey: @"Darcs"]];
[taskVC setArguments:
[NSArray arrayWithObjects: @"show", @"contents", vcFile, nil]];
}
else
{
NSLog(@"unsupported VC?");
[taskVC release];
return;
}
if ([[NSFileManager defaultManager] createFileAtPath: filename2
contents: nil
attributes: nil] == NO)
{
[taskVC release];
return;
}
fh = [NSFileHandle fileHandleForWritingAtPath: filename2];
if (fh == nil)
{
[taskVC release];
return;
}
[taskVC setStandardOutput: fh];
[taskVC launch];
[taskVC waitUntilExit];
[taskVC release];
[fh closeFile];
if ([[[[NSFileManager defaultManager]
fileAttributesAtPath: filename2
traverseLink: NO] objectForKey: NSFileSize] intValue]
== 0)
{
NSLog(@"file does not exist on VC repository");
return;
}
}
{
// NB The controller will be released when its window is closed
[[DiffWindowController alloc] initWithFilename: filename1
andTempFilename: filename2];
}
}
/* preference panel methods */
- (IBAction)showPrefPanel: (id)sender
{
NSString *str;
[prefPanel makeKeyAndOrderFront:self];
str = [vcExecPaths objectForKey: [vcPopUp titleOfSelectedItem]];
[vcPathField setStringValue: str];
}
- (IBAction)prefApply: (id)sender
{
NSUserDefaults *defaults;
NSString *str;
defaults = [NSUserDefaults standardUserDefaults];
str = [vcPathField stringValue];
if ((str != nil) && ([str length] > 0)
&& [[NSFileManager defaultManager] fileExistsAtPath:str])
{
[vcExecPaths setObject: str forKey: [vcPopUp titleOfSelectedItem]];
[defaults setObject: vcExecPaths forKey: @"VcExecutablePaths"];
}
else
{
NSRunAlertPanel(@"VC Tool not found!",
@"File %@ is invalid!",
@"OK", nil, nil, str);
}
[prefPanel performClose:nil];
[defaults synchronize];
}
- (IBAction)prefChooseVC: (id)sender
{
NSString *str;
str = [vcExecPaths objectForKey: [vcPopUp titleOfSelectedItem]];
[vcPathField setStringValue:str];
}
- (IBAction)prefChooseVCExec: (id)sender
{
NSString *file;
NSOpenPanel *openPanel;
openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanChooseFiles:YES];
if ([openPanel runModal] == NSOKButton)
{
file = [[openPanel filenames] objectAtIndex:0];
[vcPathField setStringValue:file];
}
}
- (IBAction)prefCancel:(id)sender
{
[prefPanel performClose:nil];
}
@end