Allow diff and index commands to be interrupted
[git/dscho.git] / rev-parse.c
blob6bf205a8f53c6af76bb1cc4df7bf37e46d378890
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
9 #include "quote.h"
11 #define DO_REVS 1
12 #define DO_NOREV 2
13 #define DO_FLAGS 4
14 #define DO_NONFLAGS 8
15 static int filter = ~0;
17 static char *def = NULL;
19 #define NORMAL 0
20 #define REVERSED 1
21 static int show_type = NORMAL;
22 static int symbolic = 0;
23 static int abbrev = 0;
24 static int output_sq = 0;
26 static int revs_count = 0;
29 * Some arguments are relevant "revision" arguments,
30 * others are about output format or other details.
31 * This sorts it all out.
33 static int is_rev_argument(const char *arg)
35 static const char *rev_args[] = {
36 "--all",
37 "--bisect",
38 "--dense",
39 "--header",
40 "--max-age=",
41 "--max-count=",
42 "--merge-order",
43 "--min-age=",
44 "--no-merges",
45 "--objects",
46 "--parents",
47 "--pretty",
48 "--show-breaks",
49 "--sparse",
50 "--topo-order",
51 "--unpacked",
52 NULL
54 const char **p = rev_args;
56 /* accept -<digit>, like traditional "head" */
57 if ((*arg == '-') && isdigit(arg[1]))
58 return 1;
60 for (;;) {
61 const char *str = *p++;
62 int len;
63 if (!str)
64 return 0;
65 len = strlen(str);
66 if (!strcmp(arg, str) ||
67 (str[len-1] == '=' && !strncmp(arg, str, len)))
68 return 1;
72 /* Output argument as a string, either SQ or normal */
73 static void show(const char *arg)
75 if (output_sq) {
76 int sq = '\'', ch;
78 putchar(sq);
79 while ((ch = *arg++)) {
80 if (ch == sq)
81 fputs("'\\'", stdout);
82 putchar(ch);
84 putchar(sq);
85 putchar(' ');
87 else
88 puts(arg);
91 /* Output a revision, only if filter allows it */
92 static void show_rev(int type, const unsigned char *sha1, const char *name)
94 if (!(filter & DO_REVS))
95 return;
96 def = NULL;
97 revs_count++;
99 if (type != show_type)
100 putchar('^');
101 if (symbolic && name)
102 show(name);
103 else if (abbrev)
104 show(find_unique_abbrev(sha1, abbrev));
105 else
106 show(sha1_to_hex(sha1));
109 /* Output a flag, only if filter allows it. */
110 static void show_flag(char *arg)
112 if (!(filter & DO_FLAGS))
113 return;
114 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
115 show(arg);
118 static void show_default(void)
120 char *s = def;
122 if (s) {
123 unsigned char sha1[20];
125 def = NULL;
126 if (!get_sha1(s, sha1)) {
127 show_rev(NORMAL, sha1, s);
128 return;
133 static int show_reference(const char *refname, const unsigned char *sha1)
135 show_rev(NORMAL, sha1, refname);
136 return 0;
139 static void show_datestring(const char *flag, const char *datestr)
141 static char buffer[100];
143 /* date handling requires both flags and revs */
144 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
145 return;
146 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
147 show(buffer);
150 static void show_file(const char *arg)
152 show_default();
153 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
154 show(arg);
157 int main(int argc, char **argv)
159 int i, as_is = 0, verify = 0;
160 unsigned char sha1[20];
161 const char *prefix = setup_git_directory();
163 for (i = 1; i < argc; i++) {
164 struct stat st;
165 char *arg = argv[i];
166 char *dotdot;
168 if (as_is) {
169 show_file(arg);
170 continue;
172 if (!strcmp(arg,"-n")) {
173 if (++i >= argc)
174 die("-n requires an argument");
175 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
176 show(arg);
177 show(argv[i]);
179 continue;
181 if (!strncmp(arg,"-n",2)) {
182 if ((filter & DO_FLAGS) && (filter & DO_REVS))
183 show(arg);
184 continue;
187 if (*arg == '-') {
188 if (!strcmp(arg, "--")) {
189 as_is = 1;
190 /* Pass on the "--" if we show anything but files.. */
191 if (filter & (DO_FLAGS | DO_REVS))
192 show_file(arg);
193 continue;
195 if (!strcmp(arg, "--default")) {
196 def = argv[i+1];
197 i++;
198 continue;
200 if (!strcmp(arg, "--revs-only")) {
201 filter &= ~DO_NOREV;
202 continue;
204 if (!strcmp(arg, "--no-revs")) {
205 filter &= ~DO_REVS;
206 continue;
208 if (!strcmp(arg, "--flags")) {
209 filter &= ~DO_NONFLAGS;
210 continue;
212 if (!strcmp(arg, "--no-flags")) {
213 filter &= ~DO_FLAGS;
214 continue;
216 if (!strcmp(arg, "--verify")) {
217 filter &= ~(DO_FLAGS|DO_NOREV);
218 verify = 1;
219 continue;
221 if (!strcmp(arg, "--short") ||
222 !strncmp(arg, "--short=", 9)) {
223 filter &= ~(DO_FLAGS|DO_NOREV);
224 verify = 1;
225 abbrev = DEFAULT_ABBREV;
226 if (arg[8] == '=')
227 abbrev = strtoul(arg + 9, NULL, 10);
228 if (abbrev < MINIMUM_ABBREV)
229 abbrev = MINIMUM_ABBREV;
230 else if (40 <= abbrev)
231 abbrev = 40;
232 continue;
234 if (!strcmp(arg, "--sq")) {
235 output_sq = 1;
236 continue;
238 if (!strcmp(arg, "--not")) {
239 show_type ^= REVERSED;
240 continue;
242 if (!strcmp(arg, "--symbolic")) {
243 symbolic = 1;
244 continue;
246 if (!strcmp(arg, "--all")) {
247 for_each_ref(show_reference);
248 continue;
250 if (!strcmp(arg, "--show-prefix")) {
251 if (prefix)
252 puts(prefix);
253 continue;
255 if (!strcmp(arg, "--show-cdup")) {
256 const char *pfx = prefix;
257 while (pfx) {
258 pfx = strchr(pfx, '/');
259 if (pfx) {
260 pfx++;
261 printf("../");
264 putchar('\n');
265 continue;
267 if (!strcmp(arg, "--git-dir")) {
268 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
269 static char cwd[PATH_MAX];
270 if (gitdir) {
271 puts(gitdir);
272 continue;
274 if (!prefix) {
275 puts(".git");
276 continue;
278 if (!getcwd(cwd, PATH_MAX))
279 die("unable to get current working directory");
280 printf("%s/.git\n", cwd);
281 continue;
283 if (!strncmp(arg, "--since=", 8)) {
284 show_datestring("--max-age=", arg+8);
285 continue;
287 if (!strncmp(arg, "--after=", 8)) {
288 show_datestring("--max-age=", arg+8);
289 continue;
291 if (!strncmp(arg, "--before=", 9)) {
292 show_datestring("--min-age=", arg+9);
293 continue;
295 if (!strncmp(arg, "--until=", 8)) {
296 show_datestring("--min-age=", arg+8);
297 continue;
299 if (verify)
300 die("Needed a single revision");
301 show_flag(arg);
302 continue;
305 /* Not a flag argument */
306 dotdot = strstr(arg, "..");
307 if (dotdot) {
308 unsigned char end[20];
309 char *n = dotdot+2;
310 *dotdot = 0;
311 if (!get_sha1(arg, sha1)) {
312 if (!*n)
313 n = "HEAD";
314 if (!get_sha1(n, end)) {
315 show_rev(NORMAL, end, n);
316 show_rev(REVERSED, sha1, arg);
317 continue;
320 *dotdot = '.';
322 if (!get_sha1(arg, sha1)) {
323 show_rev(NORMAL, sha1, arg);
324 continue;
326 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
327 show_rev(REVERSED, sha1, arg+1);
328 continue;
330 if (verify)
331 die("Needed a single revision");
332 if ((filter & DO_REVS) &&
333 (filter & DO_NONFLAGS) && /* !def && */
334 lstat(arg, &st) < 0)
335 die("'%s': %s", arg, strerror(errno));
336 as_is = 1;
337 show_file(arg);
339 show_default();
340 if (verify && revs_count != 1)
341 die("Needed a single revision");
342 return 0;