-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySvnLogView.m
769 lines (604 loc) · 20.8 KB
/
MySvnLogView.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#import "MySvnLogView.h"
#import "MySvnLogAC.h"
#import "MySvnLogParser.h"
#import "MyRepository.h"
#import "MyApp.h"
#import "NSString+MyAdditions.h"
#import "RepoItem.h"
#import "Tasks.h"
#import "CommonUtils.h"
#import "ViewUtils.h"
//----------------------------------------------------------------------------------------
static NSString*
getRevisionAtIndex (NSArray* array, int index)
{
return (index >= 0 && index < [array count]) ? [[array objectAtIndex: index] objectForKey: @"revision"] : nil;
}
//----------------------------------------------------------------------------------------
static NSString*
pathItemToString (NSDictionary* item)
{
NSString* str = [NSString stringWithFormat: @"%@\t%@", [item objectForKey: @"action"],
[item objectForKey: @"path"]];
NSString* fromPath = [item objectForKey: @"copyfrompath"];
if (fromPath)
str = [NSString stringWithFormat: @"%@\t%@\t%@", str,
[item objectForKey: @"copyfromrev"], fromPath];
return str;
}
//----------------------------------------------------------------------------------------
static NSString*
logItemToString (NSDictionary* item, BOOL isAdvanced)
{
NSMutableString* str = [NSMutableString string];
[str appendString: [NSString stringWithFormat: @"%@\t%@\t%@\n", [item objectForKey: @"revision"],
[item objectForKey: @"author"], [item objectForKey: @"date"]]];
if (isAdvanced)
{
NSArray* const paths = [item objectForKey: @"paths"];
NSEnumerator* enumerator = [paths objectEnumerator];
id pathItem;
while (pathItem = [enumerator nextObject])
{
[str appendString: pathItemToString(pathItem)];
[str appendString: @"\n"];
}
}
[str appendString: [item objectForKey: @"msg"]];
[str appendString: @"\n"];
return str;
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
@interface ActionToColor : NSValueTransformer
{
}
@end
//----------------------------------------------------------------------------------------
@implementation ActionToColor
+ (Class) transformedValueClass
{
return [NSColor class];
}
+ (BOOL) allowsReverseTransformation
{
return NO;
}
- (id) transformedValue: (id) aString
{
switch (([aString length] == 1) ? [aString characterAtIndex: 0] : 0)
{
case 'A': // Add
{
static id color = nil;
if (color == nil)
color = [[NSColor colorWithDeviceRed: 0 green: .6 blue: 0 alpha: 1] retain];
return color;
}
case 'M': // Modify
{
static id color = nil;
if (color == nil)
color = [[NSColor colorWithDeviceRed: 0 green: 0 blue: .6 alpha: 1] retain];
return color;
}
case 'D': // Delete
{
static id color = nil;
if (color == nil)
color = [[NSColor colorWithDeviceRed: .7 green: 0 blue: 0 alpha: 1] retain];
return color;
}
case 'R': // Replace
{
static id color = nil;
if (color == nil)
color = [[NSColor colorWithDeviceRed: .85 green: .33 blue: 0 alpha: 1] retain];
return color;
}
}
// Other
static id color = nil;
if (color == nil)
color = [[NSColor blackColor] retain];
return color;
}
@end
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
@interface MySvnLogView (Private)
- (NSString*) path;
- (int) mostRecentRevision;
- (void) setMostRecentRevision: (int) aMostRecentRevision;
- (NSString*) getCachePath;
@end
//----------------------------------------------------------------------------------------
@implementation MySvnLogView
- (id) initWithFrame: (NSRect) frameRect
{
self = [super initWithFrame: frameRect];
if (self != nil)
{
isVerbose = YES;
fIsAdvanced = YES;
if ([NSBundle loadNibNamed: @"MySvnLogView2" owner: self])
{
[fView setFrame: [self bounds]];
[self addSubview: fView];
// [self addObserver: self forKeyPath: @"currentRevision"
// options: (NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context: nil];
}
[self setMostRecentRevision: 0];
}
return self;
}
//----------------------------------------------------------------------------------------
- (void) dealloc
{
[self setPath: nil];
[self setLogArray: nil];
[self setCurrentRevision: nil];
[super dealloc];
}
//----------------------------------------------------------------------------------------
- (void) unload
{
// the nib is responsible for releasing its top-level objects
// [fView release]; // this is done by super
[logsAC release];
[logsACSelection release];
// Unbind objects that are bound to the NIB file's owner
id advancedBtn = [[fView subviews] objectAtIndex: 0];
AssertClass(advancedBtn, NSButton);
[logsAC unbind: NSContentArrayBinding];
[advancedBtn unbind: NSValueBinding];
[super unload];
}
//----------------------------------------------------------------------------------------
// This gets called twice because of the loadNibNamed call above.
// The first time with [self window] == nil.
- (void) awakeFromNib
{
NSWindow* const window = [self window];
if (window)
{
id desc = [[AlphaNumSortDesc alloc] initWithKey: @"path" ascending: YES];
[logsACSelection setSortDescriptors: [NSArray arrayWithObjects: desc, desc, desc, desc, nil]];
[desc release];
[self setAdvanced: [self repository] && GetPreferenceBool(@"defaultLogViewKindIsAdvanced")];
[splitView setDelegate: self]; // allow us to keep the paths pane hidden during window resize
// Paths table
[pathsTable setTarget: self];
[pathsTable setDoubleAction: @selector(doubleClickPath:)];
SetColumnSort(pathsTable, @"path", @"path");
SetColumnSort(pathsTable, @"from", @"copyfromrev");
SetColumnSort(pathsTable, @"srcPath", @"copyfrompath");
[pathsTable setDataSource: self];
[window makeFirstResponder: logTable];
if ([self repository]) // Enable dragging from repository log & paths tables only
{
const NSDragOperation kSourceMask = NSDragOperationCopy;
[logTable setDraggingSourceOperationMask: kSourceMask forLocal: NO];
[logTable setDraggingSourceOperationMask: kSourceMask forLocal: YES];
[pathsTable setDraggingSourceOperationMask: kSourceMask forLocal: NO];
[pathsTable setDraggingSourceOperationMask: kSourceMask forLocal: YES];
}
}
}
//----------------------------------------------------------------------------------------
// Set unique name for saving log & paths tables' size & sort, and load that info.
- (void) setAutosaveName: (NSString*) name
{
[logTable setAutosaveName: name];
[pathsTable setAutosaveName: [@"P:" stringByAppendingString: name]];
}
//----------------------------------------------------------------------------------------
- (void) resetUrl: (NSURL*) anUrl
{
[self setUrl: anUrl];
[self setLogArray: nil];
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
- (IBAction) doubleClickPath: (id) sender
{
#pragma unused(sender)
MyRepository* repository = [self repository];
if (repository)
{
[repository openLogPath: [[logsACSelection arrangedObjects] objectAtIndex: [pathsTable clickedRow]]
forLogEntry: [[logsAC selectedObjects] objectAtIndex: 0]];
}
}
//----------------------------------------------------------------------------------------
- (IBAction) copy: (id) sender
{
#pragma unused(sender)
NSString* str = nil;
id view = [[self window] firstResponder];
if (view == logTable)
{
str = logItemToString([[logsAC selectedObjects] objectAtIndex: 0], fIsAdvanced);
}
else if (view == pathsTable)
{
str = pathItemToString([[logsACSelection selectedObjects] objectAtIndex: 0]);
}
else
dprintf("firstResponder=%@", view);
if (str)
{
NSPasteboard* clipboard = [NSPasteboard generalPasteboard];
[clipboard declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: self];
[clipboard setString: str forType: NSStringPboardType];
}
}
//----------------------------------------------------------------------------------------
- (IBAction) keyDown: (NSEvent*) theEvent
{
// Make space-bar in log table => acts as if the radio button in the selected row was clicked
if ([[self window] firstResponder] == logTable &&
[[theEvent characters] characterAtIndex: 0] == ' ')
{
NSString* revision = [self selectedRevision];
if (revision)
{
[self setCurrentRevision: revision];
[logTable setNeedsDisplay: YES];
}
}
else
[super keyDown: theEvent];
}
//----------------------------------------------------------------------------------------
// <sender> is splitView with paths in lower pane
- (void) splitView: (NSSplitView*) sender
resizeSubviewsWithOldSize: (NSSize) oldSize
{
#pragma unused(oldSize)
if (!fIsAdvanced)
{
const NSSize newSize = [sender bounds].size;
NSView* const view0 = [[sender subviews] objectAtIndex: 0];
NSRect frame = [view0 frame];
frame.size.width = newSize.width;
frame.size.height = newSize.height - [sender dividerThickness];
[view0 setFrame: frame];
}
[sender adjustSubviews];
}
//----------------------------------------------------------------------------------------
#pragma mark -
#pragma mark svn related methods
//----------------------------------------------------------------------------------------
- (void) doSvnLog: (NSString*) aPath
pegRevision: (NSString*) pegRev
callback: (NSInvocation*) callback
{
// dprintf("(aPath='%@' pegRev=%@) fRevision=%@", aPath, pegRev, fRevision);
if (pegRev == nil)
pegRev = fRevision;
if ([pegRev length] != 0 && ![pegRev isEqualToString: @"0"])
aPath = PathPegRevision(aPath, pegRev);
else
pegRev = @"HEAD";
id taskInfo = [MySvn log: aPath
generalOptions: [self svnOptionsInvocation]
options: [NSArray arrayWithObjects: @"--xml",
[NSString stringWithFormat: @"-r%@:%d", pegRev, [self mostRecentRevision]],
isVerbose ? @"-v" : nil,
nil]
callback: callback ? callback : MakeCallbackInvocation(self, @selector(svnCommandComplete:))
callbackInfo: nil
taskInfo: [self documentNameDict]];
[self setPendingTask: taskInfo];
}
//----------------------------------------------------------------------------------------
- (void) doSvnLog: (NSString*) aPath
pegRevision: (NSString*) pegRev
{
[self doSvnLog: aPath
pegRevision: pegRev
callback: nil];
}
//----------------------------------------------------------------------------------------
- (void) fetchSvnLogForUrl: (NSInvocation*) callback
{
NSDictionary* cacheDict = nil;
BOOL useCache = GetPreferenceBool(@"cacheSvnQueries");
NSData* cacheData;
if (useCache && (cacheData = [NSData dataWithContentsOfFile: [self getCachePath]]))
{
NSString* errorString = nil;
cacheDict = [NSPropertyListSerialization propertyListFromData: cacheData
mutabilityOption: kCFPropertyListMutableContainers
format: NULL
errorDescription: &errorString];
if (errorString)
NSLog(@"fetchSvnLogForUrl: ERROR: %@", errorString);
[errorString release];
}
if (cacheDict)
{
[self setLogArray: [cacheDict objectForKey: @"logArray"]];
}
[self doSvnLog: [[self url] absoluteString] pegRevision: [self revision] callback: callback];
}
//----------------------------------------------------------------------------------------
- (void) fetchSvnLogForUrl
{
[self fetchSvnLogForUrl: nil];
}
//----------------------------------------------------------------------------------------
// Triggers the fetching
- (void) fetchSvn: (NSInvocation*) callback
{
[super fetchSvn];
if ([self path]) // when called from the working copy window, the fileMerge operation
{ // (svn diff) takes a filesystem path, not an url+revision
Assert(!fRepository);
[self doSvnLog: [self path] pegRevision: nil callback: callback];
}
else
{
Assert(fRepository);
[self fetchSvnLogForUrl: callback];
}
}
//----------------------------------------------------------------------------------------
// Triggers the fetching
- (void) fetchSvn
{
[self fetchSvn: nil];
}
//----------------------------------------------------------------------------------------
- (void) fetchSvnReceiveDataFinished: (id) taskObj
{
[super fetchSvnReceiveDataFinished: taskObj];
NSData* data = stdOutData(taskObj);
if (data != nil && [data length] != 0)
{
NSMutableArray* newEntries = [MySvnLogParser parseData: data];
NSMutableArray* array = logArray;
if (array)
{
if (newEntries)
[array addObjectsFromArray: newEntries];
}
else
array = newEntries;
[MyRepository cleanUpLog: array];
[self setLogArray: array];
if (currentRevision == nil)
{
[self setCurrentRevision: [array count] ? getRevisionAtIndex(array, 0) : @"0"];
}
if ([self url] != nil) // Only cache logs for repository URLs not Working Copy paths
{
id dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: [self mostRecentRevision]], @"revision",
array, @"logArray",
nil];
NSString* errorString = nil;
id data = [NSPropertyListSerialization dataFromPropertyList: dict
format: NSPropertyListBinaryFormat_v1_0
errorDescription: &errorString];
if (data)
{
[data writeToFile: [self getCachePath] atomically: YES];
}
else
{
dprintf("ERROR: %@", errorString);
[errorString release];
}
}
}
}
//----------------------------------------------------------------------------------------
#pragma mark -
#pragma mark Table View datasource
//----------------------------------------------------------------------------------------
// The tableview is driven by the bindings, except for the radio button column.
- (id) tableView: (NSTableView*) aTableView
objectValueForTableColumn: (NSTableColumn*) aTableColumn
row: (int) rowIndex
{
#pragma unused(aTableView)
if ([[aTableColumn identifier] isEqualToString: @"currentRevision"]) // should be always the case
{
return NSBool([getRevisionAtIndex([logsAC arrangedObjects], rowIndex) isEqualToString: currentRevision]);
}
return nil;
}
//----------------------------------------------------------------------------------------
- (void) tableView: (NSTableView*) aTableView
setObjectValue: (id) anObject
forTableColumn: (NSTableColumn*) aTableColumn
row: (int) rowIndex
{
#pragma unused(anObject)
// The tableview is driven by the bindings, except for the first column !
if ([[aTableColumn identifier] isEqualToString: @"currentRevision"]) // should be always the case
{
NSString* newRevision = getRevisionAtIndex([logsAC arrangedObjects], rowIndex);
if (currentRevision == nil || ![currentRevision isEqualToString: newRevision])
{
[self setCurrentRevision: newRevision];
[aTableView setNeedsDisplay: YES];
}
}
}
//----------------------------------------------------------------------------------------
- (BOOL) tableView: (NSTableView*) tableView
writeRowsWithIndexes: (NSIndexSet*) rowIndexes
toPasteboard: (NSPasteboard*) pboard
{
if (![self repository])
return NO;
NSString* string = nil;
id logObj = nil;
NSString* path_ = nil;
if (tableView == logTable)
{
logObj = [[logsAC arrangedObjects] objectAtIndex: [rowIndexes firstIndex]];
string = logItemToString(logObj, fIsAdvanced);
}
else if (tableView == pathsTable)
{
logObj = [[logsAC selectedObjects] lastObject];
const id pathObj = [[logsACSelection arrangedObjects] objectAtIndex: [rowIndexes firstIndex]];
string = pathItemToString(pathObj);
if (![[pathObj objectForKey: @"action"] isEqualToString: @"D"])
path_ = [pathObj objectForKey: @"path"];
else
logObj = nil; // Disallow merge from deleted path
}
if (logObj && string)
{
RepoItem* repoItem = [RepoItem repoPath: path_
revision: [[logObj objectForKey: @"revision_n"] intValue]
url: [fRepository rootURL]];
[repoItem svnInfo: fRepository];
/* dprintf_("isLog=%d isDir=%d isRoot=%d path=\"%@\" name=\"%@\" r%u\n\turl=<%@>",
[repoItem isLog], [repoItem isDir], [repoItem isRoot], [repoItem path],
[repoItem name], [repoItem revisionNum], [repoItem url]);*/
[pboard declareTypes: [NSArray arrayWithObjects: NSStringPboardType, kTypeRepoItem, nil] owner: nil];
[pboard setString: string forType: NSStringPboardType];
[pboard setData: [NSData dataWithBytes: &repoItem length: sizeof(repoItem)]
forType: kTypeRepoItem];
return YES;
}
else if (string)
{
[pboard declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
[pboard setString: string forType: NSStringPboardType];
return YES;
}
return NO;
}
//----------------------------------------------------------------------------------------
// Sometimes required by the compiler, sometimes not.
- (int) numberOfRowsInTableView: (NSTableView*) aTableView
{
#pragma unused(aTableView)
return [[logsAC arrangedObjects] count];
}
//----------------------------------------------------------------------------------------
#pragma mark -
#pragma mark Accessors
//----------------------------------------------------------------------------------------
- (NSString*) selectedRevision // This is different from the checked one
{
return getRevisionAtIndex([logsAC selectedObjects], 0);
}
// - currentRevision:
- (NSString*) currentRevision { return currentRevision; }
// - setCurrentRevision:
- (void) setCurrentRevision: (NSString*) aCurrentRevision
{
id old = currentRevision;
currentRevision = [aCurrentRevision retain];
[old release];
}
// - path:
- (NSString*) path { return path; }
// - setPath:
- (void) setPath: (NSString*) aPath
{
id old = path;
path = [aPath retain];
[old release];
}
// - logArray:
- (NSMutableArray*) logArray { return logArray; }
// - setLogArray:
- (void) setLogArray: (NSMutableArray*) aLogArray
{
id old = logArray;
logArray = [aLogArray retain];
[old release];
mostRecentRevision = [getRevisionAtIndex(aLogArray, 0) intValue];
}
// - mostRecentRevision:
- (int) mostRecentRevision { return mostRecentRevision; }
// - setMostRecentRevision:
- (void) setMostRecentRevision: (int) aMostRecentRevision
{
mostRecentRevision = aMostRecentRevision;
}
//----------------------------------------------------------------------------------------
- (BOOL) advanced
{
return fIsAdvanced;
}
//----------------------------------------------------------------------------------------
- (void) setAdvanced: (BOOL) isAdvanced
{
if (fIsAdvanced == isAdvanced)
return;
fIsAdvanced = isAdvanced;
const id firstResponder = isAdvanced ? nil : [[self window] firstResponder];
// Hide or show Paths search field
[searchPaths setHidden: !isAdvanced];
if (!isAdvanced && [[searchPaths stringValue] length])
{
[searchPaths setStringValue: @""];
[logsAC clearSearchPaths];
}
// Hide or show pathsCount column
NSTableColumn* col = [logTable tableColumnWithIdentifier: @"pathsCount"];
Assert(col);
const GCoord colWidth = isAdvanced ? 30 : -4;
[col setMinWidth: colWidth];
[col setWidth: colWidth];
// Collapse or expand paths table
const id subViews = [splitView subviews];
NSView* pathsView = [subViews objectAtIndex: 1];
Assert(pathsView);
GCoord splitterSize = [splitView dividerThickness];
if (isAdvanced)
splitterSize = -splitterSize;
NSRect frame = [splitView frame];
frame.origin.y -= splitterSize;
frame.size.height += splitterSize;
[splitView setFrame: frame];
frame = [pathsView frame];
frame.size.height = isAdvanced ? 100 : 0;
[pathsView setFrame: frame];
[pathsView setHidden: !isAdvanced];
[splitView adjustSubviews];
[splitView setNeedsDisplay: YES];
[[subViews objectAtIndex: 0] setNeedsDisplay: YES];
// If we just hid the focused view then focus the log table.
// This is required as hiding an NSSearchField doesn't update the focus.
if (firstResponder && [firstResponder isKindOfClass: [NSView class]] &&
[firstResponder isHiddenOrHasHiddenAncestor])
[[self window] makeFirstResponder: logTable];
}
//----------------------------------------------------------------------------------------
- (NSArray*) arrangedObjects
{
return [logsAC arrangedObjects];
}
//----------------------------------------------------------------------------------------
- (NSString*) getCachePath
{
NSString* logName = isVerbose ? @" log_verbose" : @" log";
return [MySvn cachePathForKey: [[[self url] absoluteString] stringByAppendingString: logName]];
}
//----------------------------------------------------------------------------------------
// Return the currently user focused log-item, log-item path or nil.
- (NSDictionary*) targetSvnItem
{
const id firstResponder = [[self window] firstResponder];
if (firstResponder == logTable)
return [[logsAC selectedObjects] lastObject];
if (firstResponder == pathsTable)
return [[logsACSelection selectedObjects] lastObject];
return nil;
}
@end // MySvnLogView