Do not fall back on vi on dumb terminals.
[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=",
921d865e 42 "--merge-order",
4866ccf0 43 "--min-age=",
5ccfb758 44 "--no-merges",
4866ccf0
JH
45 "--objects",
46 "--parents",
47 "--pretty",
48 "--show-breaks",
5a83f3be 49 "--sparse",
4866ccf0
JH
50 "--topo-order",
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
JH
109/* Output a flag, only if filter allows it. */
110static void show_flag(char *arg)
023d66ed 111{
4866ccf0 112 if (!(filter & DO_FLAGS))
023d66ed 113 return;
4866ccf0 114 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
0360e99d 115 show(arg);
023d66ed
LT
116}
117
023d66ed
LT
118static void show_default(void)
119{
120 char *s = def;
121
122 if (s) {
123 unsigned char sha1[20];
124
125 def = NULL;
9938af6a 126 if (!get_sha1(s, sha1)) {
30b96fce 127 show_rev(NORMAL, sha1, s);
023d66ed
LT
128 return;
129 }
023d66ed
LT
130 }
131}
132
960bba0d
LT
133static int show_reference(const char *refname, const unsigned char *sha1)
134{
30b96fce 135 show_rev(NORMAL, sha1, refname);
960bba0d
LT
136 return 0;
137}
138
c1babb1d
LT
139static void show_datestring(const char *flag, const char *datestr)
140{
c1babb1d 141 static char buffer[100];
c1babb1d
LT
142
143 /* date handling requires both flags and revs */
144 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
145 return;
3c07b1d1 146 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
c1babb1d
LT
147 show(buffer);
148}
149
7a3dd472
LT
150static void show_file(const char *arg)
151{
7b34c2fa 152 show_default();
7a3dd472
LT
153 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
154 show(arg);
155}
156
178cb243
LT
157int main(int argc, char **argv)
158{
4866ccf0 159 int i, as_is = 0, verify = 0;
178cb243 160 unsigned char sha1[20];
d288a700
LT
161 const char *prefix = setup_git_directory();
162
178cb243 163 for (i = 1; i < argc; i++) {
d8f6b342 164 struct stat st;
178cb243
LT
165 char *arg = argv[i];
166 char *dotdot;
167
168 if (as_is) {
7a3dd472 169 show_file(arg);
178cb243
LT
170 continue;
171 }
3af06987
EW
172 if (!strcmp(arg,"-n")) {
173 if (++i >= argc)
174 die("-n requires an argument");
175 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
176 show(arg);
177 show(argv[i]);
178 }
179 continue;
180 }
181 if (!strncmp(arg,"-n",2)) {
182 if ((filter & DO_FLAGS) && (filter & DO_REVS))
183 show(arg);
184 continue;
185 }
186
178cb243
LT
187 if (*arg == '-') {
188 if (!strcmp(arg, "--")) {
178cb243 189 as_is = 1;
a08b6505
LT
190 /* Pass on the "--" if we show anything but files.. */
191 if (filter & (DO_FLAGS | DO_REVS))
192 show_file(arg);
4866ccf0 193 continue;
178cb243
LT
194 }
195 if (!strcmp(arg, "--default")) {
178cb243
LT
196 def = argv[i+1];
197 i++;
198 continue;
199 }
8ebb0184 200 if (!strcmp(arg, "--revs-only")) {
4866ccf0 201 filter &= ~DO_NOREV;
8ebb0184
LT
202 continue;
203 }
204 if (!strcmp(arg, "--no-revs")) {
4866ccf0 205 filter &= ~DO_REVS;
8ebb0184
LT
206 continue;
207 }
f79b65aa 208 if (!strcmp(arg, "--flags")) {
4866ccf0 209 filter &= ~DO_NONFLAGS;
f79b65aa
LT
210 continue;
211 }
212 if (!strcmp(arg, "--no-flags")) {
4866ccf0 213 filter &= ~DO_FLAGS;
f79b65aa
LT
214 continue;
215 }
023d66ed 216 if (!strcmp(arg, "--verify")) {
4866ccf0
JH
217 filter &= ~(DO_FLAGS|DO_NOREV);
218 verify = 1;
023d66ed 219 continue;
921d865e 220 }
62a604ba
JH
221 if (!strcmp(arg, "--short") ||
222 !strncmp(arg, "--short=", 9)) {
d5012508
JH
223 filter &= ~(DO_FLAGS|DO_NOREV);
224 verify = 1;
225 abbrev = DEFAULT_ABBREV;
226 if (arg[8] == '=')
227 abbrev = strtoul(arg + 9, NULL, 10);
1dc4fb84
JH
228 if (abbrev < MINIMUM_ABBREV)
229 abbrev = MINIMUM_ABBREV;
230 else if (40 <= abbrev)
231 abbrev = 40;
d5012508
JH
232 continue;
233 }
5bb2c65a
JH
234 if (!strcmp(arg, "--sq")) {
235 output_sq = 1;
236 continue;
237 }
042a4ed7
LT
238 if (!strcmp(arg, "--not")) {
239 show_type ^= REVERSED;
240 continue;
241 }
30b96fce
JH
242 if (!strcmp(arg, "--symbolic")) {
243 symbolic = 1;
244 continue;
245 }
960bba0d
LT
246 if (!strcmp(arg, "--all")) {
247 for_each_ref(show_reference);
248 continue;
249 }
d288a700 250 if (!strcmp(arg, "--show-prefix")) {
4866ccf0
JH
251 if (prefix)
252 puts(prefix);
d288a700
LT
253 continue;
254 }
5f94c730
JH
255 if (!strcmp(arg, "--show-cdup")) {
256 const char *pfx = prefix;
257 while (pfx) {
258 pfx = strchr(pfx, '/');
259 if (pfx) {
260 pfx++;
261 printf("../");
262 }
263 }
264 putchar('\n');
265 continue;
266 }
a8783eeb
LT
267 if (!strcmp(arg, "--git-dir")) {
268 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
269 static char cwd[PATH_MAX];
270 if (gitdir) {
271 puts(gitdir);
272 continue;
273 }
274 if (!prefix) {
275 puts(".git");
276 continue;
277 }
278 if (!getcwd(cwd, PATH_MAX))
279 die("unable to get current working directory");
280 printf("%s/.git\n", cwd);
281 continue;
282 }
c1babb1d
LT
283 if (!strncmp(arg, "--since=", 8)) {
284 show_datestring("--max-age=", arg+8);
285 continue;
286 }
287 if (!strncmp(arg, "--after=", 8)) {
288 show_datestring("--max-age=", arg+8);
289 continue;
290 }
291 if (!strncmp(arg, "--before=", 9)) {
292 show_datestring("--min-age=", arg+9);
293 continue;
294 }
295 if (!strncmp(arg, "--until=", 8)) {
296 show_datestring("--min-age=", arg+8);
297 continue;
298 }
4866ccf0
JH
299 if (verify)
300 die("Needed a single revision");
301 show_flag(arg);
178cb243
LT
302 continue;
303 }
4866ccf0
JH
304
305 /* Not a flag argument */
178cb243
LT
306 dotdot = strstr(arg, "..");
307 if (dotdot) {
308 unsigned char end[20];
309 char *n = dotdot+2;
310 *dotdot = 0;
9938af6a 311 if (!get_sha1(arg, sha1)) {
178cb243
LT
312 if (!*n)
313 n = "HEAD";
9938af6a 314 if (!get_sha1(n, end)) {
30b96fce
JH
315 show_rev(NORMAL, end, n);
316 show_rev(REVERSED, sha1, arg);
178cb243
LT
317 continue;
318 }
319 }
320 *dotdot = '.';
321 }
9938af6a 322 if (!get_sha1(arg, sha1)) {
30b96fce 323 show_rev(NORMAL, sha1, arg);
800644c5
LT
324 continue;
325 }
9938af6a 326 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
30b96fce 327 show_rev(REVERSED, sha1, arg+1);
800644c5
LT
328 continue;
329 }
4866ccf0
JH
330 if (verify)
331 die("Needed a single revision");
b33aba51
JH
332 if ((filter & DO_REVS) &&
333 (filter & DO_NONFLAGS) && /* !def && */
334 lstat(arg, &st) < 0)
d8f6b342 335 die("'%s': %s", arg, strerror(errno));
af13cdf2 336 as_is = 1;
7a3dd472 337 show_file(arg);
023d66ed
LT
338 }
339 show_default();
4866ccf0
JH
340 if (verify && revs_count != 1)
341 die("Needed a single revision");
178cb243
LT
342 return 0;
343}