p5551: add a script to test fetch pack-dir rescans
[gitweb.git] / builtin / rev-parse.c
index ed8819b4516413d1597e3a3673a8eec343c82903..7f965fe74e5e98496522eddb3ef87fa831265e17 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "config.h"
 #include "commit.h"
 #include "refs.h"
 #include "quote.h"
@@ -12,6 +13,7 @@
 #include "diff.h"
 #include "revision.h"
 #include "split-index.h"
+#include "submodule.h"
 
 #define DO_REVS                1
 #define DO_NOREV       2
@@ -120,7 +122,7 @@ static void show_with_type(int type, const char *arg)
 }
 
 /* Output a revision, only if filter allows it */
-static void show_rev(int type, const unsigned char *sha1, const char *name)
+static void show_rev(int type, const struct object_id *oid, const char *name)
 {
        if (!(filter & DO_REVS))
                return;
@@ -128,10 +130,10 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
 
        if ((symbolic || abbrev_ref) && name) {
                if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
-                       unsigned char discard[20];
+                       struct object_id discard;
                        char *full;
 
-                       switch (dwim_ref(name, strlen(name), discard, &full)) {
+                       switch (dwim_ref(name, strlen(name), discard.hash, &full)) {
                        case 0:
                                /*
                                 * Not found -- not a ref.  We could
@@ -157,9 +159,9 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
                }
        }
        else if (abbrev)
-               show_with_type(type, find_unique_abbrev(sha1, abbrev));
+               show_with_type(type, find_unique_abbrev(oid->hash, abbrev));
        else
-               show_with_type(type, sha1_to_hex(sha1));
+               show_with_type(type, oid_to_hex(oid));
 }
 
 /* Output a flag, only if filter allows it. */
@@ -179,11 +181,11 @@ static int show_default(void)
        const char *s = def;
 
        if (s) {
-               unsigned char sha1[20];
+               struct object_id oid;
 
                def = NULL;
-               if (!get_sha1(s, sha1)) {
-                       show_rev(NORMAL, sha1, s);
+               if (!get_oid(s, &oid)) {
+                       show_rev(NORMAL, &oid, s);
                        return 1;
                }
        }
@@ -194,31 +196,32 @@ static int show_reference(const char *refname, const struct object_id *oid, int
 {
        if (ref_excluded(ref_excludes, refname))
                return 0;
-       show_rev(NORMAL, oid->hash, refname);
+       show_rev(NORMAL, oid, refname);
        return 0;
 }
 
 static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
 {
-       show_rev(REVERSED, oid->hash, refname);
+       show_rev(REVERSED, oid, refname);
        return 0;
 }
 
-static int show_abbrev(const unsigned char *sha1, void *cb_data)
+static int show_abbrev(const struct object_id *oid, void *cb_data)
 {
-       show_rev(NORMAL, sha1, NULL);
+       show_rev(NORMAL, oid, NULL);
        return 0;
 }
 
 static void show_datestring(const char *flag, const char *datestr)
 {
-       static char buffer[100];
+       char *buffer;
 
        /* date handling requires both flags and revs */
        if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
                return;
-       snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
+       buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
        show(buffer);
+       free(buffer);
 }
 
 static int show_file(const char *arg, int output_prefix)
@@ -227,9 +230,9 @@ static int show_file(const char *arg, int output_prefix)
        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
                if (output_prefix) {
                        const char *prefix = startup_info->prefix;
-                       show(prefix_filename(prefix,
-                                            prefix ? strlen(prefix) : 0,
-                                            arg));
+                       char *fname = prefix_filename(prefix, arg);
+                       show(fname);
+                       free(fname);
                } else
                        show(arg);
                return 1;
@@ -240,8 +243,8 @@ static int show_file(const char *arg, int output_prefix)
 static int try_difference(const char *arg)
 {
        char *dotdot;
-       unsigned char sha1[20];
-       unsigned char end[20];
+       struct object_id oid;
+       struct object_id end;
        const char *next;
        const char *this;
        int symmetric;
@@ -271,18 +274,18 @@ static int try_difference(const char *arg)
                return 0;
        }
 
-       if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) {
-               show_rev(NORMAL, end, next);
-               show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
+       if (!get_sha1_committish(this, oid.hash) && !get_sha1_committish(next, end.hash)) {
+               show_rev(NORMAL, &end, next);
+               show_rev(symmetric ? NORMAL : REVERSED, &oid, this);
                if (symmetric) {
                        struct commit_list *exclude;
                        struct commit *a, *b;
-                       a = lookup_commit_reference(sha1);
-                       b = lookup_commit_reference(end);
+                       a = lookup_commit_reference(&oid);
+                       b = lookup_commit_reference(&end);
                        exclude = get_merge_bases(a, b);
                        while (exclude) {
                                struct commit *commit = pop_commit(&exclude);
-                               show_rev(REVERSED, commit->object.oid.hash, NULL);
+                               show_rev(REVERSED, &commit->object.oid, NULL);
                        }
                }
                *dotdot = '.';
@@ -295,7 +298,7 @@ static int try_difference(const char *arg)
 static int try_parent_shorthands(const char *arg)
 {
        char *dotdot;
-       unsigned char sha1[20];
+       struct object_id oid;
        struct commit *commit;
        struct commit_list *parents;
        int parent_number;
@@ -325,12 +328,12 @@ static int try_parent_shorthands(const char *arg)
                return 0;
 
        *dotdot = 0;
-       if (get_sha1_committish(arg, sha1)) {
+       if (get_sha1_committish(arg, oid.hash)) {
                *dotdot = '^';
                return 0;
        }
 
-       commit = lookup_commit_reference(sha1);
+       commit = lookup_commit_reference(&oid);
        if (exclude_parent &&
            exclude_parent > commit_list_count(commit->parents)) {
                *dotdot = '^';
@@ -338,7 +341,7 @@ static int try_parent_shorthands(const char *arg)
        }
 
        if (include_rev)
-               show_rev(NORMAL, sha1, arg);
+               show_rev(NORMAL, &oid, arg);
        for (parents = commit->parents, parent_number = 1;
             parents;
             parents = parents->next, parent_number++) {
@@ -350,7 +353,7 @@ static int try_parent_shorthands(const char *arg)
                if (symbolic)
                        name = xstrfmt("%s^%d", arg, parent_number);
                show_rev(include_parents ? NORMAL : REVERSED,
-                        parents->item->object.oid.hash, name);
+                        &parents->item->object.oid, name);
                free(name);
        }
 
@@ -569,10 +572,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
        int did_repo_setup = 0;
        int has_dashdash = 0;
        int output_prefix = 0;
-       unsigned char sha1[20];
+       struct object_id oid;
        unsigned int flags = 0;
        const char *name = NULL;
        struct object_context unused;
+       struct strbuf buf = STRBUF_INIT;
 
        if (argc > 1 && !strcmp("--parseopt", argv[1]))
                return cmd_parseopt(argc - 1, argv + 1, prefix);
@@ -627,7 +631,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                if (!strcmp(arg, "--git-path")) {
                        if (!argv[i + 1])
                                die("--git-path requires an argument");
-                       puts(git_path("%s", argv[i + 1]));
+                       strbuf_reset(&buf);
+                       puts(relative_path(git_path("%s", argv[i + 1]),
+                                          prefix, &buf));
                        i++;
                        continue;
                }
@@ -751,8 +757,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--bisect")) {
-                               for_each_ref_in("refs/bisect/bad", show_reference, NULL);
-                               for_each_ref_in("refs/bisect/good", anti_reference, NULL);
+                               for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
+                               for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
                                continue;
                        }
                        if (opt_with_value(arg, "--branches", &arg)) {
@@ -781,6 +787,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                        puts(work_tree);
                                continue;
                        }
+                       if (!strcmp(arg, "--show-superproject-working-tree")) {
+                               const char *superproject = get_superproject_working_tree();
+                               if (superproject)
+                                       puts(superproject);
+                               continue;
+                       }
                        if (!strcmp(arg, "--show-prefix")) {
                                if (prefix)
                                        puts(prefix);
@@ -807,17 +819,27 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                putchar('\n');
                                continue;
                        }
-                       if (!strcmp(arg, "--git-dir")) {
+                       if (!strcmp(arg, "--git-dir") ||
+                           !strcmp(arg, "--absolute-git-dir")) {
                                const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
                                char *cwd;
                                int len;
-                               if (gitdir) {
-                                       puts(gitdir);
-                                       continue;
-                               }
-                               if (!prefix) {
-                                       puts(".git");
-                                       continue;
+                               if (arg[2] == 'g') {    /* --git-dir */
+                                       if (gitdir) {
+                                               puts(gitdir);
+                                               continue;
+                                       }
+                                       if (!prefix) {
+                                               puts(".git");
+                                               continue;
+                                       }
+                               } else {                /* --absolute-git-dir */
+                                       if (!gitdir && !prefix)
+                                               gitdir = ".git";
+                                       if (gitdir) {
+                                               puts(real_path(gitdir));
+                                               continue;
+                                       }
                                }
                                cwd = xgetcwd();
                                len = strlen(cwd);
@@ -826,8 +848,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--git-common-dir")) {
-                               const char *pfx = prefix ? prefix : "";
-                               puts(prefix_filename(pfx, strlen(pfx), get_git_common_dir()));
+                               strbuf_reset(&buf);
+                               puts(relative_path(get_git_common_dir(),
+                                                  prefix, &buf));
                                continue;
                        }
                        if (!strcmp(arg, "--is-inside-git-dir")) {
@@ -850,7 +873,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                        die(_("Could not read the index"));
                                if (the_index.split_index) {
                                        const unsigned char *sha1 = the_index.split_index->base_sha1;
-                                       puts(git_path("sharedindex.%s", sha1_to_hex(sha1)));
+                                       const char *path = git_path("sharedindex.%s", sha1_to_hex(sha1));
+                                       strbuf_reset(&buf);
+                                       puts(relative_path(path, prefix, &buf));
                                }
                                continue;
                        }
@@ -886,11 +911,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        name++;
                        type = REVERSED;
                }
-               if (!get_sha1_with_context(name, flags, sha1, &unused)) {
+               if (!get_sha1_with_context(name, flags, oid.hash, &unused)) {
                        if (verify)
                                revs_count++;
                        else
-                               show_rev(type, sha1, name);
+                               show_rev(type, &oid, name);
                        continue;
                }
                if (verify)
@@ -902,9 +927,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        continue;
                verify_filename(prefix, arg, 1);
        }
+       strbuf_release(&buf);
        if (verify) {
                if (revs_count == 1) {
-                       show_rev(type, sha1, name);
+                       show_rev(type, &oid, name);
                        return 0;
                } else if (revs_count == 0 && show_default())
                        return 0;