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