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 | */ |
| 6 | #include "cache.h" |
Linus Torvalds | a8be83f | 2005-06-20 20:28:09 -0700 | [diff] [blame] | 7 | #include "commit.h" |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 8 | #include "refs.h" |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 9 | #include "quote.h" |
Christian Couder | 895f10c | 2006-06-03 18:45:43 +0200 | [diff] [blame] | 10 | #include "builtin.h" |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 11 | #include "parse-options.h" |
Linus Torvalds | a8be83f | 2005-06-20 20:28:09 -0700 | [diff] [blame] | 12 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 13 | #define DO_REVS 1 |
| 14 | #define DO_NOREV 2 |
| 15 | #define DO_FLAGS 4 |
| 16 | #define DO_NONFLAGS 8 |
| 17 | static int filter = ~0; |
| 18 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 19 | static const char *def; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 042a4ed | 2005-06-26 11:34:30 -0700 | [diff] [blame] | 21 | #define NORMAL 0 |
| 22 | #define REVERSED 1 |
| 23 | static int show_type = NORMAL; |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 24 | |
| 25 | #define SHOW_SYMBOLIC_ASIS 1 |
| 26 | #define SHOW_SYMBOLIC_FULL 2 |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 27 | static int symbolic; |
| 28 | static int abbrev; |
| 29 | static int output_sq; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 31 | /* |
| 32 | * Some arguments are relevant "revision" arguments, |
| 33 | * others are about output format or other details. |
| 34 | * This sorts it all out. |
| 35 | */ |
| 36 | static int is_rev_argument(const char *arg) |
| 37 | { |
| 38 | static const char *rev_args[] = { |
Junio C Hamano | e091eb9 | 2005-10-05 14:49:54 -0700 | [diff] [blame] | 39 | "--all", |
Junio C Hamano | 5ccfb75 | 2005-08-08 19:31:37 -0700 | [diff] [blame] | 40 | "--bisect", |
Junio C Hamano | 5a83f3b | 2005-10-30 01:08:35 -0800 | [diff] [blame] | 41 | "--dense", |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 42 | "--branches", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 43 | "--header", |
| 44 | "--max-age=", |
| 45 | "--max-count=", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 46 | "--min-age=", |
Junio C Hamano | 5ccfb75 | 2005-08-08 19:31:37 -0700 | [diff] [blame] | 47 | "--no-merges", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 48 | "--objects", |
Junio C Hamano | c649657 | 2006-02-19 03:32:31 -0800 | [diff] [blame] | 49 | "--objects-edge", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 50 | "--parents", |
| 51 | "--pretty", |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 52 | "--remotes", |
Junio C Hamano | 5a83f3b | 2005-10-30 01:08:35 -0800 | [diff] [blame] | 53 | "--sparse", |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 54 | "--tags", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 55 | "--topo-order", |
Junio C Hamano | 4c8725f | 2006-02-15 22:05:33 -0800 | [diff] [blame] | 56 | "--date-order", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 57 | "--unpacked", |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 58 | NULL |
| 59 | }; |
| 60 | const char **p = rev_args; |
| 61 | |
Eric Wong | 8233340 | 2006-01-29 16:28:02 -0800 | [diff] [blame] | 62 | /* accept -<digit>, like traditional "head" */ |
| 63 | if ((*arg == '-') && isdigit(arg[1])) |
| 64 | return 1; |
| 65 | |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 66 | for (;;) { |
| 67 | const char *str = *p++; |
| 68 | int len; |
| 69 | if (!str) |
| 70 | return 0; |
| 71 | len = strlen(str); |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 72 | if (!strcmp(arg, str) || |
| 73 | (str[len-1] == '=' && !strncmp(arg, str, len))) |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 74 | return 1; |
| 75 | } |
| 76 | } |
| 77 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 78 | /* Output argument as a string, either SQ or normal */ |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 79 | static void show(const char *arg) |
| 80 | { |
| 81 | if (output_sq) { |
| 82 | int sq = '\'', ch; |
| 83 | |
| 84 | putchar(sq); |
| 85 | while ((ch = *arg++)) { |
| 86 | if (ch == sq) |
| 87 | fputs("'\\'", stdout); |
| 88 | putchar(ch); |
| 89 | } |
| 90 | putchar(sq); |
| 91 | putchar(' '); |
| 92 | } |
| 93 | else |
| 94 | puts(arg); |
| 95 | } |
| 96 | |
Johannes Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 97 | /* Like show(), but with a negation prefix according to type */ |
| 98 | static void show_with_type(int type, const char *arg) |
| 99 | { |
| 100 | if (type != show_type) |
| 101 | putchar('^'); |
| 102 | show(arg); |
| 103 | } |
| 104 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 105 | /* Output a revision, only if filter allows it */ |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 106 | static void show_rev(int type, const unsigned char *sha1, const char *name) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 107 | { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 108 | if (!(filter & DO_REVS)) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 109 | return; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 110 | def = NULL; |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 111 | |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 112 | if (symbolic && name) { |
| 113 | if (symbolic == SHOW_SYMBOLIC_FULL) { |
| 114 | unsigned char discard[20]; |
| 115 | char *full; |
| 116 | |
| 117 | switch (dwim_ref(name, strlen(name), discard, &full)) { |
| 118 | case 0: |
| 119 | /* |
| 120 | * Not found -- not a ref. We could |
| 121 | * emit "name" here, but symbolic-full |
| 122 | * users are interested in finding the |
| 123 | * refs spelled in full, and they would |
| 124 | * need to filter non-refs if we did so. |
| 125 | */ |
| 126 | break; |
| 127 | case 1: /* happy */ |
Johannes Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 128 | show_with_type(type, full); |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 129 | break; |
| 130 | default: /* ambiguous */ |
| 131 | error("refname '%s' is ambiguous", name); |
| 132 | break; |
| 133 | } |
| 134 | } else { |
Johannes Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 135 | show_with_type(type, name); |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
Junio C Hamano | d501250 | 2006-01-25 01:35:38 -0800 | [diff] [blame] | 138 | else if (abbrev) |
Johannes Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 139 | show_with_type(type, find_unique_abbrev(sha1, abbrev)); |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 140 | else |
Johannes Sixt | e00f379 | 2008-05-23 16:13:05 +0200 | [diff] [blame] | 141 | show_with_type(type, sha1_to_hex(sha1)); |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 144 | /* Output a flag, only if filter allows it. */ |
Junio C Hamano | 16cee38 | 2006-06-05 22:36:21 -0700 | [diff] [blame] | 145 | static int show_flag(const char *arg) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 146 | { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 147 | if (!(filter & DO_FLAGS)) |
Linus Torvalds | 9523a4c | 2006-02-05 11:58:34 -0800 | [diff] [blame] | 148 | return 0; |
| 149 | if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { |
Linus Torvalds | 0360e99 | 2005-08-23 10:47:54 -0700 | [diff] [blame] | 150 | show(arg); |
Linus Torvalds | 9523a4c | 2006-02-05 11:58:34 -0800 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | return 0; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 156 | static int show_default(void) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 157 | { |
Junio C Hamano | 16cee38 | 2006-06-05 22:36:21 -0700 | [diff] [blame] | 158 | const char *s = def; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 159 | |
| 160 | if (s) { |
| 161 | unsigned char sha1[20]; |
| 162 | |
| 163 | def = NULL; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 164 | if (!get_sha1(s, sha1)) { |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 165 | show_rev(NORMAL, sha1, s); |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 166 | return 1; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 167 | } |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 168 | } |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 169 | return 0; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 172 | static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 173 | { |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 174 | show_rev(NORMAL, sha1, refname); |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 178 | static void show_datestring(const char *flag, const char *datestr) |
| 179 | { |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 180 | static char buffer[100]; |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 181 | |
| 182 | /* date handling requires both flags and revs */ |
| 183 | if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) |
| 184 | return; |
Linus Torvalds | 3c07b1d | 2005-11-14 19:29:06 -0800 | [diff] [blame] | 185 | snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr)); |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 186 | show(buffer); |
| 187 | } |
| 188 | |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 189 | static int show_file(const char *arg) |
Linus Torvalds | 7a3dd47 | 2005-10-18 00:16:45 -0700 | [diff] [blame] | 190 | { |
Linus Torvalds | 7b34c2f | 2005-10-25 15:24:55 -0700 | [diff] [blame] | 191 | show_default(); |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 192 | if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { |
Linus Torvalds | 7a3dd47 | 2005-10-18 00:16:45 -0700 | [diff] [blame] | 193 | show(arg); |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 194 | return 1; |
| 195 | } |
| 196 | return 0; |
Linus Torvalds | 7a3dd47 | 2005-10-18 00:16:45 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Junio C Hamano | b7d936b | 2006-07-06 00:16:35 -0700 | [diff] [blame] | 199 | static int try_difference(const char *arg) |
Santi BĂ©jar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 200 | { |
| 201 | char *dotdot; |
| 202 | unsigned char sha1[20]; |
| 203 | unsigned char end[20]; |
| 204 | const char *next; |
| 205 | const char *this; |
| 206 | int symmetric; |
| 207 | |
| 208 | if (!(dotdot = strstr(arg, ".."))) |
| 209 | return 0; |
| 210 | next = dotdot + 2; |
| 211 | this = arg; |
| 212 | symmetric = (*next == '.'); |
| 213 | |
| 214 | *dotdot = 0; |
| 215 | next += symmetric; |
| 216 | |
| 217 | if (!*next) |
| 218 | next = "HEAD"; |
| 219 | if (dotdot == arg) |
| 220 | this = "HEAD"; |
| 221 | if (!get_sha1(this, sha1) && !get_sha1(next, end)) { |
| 222 | show_rev(NORMAL, end, next); |
| 223 | show_rev(symmetric ? NORMAL : REVERSED, sha1, this); |
| 224 | if (symmetric) { |
| 225 | struct commit_list *exclude; |
| 226 | struct commit *a, *b; |
| 227 | a = lookup_commit_reference(sha1); |
| 228 | b = lookup_commit_reference(end); |
| 229 | exclude = get_merge_bases(a, b, 1); |
| 230 | while (exclude) { |
| 231 | struct commit_list *n = exclude->next; |
| 232 | show_rev(REVERSED, |
| 233 | exclude->item->object.sha1,NULL); |
| 234 | free(exclude); |
| 235 | exclude = n; |
| 236 | } |
| 237 | } |
| 238 | return 1; |
| 239 | } |
| 240 | *dotdot = '.'; |
| 241 | return 0; |
| 242 | } |
| 243 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 244 | static int parseopt_dump(const struct option *o, const char *arg, int unset) |
| 245 | { |
| 246 | struct strbuf *parsed = o->value; |
| 247 | if (unset) |
| 248 | strbuf_addf(parsed, " --no-%s", o->long_name); |
| 249 | else if (o->short_name) |
| 250 | strbuf_addf(parsed, " -%c", o->short_name); |
| 251 | else |
| 252 | strbuf_addf(parsed, " --%s", o->long_name); |
| 253 | if (arg) { |
| 254 | strbuf_addch(parsed, ' '); |
| 255 | sq_quote_buf(parsed, arg); |
| 256 | } |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static const char *skipspaces(const char *s) |
| 261 | { |
| 262 | while (isspace(*s)) |
| 263 | s++; |
| 264 | return s; |
| 265 | } |
| 266 | |
| 267 | static int cmd_parseopt(int argc, const char **argv, const char *prefix) |
| 268 | { |
| 269 | static int keep_dashdash = 0; |
| 270 | static char const * const parseopt_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 271 | "git rev-parse --parseopt [options] -- [<args>...]", |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 272 | NULL |
| 273 | }; |
| 274 | static struct option parseopt_opts[] = { |
| 275 | OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash, |
| 276 | "keep the `--` passed as an arg"), |
| 277 | OPT_END(), |
| 278 | }; |
| 279 | |
| 280 | struct strbuf sb, parsed; |
| 281 | const char **usage = NULL; |
| 282 | struct option *opts = NULL; |
| 283 | int onb = 0, osz = 0, unb = 0, usz = 0; |
| 284 | |
| 285 | strbuf_init(&parsed, 0); |
| 286 | strbuf_addstr(&parsed, "set --"); |
| 287 | argc = parse_options(argc, argv, parseopt_opts, parseopt_usage, |
| 288 | PARSE_OPT_KEEP_DASHDASH); |
| 289 | if (argc < 1 || strcmp(argv[0], "--")) |
| 290 | usage_with_options(parseopt_usage, parseopt_opts); |
| 291 | |
| 292 | strbuf_init(&sb, 0); |
| 293 | /* get the usage up to the first line with a -- on it */ |
| 294 | for (;;) { |
| 295 | if (strbuf_getline(&sb, stdin, '\n') == EOF) |
| 296 | die("premature end of input"); |
| 297 | ALLOC_GROW(usage, unb + 1, usz); |
| 298 | if (!strcmp("--", sb.buf)) { |
| 299 | if (unb < 1) |
| 300 | die("no usage string given before the `--' separator"); |
| 301 | usage[unb] = NULL; |
| 302 | break; |
| 303 | } |
| 304 | usage[unb++] = strbuf_detach(&sb, NULL); |
| 305 | } |
| 306 | |
| 307 | /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */ |
| 308 | while (strbuf_getline(&sb, stdin, '\n') != EOF) { |
| 309 | const char *s; |
| 310 | struct option *o; |
| 311 | |
| 312 | if (!sb.len) |
| 313 | continue; |
| 314 | |
| 315 | ALLOC_GROW(opts, onb + 1, osz); |
| 316 | memset(opts + onb, 0, sizeof(opts[onb])); |
| 317 | |
| 318 | o = &opts[onb++]; |
| 319 | s = strchr(sb.buf, ' '); |
| 320 | if (!s || *sb.buf == ' ') { |
| 321 | o->type = OPTION_GROUP; |
Jay Soffian | e103343 | 2008-02-25 23:07:39 -0500 | [diff] [blame] | 322 | o->help = xstrdup(skipspaces(sb.buf)); |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 323 | continue; |
| 324 | } |
| 325 | |
| 326 | o->type = OPTION_CALLBACK; |
| 327 | o->help = xstrdup(skipspaces(s)); |
| 328 | o->value = &parsed; |
Pierre Habouzit | ff962a3 | 2008-03-02 09:21:38 +0100 | [diff] [blame] | 329 | o->flags = PARSE_OPT_NOARG; |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 330 | o->callback = &parseopt_dump; |
Pierre Habouzit | ff962a3 | 2008-03-02 09:21:38 +0100 | [diff] [blame] | 331 | while (s > sb.buf && strchr("*=?!", s[-1])) { |
| 332 | switch (*--s) { |
| 333 | case '=': |
| 334 | o->flags &= ~PARSE_OPT_NOARG; |
| 335 | break; |
| 336 | case '?': |
| 337 | o->flags &= ~PARSE_OPT_NOARG; |
| 338 | o->flags |= PARSE_OPT_OPTARG; |
| 339 | break; |
| 340 | case '!': |
| 341 | o->flags |= PARSE_OPT_NONEG; |
| 342 | break; |
| 343 | case '*': |
| 344 | o->flags |= PARSE_OPT_HIDDEN; |
| 345 | break; |
| 346 | } |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (s - sb.buf == 1) /* short option only */ |
| 350 | o->short_name = *sb.buf; |
| 351 | else if (sb.buf[1] != ',') /* long option only */ |
| 352 | o->long_name = xmemdupz(sb.buf, s - sb.buf); |
| 353 | else { |
| 354 | o->short_name = *sb.buf; |
| 355 | o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2); |
| 356 | } |
| 357 | } |
| 358 | strbuf_release(&sb); |
| 359 | |
| 360 | /* put an OPT_END() */ |
| 361 | ALLOC_GROW(opts, onb + 1, osz); |
| 362 | memset(opts + onb, 0, sizeof(opts[onb])); |
| 363 | argc = parse_options(argc, argv, opts, usage, |
| 364 | keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0); |
| 365 | |
| 366 | strbuf_addf(&parsed, " --"); |
Christian Couder | b319ce4 | 2007-12-03 05:51:50 +0100 | [diff] [blame] | 367 | sq_quote_argv(&parsed, argv, 0); |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 368 | puts(parsed.buf); |
| 369 | return 0; |
| 370 | } |
| 371 | |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 372 | static void die_no_single_rev(int quiet) |
| 373 | { |
| 374 | if (quiet) |
| 375 | exit(1); |
| 376 | else |
| 377 | die("Needed a single revision"); |
| 378 | } |
| 379 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 380 | int cmd_rev_parse(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 381 | { |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 382 | int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 383 | unsigned char sha1[20]; |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 384 | const char *name = NULL; |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 385 | |
Pierre Habouzit | 21d4783 | 2007-11-04 11:30:53 +0100 | [diff] [blame] | 386 | if (argc > 1 && !strcmp("--parseopt", argv[1])) |
| 387 | return cmd_parseopt(argc - 1, argv + 1, prefix); |
| 388 | |
Junio C Hamano | 5410a02 | 2007-11-06 12:23:14 -0800 | [diff] [blame] | 389 | prefix = setup_git_directory(); |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 390 | git_config(git_default_config, NULL); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 391 | for (i = 1; i < argc; i++) { |
Junio C Hamano | 16cee38 | 2006-06-05 22:36:21 -0700 | [diff] [blame] | 392 | const char *arg = argv[i]; |
Linus Torvalds | fb18a2e | 2006-03-26 16:28:20 -0800 | [diff] [blame] | 393 | |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 394 | if (as_is) { |
Linus Torvalds | fb18a2e | 2006-03-26 16:28:20 -0800 | [diff] [blame] | 395 | if (show_file(arg) && as_is < 2) |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 396 | verify_filename(prefix, arg); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 397 | continue; |
| 398 | } |
Eric Wong | 3af0698 | 2006-01-29 16:26:40 -0800 | [diff] [blame] | 399 | if (!strcmp(arg,"-n")) { |
| 400 | if (++i >= argc) |
| 401 | die("-n requires an argument"); |
| 402 | if ((filter & DO_FLAGS) && (filter & DO_REVS)) { |
| 403 | show(arg); |
| 404 | show(argv[i]); |
| 405 | } |
| 406 | continue; |
| 407 | } |
Junio C Hamano | 1968d77 | 2007-02-20 01:55:07 -0800 | [diff] [blame] | 408 | if (!prefixcmp(arg, "-n")) { |
Eric Wong | 3af0698 | 2006-01-29 16:26:40 -0800 | [diff] [blame] | 409 | if ((filter & DO_FLAGS) && (filter & DO_REVS)) |
| 410 | show(arg); |
| 411 | continue; |
| 412 | } |
| 413 | |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 414 | if (*arg == '-') { |
| 415 | if (!strcmp(arg, "--")) { |
Linus Torvalds | fb18a2e | 2006-03-26 16:28:20 -0800 | [diff] [blame] | 416 | as_is = 2; |
Linus Torvalds | a08b650 | 2005-10-20 17:16:30 -0700 | [diff] [blame] | 417 | /* Pass on the "--" if we show anything but files.. */ |
| 418 | if (filter & (DO_FLAGS | DO_REVS)) |
| 419 | show_file(arg); |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 420 | continue; |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 421 | } |
| 422 | if (!strcmp(arg, "--default")) { |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 423 | def = argv[i+1]; |
| 424 | i++; |
| 425 | continue; |
| 426 | } |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 427 | if (!strcmp(arg, "--revs-only")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 428 | filter &= ~DO_NOREV; |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 429 | continue; |
| 430 | } |
| 431 | if (!strcmp(arg, "--no-revs")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 432 | filter &= ~DO_REVS; |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 433 | continue; |
| 434 | } |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 435 | if (!strcmp(arg, "--flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 436 | filter &= ~DO_NONFLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 437 | continue; |
| 438 | } |
| 439 | if (!strcmp(arg, "--no-flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 440 | filter &= ~DO_FLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 441 | continue; |
| 442 | } |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 443 | if (!strcmp(arg, "--verify")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 444 | filter &= ~(DO_FLAGS|DO_NOREV); |
| 445 | verify = 1; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 446 | continue; |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 447 | } |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 448 | if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { |
| 449 | quiet = 1; |
| 450 | continue; |
| 451 | } |
Junio C Hamano | 62a604b | 2006-01-26 17:02:07 -0800 | [diff] [blame] | 452 | if (!strcmp(arg, "--short") || |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 453 | !prefixcmp(arg, "--short=")) { |
Junio C Hamano | d501250 | 2006-01-25 01:35:38 -0800 | [diff] [blame] | 454 | filter &= ~(DO_FLAGS|DO_NOREV); |
| 455 | verify = 1; |
| 456 | abbrev = DEFAULT_ABBREV; |
Jonas Fonseca | 44de0da | 2006-02-18 02:10:53 +0100 | [diff] [blame] | 457 | if (arg[7] == '=') |
| 458 | abbrev = strtoul(arg + 8, NULL, 10); |
Junio C Hamano | 1dc4fb8 | 2006-01-26 00:48:19 -0800 | [diff] [blame] | 459 | if (abbrev < MINIMUM_ABBREV) |
| 460 | abbrev = MINIMUM_ABBREV; |
| 461 | else if (40 <= abbrev) |
| 462 | abbrev = 40; |
Junio C Hamano | d501250 | 2006-01-25 01:35:38 -0800 | [diff] [blame] | 463 | continue; |
| 464 | } |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 465 | if (!strcmp(arg, "--sq")) { |
| 466 | output_sq = 1; |
| 467 | continue; |
| 468 | } |
Linus Torvalds | 042a4ed | 2005-06-26 11:34:30 -0700 | [diff] [blame] | 469 | if (!strcmp(arg, "--not")) { |
| 470 | show_type ^= REVERSED; |
| 471 | continue; |
| 472 | } |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 473 | if (!strcmp(arg, "--symbolic")) { |
Junio C Hamano | a6d97d4 | 2008-01-05 12:09:55 -0800 | [diff] [blame] | 474 | symbolic = SHOW_SYMBOLIC_ASIS; |
| 475 | continue; |
| 476 | } |
| 477 | if (!strcmp(arg, "--symbolic-full-name")) { |
| 478 | symbolic = SHOW_SYMBOLIC_FULL; |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 479 | continue; |
| 480 | } |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 481 | if (!strcmp(arg, "--all")) { |
Junio C Hamano | cb5d709 | 2006-09-20 21:47:42 -0700 | [diff] [blame] | 482 | for_each_ref(show_reference, NULL); |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 483 | continue; |
| 484 | } |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 485 | if (!strcmp(arg, "--branches")) { |
Junio C Hamano | cb5d709 | 2006-09-20 21:47:42 -0700 | [diff] [blame] | 486 | for_each_branch_ref(show_reference, NULL); |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 487 | continue; |
| 488 | } |
| 489 | if (!strcmp(arg, "--tags")) { |
Junio C Hamano | cb5d709 | 2006-09-20 21:47:42 -0700 | [diff] [blame] | 490 | for_each_tag_ref(show_reference, NULL); |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 491 | continue; |
| 492 | } |
| 493 | if (!strcmp(arg, "--remotes")) { |
Junio C Hamano | cb5d709 | 2006-09-20 21:47:42 -0700 | [diff] [blame] | 494 | for_each_remote_ref(show_reference, NULL); |
Sean | a62be77 | 2006-05-13 21:43:00 -0400 | [diff] [blame] | 495 | continue; |
| 496 | } |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 497 | if (!strcmp(arg, "--show-prefix")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 498 | if (prefix) |
| 499 | puts(prefix); |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 500 | continue; |
| 501 | } |
Junio C Hamano | 5f94c73 | 2005-12-22 22:35:38 -0800 | [diff] [blame] | 502 | if (!strcmp(arg, "--show-cdup")) { |
| 503 | const char *pfx = prefix; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 504 | if (!is_inside_work_tree()) { |
| 505 | const char *work_tree = |
| 506 | get_git_work_tree(); |
| 507 | if (work_tree) |
| 508 | printf("%s\n", work_tree); |
| 509 | continue; |
| 510 | } |
Junio C Hamano | 5f94c73 | 2005-12-22 22:35:38 -0800 | [diff] [blame] | 511 | while (pfx) { |
| 512 | pfx = strchr(pfx, '/'); |
| 513 | if (pfx) { |
| 514 | pfx++; |
| 515 | printf("../"); |
| 516 | } |
| 517 | } |
| 518 | putchar('\n'); |
| 519 | continue; |
| 520 | } |
Linus Torvalds | a8783ee | 2005-09-18 11:18:30 -0700 | [diff] [blame] | 521 | if (!strcmp(arg, "--git-dir")) { |
| 522 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); |
| 523 | static char cwd[PATH_MAX]; |
| 524 | if (gitdir) { |
| 525 | puts(gitdir); |
| 526 | continue; |
| 527 | } |
| 528 | if (!prefix) { |
| 529 | puts(".git"); |
| 530 | continue; |
| 531 | } |
| 532 | if (!getcwd(cwd, PATH_MAX)) |
| 533 | die("unable to get current working directory"); |
| 534 | printf("%s/.git\n", cwd); |
| 535 | continue; |
| 536 | } |
Johannes Schindelin | 6d9ba67 | 2007-01-23 13:30:20 +0100 | [diff] [blame] | 537 | if (!strcmp(arg, "--is-inside-git-dir")) { |
| 538 | printf("%s\n", is_inside_git_dir() ? "true" |
| 539 | : "false"); |
| 540 | continue; |
| 541 | } |
Matthias Lederhofer | 892c41b | 2007-06-06 09:10:42 +0200 | [diff] [blame] | 542 | if (!strcmp(arg, "--is-inside-work-tree")) { |
| 543 | printf("%s\n", is_inside_work_tree() ? "true" |
| 544 | : "false"); |
| 545 | continue; |
| 546 | } |
Matthias Lederhofer | 493c774 | 2007-06-03 16:46:36 +0200 | [diff] [blame] | 547 | if (!strcmp(arg, "--is-bare-repository")) { |
| 548 | printf("%s\n", is_bare_repository() ? "true" |
| 549 | : "false"); |
| 550 | continue; |
| 551 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 552 | if (!prefixcmp(arg, "--since=")) { |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 553 | show_datestring("--max-age=", arg+8); |
| 554 | continue; |
| 555 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 556 | if (!prefixcmp(arg, "--after=")) { |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 557 | show_datestring("--max-age=", arg+8); |
| 558 | continue; |
| 559 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 560 | if (!prefixcmp(arg, "--before=")) { |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 561 | show_datestring("--min-age=", arg+9); |
| 562 | continue; |
| 563 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 564 | if (!prefixcmp(arg, "--until=")) { |
Linus Torvalds | c1babb1 | 2005-09-20 14:13:24 -0700 | [diff] [blame] | 565 | show_datestring("--min-age=", arg+8); |
| 566 | continue; |
| 567 | } |
Linus Torvalds | 9523a4c | 2006-02-05 11:58:34 -0800 | [diff] [blame] | 568 | if (show_flag(arg) && verify) |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 569 | die_no_single_rev(quiet); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 570 | continue; |
| 571 | } |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 572 | |
| 573 | /* Not a flag argument */ |
Santi BĂ©jar | 3dd4e73 | 2006-07-04 11:02:22 +0200 | [diff] [blame] | 574 | if (try_difference(arg)) |
| 575 | continue; |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 576 | name = arg; |
| 577 | type = NORMAL; |
| 578 | if (*arg == '^') { |
| 579 | name++; |
| 580 | type = REVERSED; |
Linus Torvalds | 800644c | 2005-06-20 08:29:13 -0700 | [diff] [blame] | 581 | } |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 582 | if (!get_sha1(name, sha1)) { |
| 583 | if (verify) |
| 584 | revs_count++; |
| 585 | else |
| 586 | show_rev(type, sha1, name); |
Linus Torvalds | 800644c | 2005-06-20 08:29:13 -0700 | [diff] [blame] | 587 | continue; |
| 588 | } |
Christian Couder | 75ecfce | 2008-04-26 15:19:29 +0200 | [diff] [blame] | 589 | if (verify) |
| 590 | die_no_single_rev(quiet); |
Linus Torvalds | 9ad0a93 | 2006-02-05 21:41:47 -0800 | [diff] [blame] | 591 | as_is = 1; |
| 592 | if (!show_file(arg)) |
| 593 | continue; |
Linus Torvalds | e23d0b4 | 2006-04-26 10:15:54 -0700 | [diff] [blame] | 594 | verify_filename(prefix, arg); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 595 | } |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 596 | if (verify) { |
| 597 | if (revs_count == 1) { |
| 598 | show_rev(type, sha1, name); |
| 599 | return 0; |
| 600 | } else if (revs_count == 0 && show_default()) |
| 601 | return 0; |
Christian Couder | b1b3596 | 2008-04-26 13:57:23 +0200 | [diff] [blame] | 602 | die_no_single_rev(quiet); |
Christian Couder | dfd1b74 | 2008-05-11 18:28:25 +0200 | [diff] [blame] | 603 | } else |
| 604 | show_default(); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 605 | return 0; |
| 606 | } |