-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommitWindowCommandLine.m
205 lines (164 loc) · 5.39 KB
/
CommitWindowCommandLine.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
//
// CommitWindowCommandLine.m
// CommitWindow
//
// Created by Chris Thomas on 6/24/06.
// Copyright 2006 Chris Thomas. All rights reserved.
//
#import "CommitWindowCommandLine.h"
@implementation CommitWindowController(CommandLine)
- (void) awakeFromNib
{
NSProcessInfo * processInfo = [NSProcessInfo processInfo];
NSArray * args;
int i;
int argc;
args = [processInfo arguments];
argc = [args count];
if( args == nil || argc < 2 )
{
fprintf(stderr, "commit window: Arguments required\n");
[self cancel:nil];
}
//
// Parse the command line.
//
// Program name is the first argument -- get rid of it. (COW semantics should make this cheaper than it looks.)
argc -= 1;
args = [args subarrayWithRange:NSMakeRange(1, argc)];
// Populate our NSArrayController with the command line arguments
[fFilesController objectDidBeginEditing:nil];
for( i = 0; i < argc; i += 1 )
{
NSString * argument = [args objectAtIndex:i];
if( [argument isEqualToString:@"--ask"] )
{
// Next argument should be the query text.
if( i >= (argc - 1) )
{
fprintf(stderr, "commit window: missing text: --ask \"some text\"\n");
[self cancel:nil];
}
i += 1;
argument = [args objectAtIndex:i];
[fRequestText setStringValue:argument];
}
else if( [argument isEqualToString:@"--status"] )
{
// Next argument should be a colon-seperated list of status strings, one for each path
if( i >= (argc - 1) )
{
fprintf(stderr, "commit window: missing text: --status \"A:D:M:M:M\"\n");
[self cancel:nil];
}
i += 1;
argument = [args objectAtIndex:i];
fFileStatusStrings = [[argument componentsSeparatedByString:@":"] retain];
}
else if( [argument isEqualToString:@"--diff-cmd"] )
{
// Next argument should be a comma-seperated list of command arguments to use to execute the diff
if( i >= (argc - 1) )
{
fprintf(stderr, "commit window: missing text: --diff-cmd \"/usr/bin/svn,diff\"\n");
[self cancel:nil];
}
i += 1;
argument = [args objectAtIndex:i];
fDiffCommand = [argument retain];
}
else
{
NSMutableDictionary * dictionary = [fFilesController newObject];
[dictionary setObject:[argument stringByAbbreviatingWithTildeInPath] forKey:@"path"];
[fFilesController addObject:dictionary];
}
}
//
// Done processing arguments, now add status to each item
// and choose default commit state
//
[self setupUserInterface];
}
#if 0
#pragma mark -
#pragma mark Actions
#endif
- (IBAction) commit:(id) sender
{
NSArray * objects = [fFilesController arrangedObjects];
int i;
int pathsToCommitCount = 0;
NSMutableString * commitString;
[self saveSummary];
//
// Quote any single-quotes in the commit message
// \' doesn't work with bash. We must use string concatenation.
// This sort of thing is why the Unix Hater's Handbook exists.
//
commitString = [[[fCommitMessage string] mutableCopy] autorelease];
[commitString replaceOccurrencesOfString:@"'" withString:@"'\"'\"'" options:0 range:NSMakeRange(0, [commitString length])];
fprintf(stdout, "-m '%s' ", [commitString UTF8String] );
//
// Return only the files we care about
//
for( i = 0; i < [objects count]; i += 1 )
{
NSMutableDictionary * dictionary;
NSNumber * commit;
dictionary = [objects objectAtIndex:i];
commit = [dictionary objectForKey:@"commit"];
if( commit == nil || [commit boolValue] ) // missing commit key defaults to true
{
NSMutableString * path;
//
// Quote any single-quotes in the path
//
path = [dictionary objectForKey:@"path"];
path = [[[path stringByStandardizingPath] mutableCopy] autorelease];
[path replaceOccurrencesOfString:@"'" withString:@"'\"'\"'" options:0 range:NSMakeRange(0, [path length])];
fprintf( stdout, "'%s' ", [path UTF8String] );
pathsToCommitCount += 1;
}
}
fprintf( stdout, "\n" );
//
// SVN will commit the current directory, recursively, if we don't specify files.
// So, to prevent surprises, if the user's unchecked all the boxes, let's be on the safe side and cancel.
//
if( pathsToCommitCount == 0 )
{
[self cancel:nil];
}
[NSApp terminate:self];
}
- (IBAction) cancel:(id) sender
{
[self saveSummary];
fprintf(stdout, "commit window: cancel\n");
exit(-128);
}
- (IBAction) doubleClickRowInTable:(id)sender
{
if(fDiffCommand != nil)
{
NSArray * unquotedArguments = [fDiffCommand componentsSeparatedByString:@","];
NSString * quoteArgument = @"\"%@\"";
NSString * diffCommand = @"";
NSString * mateCommand = [NSString stringWithFormat:@"\"%s/bin/mate\" &> /dev/null &", getenv("TM_SUPPORT_PATH")];
NSString * filePath = [[[fFilesController arrangedObjects] objectAtIndex:[sender selectedRow]] objectForKey:@"path"];
unsigned int argumentCount = [unquotedArguments count];
unsigned int index;
for(index = 0; index < argumentCount; index += 1)
{
NSString * argument = [unquotedArguments objectAtIndex:index];
NSString * quotedArgument = [NSString stringWithFormat:quoteArgument, argument];
diffCommand = [diffCommand stringByAppendingString:quotedArgument];
diffCommand = [diffCommand stringByAppendingString:@" "];
}
diffCommand = [diffCommand stringByAppendingString:[NSString stringWithFormat:quoteArgument, filePath]];
diffCommand = [diffCommand stringByAppendingString:[NSString stringWithFormat:@" | %@", mateCommand]];
system([diffCommand UTF8String]);
}
}
@end