]>
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", |
4866ccf0 | 39 | "--header", |
921d865e | 40 | "--max-age=", |
4866ccf0 | 41 | "--max-count=", |
921d865e | 42 | "--merge-order", |
4866ccf0 | 43 | "--min-age=", |
5ccfb758 | 44 | "--no-merges", |
4866ccf0 | 45 | "--objects", |
c6496575 | 46 | "--objects-edge", |
4866ccf0 JH |
47 | "--parents", |
48 | "--pretty", | |
49 | "--show-breaks", | |
5a83f3be | 50 | "--sparse", |
4866ccf0 | 51 | "--topo-order", |
4c8725f1 | 52 | "--date-order", |
4866ccf0 | 53 | "--unpacked", |
921d865e LT |
54 | NULL |
55 | }; | |
56 | const char **p = rev_args; | |
57 | ||
8233340c EW |
58 | /* accept -<digit>, like traditional "head" */ |
59 | if ((*arg == '-') && isdigit(arg[1])) | |
60 | return 1; | |
61 | ||
921d865e LT |
62 | for (;;) { |
63 | const char *str = *p++; | |
64 | int len; | |
65 | if (!str) | |
66 | return 0; | |
67 | len = strlen(str); | |
4866ccf0 JH |
68 | if (!strcmp(arg, str) || |
69 | (str[len-1] == '=' && !strncmp(arg, str, len))) | |
921d865e LT |
70 | return 1; |
71 | } | |
72 | } | |
73 | ||
4866ccf0 | 74 | /* Output argument as a string, either SQ or normal */ |
5bb2c65a JH |
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 | ||
4866ccf0 | 93 | /* Output a revision, only if filter allows it */ |
30b96fce | 94 | static void show_rev(int type, const unsigned char *sha1, const char *name) |
023d66ed | 95 | { |
4866ccf0 | 96 | if (!(filter & DO_REVS)) |
023d66ed | 97 | return; |
4866ccf0 JH |
98 | def = NULL; |
99 | revs_count++; | |
5bb2c65a | 100 | |
30b96fce JH |
101 | if (type != show_type) |
102 | putchar('^'); | |
103 | if (symbolic && name) | |
104 | show(name); | |
d5012508 JH |
105 | else if (abbrev) |
106 | show(find_unique_abbrev(sha1, abbrev)); | |
30b96fce JH |
107 | else |
108 | show(sha1_to_hex(sha1)); | |
023d66ed LT |
109 | } |
110 | ||
4866ccf0 | 111 | /* Output a flag, only if filter allows it. */ |
9523a4c2 | 112 | static int show_flag(char *arg) |
023d66ed | 113 | { |
4866ccf0 | 114 | if (!(filter & DO_FLAGS)) |
9523a4c2 LT |
115 | return 0; |
116 | if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { | |
0360e99d | 117 | show(arg); |
9523a4c2 LT |
118 | return 1; |
119 | } | |
120 | return 0; | |
023d66ed LT |
121 | } |
122 | ||
023d66ed LT |
123 | static void show_default(void) |
124 | { | |
125 | char *s = def; | |
126 | ||
127 | if (s) { | |
128 | unsigned char sha1[20]; | |
129 | ||
130 | def = NULL; | |
9938af6a | 131 | if (!get_sha1(s, sha1)) { |
30b96fce | 132 | show_rev(NORMAL, sha1, s); |
023d66ed LT |
133 | return; |
134 | } | |
023d66ed LT |
135 | } |
136 | } | |
137 | ||
960bba0d LT |
138 | static int show_reference(const char *refname, const unsigned char *sha1) |
139 | { | |
30b96fce | 140 | show_rev(NORMAL, sha1, refname); |
960bba0d LT |
141 | return 0; |
142 | } | |
143 | ||
c1babb1d LT |
144 | static void show_datestring(const char *flag, const char *datestr) |
145 | { | |
c1babb1d | 146 | static char buffer[100]; |
c1babb1d LT |
147 | |
148 | /* date handling requires both flags and revs */ | |
149 | if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) | |
150 | return; | |
3c07b1d1 | 151 | snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr)); |
c1babb1d LT |
152 | show(buffer); |
153 | } | |
154 | ||
9ad0a933 | 155 | static int show_file(const char *arg) |
7a3dd472 | 156 | { |
7b34c2fa | 157 | show_default(); |
9ad0a933 | 158 | if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { |
7a3dd472 | 159 | show(arg); |
9ad0a933 LT |
160 | return 1; |
161 | } | |
162 | return 0; | |
7a3dd472 LT |
163 | } |
164 | ||
178cb243 LT |
165 | int main(int argc, char **argv) |
166 | { | |
4866ccf0 | 167 | int i, as_is = 0, verify = 0; |
178cb243 | 168 | unsigned char sha1[20]; |
d288a700 LT |
169 | const char *prefix = setup_git_directory(); |
170 | ||
178cb243 | 171 | for (i = 1; i < argc; i++) { |
d8f6b342 | 172 | struct stat st; |
178cb243 LT |
173 | char *arg = argv[i]; |
174 | char *dotdot; | |
175 | ||
176 | if (as_is) { | |
7a3dd472 | 177 | show_file(arg); |
178cb243 LT |
178 | continue; |
179 | } | |
3af06987 EW |
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 | ||
178cb243 LT |
195 | if (*arg == '-') { |
196 | if (!strcmp(arg, "--")) { | |
178cb243 | 197 | as_is = 1; |
a08b6505 LT |
198 | /* Pass on the "--" if we show anything but files.. */ |
199 | if (filter & (DO_FLAGS | DO_REVS)) | |
200 | show_file(arg); | |
4866ccf0 | 201 | continue; |
178cb243 LT |
202 | } |
203 | if (!strcmp(arg, "--default")) { | |
178cb243 LT |
204 | def = argv[i+1]; |
205 | i++; | |
206 | continue; | |
207 | } | |
8ebb0184 | 208 | if (!strcmp(arg, "--revs-only")) { |
4866ccf0 | 209 | filter &= ~DO_NOREV; |
8ebb0184 LT |
210 | continue; |
211 | } | |
212 | if (!strcmp(arg, "--no-revs")) { | |
4866ccf0 | 213 | filter &= ~DO_REVS; |
8ebb0184 LT |
214 | continue; |
215 | } | |
f79b65aa | 216 | if (!strcmp(arg, "--flags")) { |
4866ccf0 | 217 | filter &= ~DO_NONFLAGS; |
f79b65aa LT |
218 | continue; |
219 | } | |
220 | if (!strcmp(arg, "--no-flags")) { | |
4866ccf0 | 221 | filter &= ~DO_FLAGS; |
f79b65aa LT |
222 | continue; |
223 | } | |
023d66ed | 224 | if (!strcmp(arg, "--verify")) { |
4866ccf0 JH |
225 | filter &= ~(DO_FLAGS|DO_NOREV); |
226 | verify = 1; | |
023d66ed | 227 | continue; |
921d865e | 228 | } |
62a604ba | 229 | if (!strcmp(arg, "--short") || |
44de0da4 | 230 | !strncmp(arg, "--short=", 8)) { |
d5012508 JH |
231 | filter &= ~(DO_FLAGS|DO_NOREV); |
232 | verify = 1; | |
233 | abbrev = DEFAULT_ABBREV; | |
44de0da4 JF |
234 | if (arg[7] == '=') |
235 | abbrev = strtoul(arg + 8, NULL, 10); | |
1dc4fb84 JH |
236 | if (abbrev < MINIMUM_ABBREV) |
237 | abbrev = MINIMUM_ABBREV; | |
238 | else if (40 <= abbrev) | |
239 | abbrev = 40; | |
d5012508 JH |
240 | continue; |
241 | } | |
5bb2c65a JH |
242 | if (!strcmp(arg, "--sq")) { |
243 | output_sq = 1; | |
244 | continue; | |
245 | } | |
042a4ed7 LT |
246 | if (!strcmp(arg, "--not")) { |
247 | show_type ^= REVERSED; | |
248 | continue; | |
249 | } | |
30b96fce JH |
250 | if (!strcmp(arg, "--symbolic")) { |
251 | symbolic = 1; | |
252 | continue; | |
253 | } | |
960bba0d LT |
254 | if (!strcmp(arg, "--all")) { |
255 | for_each_ref(show_reference); | |
256 | continue; | |
257 | } | |
d288a700 | 258 | if (!strcmp(arg, "--show-prefix")) { |
4866ccf0 JH |
259 | if (prefix) |
260 | puts(prefix); | |
d288a700 LT |
261 | continue; |
262 | } | |
5f94c730 JH |
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 | } | |
a8783eeb LT |
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 | } | |
c1babb1d LT |
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 | } | |
9523a4c2 | 307 | if (show_flag(arg) && verify) |
4866ccf0 | 308 | die("Needed a single revision"); |
178cb243 LT |
309 | continue; |
310 | } | |
4866ccf0 JH |
311 | |
312 | /* Not a flag argument */ | |
178cb243 LT |
313 | dotdot = strstr(arg, ".."); |
314 | if (dotdot) { | |
315 | unsigned char end[20]; | |
316 | char *n = dotdot+2; | |
317 | *dotdot = 0; | |
9938af6a | 318 | if (!get_sha1(arg, sha1)) { |
178cb243 LT |
319 | if (!*n) |
320 | n = "HEAD"; | |
9938af6a | 321 | if (!get_sha1(n, end)) { |
30b96fce JH |
322 | show_rev(NORMAL, end, n); |
323 | show_rev(REVERSED, sha1, arg); | |
178cb243 LT |
324 | continue; |
325 | } | |
326 | } | |
327 | *dotdot = '.'; | |
328 | } | |
9938af6a | 329 | if (!get_sha1(arg, sha1)) { |
30b96fce | 330 | show_rev(NORMAL, sha1, arg); |
800644c5 LT |
331 | continue; |
332 | } | |
9938af6a | 333 | if (*arg == '^' && !get_sha1(arg+1, sha1)) { |
30b96fce | 334 | show_rev(REVERSED, sha1, arg+1); |
800644c5 LT |
335 | continue; |
336 | } | |
9ad0a933 LT |
337 | as_is = 1; |
338 | if (!show_file(arg)) | |
339 | continue; | |
4866ccf0 JH |
340 | if (verify) |
341 | die("Needed a single revision"); | |
9ad0a933 | 342 | if (lstat(arg, &st) < 0) |
d8f6b342 | 343 | die("'%s': %s", arg, strerror(errno)); |
023d66ed LT |
344 | } |
345 | show_default(); | |
4866ccf0 JH |
346 | if (verify && revs_count != 1) |
347 | die("Needed a single revision"); | |
178cb243 LT |
348 | return 0; |
349 | } |