-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReviewCommit.m
1434 lines (1104 loc) · 38.3 KB
/
ReviewCommit.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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//----------------------------------------------------------------------------------------
// ReviewCommit.m - Review and edit a commit
//
// Copyright © Chris, 2008 - 2010. All rights reserved.
//----------------------------------------------------------------------------------------
#import <fcntl.h>
#import <sys/stat.h>
#import <unistd.h>
#import <WebKit/WebKit.h>
#import "ReviewCommit.h"
#import "MySvnLogParser.h"
#import "MyWorkingCopy.h"
#import "MyWorkingCopyController.h"
#import "MySvn.h"
#import "SvnDateTransformer.h"
#import "TableViewDelegate.h"
#import "Tasks.h"
#import "IconTextCell.h"
#import "CommonUtils.h"
#import "IconUtils.h"
#import "ViewUtils.h"
#import "NSString+MyAdditions.h"
//----------------------------------------------------------------------------------------
@interface ReviewFile : NSObject
{
NSDictionary* fItem;
IconRef fIcon;
BOOL fCommit;
}
- (id) init: (NSDictionary*) item commit: (BOOL) commit;
- (NSDictionary*) item;
- (BOOL) commit;
- (void) setCommit: (BOOL) commit;
- (NSString*) name;
- (NSString*) fullPath;
@end // ReviewFile
//----------------------------------------------------------------------------------------
@interface ReviewController (Private)
- (id) initWithDocument: (MyWorkingCopy*) document;
- (void) buildFileList: (BOOL) commitDefault;
- (void) taskCompleted: (Task*) task arg: (id) tmpHtmlPath;
- (void) displaySelectedFileDiff;
- (void) setIsBusy: (BOOL) isBusy;
- (BOOL) canCommit;
- (void) setCommitFileCount: (int) count;
@end // ReviewController
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
static int
compareNames (id obj1, id obj2, void* context)
{
#pragma unused(context)
return [[obj1 name] compare: [obj2 name] options: kSortOptions];
}
//----------------------------------------------------------------------------------------
static int
compareTemplateNames (id obj1, id obj2, void* context)
{
#pragma unused(context)
return [[obj1 objectForKey: @"name"] compare: [obj2 objectForKey: @"name"]
options: kSortOptions];
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
@implementation ReviewFile
//----------------------------------------------------------------------------------------
- (id) init: (NSDictionary*) item
commit: (BOOL) commit
{
if (self = [super init])
{
fItem = [item retain];
fCommit = commit;
}
return self;
}
//----------------------------------------------------------------------------------------
- (void) dealloc
{
[fItem release];
if (fIcon)
WarnIf(ReleaseIconRef(fIcon));
[super dealloc];
}
//----------------------------------------------------------------------------------------
- (NSDictionary*) item
{
return fItem;
}
//----------------------------------------------------------------------------------------
- (BOOL) commit
{
return fCommit;
}
//----------------------------------------------------------------------------------------
- (void) setCommit: (BOOL) commit
{
fCommit = commit;
}
//----------------------------------------------------------------------------------------
- (NSString*) name
{
return [fItem objectForKey: @"path"];
}
//----------------------------------------------------------------------------------------
- (NSString*) fullPath
{
return [fItem objectForKey: @"fullPath"];
}
//----------------------------------------------------------------------------------------
- (IconRef) icon
{
if (fIcon == NULL)
{
Boolean isDir = false;
ConstString fullPath = [self fullPath];
fIcon = GetFileOrTypeIcon([fullPath fileSystemRepresentation], fullPath, &isDir);
}
return fIcon;
}
@end // ReviewFile
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
@implementation ReviewController
static ConstString kPrefTemplates = @"msgTemplates",
kPrefKeySplits = @"reviewSplits";
static ConstString kPrefDefaultTab = @"diffDefaultTab",
kPrefContextLines = @"diffContextLines",
kPrefShowFunction = @"diffShowFunction",
kPrefShowChars = @"diffShowCharacters";
enum {
kPaneMessage = 0,
kPaneRecent = 1,
kPaneTemplates = 2,
cmdDefaultTab = 1000,
cmdShowFunction = 2000,
cmdShowChars = 2001,
vDiffSettingsPopUp = 510, // NSPopUpButton
kMaxTempHTMLFiles = 8
};
//----------------------------------------------------------------------------------------
+ (void) openForDocument: (MyWorkingCopy*) document
{
ReviewController* obj = [[ReviewController alloc] initWithDocument: document];
[obj release];
}
//----------------------------------------------------------------------------------------
- (id) initWithDocument: (MyWorkingCopy*) document
{
if (self = [super init])
{
// [[[document windowControllers] objectAtIndex: 0] setShouldCloseDocument: NO];
// [[document controller] retain];
[document registerSubController: self];
fDocument = [document retain];
fTemplates = [[NSMutableArray array] retain];
if ([NSBundle loadNibNamed: @"ReviewCommit" owner: self])
{
[fWindow retain];
[self buildFileList: YES];
}
}
return self;
}
//----------------------------------------------------------------------------------------
- (void) dealloc
{
// NSLog(@"dealloc ReviewController");
// [[[fDocument windowControllers] objectAtIndex: 0] setShouldCloseDocument: YES];
// [[fDocument controller] release];
[fTemplates release];
[fDocument release];
[fFileDiffTask release];
[super dealloc];
}
//----------------------------------------------------------------------------------------
- (NSView*) unbindSuperView: (NSView*) view
{
while (view) // Find first super-view that is an NSView
{
view = [view superview];
if ([view isMemberOfClass: [NSView class]])
{
[view unbind: NSHiddenBinding];
break;
}
}
return view;
}
//----------------------------------------------------------------------------------------
- (void) unload
{
enum {
vPaneSelector = 500, // NSSegmentedControl
vCommitButton = 501,
vCommitInfo = 502,
iBusyIndicator = 0 // NSProgressIndicator
};
[Tasks cancelCallbacksOnTarget: self];
NSWindow* const window = fWindow;
fWindow = NULL;
NSView* const root = [window contentView];
[fFilesAC unbind: NSContentArrayBinding];
[[root viewWithTag: vPaneSelector] unbind: NSSelectedIndexBinding];
[self unbindSuperView: fRecentView];
[self unbindSuperView: fTemplatesView];
NSView* view = [root viewWithTag: vCommitButton];
[view unbind: NSEnabledBinding];
[view unbind: NSEnabledBinding];
view = [root viewWithTag: vCommitInfo];
[view unbind: NSDisplayPatternValueBinding];
[view unbind: NSDisplayPatternValueBinding];
view = [[[view superview] subviews] objectAtIndex: iBusyIndicator];
[view unbind: NSAnimateBinding];
[window release];
}
//----------------------------------------------------------------------------------------
// Private:
- (NSInvocation*) makeCallback: (SEL) selector
{
return MakeCallbackInvocation([self retain], selector);
}
//----------------------------------------------------------------------------------------
// Private:
- (void) buildFileList: (BOOL) commitDefault
{
NSArray* const svnFiles = [fDocument svnFiles];
NSArray* const oldFiles = [fFilesAC content];
NSMutableArray* const newFiles = [NSMutableArray array];
int commitFileCount = 0;
for_each_obj(oEnum, item, svnFiles)
{
if ([[item objectForKey: @"committable"] boolValue])
{
BOOL commit = commitDefault;
NSString* const name = [item objectForKey: @"path"];
for_each_obj(oEnum2, item2, oldFiles)
if ([name isEqualToString: [item2 name]])
{
commit = [item2 commit];
break;
}
[newFiles addObject: [[ReviewFile alloc] init: item commit: commit]];
if (commit)
++commitFileCount;
}
}
fFiles = newFiles;
[newFiles sortUsingFunction: compareNames context: NULL];
[fFilesAC setContent: newFiles];
[self setCommitFileCount: commitFileCount];
if (!commitDefault)
[self displaySelectedFileDiff];
}
//----------------------------------------------------------------------------------------
- (void) buildFileList
{
[self buildFileList: NO];
}
//----------------------------------------------------------------------------------------
// If there is a selected item then return it else return nil.
// Private:
- (ReviewFile*) selectedItemOrNil
{
int rowIndex = [fFilesView selectedRow];
return (rowIndex >= 0) ? [fFiles objectAtIndex: rowIndex] : nil;
}
//----------------------------------------------------------------------------------------
// TO_DO: Move this into document & have it notify all review windows
// Build list of recent commit messages
// Private:
- (void) buildRecentList: (BOOL) full
{
[MySvn log: [[fDocument repositoryUrl] absoluteString]
generalOptions: [fDocument svnOptionsInvocation]
options: [NSArray arrayWithObjects: @"--limit", (full ? @"50" : @"1"), @"--xml", nil]
callback: [self makeCallback: @selector(buildRecentMessages:)]
callbackInfo: nil
taskInfo: nil];
}
//----------------------------------------------------------------------------------------
- (void) buildRecentMessages: (id) taskObj
{
if ([fWindow isVisible] && isCompleted(taskObj) && stdErr(taskObj) == nil)
{
NSData* data = stdOutData(taskObj);
if (data != nil && [data length] != 0)
{
NSArray* const array = [MySvnLogParser parseData: data];
const int count = [array count];
NSDateFormatter* const formatter = [SvnDateTransformer formatter];
NSDate* const date = [[NSDate alloc] init];
for_each_obj(oEnum, item, array)
{
NSString* str = [item objectForKey: @"date"];
str = [NSString stringWithFormat: @"%@ %@ +0000",
[str substringToIndex: 10],
[str substringWithRange: NSMakeRange(11, 8)]];
str = [formatter stringFromDate: [date initWithString: str]];
id obj = [NSDictionary dictionaryWithObject:
[NSString stringWithFormat: @"r%@\t%@\t%@\n%@",
[item objectForKey: @"revision"],
[item objectForKey: @"author"],
str,
[item objectForKey: @"msg"]]
forKey: @"log"];
if (count == 1)
{
// If the last commit was to an svn:external then this log entry
// may be a duplicate. If it is then don't add it.
NSArray* recentArray = [fRecentAC arrangedObjects];
if ([recentArray count] == 0 || ![[recentArray objectAtIndex: 0] isEqual: obj])
[fRecentAC insertObject: obj atArrangedObjectIndex: 0];
}
else
[fRecentAC addObject: obj];
}
[date release];
}
}
[self release];
}
//----------------------------------------------------------------------------------------
// Build list of template commit messages
// Private:
- (void) buildTemplatesList
{
for_each_obj(en, it, GetPreference(kPrefTemplates))
[fTemplates addObject: [[it mutableCopy] autorelease]];
[fTemplates sortUsingFunction: compareTemplateNames context: NULL];
[fTemplatesAC setContent: fTemplates];
}
//----------------------------------------------------------------------------------------
- (IBAction) addTemplate: (id) sender
{
#pragma unused(sender)
id obj = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"untitled", @"name",
@"template body", @"body", nil];
[fTemplatesAC addObject: obj];
[fTemplatesAC setSelectionIndex: [fTemplates count] - 1];
}
//----------------------------------------------------------------------------------------
- (void) saveTemplates
{
SetPreference(kPrefTemplates, fTemplates);
}
//----------------------------------------------------------------------------------------
- (void) setAllFilesCommit: (BOOL) commit
{
for_each_obj(oEnum, item, fFiles)
{
[item setCommit: commit];
}
[self setCommitFileCount: (commit ? [fFiles count] : 0)];
[fFilesAC rearrangeObjects];
}
//----------------------------------------------------------------------------------------
- (IBAction) checkAllFiles: (id) sender
{
#pragma unused(sender)
[self setAllFilesCommit: YES];
}
//----------------------------------------------------------------------------------------
- (IBAction) checkNoFiles: (id) sender
{
#pragma unused(sender)
[self setAllFilesCommit: NO];
}
//----------------------------------------------------------------------------------------
- (IBAction) refreshFiles: (id) sender
{
#pragma unused(sender)
[fDocument svnRefresh];
}
//----------------------------------------------------------------------------------------
- (IBAction) openSelectedFile: (id) sender
{
#pragma unused(sender)
ReviewFile* item = [self selectedItemOrNil];
if (item)
{
OpenFiles([item fullPath]);
}
}
//----------------------------------------------------------------------------------------
- (void) svnErrorAlertDidEnd: (NSAlert*) alert
returnCode: (int) returnCode
contextInfo: (void*) contextInfo
{
#pragma unused(alert, returnCode, contextInfo)
}
//----------------------------------------------------------------------------------------
- (BOOL) svnError: (id) taskObj
{
NSString* errMsg = nil;
const BOOL isErr = (!isCompleted(taskObj) && (errMsg = stdErr(taskObj)) != nil);
if (isErr)
{
if ([fWindow attachedSheet])
[NSApp endSheet: [fWindow attachedSheet]];
[[fDocument controller] stopProgressIndicator];
if ([fWindow isVisible])
{
NSAlert* alert = [NSAlert alertWithMessageText: @"Error"
defaultButton: @"OK"
alternateButton: nil
otherButton: nil
informativeTextWithFormat: @"%@", errMsg];
[alert setAlertStyle: NSCriticalAlertStyle];
[alert beginSheetModalForWindow: fWindow
modalDelegate: self
didEndSelector: @selector(svnErrorAlertDidEnd:returnCode:contextInfo:)
contextInfo: NULL];
}
}
return isErr;
}
//----------------------------------------------------------------------------------------
- (void) svnDiff_Completed: (id) taskObj
{
if ([fWindow isVisible])
{
[self svnError: taskObj];
}
[self release];
}
//----------------------------------------------------------------------------------------
- (IBAction) diffSelectedFile: (id) sender
{
#pragma unused(sender)
ReviewFile* item = [self selectedItemOrNil];
if (item)
[fDocument diffItems: [NSArray arrayWithObject: [item fullPath]]
callback: [self makeCallback: @selector(svnDiff_Completed:)]
callbackInfo: nil];
}
//----------------------------------------------------------------------------------------
- (void) svnCommit_Completed: (id) taskObj
{
// dprintf("0x%X taskObj=%@", self, taskObj);
if ([fWindow isVisible])
{
[self setIsBusy: NO];
if (![self svnError: taskObj])
{
[self refreshFiles: nil];
[self buildRecentList: NO];
}
}
[self release];
}
//----------------------------------------------------------------------------------------
- (void) doCommitFiles
{
Assert([self canCommit]);
NSMutableArray* commitFiles = [NSMutableArray array];
for_each_obj(oEnum, item, fFiles)
{
if ([item commit])
[commitFiles addObject: [item item]];
}
[self setIsBusy: YES];
[fDocument svnCommit: commitFiles
message: [fMessageView string]
callback: [self makeCallback: @selector(svnCommit_Completed:)]
callbackInfo: nil];
}
//----------------------------------------------------------------------------------------
- (void) commitFiles: (NSAlert*) alert
returnCode: (int) returnCode
contextInfo: (void*) context
{
#pragma unused(alert, context)
if (returnCode == NSOKButton)
{
fSuppressAutoRefresh = TRUE;
[self doCommitFiles];
}
}
//----------------------------------------------------------------------------------------
- (IBAction) commitFiles: (id) sender
{
#pragma unused(sender)
if (AltOrShiftPressed())
[self doCommitFiles];
else
{
NSAlert* alert =
[NSAlert alertWithMessageText: [NSString stringWithFormat:
@"Commit changes to the repository\nfor %u of %u items.",
fCommitFileCount, [fFiles count]]
defaultButton: nil
alternateButton: @"Cancel"
otherButton: nil
informativeTextWithFormat: @""];
[alert setAlertStyle: NSInformationalAlertStyle];
[alert beginSheetModalForWindow: fWindow
modalDelegate: self
didEndSelector: @selector(commitFiles:returnCode:contextInfo:)
contextInfo: nil];
}
}
//----------------------------------------------------------------------------------------
- (IBAction) toggleSelectedFile: (id) sender
{
#pragma unused(sender)
ReviewFile* item = [self selectedItemOrNil];
if (item)
{
const BOOL commit = ![item commit];
[item setCommit: commit];
NSRect r = [fFilesView rectOfRow: [fFilesView selectedRow]];
[fFilesView setNeedsDisplayInRect: r];
[self setCommitFileCount: fCommitFileCount + (commit ? 1 : -1)];
}
}
//----------------------------------------------------------------------------------------
- (IBAction) revealSelectedFile: (id) sender
{
#pragma unused(sender)
ReviewFile* item = [self selectedItemOrNil];
if (item)
{
[[NSWorkspace sharedWorkspace] selectFile: [item fullPath] inFileViewerRootedAtPath: nil];
}
}
//----------------------------------------------------------------------------------------
- (IBAction) doubleClick: (id) sender
{
[self openSelectedFile: sender];
}
//----------------------------------------------------------------------------------------
// The 'review.sh >> <tmpHtmlPath>' task has completed
- (void) taskCompleted: (Task*) task object: (id) tmpHtmlPath
{
if (task != fFileDiffTask && fFileDiffTask != nil)
return;
if (task == fFileDiffTask)
{
fFileDiffTask = nil;
[task release];
}
if ([fWindow isVisible])
[[fDiffView mainFrame] loadRequest: [NSURLRequest requestWithURL:
[NSURL fileURLWithPath: tmpHtmlPath]]];
}
//----------------------------------------------------------------------------------------
// Private:
- (NSString*) tmpHtmlPath
{
static unsigned int fileIndex = 0;
return [NSString stringWithFormat: @"/tmp/svnx-review-%X%c.html",
self, 'z' - (++fileIndex % kMaxTempHTMLFiles)];
}
//----------------------------------------------------------------------------------------
- (void) displayFileDiff: (ReviewFile*) item
{
// dprintf("item=%@ '%@'", item, [item name]);
if (item)
{
Task* task = fFileDiffTask;
if (task) // Kill old task
{
fFileDiffTask = nil;
[[task task] interrupt];
[task release];
}
// NSString* options = [NSString stringWithFormat: @"-r%@:1", fRevision];
NSString* tmpHtmlPath = [self tmpHtmlPath];
// review.sh <svn-tool> <options> <ctx-lines> <show-func> <show-chars> <dest-html> <paths...>
NSArray* arguments = [NSArray arrayWithObjects:
SvnCmdPath(), // svn tool
@"", // options
GetPreference(kPrefDefaultTab), // default tab
GetPreference(kPrefContextLines), // context lines
GetPreferenceBool(kPrefShowFunction) ? @"1" : @"", // show function
GetPreferenceBool(kPrefShowChars) ? @"1" : @"", // show characters
tmpHtmlPath, // destination html file
[item fullPath], // path
nil];
task = [Task taskWithDelegate: self object: tmpHtmlPath];
fFileDiffTask = [task retain];
[task launch: ShellScriptPath(@"review") arguments: arguments];
}
}
//----------------------------------------------------------------------------------------
- (void) displaySelectedFileDiff
{
[self displayFileDiff: [self selectedItemOrNil]];
}
//----------------------------------------------------------------------------------------
- (void) alertUserShouldClose
{
NSAlert* alert =
[NSAlert alertWithMessageText: @"Close this window?"
defaultButton: @"Close"
alternateButton: @"Cancel"
otherButton: nil
informativeTextWithFormat: @"You have selected items &"
" a message that has not been commited."];
[alert setAlertStyle: NSWarningAlertStyle];
[alert beginSheetModalForWindow: fWindow
modalDelegate: self
didEndSelector: @selector(shouldClose:returnCode:contextInfo:)
contextInfo: nil];
NSBeep();
}
//----------------------------------------------------------------------------------------
- (void) shouldClose: (NSAlert*) alert
returnCode: (int) returnCode
contextInfo: (void*) context
{
#pragma unused(alert, context)
if (returnCode == NSOKButton)
{
fSuppressAutoRefresh = TRUE;
[fWindow setDocumentEdited: FALSE];
[fWindow performSelector: @selector(performClose:) withObject: self afterDelay: 0];
}
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
- (BOOL) isBusy
{
return fIsBusy;
}
//----------------------------------------------------------------------------------------
- (void) setIsBusy: (BOOL) isBusy
{
fIsBusy = isBusy;
}
//----------------------------------------------------------------------------------------
- (BOOL) canCommit
{
return fCommitFileCount > 0 && [[fMessageView string] length] > 0;
}
//----------------------------------------------------------------------------------------
// Called by 'textDidChange' & 'setCommitFileCount'.
// Forces NIB to re-evaluate 'canCommit' and updates window 'dirty' flag.
- (void) setCanCommit: (id) ignored
{
#pragma unused(ignored)
[fWindow setDocumentEdited: [self canCommit]];
}
//----------------------------------------------------------------------------------------
- (int) commitFileCount
{
return fCommitFileCount;
}
//----------------------------------------------------------------------------------------
- (void) setCommitFileCount: (int) count
{
fCommitFileCount = count;
[self setCanCommit: nil];
}
//----------------------------------------------------------------------------------------
- (IBAction) changeEditView: (id) sender
{
#pragma unused(sender)
// dprintf("%d", [sender selectedSegment]);
// [self setEditPane: [sender selectedSegment]];
}
//----------------------------------------------------------------------------------------
- (BOOL) hideMessage { return fEditState != kPaneMessage; }
- (BOOL) hideRecent { return fEditState != kPaneRecent; }
- (BOOL) hideTemplates { return fEditState != kPaneTemplates; }
//----------------------------------------------------------------------------------------
- (void) setHideMessage: (BOOL) state { _Pragma("unused(state)") }
- (void) setHideRecent: (BOOL) state { _Pragma("unused(state)") }
- (void) setHideTemplates: (BOOL) state { _Pragma("unused(state)") }
//----------------------------------------------------------------------------------------
- (int) editPane
{
return fEditState;
}
//----------------------------------------------------------------------------------------
- (void) setEditPane: (int) pane
{
fEditState = pane;
[self setHideMessage: (pane != kPaneMessage)];
[self setHideRecent: (pane != kPaneRecent)];
[self setHideTemplates: (pane != kPaneTemplates)];
if (pane == kPaneMessage)
[fWindow makeFirstResponder: fMessageView];
else if (pane == kPaneRecent)
[fWindow makeFirstResponder: fRecentView];
else if (pane == kPaneTemplates)
[fWindow makeFirstResponder: fTemplatesView];
}
//----------------------------------------------------------------------------------------
- (NSWindow*) window
{
return fWindow;
}
//----------------------------------------------------------------------------------------
- (BOOL) isDocumentEdited
{
return [fWindow isDocumentEdited];
}
//----------------------------------------------------------------------------------------
- (void) textDidChange: (NSNotification*) notification
{
#pragma unused(notification)
[self setCanCommit: nil];
}
//----------------------------------------------------------------------------------------
- (void) insertRecent: (id) sender
{
#pragma unused(sender)
int rowIndex = [fRecentView selectedRow];
if (rowIndex >= 0)
{
NSString* str = [[[fRecentAC arrangedObjects] objectAtIndex: rowIndex] objectForKey: @"log"];
NSRange range = [str rangeOfString: @"\n"];
str = [str substringFromIndex: range.location + 1];
[fMessageView insertText: str];
[self setEditPane: kPaneMessage];
}
else
NSBeep();
}
//----------------------------------------------------------------------------------------
// Calls a script with the args: svnBinDir, wcPath, 3, commit-file-names...
- (NSString*) insertTemplateScript: (NSString*) script
{
const NSStringEncoding kEncoding = NSUTF8StringEncoding;
NSMutableArray* args = [NSMutableArray arrayWithObjects:
GetPreference(@"svnBinariesFolder"),
[fDocument workingCopyPath],
@"3", // count of args before first file
nil];
for_each_obj(oEnum, it, fFiles)
{
if ([it commit])
[args addObject: [it name]];
}
// dprintf("args=%@\nscript='%@'", args, script);
NSString* result = @"[SCRIPT: Couldn't run]";
static unsigned int uid = 0;
NSString* const path = [NSString stringWithFormat: @"/tmp/svnx%u-script%u.sh", getpid(), ++uid];
char cpath[64];
if ([[script normalizeEOLs] writeToFile: path atomically: NO encoding: kEncoding error: nil] &&
[path getCString: cpath maxLength: sizeof(cpath) encoding: kEncoding] &&
chmod(cpath, S_IRWXU) == 0)
{
NSPipe* const pipe = [NSPipe pipe];
Task* const task = [[Task task] retain];
[task setStandardOutput: pipe];
[task launch: path arguments: args];
NSTask* const nsTask = [task task];
NSFileHandle* const handle = [pipe fileHandleForReading];
NSMutableData* const data = [NSMutableData dataWithLength: 0];
const UTCTime endTime = CFAbsoluteTimeGetCurrent() + 30; // Wait a max of 30 secs
while ([nsTask isRunning] && CFAbsoluteTimeGetCurrent() < endTime)
{
// [NSThread sleepForTimeInterval: 1.0 / 8];
[data appendData: [handle availableData]];
}