shallow: automatically clean up shallow tempfiles
[gitweb.git] / builtin / describe.c
index 6636a68cd919ba227a10cfa39efcd5f4e77a801d..7db43dae1be95e86c21249d9150fc8553c367527 100644 (file)
@@ -7,12 +7,13 @@
 #include "parse-options.h"
 #include "diff.h"
 #include "hash.h"
+#include "argv-array.h"
 
-#define SEEN           (1u<<0)
+#define SEEN           (1u << 0)
 #define MAX_TAGS       (FLAG_BITS - 1)
 
 static const char * const describe_usage[] = {
-       N_("git describe [options] <committish>*"),
+       N_("git describe [options] <commit-ish>*"),
        N_("git describe [options] --dirty"),
        NULL
 };
@@ -21,6 +22,7 @@ static int debug;     /* Display lots of verbose info */
 static int all;        /* Any valid ref can be used */
 static int tags;       /* Allow lightweight tags */
 static int longformat;
+static int first_parent;
 static int abbrev = -1; /* unspecified */
 static int max_candidates = 10;
 static struct hash_table names;
@@ -34,7 +36,6 @@ static const char *diff_index_args[] = {
        "diff-index", "--quiet", "HEAD", "--", NULL
 };
 
-
 struct commit_name {
        struct commit_name *next;
        unsigned char peeled[20];
@@ -42,8 +43,9 @@ struct commit_name {
        unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
        unsigned name_checked:1;
        unsigned char sha1[20];
-       const char *path;
+       char *path;
 };
+
 static const char *prio_names[] = {
        "head", "lightweight", "annotated",
 };
@@ -126,18 +128,20 @@ static void add_to_known_names(const char *path,
                        } else {
                                e->next = NULL;
                        }
+                       e->path = NULL;
                }
                e->tag = tag;
                e->prio = prio;
                e->name_checked = 0;
                hashcpy(e->sha1, sha1);
-               e->path = path;
+               free(e->path);
+               e->path = xstrdup(path);
        }
 }
 
 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 {
-       int is_tag = !prefixcmp(path, "refs/tags/");
+       int is_tag = starts_with(path, "refs/tags/");
        unsigned char peeled[20];
        int is_annotated, prio;
 
@@ -336,6 +340,9 @@ static void describe(const char *arg, int last_one)
                                commit_list_insert_by_date(p, &list);
                        p->object.flags |= c->object.flags;
                        parents = parents->next;
+
+                       if (first_parent)
+                               break;
                }
        }
 
@@ -399,11 +406,12 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 {
        int contains = 0;
        struct option options[] = {
-               OPT_BOOLEAN(0, "contains",   &contains, N_("find the tag that comes after the commit")),
-               OPT_BOOLEAN(0, "debug",      &debug, N_("debug search strategy on stderr")),
-               OPT_BOOLEAN(0, "all",        &all, N_("use any ref")),
-               OPT_BOOLEAN(0, "tags",       &tags, N_("use any tag, even unannotated")),
-               OPT_BOOLEAN(0, "long",       &longformat, N_("always use long format")),
+               OPT_BOOL(0, "contains",   &contains, N_("find the tag that comes after the commit")),
+               OPT_BOOL(0, "debug",      &debug, N_("debug search strategy on stderr")),
+               OPT_BOOL(0, "all",        &all, N_("use any ref")),
+               OPT_BOOL(0, "tags",       &tags, N_("use any tag, even unannotated")),
+               OPT_BOOL(0, "long",       &longformat, N_("always use long format")),
+               OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
                OPT__ABBREV(&abbrev),
                OPT_SET_INT(0, "exact-match", &max_candidates,
                            N_("only output exact matches"), 0),
@@ -411,11 +419,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                            N_("consider <n> most recent tags (default: 10)")),
                OPT_STRING(0, "match",       &pattern, N_("pattern"),
                           N_("only consider tags matching <pattern>")),
-               OPT_BOOLEAN(0, "always",     &always,
-                          N_("show abbreviated commit object as fallback")),
+               OPT_BOOL(0, "always",        &always,
+                       N_("show abbreviated commit object as fallback")),
                {OPTION_STRING, 0, "dirty",  &dirty, N_("mark"),
-                          N_("append <mark> on dirty working tree (default: \"-dirty\")"),
-                PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
+                       N_("append <mark> on dirty working tree (default: \"-dirty\")"),
+                       PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
                OPT_END(),
        };
 
@@ -435,24 +443,24 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                die(_("--long is incompatible with --abbrev=0"));
 
        if (contains) {
-               const char **args = xmalloc((7 + argc) * sizeof(char *));
-               int i = 0;
-               args[i++] = "name-rev";
-               args[i++] = "--name-only";
-               args[i++] = "--no-undefined";
+               struct argv_array args;
+
+               argv_array_init(&args);
+               argv_array_pushl(&args, "name-rev",
+                                "--peel-tag", "--name-only", "--no-undefined",
+                                NULL);
                if (always)
-                       args[i++] = "--always";
+                       argv_array_push(&args, "--always");
                if (!all) {
-                       args[i++] = "--tags";
-                       if (pattern) {
-                               char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
-                               sprintf(s, "--refs=refs/tags/%s", pattern);
-                               args[i++] = s;
-                       }
+                       argv_array_push(&args, "--tags");
+                       if (pattern)
+                               argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
                }
-               memcpy(args + i, argv, argc * sizeof(char *));
-               args[i + argc] = NULL;
-               return cmd_name_rev(i + argc, args, prefix);
+               while (*argv) {
+                       argv_array_push(&args, *argv);
+                       argv++;
+               }
+               return cmd_name_rev(args.argc, args.argv, prefix);
        }
 
        init_hash(&names);
@@ -478,11 +486,10 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                }
                describe("HEAD", 1);
        } else if (dirty) {
-               die(_("--dirty is incompatible with committishes"));
+               die(_("--dirty is incompatible with commit-ishes"));
        } else {
-               while (argc-- > 0) {
+               while (argc-- > 0)
                        describe(*argv++, argc == 0);
-               }
        }
        return 0;
 }