X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=builtin-rev-parse.c;h=a7860ed75ac4a7b1895bfe088e34a39b24448f57;hb=6e1c23442a0315ad440bb8457703dcf1ad943b96;hp=0e597073239c97160582fbb7047794796f67987f;hpb=e4b9c36ca40a0869ee5fc64aaeabaf2655ef595b;p=git.git diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 0e5970732..a7860ed75 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -28,8 +28,6 @@ static int symbolic; static int abbrev; static int output_sq; -static int revs_count; - /* * Some arguments are relevant "revision" arguments, * others are about output format or other details. @@ -96,16 +94,21 @@ static void show(const char *arg) puts(arg); } +/* Like show(), but with a negation prefix according to type */ +static void show_with_type(int type, const char *arg) +{ + if (type != show_type) + putchar('^'); + show(arg); +} + /* Output a revision, only if filter allows it */ static void show_rev(int type, const unsigned char *sha1, const char *name) { if (!(filter & DO_REVS)) return; def = NULL; - revs_count++; - if (type != show_type) - putchar('^'); if (symbolic && name) { if (symbolic == SHOW_SYMBOLIC_FULL) { unsigned char discard[20]; @@ -122,20 +125,20 @@ static void show_rev(int type, const unsigned char *sha1, const char *name) */ break; case 1: /* happy */ - show(full); + show_with_type(type, full); break; default: /* ambiguous */ error("refname '%s' is ambiguous", name); break; } } else { - show(name); + show_with_type(type, name); } } else if (abbrev) - show(find_unique_abbrev(sha1, abbrev)); + show_with_type(type, find_unique_abbrev(sha1, abbrev)); else - show(sha1_to_hex(sha1)); + show_with_type(type, sha1_to_hex(sha1)); } /* Output a flag, only if filter allows it. */ @@ -150,7 +153,7 @@ static int show_flag(const char *arg) return 0; } -static void show_default(void) +static int show_default(void) { const char *s = def; @@ -160,9 +163,10 @@ static void show_default(void) def = NULL; if (!get_sha1(s, sha1)) { show_rev(NORMAL, sha1, s); - return; + return 1; } } + return 0; } static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data) @@ -375,14 +379,15 @@ static void die_no_single_rev(int quiet) int cmd_rev_parse(int argc, const char **argv, const char *prefix) { - int i, as_is = 0, verify = 0, quiet = 0; + int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; unsigned char sha1[20]; + const char *name = NULL; if (argc > 1 && !strcmp("--parseopt", argv[1])) return cmd_parseopt(argc - 1, argv + 1, prefix); prefix = setup_git_directory(); - git_config(git_default_config); + git_config(git_default_config, NULL); for (i = 1; i < argc; i++) { const char *arg = argv[i]; @@ -568,12 +573,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) /* Not a flag argument */ if (try_difference(arg)) continue; - if (!get_sha1(arg, sha1)) { - show_rev(NORMAL, sha1, arg); - continue; + name = arg; + type = NORMAL; + if (*arg == '^') { + name++; + type = REVERSED; } - if (*arg == '^' && !get_sha1(arg+1, sha1)) { - show_rev(REVERSED, sha1, arg+1); + if (!get_sha1(name, sha1)) { + if (verify) + revs_count++; + else + show_rev(type, sha1, name); continue; } if (verify) @@ -583,8 +593,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; verify_filename(prefix, arg); } - show_default(); - if (verify && revs_count != 1) + if (verify) { + if (revs_count == 1) { + show_rev(type, sha1, name); + return 0; + } else if (revs_count == 0 && show_default()) + return 0; die_no_single_rev(quiet); + } else + show_default(); return 0; }