Merge branch 'np/lookup-object-hashing'
[gitweb.git] / builtin / name-rev.c
index 29a6f5608a56c628be536614cb09493e0faf8844..20fcf8c696700993617649da77e5bbf6b416498a 100644 (file)
@@ -307,25 +307,31 @@ static void name_rev_line(char *p, struct name_ref_data *data)
 int cmd_name_rev(int argc, const char **argv, const char *prefix)
 {
        struct object_array revs = OBJECT_ARRAY_INIT;
-       int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0;
+       int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
        struct name_ref_data data = { 0, 0, NULL };
        struct option opts[] = {
-               OPT_BOOLEAN(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
-               OPT_BOOLEAN(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
+               OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
+               OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
                OPT_STRING(0, "refs", &data.ref_filter, N_("pattern"),
                                   N_("only use refs matching <pattern>")),
                OPT_GROUP(""),
-               OPT_BOOLEAN(0, "all", &all, N_("list all commits reachable from all refs")),
-               OPT_BOOLEAN(0, "stdin", &transform_stdin, N_("read from stdin")),
-               OPT_BOOLEAN(0, "undefined", &allow_undefined, N_("allow to print `undefined` names")),
-               OPT_BOOLEAN(0, "always",     &always,
+               OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
+               OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
+               OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
+               OPT_BOOL(0, "always",     &always,
                           N_("show abbreviated commit object as fallback")),
+               {
+                       /* A Hidden OPT_BOOL */
+                       OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
+                       N_("dereference tags in the input (internal use)"),
+                       PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
+               },
                OPT_END(),
        };
 
        git_config(git_default_config, NULL);
        argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
-       if (!!all + !!transform_stdin + !!argc > 1) {
+       if (all + transform_stdin + !!argc > 1) {
                error("Specify either a list, or --all, not both!");
                usage_with_options(name_rev_usage, opts);
        }
@@ -334,7 +340,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 
        for (; argc; argc--, argv++) {
                unsigned char sha1[20];
-               struct object *o;
+               struct object *object;
                struct commit *commit;
 
                if (get_sha1(*argv, sha1)) {
@@ -343,17 +349,34 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
                        continue;
                }
 
-               o = deref_tag(parse_object(sha1), *argv, 0);
-               if (!o || o->type != OBJ_COMMIT) {
-                       fprintf(stderr, "Could not get commit for %s. Skipping.\n",
+               commit = NULL;
+               object = parse_object(sha1);
+               if (object) {
+                       struct object *peeled = deref_tag(object, *argv, 0);
+                       if (peeled && peeled->type == OBJ_COMMIT)
+                               commit = (struct commit *)peeled;
+               }
+
+               if (!object) {
+                       fprintf(stderr, "Could not get object for %s. Skipping.\n",
                                        *argv);
                        continue;
                }
 
-               commit = (struct commit *)o;
-               if (cutoff > commit->date)
-                       cutoff = commit->date;
-               add_object_array((struct object *)commit, *argv, &revs);
+               if (commit) {
+                       if (cutoff > commit->date)
+                               cutoff = commit->date;
+               }
+
+               if (peel_tag) {
+                       if (!commit) {
+                               fprintf(stderr, "Could not get commit for %s. Skipping.\n",
+                                       *argv);
+                               continue;
+                       }
+                       object = (struct object *)commit;
+               }
+               add_object_array(object, *argv, &revs);
        }
 
        if (cutoff)