blob: 7a00da820355b61c548449c55381577a2232a0e8 [file] [log] [blame]
Linus Torvalds178cb242005-06-13 10:06:50 -07001/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07006#define USE_THE_INDEX_COMPATIBILITY_MACROS
Linus Torvalds178cb242005-06-13 10:06:50 -07007#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Linus Torvaldsa8be83f2005-06-20 20:28:09 -07009#include "commit.h"
Linus Torvalds960bba02005-07-03 13:07:52 -070010#include "refs.h"
Linus Torvaldsc1babb12005-09-20 14:13:24 -070011#include "quote.h"
Christian Couder895f10c2006-06-03 18:45:43 +020012#include "builtin.h"
Pierre Habouzit21d47832007-11-04 11:30:53 +010013#include "parse-options.h"
Junio C Hamano9dc01bf2013-11-01 12:13:01 -070014#include "diff.h"
15#include "revision.h"
Nguyễn Thái Ngọc Duya76295d2014-06-13 19:19:46 +070016#include "split-index.h"
Stefan Bellerbf0231c2017-03-08 15:07:42 -080017#include "submodule.h"
Derrick Stolee64043552018-07-20 16:33:04 +000018#include "commit-reach.h"
Linus Torvaldsa8be83f2005-06-20 20:28:09 -070019
Junio C Hamano4866ccf2005-08-24 14:30:04 -070020#define DO_REVS 1
21#define DO_NOREV 2
22#define DO_FLAGS 4
23#define DO_NONFLAGS 8
24static int filter = ~0;
25
David Rientjes96f1e582006-08-15 10:23:48 -070026static const char *def;
Linus Torvalds023d66e2005-06-24 10:12:55 -070027
Linus Torvalds042a4ed2005-06-26 11:34:30 -070028#define NORMAL 0
29#define REVERSED 1
30static int show_type = NORMAL;
Junio C Hamanoa6d97d42008-01-05 12:09:55 -080031
32#define SHOW_SYMBOLIC_ASIS 1
33#define SHOW_SYMBOLIC_FULL 2
David Rientjes96f1e582006-08-15 10:23:48 -070034static int symbolic;
35static int abbrev;
Bert Wesarga45d3462009-04-13 13:20:26 +020036static int abbrev_ref;
37static int abbrev_ref_strict;
David Rientjes96f1e582006-08-15 10:23:48 -070038static int output_sq;
Junio C Hamano4866ccf2005-08-24 14:30:04 -070039
Nicolas Vigierf8c87212013-10-31 12:08:29 +010040static int stuck_long;
Junio C Hamano9dc01bf2013-11-01 12:13:01 -070041static struct string_list *ref_excludes;
Nicolas Vigierf8c87212013-10-31 12:08:29 +010042
Linus Torvalds921d8652005-06-13 11:14:20 -070043/*
44 * Some arguments are relevant "revision" arguments,
45 * others are about output format or other details.
46 * This sorts it all out.
47 */
48static int is_rev_argument(const char *arg)
49{
50 static const char *rev_args[] = {
Junio C Hamanoe091eb92005-10-05 14:49:54 -070051 "--all",
Junio C Hamano5ccfb752005-08-08 19:31:37 -070052 "--bisect",
Junio C Hamano5a83f3b2005-10-30 01:08:35 -080053 "--dense",
Ilari Liusvaarab09fe972010-01-20 11:48:26 +020054 "--branches=",
Seana62be772006-05-13 21:43:00 -040055 "--branches",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070056 "--header",
Junio C Hamanocc243c32011-05-18 18:08:09 -070057 "--ignore-missing",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070058 "--max-age=",
59 "--max-count=",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070060 "--min-age=",
Junio C Hamano5ccfb752005-08-08 19:31:37 -070061 "--no-merges",
Michael J Gruberad5aeed2011-03-21 11:14:06 +010062 "--min-parents=",
63 "--no-min-parents",
64 "--max-parents=",
65 "--no-max-parents",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070066 "--objects",
Junio C Hamanoc6496572006-02-19 03:32:31 -080067 "--objects-edge",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070068 "--parents",
69 "--pretty",
Ilari Liusvaarab09fe972010-01-20 11:48:26 +020070 "--remotes=",
Seana62be772006-05-13 21:43:00 -040071 "--remotes",
Ilari Liusvaarad08bae72010-01-20 11:48:25 +020072 "--glob=",
Junio C Hamano5a83f3b2005-10-30 01:08:35 -080073 "--sparse",
Ilari Liusvaarab09fe972010-01-20 11:48:26 +020074 "--tags=",
Seana62be772006-05-13 21:43:00 -040075 "--tags",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070076 "--topo-order",
Junio C Hamano4c8725f2006-02-15 22:05:33 -080077 "--date-order",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070078 "--unpacked",
Linus Torvalds921d8652005-06-13 11:14:20 -070079 NULL
80 };
81 const char **p = rev_args;
82
Eric Wong82333402006-01-29 16:28:02 -080083 /* accept -<digit>, like traditional "head" */
84 if ((*arg == '-') && isdigit(arg[1]))
85 return 1;
86
Linus Torvalds921d8652005-06-13 11:14:20 -070087 for (;;) {
88 const char *str = *p++;
89 int len;
90 if (!str)
91 return 0;
92 len = strlen(str);
Junio C Hamano4866ccf2005-08-24 14:30:04 -070093 if (!strcmp(arg, str) ||
94 (str[len-1] == '=' && !strncmp(arg, str, len)))
Linus Torvalds921d8652005-06-13 11:14:20 -070095 return 1;
96 }
97}
98
Junio C Hamano4866ccf2005-08-24 14:30:04 -070099/* Output argument as a string, either SQ or normal */
Junio C Hamano5bb2c652005-07-22 19:08:32 -0700100static void show(const char *arg)
101{
102 if (output_sq) {
103 int sq = '\'', ch;
104
105 putchar(sq);
106 while ((ch = *arg++)) {
107 if (ch == sq)
108 fputs("'\\'", stdout);
109 putchar(ch);
110 }
111 putchar(sq);
112 putchar(' ');
113 }
114 else
115 puts(arg);
116}
117
Johannes Sixte00f3792008-05-23 16:13:05 +0200118/* Like show(), but with a negation prefix according to type */
119static void show_with_type(int type, const char *arg)
120{
121 if (type != show_type)
122 putchar('^');
123 show(arg);
124}
125
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700126/* Output a revision, only if filter allows it */
brian m. carlson8bc095f2017-05-01 02:29:02 +0000127static void show_rev(int type, const struct object_id *oid, const char *name)
Linus Torvalds023d66e2005-06-24 10:12:55 -0700128{
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700129 if (!(filter & DO_REVS))
Linus Torvalds023d66e2005-06-24 10:12:55 -0700130 return;
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700131 def = NULL;
Junio C Hamano5bb2c652005-07-22 19:08:32 -0700132
Bert Wesarga45d3462009-04-13 13:20:26 +0200133 if ((symbolic || abbrev_ref) && name) {
134 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
brian m. carlson8bc095f2017-05-01 02:29:02 +0000135 struct object_id discard;
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800136 char *full;
137
brian m. carlsoncca5fa62017-10-15 22:06:57 +0000138 switch (dwim_ref(name, strlen(name), &discard, &full)) {
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800139 case 0:
140 /*
141 * Not found -- not a ref. We could
142 * emit "name" here, but symbolic-full
143 * users are interested in finding the
144 * refs spelled in full, and they would
145 * need to filter non-refs if we did so.
146 */
147 break;
148 case 1: /* happy */
Bert Wesarga45d3462009-04-13 13:20:26 +0200149 if (abbrev_ref)
150 full = shorten_unambiguous_ref(full,
151 abbrev_ref_strict);
Johannes Sixte00f3792008-05-23 16:13:05 +0200152 show_with_type(type, full);
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800153 break;
154 default: /* ambiguous */
155 error("refname '%s' is ambiguous", name);
156 break;
157 }
Jeff King28b35632014-07-24 00:41:11 -0400158 free(full);
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800159 } else {
Johannes Sixte00f3792008-05-23 16:13:05 +0200160 show_with_type(type, name);
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800161 }
162 }
Junio C Hamanod5012502006-01-25 01:35:38 -0800163 else if (abbrev)
brian m. carlsonaab95832018-03-12 02:27:30 +0000164 show_with_type(type, find_unique_abbrev(oid, abbrev));
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700165 else
brian m. carlson8bc095f2017-05-01 02:29:02 +0000166 show_with_type(type, oid_to_hex(oid));
Linus Torvalds023d66e2005-06-24 10:12:55 -0700167}
168
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700169/* Output a flag, only if filter allows it. */
Junio C Hamano16cee382006-06-05 22:36:21 -0700170static int show_flag(const char *arg)
Linus Torvalds023d66e2005-06-24 10:12:55 -0700171{
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700172 if (!(filter & DO_FLAGS))
Linus Torvalds9523a4c2006-02-05 11:58:34 -0800173 return 0;
174 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
Linus Torvalds0360e992005-08-23 10:47:54 -0700175 show(arg);
Linus Torvalds9523a4c2006-02-05 11:58:34 -0800176 return 1;
177 }
178 return 0;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700179}
180
Christian Couderdfd1b742008-05-11 18:28:25 +0200181static int show_default(void)
Linus Torvalds023d66e2005-06-24 10:12:55 -0700182{
Junio C Hamano16cee382006-06-05 22:36:21 -0700183 const char *s = def;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700184
185 if (s) {
brian m. carlson8bc095f2017-05-01 02:29:02 +0000186 struct object_id oid;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700187
188 def = NULL;
brian m. carlson8bc095f2017-05-01 02:29:02 +0000189 if (!get_oid(s, &oid)) {
190 show_rev(NORMAL, &oid, s);
Christian Couderdfd1b742008-05-11 18:28:25 +0200191 return 1;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700192 }
Linus Torvalds023d66e2005-06-24 10:12:55 -0700193 }
Christian Couderdfd1b742008-05-11 18:28:25 +0200194 return 0;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700195}
196
Michael Haggertye23b0362015-05-25 18:38:29 +0000197static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
Linus Torvalds960bba02005-07-03 13:07:52 -0700198{
Junio C Hamano9dc01bf2013-11-01 12:13:01 -0700199 if (ref_excluded(ref_excludes, refname))
200 return 0;
brian m. carlson8bc095f2017-05-01 02:29:02 +0000201 show_rev(NORMAL, oid, refname);
Linus Torvalds960bba02005-07-03 13:07:52 -0700202 return 0;
203}
204
Michael Haggertye23b0362015-05-25 18:38:29 +0000205static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
Linus Torvaldsad3f9a72009-10-27 11:28:07 -0700206{
brian m. carlson8bc095f2017-05-01 02:29:02 +0000207 show_rev(REVERSED, oid, refname);
Linus Torvaldsad3f9a72009-10-27 11:28:07 -0700208 return 0;
209}
210
brian m. carlson1b7ba792017-03-31 01:39:59 +0000211static int show_abbrev(const struct object_id *oid, void *cb_data)
Junio C Hamano957d7402012-07-03 14:21:59 -0700212{
brian m. carlson8bc095f2017-05-01 02:29:02 +0000213 show_rev(NORMAL, oid, NULL);
Junio C Hamano957d7402012-07-03 14:21:59 -0700214 return 0;
215}
216
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700217static void show_datestring(const char *flag, const char *datestr)
218{
Jeff King5b1ef2c2017-03-28 15:46:50 -0400219 char *buffer;
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700220
221 /* date handling requires both flags and revs */
222 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
223 return;
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200224 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700225 show(buffer);
Jeff King5b1ef2c2017-03-28 15:46:50 -0400226 free(buffer);
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700227}
228
John Keeping12b9d322013-06-16 15:18:17 +0100229static int show_file(const char *arg, int output_prefix)
Linus Torvalds7a3dd472005-10-18 00:16:45 -0700230{
Linus Torvalds7b34c2f2005-10-25 15:24:55 -0700231 show_default();
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800232 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
John Keeping12b9d322013-06-16 15:18:17 +0100233 if (output_prefix) {
234 const char *prefix = startup_info->prefix;
Jeff Kinge4da43b2017-03-20 21:28:49 -0400235 char *fname = prefix_filename(prefix, arg);
236 show(fname);
237 free(fname);
John Keeping12b9d322013-06-16 15:18:17 +0100238 } else
239 show(arg);
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800240 return 1;
241 }
242 return 0;
Linus Torvalds7a3dd472005-10-18 00:16:45 -0700243}
244
Junio C Hamanob7d936b2006-07-06 00:16:35 -0700245static int try_difference(const char *arg)
Santi Béjar3dd4e732006-07-04 11:02:22 +0200246{
247 char *dotdot;
Brandon Williamsae90bdc2018-02-14 10:59:27 -0800248 struct object_id start_oid;
249 struct object_id end_oid;
250 const char *end;
251 const char *start;
Santi Béjar3dd4e732006-07-04 11:02:22 +0200252 int symmetric;
Junio C Hamano003c84f2011-05-02 13:39:16 -0700253 static const char head_by_default[] = "HEAD";
Santi Béjar3dd4e732006-07-04 11:02:22 +0200254
255 if (!(dotdot = strstr(arg, "..")))
256 return 0;
Brandon Williamsae90bdc2018-02-14 10:59:27 -0800257 end = dotdot + 2;
258 start = arg;
259 symmetric = (*end == '.');
Santi Béjar3dd4e732006-07-04 11:02:22 +0200260
261 *dotdot = 0;
Brandon Williamsae90bdc2018-02-14 10:59:27 -0800262 end += symmetric;
Santi Béjar3dd4e732006-07-04 11:02:22 +0200263
Brandon Williamsae90bdc2018-02-14 10:59:27 -0800264 if (!*end)
265 end = head_by_default;
Santi Béjar3dd4e732006-07-04 11:02:22 +0200266 if (dotdot == arg)
Brandon Williamsae90bdc2018-02-14 10:59:27 -0800267 start = head_by_default;
Junio C Hamano003c84f2011-05-02 13:39:16 -0700268
Brandon Williamsae90bdc2018-02-14 10:59:27 -0800269 if (start == head_by_default && end == head_by_default &&
Junio C Hamano003c84f2011-05-02 13:39:16 -0700270 !symmetric) {
271 /*
272 * Just ".."? That is not a range but the
273 * pathspec for the parent directory.
274 */
275 *dotdot = '.';
276 return 0;
277 }
278
Brandon Williamsae90bdc2018-02-14 10:59:27 -0800279 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
280 show_rev(NORMAL, &end_oid, end);
281 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
Santi Béjar3dd4e732006-07-04 11:02:22 +0200282 if (symmetric) {
283 struct commit_list *exclude;
284 struct commit *a, *b;
Stefan Beller2122f672018-06-28 18:21:58 -0700285 a = lookup_commit_reference(the_repository, &start_oid);
286 b = lookup_commit_reference(the_repository, &end_oid);
Elijah Newren0ed556d2018-05-23 23:27:33 -0700287 if (!a || !b) {
288 *dotdot = '.';
289 return 0;
290 }
Junio C Hamano2ce406c2014-10-30 12:20:44 -0700291 exclude = get_merge_bases(a, b);
Santi Béjar3dd4e732006-07-04 11:02:22 +0200292 while (exclude) {
René Scharfee510ab82015-10-24 18:21:31 +0200293 struct commit *commit = pop_commit(&exclude);
brian m. carlson8bc095f2017-05-01 02:29:02 +0000294 show_rev(REVERSED, &commit->object.oid, NULL);
Santi Béjar3dd4e732006-07-04 11:02:22 +0200295 }
296 }
Jeff King62f162f2013-12-06 17:07:52 -0500297 *dotdot = '.';
Santi Béjar3dd4e732006-07-04 11:02:22 +0200298 return 1;
299 }
300 *dotdot = '.';
301 return 0;
302}
303
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200304static int try_parent_shorthands(const char *arg)
305{
306 char *dotdot;
brian m. carlson8bc095f2017-05-01 02:29:02 +0000307 struct object_id oid;
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200308 struct commit *commit;
309 struct commit_list *parents;
Vegard Nossum87793512016-09-27 10:32:49 +0200310 int parent_number;
311 int include_rev = 0;
312 int include_parents = 0;
313 int exclude_parent = 0;
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200314
Vegard Nossum87793512016-09-27 10:32:49 +0200315 if ((dotdot = strstr(arg, "^!"))) {
316 include_rev = 1;
317 if (dotdot[2])
318 return 0;
319 } else if ((dotdot = strstr(arg, "^@"))) {
320 include_parents = 1;
321 if (dotdot[2])
322 return 0;
323 } else if ((dotdot = strstr(arg, "^-"))) {
324 include_rev = 1;
325 exclude_parent = 1;
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200326
Vegard Nossum87793512016-09-27 10:32:49 +0200327 if (dotdot[2]) {
328 char *end;
329 exclude_parent = strtoul(dotdot + 2, &end, 10);
330 if (*end != '\0' || !exclude_parent)
331 return 0;
332 }
333 } else
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200334 return 0;
335
336 *dotdot = 0;
Elijah Newren0ed556d2018-05-23 23:27:33 -0700337 if (get_oid_committish(arg, &oid) ||
Stefan Beller2122f672018-06-28 18:21:58 -0700338 !(commit = lookup_commit_reference(the_repository, &oid))) {
Jeff King62f162f2013-12-06 17:07:52 -0500339 *dotdot = '^';
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200340 return 0;
Jeff King62f162f2013-12-06 17:07:52 -0500341 }
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200342
Vegard Nossum87793512016-09-27 10:32:49 +0200343 if (exclude_parent &&
344 exclude_parent > commit_list_count(commit->parents)) {
345 *dotdot = '^';
346 return 0;
347 }
348
349 if (include_rev)
brian m. carlson8bc095f2017-05-01 02:29:02 +0000350 show_rev(NORMAL, &oid, arg);
Vegard Nossum87793512016-09-27 10:32:49 +0200351 for (parents = commit->parents, parent_number = 1;
352 parents;
353 parents = parents->next, parent_number++) {
Jeff Kinga2e7b042016-11-16 00:46:26 -0800354 char *name = NULL;
355
Vegard Nossum87793512016-09-27 10:32:49 +0200356 if (exclude_parent && parent_number != exclude_parent)
357 continue;
358
Jeff Kinga2e7b042016-11-16 00:46:26 -0800359 if (symbolic)
360 name = xstrfmt("%s^%d", arg, parent_number);
Vegard Nossum87793512016-09-27 10:32:49 +0200361 show_rev(include_parents ? NORMAL : REVERSED,
brian m. carlson8bc095f2017-05-01 02:29:02 +0000362 &parents->item->object.oid, name);
Jeff Kinga2e7b042016-11-16 00:46:26 -0800363 free(name);
Vegard Nossum87793512016-09-27 10:32:49 +0200364 }
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200365
Jeff King62f162f2013-12-06 17:07:52 -0500366 *dotdot = '^';
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200367 return 1;
368}
369
Pierre Habouzit21d47832007-11-04 11:30:53 +0100370static int parseopt_dump(const struct option *o, const char *arg, int unset)
371{
372 struct strbuf *parsed = o->value;
373 if (unset)
374 strbuf_addf(parsed, " --no-%s", o->long_name);
Nicolas Vigierf8c87212013-10-31 12:08:29 +0100375 else if (o->short_name && (o->long_name == NULL || !stuck_long))
Pierre Habouzit21d47832007-11-04 11:30:53 +0100376 strbuf_addf(parsed, " -%c", o->short_name);
377 else
378 strbuf_addf(parsed, " --%s", o->long_name);
379 if (arg) {
Nicolas Vigierf8c87212013-10-31 12:08:29 +0100380 if (!stuck_long)
381 strbuf_addch(parsed, ' ');
382 else if (o->long_name)
383 strbuf_addch(parsed, '=');
Pierre Habouzit21d47832007-11-04 11:30:53 +0100384 sq_quote_buf(parsed, arg);
385 }
386 return 0;
387}
388
389static const char *skipspaces(const char *s)
390{
391 while (isspace(*s))
392 s++;
393 return s;
394}
395
Brandon Casey33e75122017-09-17 15:28:16 -0700396static char *findspace(const char *s)
397{
398 for (; *s; s++)
399 if (isspace(*s))
400 return (char*)s;
401 return NULL;
402}
403
Pierre Habouzit21d47832007-11-04 11:30:53 +0100404static int cmd_parseopt(int argc, const char **argv, const char *prefix)
405{
Uwe Kleine-König6e0800e2009-06-14 01:58:43 +0200406 static int keep_dashdash = 0, stop_at_non_option = 0;
Pierre Habouzit21d47832007-11-04 11:30:53 +0100407 static char const * const parseopt_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700408 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
Pierre Habouzit21d47832007-11-04 11:30:53 +0100409 NULL
410 };
411 static struct option parseopt_opts[] = {
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200412 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
Nguyễn Thái Ngọc Duy2c7c1842012-08-20 19:32:40 +0700413 N_("keep the `--` passed as an arg")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200414 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
Nguyễn Thái Ngọc Duy2c7c1842012-08-20 19:32:40 +0700415 N_("stop parsing after the "
416 "first non-option argument")),
Nicolas Vigierf8c87212013-10-31 12:08:29 +0100417 OPT_BOOL(0, "stuck-long", &stuck_long,
418 N_("output in stuck long form")),
Pierre Habouzit21d47832007-11-04 11:30:53 +0100419 OPT_END(),
420 };
Ilya Bobyr2d893df2015-07-14 01:17:44 -0700421 static const char * const flag_chars = "*=?!";
Pierre Habouzit21d47832007-11-04 11:30:53 +0100422
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500423 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
Pierre Habouzit21d47832007-11-04 11:30:53 +0100424 const char **usage = NULL;
425 struct option *opts = NULL;
426 int onb = 0, osz = 0, unb = 0, usz = 0;
427
Pierre Habouzit21d47832007-11-04 11:30:53 +0100428 strbuf_addstr(&parsed, "set --");
Stephen Boyd37782922009-05-23 11:53:12 -0700429 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
Pierre Habouzit21d47832007-11-04 11:30:53 +0100430 PARSE_OPT_KEEP_DASHDASH);
431 if (argc < 1 || strcmp(argv[0], "--"))
432 usage_with_options(parseopt_usage, parseopt_opts);
433
Pierre Habouzit21d47832007-11-04 11:30:53 +0100434 /* get the usage up to the first line with a -- on it */
435 for (;;) {
Junio C Hamano72e37b62015-10-28 13:59:44 -0700436 if (strbuf_getline(&sb, stdin) == EOF)
Pierre Habouzit21d47832007-11-04 11:30:53 +0100437 die("premature end of input");
438 ALLOC_GROW(usage, unb + 1, usz);
439 if (!strcmp("--", sb.buf)) {
440 if (unb < 1)
441 die("no usage string given before the `--' separator");
442 usage[unb] = NULL;
443 break;
444 }
445 usage[unb++] = strbuf_detach(&sb, NULL);
446 }
447
Ilya Bobyr9bab5b62014-03-22 02:47:34 -0700448 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
Junio C Hamano72e37b62015-10-28 13:59:44 -0700449 while (strbuf_getline(&sb, stdin) != EOF) {
Pierre Habouzit21d47832007-11-04 11:30:53 +0100450 const char *s;
Brandon Casey28a8d0f2017-09-17 15:28:15 -0700451 char *help;
Pierre Habouzit21d47832007-11-04 11:30:53 +0100452 struct option *o;
453
454 if (!sb.len)
455 continue;
456
457 ALLOC_GROW(opts, onb + 1, osz);
458 memset(opts + onb, 0, sizeof(opts[onb]));
459
460 o = &opts[onb++];
Brandon Casey33e75122017-09-17 15:28:16 -0700461 help = findspace(sb.buf);
462 if (!help || sb.buf == help) {
Pierre Habouzit21d47832007-11-04 11:30:53 +0100463 o->type = OPTION_GROUP;
Jay Soffiane1033432008-02-25 23:07:39 -0500464 o->help = xstrdup(skipspaces(sb.buf));
Pierre Habouzit21d47832007-11-04 11:30:53 +0100465 continue;
466 }
467
Brandon Casey28a8d0f2017-09-17 15:28:15 -0700468 *help = '\0';
469
Pierre Habouzit21d47832007-11-04 11:30:53 +0100470 o->type = OPTION_CALLBACK;
Brandon Casey28a8d0f2017-09-17 15:28:15 -0700471 o->help = xstrdup(skipspaces(help+1));
Pierre Habouzit21d47832007-11-04 11:30:53 +0100472 o->value = &parsed;
Pierre Habouzitff962a32008-03-02 09:21:38 +0100473 o->flags = PARSE_OPT_NOARG;
Pierre Habouzit21d47832007-11-04 11:30:53 +0100474 o->callback = &parseopt_dump;
Ilya Bobyr9bab5b62014-03-22 02:47:34 -0700475
Ilya Bobyr2d893df2015-07-14 01:17:44 -0700476 /* name(s) */
477 s = strpbrk(sb.buf, flag_chars);
478 if (s == NULL)
479 s = help;
Pierre Habouzit21d47832007-11-04 11:30:53 +0100480
481 if (s - sb.buf == 1) /* short option only */
482 o->short_name = *sb.buf;
483 else if (sb.buf[1] != ',') /* long option only */
484 o->long_name = xmemdupz(sb.buf, s - sb.buf);
485 else {
486 o->short_name = *sb.buf;
487 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
488 }
Ilya Bobyr2d893df2015-07-14 01:17:44 -0700489
490 /* flags */
491 while (s < help) {
492 switch (*s++) {
493 case '=':
494 o->flags &= ~PARSE_OPT_NOARG;
495 continue;
496 case '?':
497 o->flags &= ~PARSE_OPT_NOARG;
498 o->flags |= PARSE_OPT_OPTARG;
499 continue;
500 case '!':
501 o->flags |= PARSE_OPT_NONEG;
502 continue;
503 case '*':
504 o->flags |= PARSE_OPT_HIDDEN;
505 continue;
506 }
507 s--;
508 break;
509 }
510
511 if (s < help)
512 o->argh = xmemdupz(s, help - s);
Pierre Habouzit21d47832007-11-04 11:30:53 +0100513 }
514 strbuf_release(&sb);
515
516 /* put an OPT_END() */
517 ALLOC_GROW(opts, onb + 1, osz);
518 memset(opts + onb, 0, sizeof(opts[onb]));
Stephen Boyd37782922009-05-23 11:53:12 -0700519 argc = parse_options(argc, argv, prefix, opts, usage,
Uwe Kleine-König29981382010-07-06 16:46:05 +0200520 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
Junio C Hamanofcd91f82010-07-07 11:18:26 -0700521 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
Thomas Rast47e9cd22010-06-12 14:57:39 +0200522 PARSE_OPT_SHELL_EVAL);
Pierre Habouzit21d47832007-11-04 11:30:53 +0100523
René Scharfe02962d32016-07-30 19:36:23 +0200524 strbuf_addstr(&parsed, " --");
Jeff Kinge35f11c2018-01-15 17:59:43 +0700525 sq_quote_argv(&parsed, argv);
Pierre Habouzit21d47832007-11-04 11:30:53 +0100526 puts(parsed.buf);
527 return 0;
528}
529
Christian Couder50325372009-04-25 06:55:26 +0200530static int cmd_sq_quote(int argc, const char **argv)
531{
532 struct strbuf buf = STRBUF_INIT;
533
534 if (argc)
Jeff Kinge35f11c2018-01-15 17:59:43 +0700535 sq_quote_argv(&buf, argv);
Christian Couder50325372009-04-25 06:55:26 +0200536 printf("%s\n", buf.buf);
537 strbuf_release(&buf);
538
539 return 0;
540}
541
Christian Couderb1b35962008-04-26 13:57:23 +0200542static void die_no_single_rev(int quiet)
543{
544 if (quiet)
545 exit(1);
546 else
547 die("Needed a single revision");
548}
549
Jonathan Nieder7006b5b2009-11-09 09:04:54 -0600550static const char builtin_rev_parse_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700551N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
Nguyễn Thái Ngọc Duy2c7c1842012-08-20 19:32:40 +0700552 " or: git rev-parse --sq-quote [<arg>...]\n"
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700553 " or: git rev-parse [<options>] [<arg>...]\n"
Nguyễn Thái Ngọc Duy2c7c1842012-08-20 19:32:40 +0700554 "\n"
555 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
Jonathan Nieder7006b5b2009-11-09 09:04:54 -0600556
Jeff King9d16ca62017-03-15 16:06:53 -0400557/*
558 * Parse "opt" or "opt=<value>", setting value respectively to either
559 * NULL or the string after "=".
560 */
561static int opt_with_value(const char *arg, const char *opt, const char **value)
562{
563 if (skip_prefix(arg, opt, &arg)) {
564 if (!*arg) {
565 *value = NULL;
566 return 1;
567 }
568 if (*arg++ == '=') {
569 *value = arg;
570 return 1;
571 }
572 }
573 return 0;
574}
575
Jeff Kingffddfc62017-03-15 16:08:02 -0400576static void handle_ref_opt(const char *pattern, const char *prefix)
577{
578 if (pattern)
579 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
580 else
581 for_each_ref_in(prefix, show_reference, NULL);
582 clear_ref_exclusion(&ref_excludes);
583}
584
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700585int cmd_rev_parse(int argc, const char **argv, const char *prefix)
Linus Torvalds178cb242005-06-13 10:06:50 -0700586{
Christian Couderdfd1b742008-05-11 18:28:25 +0200587 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
Jeff Kingfc7d47f2016-02-29 06:01:56 -0500588 int did_repo_setup = 0;
Jeff King14185672013-12-06 17:05:48 -0500589 int has_dashdash = 0;
John Keeping12b9d322013-06-16 15:18:17 +0100590 int output_prefix = 0;
brian m. carlson8bc095f2017-05-01 02:29:02 +0000591 struct object_id oid;
David Aguilarc41a87d2014-09-18 20:45:37 -0700592 unsigned int flags = 0;
Christian Couderdfd1b742008-05-11 18:28:25 +0200593 const char *name = NULL;
David Aguilarc41a87d2014-09-18 20:45:37 -0700594 struct object_context unused;
Johannes Schindelin098aa862017-02-17 17:59:06 +0100595 struct strbuf buf = STRBUF_INIT;
brian m. carlson7e0d0292019-08-18 20:04:07 +0000596 const int hexsz = the_hash_algo->hexsz;
Seana62be772006-05-13 21:43:00 -0400597
Pierre Habouzit21d47832007-11-04 11:30:53 +0100598 if (argc > 1 && !strcmp("--parseopt", argv[1]))
599 return cmd_parseopt(argc - 1, argv + 1, prefix);
600
Christian Couder50325372009-04-25 06:55:26 +0200601 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
602 return cmd_sq_quote(argc - 2, argv + 2);
603
Jonathan Nieder7006b5b2009-11-09 09:04:54 -0600604 if (argc > 1 && !strcmp("-h", argv[1]))
605 usage(builtin_rev_parse_usage);
606
Jeff King14185672013-12-06 17:05:48 -0500607 for (i = 1; i < argc; i++) {
608 if (!strcmp(argv[i], "--")) {
609 has_dashdash = 1;
610 break;
611 }
612 }
613
Jeff Kingfc7d47f2016-02-29 06:01:56 -0500614 /* No options; just report on whether we're in a git repo or not. */
615 if (argc == 1) {
616 setup_git_directory();
617 git_config(git_default_config, NULL);
618 return 0;
619 }
620
Linus Torvalds178cb242005-06-13 10:06:50 -0700621 for (i = 1; i < argc; i++) {
Junio C Hamano16cee382006-06-05 22:36:21 -0700622 const char *arg = argv[i];
Linus Torvaldsfb18a2e2006-03-26 16:28:20 -0800623
Jeff Kingfc7d47f2016-02-29 06:01:56 -0500624 if (!strcmp(arg, "--local-env-vars")) {
625 int i;
626 for (i = 0; local_repo_env[i]; i++)
627 printf("%s\n", local_repo_env[i]);
628 continue;
629 }
630 if (!strcmp(arg, "--resolve-git-dir")) {
631 const char *gitdir = argv[++i];
632 if (!gitdir)
633 die("--resolve-git-dir requires an argument");
634 gitdir = resolve_gitdir(gitdir);
635 if (!gitdir)
636 die("not a gitdir '%s'", argv[i]);
637 puts(gitdir);
638 continue;
639 }
640
641 /* The rest of the options require a git repository. */
642 if (!did_repo_setup) {
643 prefix = setup_git_directory();
644 git_config(git_default_config, NULL);
645 did_repo_setup = 1;
646 }
647
Nguyễn Thái Ngọc Duy557bd832014-11-30 15:24:31 +0700648 if (!strcmp(arg, "--git-path")) {
649 if (!argv[i + 1])
650 die("--git-path requires an argument");
Johannes Schindelin098aa862017-02-17 17:59:06 +0100651 strbuf_reset(&buf);
652 puts(relative_path(git_path("%s", argv[i + 1]),
653 prefix, &buf));
Nguyễn Thái Ngọc Duy557bd832014-11-30 15:24:31 +0700654 i++;
655 continue;
656 }
Linus Torvalds178cb242005-06-13 10:06:50 -0700657 if (as_is) {
John Keeping12b9d322013-06-16 15:18:17 +0100658 if (show_file(arg, output_prefix) && as_is < 2)
Matthieu Moy023e37c2012-06-18 20:18:21 +0200659 verify_filename(prefix, arg, 0);
Linus Torvalds178cb242005-06-13 10:06:50 -0700660 continue;
661 }
Eric Wong3af06982006-01-29 16:26:40 -0800662 if (!strcmp(arg,"-n")) {
663 if (++i >= argc)
664 die("-n requires an argument");
665 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
666 show(arg);
667 show(argv[i]);
668 }
669 continue;
670 }
Christian Couder59556542013-11-30 21:55:40 +0100671 if (starts_with(arg, "-n")) {
Eric Wong3af06982006-01-29 16:26:40 -0800672 if ((filter & DO_FLAGS) && (filter & DO_REVS))
673 show(arg);
674 continue;
675 }
676
Linus Torvalds178cb242005-06-13 10:06:50 -0700677 if (*arg == '-') {
678 if (!strcmp(arg, "--")) {
Linus Torvaldsfb18a2e2006-03-26 16:28:20 -0800679 as_is = 2;
Linus Torvaldsa08b6502005-10-20 17:16:30 -0700680 /* Pass on the "--" if we show anything but files.. */
681 if (filter & (DO_FLAGS | DO_REVS))
John Keeping12b9d322013-06-16 15:18:17 +0100682 show_file(arg, 0);
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700683 continue;
Linus Torvalds178cb242005-06-13 10:06:50 -0700684 }
685 if (!strcmp(arg, "--default")) {
David Sharpa43219f2014-01-28 13:21:00 -0800686 def = argv[++i];
687 if (!def)
688 die("--default requires an argument");
Linus Torvalds178cb242005-06-13 10:06:50 -0700689 continue;
690 }
John Keeping12b9d322013-06-16 15:18:17 +0100691 if (!strcmp(arg, "--prefix")) {
David Sharpa43219f2014-01-28 13:21:00 -0800692 prefix = argv[++i];
693 if (!prefix)
694 die("--prefix requires an argument");
John Keeping12b9d322013-06-16 15:18:17 +0100695 startup_info->prefix = prefix;
696 output_prefix = 1;
John Keeping12b9d322013-06-16 15:18:17 +0100697 continue;
698 }
Linus Torvalds8ebb0182005-06-13 10:21:11 -0700699 if (!strcmp(arg, "--revs-only")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700700 filter &= ~DO_NOREV;
Linus Torvalds8ebb0182005-06-13 10:21:11 -0700701 continue;
702 }
703 if (!strcmp(arg, "--no-revs")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700704 filter &= ~DO_REVS;
Linus Torvalds8ebb0182005-06-13 10:21:11 -0700705 continue;
706 }
Linus Torvaldsf79b65a2005-07-06 10:08:08 -0700707 if (!strcmp(arg, "--flags")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700708 filter &= ~DO_NONFLAGS;
Linus Torvaldsf79b65a2005-07-06 10:08:08 -0700709 continue;
710 }
711 if (!strcmp(arg, "--no-flags")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700712 filter &= ~DO_FLAGS;
Linus Torvaldsf79b65a2005-07-06 10:08:08 -0700713 continue;
714 }
Linus Torvalds023d66e2005-06-24 10:12:55 -0700715 if (!strcmp(arg, "--verify")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700716 filter &= ~(DO_FLAGS|DO_NOREV);
717 verify = 1;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700718 continue;
Linus Torvalds921d8652005-06-13 11:14:20 -0700719 }
Christian Couderb1b35962008-04-26 13:57:23 +0200720 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
721 quiet = 1;
brian m. carlson321c89b2017-07-13 23:49:29 +0000722 flags |= GET_OID_QUIETLY;
Christian Couderb1b35962008-04-26 13:57:23 +0200723 continue;
724 }
Jeff King9d16ca62017-03-15 16:06:53 -0400725 if (opt_with_value(arg, "--short", &arg)) {
Junio C Hamanod5012502006-01-25 01:35:38 -0800726 filter &= ~(DO_FLAGS|DO_NOREV);
727 verify = 1;
728 abbrev = DEFAULT_ABBREV;
Jeff King9d16ca62017-03-15 16:06:53 -0400729 if (!arg)
Junio C Hamano7b5b7722016-09-30 17:19:36 -0700730 continue;
Jeff King9d16ca62017-03-15 16:06:53 -0400731 abbrev = strtoul(arg, NULL, 10);
Junio C Hamano1dc4fb82006-01-26 00:48:19 -0800732 if (abbrev < MINIMUM_ABBREV)
733 abbrev = MINIMUM_ABBREV;
brian m. carlson7e0d0292019-08-18 20:04:07 +0000734 else if (hexsz <= abbrev)
735 abbrev = hexsz;
Junio C Hamanod5012502006-01-25 01:35:38 -0800736 continue;
737 }
Junio C Hamano5bb2c652005-07-22 19:08:32 -0700738 if (!strcmp(arg, "--sq")) {
739 output_sq = 1;
740 continue;
741 }
Linus Torvalds042a4ed2005-06-26 11:34:30 -0700742 if (!strcmp(arg, "--not")) {
743 show_type ^= REVERSED;
744 continue;
745 }
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700746 if (!strcmp(arg, "--symbolic")) {
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800747 symbolic = SHOW_SYMBOLIC_ASIS;
748 continue;
749 }
750 if (!strcmp(arg, "--symbolic-full-name")) {
751 symbolic = SHOW_SYMBOLIC_FULL;
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700752 continue;
753 }
Jeff King9d16ca62017-03-15 16:06:53 -0400754 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
Bert Wesarga45d3462009-04-13 13:20:26 +0200755 abbrev_ref = 1;
756 abbrev_ref_strict = warn_ambiguous_refs;
Jeff King9d16ca62017-03-15 16:06:53 -0400757 if (arg) {
758 if (!strcmp(arg, "strict"))
Bert Wesarga45d3462009-04-13 13:20:26 +0200759 abbrev_ref_strict = 1;
Jeff King9d16ca62017-03-15 16:06:53 -0400760 else if (!strcmp(arg, "loose"))
Bert Wesarga45d3462009-04-13 13:20:26 +0200761 abbrev_ref_strict = 0;
762 else
Jeff King9d16ca62017-03-15 16:06:53 -0400763 die("unknown mode for --abbrev-ref: %s",
764 arg);
Bert Wesarga45d3462009-04-13 13:20:26 +0200765 }
766 continue;
767 }
Linus Torvalds960bba02005-07-03 13:07:52 -0700768 if (!strcmp(arg, "--all")) {
Michael Haggertye23b0362015-05-25 18:38:29 +0000769 for_each_ref(show_reference, NULL);
Andreas Gruenbacher52210482018-10-23 21:17:58 +0200770 clear_ref_exclusion(&ref_excludes);
Linus Torvalds960bba02005-07-03 13:07:52 -0700771 continue;
772 }
Jeff Kingef87cc72017-03-15 16:05:36 -0400773 if (skip_prefix(arg, "--disambiguate=", &arg)) {
774 for_each_abbrev(arg, show_abbrev, NULL);
Junio C Hamano957d7402012-07-03 14:21:59 -0700775 continue;
776 }
Linus Torvaldsad3f9a72009-10-27 11:28:07 -0700777 if (!strcmp(arg, "--bisect")) {
Jeff King1d0538e2017-09-06 07:53:10 -0400778 for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
779 for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
Linus Torvaldsad3f9a72009-10-27 11:28:07 -0700780 continue;
781 }
Jeff Kingffddfc62017-03-15 16:08:02 -0400782 if (opt_with_value(arg, "--branches", &arg)) {
783 handle_ref_opt(arg, "refs/heads/");
Ilari Liusvaarab09fe972010-01-20 11:48:26 +0200784 continue;
785 }
Jeff Kingffddfc62017-03-15 16:08:02 -0400786 if (opt_with_value(arg, "--tags", &arg)) {
787 handle_ref_opt(arg, "refs/tags/");
Seana62be772006-05-13 21:43:00 -0400788 continue;
789 }
Jeff Kingef87cc72017-03-15 16:05:36 -0400790 if (skip_prefix(arg, "--glob=", &arg)) {
Jeff Kingffddfc62017-03-15 16:08:02 -0400791 handle_ref_opt(arg, NULL);
Ilari Liusvaarab09fe972010-01-20 11:48:26 +0200792 continue;
793 }
Jeff Kingffddfc62017-03-15 16:08:02 -0400794 if (opt_with_value(arg, "--remotes", &arg)) {
795 handle_ref_opt(arg, "refs/remotes/");
Seana62be772006-05-13 21:43:00 -0400796 continue;
797 }
Jeff Kingef87cc72017-03-15 16:05:36 -0400798 if (skip_prefix(arg, "--exclude=", &arg)) {
799 add_ref_exclusion(&ref_excludes, arg);
Seana62be772006-05-13 21:43:00 -0400800 continue;
801 }
Steven Drake7cceca52010-01-12 11:33:48 +1300802 if (!strcmp(arg, "--show-toplevel")) {
803 const char *work_tree = get_git_work_tree();
804 if (work_tree)
805 puts(work_tree);
Jeff King2d92ab32019-11-19 03:05:43 -0500806 else
807 die("this operation must be run in a work tree");
Steven Drake7cceca52010-01-12 11:33:48 +1300808 continue;
809 }
Stefan Bellerbf0231c2017-03-08 15:07:42 -0800810 if (!strcmp(arg, "--show-superproject-working-tree")) {
811 const char *superproject = get_superproject_working_tree();
812 if (superproject)
813 puts(superproject);
814 continue;
815 }
Linus Torvaldsd288a702005-08-16 18:06:34 -0700816 if (!strcmp(arg, "--show-prefix")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700817 if (prefix)
818 puts(prefix);
Ross Lagerwall658219f2012-04-09 15:27:56 +0200819 else
820 putchar('\n');
Linus Torvaldsd288a702005-08-16 18:06:34 -0700821 continue;
822 }
Junio C Hamano5f94c732005-12-22 22:35:38 -0800823 if (!strcmp(arg, "--show-cdup")) {
824 const char *pfx = prefix;
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100825 if (!is_inside_work_tree()) {
826 const char *work_tree =
827 get_git_work_tree();
828 if (work_tree)
829 printf("%s\n", work_tree);
830 continue;
831 }
Junio C Hamano5f94c732005-12-22 22:35:38 -0800832 while (pfx) {
833 pfx = strchr(pfx, '/');
834 if (pfx) {
835 pfx++;
836 printf("../");
837 }
838 }
839 putchar('\n');
840 continue;
841 }
SZEDER Gábora2f5a872017-02-03 03:48:23 +0100842 if (!strcmp(arg, "--git-dir") ||
843 !strcmp(arg, "--absolute-git-dir")) {
Linus Torvaldsa8783ee2005-09-18 11:18:30 -0700844 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
René Scharfe56b9f6e2014-07-28 20:30:39 +0200845 char *cwd;
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700846 int len;
SZEDER Gábora2f5a872017-02-03 03:48:23 +0100847 if (arg[2] == 'g') { /* --git-dir */
848 if (gitdir) {
849 puts(gitdir);
850 continue;
851 }
852 if (!prefix) {
853 puts(".git");
854 continue;
855 }
856 } else { /* --absolute-git-dir */
857 if (!gitdir && !prefix)
858 gitdir = ".git";
859 if (gitdir) {
860 puts(real_path(gitdir));
861 continue;
862 }
Linus Torvaldsa8783ee2005-09-18 11:18:30 -0700863 }
René Scharfe56b9f6e2014-07-28 20:30:39 +0200864 cwd = xgetcwd();
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700865 len = strlen(cwd);
866 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
René Scharfe56b9f6e2014-07-28 20:30:39 +0200867 free(cwd);
Linus Torvaldsa8783ee2005-09-18 11:18:30 -0700868 continue;
869 }
Nguyễn Thái Ngọc Duy31e26eb2014-11-30 15:24:44 +0700870 if (!strcmp(arg, "--git-common-dir")) {
Johannes Schindelin098aa862017-02-17 17:59:06 +0100871 strbuf_reset(&buf);
872 puts(relative_path(get_git_common_dir(),
873 prefix, &buf));
Nguyễn Thái Ngọc Duy31e26eb2014-11-30 15:24:44 +0700874 continue;
875 }
Johannes Schindelin6d9ba672007-01-23 13:30:20 +0100876 if (!strcmp(arg, "--is-inside-git-dir")) {
877 printf("%s\n", is_inside_git_dir() ? "true"
878 : "false");
879 continue;
880 }
Matthias Lederhofer892c41b2007-06-06 09:10:42 +0200881 if (!strcmp(arg, "--is-inside-work-tree")) {
882 printf("%s\n", is_inside_work_tree() ? "true"
883 : "false");
884 continue;
885 }
Matthias Lederhofer493c7742007-06-03 16:46:36 +0200886 if (!strcmp(arg, "--is-bare-repository")) {
887 printf("%s\n", is_bare_repository() ? "true"
888 : "false");
889 continue;
890 }
Øystein Walle417abfd2017-09-18 19:04:29 +0200891 if (!strcmp(arg, "--is-shallow-repository")) {
Stefan Bellerc8813482018-05-17 15:51:46 -0700892 printf("%s\n",
893 is_repository_shallow(the_repository) ? "true"
Øystein Walle417abfd2017-09-18 19:04:29 +0200894 : "false");
895 continue;
896 }
Nguyễn Thái Ngọc Duya76295d2014-06-13 19:19:46 +0700897 if (!strcmp(arg, "--shared-index-path")) {
898 if (read_cache() < 0)
899 die(_("Could not read the index"));
900 if (the_index.split_index) {
brian m. carlson2182abd2018-05-02 00:25:43 +0000901 const struct object_id *oid = &the_index.split_index->base_oid;
902 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
Johannes Schindelin098aa862017-02-17 17:59:06 +0100903 strbuf_reset(&buf);
904 puts(relative_path(path, prefix, &buf));
Nguyễn Thái Ngọc Duya76295d2014-06-13 19:19:46 +0700905 }
906 continue;
907 }
Jeff Kingef87cc72017-03-15 16:05:36 -0400908 if (skip_prefix(arg, "--since=", &arg)) {
909 show_datestring("--max-age=", arg);
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700910 continue;
911 }
Jeff Kingef87cc72017-03-15 16:05:36 -0400912 if (skip_prefix(arg, "--after=", &arg)) {
913 show_datestring("--max-age=", arg);
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700914 continue;
915 }
Jeff Kingef87cc72017-03-15 16:05:36 -0400916 if (skip_prefix(arg, "--before=", &arg)) {
917 show_datestring("--min-age=", arg);
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700918 continue;
919 }
Jeff Kingef87cc72017-03-15 16:05:36 -0400920 if (skip_prefix(arg, "--until=", &arg)) {
921 show_datestring("--min-age=", arg);
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700922 continue;
923 }
brian m. carlson2eabd382019-10-28 00:58:55 +0000924 if (opt_with_value(arg, "--show-object-format", &arg)) {
925 const char *val = arg ? arg : "storage";
926
927 if (strcmp(val, "storage") &&
928 strcmp(val, "input") &&
929 strcmp(val, "output"))
930 die("unknown mode for --show-object-format: %s",
931 arg);
932 puts(the_hash_algo->name);
933 continue;
934 }
Linus Torvalds9523a4c2006-02-05 11:58:34 -0800935 if (show_flag(arg) && verify)
Christian Couderb1b35962008-04-26 13:57:23 +0200936 die_no_single_rev(quiet);
Linus Torvalds178cb242005-06-13 10:06:50 -0700937 continue;
938 }
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700939
940 /* Not a flag argument */
Santi Béjar3dd4e732006-07-04 11:02:22 +0200941 if (try_difference(arg))
942 continue;
Björn Steinbrink2122f8b2008-07-26 18:37:56 +0200943 if (try_parent_shorthands(arg))
944 continue;
Christian Couderdfd1b742008-05-11 18:28:25 +0200945 name = arg;
946 type = NORMAL;
947 if (*arg == '^') {
948 name++;
949 type = REVERSED;
Linus Torvalds800644c2005-06-20 08:29:13 -0700950 }
Nguyễn Thái Ngọc Duy3a7a6982019-01-12 09:13:28 +0700951 if (!get_oid_with_context(the_repository, name,
952 flags, &oid, &unused)) {
Christian Couderdfd1b742008-05-11 18:28:25 +0200953 if (verify)
954 revs_count++;
955 else
brian m. carlson8bc095f2017-05-01 02:29:02 +0000956 show_rev(type, &oid, name);
Linus Torvalds800644c2005-06-20 08:29:13 -0700957 continue;
958 }
Christian Couder75ecfce2008-04-26 15:19:29 +0200959 if (verify)
960 die_no_single_rev(quiet);
Jeff King14185672013-12-06 17:05:48 -0500961 if (has_dashdash)
962 die("bad revision '%s'", arg);
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800963 as_is = 1;
John Keeping12b9d322013-06-16 15:18:17 +0100964 if (!show_file(arg, output_prefix))
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800965 continue;
Matthieu Moy023e37c2012-06-18 20:18:21 +0200966 verify_filename(prefix, arg, 1);
Linus Torvalds178cb242005-06-13 10:06:50 -0700967 }
Johannes Schindelin098aa862017-02-17 17:59:06 +0100968 strbuf_release(&buf);
Christian Couderdfd1b742008-05-11 18:28:25 +0200969 if (verify) {
970 if (revs_count == 1) {
brian m. carlson8bc095f2017-05-01 02:29:02 +0000971 show_rev(type, &oid, name);
Christian Couderdfd1b742008-05-11 18:28:25 +0200972 return 0;
973 } else if (revs_count == 0 && show_default())
974 return 0;
Christian Couderb1b35962008-04-26 13:57:23 +0200975 die_no_single_rev(quiet);
Christian Couderdfd1b742008-05-11 18:28:25 +0200976 } else
977 show_default();
Linus Torvalds178cb242005-06-13 10:06:50 -0700978 return 0;
979}