]> asedeno.scripts.mit.edu Git - git.git/blobdiff - builtin-rev-parse.c
Merge branch 'maint'
[git.git] / builtin-rev-parse.c
index a7860ed75ac4a7b1895bfe088e34a39b24448f57..22c6d6ad161f0de7dee88336a81a8f1f873c0bb0 100644 (file)
@@ -26,6 +26,8 @@ static int show_type = NORMAL;
 #define SHOW_SYMBOLIC_FULL 2
 static int symbolic;
 static int abbrev;
+static int abbrev_ref;
+static int abbrev_ref_strict;
 static int output_sq;
 
 /*
@@ -109,8 +111,8 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
                return;
        def = NULL;
 
-       if (symbolic && name) {
-               if (symbolic == SHOW_SYMBOLIC_FULL) {
+       if ((symbolic || abbrev_ref) && name) {
+               if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
                        unsigned char discard[20];
                        char *full;
 
@@ -125,6 +127,9 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
                                 */
                                break;
                        case 1: /* happy */
+                               if (abbrev_ref)
+                                       full = shorten_unambiguous_ref(full,
+                                               abbrev_ref_strict);
                                show_with_type(type, full);
                                break;
                        default: /* ambiguous */
@@ -241,6 +246,36 @@ static int try_difference(const char *arg)
        return 0;
 }
 
+static int try_parent_shorthands(const char *arg)
+{
+       char *dotdot;
+       unsigned char sha1[20];
+       struct commit *commit;
+       struct commit_list *parents;
+       int parents_only;
+
+       if ((dotdot = strstr(arg, "^!")))
+               parents_only = 0;
+       else if ((dotdot = strstr(arg, "^@")))
+               parents_only = 1;
+
+       if (!dotdot || dotdot[2])
+               return 0;
+
+       *dotdot = 0;
+       if (get_sha1(arg, sha1))
+               return 0;
+
+       if (!parents_only)
+               show_rev(NORMAL, sha1, arg);
+       commit = lookup_commit_reference(sha1);
+       for (parents = commit->parents; parents; parents = parents->next)
+               show_rev(parents_only ? NORMAL : REVERSED,
+                               parents->item->object.sha1, arg);
+
+       return 1;
+}
+
 static int parseopt_dump(const struct option *o, const char *arg, int unset)
 {
        struct strbuf *parsed = o->value;
@@ -268,7 +303,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 {
        static int keep_dashdash = 0;
        static char const * const parseopt_usage[] = {
-               "git-rev-parse --parseopt [options] -- [<args>...]",
+               "git rev-parse --parseopt [options] -- [<args>...]",
                NULL
        };
        static struct option parseopt_opts[] = {
@@ -277,19 +312,17 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
                OPT_END(),
        };
 
-       struct strbuf sb, parsed;
+       struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
        const char **usage = NULL;
        struct option *opts = NULL;
        int onb = 0, osz = 0, unb = 0, usz = 0;
 
-       strbuf_init(&parsed, 0);
        strbuf_addstr(&parsed, "set --");
        argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
                             PARSE_OPT_KEEP_DASHDASH);
        if (argc < 1 || strcmp(argv[0], "--"))
                usage_with_options(parseopt_usage, parseopt_opts);
 
-       strbuf_init(&sb, 0);
        /* get the usage up to the first line with a -- on it */
        for (;;) {
                if (strbuf_getline(&sb, stdin, '\n') == EOF)
@@ -478,6 +511,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                symbolic = SHOW_SYMBOLIC_FULL;
                                continue;
                        }
+                       if (!prefixcmp(arg, "--abbrev-ref") &&
+                           (!arg[12] || arg[12] == '=')) {
+                               abbrev_ref = 1;
+                               abbrev_ref_strict = warn_ambiguous_refs;
+                               if (arg[12] == '=') {
+                                       if (!strcmp(arg + 13, "strict"))
+                                               abbrev_ref_strict = 1;
+                                       else if (!strcmp(arg + 13, "loose"))
+                                               abbrev_ref_strict = 0;
+                                       else
+                                               die("unknown mode for %s", arg);
+                               }
+                               continue;
+                       }
                        if (!strcmp(arg, "--all")) {
                                for_each_ref(show_reference, NULL);
                                continue;
@@ -573,6 +620,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                /* Not a flag argument */
                if (try_difference(arg))
                        continue;
+               if (try_parent_shorthands(arg))
+                       continue;
                name = arg;
                type = NORMAL;
                if (*arg == '^') {