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