]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/rev-parse.c
Merge branch 'km/submodule-doc-use-sm-path' into maint
[thirdparty/git.git] / builtin / rev-parse.c
index 74aa644cbb37b5741c521eba53d0a040aaf1582e..7a00da820355b61c548449c55381577a2232a0e8 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) Linus Torvalds, 2005
  */
+#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "config.h"
 #include "commit.h"
@@ -14,6 +15,7 @@
 #include "revision.h"
 #include "split-index.h"
 #include "submodule.h"
+#include "commit-reach.h"
 
 #define DO_REVS                1
 #define DO_NOREV       2
@@ -159,7 +161,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
                }
        }
        else if (abbrev)
-               show_with_type(type, find_unique_abbrev(oid->hash, abbrev));
+               show_with_type(type, find_unique_abbrev(oid, abbrev));
        else
                show_with_type(type, oid_to_hex(oid));
 }
@@ -243,28 +245,28 @@ static int show_file(const char *arg, int output_prefix)
 static int try_difference(const char *arg)
 {
        char *dotdot;
-       struct object_id oid;
-       struct object_id end;
-       const char *next;
-       const char *this;
+       struct object_id start_oid;
+       struct object_id end_oid;
+       const char *end;
+       const char *start;
        int symmetric;
        static const char head_by_default[] = "HEAD";
 
        if (!(dotdot = strstr(arg, "..")))
                return 0;
-       next = dotdot + 2;
-       this = arg;
-       symmetric = (*next == '.');
+       end = dotdot + 2;
+       start = arg;
+       symmetric = (*end == '.');
 
        *dotdot = 0;
-       next += symmetric;
+       end += symmetric;
 
-       if (!*next)
-               next = head_by_default;
+       if (!*end)
+               end = head_by_default;
        if (dotdot == arg)
-               this = head_by_default;
+               start = head_by_default;
 
-       if (this == head_by_default && next == head_by_default &&
+       if (start == head_by_default && end == head_by_default &&
            !symmetric) {
                /*
                 * Just ".."?  That is not a range but the
@@ -274,14 +276,18 @@ static int try_difference(const char *arg)
                return 0;
        }
 
-       if (!get_oid_committish(this, &oid) && !get_oid_committish(next, &end)) {
-               show_rev(NORMAL, &end, next);
-               show_rev(symmetric ? NORMAL : REVERSED, &oid, this);
+       if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
+               show_rev(NORMAL, &end_oid, end);
+               show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
                if (symmetric) {
                        struct commit_list *exclude;
                        struct commit *a, *b;
-                       a = lookup_commit_reference(&oid);
-                       b = lookup_commit_reference(&end);
+                       a = lookup_commit_reference(the_repository, &start_oid);
+                       b = lookup_commit_reference(the_repository, &end_oid);
+                       if (!a || !b) {
+                               *dotdot = '.';
+                               return 0;
+                       }
                        exclude = get_merge_bases(a, b);
                        while (exclude) {
                                struct commit *commit = pop_commit(&exclude);
@@ -328,12 +334,12 @@ static int try_parent_shorthands(const char *arg)
                return 0;
 
        *dotdot = 0;
-       if (get_oid_committish(arg, &oid)) {
+       if (get_oid_committish(arg, &oid) ||
+           !(commit = lookup_commit_reference(the_repository, &oid))) {
                *dotdot = '^';
                return 0;
        }
 
-       commit = lookup_commit_reference(&oid);
        if (exclude_parent &&
            exclude_parent > commit_list_count(commit->parents)) {
                *dotdot = '^';
@@ -516,7 +522,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
                        PARSE_OPT_SHELL_EVAL);
 
        strbuf_addstr(&parsed, " --");
-       sq_quote_argv(&parsed, argv, 0);
+       sq_quote_argv(&parsed, argv);
        puts(parsed.buf);
        return 0;
 }
@@ -526,7 +532,7 @@ static int cmd_sq_quote(int argc, const char **argv)
        struct strbuf buf = STRBUF_INIT;
 
        if (argc)
-               sq_quote_argv(&buf, argv, 0);
+               sq_quote_argv(&buf, argv);
        printf("%s\n", buf.buf);
        strbuf_release(&buf);
 
@@ -587,6 +593,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
        const char *name = NULL;
        struct object_context unused;
        struct strbuf buf = STRBUF_INIT;
+       const int hexsz = the_hash_algo->hexsz;
 
        if (argc > 1 && !strcmp("--parseopt", argv[1]))
                return cmd_parseopt(argc - 1, argv + 1, prefix);
@@ -724,8 +731,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                abbrev = strtoul(arg, NULL, 10);
                                if (abbrev < MINIMUM_ABBREV)
                                        abbrev = MINIMUM_ABBREV;
-                               else if (40 <= abbrev)
-                                       abbrev = 40;
+                               else if (hexsz <= abbrev)
+                                       abbrev = hexsz;
                                continue;
                        }
                        if (!strcmp(arg, "--sq")) {
@@ -760,6 +767,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        }
                        if (!strcmp(arg, "--all")) {
                                for_each_ref(show_reference, NULL);
+                               clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (skip_prefix(arg, "--disambiguate=", &arg)) {
@@ -795,6 +803,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                const char *work_tree = get_git_work_tree();
                                if (work_tree)
                                        puts(work_tree);
+                               else
+                                       die("this operation must be run in a work tree");
                                continue;
                        }
                        if (!strcmp(arg, "--show-superproject-working-tree")) {
@@ -879,7 +889,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--is-shallow-repository")) {
-                               printf("%s\n", is_repository_shallow() ? "true"
+                               printf("%s\n",
+                                               is_repository_shallow(the_repository) ? "true"
                                                : "false");
                                continue;
                        }
@@ -887,8 +898,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                if (read_cache() < 0)
                                        die(_("Could not read the index"));
                                if (the_index.split_index) {
-                                       const unsigned char *sha1 = the_index.split_index->base_sha1;
-                                       const char *path = git_path("sharedindex.%s", sha1_to_hex(sha1));
+                                       const struct object_id *oid = &the_index.split_index->base_oid;
+                                       const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
                                        strbuf_reset(&buf);
                                        puts(relative_path(path, prefix, &buf));
                                }
@@ -910,6 +921,17 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                show_datestring("--min-age=", arg);
                                continue;
                        }
+                       if (opt_with_value(arg, "--show-object-format", &arg)) {
+                               const char *val = arg ? arg : "storage";
+
+                               if (strcmp(val, "storage") &&
+                                   strcmp(val, "input") &&
+                                   strcmp(val, "output"))
+                                       die("unknown mode for --show-object-format: %s",
+                                           arg);
+                               puts(the_hash_algo->name);
+                               continue;
+                       }
                        if (show_flag(arg) && verify)
                                die_no_single_rev(quiet);
                        continue;
@@ -926,7 +948,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        name++;
                        type = REVERSED;
                }
-               if (!get_oid_with_context(name, flags, &oid, &unused)) {
+               if (!get_oid_with_context(the_repository, name,
+                                         flags, &oid, &unused)) {
                        if (verify)
                                revs_count++;
                        else