builtin/describe: introduce --broken flag
[gitweb.git] / builtin / describe.c
index 738e68f95b1bccafeff6d1c58f4245dbd8265ad8..45adbf67d5a065b0f55eeab6524563a0a7951c6c 100644 (file)
@@ -9,6 +9,7 @@
 #include "diff.h"
 #include "hashmap.h"
 #include "argv-array.h"
+#include "run-command.h"
 
 #define SEEN           (1u << 0)
 #define MAX_TAGS       (FLAG_BITS - 1)
@@ -28,9 +29,10 @@ static int abbrev = -1; /* unspecified */
 static int max_candidates = 10;
 static struct hashmap names;
 static int have_util;
-static const char *pattern;
+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 *dirty;
+static const char *suffix, *dirty, *broken;
 
 /* diff-index command arguments to check if working tree is dirty. */
 static const char *diff_index_args[] = {
@@ -129,9 +131,40 @@ static int get_name(const char *path, const struct object_id *oid, int flag, voi
        if (!all && !is_tag)
                return 0;
 
-       /* Accept only tags that match the pattern, if given */
-       if (pattern && (!is_tag || wildmatch(pattern, path + 10, 0, NULL)))
-               return 0;
+       /*
+        * If we're given exclude patterns, first exclude any tag which match
+        * any of the exclude pattern.
+        */
+       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, NULL))
+                               return 0;
+               }
+       }
+
+       /*
+        * If we're given patterns, accept only tags which match at least one
+        * pattern.
+        */
+       if (patterns.nr) {
+               struct string_list_item *item;
+
+               if (!is_tag)
+                       return 0;
+
+               for_each_string_list_item(item, &patterns) {
+                       if (!wildmatch(item->string, path + 10, 0, NULL))
+                               break;
+
+                       /* If we get here, no pattern matched. */
+                       return 0;
+               }
+       }
 
        /* Is it annotated? */
        if (!peel_ref(path, peeled.hash)) {
@@ -260,8 +293,8 @@ static void describe(const char *arg, int last_one)
                display_name(n);
                if (longformat)
                        show_suffix(0, n->tag ? &n->tag->tagged->oid : &oid);
-               if (dirty)
-                       printf("%s", dirty);
+               if (suffix)
+                       printf("%s", suffix);
                printf("\n");
                return;
        }
@@ -337,8 +370,8 @@ static void describe(const char *arg, int last_one)
                struct object_id *oid = &cmit->object.oid;
                if (always) {
                        printf("%s", find_unique_abbrev(oid->hash, abbrev));
-                       if (dirty)
-                               printf("%s", dirty);
+                       if (suffix)
+                               printf("%s", suffix);
                        printf("\n");
                        return;
                }
@@ -381,8 +414,8 @@ static void describe(const char *arg, int last_one)
        display_name(all_matches[0].name);
        if (abbrev)
                show_suffix(all_matches[0].depth, &cmit->object.oid);
-       if (dirty)
-               printf("%s", dirty);
+       if (suffix)
+               printf("%s", suffix);
        printf("\n");
 
        if (!last_one)
@@ -404,13 +437,18 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                            N_("only output exact matches"), 0),
                OPT_INTEGER(0, "candidates", &max_candidates,
                            N_("consider <n> most recent tags (default: 10)")),
-               OPT_STRING(0, "match",       &pattern, N_("pattern"),
+               OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
                           N_("only consider tags matching <pattern>")),
+               OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"),
+                          N_("do not consider tags matching <pattern>")),
                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"},
+               {OPTION_STRING, 0, "broken",  &broken, N_("mark"),
+                       N_("append <mark> on broken working tree (default: \"-broken\")"),
+                       PARSE_OPT_OPTARG, NULL, (intptr_t) "-broken"},
                OPT_END(),
        };
 
@@ -430,6 +468,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                die(_("--long is incompatible with --abbrev=0"));
 
        if (contains) {
+               struct string_list_item *item;
                struct argv_array args;
 
                argv_array_init(&args);
@@ -440,8 +479,10 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                        argv_array_push(&args, "--always");
                if (!all) {
                        argv_array_push(&args, "--tags");
-                       if (pattern)
-                               argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
+                       for_each_string_list_item(item, &patterns)
+                               argv_array_pushf(&args, "--refs=refs/tags/%s", item->string);
+                       for_each_string_list_item(item, &exclude_patterns)
+                               argv_array_pushf(&args, "--exclude=refs/tags/%s", item->string);
                }
                if (argc)
                        argv_array_pushv(&args, argv);
@@ -456,7 +497,28 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
                die(_("No names found, cannot describe anything."));
 
        if (argc == 0) {
-               if (dirty) {
+               if (broken) {
+                       struct child_process cp = CHILD_PROCESS_INIT;
+                       argv_array_pushv(&cp.args, diff_index_args);
+                       cp.git_cmd = 1;
+                       cp.no_stdin = 1;
+                       cp.no_stdout = 1;
+
+                       if (!dirty)
+                               dirty = "-dirty";
+
+                       switch (run_command(&cp)) {
+                       case 0:
+                               suffix = NULL;
+                               break;
+                       case 1:
+                               suffix = dirty;
+                               break;
+                       default:
+                               /* diff-index aborted abnormally */
+                               suffix = broken;
+                       }
+               } else if (dirty) {
                        static struct lock_file index_lock;
                        int fd;
 
@@ -469,11 +531,15 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 
                        if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
                                            diff_index_args, prefix))
-                               dirty = NULL;
+                               suffix = NULL;
+                       else
+                               suffix = dirty;
                }
                describe("HEAD", 1);
        } else if (dirty) {
                die(_("--dirty is incompatible with commit-ishes"));
+       } else if (broken) {
+               die(_("--broken is incompatible with commit-ishes"));
        } else {
                while (argc-- > 0)
                        describe(*argv++, argc == 0);