Merge branch 'nd/travis-gcc-8'
[gitweb.git] / builtin / describe.c
index 94ff2fba0b024dd351d65f11510cb07070084d4d..cf1ae77d7c705cdfb34bdb5403bad8db4148eccf 100644 (file)
@@ -3,16 +3,19 @@
 #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 "revision.h"
+#include "list-objects.h"
 
-#define SEEN           (1u << 0)
 #define MAX_TAGS       (FLAG_BITS - 1)
 
 static const char * const describe_usage[] = {
@@ -55,10 +58,13 @@ static const char *prio_names[] = {
 };
 
 static int commit_name_cmp(const void *unused_cmp_data,
-                          const struct commit_name *cn1,
-                          const struct commit_name *cn2,
+                          const void *entry,
+                          const void *entry_or_key,
                           const void *peeled)
 {
+       const struct commit_name *cn1 = entry;
+       const struct commit_name *cn2 = entry_or_key;
+
        return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
 }
 
@@ -126,13 +132,24 @@ static void add_to_known_names(const char *path,
 
 static int get_name(const char *path, const struct object_id *oid, int flag, void *cb_data)
 {
-       int is_tag = starts_with(path, "refs/tags/");
+       int is_tag = 0;
        struct object_id peeled;
        int is_annotated, prio;
-
-       /* Reject anything outside refs/tags/ unless --all */
-       if (!all && !is_tag)
+       const char *path_to_match = NULL;
+
+       if (skip_prefix(path, "refs/tags/", &path_to_match)) {
+               is_tag = 1;
+       } else if (all) {
+               if ((exclude_patterns.nr || patterns.nr) &&
+                   !skip_prefix(path, "refs/heads/", &path_to_match) &&
+                   !skip_prefix(path, "refs/remotes/", &path_to_match)) {
+                       /* Only accept reference of known type if there are match/exclude patterns */
+                       return 0;
+               }
+       } else {
+               /* Reject anything outside refs/tags/ unless --all */
                return 0;
+       }
 
        /*
         * If we're given exclude patterns, first exclude any tag which match
@@ -141,11 +158,8 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
        if (exclude_patterns.nr) {
                struct string_list_item *item;
 
-               if (!is_tag)
-                       return 0;
-
                for_each_string_list_item(item, &exclude_patterns) {
-                       if (!wildmatch(item->string, path + 10, 0))
+                       if (!wildmatch(item->string, path_to_match, 0))
                                return 0;
                }
        }
@@ -158,11 +172,8 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
                int found = 0;
                struct string_list_item *item;
 
-               if (!is_tag)
-                       return 0;
-
                for_each_string_list_item(item, &patterns) {
-                       if (!wildmatch(item->string, path + 10, 0)) {
+                       if (!wildmatch(item->string, path_to_match, 0)) {
                                found = 1;
                                break;
                        }
@@ -173,7 +184,7 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
        }
 
        /* Is it annotated? */
-       if (!peel_ref(path, peeled.hash)) {
+       if (!peel_ref(path, &peeled)) {
                is_annotated = !!oidcmp(oid, &peeled);
        } else {
                oidcpy(&peeled, oid);
@@ -248,7 +259,7 @@ 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);
@@ -263,20 +274,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;
@@ -285,30 +298,25 @@ 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(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;
@@ -373,22 +381,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);
@@ -426,15 +433,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 (!oidcmp(&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);
+
+       init_revisions(&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(&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)
@@ -506,9 +584,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                return cmd_name_rev(args.argc, args.argv, prefix);
        }
 
-       hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
+       hashmap_init(&names, commit_name_cmp, NULL, 0);
        for_each_rawref(get_name, NULL);
-       if (!names.size && !always)
+       if (!hashmap_get_size(&names) && !always)
                die(_("No names found, cannot describe anything."));
 
        if (argc == 0) {
@@ -534,8 +612,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,
@@ -544,8 +624,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))
+                       init_revisions(&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;