git-rev-parse: split "revs" and "non-revs"
[git/git.git] / rev-parse.c
1 /*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7
8 int main(int argc, char **argv)
9 {
10 int i, as_is = 0, revs_only = 0, no_revs = 0;
11 char *def = NULL;
12 unsigned char sha1[20];
13
14 for (i = 1; i < argc; i++) {
15 char *arg = argv[i];
16 char *dotdot;
17
18 if (as_is) {
19 printf("%s\n", arg);
20 continue;
21 }
22 if (*arg == '-') {
23 if (!strcmp(arg, "--")) {
24 if (def) {
25 printf("%s\n", def);
26 def = NULL;
27 }
28 if (revs_only)
29 break;
30 as_is = 1;
31 }
32 if (!strcmp(arg, "--default")) {
33 if (def)
34 printf("%s\n", def);
35 def = argv[i+1];
36 i++;
37 continue;
38 }
39 if (!strcmp(arg, "--revs-only")) {
40 revs_only = 1;
41 continue;
42 }
43 if (!strcmp(arg, "--no-revs")) {
44 no_revs = 1;
45 continue;
46 }
47 if (revs_only)
48 continue;
49 printf("%s\n", arg);
50 continue;
51 }
52 if (!get_sha1(arg, sha1)) {
53 if (no_revs)
54 continue;
55 def = NULL;
56 printf("%s\n", sha1_to_hex(sha1));
57 continue;
58 }
59 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
60 if (no_revs)
61 continue;
62 def = NULL;
63 printf("^%s\n", sha1_to_hex(sha1));
64 continue;
65 }
66 dotdot = strstr(arg, "..");
67 if (dotdot) {
68 unsigned char end[20];
69 char *n = dotdot+2;
70 *dotdot = 0;
71 if (!get_sha1(arg, sha1)) {
72 if (!*n)
73 n = "HEAD";
74 if (!get_sha1(n, end)) {
75 if (no_revs)
76 continue;
77 def = NULL;
78 printf("%s\n", sha1_to_hex(end));
79 printf("^%s\n", sha1_to_hex(sha1));
80 continue;
81 }
82 }
83 *dotdot = '.';
84 }
85 if (revs_only)
86 continue;
87 def = NULL;
88 printf("%s\n", arg);
89 }
90 if (def)
91 printf("%s\n", def);
92 return 0;
93 }