Merge git-mv fixes from 'maint'
[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 #include "quote.h"
10
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;
16
17 static char *def = NULL;
18
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;
25
26 static int revs_count = 0;
27
28 /*
29  * Some arguments are relevant "revision" arguments,
30  * others are about output format or other details.
31  * This sorts it all out.
32  */
33 static int is_rev_argument(const char *arg)
34 {
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                 "--objects-edge",
47                 "--parents",
48                 "--pretty",
49                 "--show-breaks",
50                 "--sparse",
51                 "--topo-order",
52                 "--date-order",
53                 "--unpacked",
54                 NULL
55         };
56         const char **p = rev_args;
57
58         /* accept -<digit>, like traditional "head" */
59         if ((*arg == '-') && isdigit(arg[1]))
60                 return 1;
61
62         for (;;) {
63                 const char *str = *p++;
64                 int len;
65                 if (!str)
66                         return 0;
67                 len = strlen(str);
68                 if (!strcmp(arg, str) ||
69                     (str[len-1] == '=' && !strncmp(arg, str, len)))
70                         return 1;
71         }
72 }
73
74 /* Output argument as a string, either SQ or normal */
75 static void show(const char *arg)
76 {
77         if (output_sq) {
78                 int sq = '\'', ch;
79
80                 putchar(sq);
81                 while ((ch = *arg++)) {
82                         if (ch == sq)
83                                 fputs("'\\'", stdout);
84                         putchar(ch);
85                 }
86                 putchar(sq);
87                 putchar(' ');
88         }
89         else
90                 puts(arg);
91 }
92
93 /* Output a revision, only if filter allows it */
94 static void show_rev(int type, const unsigned char *sha1, const char *name)
95 {
96         if (!(filter & DO_REVS))
97                 return;
98         def = NULL;
99         revs_count++;
100
101         if (type != show_type)
102                 putchar('^');
103         if (symbolic && name)
104                 show(name);
105         else if (abbrev)
106                 show(find_unique_abbrev(sha1, abbrev));
107         else
108                 show(sha1_to_hex(sha1));
109 }
110
111 /* Output a flag, only if filter allows it. */
112 static int show_flag(char *arg)
113 {
114         if (!(filter & DO_FLAGS))
115                 return 0;
116         if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
117                 show(arg);
118                 return 1;
119         }
120         return 0;
121 }
122
123 static void show_default(void)
124 {
125         char *s = def;
126
127         if (s) {
128                 unsigned char sha1[20];
129
130                 def = NULL;
131                 if (!get_sha1(s, sha1)) {
132                         show_rev(NORMAL, sha1, s);
133                         return;
134                 }
135         }
136 }
137
138 static int show_reference(const char *refname, const unsigned char *sha1)
139 {
140         show_rev(NORMAL, sha1, refname);
141         return 0;
142 }
143
144 static void show_datestring(const char *flag, const char *datestr)
145 {
146         static char buffer[100];
147
148         /* date handling requires both flags and revs */
149         if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
150                 return;
151         snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
152         show(buffer);
153 }
154
155 static int show_file(const char *arg)
156 {
157         show_default();
158         if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
159                 show(arg);
160                 return 1;
161         }
162         return 0;
163 }
164
165 int main(int argc, char **argv)
166 {
167         int i, as_is = 0, verify = 0;
168         unsigned char sha1[20];
169         const char *prefix = setup_git_directory();
170         
171         for (i = 1; i < argc; i++) {
172                 struct stat st;
173                 char *arg = argv[i];
174                 char *dotdot;
175         
176                 if (as_is) {
177                         show_file(arg);
178                         continue;
179                 }
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
195                 if (*arg == '-') {
196                         if (!strcmp(arg, "--")) {
197                                 as_is = 1;
198                                 /* Pass on the "--" if we show anything but files.. */
199                                 if (filter & (DO_FLAGS | DO_REVS))
200                                         show_file(arg);
201                                 continue;
202                         }
203                         if (!strcmp(arg, "--default")) {
204                                 def = argv[i+1];
205                                 i++;
206                                 continue;
207                         }
208                         if (!strcmp(arg, "--revs-only")) {
209                                 filter &= ~DO_NOREV;
210                                 continue;
211                         }
212                         if (!strcmp(arg, "--no-revs")) {
213                                 filter &= ~DO_REVS;
214                                 continue;
215                         }
216                         if (!strcmp(arg, "--flags")) {
217                                 filter &= ~DO_NONFLAGS;
218                                 continue;
219                         }
220                         if (!strcmp(arg, "--no-flags")) {
221                                 filter &= ~DO_FLAGS;
222                                 continue;
223                         }
224                         if (!strcmp(arg, "--verify")) {
225                                 filter &= ~(DO_FLAGS|DO_NOREV);
226                                 verify = 1;
227                                 continue;
228                         }
229                         if (!strcmp(arg, "--short") ||
230                             !strncmp(arg, "--short=", 8)) {
231                                 filter &= ~(DO_FLAGS|DO_NOREV);
232                                 verify = 1;
233                                 abbrev = DEFAULT_ABBREV;
234                                 if (arg[7] == '=')
235                                         abbrev = strtoul(arg + 8, NULL, 10);
236                                 if (abbrev < MINIMUM_ABBREV)
237                                         abbrev = MINIMUM_ABBREV;
238                                 else if (40 <= abbrev)
239                                         abbrev = 40;
240                                 continue;
241                         }
242                         if (!strcmp(arg, "--sq")) {
243                                 output_sq = 1;
244                                 continue;
245                         }
246                         if (!strcmp(arg, "--not")) {
247                                 show_type ^= REVERSED;
248                                 continue;
249                         }
250                         if (!strcmp(arg, "--symbolic")) {
251                                 symbolic = 1;
252                                 continue;
253                         }
254                         if (!strcmp(arg, "--all")) {
255                                 for_each_ref(show_reference);
256                                 continue;
257                         }
258                         if (!strcmp(arg, "--show-prefix")) {
259                                 if (prefix)
260                                         puts(prefix);
261                                 continue;
262                         }
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                         }
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                         }
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                         }
307                         if (show_flag(arg) && verify)
308                                 die("Needed a single revision");
309                         continue;
310                 }
311
312                 /* Not a flag argument */
313                 dotdot = strstr(arg, "..");
314                 if (dotdot) {
315                         unsigned char end[20];
316                         char *n = dotdot+2;
317                         *dotdot = 0;
318                         if (!get_sha1(arg, sha1)) {
319                                 if (!*n)
320                                         n = "HEAD";
321                                 if (!get_sha1(n, end)) {
322                                         show_rev(NORMAL, end, n);
323                                         show_rev(REVERSED, sha1, arg);
324                                         continue;
325                                 }
326                         }
327                         *dotdot = '.';
328                 }
329                 if (!get_sha1(arg, sha1)) {
330                         show_rev(NORMAL, sha1, arg);
331                         continue;
332                 }
333                 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
334                         show_rev(REVERSED, sha1, arg+1);
335                         continue;
336                 }
337                 as_is = 1;
338                 if (!show_file(arg))
339                         continue;
340                 if (verify)
341                         die("Needed a single revision");
342                 if (lstat(arg, &st) < 0)
343                         die("'%s': %s", arg, strerror(errno));
344         }
345         show_default();
346         if (verify && revs_count != 1)
347                 die("Needed a single revision");
348         return 0;
349 }