Merge with master to pick up safety patches.
[git.git] / rev-parse.c
1 /*
2  * rev-parse.c
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
9
10 static char *def = NULL;
11 static int no_revs = 0;
12 static int single_rev = 0;
13 static int revs_only = 0;
14 static int do_rev_argument = 1;
15 static int output_revs = 0;
16 static int flags_only = 0;
17 static int no_flags = 0;
18 static int output_sq = 0;
19 static int symbolic = 0;
20
21 #define NORMAL 0
22 #define REVERSED 1
23 static int show_type = NORMAL;
24
25 /*
26  * Some arguments are relevant "revision" arguments,
27  * others are about output format or other details.
28  * This sorts it all out.
29  */
30 static int is_rev_argument(const char *arg)
31 {
32         static const char *rev_args[] = {
33                 "--max-count=",
34                 "--max-age=",
35                 "--min-age=",
36                 "--merge-order",
37                 "--topo-order",
38                 "--bisect",
39                 "--no-merges",
40                 NULL
41         };
42         const char **p = rev_args;
43
44         for (;;) {
45                 const char *str = *p++;
46                 int len;
47                 if (!str)
48                         return 0;
49                 len = strlen(str);
50                 if (!strncmp(arg, str, len))
51                         return 1;
52         }
53 }
54
55 static void show(const char *arg)
56 {
57         if (output_sq) {
58                 int sq = '\'', ch;
59
60                 putchar(sq);
61                 while ((ch = *arg++)) {
62                         if (ch == sq)
63                                 fputs("'\\'", stdout);
64                         putchar(ch);
65                 }
66                 putchar(sq);
67                 putchar(' ');
68         }
69         else
70                 puts(arg);
71 }
72
73 static void show_rev(int type, const unsigned char *sha1, const char *name)
74 {
75         if (no_revs)
76                 return;
77         output_revs++;
78
79         if (type != show_type)
80                 putchar('^');
81         if (symbolic && name)
82                 show(name);
83         else
84                 show(sha1_to_hex(sha1));
85 }
86
87 static void show_rev_arg(char *rev)
88 {
89         if (no_revs)
90                 return;
91         show(rev);
92 }
93
94 static void show_norev(char *norev)
95 {
96         if (flags_only)
97                 return;
98         if (revs_only)
99                 return;
100         show(norev);
101 }
102
103 static void show_arg(char *arg)
104 {
105         if (no_flags)
106                 return;
107         if (do_rev_argument && is_rev_argument(arg))
108                 show_rev_arg(arg);
109         else
110                 show_norev(arg);
111 }
112
113 static void show_default(void)
114 {
115         char *s = def;
116
117         if (s) {
118                 unsigned char sha1[20];
119
120                 def = NULL;
121                 if (!get_sha1(s, sha1)) {
122                         show_rev(NORMAL, sha1, s);
123                         return;
124                 }
125                 show_arg(s);
126         }
127 }
128
129 static int show_reference(const char *refname, const unsigned char *sha1)
130 {
131         show_rev(NORMAL, sha1, refname);
132         return 0;
133 }
134
135 int main(int argc, char **argv)
136 {
137         int i, as_is = 0;
138         unsigned char sha1[20];
139         const char *prefix = setup_git_directory();
140         
141         for (i = 1; i < argc; i++) {
142                 char *arg = argv[i];
143                 char *dotdot;
144         
145                 if (as_is) {
146                         show_norev(arg);
147                         continue;
148                 }
149                 if (*arg == '-') {
150                         if (!strcmp(arg, "--")) {
151                                 show_default();
152                                 if (revs_only)
153                                         break;
154                                 as_is = 1;
155                         }
156                         if (!strcmp(arg, "--default")) {
157                                 def = argv[i+1];
158                                 i++;
159                                 continue;
160                         }
161                         if (!strcmp(arg, "--revs-only")) {
162                                 revs_only = 1;
163                                 continue;
164                         }
165                         if (!strcmp(arg, "--no-revs")) {
166                                 no_revs = 1;
167                                 continue;
168                         }
169                         if (!strcmp(arg, "--flags")) {
170                                 flags_only = 1;
171                                 continue;
172                         }
173                         if (!strcmp(arg, "--no-flags")) {
174                                 no_flags = 1;
175                                 continue;
176                         }
177                         if (!strcmp(arg, "--verify")) {
178                                 revs_only = 1;
179                                 do_rev_argument = 0;
180                                 single_rev = 1;
181                                 continue;
182                         }
183                         if (!strcmp(arg, "--sq")) {
184                                 output_sq = 1;
185                                 continue;
186                         }
187                         if (!strcmp(arg, "--not")) {
188                                 show_type ^= REVERSED;
189                                 continue;
190                         }
191                         if (!strcmp(arg, "--symbolic")) {
192                                 symbolic = 1;
193                                 continue;
194                         }
195                         if (!strcmp(arg, "--all")) {
196                                 for_each_ref(show_reference);
197                                 continue;
198                         }
199                         if (!strcmp(arg, "--show-prefix")) {
200                                 puts(prefix);
201                                 continue;
202                         }
203                         show_arg(arg);
204                         continue;
205                 }
206                 dotdot = strstr(arg, "..");
207                 if (dotdot) {
208                         unsigned char end[20];
209                         char *n = dotdot+2;
210                         *dotdot = 0;
211                         if (!get_sha1(arg, sha1)) {
212                                 if (!*n)
213                                         n = "HEAD";
214                                 if (!get_sha1(n, end)) {
215                                         if (no_revs)
216                                                 continue;
217                                         def = NULL;
218                                         show_rev(NORMAL, end, n);
219                                         show_rev(REVERSED, sha1, arg);
220                                         continue;
221                                 }
222                         }
223                         *dotdot = '.';
224                 }
225                 if (!get_sha1(arg, sha1)) {
226                         if (no_revs)
227                                 continue;
228                         def = NULL;
229                         show_rev(NORMAL, sha1, arg);
230                         continue;
231                 }
232                 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
233                         if (no_revs)
234                                 continue;
235                         def = NULL;
236                         show_rev(REVERSED, sha1, arg+1);
237                         continue;
238                 }
239                 show_default();
240                 show_norev(arg);
241         }
242         show_default();
243         if (single_rev && output_revs != 1) {
244                 fprintf(stderr, "Needed a single revision\n");
245                 exit(1);
246         }
247         return 0;
248 }