Demote git-p4import to contrib status.
[git/mingw/4msysgit/kblees.git] / builtin-rev-parse.c
blob497903a85a69288afc291ff49e32f7c80140beae
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"
10 #include "builtin.h"
12 #define DO_REVS 1
13 #define DO_NOREV 2
14 #define DO_FLAGS 4
15 #define DO_NONFLAGS 8
16 static int filter = ~0;
18 static const char *def;
20 #define NORMAL 0
21 #define REVERSED 1
22 static int show_type = NORMAL;
23 static int symbolic;
24 static int abbrev;
25 static int output_sq;
27 static int revs_count;
30 * Some arguments are relevant "revision" arguments,
31 * others are about output format or other details.
32 * This sorts it all out.
34 static int is_rev_argument(const char *arg)
36 static const char *rev_args[] = {
37 "--all",
38 "--bisect",
39 "--dense",
40 "--branches",
41 "--header",
42 "--max-age=",
43 "--max-count=",
44 "--min-age=",
45 "--no-merges",
46 "--objects",
47 "--objects-edge",
48 "--parents",
49 "--pretty",
50 "--remotes",
51 "--sparse",
52 "--tags",
53 "--topo-order",
54 "--date-order",
55 "--unpacked",
56 NULL
58 const char **p = rev_args;
60 /* accept -<digit>, like traditional "head" */
61 if ((*arg == '-') && isdigit(arg[1]))
62 return 1;
64 for (;;) {
65 const char *str = *p++;
66 int len;
67 if (!str)
68 return 0;
69 len = strlen(str);
70 if (!strcmp(arg, str) ||
71 (str[len-1] == '=' && !strncmp(arg, str, len)))
72 return 1;
76 /* Output argument as a string, either SQ or normal */
77 static void show(const char *arg)
79 if (output_sq) {
80 int sq = '\'', ch;
82 putchar(sq);
83 while ((ch = *arg++)) {
84 if (ch == sq)
85 fputs("'\\'", stdout);
86 putchar(ch);
88 putchar(sq);
89 putchar(' ');
91 else
92 puts(arg);
95 /* Output a revision, only if filter allows it */
96 static void show_rev(int type, const unsigned char *sha1, const char *name)
98 if (!(filter & DO_REVS))
99 return;
100 def = NULL;
101 revs_count++;
103 if (type != show_type)
104 putchar('^');
105 if (symbolic && name)
106 show(name);
107 else if (abbrev)
108 show(find_unique_abbrev(sha1, abbrev));
109 else
110 show(sha1_to_hex(sha1));
113 /* Output a flag, only if filter allows it. */
114 static int show_flag(const char *arg)
116 if (!(filter & DO_FLAGS))
117 return 0;
118 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
119 show(arg);
120 return 1;
122 return 0;
125 static void show_default(void)
127 const char *s = def;
129 if (s) {
130 unsigned char sha1[20];
132 def = NULL;
133 if (!get_sha1(s, sha1)) {
134 show_rev(NORMAL, sha1, s);
135 return;
140 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
142 show_rev(NORMAL, sha1, refname);
143 return 0;
146 static void show_datestring(const char *flag, const char *datestr)
148 static char buffer[100];
150 /* date handling requires both flags and revs */
151 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
152 return;
153 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
154 show(buffer);
157 static int show_file(const char *arg)
159 show_default();
160 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
161 show(arg);
162 return 1;
164 return 0;
167 static int try_difference(const char *arg)
169 char *dotdot;
170 unsigned char sha1[20];
171 unsigned char end[20];
172 const char *next;
173 const char *this;
174 int symmetric;
176 if (!(dotdot = strstr(arg, "..")))
177 return 0;
178 next = dotdot + 2;
179 this = arg;
180 symmetric = (*next == '.');
182 *dotdot = 0;
183 next += symmetric;
185 if (!*next)
186 next = "HEAD";
187 if (dotdot == arg)
188 this = "HEAD";
189 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
190 show_rev(NORMAL, end, next);
191 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
192 if (symmetric) {
193 struct commit_list *exclude;
194 struct commit *a, *b;
195 a = lookup_commit_reference(sha1);
196 b = lookup_commit_reference(end);
197 exclude = get_merge_bases(a, b, 1);
198 while (exclude) {
199 struct commit_list *n = exclude->next;
200 show_rev(REVERSED,
201 exclude->item->object.sha1,NULL);
202 free(exclude);
203 exclude = n;
206 return 1;
208 *dotdot = '.';
209 return 0;
212 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
214 int i, as_is = 0, verify = 0;
215 unsigned char sha1[20];
217 git_config(git_default_config);
219 for (i = 1; i < argc; i++) {
220 const char *arg = argv[i];
222 if (as_is) {
223 if (show_file(arg) && as_is < 2)
224 verify_filename(prefix, arg);
225 continue;
227 if (!strcmp(arg,"-n")) {
228 if (++i >= argc)
229 die("-n requires an argument");
230 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
231 show(arg);
232 show(argv[i]);
234 continue;
236 if (!prefixcmp(arg, "-n")) {
237 if ((filter & DO_FLAGS) && (filter & DO_REVS))
238 show(arg);
239 continue;
242 if (*arg == '-') {
243 if (!strcmp(arg, "--")) {
244 as_is = 2;
245 /* Pass on the "--" if we show anything but files.. */
246 if (filter & (DO_FLAGS | DO_REVS))
247 show_file(arg);
248 continue;
250 if (!strcmp(arg, "--default")) {
251 def = argv[i+1];
252 i++;
253 continue;
255 if (!strcmp(arg, "--revs-only")) {
256 filter &= ~DO_NOREV;
257 continue;
259 if (!strcmp(arg, "--no-revs")) {
260 filter &= ~DO_REVS;
261 continue;
263 if (!strcmp(arg, "--flags")) {
264 filter &= ~DO_NONFLAGS;
265 continue;
267 if (!strcmp(arg, "--no-flags")) {
268 filter &= ~DO_FLAGS;
269 continue;
271 if (!strcmp(arg, "--verify")) {
272 filter &= ~(DO_FLAGS|DO_NOREV);
273 verify = 1;
274 continue;
276 if (!strcmp(arg, "--short") ||
277 !prefixcmp(arg, "--short=")) {
278 filter &= ~(DO_FLAGS|DO_NOREV);
279 verify = 1;
280 abbrev = DEFAULT_ABBREV;
281 if (arg[7] == '=')
282 abbrev = strtoul(arg + 8, NULL, 10);
283 if (abbrev < MINIMUM_ABBREV)
284 abbrev = MINIMUM_ABBREV;
285 else if (40 <= abbrev)
286 abbrev = 40;
287 continue;
289 if (!strcmp(arg, "--sq")) {
290 output_sq = 1;
291 continue;
293 if (!strcmp(arg, "--not")) {
294 show_type ^= REVERSED;
295 continue;
297 if (!strcmp(arg, "--symbolic")) {
298 symbolic = 1;
299 continue;
301 if (!strcmp(arg, "--all")) {
302 for_each_ref(show_reference, NULL);
303 continue;
305 if (!strcmp(arg, "--branches")) {
306 for_each_branch_ref(show_reference, NULL);
307 continue;
309 if (!strcmp(arg, "--tags")) {
310 for_each_tag_ref(show_reference, NULL);
311 continue;
313 if (!strcmp(arg, "--remotes")) {
314 for_each_remote_ref(show_reference, NULL);
315 continue;
317 if (!strcmp(arg, "--show-prefix")) {
318 if (prefix)
319 puts(prefix);
320 continue;
322 if (!strcmp(arg, "--show-cdup")) {
323 const char *pfx = prefix;
324 while (pfx) {
325 pfx = strchr(pfx, '/');
326 if (pfx) {
327 pfx++;
328 printf("../");
331 putchar('\n');
332 continue;
334 if (!strcmp(arg, "--git-dir")) {
335 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
336 static char cwd[PATH_MAX];
337 if (gitdir) {
338 puts(gitdir);
339 continue;
341 if (!prefix) {
342 puts(".git");
343 continue;
345 if (!getcwd(cwd, PATH_MAX))
346 die("unable to get current working directory");
347 printf("%s/.git\n", cwd);
348 continue;
350 if (!strcmp(arg, "--is-inside-git-dir")) {
351 printf("%s\n", is_inside_git_dir() ? "true"
352 : "false");
353 continue;
355 if (!strcmp(arg, "--is-inside-work-tree")) {
356 printf("%s\n", is_inside_work_tree() ? "true"
357 : "false");
358 continue;
360 if (!strcmp(arg, "--is-bare-repository")) {
361 printf("%s\n", is_bare_repository() ? "true"
362 : "false");
363 continue;
365 if (!prefixcmp(arg, "--since=")) {
366 show_datestring("--max-age=", arg+8);
367 continue;
369 if (!prefixcmp(arg, "--after=")) {
370 show_datestring("--max-age=", arg+8);
371 continue;
373 if (!prefixcmp(arg, "--before=")) {
374 show_datestring("--min-age=", arg+9);
375 continue;
377 if (!prefixcmp(arg, "--until=")) {
378 show_datestring("--min-age=", arg+8);
379 continue;
381 if (show_flag(arg) && verify)
382 die("Needed a single revision");
383 continue;
386 /* Not a flag argument */
387 if (try_difference(arg))
388 continue;
389 if (!get_sha1(arg, sha1)) {
390 show_rev(NORMAL, sha1, arg);
391 continue;
393 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
394 show_rev(REVERSED, sha1, arg+1);
395 continue;
397 as_is = 1;
398 if (!show_file(arg))
399 continue;
400 if (verify)
401 die("Needed a single revision");
402 verify_filename(prefix, arg);
404 show_default();
405 if (verify && revs_count != 1)
406 die("Needed a single revision");
407 return 0;