Merge branch 'nd/status-refresh-progress'
[gitweb.git] / builtin / describe.c
index 29075dbd0f884513420b646cdb06d7c0b3d33643..c48c34e8667c36a42101fffe51bd96f1a4d36842 100644 (file)
@@ -3,18 +3,25 @@
 #include "lockfile.h"
 #include "commit.h"
 #include "tag.h"
+#include "blob.h"
 #include "refs.h"
 #include "builtin.h"
-#include "exec_cmd.h"
+#include "exec-cmd.h"
 #include "parse-options.h"
+#include "revision.h"
 #include "diff.h"
 #include "hashmap.h"
 #include "argv-array.h"
 #include "run-command.h"
+#include "object-store.h"
+#include "revision.h"
+#include "list-objects.h"
+#include "commit-slab.h"
 
-#define SEEN           (1u << 0)
 #define MAX_TAGS       (FLAG_BITS - 1)
 
+define_commit_slab(commit_names, struct commit_name *);
+
 static const char * const describe_usage[] = {
        N_("git describe [<options>] [<commit-ish>...]"),
        N_("git describe [<options>] --dirty"),
@@ -34,6 +41,7 @@ static struct string_list patterns = STRING_LIST_INIT_NODUP;
 static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP;
 static int always;
 static const char *suffix, *dirty, *broken;
+static struct commit_names commit_names;
 
 /* diff-index command arguments to check if working tree is dirty. */
 static const char *diff_index_args[] = {
@@ -54,7 +62,7 @@ static const char *prio_names[] = {
        N_("head"), N_("lightweight"), N_("annotated"),
 };
 
-static int commit_name_cmp(const void *unused_cmp_data,
+static int commit_name_neq(const void *unused_cmp_data,
                           const void *entry,
                           const void *entry_or_key,
                           const void *peeled)
@@ -62,7 +70,7 @@ static int commit_name_cmp(const void *unused_cmp_data,
        const struct commit_name *cn1 = entry;
        const struct commit_name *cn2 = entry_or_key;
 
-       return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
+       return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled);
 }
 
 static inline struct commit_name *find_commit_name(const struct object_id *peeled)
@@ -85,13 +93,13 @@ static int replace_name(struct commit_name *e,
                struct tag *t;
 
                if (!e->tag) {
-                       t = lookup_tag(&e->oid);
+                       t = lookup_tag(the_repository, &e->oid);
                        if (!t || parse_tag(t))
                                return 1;
                        e->tag = t;
                }
 
-               t = lookup_tag(oid);
+               t = lookup_tag(the_repository, oid);
                if (!t || parse_tag(t))
                        return 0;
                *tag = t;
@@ -181,8 +189,8 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
        }
 
        /* Is it annotated? */
-       if (!peel_ref(path, peeled.hash)) {
-               is_annotated = !!oidcmp(oid, &peeled);
+       if (!peel_ref(path, &peeled)) {
+               is_annotated = !oideq(oid, &peeled);
        } else {
                oidcpy(&peeled, oid);
                is_annotated = 0;
@@ -256,10 +264,10 @@ static unsigned long finish_depth_computation(
        return seen_commits;
 }
 
-static void display_name(struct commit_name *n)
+static void append_name(struct commit_name *n, struct strbuf *dst)
 {
        if (n->prio == 2 && !n->tag) {
-               n->tag = lookup_tag(&n->oid);
+               n->tag = lookup_tag(the_repository, &n->oid);
                if (!n->tag || parse_tag(n->tag))
                        die(_("annotated tag %s not available"), n->path);
        }
@@ -271,20 +279,22 @@ static void display_name(struct commit_name *n)
                n->name_checked = 1;
        }
 
-       if (n->tag)
-               printf("%s", n->tag->tag);
-       else
-               printf("%s", n->path);
+       if (n->tag) {
+               if (all)
+                       strbuf_addstr(dst, "tags/");
+               strbuf_addstr(dst, n->tag->tag);
+       } else {
+               strbuf_addstr(dst, n->path);
+       }
 }
 
-static void show_suffix(int depth, const struct object_id *oid)
+static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst)
 {
-       printf("-%d-g%s", depth, find_unique_abbrev(oid->hash, abbrev));
+       strbuf_addf(dst, "-%d-g%s", depth, find_unique_abbrev(oid, abbrev));
 }
 
-static void describe(const char *arg, int last_one)
+static void describe_commit(struct object_id *oid, struct strbuf *dst)
 {
-       struct object_id oid;
        struct commit *cmit, *gave_up_on = NULL;
        struct commit_list *list;
        struct commit_name *n;
@@ -293,39 +303,38 @@ static void describe(const char *arg, int last_one)
        unsigned long seen_commits = 0;
        unsigned int unannotated_cnt = 0;
 
-       if (get_oid(arg, &oid))
-               die(_("Not a valid object name %s"), arg);
-       cmit = lookup_commit_reference(&oid);
-       if (!cmit)
-               die(_("%s is not a valid '%s' object"), arg, commit_type);
+       cmit = lookup_commit_reference(the_repository, oid);
 
        n = find_commit_name(&cmit->object.oid);
        if (n && (tags || all || n->prio == 2)) {
                /*
                 * Exact match to an existing ref.
                 */
-               display_name(n);
+               append_name(n, dst);
                if (longformat)
-                       show_suffix(0, n->tag ? &n->tag->tagged->oid : &oid);
+                       append_suffix(0, n->tag ? &n->tag->tagged->oid : oid, dst);
                if (suffix)
-                       printf("%s", suffix);
-               printf("\n");
+                       strbuf_addstr(dst, suffix);
                return;
        }
 
        if (!max_candidates)
                die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid));
        if (debug)
-               fprintf(stderr, _("searching to describe %s\n"), arg);
+               fprintf(stderr, _("No exact match on refs or tags, searching to describe\n"));
 
        if (!have_util) {
                struct hashmap_iter iter;
                struct commit *c;
-               struct commit_name *n = hashmap_iter_first(&names, &iter);
+               struct commit_name *n;
+
+               init_commit_names(&commit_names);
+               n = hashmap_iter_first(&names, &iter);
                for (; n; n = hashmap_iter_next(&iter)) {
-                       c = lookup_commit_reference_gently(&n->peeled, 1);
+                       c = lookup_commit_reference_gently(the_repository,
+                                                          &n->peeled, 1);
                        if (c)
-                               c->util = n;
+                               *commit_names_at(&commit_names, c) = n;
                }
                have_util = 1;
        }
@@ -336,8 +345,11 @@ static void describe(const char *arg, int last_one)
        while (list) {
                struct commit *c = pop_commit(&list);
                struct commit_list *parents = c->parents;
+               struct commit_name **slot;
+
                seen_commits++;
-               n = c->util;
+               slot = commit_names_peek(&commit_names, c);
+               n = slot ? *slot : NULL;
                if (n) {
                        if (!tags && !all && n->prio < 2) {
                                unannotated_cnt++;
@@ -381,22 +393,21 @@ static void describe(const char *arg, int last_one)
        }
 
        if (!match_cnt) {
-               struct object_id *oid = &cmit->object.oid;
+               struct object_id *cmit_oid = &cmit->object.oid;
                if (always) {
-                       printf("%s", find_unique_abbrev(oid->hash, abbrev));
+                       strbuf_add_unique_abbrev(dst, cmit_oid, abbrev);
                        if (suffix)
-                               printf("%s", suffix);
-                       printf("\n");
+                               strbuf_addstr(dst, suffix);
                        return;
                }
                if (unannotated_cnt)
                        die(_("No annotated tags can describe '%s'.\n"
                            "However, there were unannotated tags: try --tags."),
-                           oid_to_hex(oid));
+                           oid_to_hex(cmit_oid));
                else
                        die(_("No tags can describe '%s'.\n"
                            "Try --always, or create some tags."),
-                           oid_to_hex(oid));
+                           oid_to_hex(cmit_oid));
        }
 
        QSORT(all_matches, match_cnt, compare_pt);
@@ -434,15 +445,86 @@ static void describe(const char *arg, int last_one)
                }
        }
 
-       display_name(all_matches[0].name);
+       append_name(all_matches[0].name, dst);
        if (abbrev)
-               show_suffix(all_matches[0].depth, &cmit->object.oid);
+               append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
        if (suffix)
-               printf("%s", suffix);
-       printf("\n");
+               strbuf_addstr(dst, suffix);
+}
+
+struct process_commit_data {
+       struct object_id current_commit;
+       struct object_id looking_for;
+       struct strbuf *dst;
+       struct rev_info *revs;
+};
+
+static void process_commit(struct commit *commit, void *data)
+{
+       struct process_commit_data *pcd = data;
+       pcd->current_commit = commit->object.oid;
+}
+
+static void process_object(struct object *obj, const char *path, void *data)
+{
+       struct process_commit_data *pcd = data;
+
+       if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
+               reset_revision_walk();
+               describe_commit(&pcd->current_commit, pcd->dst);
+               strbuf_addf(pcd->dst, ":%s", path);
+               free_commit_list(pcd->revs->commits);
+               pcd->revs->commits = NULL;
+       }
+}
+
+static void describe_blob(struct object_id oid, struct strbuf *dst)
+{
+       struct rev_info revs;
+       struct argv_array args = ARGV_ARRAY_INIT;
+       struct process_commit_data pcd = { null_oid, oid, dst, &revs};
+
+       argv_array_pushl(&args, "internal: The first arg is not parsed",
+               "--objects", "--in-commit-order", "--reverse", "HEAD",
+               NULL);
+
+       repo_init_revisions(the_repository, &revs, NULL);
+       if (setup_revisions(args.argc, args.argv, &revs, NULL) > 1)
+               BUG("setup_revisions could not handle all args?");
+
+       if (prepare_revision_walk(&revs))
+               die("revision walk setup failed");
+
+       traverse_commit_list(&revs, process_commit, process_object, &pcd);
+       reset_revision_walk();
+}
+
+static void describe(const char *arg, int last_one)
+{
+       struct object_id oid;
+       struct commit *cmit;
+       struct strbuf sb = STRBUF_INIT;
+
+       if (debug)
+               fprintf(stderr, _("describe %s\n"), arg);
+
+       if (get_oid(arg, &oid))
+               die(_("Not a valid object name %s"), arg);
+       cmit = lookup_commit_reference_gently(the_repository, &oid, 1);
+
+       if (cmit)
+               describe_commit(&oid, &sb);
+       else if (oid_object_info(the_repository, &oid, NULL) == OBJ_BLOB)
+               describe_blob(oid, &sb);
+       else
+               die(_("%s is neither a commit nor blob"), arg);
+
+       puts(sb.buf);
 
        if (!last_one)
                clear_commit_marks(cmit, -1);
+
+       strbuf_release(&sb);
 }
 
 int cmd_describe(int argc, const char **argv, const char *prefix)
@@ -514,7 +596,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                return cmd_name_rev(args.argc, args.argv, prefix);
        }
 
-       hashmap_init(&names, commit_name_cmp, NULL, 0);
+       hashmap_init(&names, commit_name_neq, NULL, 0);
        for_each_rawref(get_name, NULL);
        if (!hashmap_get_size(&names) && !always)
                die(_("No names found, cannot describe anything."));
@@ -542,8 +624,10 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                                suffix = broken;
                        }
                } else if (dirty) {
-                       static struct lock_file index_lock;
-                       int fd;
+                       struct lock_file index_lock = LOCK_INIT;
+                       struct rev_info revs;
+                       struct argv_array args = ARGV_ARRAY_INIT;
+                       int fd, result;
 
                        read_cache_preload(NULL);
                        refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
@@ -552,8 +636,13 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                        if (0 <= fd)
                                update_index_if_able(&the_index, &index_lock);
 
-                       if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
-                                           diff_index_args, prefix))
+                       repo_init_revisions(the_repository, &revs, prefix);
+                       argv_array_pushv(&args, diff_index_args);
+                       if (setup_revisions(args.argc, args.argv, &revs, NULL) != 1)
+                               BUG("malformed internal diff-index command line");
+                       result = run_diff_index(&revs, 0);
+
+                       if (!diff_result_code(&revs.diffopt, result))
                                suffix = NULL;
                        else
                                suffix = dirty;