🌐 AI搜索 & 代理 主页
blob: 62e16af33c29ddd6a9c8b2d9755dc11804d86bc4 [file] [log] [blame]
Linus Torvalds178cb242005-06-13 17:06:501/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
Linus Torvaldsa8be83f2005-06-21 03:28:097#include "commit.h"
Linus Torvalds960bba02005-07-03 20:07:528#include "refs.h"
Linus Torvaldsc1babb12005-09-20 21:13:249#include "quote.h"
Linus Torvaldsa8be83f2005-06-21 03:28:0910
Junio C Hamano4866ccf2005-08-24 21:30:0411#define DO_REVS 1
12#define DO_NOREV 2
13#define DO_FLAGS 4
14#define DO_NONFLAGS 8
15static int filter = ~0;
16
Linus Torvalds023d66e2005-06-24 17:12:5517static char *def = NULL;
Linus Torvalds023d66e2005-06-24 17:12:5518
Linus Torvalds042a4ed2005-06-26 18:34:3019#define NORMAL 0
20#define REVERSED 1
21static int show_type = NORMAL;
Junio C Hamano4866ccf2005-08-24 21:30:0422static int symbolic = 0;
Junio C Hamanod5012502006-01-25 09:35:3823static int abbrev = 0;
Junio C Hamano4866ccf2005-08-24 21:30:0424static int output_sq = 0;
25
26static int revs_count = 0;
Linus Torvalds042a4ed2005-06-26 18:34:3027
Linus Torvalds921d8652005-06-13 18:14:2028/*
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[] = {
Junio C Hamanoe091eb92005-10-05 21:49:5436 "--all",
Junio C Hamano5ccfb752005-08-09 02:31:3737 "--bisect",
Junio C Hamano5a83f3b2005-10-30 09:08:3538 "--dense",
Junio C Hamano4866ccf2005-08-24 21:30:0439 "--header",
40 "--max-age=",
41 "--max-count=",
Junio C Hamano4866ccf2005-08-24 21:30:0442 "--min-age=",
Junio C Hamano5ccfb752005-08-09 02:31:3743 "--no-merges",
Junio C Hamano4866ccf2005-08-24 21:30:0444 "--objects",
Junio C Hamanoc6496572006-02-19 11:32:3145 "--objects-edge",
Junio C Hamano4866ccf2005-08-24 21:30:0446 "--parents",
47 "--pretty",
Junio C Hamano5a83f3b2005-10-30 09:08:3548 "--sparse",
Junio C Hamano4866ccf2005-08-24 21:30:0449 "--topo-order",
Junio C Hamano4c8725f2006-02-16 06:05:3350 "--date-order",
Junio C Hamano4866ccf2005-08-24 21:30:0451 "--unpacked",
Linus Torvalds921d8652005-06-13 18:14:2052 NULL
53 };
54 const char **p = rev_args;
55
Eric Wong82333402006-01-30 00:28:0256 /* accept -<digit>, like traditional "head" */
57 if ((*arg == '-') && isdigit(arg[1]))
58 return 1;
59
Linus Torvalds921d8652005-06-13 18:14:2060 for (;;) {
61 const char *str = *p++;
62 int len;
63 if (!str)
64 return 0;
65 len = strlen(str);
Junio C Hamano4866ccf2005-08-24 21:30:0466 if (!strcmp(arg, str) ||
67 (str[len-1] == '=' && !strncmp(arg, str, len)))
Linus Torvalds921d8652005-06-13 18:14:2068 return 1;
69 }
70}
71
Junio C Hamano4866ccf2005-08-24 21:30:0472/* Output argument as a string, either SQ or normal */
Junio C Hamano5bb2c652005-07-23 02:08:3273static 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
Junio C Hamano4866ccf2005-08-24 21:30:0491/* Output a revision, only if filter allows it */
Junio C Hamano30b96fc2005-08-16 19:36:4692static void show_rev(int type, const unsigned char *sha1, const char *name)
Linus Torvalds023d66e2005-06-24 17:12:5593{
Junio C Hamano4866ccf2005-08-24 21:30:0494 if (!(filter & DO_REVS))
Linus Torvalds023d66e2005-06-24 17:12:5595 return;
Junio C Hamano4866ccf2005-08-24 21:30:0496 def = NULL;
97 revs_count++;
Junio C Hamano5bb2c652005-07-23 02:08:3298
Junio C Hamano30b96fc2005-08-16 19:36:4699 if (type != show_type)
100 putchar('^');
101 if (symbolic && name)
102 show(name);
Junio C Hamanod5012502006-01-25 09:35:38103 else if (abbrev)
104 show(find_unique_abbrev(sha1, abbrev));
Junio C Hamano30b96fc2005-08-16 19:36:46105 else
106 show(sha1_to_hex(sha1));
Linus Torvalds023d66e2005-06-24 17:12:55107}
108
Junio C Hamano4866ccf2005-08-24 21:30:04109/* Output a flag, only if filter allows it. */
Linus Torvalds9523a4c2006-02-05 19:58:34110static int show_flag(char *arg)
Linus Torvalds023d66e2005-06-24 17:12:55111{
Junio C Hamano4866ccf2005-08-24 21:30:04112 if (!(filter & DO_FLAGS))
Linus Torvalds9523a4c2006-02-05 19:58:34113 return 0;
114 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
Linus Torvalds0360e992005-08-23 17:47:54115 show(arg);
Linus Torvalds9523a4c2006-02-05 19:58:34116 return 1;
117 }
118 return 0;
Linus Torvalds023d66e2005-06-24 17:12:55119}
120
Linus Torvalds023d66e2005-06-24 17:12:55121static void show_default(void)
122{
123 char *s = def;
124
125 if (s) {
126 unsigned char sha1[20];
127
128 def = NULL;
Junio C Hamano9938af62005-08-04 05:15:49129 if (!get_sha1(s, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46130 show_rev(NORMAL, sha1, s);
Linus Torvalds023d66e2005-06-24 17:12:55131 return;
132 }
Linus Torvalds023d66e2005-06-24 17:12:55133 }
134}
135
Linus Torvalds960bba02005-07-03 20:07:52136static int show_reference(const char *refname, const unsigned char *sha1)
137{
Junio C Hamano30b96fc2005-08-16 19:36:46138 show_rev(NORMAL, sha1, refname);
Linus Torvalds960bba02005-07-03 20:07:52139 return 0;
140}
141
Linus Torvaldsc1babb12005-09-20 21:13:24142static void show_datestring(const char *flag, const char *datestr)
143{
Linus Torvaldsc1babb12005-09-20 21:13:24144 static char buffer[100];
Linus Torvaldsc1babb12005-09-20 21:13:24145
146 /* date handling requires both flags and revs */
147 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
148 return;
Linus Torvalds3c07b1d2005-11-15 03:29:06149 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
Linus Torvaldsc1babb12005-09-20 21:13:24150 show(buffer);
151}
152
Linus Torvalds9ad0a932006-02-06 05:41:47153static int show_file(const char *arg)
Linus Torvalds7a3dd472005-10-18 07:16:45154{
Linus Torvalds7b34c2f2005-10-25 22:24:55155 show_default();
Linus Torvalds9ad0a932006-02-06 05:41:47156 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
Linus Torvalds7a3dd472005-10-18 07:16:45157 show(arg);
Linus Torvalds9ad0a932006-02-06 05:41:47158 return 1;
159 }
160 return 0;
Linus Torvalds7a3dd472005-10-18 07:16:45161}
162
Linus Torvalds178cb242005-06-13 17:06:50163int main(int argc, char **argv)
164{
Junio C Hamano4866ccf2005-08-24 21:30:04165 int i, as_is = 0, verify = 0;
Linus Torvalds178cb242005-06-13 17:06:50166 unsigned char sha1[20];
Linus Torvaldsd288a702005-08-17 01:06:34167 const char *prefix = setup_git_directory();
168
Junio C Hamano84a9b582006-03-24 07:41:18169 git_config(git_default_config);
170
Linus Torvalds178cb242005-06-13 17:06:50171 for (i = 1; i < argc; i++) {
Linus Torvalds178cb242005-06-13 17:06:50172 char *arg = argv[i];
173 char *dotdot;
Linus Torvaldsfb18a2e2006-03-27 00:28:20174
Linus Torvalds178cb242005-06-13 17:06:50175 if (as_is) {
Linus Torvaldsfb18a2e2006-03-27 00:28:20176 if (show_file(arg) && as_is < 2)
Linus Torvaldse23d0b42006-04-26 17:15:54177 verify_filename(prefix, arg);
Linus Torvalds178cb242005-06-13 17:06:50178 continue;
179 }
Eric Wong3af06982006-01-30 00:26:40180 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
Linus Torvalds178cb242005-06-13 17:06:50195 if (*arg == '-') {
196 if (!strcmp(arg, "--")) {
Linus Torvaldsfb18a2e2006-03-27 00:28:20197 as_is = 2;
Linus Torvaldsa08b6502005-10-21 00:16:30198 /* Pass on the "--" if we show anything but files.. */
199 if (filter & (DO_FLAGS | DO_REVS))
200 show_file(arg);
Junio C Hamano4866ccf2005-08-24 21:30:04201 continue;
Linus Torvalds178cb242005-06-13 17:06:50202 }
203 if (!strcmp(arg, "--default")) {
Linus Torvalds178cb242005-06-13 17:06:50204 def = argv[i+1];
205 i++;
206 continue;
207 }
Linus Torvalds8ebb0182005-06-13 17:21:11208 if (!strcmp(arg, "--revs-only")) {
Junio C Hamano4866ccf2005-08-24 21:30:04209 filter &= ~DO_NOREV;
Linus Torvalds8ebb0182005-06-13 17:21:11210 continue;
211 }
212 if (!strcmp(arg, "--no-revs")) {
Junio C Hamano4866ccf2005-08-24 21:30:04213 filter &= ~DO_REVS;
Linus Torvalds8ebb0182005-06-13 17:21:11214 continue;
215 }
Linus Torvaldsf79b65a2005-07-06 17:08:08216 if (!strcmp(arg, "--flags")) {
Junio C Hamano4866ccf2005-08-24 21:30:04217 filter &= ~DO_NONFLAGS;
Linus Torvaldsf79b65a2005-07-06 17:08:08218 continue;
219 }
220 if (!strcmp(arg, "--no-flags")) {
Junio C Hamano4866ccf2005-08-24 21:30:04221 filter &= ~DO_FLAGS;
Linus Torvaldsf79b65a2005-07-06 17:08:08222 continue;
223 }
Linus Torvalds023d66e2005-06-24 17:12:55224 if (!strcmp(arg, "--verify")) {
Junio C Hamano4866ccf2005-08-24 21:30:04225 filter &= ~(DO_FLAGS|DO_NOREV);
226 verify = 1;
Linus Torvalds023d66e2005-06-24 17:12:55227 continue;
Linus Torvalds921d8652005-06-13 18:14:20228 }
Junio C Hamano62a604b2006-01-27 01:02:07229 if (!strcmp(arg, "--short") ||
Jonas Fonseca44de0da2006-02-18 01:10:53230 !strncmp(arg, "--short=", 8)) {
Junio C Hamanod5012502006-01-25 09:35:38231 filter &= ~(DO_FLAGS|DO_NOREV);
232 verify = 1;
233 abbrev = DEFAULT_ABBREV;
Jonas Fonseca44de0da2006-02-18 01:10:53234 if (arg[7] == '=')
235 abbrev = strtoul(arg + 8, NULL, 10);
Junio C Hamano1dc4fb82006-01-26 08:48:19236 if (abbrev < MINIMUM_ABBREV)
237 abbrev = MINIMUM_ABBREV;
238 else if (40 <= abbrev)
239 abbrev = 40;
Junio C Hamanod5012502006-01-25 09:35:38240 continue;
241 }
Junio C Hamano5bb2c652005-07-23 02:08:32242 if (!strcmp(arg, "--sq")) {
243 output_sq = 1;
244 continue;
245 }
Linus Torvalds042a4ed2005-06-26 18:34:30246 if (!strcmp(arg, "--not")) {
247 show_type ^= REVERSED;
248 continue;
249 }
Junio C Hamano30b96fc2005-08-16 19:36:46250 if (!strcmp(arg, "--symbolic")) {
251 symbolic = 1;
252 continue;
253 }
Linus Torvalds960bba02005-07-03 20:07:52254 if (!strcmp(arg, "--all")) {
255 for_each_ref(show_reference);
256 continue;
257 }
Linus Torvaldsd288a702005-08-17 01:06:34258 if (!strcmp(arg, "--show-prefix")) {
Junio C Hamano4866ccf2005-08-24 21:30:04259 if (prefix)
260 puts(prefix);
Linus Torvaldsd288a702005-08-17 01:06:34261 continue;
262 }
Junio C Hamano5f94c732005-12-23 06:35:38263 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 }
Linus Torvaldsa8783ee2005-09-18 18:18:30275 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 }
Linus Torvaldsc1babb12005-09-20 21:13:24291 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 }
Linus Torvalds9523a4c2006-02-05 19:58:34307 if (show_flag(arg) && verify)
Junio C Hamano4866ccf2005-08-24 21:30:04308 die("Needed a single revision");
Linus Torvalds178cb242005-06-13 17:06:50309 continue;
310 }
Junio C Hamano4866ccf2005-08-24 21:30:04311
312 /* Not a flag argument */
Linus Torvalds178cb242005-06-13 17:06:50313 dotdot = strstr(arg, "..");
314 if (dotdot) {
315 unsigned char end[20];
Junio C Hamanoce4a7062006-03-30 03:41:37316 char *next = dotdot + 2;
317 char *this = arg;
Linus Torvalds178cb242005-06-13 17:06:50318 *dotdot = 0;
Junio C Hamanoce4a7062006-03-30 03:41:37319 if (!*next)
320 next = "HEAD";
321 if (dotdot == arg)
322 this = "HEAD";
323 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
324 show_rev(NORMAL, end, next);
325 show_rev(REVERSED, sha1, this);
326 continue;
Linus Torvalds178cb242005-06-13 17:06:50327 }
328 *dotdot = '.';
329 }
Junio C Hamano9938af62005-08-04 05:15:49330 if (!get_sha1(arg, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46331 show_rev(NORMAL, sha1, arg);
Linus Torvalds800644c2005-06-20 15:29:13332 continue;
333 }
Junio C Hamano9938af62005-08-04 05:15:49334 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46335 show_rev(REVERSED, sha1, arg+1);
Linus Torvalds800644c2005-06-20 15:29:13336 continue;
337 }
Linus Torvalds9ad0a932006-02-06 05:41:47338 as_is = 1;
339 if (!show_file(arg))
340 continue;
Junio C Hamano4866ccf2005-08-24 21:30:04341 if (verify)
342 die("Needed a single revision");
Linus Torvaldse23d0b42006-04-26 17:15:54343 verify_filename(prefix, arg);
Linus Torvalds178cb242005-06-13 17:06:50344 }
Linus Torvalds023d66e2005-06-24 17:12:55345 show_default();
Junio C Hamano4866ccf2005-08-24 21:30:04346 if (verify && revs_count != 1)
347 die("Needed a single revision");
Linus Torvalds178cb242005-06-13 17:06:50348 return 0;
349}