Merge branch 'fix'
[git/git.git] / rev-parse.c
CommitLineData
178cb243
LT
1/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
a8be83fe 7#include "commit.h"
960bba0d 8#include "refs.h"
c1babb1d 9#include "quote.h"
a8be83fe 10
4866ccf0
JH
11#define DO_REVS 1
12#define DO_NOREV 2
13#define DO_FLAGS 4
14#define DO_NONFLAGS 8
15static int filter = ~0;
16
023d66ed 17static char *def = NULL;
023d66ed 18
042a4ed7
LT
19#define NORMAL 0
20#define REVERSED 1
21static int show_type = NORMAL;
4866ccf0 22static int symbolic = 0;
d5012508 23static int abbrev = 0;
4866ccf0
JH
24static int output_sq = 0;
25
26static int revs_count = 0;
042a4ed7 27
921d865e
LT
28/*
29 * Some arguments are relevant "revision" arguments,
30 * others are about output format or other details.
31 * This sorts it all out.
32 */
33static int is_rev_argument(const char *arg)
34{
35 static const char *rev_args[] = {
e091eb93 36 "--all",
4866ccf0 37 "--bisect",
5a83f3be 38 "--dense",
4866ccf0 39 "--header",
921d865e 40 "--max-age=",
4866ccf0 41 "--max-count=",
4866ccf0 42 "--min-age=",
5ccfb758 43 "--no-merges",
4866ccf0 44 "--objects",
c6496575 45 "--objects-edge",
4866ccf0
JH
46 "--parents",
47 "--pretty",
5a83f3be 48 "--sparse",
4866ccf0 49 "--topo-order",
4c8725f1 50 "--date-order",
4866ccf0 51 "--unpacked",
921d865e
LT
52 NULL
53 };
54 const char **p = rev_args;
55
8233340c
EW
56 /* accept -<digit>, like traditional "head" */
57 if ((*arg == '-') && isdigit(arg[1]))
58 return 1;
59
921d865e
LT
60 for (;;) {
61 const char *str = *p++;
62 int len;
63 if (!str)
64 return 0;
65 len = strlen(str);
4866ccf0
JH
66 if (!strcmp(arg, str) ||
67 (str[len-1] == '=' && !strncmp(arg, str, len)))
921d865e
LT
68 return 1;
69 }
70}
71
4866ccf0 72/* Output argument as a string, either SQ or normal */
5bb2c65a
JH
73static void show(const char *arg)
74{
75 if (output_sq) {
76 int sq = '\'', ch;
77
78 putchar(sq);
79 while ((ch = *arg++)) {
80 if (ch == sq)
81 fputs("'\\'", stdout);
82 putchar(ch);
83 }
84 putchar(sq);
85 putchar(' ');
86 }
87 else
88 puts(arg);
89}
90
4866ccf0 91/* Output a revision, only if filter allows it */
30b96fce 92static void show_rev(int type, const unsigned char *sha1, const char *name)
023d66ed 93{
4866ccf0 94 if (!(filter & DO_REVS))
023d66ed 95 return;
4866ccf0
JH
96 def = NULL;
97 revs_count++;
5bb2c65a 98
30b96fce
JH
99 if (type != show_type)
100 putchar('^');
101 if (symbolic && name)
102 show(name);
d5012508
JH
103 else if (abbrev)
104 show(find_unique_abbrev(sha1, abbrev));
30b96fce
JH
105 else
106 show(sha1_to_hex(sha1));
023d66ed
LT
107}
108
4866ccf0 109/* Output a flag, only if filter allows it. */
9523a4c2 110static int show_flag(char *arg)
023d66ed 111{
4866ccf0 112 if (!(filter & DO_FLAGS))
9523a4c2
LT
113 return 0;
114 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
0360e99d 115 show(arg);
9523a4c2
LT
116 return 1;
117 }
118 return 0;
023d66ed
LT
119}
120
023d66ed
LT
121static void show_default(void)
122{
123 char *s = def;
124
125 if (s) {
126 unsigned char sha1[20];
127
128 def = NULL;
9938af6a 129 if (!get_sha1(s, sha1)) {
30b96fce 130 show_rev(NORMAL, sha1, s);
023d66ed
LT
131 return;
132 }
023d66ed
LT
133 }
134}
135
960bba0d
LT
136static int show_reference(const char *refname, const unsigned char *sha1)
137{
30b96fce 138 show_rev(NORMAL, sha1, refname);
960bba0d
LT
139 return 0;
140}
141
c1babb1d
LT
142static void show_datestring(const char *flag, const char *datestr)
143{
c1babb1d 144 static char buffer[100];
c1babb1d
LT
145
146 /* date handling requires both flags and revs */
147 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
148 return;
3c07b1d1 149 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
c1babb1d
LT
150 show(buffer);
151}
152
9ad0a933 153static int show_file(const char *arg)
7a3dd472 154{
7b34c2fa 155 show_default();
9ad0a933 156 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
7a3dd472 157 show(arg);
9ad0a933
LT
158 return 1;
159 }
160 return 0;
7a3dd472
LT
161}
162
178cb243
LT
163int main(int argc, char **argv)
164{
4866ccf0 165 int i, as_is = 0, verify = 0;
178cb243 166 unsigned char sha1[20];
d288a700
LT
167 const char *prefix = setup_git_directory();
168
84a9b58c
JH
169 git_config(git_default_config);
170
178cb243
LT
171 for (i = 1; i < argc; i++) {
172 char *arg = argv[i];
173 char *dotdot;
fb18a2ed 174
178cb243 175 if (as_is) {
fb18a2ed 176 if (show_file(arg) && as_is < 2)
e23d0b4a 177 verify_filename(prefix, arg);
178cb243
LT
178 continue;
179 }
3af06987
EW
180 if (!strcmp(arg,"-n")) {
181 if (++i >= argc)
182 die("-n requires an argument");
183 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
184 show(arg);
185 show(argv[i]);
186 }
187 continue;
188 }
189 if (!strncmp(arg,"-n",2)) {
190 if ((filter & DO_FLAGS) && (filter & DO_REVS))
191 show(arg);
192 continue;
193 }
194
178cb243
LT
195 if (*arg == '-') {
196 if (!strcmp(arg, "--")) {
fb18a2ed 197 as_is = 2;
a08b6505
LT
198 /* Pass on the "--" if we show anything but files.. */
199 if (filter & (DO_FLAGS | DO_REVS))
200 show_file(arg);
4866ccf0 201 continue;
178cb243
LT
202 }
203 if (!strcmp(arg, "--default")) {
178cb243
LT
204 def = argv[i+1];
205 i++;
206 continue;
207 }
8ebb0184 208 if (!strcmp(arg, "--revs-only")) {
4866ccf0 209 filter &= ~DO_NOREV;
8ebb0184
LT
210 continue;
211 }
212 if (!strcmp(arg, "--no-revs")) {
4866ccf0 213 filter &= ~DO_REVS;
8ebb0184
LT
214 continue;
215 }
f79b65aa 216 if (!strcmp(arg, "--flags")) {
4866ccf0 217 filter &= ~DO_NONFLAGS;
f79b65aa
LT
218 continue;
219 }
220 if (!strcmp(arg, "--no-flags")) {
4866ccf0 221 filter &= ~DO_FLAGS;
f79b65aa
LT
222 continue;
223 }
023d66ed 224 if (!strcmp(arg, "--verify")) {
4866ccf0
JH
225 filter &= ~(DO_FLAGS|DO_NOREV);
226 verify = 1;
023d66ed 227 continue;
921d865e 228 }
62a604ba 229 if (!strcmp(arg, "--short") ||
44de0da4 230 !strncmp(arg, "--short=", 8)) {
d5012508
JH
231 filter &= ~(DO_FLAGS|DO_NOREV);
232 verify = 1;
233 abbrev = DEFAULT_ABBREV;
44de0da4
JF
234 if (arg[7] == '=')
235 abbrev = strtoul(arg + 8, NULL, 10);
1dc4fb84
JH
236 if (abbrev < MINIMUM_ABBREV)
237 abbrev = MINIMUM_ABBREV;
238 else if (40 <= abbrev)
239 abbrev = 40;
d5012508
JH
240 continue;
241 }
5bb2c65a
JH
242 if (!strcmp(arg, "--sq")) {
243 output_sq = 1;
244 continue;
245 }
042a4ed7
LT
246 if (!strcmp(arg, "--not")) {
247 show_type ^= REVERSED;
248 continue;
249 }
30b96fce
JH
250 if (!strcmp(arg, "--symbolic")) {
251 symbolic = 1;
252 continue;
253 }
960bba0d
LT
254 if (!strcmp(arg, "--all")) {
255 for_each_ref(show_reference);
256 continue;
257 }
d288a700 258 if (!strcmp(arg, "--show-prefix")) {
4866ccf0
JH
259 if (prefix)
260 puts(prefix);
d288a700
LT
261 continue;
262 }
5f94c730
JH
263 if (!strcmp(arg, "--show-cdup")) {
264 const char *pfx = prefix;
265 while (pfx) {
266 pfx = strchr(pfx, '/');
267 if (pfx) {
268 pfx++;
269 printf("../");
270 }
271 }
272 putchar('\n');
273 continue;
274 }
a8783eeb
LT
275 if (!strcmp(arg, "--git-dir")) {
276 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
277 static char cwd[PATH_MAX];
278 if (gitdir) {
279 puts(gitdir);
280 continue;
281 }
282 if (!prefix) {
283 puts(".git");
284 continue;
285 }
286 if (!getcwd(cwd, PATH_MAX))
287 die("unable to get current working directory");
288 printf("%s/.git\n", cwd);
289 continue;
290 }
c1babb1d
LT
291 if (!strncmp(arg, "--since=", 8)) {
292 show_datestring("--max-age=", arg+8);
293 continue;
294 }
295 if (!strncmp(arg, "--after=", 8)) {
296 show_datestring("--max-age=", arg+8);
297 continue;
298 }
299 if (!strncmp(arg, "--before=", 9)) {
300 show_datestring("--min-age=", arg+9);
301 continue;
302 }
303 if (!strncmp(arg, "--until=", 8)) {
304 show_datestring("--min-age=", arg+8);
305 continue;
306 }
9523a4c2 307 if (show_flag(arg) && verify)
4866ccf0 308 die("Needed a single revision");
178cb243
LT
309 continue;
310 }
4866ccf0
JH
311
312 /* Not a flag argument */
178cb243
LT
313 dotdot = strstr(arg, "..");
314 if (dotdot) {
315 unsigned char end[20];
ce4a7063
JH
316 char *next = dotdot + 2;
317 char *this = arg;
178cb243 318 *dotdot = 0;
ce4a7063
JH
319 if (!*next)
320 next = "HEAD";
321 if (dotdot == arg)
322 this = "HEAD";
323 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
324 show_rev(NORMAL, end, next);
325 show_rev(REVERSED, sha1, this);
326 continue;
178cb243
LT
327 }
328 *dotdot = '.';
329 }
9938af6a 330 if (!get_sha1(arg, sha1)) {
30b96fce 331 show_rev(NORMAL, sha1, arg);
800644c5
LT
332 continue;
333 }
9938af6a 334 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
30b96fce 335 show_rev(REVERSED, sha1, arg+1);
800644c5
LT
336 continue;
337 }
9ad0a933
LT
338 as_is = 1;
339 if (!show_file(arg))
340 continue;
4866ccf0
JH
341 if (verify)
342 die("Needed a single revision");
e23d0b4a 343 verify_filename(prefix, arg);
023d66ed
LT
344 }
345 show_default();
4866ccf0
JH
346 if (verify && revs_count != 1)
347 die("Needed a single revision");
178cb243
LT
348 return 0;
349}