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