Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rev-parse.c |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 6 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 7 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Linus Torvalds | a8be83f | 2005-06-20 20:28:09 -0700 | [diff] [blame] | 9 | #include "commit.h" |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 10 | #include "refs.h" |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 11 | #include "quote.h" |
Christian Couder | 895f10c | 2006-06-03 18:45:43 +0200 | [diff] [blame] | 12 | #include "builtin.h" |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 13 | #include "parse-options.h" |
Junio C Hamano | 9dc01bf | 2013-11-01 12:13:01 -0700 | [diff] [blame] | 14 | #include "diff.h" |
| 15 | #include "revision.h" |
Nguyễn Thái Ngọc Duy | a76295d | 2014-06-13 19:19:46 +0700 | [diff] [blame] | 16 | #include "split-index.h" |
Stefan Beller | bf0231c | 2017-03-08 15:07:42 -0800 | [diff] [blame] | 17 | #include "submodule.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 18 | #include "commit-reach.h" |
Linus Torvalds | a8be83f | 2005-06-20 20:28:09 -0700 | [diff] [blame] | 19 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 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 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 26 | static const char *def; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 042a4ed | 2005-06-26 11:34:30 -0700 | [diff] [blame] | 28 | #define NORMAL 0 |
| 29 | #define REVERSED 1 |
| 30 | static int show_type = NORMAL; |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 31 | |
| 32 | #define SHOW_SYMBOLIC_ASIS 1 |
| 33 | #define SHOW_SYMBOLIC_FULL 2 |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 34 | static int symbolic; |
| 35 | static int abbrev; |
Bert Wesarg | a45d346 | 2009-04-13 13:20:26 +0200 | [diff] [blame] | 36 | static int abbrev_ref; |
| 37 | static int abbrev_ref_strict; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 38 | static int output_sq; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 39 | |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 40 | static int stuck_long; |
Junio C Hamano | 9dc01bf | 2013-11-01 12:13:01 -0700 | [diff] [blame] | 41 | static struct string_list *ref_excludes; |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 42 | |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Some arguments are relevant "revision" arguments, |
| 45 | * others are about output format or other details. |
| 46 | * This sorts it all out. |
| 47 | */ |
| 48 | static int is_rev_argument(const char *arg) |
| 49 | { |
| 50 | static const char *rev_args[] = { |
Junio C Hamano | e091eb9 | 2005-10-05 14:49:54 -0700 | [diff] [blame] | 51 | "--all", |
Junio C Hamano | 5ccfb75 | 2005-08-08 19:31:37 -0700 | [diff] [blame] | 52 | "--bisect", |
Junio C Hamano | 5a83f3b | 2005-10-30 01:08:35 -0800 | [diff] [blame] | 53 | "--dense", |
Ilari Liusvaara | b09fe97 | 2010-01-20 11:48:26 +0200 | [diff] [blame] | 54 | "--branches=", |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 55 | "--branches", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 56 | "--header", |
Junio C Hamano | cc243c3 | 2011-05-18 18:08:09 -0700 | [diff] [blame] | 57 | "--ignore-missing", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 58 | "--max-age=", |
| 59 | "--max-count=", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 60 | "--min-age=", |
Junio C Hamano | 5ccfb75 | 2005-08-08 19:31:37 -0700 | [diff] [blame] | 61 | "--no-merges", |
Michael J Gruber | ad5aeed | 2011-03-21 11:14:06 +0100 | [diff] [blame] | 62 | "--min-parents=", |
| 63 | "--no-min-parents", |
| 64 | "--max-parents=", |
| 65 | "--no-max-parents", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 66 | "--objects", |
Junio C Hamano | c649657 | 2006-02-19 03:32:31 -0800 | [diff] [blame] | 67 | "--objects-edge", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 68 | "--parents", |
| 69 | "--pretty", |
Ilari Liusvaara | b09fe97 | 2010-01-20 11:48:26 +0200 | [diff] [blame] | 70 | "--remotes=", |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 71 | "--remotes", |
Ilari Liusvaara | d08bae7 | 2010-01-20 11:48:25 +0200 | [diff] [blame] | 72 | "--glob=", |
Junio C Hamano | 5a83f3b | 2005-10-30 01:08:35 -0800 | [diff] [blame] | 73 | "--sparse", |
Ilari Liusvaara | b09fe97 | 2010-01-20 11:48:26 +0200 | [diff] [blame] | 74 | "--tags=", |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 75 | "--tags", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 76 | "--topo-order", |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 77 | "--date-order", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 78 | "--unpacked", |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 79 | NULL |
| 80 | }; |
| 81 | const char **p = rev_args; |
| 82 | |
Eric Wong | 8233340 | 2006-01-29 16:28:02 -0800 | [diff] [blame] | 83 | /* accept -<digit>, like traditional "head" */ |
| 84 | if ((*arg == '-') && isdigit(arg[1])) |
| 85 | return 1; |
| 86 | |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 87 | for (;;) { |
| 88 | const char *str = *p++; |
| 89 | int len; |
| 90 | if (!str) |
| 91 | return 0; |
| 92 | len = strlen(str); |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 93 | if (!strcmp(arg, str) || |
| 94 | (str[len-1] == '=' && !strncmp(arg, str, len))) |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | } |
| 98 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 99 | /* Output argument as a string, either SQ or normal */ |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 100 | static 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 Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 118 | /* Like show(), but with a negation prefix according to type */ |
| 119 | static void show_with_type(int type, const char *arg) |
| 120 | { |
| 121 | if (type != show_type) |
| 122 | putchar('^'); |
| 123 | show(arg); |
| 124 | } |
| 125 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 126 | /* Output a revision, only if filter allows it */ |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 127 | static void show_rev(int type, const struct object_id *oid, const char *name) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 128 | { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 129 | if (!(filter & DO_REVS)) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 130 | return; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 131 | def = NULL; |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 132 | |
Bert Wesarg | a45d346 | 2009-04-13 13:20:26 +0200 | [diff] [blame] | 133 | if ((symbolic || abbrev_ref) && name) { |
| 134 | if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) { |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 135 | struct object_id discard; |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 136 | char *full; |
| 137 | |
brian m. carlson | cca5fa6 | 2017-10-15 22:06:57 +0000 | [diff] [blame] | 138 | switch (dwim_ref(name, strlen(name), &discard, &full)) { |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 139 | 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 Wesarg | a45d346 | 2009-04-13 13:20:26 +0200 | [diff] [blame] | 149 | if (abbrev_ref) |
| 150 | full = shorten_unambiguous_ref(full, |
| 151 | abbrev_ref_strict); |
Johannes Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 152 | show_with_type(type, full); |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 153 | break; |
| 154 | default: /* ambiguous */ |
| 155 | error("refname '%s' is ambiguous", name); |
| 156 | break; |
| 157 | } |
Jeff King | 28b3563 | 2014-07-24 00:41:11 -0400 | [diff] [blame] | 158 | free(full); |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 159 | } else { |
Johannes Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 160 | show_with_type(type, name); |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
Junio C Hamano | d501250 | 2006-01-25 01:35:38 -0800 | [diff] [blame] | 163 | else if (abbrev) |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 164 | show_with_type(type, find_unique_abbrev(oid, abbrev)); |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 165 | else |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 166 | show_with_type(type, oid_to_hex(oid)); |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 169 | /* Output a flag, only if filter allows it. */ |
Junio C Hamano | 16cee38 | 2006-06-05 22:36:21 -0700 | [diff] [blame] | 170 | static int show_flag(const char *arg) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 171 | { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 172 | if (!(filter & DO_FLAGS)) |
Linus Torvalds | 9523a4c | 2006-02-05 11:58:34 -0800 | [diff] [blame] | 173 | return 0; |
| 174 | if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { |
Linus Torvalds | 0360e99 | 2005-08-23 10:47:54 -0700 | [diff] [blame] | 175 | show(arg); |
Linus Torvalds | 9523a4c | 2006-02-05 11:58:34 -0800 | [diff] [blame] | 176 | return 1; |
| 177 | } |
| 178 | return 0; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 181 | static int show_default(void) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 182 | { |
Junio C Hamano | 16cee38 | 2006-06-05 22:36:21 -0700 | [diff] [blame] | 183 | const char *s = def; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 184 | |
| 185 | if (s) { |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 186 | struct object_id oid; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 187 | |
| 188 | def = NULL; |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 189 | if (!get_oid(s, &oid)) { |
| 190 | show_rev(NORMAL, &oid, s); |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 191 | return 1; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 192 | } |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 193 | } |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 194 | return 0; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Michael Haggerty | e23b036 | 2015-05-25 18:38:29 +0000 | [diff] [blame] | 197 | static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 198 | { |
Junio C Hamano | 9dc01bf | 2013-11-01 12:13:01 -0700 | [diff] [blame] | 199 | if (ref_excluded(ref_excludes, refname)) |
| 200 | return 0; |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 201 | show_rev(NORMAL, oid, refname); |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
Michael Haggerty | e23b036 | 2015-05-25 18:38:29 +0000 | [diff] [blame] | 205 | static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 206 | { |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 207 | show_rev(REVERSED, oid, refname); |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
brian m. carlson | 1b7ba79 | 2017-03-31 01:39:59 +0000 | [diff] [blame] | 211 | static int show_abbrev(const struct object_id *oid, void *cb_data) |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 212 | { |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 213 | show_rev(NORMAL, oid, NULL); |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 217 | static void show_datestring(const char *flag, const char *datestr) |
| 218 | { |
Jeff King | 5b1ef2c | 2017-03-28 15:46:50 -0400 | [diff] [blame] | 219 | char *buffer; |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 220 | |
| 221 | /* date handling requires both flags and revs */ |
| 222 | if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) |
| 223 | return; |
Johannes Schindelin | cb71f8b | 2017-04-21 12:45:48 +0200 | [diff] [blame] | 224 | buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr)); |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 225 | show(buffer); |
Jeff King | 5b1ef2c | 2017-03-28 15:46:50 -0400 | [diff] [blame] | 226 | free(buffer); |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 227 | } |
| 228 | |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 229 | static int show_file(const char *arg, int output_prefix) |
Linus Torvalds | 7a3dd47 | 2005-10-18 00:16:45 -0700 | [diff] [blame] | 230 | { |
Linus Torvalds | 7b34c2f | 2005-10-25 15:24:55 -0700 | [diff] [blame] | 231 | show_default(); |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 232 | if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 233 | if (output_prefix) { |
| 234 | const char *prefix = startup_info->prefix; |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 235 | char *fname = prefix_filename(prefix, arg); |
| 236 | show(fname); |
| 237 | free(fname); |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 238 | } else |
| 239 | show(arg); |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 240 | return 1; |
| 241 | } |
| 242 | return 0; |
Linus Torvalds | 7a3dd47 | 2005-10-18 00:16:45 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Junio C Hamano | b7d936b | 2006-07-06 00:16:35 -0700 | [diff] [blame] | 245 | static int try_difference(const char *arg) |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 246 | { |
| 247 | char *dotdot; |
Brandon Williams | ae90bdc | 2018-02-14 10:59:27 -0800 | [diff] [blame] | 248 | struct object_id start_oid; |
| 249 | struct object_id end_oid; |
| 250 | const char *end; |
| 251 | const char *start; |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 252 | int symmetric; |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 253 | static const char head_by_default[] = "HEAD"; |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 254 | |
| 255 | if (!(dotdot = strstr(arg, ".."))) |
| 256 | return 0; |
Brandon Williams | ae90bdc | 2018-02-14 10:59:27 -0800 | [diff] [blame] | 257 | end = dotdot + 2; |
| 258 | start = arg; |
| 259 | symmetric = (*end == '.'); |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 260 | |
| 261 | *dotdot = 0; |
Brandon Williams | ae90bdc | 2018-02-14 10:59:27 -0800 | [diff] [blame] | 262 | end += symmetric; |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 263 | |
Brandon Williams | ae90bdc | 2018-02-14 10:59:27 -0800 | [diff] [blame] | 264 | if (!*end) |
| 265 | end = head_by_default; |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 266 | if (dotdot == arg) |
Brandon Williams | ae90bdc | 2018-02-14 10:59:27 -0800 | [diff] [blame] | 267 | start = head_by_default; |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 268 | |
Brandon Williams | ae90bdc | 2018-02-14 10:59:27 -0800 | [diff] [blame] | 269 | if (start == head_by_default && end == head_by_default && |
Junio C Hamano | 003c84f | 2011-05-02 13:39:16 -0700 | [diff] [blame] | 270 | !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 Williams | ae90bdc | 2018-02-14 10:59:27 -0800 | [diff] [blame] | 279 | 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éjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 282 | if (symmetric) { |
| 283 | struct commit_list *exclude; |
| 284 | struct commit *a, *b; |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 285 | a = lookup_commit_reference(the_repository, &start_oid); |
| 286 | b = lookup_commit_reference(the_repository, &end_oid); |
Elijah Newren | 0ed556d | 2018-05-23 23:27:33 -0700 | [diff] [blame] | 287 | if (!a || !b) { |
| 288 | *dotdot = '.'; |
| 289 | return 0; |
| 290 | } |
Junio C Hamano | 2ce406c | 2014-10-30 12:20:44 -0700 | [diff] [blame] | 291 | exclude = get_merge_bases(a, b); |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 292 | while (exclude) { |
René Scharfe | e510ab8 | 2015-10-24 18:21:31 +0200 | [diff] [blame] | 293 | struct commit *commit = pop_commit(&exclude); |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 294 | show_rev(REVERSED, &commit->object.oid, NULL); |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 295 | } |
| 296 | } |
Jeff King | 62f162f | 2013-12-06 17:07:52 -0500 | [diff] [blame] | 297 | *dotdot = '.'; |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 298 | return 1; |
| 299 | } |
| 300 | *dotdot = '.'; |
| 301 | return 0; |
| 302 | } |
| 303 | |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 304 | static int try_parent_shorthands(const char *arg) |
| 305 | { |
| 306 | char *dotdot; |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 307 | struct object_id oid; |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 308 | struct commit *commit; |
| 309 | struct commit_list *parents; |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 310 | int parent_number; |
| 311 | int include_rev = 0; |
| 312 | int include_parents = 0; |
| 313 | int exclude_parent = 0; |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 314 | |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 315 | 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 Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 326 | |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 327 | 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 Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 334 | return 0; |
| 335 | |
| 336 | *dotdot = 0; |
Elijah Newren | 0ed556d | 2018-05-23 23:27:33 -0700 | [diff] [blame] | 337 | if (get_oid_committish(arg, &oid) || |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 338 | !(commit = lookup_commit_reference(the_repository, &oid))) { |
Jeff King | 62f162f | 2013-12-06 17:07:52 -0500 | [diff] [blame] | 339 | *dotdot = '^'; |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 340 | return 0; |
Jeff King | 62f162f | 2013-12-06 17:07:52 -0500 | [diff] [blame] | 341 | } |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 342 | |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 343 | if (exclude_parent && |
| 344 | exclude_parent > commit_list_count(commit->parents)) { |
| 345 | *dotdot = '^'; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | if (include_rev) |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 350 | show_rev(NORMAL, &oid, arg); |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 351 | for (parents = commit->parents, parent_number = 1; |
| 352 | parents; |
| 353 | parents = parents->next, parent_number++) { |
Jeff King | a2e7b04 | 2016-11-16 00:46:26 -0800 | [diff] [blame] | 354 | char *name = NULL; |
| 355 | |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 356 | if (exclude_parent && parent_number != exclude_parent) |
| 357 | continue; |
| 358 | |
Jeff King | a2e7b04 | 2016-11-16 00:46:26 -0800 | [diff] [blame] | 359 | if (symbolic) |
| 360 | name = xstrfmt("%s^%d", arg, parent_number); |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 361 | show_rev(include_parents ? NORMAL : REVERSED, |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 362 | &parents->item->object.oid, name); |
Jeff King | a2e7b04 | 2016-11-16 00:46:26 -0800 | [diff] [blame] | 363 | free(name); |
Vegard Nossum | 8779351 | 2016-09-27 10:32:49 +0200 | [diff] [blame] | 364 | } |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 365 | |
Jeff King | 62f162f | 2013-12-06 17:07:52 -0500 | [diff] [blame] | 366 | *dotdot = '^'; |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 367 | return 1; |
| 368 | } |
| 369 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 370 | static 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 Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 375 | else if (o->short_name && (o->long_name == NULL || !stuck_long)) |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 376 | strbuf_addf(parsed, " -%c", o->short_name); |
| 377 | else |
| 378 | strbuf_addf(parsed, " --%s", o->long_name); |
| 379 | if (arg) { |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 380 | if (!stuck_long) |
| 381 | strbuf_addch(parsed, ' '); |
| 382 | else if (o->long_name) |
| 383 | strbuf_addch(parsed, '='); |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 384 | sq_quote_buf(parsed, arg); |
| 385 | } |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static const char *skipspaces(const char *s) |
| 390 | { |
| 391 | while (isspace(*s)) |
| 392 | s++; |
| 393 | return s; |
| 394 | } |
| 395 | |
Brandon Casey | 33e7512 | 2017-09-17 15:28:16 -0700 | [diff] [blame] | 396 | static char *findspace(const char *s) |
| 397 | { |
| 398 | for (; *s; s++) |
| 399 | if (isspace(*s)) |
| 400 | return (char*)s; |
| 401 | return NULL; |
| 402 | } |
| 403 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 404 | static int cmd_parseopt(int argc, const char **argv, const char *prefix) |
| 405 | { |
Uwe Kleine-König | 6e0800e | 2009-06-14 01:58:43 +0200 | [diff] [blame] | 406 | static int keep_dashdash = 0, stop_at_non_option = 0; |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 407 | static char const * const parseopt_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 408 | N_("git rev-parse --parseopt [<options>] -- [<args>...]"), |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 409 | NULL |
| 410 | }; |
| 411 | static struct option parseopt_opts[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 412 | OPT_BOOL(0, "keep-dashdash", &keep_dashdash, |
Nguyễn Thái Ngọc Duy | 2c7c184 | 2012-08-20 19:32:40 +0700 | [diff] [blame] | 413 | N_("keep the `--` passed as an arg")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 414 | OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option, |
Nguyễn Thái Ngọc Duy | 2c7c184 | 2012-08-20 19:32:40 +0700 | [diff] [blame] | 415 | N_("stop parsing after the " |
| 416 | "first non-option argument")), |
Nicolas Vigier | f8c8721 | 2013-10-31 12:08:29 +0100 | [diff] [blame] | 417 | OPT_BOOL(0, "stuck-long", &stuck_long, |
| 418 | N_("output in stuck long form")), |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 419 | OPT_END(), |
| 420 | }; |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 421 | static const char * const flag_chars = "*=?!"; |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 422 | |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 423 | struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 424 | const char **usage = NULL; |
| 425 | struct option *opts = NULL; |
| 426 | int onb = 0, osz = 0, unb = 0, usz = 0; |
| 427 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 428 | strbuf_addstr(&parsed, "set --"); |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 429 | argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 430 | PARSE_OPT_KEEP_DASHDASH); |
| 431 | if (argc < 1 || strcmp(argv[0], "--")) |
| 432 | usage_with_options(parseopt_usage, parseopt_opts); |
| 433 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 434 | /* get the usage up to the first line with a -- on it */ |
| 435 | for (;;) { |
Junio C Hamano | 72e37b6 | 2015-10-28 13:59:44 -0700 | [diff] [blame] | 436 | if (strbuf_getline(&sb, stdin) == EOF) |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 437 | 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 Bobyr | 9bab5b6 | 2014-03-22 02:47:34 -0700 | [diff] [blame] | 448 | /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */ |
Junio C Hamano | 72e37b6 | 2015-10-28 13:59:44 -0700 | [diff] [blame] | 449 | while (strbuf_getline(&sb, stdin) != EOF) { |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 450 | const char *s; |
Brandon Casey | 28a8d0f | 2017-09-17 15:28:15 -0700 | [diff] [blame] | 451 | char *help; |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 452 | 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 Casey | 33e7512 | 2017-09-17 15:28:16 -0700 | [diff] [blame] | 461 | help = findspace(sb.buf); |
| 462 | if (!help || sb.buf == help) { |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 463 | o->type = OPTION_GROUP; |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 464 | o->help = xstrdup(skipspaces(sb.buf)); |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | |
Brandon Casey | 28a8d0f | 2017-09-17 15:28:15 -0700 | [diff] [blame] | 468 | *help = '\0'; |
| 469 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 470 | o->type = OPTION_CALLBACK; |
Brandon Casey | 28a8d0f | 2017-09-17 15:28:15 -0700 | [diff] [blame] | 471 | o->help = xstrdup(skipspaces(help+1)); |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 472 | o->value = &parsed; |
Pierre Habouzit | ff962a3 | 2008-03-02 09:21:38 +0100 | [diff] [blame] | 473 | o->flags = PARSE_OPT_NOARG; |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 474 | o->callback = &parseopt_dump; |
Ilya Bobyr | 9bab5b6 | 2014-03-22 02:47:34 -0700 | [diff] [blame] | 475 | |
Ilya Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 476 | /* name(s) */ |
| 477 | s = strpbrk(sb.buf, flag_chars); |
| 478 | if (s == NULL) |
| 479 | s = help; |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 480 | |
| 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 Bobyr | 2d893df | 2015-07-14 01:17:44 -0700 | [diff] [blame] | 489 | |
| 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 Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 513 | } |
| 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 Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 519 | argc = parse_options(argc, argv, prefix, opts, usage, |
Uwe Kleine-König | 2998138 | 2010-07-06 16:46:05 +0200 | [diff] [blame] | 520 | (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) | |
Junio C Hamano | fcd91f8 | 2010-07-07 11:18:26 -0700 | [diff] [blame] | 521 | (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) | |
Thomas Rast | 47e9cd2 | 2010-06-12 14:57:39 +0200 | [diff] [blame] | 522 | PARSE_OPT_SHELL_EVAL); |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 523 | |
René Scharfe | 02962d3 | 2016-07-30 19:36:23 +0200 | [diff] [blame] | 524 | strbuf_addstr(&parsed, " --"); |
Jeff King | e35f11c | 2018-01-15 17:59:43 +0700 | [diff] [blame] | 525 | sq_quote_argv(&parsed, argv); |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 526 | puts(parsed.buf); |
| 527 | return 0; |
| 528 | } |
| 529 | |
Christian Couder | 5032537 | 2009-04-25 06:55:26 +0200 | [diff] [blame] | 530 | static int cmd_sq_quote(int argc, const char **argv) |
| 531 | { |
| 532 | struct strbuf buf = STRBUF_INIT; |
| 533 | |
| 534 | if (argc) |
Jeff King | e35f11c | 2018-01-15 17:59:43 +0700 | [diff] [blame] | 535 | sq_quote_argv(&buf, argv); |
Christian Couder | 5032537 | 2009-04-25 06:55:26 +0200 | [diff] [blame] | 536 | printf("%s\n", buf.buf); |
| 537 | strbuf_release(&buf); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 542 | static 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 Nieder | 7006b5b | 2009-11-09 09:04:54 -0600 | [diff] [blame] | 550 | static const char builtin_rev_parse_usage[] = |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 551 | N_("git rev-parse --parseopt [<options>] -- [<args>...]\n" |
Nguyễn Thái Ngọc Duy | 2c7c184 | 2012-08-20 19:32:40 +0700 | [diff] [blame] | 552 | " or: git rev-parse --sq-quote [<arg>...]\n" |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 553 | " or: git rev-parse [<options>] [<arg>...]\n" |
Nguyễn Thái Ngọc Duy | 2c7c184 | 2012-08-20 19:32:40 +0700 | [diff] [blame] | 554 | "\n" |
| 555 | "Run \"git rev-parse --parseopt -h\" for more information on the first usage."); |
Jonathan Nieder | 7006b5b | 2009-11-09 09:04:54 -0600 | [diff] [blame] | 556 | |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 557 | /* |
| 558 | * Parse "opt" or "opt=<value>", setting value respectively to either |
| 559 | * NULL or the string after "=". |
| 560 | */ |
| 561 | static 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 King | ffddfc6 | 2017-03-15 16:08:02 -0400 | [diff] [blame] | 576 | static 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 Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 585 | int cmd_rev_parse(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 586 | { |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 587 | int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; |
Jeff King | fc7d47f | 2016-02-29 06:01:56 -0500 | [diff] [blame] | 588 | int did_repo_setup = 0; |
Jeff King | 1418567 | 2013-12-06 17:05:48 -0500 | [diff] [blame] | 589 | int has_dashdash = 0; |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 590 | int output_prefix = 0; |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 591 | struct object_id oid; |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 592 | unsigned int flags = 0; |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 593 | const char *name = NULL; |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 594 | struct object_context unused; |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 595 | struct strbuf buf = STRBUF_INIT; |
brian m. carlson | 7e0d029 | 2019-08-18 20:04:07 +0000 | [diff] [blame] | 596 | const int hexsz = the_hash_algo->hexsz; |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 597 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 598 | if (argc > 1 && !strcmp("--parseopt", argv[1])) |
| 599 | return cmd_parseopt(argc - 1, argv + 1, prefix); |
| 600 | |
Christian Couder | 5032537 | 2009-04-25 06:55:26 +0200 | [diff] [blame] | 601 | if (argc > 1 && !strcmp("--sq-quote", argv[1])) |
| 602 | return cmd_sq_quote(argc - 2, argv + 2); |
| 603 | |
Jonathan Nieder | 7006b5b | 2009-11-09 09:04:54 -0600 | [diff] [blame] | 604 | if (argc > 1 && !strcmp("-h", argv[1])) |
| 605 | usage(builtin_rev_parse_usage); |
| 606 | |
Jeff King | 1418567 | 2013-12-06 17:05:48 -0500 | [diff] [blame] | 607 | for (i = 1; i < argc; i++) { |
| 608 | if (!strcmp(argv[i], "--")) { |
| 609 | has_dashdash = 1; |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | |
Jeff King | fc7d47f | 2016-02-29 06:01:56 -0500 | [diff] [blame] | 614 | /* 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 Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 621 | for (i = 1; i < argc; i++) { |
Junio C Hamano | 16cee38 | 2006-06-05 22:36:21 -0700 | [diff] [blame] | 622 | const char *arg = argv[i]; |
Linus Torvalds | fb18a2e | 2006-03-26 16:28:20 -0800 | [diff] [blame] | 623 | |
Jeff King | fc7d47f | 2016-02-29 06:01:56 -0500 | [diff] [blame] | 624 | 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 Duy | 557bd83 | 2014-11-30 15:24:31 +0700 | [diff] [blame] | 648 | if (!strcmp(arg, "--git-path")) { |
| 649 | if (!argv[i + 1]) |
| 650 | die("--git-path requires an argument"); |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 651 | strbuf_reset(&buf); |
| 652 | puts(relative_path(git_path("%s", argv[i + 1]), |
| 653 | prefix, &buf)); |
Nguyễn Thái Ngọc Duy | 557bd83 | 2014-11-30 15:24:31 +0700 | [diff] [blame] | 654 | i++; |
| 655 | continue; |
| 656 | } |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 657 | if (as_is) { |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 658 | if (show_file(arg, output_prefix) && as_is < 2) |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 659 | verify_filename(prefix, arg, 0); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 660 | continue; |
| 661 | } |
Eric Wong | 3af0698 | 2006-01-29 16:26:40 -0800 | [diff] [blame] | 662 | 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 Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 671 | if (starts_with(arg, "-n")) { |
Eric Wong | 3af0698 | 2006-01-29 16:26:40 -0800 | [diff] [blame] | 672 | if ((filter & DO_FLAGS) && (filter & DO_REVS)) |
| 673 | show(arg); |
| 674 | continue; |
| 675 | } |
| 676 | |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 677 | if (*arg == '-') { |
| 678 | if (!strcmp(arg, "--")) { |
Linus Torvalds | fb18a2e | 2006-03-26 16:28:20 -0800 | [diff] [blame] | 679 | as_is = 2; |
Linus Torvalds | a08b650 | 2005-10-20 17:16:30 -0700 | [diff] [blame] | 680 | /* Pass on the "--" if we show anything but files.. */ |
| 681 | if (filter & (DO_FLAGS | DO_REVS)) |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 682 | show_file(arg, 0); |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 683 | continue; |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 684 | } |
| 685 | if (!strcmp(arg, "--default")) { |
David Sharp | a43219f | 2014-01-28 13:21:00 -0800 | [diff] [blame] | 686 | def = argv[++i]; |
| 687 | if (!def) |
| 688 | die("--default requires an argument"); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 689 | continue; |
| 690 | } |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 691 | if (!strcmp(arg, "--prefix")) { |
David Sharp | a43219f | 2014-01-28 13:21:00 -0800 | [diff] [blame] | 692 | prefix = argv[++i]; |
| 693 | if (!prefix) |
| 694 | die("--prefix requires an argument"); |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 695 | startup_info->prefix = prefix; |
| 696 | output_prefix = 1; |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 697 | continue; |
| 698 | } |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 699 | if (!strcmp(arg, "--revs-only")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 700 | filter &= ~DO_NOREV; |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 701 | continue; |
| 702 | } |
| 703 | if (!strcmp(arg, "--no-revs")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 704 | filter &= ~DO_REVS; |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 705 | continue; |
| 706 | } |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 707 | if (!strcmp(arg, "--flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 708 | filter &= ~DO_NONFLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 709 | continue; |
| 710 | } |
| 711 | if (!strcmp(arg, "--no-flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 712 | filter &= ~DO_FLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 713 | continue; |
| 714 | } |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 715 | if (!strcmp(arg, "--verify")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 716 | filter &= ~(DO_FLAGS|DO_NOREV); |
| 717 | verify = 1; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 718 | continue; |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 719 | } |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 720 | if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { |
| 721 | quiet = 1; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 722 | flags |= GET_OID_QUIETLY; |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 723 | continue; |
| 724 | } |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 725 | if (opt_with_value(arg, "--short", &arg)) { |
Junio C Hamano | d501250 | 2006-01-25 01:35:38 -0800 | [diff] [blame] | 726 | filter &= ~(DO_FLAGS|DO_NOREV); |
| 727 | verify = 1; |
| 728 | abbrev = DEFAULT_ABBREV; |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 729 | if (!arg) |
Junio C Hamano | 7b5b772 | 2016-09-30 17:19:36 -0700 | [diff] [blame] | 730 | continue; |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 731 | abbrev = strtoul(arg, NULL, 10); |
Junio C Hamano | 1dc4fb8 | 2006-01-26 00:48:19 -0800 | [diff] [blame] | 732 | if (abbrev < MINIMUM_ABBREV) |
| 733 | abbrev = MINIMUM_ABBREV; |
brian m. carlson | 7e0d029 | 2019-08-18 20:04:07 +0000 | [diff] [blame] | 734 | else if (hexsz <= abbrev) |
| 735 | abbrev = hexsz; |
Junio C Hamano | d501250 | 2006-01-25 01:35:38 -0800 | [diff] [blame] | 736 | continue; |
| 737 | } |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 738 | if (!strcmp(arg, "--sq")) { |
| 739 | output_sq = 1; |
| 740 | continue; |
| 741 | } |
Linus Torvalds | 042a4ed | 2005-06-26 11:34:30 -0700 | [diff] [blame] | 742 | if (!strcmp(arg, "--not")) { |
| 743 | show_type ^= REVERSED; |
| 744 | continue; |
| 745 | } |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 746 | if (!strcmp(arg, "--symbolic")) { |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 747 | symbolic = SHOW_SYMBOLIC_ASIS; |
| 748 | continue; |
| 749 | } |
| 750 | if (!strcmp(arg, "--symbolic-full-name")) { |
| 751 | symbolic = SHOW_SYMBOLIC_FULL; |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 752 | continue; |
| 753 | } |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 754 | if (opt_with_value(arg, "--abbrev-ref", &arg)) { |
Bert Wesarg | a45d346 | 2009-04-13 13:20:26 +0200 | [diff] [blame] | 755 | abbrev_ref = 1; |
| 756 | abbrev_ref_strict = warn_ambiguous_refs; |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 757 | if (arg) { |
| 758 | if (!strcmp(arg, "strict")) |
Bert Wesarg | a45d346 | 2009-04-13 13:20:26 +0200 | [diff] [blame] | 759 | abbrev_ref_strict = 1; |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 760 | else if (!strcmp(arg, "loose")) |
Bert Wesarg | a45d346 | 2009-04-13 13:20:26 +0200 | [diff] [blame] | 761 | abbrev_ref_strict = 0; |
| 762 | else |
Jeff King | 9d16ca6 | 2017-03-15 16:06:53 -0400 | [diff] [blame] | 763 | die("unknown mode for --abbrev-ref: %s", |
| 764 | arg); |
Bert Wesarg | a45d346 | 2009-04-13 13:20:26 +0200 | [diff] [blame] | 765 | } |
| 766 | continue; |
| 767 | } |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 768 | if (!strcmp(arg, "--all")) { |
Michael Haggerty | e23b036 | 2015-05-25 18:38:29 +0000 | [diff] [blame] | 769 | for_each_ref(show_reference, NULL); |
Andreas Gruenbacher | 5221048 | 2018-10-23 21:17:58 +0200 | [diff] [blame] | 770 | clear_ref_exclusion(&ref_excludes); |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 771 | continue; |
| 772 | } |
Jeff King | ef87cc7 | 2017-03-15 16:05:36 -0400 | [diff] [blame] | 773 | if (skip_prefix(arg, "--disambiguate=", &arg)) { |
| 774 | for_each_abbrev(arg, show_abbrev, NULL); |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 775 | continue; |
| 776 | } |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 777 | if (!strcmp(arg, "--bisect")) { |
Jeff King | 1d0538e | 2017-09-06 07:53:10 -0400 | [diff] [blame] | 778 | for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0); |
| 779 | for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0); |
Linus Torvalds | ad3f9a7 | 2009-10-27 11:28:07 -0700 | [diff] [blame] | 780 | continue; |
| 781 | } |
Jeff King | ffddfc6 | 2017-03-15 16:08:02 -0400 | [diff] [blame] | 782 | if (opt_with_value(arg, "--branches", &arg)) { |
| 783 | handle_ref_opt(arg, "refs/heads/"); |
Ilari Liusvaara | b09fe97 | 2010-01-20 11:48:26 +0200 | [diff] [blame] | 784 | continue; |
| 785 | } |
Jeff King | ffddfc6 | 2017-03-15 16:08:02 -0400 | [diff] [blame] | 786 | if (opt_with_value(arg, "--tags", &arg)) { |
| 787 | handle_ref_opt(arg, "refs/tags/"); |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 788 | continue; |
| 789 | } |
Jeff King | ef87cc7 | 2017-03-15 16:05:36 -0400 | [diff] [blame] | 790 | if (skip_prefix(arg, "--glob=", &arg)) { |
Jeff King | ffddfc6 | 2017-03-15 16:08:02 -0400 | [diff] [blame] | 791 | handle_ref_opt(arg, NULL); |
Ilari Liusvaara | b09fe97 | 2010-01-20 11:48:26 +0200 | [diff] [blame] | 792 | continue; |
| 793 | } |
Jeff King | ffddfc6 | 2017-03-15 16:08:02 -0400 | [diff] [blame] | 794 | if (opt_with_value(arg, "--remotes", &arg)) { |
| 795 | handle_ref_opt(arg, "refs/remotes/"); |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 796 | continue; |
| 797 | } |
Jeff King | ef87cc7 | 2017-03-15 16:05:36 -0400 | [diff] [blame] | 798 | if (skip_prefix(arg, "--exclude=", &arg)) { |
| 799 | add_ref_exclusion(&ref_excludes, arg); |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 800 | continue; |
| 801 | } |
Steven Drake | 7cceca5 | 2010-01-12 11:33:48 +1300 | [diff] [blame] | 802 | if (!strcmp(arg, "--show-toplevel")) { |
| 803 | const char *work_tree = get_git_work_tree(); |
| 804 | if (work_tree) |
| 805 | puts(work_tree); |
Jeff King | 2d92ab3 | 2019-11-19 03:05:43 -0500 | [diff] [blame] | 806 | else |
| 807 | die("this operation must be run in a work tree"); |
Steven Drake | 7cceca5 | 2010-01-12 11:33:48 +1300 | [diff] [blame] | 808 | continue; |
| 809 | } |
Stefan Beller | bf0231c | 2017-03-08 15:07:42 -0800 | [diff] [blame] | 810 | 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 Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 816 | if (!strcmp(arg, "--show-prefix")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 817 | if (prefix) |
| 818 | puts(prefix); |
Ross Lagerwall | 658219f | 2012-04-09 15:27:56 +0200 | [diff] [blame] | 819 | else |
| 820 | putchar('\n'); |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 821 | continue; |
| 822 | } |
Junio C Hamano | 5f94c73 | 2005-12-22 22:35:38 -0800 | [diff] [blame] | 823 | if (!strcmp(arg, "--show-cdup")) { |
| 824 | const char *pfx = prefix; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 825 | 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 Hamano | 5f94c73 | 2005-12-22 22:35:38 -0800 | [diff] [blame] | 832 | while (pfx) { |
| 833 | pfx = strchr(pfx, '/'); |
| 834 | if (pfx) { |
| 835 | pfx++; |
| 836 | printf("../"); |
| 837 | } |
| 838 | } |
| 839 | putchar('\n'); |
| 840 | continue; |
| 841 | } |
SZEDER Gábor | a2f5a87 | 2017-02-03 03:48:23 +0100 | [diff] [blame] | 842 | if (!strcmp(arg, "--git-dir") || |
| 843 | !strcmp(arg, "--absolute-git-dir")) { |
Linus Torvalds | a8783ee | 2005-09-18 11:18:30 -0700 | [diff] [blame] | 844 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 845 | char *cwd; |
Nguyễn Thái Ngọc Duy | d06f15d | 2010-02-14 22:44:42 +0700 | [diff] [blame] | 846 | int len; |
SZEDER Gábor | a2f5a87 | 2017-02-03 03:48:23 +0100 | [diff] [blame] | 847 | 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 Torvalds | a8783ee | 2005-09-18 11:18:30 -0700 | [diff] [blame] | 863 | } |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 864 | cwd = xgetcwd(); |
Nguyễn Thái Ngọc Duy | d06f15d | 2010-02-14 22:44:42 +0700 | [diff] [blame] | 865 | len = strlen(cwd); |
| 866 | printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : ""); |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 867 | free(cwd); |
Linus Torvalds | a8783ee | 2005-09-18 11:18:30 -0700 | [diff] [blame] | 868 | continue; |
| 869 | } |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 870 | if (!strcmp(arg, "--git-common-dir")) { |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 871 | strbuf_reset(&buf); |
| 872 | puts(relative_path(get_git_common_dir(), |
| 873 | prefix, &buf)); |
Nguyễn Thái Ngọc Duy | 31e26eb | 2014-11-30 15:24:44 +0700 | [diff] [blame] | 874 | continue; |
| 875 | } |
Johannes Schindelin | 6d9ba67 | 2007-01-23 13:30:20 +0100 | [diff] [blame] | 876 | if (!strcmp(arg, "--is-inside-git-dir")) { |
| 877 | printf("%s\n", is_inside_git_dir() ? "true" |
| 878 | : "false"); |
| 879 | continue; |
| 880 | } |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 881 | if (!strcmp(arg, "--is-inside-work-tree")) { |
| 882 | printf("%s\n", is_inside_work_tree() ? "true" |
| 883 | : "false"); |
| 884 | continue; |
| 885 | } |
Matthias Lederhofer | 493c774 | 2007-06-03 16:46:36 +0200 | [diff] [blame] | 886 | if (!strcmp(arg, "--is-bare-repository")) { |
| 887 | printf("%s\n", is_bare_repository() ? "true" |
| 888 | : "false"); |
| 889 | continue; |
| 890 | } |
Øystein Walle | 417abfd | 2017-09-18 19:04:29 +0200 | [diff] [blame] | 891 | if (!strcmp(arg, "--is-shallow-repository")) { |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 892 | printf("%s\n", |
| 893 | is_repository_shallow(the_repository) ? "true" |
Øystein Walle | 417abfd | 2017-09-18 19:04:29 +0200 | [diff] [blame] | 894 | : "false"); |
| 895 | continue; |
| 896 | } |
Nguyễn Thái Ngọc Duy | a76295d | 2014-06-13 19:19:46 +0700 | [diff] [blame] | 897 | 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. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 901 | const struct object_id *oid = &the_index.split_index->base_oid; |
| 902 | const char *path = git_path("sharedindex.%s", oid_to_hex(oid)); |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 903 | strbuf_reset(&buf); |
| 904 | puts(relative_path(path, prefix, &buf)); |
Nguyễn Thái Ngọc Duy | a76295d | 2014-06-13 19:19:46 +0700 | [diff] [blame] | 905 | } |
| 906 | continue; |
| 907 | } |
Jeff King | ef87cc7 | 2017-03-15 16:05:36 -0400 | [diff] [blame] | 908 | if (skip_prefix(arg, "--since=", &arg)) { |
| 909 | show_datestring("--max-age=", arg); |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 910 | continue; |
| 911 | } |
Jeff King | ef87cc7 | 2017-03-15 16:05:36 -0400 | [diff] [blame] | 912 | if (skip_prefix(arg, "--after=", &arg)) { |
| 913 | show_datestring("--max-age=", arg); |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 914 | continue; |
| 915 | } |
Jeff King | ef87cc7 | 2017-03-15 16:05:36 -0400 | [diff] [blame] | 916 | if (skip_prefix(arg, "--before=", &arg)) { |
| 917 | show_datestring("--min-age=", arg); |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 918 | continue; |
| 919 | } |
Jeff King | ef87cc7 | 2017-03-15 16:05:36 -0400 | [diff] [blame] | 920 | if (skip_prefix(arg, "--until=", &arg)) { |
| 921 | show_datestring("--min-age=", arg); |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 922 | continue; |
| 923 | } |
brian m. carlson | 2eabd38 | 2019-10-28 00:58:55 +0000 | [diff] [blame] | 924 | 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 Torvalds | 9523a4c | 2006-02-05 11:58:34 -0800 | [diff] [blame] | 935 | if (show_flag(arg) && verify) |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 936 | die_no_single_rev(quiet); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 937 | continue; |
| 938 | } |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 939 | |
| 940 | /* Not a flag argument */ |
Santi Béjar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 941 | if (try_difference(arg)) |
| 942 | continue; |
Björn Steinbrink | 2122f8b | 2008-07-26 18:37:56 +0200 | [diff] [blame] | 943 | if (try_parent_shorthands(arg)) |
| 944 | continue; |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 945 | name = arg; |
| 946 | type = NORMAL; |
| 947 | if (*arg == '^') { |
| 948 | name++; |
| 949 | type = REVERSED; |
Linus Torvalds | 800644c | 2005-06-20 08:29:13 -0700 | [diff] [blame] | 950 | } |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 951 | if (!get_oid_with_context(the_repository, name, |
| 952 | flags, &oid, &unused)) { |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 953 | if (verify) |
| 954 | revs_count++; |
| 955 | else |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 956 | show_rev(type, &oid, name); |
Linus Torvalds | 800644c | 2005-06-20 08:29:13 -0700 | [diff] [blame] | 957 | continue; |
| 958 | } |
Christian Couder | 75ecfce | 2008-04-26 15:19:29 +0200 | [diff] [blame] | 959 | if (verify) |
| 960 | die_no_single_rev(quiet); |
Jeff King | 1418567 | 2013-12-06 17:05:48 -0500 | [diff] [blame] | 961 | if (has_dashdash) |
| 962 | die("bad revision '%s'", arg); |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 963 | as_is = 1; |
John Keeping | 12b9d32 | 2013-06-16 15:18:17 +0100 | [diff] [blame] | 964 | if (!show_file(arg, output_prefix)) |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 965 | continue; |
Matthieu Moy | 023e37c | 2012-06-18 20:18:21 +0200 | [diff] [blame] | 966 | verify_filename(prefix, arg, 1); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 967 | } |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 968 | strbuf_release(&buf); |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 969 | if (verify) { |
| 970 | if (revs_count == 1) { |
brian m. carlson | 8bc095f | 2017-05-01 02:29:02 +0000 | [diff] [blame] | 971 | show_rev(type, &oid, name); |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 972 | return 0; |
| 973 | } else if (revs_count == 0 && show_default()) |
| 974 | return 0; |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 975 | die_no_single_rev(quiet); |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 976 | } else |
| 977 | show_default(); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 978 | return 0; |
| 979 | } |