]> git.ipfire.org Git - thirdparty/git.git/blame - rev-parse.c
Merge branch 'jc/clone' into next
[thirdparty/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
178cb243 169 for (i = 1; i < argc; i++) {
d8f6b342 170 struct stat st;
178cb243
LT
171 char *arg = argv[i];
172 char *dotdot;
173
174 if (as_is) {
7a3dd472 175 show_file(arg);
178cb243
LT
176 continue;
177 }
3af06987
EW
178 if (!strcmp(arg,"-n")) {
179 if (++i >= argc)
180 die("-n requires an argument");
181 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
182 show(arg);
183 show(argv[i]);
184 }
185 continue;
186 }
187 if (!strncmp(arg,"-n",2)) {
188 if ((filter & DO_FLAGS) && (filter & DO_REVS))
189 show(arg);
190 continue;
191 }
192
178cb243
LT
193 if (*arg == '-') {
194 if (!strcmp(arg, "--")) {
178cb243 195 as_is = 1;
a08b6505
LT
196 /* Pass on the "--" if we show anything but files.. */
197 if (filter & (DO_FLAGS | DO_REVS))
198 show_file(arg);
4866ccf0 199 continue;
178cb243
LT
200 }
201 if (!strcmp(arg, "--default")) {
178cb243
LT
202 def = argv[i+1];
203 i++;
204 continue;
205 }
8ebb0184 206 if (!strcmp(arg, "--revs-only")) {
4866ccf0 207 filter &= ~DO_NOREV;
8ebb0184
LT
208 continue;
209 }
210 if (!strcmp(arg, "--no-revs")) {
4866ccf0 211 filter &= ~DO_REVS;
8ebb0184
LT
212 continue;
213 }
f79b65aa 214 if (!strcmp(arg, "--flags")) {
4866ccf0 215 filter &= ~DO_NONFLAGS;
f79b65aa
LT
216 continue;
217 }
218 if (!strcmp(arg, "--no-flags")) {
4866ccf0 219 filter &= ~DO_FLAGS;
f79b65aa
LT
220 continue;
221 }
023d66ed 222 if (!strcmp(arg, "--verify")) {
4866ccf0
JH
223 filter &= ~(DO_FLAGS|DO_NOREV);
224 verify = 1;
023d66ed 225 continue;
921d865e 226 }
62a604ba 227 if (!strcmp(arg, "--short") ||
44de0da4 228 !strncmp(arg, "--short=", 8)) {
d5012508
JH
229 filter &= ~(DO_FLAGS|DO_NOREV);
230 verify = 1;
231 abbrev = DEFAULT_ABBREV;
44de0da4
JF
232 if (arg[7] == '=')
233 abbrev = strtoul(arg + 8, NULL, 10);
1dc4fb84
JH
234 if (abbrev < MINIMUM_ABBREV)
235 abbrev = MINIMUM_ABBREV;
236 else if (40 <= abbrev)
237 abbrev = 40;
d5012508
JH
238 continue;
239 }
5bb2c65a
JH
240 if (!strcmp(arg, "--sq")) {
241 output_sq = 1;
242 continue;
243 }
042a4ed7
LT
244 if (!strcmp(arg, "--not")) {
245 show_type ^= REVERSED;
246 continue;
247 }
30b96fce
JH
248 if (!strcmp(arg, "--symbolic")) {
249 symbolic = 1;
250 continue;
251 }
960bba0d
LT
252 if (!strcmp(arg, "--all")) {
253 for_each_ref(show_reference);
254 continue;
255 }
d288a700 256 if (!strcmp(arg, "--show-prefix")) {
4866ccf0
JH
257 if (prefix)
258 puts(prefix);
d288a700
LT
259 continue;
260 }
5f94c730
JH
261 if (!strcmp(arg, "--show-cdup")) {
262 const char *pfx = prefix;
263 while (pfx) {
264 pfx = strchr(pfx, '/');
265 if (pfx) {
266 pfx++;
267 printf("../");
268 }
269 }
270 putchar('\n');
271 continue;
272 }
a8783eeb
LT
273 if (!strcmp(arg, "--git-dir")) {
274 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
275 static char cwd[PATH_MAX];
276 if (gitdir) {
277 puts(gitdir);
278 continue;
279 }
280 if (!prefix) {
281 puts(".git");
282 continue;
283 }
284 if (!getcwd(cwd, PATH_MAX))
285 die("unable to get current working directory");
286 printf("%s/.git\n", cwd);
287 continue;
288 }
c1babb1d
LT
289 if (!strncmp(arg, "--since=", 8)) {
290 show_datestring("--max-age=", arg+8);
291 continue;
292 }
293 if (!strncmp(arg, "--after=", 8)) {
294 show_datestring("--max-age=", arg+8);
295 continue;
296 }
297 if (!strncmp(arg, "--before=", 9)) {
298 show_datestring("--min-age=", arg+9);
299 continue;
300 }
301 if (!strncmp(arg, "--until=", 8)) {
302 show_datestring("--min-age=", arg+8);
303 continue;
304 }
9523a4c2 305 if (show_flag(arg) && verify)
4866ccf0 306 die("Needed a single revision");
178cb243
LT
307 continue;
308 }
4866ccf0
JH
309
310 /* Not a flag argument */
178cb243
LT
311 dotdot = strstr(arg, "..");
312 if (dotdot) {
313 unsigned char end[20];
314 char *n = dotdot+2;
315 *dotdot = 0;
9938af6a 316 if (!get_sha1(arg, sha1)) {
178cb243
LT
317 if (!*n)
318 n = "HEAD";
9938af6a 319 if (!get_sha1(n, end)) {
30b96fce
JH
320 show_rev(NORMAL, end, n);
321 show_rev(REVERSED, sha1, arg);
178cb243
LT
322 continue;
323 }
324 }
325 *dotdot = '.';
326 }
9938af6a 327 if (!get_sha1(arg, sha1)) {
30b96fce 328 show_rev(NORMAL, sha1, arg);
800644c5
LT
329 continue;
330 }
9938af6a 331 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
30b96fce 332 show_rev(REVERSED, sha1, arg+1);
800644c5
LT
333 continue;
334 }
9ad0a933
LT
335 as_is = 1;
336 if (!show_file(arg))
337 continue;
4866ccf0
JH
338 if (verify)
339 die("Needed a single revision");
9ad0a933 340 if (lstat(arg, &st) < 0)
d8f6b342 341 die("'%s': %s", arg, strerror(errno));
023d66ed
LT
342 }
343 show_default();
4866ccf0
JH
344 if (verify && revs_count != 1)
345 die("Needed a single revision");
178cb243
LT
346 return 0;
347}