rev-parse.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /*
  2. * rev-parse.c
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  7. #include "cache.h"
  8. #include "config.h"
  9. #include "commit.h"
  10. #include "refs.h"
  11. #include "quote.h"
  12. #include "builtin.h"
  13. #include "parse-options.h"
  14. #include "diff.h"
  15. #include "revision.h"
  16. #include "split-index.h"
  17. #include "submodule.h"
  18. #include "commit-reach.h"
  19. #include "shallow.h"
  20. #define DO_REVS 1
  21. #define DO_NOREV 2
  22. #define DO_FLAGS 4
  23. #define DO_NONFLAGS 8
  24. static int filter = ~0;
  25. static const char *def;
  26. #define NORMAL 0
  27. #define REVERSED 1
  28. static int show_type = NORMAL;
  29. #define SHOW_SYMBOLIC_ASIS 1
  30. #define SHOW_SYMBOLIC_FULL 2
  31. static int symbolic;
  32. static int abbrev;
  33. static int abbrev_ref;
  34. static int abbrev_ref_strict;
  35. static int output_sq;
  36. static int stuck_long;
  37. static struct string_list *ref_excludes;
  38. /*
  39. * Some arguments are relevant "revision" arguments,
  40. * others are about output format or other details.
  41. * This sorts it all out.
  42. */
  43. static int is_rev_argument(const char *arg)
  44. {
  45. static const char *rev_args[] = {
  46. "--all",
  47. "--bisect",
  48. "--dense",
  49. "--branches=",
  50. "--branches",
  51. "--header",
  52. "--ignore-missing",
  53. "--max-age=",
  54. "--max-count=",
  55. "--min-age=",
  56. "--no-merges",
  57. "--min-parents=",
  58. "--no-min-parents",
  59. "--max-parents=",
  60. "--no-max-parents",
  61. "--objects",
  62. "--objects-edge",
  63. "--parents",
  64. "--pretty",
  65. "--remotes=",
  66. "--remotes",
  67. "--glob=",
  68. "--sparse",
  69. "--tags=",
  70. "--tags",
  71. "--topo-order",
  72. "--date-order",
  73. "--unpacked",
  74. NULL
  75. };
  76. const char **p = rev_args;
  77. /* accept -<digit>, like traditional "head" */
  78. if ((*arg == '-') && isdigit(arg[1]))
  79. return 1;
  80. for (;;) {
  81. const char *str = *p++;
  82. int len;
  83. if (!str)
  84. return 0;
  85. len = strlen(str);
  86. if (!strcmp(arg, str) ||
  87. (str[len-1] == '=' && !strncmp(arg, str, len)))
  88. return 1;
  89. }
  90. }
  91. /* Output argument as a string, either SQ or normal */
  92. static void show(const char *arg)
  93. {
  94. if (output_sq) {
  95. int sq = '\'', ch;
  96. putchar(sq);
  97. while ((ch = *arg++)) {
  98. if (ch == sq)
  99. fputs("'\\'", stdout);
  100. putchar(ch);
  101. }
  102. putchar(sq);
  103. putchar(' ');
  104. }
  105. else
  106. puts(arg);
  107. }
  108. /* Like show(), but with a negation prefix according to type */
  109. static void show_with_type(int type, const char *arg)
  110. {
  111. if (type != show_type)
  112. putchar('^');
  113. show(arg);
  114. }
  115. /* Output a revision, only if filter allows it */
  116. static void show_rev(int type, const struct object_id *oid, const char *name)
  117. {
  118. if (!(filter & DO_REVS))
  119. return;
  120. def = NULL;
  121. if ((symbolic || abbrev_ref) && name) {
  122. if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
  123. struct object_id discard;
  124. char *full;
  125. switch (dwim_ref(name, strlen(name), &discard, &full, 0)) {
  126. case 0:
  127. /*
  128. * Not found -- not a ref. We could
  129. * emit "name" here, but symbolic-full
  130. * users are interested in finding the
  131. * refs spelled in full, and they would
  132. * need to filter non-refs if we did so.
  133. */
  134. break;
  135. case 1: /* happy */
  136. if (abbrev_ref)
  137. full = shorten_unambiguous_ref(full,
  138. abbrev_ref_strict);
  139. show_with_type(type, full);
  140. break;
  141. default: /* ambiguous */
  142. error("refname '%s' is ambiguous", name);
  143. break;
  144. }
  145. free(full);
  146. } else {
  147. show_with_type(type, name);
  148. }
  149. }
  150. else if (abbrev)
  151. show_with_type(type, find_unique_abbrev(oid, abbrev));
  152. else
  153. show_with_type(type, oid_to_hex(oid));
  154. }
  155. /* Output a flag, only if filter allows it. */
  156. static int show_flag(const char *arg)
  157. {
  158. if (!(filter & DO_FLAGS))
  159. return 0;
  160. if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
  161. show(arg);
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. static int show_default(void)
  167. {
  168. const char *s = def;
  169. if (s) {
  170. struct object_id oid;
  171. def = NULL;
  172. if (!get_oid(s, &oid)) {
  173. show_rev(NORMAL, &oid, s);
  174. return 1;
  175. }
  176. }
  177. return 0;
  178. }
  179. static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
  180. {
  181. if (ref_excluded(ref_excludes, refname))
  182. return 0;
  183. show_rev(NORMAL, oid, refname);
  184. return 0;
  185. }
  186. static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
  187. {
  188. show_rev(REVERSED, oid, refname);
  189. return 0;
  190. }
  191. static int show_abbrev(const struct object_id *oid, void *cb_data)
  192. {
  193. show_rev(NORMAL, oid, NULL);
  194. return 0;
  195. }
  196. static void show_datestring(const char *flag, const char *datestr)
  197. {
  198. char *buffer;
  199. /* date handling requires both flags and revs */
  200. if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
  201. return;
  202. buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
  203. show(buffer);
  204. free(buffer);
  205. }
  206. static int show_file(const char *arg, int output_prefix)
  207. {
  208. show_default();
  209. if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
  210. if (output_prefix) {
  211. const char *prefix = startup_info->prefix;
  212. char *fname = prefix_filename(prefix, arg);
  213. show(fname);
  214. free(fname);
  215. } else
  216. show(arg);
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. static int try_difference(const char *arg)
  222. {
  223. char *dotdot;
  224. struct object_id start_oid;
  225. struct object_id end_oid;
  226. const char *end;
  227. const char *start;
  228. int symmetric;
  229. static const char head_by_default[] = "HEAD";
  230. if (!(dotdot = strstr(arg, "..")))
  231. return 0;
  232. end = dotdot + 2;
  233. start = arg;
  234. symmetric = (*end == '.');
  235. *dotdot = 0;
  236. end += symmetric;
  237. if (!*end)
  238. end = head_by_default;
  239. if (dotdot == arg)
  240. start = head_by_default;
  241. if (start == head_by_default && end == head_by_default &&
  242. !symmetric) {
  243. /*
  244. * Just ".."? That is not a range but the
  245. * pathspec for the parent directory.
  246. */
  247. *dotdot = '.';
  248. return 0;
  249. }
  250. if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
  251. show_rev(NORMAL, &end_oid, end);
  252. show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
  253. if (symmetric) {
  254. struct commit_list *exclude;
  255. struct commit *a, *b;
  256. a = lookup_commit_reference(the_repository, &start_oid);
  257. b = lookup_commit_reference(the_repository, &end_oid);
  258. if (!a || !b) {
  259. *dotdot = '.';
  260. return 0;
  261. }
  262. exclude = get_merge_bases(a, b);
  263. while (exclude) {
  264. struct commit *commit = pop_commit(&exclude);
  265. show_rev(REVERSED, &commit->object.oid, NULL);
  266. }
  267. }
  268. *dotdot = '.';
  269. return 1;
  270. }
  271. *dotdot = '.';
  272. return 0;
  273. }
  274. static int try_parent_shorthands(const char *arg)
  275. {
  276. char *dotdot;
  277. struct object_id oid;
  278. struct commit *commit;
  279. struct commit_list *parents;
  280. int parent_number;
  281. int include_rev = 0;
  282. int include_parents = 0;
  283. int exclude_parent = 0;
  284. if ((dotdot = strstr(arg, "^!"))) {
  285. include_rev = 1;
  286. if (dotdot[2])
  287. return 0;
  288. } else if ((dotdot = strstr(arg, "^@"))) {
  289. include_parents = 1;
  290. if (dotdot[2])
  291. return 0;
  292. } else if ((dotdot = strstr(arg, "^-"))) {
  293. include_rev = 1;
  294. exclude_parent = 1;
  295. if (dotdot[2]) {
  296. char *end;
  297. exclude_parent = strtoul(dotdot + 2, &end, 10);
  298. if (*end != '\0' || !exclude_parent)
  299. return 0;
  300. }
  301. } else
  302. return 0;
  303. *dotdot = 0;
  304. if (get_oid_committish(arg, &oid) ||
  305. !(commit = lookup_commit_reference(the_repository, &oid))) {
  306. *dotdot = '^';
  307. return 0;
  308. }
  309. if (exclude_parent &&
  310. exclude_parent > commit_list_count(commit->parents)) {
  311. *dotdot = '^';
  312. return 0;
  313. }
  314. if (include_rev)
  315. show_rev(NORMAL, &oid, arg);
  316. for (parents = commit->parents, parent_number = 1;
  317. parents;
  318. parents = parents->next, parent_number++) {
  319. char *name = NULL;
  320. if (exclude_parent && parent_number != exclude_parent)
  321. continue;
  322. if (symbolic)
  323. name = xstrfmt("%s^%d", arg, parent_number);
  324. show_rev(include_parents ? NORMAL : REVERSED,
  325. &parents->item->object.oid, name);
  326. free(name);
  327. }
  328. *dotdot = '^';
  329. return 1;
  330. }
  331. static int parseopt_dump(const struct option *o, const char *arg, int unset)
  332. {
  333. struct strbuf *parsed = o->value;
  334. if (unset)
  335. strbuf_addf(parsed, " --no-%s", o->long_name);
  336. else if (o->short_name && (o->long_name == NULL || !stuck_long))
  337. strbuf_addf(parsed, " -%c", o->short_name);
  338. else
  339. strbuf_addf(parsed, " --%s", o->long_name);
  340. if (arg) {
  341. if (!stuck_long)
  342. strbuf_addch(parsed, ' ');
  343. else if (o->long_name)
  344. strbuf_addch(parsed, '=');
  345. sq_quote_buf(parsed, arg);
  346. }
  347. return 0;
  348. }
  349. static const char *skipspaces(const char *s)
  350. {
  351. while (isspace(*s))
  352. s++;
  353. return s;
  354. }
  355. static char *findspace(const char *s)
  356. {
  357. for (; *s; s++)
  358. if (isspace(*s))
  359. return (char*)s;
  360. return NULL;
  361. }
  362. static int cmd_parseopt(int argc, const char **argv, const char *prefix)
  363. {
  364. static int keep_dashdash = 0, stop_at_non_option = 0;
  365. static char const * const parseopt_usage[] = {
  366. N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
  367. NULL
  368. };
  369. static struct option parseopt_opts[] = {
  370. OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
  371. N_("keep the `--` passed as an arg")),
  372. OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
  373. N_("stop parsing after the "
  374. "first non-option argument")),
  375. OPT_BOOL(0, "stuck-long", &stuck_long,
  376. N_("output in stuck long form")),
  377. OPT_END(),
  378. };
  379. static const char * const flag_chars = "*=?!";
  380. struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
  381. const char **usage = NULL;
  382. struct option *opts = NULL;
  383. int onb = 0, osz = 0, unb = 0, usz = 0;
  384. strbuf_addstr(&parsed, "set --");
  385. argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
  386. PARSE_OPT_KEEP_DASHDASH);
  387. if (argc < 1 || strcmp(argv[0], "--"))
  388. usage_with_options(parseopt_usage, parseopt_opts);
  389. /* get the usage up to the first line with a -- on it */
  390. for (;;) {
  391. if (strbuf_getline(&sb, stdin) == EOF)
  392. die("premature end of input");
  393. ALLOC_GROW(usage, unb + 1, usz);
  394. if (!strcmp("--", sb.buf)) {
  395. if (unb < 1)
  396. die("no usage string given before the `--' separator");
  397. usage[unb] = NULL;
  398. break;
  399. }
  400. usage[unb++] = strbuf_detach(&sb, NULL);
  401. }
  402. /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
  403. while (strbuf_getline(&sb, stdin) != EOF) {
  404. const char *s;
  405. char *help;
  406. struct option *o;
  407. if (!sb.len)
  408. continue;
  409. ALLOC_GROW(opts, onb + 1, osz);
  410. memset(opts + onb, 0, sizeof(opts[onb]));
  411. o = &opts[onb++];
  412. help = findspace(sb.buf);
  413. if (!help || sb.buf == help) {
  414. o->type = OPTION_GROUP;
  415. o->help = xstrdup(skipspaces(sb.buf));
  416. continue;
  417. }
  418. *help = '\0';
  419. o->type = OPTION_CALLBACK;
  420. o->help = xstrdup(skipspaces(help+1));
  421. o->value = &parsed;
  422. o->flags = PARSE_OPT_NOARG;
  423. o->callback = &parseopt_dump;
  424. /* name(s) */
  425. s = strpbrk(sb.buf, flag_chars);
  426. if (s == NULL)
  427. s = help;
  428. if (s - sb.buf == 1) /* short option only */
  429. o->short_name = *sb.buf;
  430. else if (sb.buf[1] != ',') /* long option only */
  431. o->long_name = xmemdupz(sb.buf, s - sb.buf);
  432. else {
  433. o->short_name = *sb.buf;
  434. o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
  435. }
  436. /* flags */
  437. while (s < help) {
  438. switch (*s++) {
  439. case '=':
  440. o->flags &= ~PARSE_OPT_NOARG;
  441. continue;
  442. case '?':
  443. o->flags &= ~PARSE_OPT_NOARG;
  444. o->flags |= PARSE_OPT_OPTARG;
  445. continue;
  446. case '!':
  447. o->flags |= PARSE_OPT_NONEG;
  448. continue;
  449. case '*':
  450. o->flags |= PARSE_OPT_HIDDEN;
  451. continue;
  452. }
  453. s--;
  454. break;
  455. }
  456. if (s < help)
  457. o->argh = xmemdupz(s, help - s);
  458. }
  459. strbuf_release(&sb);
  460. /* put an OPT_END() */
  461. ALLOC_GROW(opts, onb + 1, osz);
  462. memset(opts + onb, 0, sizeof(opts[onb]));
  463. argc = parse_options(argc, argv, prefix, opts, usage,
  464. (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
  465. (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
  466. PARSE_OPT_SHELL_EVAL);
  467. strbuf_addstr(&parsed, " --");
  468. sq_quote_argv(&parsed, argv);
  469. puts(parsed.buf);
  470. return 0;
  471. }
  472. static int cmd_sq_quote(int argc, const char **argv)
  473. {
  474. struct strbuf buf = STRBUF_INIT;
  475. if (argc)
  476. sq_quote_argv(&buf, argv);
  477. printf("%s\n", buf.buf);
  478. strbuf_release(&buf);
  479. return 0;
  480. }
  481. static void die_no_single_rev(int quiet)
  482. {
  483. if (quiet)
  484. exit(1);
  485. else
  486. die("Needed a single revision");
  487. }
  488. static const char builtin_rev_parse_usage[] =
  489. N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
  490. " or: git rev-parse --sq-quote [<arg>...]\n"
  491. " or: git rev-parse [<options>] [<arg>...]\n"
  492. "\n"
  493. "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
  494. /*
  495. * Parse "opt" or "opt=<value>", setting value respectively to either
  496. * NULL or the string after "=".
  497. */
  498. static int opt_with_value(const char *arg, const char *opt, const char **value)
  499. {
  500. if (skip_prefix(arg, opt, &arg)) {
  501. if (!*arg) {
  502. *value = NULL;
  503. return 1;
  504. }
  505. if (*arg++ == '=') {
  506. *value = arg;
  507. return 1;
  508. }
  509. }
  510. return 0;
  511. }
  512. static void handle_ref_opt(const char *pattern, const char *prefix)
  513. {
  514. if (pattern)
  515. for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
  516. else
  517. for_each_ref_in(prefix, show_reference, NULL);
  518. clear_ref_exclusion(&ref_excludes);
  519. }
  520. int cmd_rev_parse(int argc, const char **argv, const char *prefix)
  521. {
  522. int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
  523. int did_repo_setup = 0;
  524. int has_dashdash = 0;
  525. int output_prefix = 0;
  526. struct object_id oid;
  527. unsigned int flags = 0;
  528. const char *name = NULL;
  529. struct object_context unused;
  530. struct strbuf buf = STRBUF_INIT;
  531. const int hexsz = the_hash_algo->hexsz;
  532. if (argc > 1 && !strcmp("--parseopt", argv[1]))
  533. return cmd_parseopt(argc - 1, argv + 1, prefix);
  534. if (argc > 1 && !strcmp("--sq-quote", argv[1]))
  535. return cmd_sq_quote(argc - 2, argv + 2);
  536. if (argc > 1 && !strcmp("-h", argv[1]))
  537. usage(builtin_rev_parse_usage);
  538. for (i = 1; i < argc; i++) {
  539. if (!strcmp(argv[i], "--")) {
  540. has_dashdash = 1;
  541. break;
  542. }
  543. }
  544. /* No options; just report on whether we're in a git repo or not. */
  545. if (argc == 1) {
  546. setup_git_directory();
  547. git_config(git_default_config, NULL);
  548. return 0;
  549. }
  550. for (i = 1; i < argc; i++) {
  551. const char *arg = argv[i];
  552. if (!strcmp(arg, "--local-env-vars")) {
  553. int i;
  554. for (i = 0; local_repo_env[i]; i++)
  555. printf("%s\n", local_repo_env[i]);
  556. continue;
  557. }
  558. if (!strcmp(arg, "--resolve-git-dir")) {
  559. const char *gitdir = argv[++i];
  560. if (!gitdir)
  561. die("--resolve-git-dir requires an argument");
  562. gitdir = resolve_gitdir(gitdir);
  563. if (!gitdir)
  564. die("not a gitdir '%s'", argv[i]);
  565. puts(gitdir);
  566. continue;
  567. }
  568. /* The rest of the options require a git repository. */
  569. if (!did_repo_setup) {
  570. prefix = setup_git_directory();
  571. git_config(git_default_config, NULL);
  572. did_repo_setup = 1;
  573. }
  574. if (!strcmp(arg, "--git-path")) {
  575. if (!argv[i + 1])
  576. die("--git-path requires an argument");
  577. strbuf_reset(&buf);
  578. puts(relative_path(git_path("%s", argv[i + 1]),
  579. prefix, &buf));
  580. i++;
  581. continue;
  582. }
  583. if (as_is) {
  584. if (show_file(arg, output_prefix) && as_is < 2)
  585. verify_filename(prefix, arg, 0);
  586. continue;
  587. }
  588. if (!strcmp(arg,"-n")) {
  589. if (++i >= argc)
  590. die("-n requires an argument");
  591. if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
  592. show(arg);
  593. show(argv[i]);
  594. }
  595. continue;
  596. }
  597. if (starts_with(arg, "-n")) {
  598. if ((filter & DO_FLAGS) && (filter & DO_REVS))
  599. show(arg);
  600. continue;
  601. }
  602. if (*arg == '-') {
  603. if (!strcmp(arg, "--")) {
  604. as_is = 2;
  605. /* Pass on the "--" if we show anything but files.. */
  606. if (filter & (DO_FLAGS | DO_REVS))
  607. show_file(arg, 0);
  608. continue;
  609. }
  610. if (!strcmp(arg, "--default")) {
  611. def = argv[++i];
  612. if (!def)
  613. die("--default requires an argument");
  614. continue;
  615. }
  616. if (!strcmp(arg, "--prefix")) {
  617. prefix = argv[++i];
  618. if (!prefix)
  619. die("--prefix requires an argument");
  620. startup_info->prefix = prefix;
  621. output_prefix = 1;
  622. continue;
  623. }
  624. if (!strcmp(arg, "--revs-only")) {
  625. filter &= ~DO_NOREV;
  626. continue;
  627. }
  628. if (!strcmp(arg, "--no-revs")) {
  629. filter &= ~DO_REVS;
  630. continue;
  631. }
  632. if (!strcmp(arg, "--flags")) {
  633. filter &= ~DO_NONFLAGS;
  634. continue;
  635. }
  636. if (!strcmp(arg, "--no-flags")) {
  637. filter &= ~DO_FLAGS;
  638. continue;
  639. }
  640. if (!strcmp(arg, "--verify")) {
  641. filter &= ~(DO_FLAGS|DO_NOREV);
  642. verify = 1;
  643. continue;
  644. }
  645. if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
  646. quiet = 1;
  647. flags |= GET_OID_QUIETLY;
  648. continue;
  649. }
  650. if (opt_with_value(arg, "--short", &arg)) {
  651. filter &= ~(DO_FLAGS|DO_NOREV);
  652. verify = 1;
  653. abbrev = DEFAULT_ABBREV;
  654. if (!arg)
  655. continue;
  656. abbrev = strtoul(arg, NULL, 10);
  657. if (abbrev < MINIMUM_ABBREV)
  658. abbrev = MINIMUM_ABBREV;
  659. else if (hexsz <= abbrev)
  660. abbrev = hexsz;
  661. continue;
  662. }
  663. if (!strcmp(arg, "--sq")) {
  664. output_sq = 1;
  665. continue;
  666. }
  667. if (!strcmp(arg, "--not")) {
  668. show_type ^= REVERSED;
  669. continue;
  670. }
  671. if (!strcmp(arg, "--symbolic")) {
  672. symbolic = SHOW_SYMBOLIC_ASIS;
  673. continue;
  674. }
  675. if (!strcmp(arg, "--symbolic-full-name")) {
  676. symbolic = SHOW_SYMBOLIC_FULL;
  677. continue;
  678. }
  679. if (opt_with_value(arg, "--abbrev-ref", &arg)) {
  680. abbrev_ref = 1;
  681. abbrev_ref_strict = warn_ambiguous_refs;
  682. if (arg) {
  683. if (!strcmp(arg, "strict"))
  684. abbrev_ref_strict = 1;
  685. else if (!strcmp(arg, "loose"))
  686. abbrev_ref_strict = 0;
  687. else
  688. die("unknown mode for --abbrev-ref: %s",
  689. arg);
  690. }
  691. continue;
  692. }
  693. if (!strcmp(arg, "--all")) {
  694. for_each_ref(show_reference, NULL);
  695. clear_ref_exclusion(&ref_excludes);
  696. continue;
  697. }
  698. if (skip_prefix(arg, "--disambiguate=", &arg)) {
  699. for_each_abbrev(arg, show_abbrev, NULL);
  700. continue;
  701. }
  702. if (!strcmp(arg, "--bisect")) {
  703. for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
  704. for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
  705. continue;
  706. }
  707. if (opt_with_value(arg, "--branches", &arg)) {
  708. handle_ref_opt(arg, "refs/heads/");
  709. continue;
  710. }
  711. if (opt_with_value(arg, "--tags", &arg)) {
  712. handle_ref_opt(arg, "refs/tags/");
  713. continue;
  714. }
  715. if (skip_prefix(arg, "--glob=", &arg)) {
  716. handle_ref_opt(arg, NULL);
  717. continue;
  718. }
  719. if (opt_with_value(arg, "--remotes", &arg)) {
  720. handle_ref_opt(arg, "refs/remotes/");
  721. continue;
  722. }
  723. if (skip_prefix(arg, "--exclude=", &arg)) {
  724. add_ref_exclusion(&ref_excludes, arg);
  725. continue;
  726. }
  727. if (!strcmp(arg, "--show-toplevel")) {
  728. const char *work_tree = get_git_work_tree();
  729. if (work_tree)
  730. puts(work_tree);
  731. else
  732. die("this operation must be run in a work tree");
  733. continue;
  734. }
  735. if (!strcmp(arg, "--show-superproject-working-tree")) {
  736. struct strbuf superproject = STRBUF_INIT;
  737. if (get_superproject_working_tree(&superproject))
  738. puts(superproject.buf);
  739. strbuf_release(&superproject);
  740. continue;
  741. }
  742. if (!strcmp(arg, "--show-prefix")) {
  743. if (prefix)
  744. puts(prefix);
  745. else
  746. putchar('\n');
  747. continue;
  748. }
  749. if (!strcmp(arg, "--show-cdup")) {
  750. const char *pfx = prefix;
  751. if (!is_inside_work_tree()) {
  752. const char *work_tree =
  753. get_git_work_tree();
  754. if (work_tree)
  755. printf("%s\n", work_tree);
  756. continue;
  757. }
  758. while (pfx) {
  759. pfx = strchr(pfx, '/');
  760. if (pfx) {
  761. pfx++;
  762. printf("../");
  763. }
  764. }
  765. putchar('\n');
  766. continue;
  767. }
  768. if (!strcmp(arg, "--git-dir") ||
  769. !strcmp(arg, "--absolute-git-dir")) {
  770. const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
  771. char *cwd;
  772. int len;
  773. if (arg[2] == 'g') { /* --git-dir */
  774. if (gitdir) {
  775. puts(gitdir);
  776. continue;
  777. }
  778. if (!prefix) {
  779. puts(".git");
  780. continue;
  781. }
  782. } else { /* --absolute-git-dir */
  783. if (!gitdir && !prefix)
  784. gitdir = ".git";
  785. if (gitdir) {
  786. struct strbuf realpath = STRBUF_INIT;
  787. strbuf_realpath(&realpath, gitdir, 1);
  788. puts(realpath.buf);
  789. strbuf_release(&realpath);
  790. continue;
  791. }
  792. }
  793. cwd = xgetcwd();
  794. len = strlen(cwd);
  795. printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
  796. free(cwd);
  797. continue;
  798. }
  799. if (!strcmp(arg, "--git-common-dir")) {
  800. strbuf_reset(&buf);
  801. puts(relative_path(get_git_common_dir(),
  802. prefix, &buf));
  803. continue;
  804. }
  805. if (!strcmp(arg, "--is-inside-git-dir")) {
  806. printf("%s\n", is_inside_git_dir() ? "true"
  807. : "false");
  808. continue;
  809. }
  810. if (!strcmp(arg, "--is-inside-work-tree")) {
  811. printf("%s\n", is_inside_work_tree() ? "true"
  812. : "false");
  813. continue;
  814. }
  815. if (!strcmp(arg, "--is-bare-repository")) {
  816. printf("%s\n", is_bare_repository() ? "true"
  817. : "false");
  818. continue;
  819. }
  820. if (!strcmp(arg, "--is-shallow-repository")) {
  821. printf("%s\n",
  822. is_repository_shallow(the_repository) ? "true"
  823. : "false");
  824. continue;
  825. }
  826. if (!strcmp(arg, "--shared-index-path")) {
  827. if (read_cache() < 0)
  828. die(_("Could not read the index"));
  829. if (the_index.split_index) {
  830. const struct object_id *oid = &the_index.split_index->base_oid;
  831. const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
  832. strbuf_reset(&buf);
  833. puts(relative_path(path, prefix, &buf));
  834. }
  835. continue;
  836. }
  837. if (skip_prefix(arg, "--since=", &arg)) {
  838. show_datestring("--max-age=", arg);
  839. continue;
  840. }
  841. if (skip_prefix(arg, "--after=", &arg)) {
  842. show_datestring("--max-age=", arg);
  843. continue;
  844. }
  845. if (skip_prefix(arg, "--before=", &arg)) {
  846. show_datestring("--min-age=", arg);
  847. continue;
  848. }
  849. if (skip_prefix(arg, "--until=", &arg)) {
  850. show_datestring("--min-age=", arg);
  851. continue;
  852. }
  853. if (opt_with_value(arg, "--show-object-format", &arg)) {
  854. const char *val = arg ? arg : "storage";
  855. if (strcmp(val, "storage") &&
  856. strcmp(val, "input") &&
  857. strcmp(val, "output"))
  858. die("unknown mode for --show-object-format: %s",
  859. arg);
  860. puts(the_hash_algo->name);
  861. continue;
  862. }
  863. if (show_flag(arg) && verify)
  864. die_no_single_rev(quiet);
  865. continue;
  866. }
  867. /* Not a flag argument */
  868. if (try_difference(arg))
  869. continue;
  870. if (try_parent_shorthands(arg))
  871. continue;
  872. name = arg;
  873. type = NORMAL;
  874. if (*arg == '^') {
  875. name++;
  876. type = REVERSED;
  877. }
  878. if (!get_oid_with_context(the_repository, name,
  879. flags, &oid, &unused)) {
  880. if (verify)
  881. revs_count++;
  882. else
  883. show_rev(type, &oid, name);
  884. continue;
  885. }
  886. if (verify)
  887. die_no_single_rev(quiet);
  888. if (has_dashdash)
  889. die("bad revision '%s'", arg);
  890. as_is = 1;
  891. if (!show_file(arg, output_prefix))
  892. continue;
  893. verify_filename(prefix, arg, 1);
  894. }
  895. strbuf_release(&buf);
  896. if (verify) {
  897. if (revs_count == 1) {
  898. show_rev(type, &oid, name);
  899. return 0;
  900. } else if (revs_count == 0 && show_default())
  901. return 0;
  902. die_no_single_rev(quiet);
  903. } else
  904. show_default();
  905. return 0;
  906. }