-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mm
348 lines (295 loc) · 12.5 KB
/
main.mm
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "main.h"
#import "icons.h"
#import "SVNProgress.h"
#import "SVNCommit.h"
#import "SVNProperties.h"
#import "SVNLog.h"
#import "SVNBlame.h"
#import "SVNBrowse.h"
#import "SVNImport.h"
#import "SVNSettings.h"
#if 0
@interface MyApplication : NSApplication
@end
@implementation MyApplication : NSApplication
-(id)init {
if (self = [super init]) {
}
return self;
}
-(BOOL)sendAction:(SEL)anAction to:(id)aTarget from:(id)sender {
NSLog(@"action:%@\n", NSStringFromSelector(anAction));
return [super sendAction:anAction to:aTarget from:sender];
}
@end
#endif
void RecycleFiles(NSArray *paths) {
// NSString *trash = [NSHomeDirectory() stringByAppendingPathComponent:@".Trash"];
NSString *source = [[paths objectAtIndex:0] stringByDeletingLastPathComponent];
NSMutableArray *files = [NSMutableArray arrayWithCapacity:[paths count]];
NSInteger tag;
for (NSString *path in paths) {
if (![source isEqualToString:[path stringByDeletingLastPathComponent]]) {
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation
source:source destination:@"" files:files tag:&tag
];
[files removeAllObjects];
source = [path stringByDeletingLastPathComponent];
}
[files addObject:[path lastPathComponent]];
}
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation
source:source destination:@"" files:files tag:&tag
];
}
@implementation SVNService
+(void)addLRU:(NSString*)list value:(id)value {
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSDictionary *dict = [defs dictionaryForKey:@"LRU"];
NSMutableArray *lru = [dict objectForKey:list];
if (!lru) {
lru = [NSMutableArray new];
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithDictionary:dict];
[temp setObject:lru forKey:list];
dict = temp;
}
if ([lru indexOfObject:value] == NSNotFound) {
if ([lru count] > [defs integerForKey:@"lru_items"])
[lru removeObjectAtIndex:0];
[lru addObject:value];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"LRU"];
}
}
+(NSArray*)getLRU:(NSString*)list {
return [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"LRU"] objectForKey:list];
}
-(void)dealloc {
[cache_dir release];
[super dealloc];
}
-(void)applicationDidFinishLaunching:(NSNotification*)notification {
cache_dir = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] retain];
NSDictionary *defaults = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"defaults"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
[[NSApplication sharedApplication] setServicesProvider:self];
}
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender {
return YES;
}
-(void)doCommit:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString**)error {
[SVNCommit paths:[pboard propertyListForType:NSFilenamesPboardType]];
}
-(void)doUpdate:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString**)error {
SVNProgress *prog = [[SVNProgress alloc] initWithTitle:@"Update"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
APRarray<svn_revnum_t> revs;
[prog finishedWithError:prog->svn->Update(prog, [pboard propertyListForType:NSFilenamesPboardType], revs, SVNrevision::head())];
});
}
-(void)doAdd:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString**)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
SVNcontext svn;
NSString *parent = [[paths objectAtIndex:0] stringByDeletingLastPathComponent];
svn_client_info2_t *info = 0;
svn_error_t *err = svn.GetInfo(parent, SVNrevision::head(), &info);
if (err) {
[[SVNImport new] svn_import:paths];
} else {
SVNProgress *prog = [[SVNProgress alloc] initWithTitle:@"Add"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
svn_error_t *err = 0;
for (NSString *path in paths) {
if ((err = prog->svn->Add(prog, path)))
break;
}
[prog finishedWithError:err];
});
}
}
-(void)doRemove:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString**)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
SVNcontext svn;
if (svn_error_t *err = svn.Delete(nil, paths))
SVNErrorAlert(nil, err);
}
-(void)doRevert:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString**)error {
#if 1
[SVNRevert paths:[pboard propertyListForType:NSFilenamesPboardType]];
#else
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
// RecycleFiles(paths);
SVNProgress *prog = [[SVNProgress alloc] initWithTitle:@"Revert"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
[prog finishedWithError:prog->svn->Revert(prog, paths)];
});
#endif
}
-(void)doResolve2:(NSString*)path {
SVNProgress *prog = [[SVNProgress alloc] initWithTitle:@"Resolve"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
[prog finishedWithError:prog->svn->Resolve(prog, path)];
});
}
-(void)doResolve:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString**)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
NSString *path = [paths objectAtIndex:0];
NSString *dir = [path stringByDeletingLastPathComponent];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:nil];
NSString *match = [[path lastPathComponent] stringByAppendingPathExtension:@"r*"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", match];
NSArray *matches= [files filteredArrayUsingPredicate:pred];
if ([matches count] != 2) {
[[NSAlert alertWithMessageText:@"Subversion" defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@ does not need resolving.", path] runModal];
return;
}
NSString *base = [matches objectAtIndex:0];
NSString *left = [matches objectAtIndex:1];
if ([base compare:left] == NSOrderedDescending) {
base = left;
left = [matches objectAtIndex:0];
}
NSString *merge = [[[[[[NSUserDefaults standardUserDefaults] stringForKey:@"merge_command"]
stringByReplacingOccurrencesOfString:@"%base" withString:[dir stringByAppendingPathComponent:base] ]
stringByReplacingOccurrencesOfString:@"%theirs" withString:[dir stringByAppendingPathComponent:left] ]
stringByReplacingOccurrencesOfString:@"%mine" withString:[path stringByAppendingPathExtension:@"mine"] ]
stringByReplacingOccurrencesOfString:@"%merged" withString:path
];
NSArray *args = [merge componentsSeparatedByString:@" "];
NSTask *task = [NSTask launchedTaskWithLaunchPath:[args objectAtIndex:0] arguments:[args subarrayWithRange:NSMakeRange(1, [args count] - 1)]];
task.terminationHandler = ^(NSTask *task) {
NSLog(@"Merger terminated with status=%i\n", task.terminationStatus);
[self performSelectorOnMainThread:@selector(doResolve2:) withObject:path waitUntilDone:NO];
};
}
-(void)doProps:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
[[SVNProperties new] svn_props_multi:paths];
}
-(void)doLock:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
[SVNGetLock paths:[pboard propertyListForType:NSFilenamesPboardType]];
}
-(void)doUnlock:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
[SVNReleaseLock paths:[pboard propertyListForType:NSFilenamesPboardType]];
}
-(void)doLog:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
[[SVNLog new] svn_log:[pboard propertyListForType:NSFilenamesPboardType]];
}
-(void)doUpgrade:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
SVNProgress *prog = [[SVNProgress alloc] initWithTitle:@"Upgrade"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
svn_error_t *err = 0;
for (NSString *path in paths) {
if ((err = prog->svn->Upgrade(prog, path)))
break;
}
[prog finishedWithError:err];
});
}
-(void)doBlame:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
[[SVNBlame new] svn_blame:[paths objectAtIndex:0] fromRevision:1 toRevision:SVNrevision::head()];
}
-(void)doCleanup:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
SVNProgress *prog = [[SVNProgress alloc] initWithTitle:@"Cleanup"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
//chflags -R nouchg *
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *attribs = @{NSFileImmutable:@NO};
for (NSString *path in paths) {
[prog addentry:@"Checking for immutable files" path:path];
for (NSString *file in [fm enumeratorAtPath:path]) {
NSError *error;
NSString *fullfile = [path stringByAppendingPathComponent:file];
NSDictionary *attribs0 = [fm attributesOfItemAtPath:fullfile error:&error];
if ([[attribs0 objectForKey:NSFileImmutable] boolValue]) {
if (![fm setAttributes:attribs ofItemAtPath:fullfile error:&error]) {
[prog addentry:@"Failed to clear immutable" path:fullfile type:[error.userInfo objectForKey:NSLocalizedDescriptionKey] colour:[NSColor redColor]];
} else {
[prog addentry:@"Clear immutable" path:fullfile colour:[NSColor greenColor]];
}
}
}
}
svn_error_t *err = 0;
for (NSString *path in paths) {
[prog addentry:@"Cleaning up" path:path];
if ((err = prog->svn->Cleanup(prog, path)))
break;
}
[prog finishedWithError:err];
});
}
-(void)doBrowse:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString **)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
[[SVNBrowse new] svn_browse:[paths objectAtIndex:0]];
}
-(void)doImport:(NSPasteboard*)pboard userData:(NSString*)user error:(NSString**)error {
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
[[SVNImport new] svn_import:paths];
}
-(IBAction)doPreferences:(id)sender {
APRInit();
[SVNSettings new];
}
-(IBAction)doBrowse:(id)sender {
[[SVNBrowse new] svn_browse:[[SVNService getLRU:@"repository"] objectAtIndex:0]];
}
//------------------------------------------------------------------------------
// SVN delegate
//------------------------------------------------------------------------------
-(svn_error_t*)SVNstatus:(const svn_client_status_t*)client_status path:(const char*)path pool:(apr_pool_t*)pool {
NSString *overlay = 0;
switch (client_status->node_status) {
case svn_wc_status_missing:
case svn_wc_status_none:
case svn_wc_status_unversioned: return 0;
case svn_wc_status_normal: overlay= @"Checkmark.icns"; break;
case svn_wc_status_added: overlay= @"Added.icns"; break;
case svn_wc_status_deleted: overlay= @"Deleted.icns"; break;
case svn_wc_status_modified:
case svn_wc_status_replaced: overlay= @"Modified.icns"; break;
case svn_wc_status_conflicted: overlay= @"Conflict.icns"; break;
case svn_wc_status_ignored: overlay= @"Ignored.icns"; break;
default: break;
}
NSString *local = [NSString stringWithUTF8String:client_status->local_abspath];
NSString *type = client_status->kind == svn_node_dir ? NSFileTypeForHFSTypeCode(kGenericFolderIcon) : [local pathExtension];
NSImage *icon;
#if 1
if (overlay) {
NSString *cache = [[[cache_dir stringByAppendingPathComponent:[overlay stringByDeletingPathExtension]] stringByAppendingPathExtension:type] stringByAppendingPathExtension:@"icns"];
icon = [[[NSImage alloc] initWithContentsOfFile:cache] autorelease];
if (!icon) {
icon = [[[NSWorkspace sharedWorkspace] iconForFileType:type] composite:[NSImage imageNamed:overlay]];
[icon writeToICNSFile:cache];
}
} else {
icon = [[NSWorkspace sharedWorkspace] iconForFileType:type];
}
#else
icon = [[NSWorkspace sharedWorkspace] iconForFileType:type];
if (overlay)
icon = [icon composite:[NSImage imageNamed:overlay]];
#endif
[[NSWorkspace sharedWorkspace] setIcon:icon forFile:local options:0];
return 0;
}
@end
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main (int argc, const char *argv[]) {
#if 1
return NSApplicationMain(argc, argv);
#else
NSAutoreleasePool *ns_pool = [NSAutoreleasePool new];
NSRegisterServicesProvider([SVNService new], @"SVNService");
[[NSApplication sharedApplication] run];
[ns_pool release];
return 0;
#endif
}