Merge branch 'rt/placeholder-in-usage'
[gitweb.git] / ref-filter.c
index f046d826bde8474db2f249f5bf06d2833728841d..1194f10ed60f2bb476e1d95d0cc11b4ad4265c7f 100644 (file)
@@ -11,6 +11,8 @@
 #include "ref-filter.h"
 #include "revision.h"
 #include "utf8.h"
+#include "git-compat-util.h"
+#include "version.h"
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
@@ -45,9 +47,6 @@ static struct {
        { "subject" },
        { "body" },
        { "contents" },
-       { "contents:subject" },
-       { "contents:body" },
-       { "contents:signature" },
        { "upstream" },
        { "push" },
        { "symref" },
@@ -65,6 +64,11 @@ struct align {
        unsigned int width;
 };
 
+struct contents {
+       unsigned int lines;
+       struct object_id oid;
+};
+
 struct ref_formatting_stack {
        struct ref_formatting_stack *prev;
        struct strbuf output;
@@ -81,6 +85,7 @@ struct atom_value {
        const char *s;
        union {
                struct align align;
+               struct contents contents;
        } u;
        void (*handler)(struct atom_value *atomv, struct ref_formatting_state *state);
        unsigned long ul; /* used for sorting when not FIELD_STR */
@@ -338,9 +343,7 @@ static int grab_objectname(const char *name, const unsigned char *sha1,
                            struct atom_value *v)
 {
        if (!strcmp(name, "objectname")) {
-               char *s = xmalloc(41);
-               strcpy(s, sha1_to_hex(sha1));
-               v->s = s;
+               v->s = xstrdup(sha1_to_hex(sha1));
                return 1;
        }
        if (!strcmp(name, "objectname:short")) {
@@ -365,10 +368,8 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
                if (!strcmp(name, "objecttype"))
                        v->s = typename(obj->type);
                else if (!strcmp(name, "objectsize")) {
-                       char *s = xmalloc(40);
-                       sprintf(s, "%lu", sz);
                        v->ul = sz;
-                       v->s = s;
+                       v->s = xstrfmt("%lu", sz);
                }
                else if (deref)
                        grab_objectname(name, obj->sha1, v);
@@ -392,11 +393,8 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob
                        v->s = tag->tag;
                else if (!strcmp(name, "type") && tag->tagged)
                        v->s = typename(tag->tagged->type);
-               else if (!strcmp(name, "object") && tag->tagged) {
-                       char *s = xmalloc(41);
-                       strcpy(s, sha1_to_hex(tag->tagged->sha1));
-                       v->s = s;
-               }
+               else if (!strcmp(name, "object") && tag->tagged)
+                       v->s = xstrdup(sha1_to_hex(tag->tagged->sha1));
        }
 }
 
@@ -414,32 +412,22 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
                if (deref)
                        name++;
                if (!strcmp(name, "tree")) {
-                       char *s = xmalloc(41);
-                       strcpy(s, sha1_to_hex(commit->tree->object.sha1));
-                       v->s = s;
+                       v->s = xstrdup(sha1_to_hex(commit->tree->object.sha1));
                }
-               if (!strcmp(name, "numparent")) {
-                       char *s = xmalloc(40);
+               else if (!strcmp(name, "numparent")) {
                        v->ul = commit_list_count(commit->parents);
-                       sprintf(s, "%lu", v->ul);
-                       v->s = s;
+                       v->s = xstrfmt("%lu", v->ul);
                }
                else if (!strcmp(name, "parent")) {
-                       int num = commit_list_count(commit->parents);
-                       int i;
                        struct commit_list *parents;
-                       char *s = xmalloc(41 * num + 1);
-                       v->s = s;
-                       for (i = 0, parents = commit->parents;
-                            parents;
-                            parents = parents->next, i = i + 41) {
+                       struct strbuf s = STRBUF_INIT;
+                       for (parents = commit->parents; parents; parents = parents->next) {
                                struct commit *parent = parents->item;
-                               strcpy(s+i, sha1_to_hex(parent->object.sha1));
-                               if (parents->next)
-                                       s[i+40] = ' ';
+                               if (parents != commit->parents)
+                                       strbuf_addch(&s, ' ');
+                               strbuf_addstr(&s, sha1_to_hex(parent->object.sha1));
                        }
-                       if (!i)
-                               *s = '\0';
+                       v->s = strbuf_detach(&s, NULL);
                }
        }
 }
@@ -643,6 +631,30 @@ static void find_subpos(const char *buf, unsigned long sz,
        *nonsiglen = *sig - buf;
 }
 
+/*
+ * If 'lines' is greater than 0, append that many lines from the given
+ * 'buf' of length 'size' to the given strbuf.
+ */
+static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
+{
+       int i;
+       const char *sp, *eol;
+       size_t len;
+
+       sp = buf;
+
+       for (i = 0; i < lines && sp < buf + size; i++) {
+               if (i)
+                       strbuf_addstr(out, "\n    ");
+               eol = memchr(sp, '\n', size - (sp - buf));
+               len = eol ? eol - sp : size - (sp - buf);
+               strbuf_add(out, sp, len);
+               if (!eol)
+                       break;
+               sp = eol + 1;
+       }
+}
+
 /* See grab_values */
 static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 {
@@ -653,6 +665,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
        for (i = 0; i < used_atom_cnt; i++) {
                const char *name = used_atom[i];
                struct atom_value *v = &val[i];
+               const char *valp = NULL;
                if (!!deref != (*name == '*'))
                        continue;
                if (deref)
@@ -662,7 +675,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                    strcmp(name, "contents") &&
                    strcmp(name, "contents:subject") &&
                    strcmp(name, "contents:body") &&
-                   strcmp(name, "contents:signature"))
+                   strcmp(name, "contents:signature") &&
+                   !starts_with(name, "contents:lines="))
                        continue;
                if (!subpos)
                        find_subpos(buf, sz,
@@ -682,6 +696,16 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                        v->s = xmemdupz(sigpos, siglen);
                else if (!strcmp(name, "contents"))
                        v->s = xstrdup(subpos);
+               else if (skip_prefix(name, "contents:lines=", &valp)) {
+                       struct strbuf s = STRBUF_INIT;
+                       const char *contents_end = bodylen + bodypos - siglen;
+
+                       if (strtoul_ui(valp, 10, &v->u.contents.lines))
+                               die(_("positive value expected contents:lines=%s"), valp);
+                       /*  Size is the length of the message after removing the signature */
+                       append_lines(&s, subpos, contents_end - subpos, v->u.contents.lines);
+                       v->s = strbuf_detach(&s, NULL);
+               }
        }
 }
 
@@ -893,7 +917,6 @@ static void populate_value(struct ref_array_item *ref)
                        else if (!strcmp(formatp, "track") &&
                                 (starts_with(name, "upstream") ||
                                  starts_with(name, "push"))) {
-                               char buf[40];
 
                                if (stat_tracking_info(branch, &num_ours,
                                                       &num_theirs, NULL))
@@ -901,17 +924,13 @@ static void populate_value(struct ref_array_item *ref)
 
                                if (!num_ours && !num_theirs)
                                        v->s = "";
-                               else if (!num_ours) {
-                                       sprintf(buf, "[behind %d]", num_theirs);
-                                       v->s = xstrdup(buf);
-                               } else if (!num_theirs) {
-                                       sprintf(buf, "[ahead %d]", num_ours);
-                                       v->s = xstrdup(buf);
-                               } else {
-                                       sprintf(buf, "[ahead %d, behind %d]",
-                                               num_ours, num_theirs);
-                                       v->s = xstrdup(buf);
-                               }
+                               else if (!num_ours)
+                                       v->s = xstrfmt("[behind %d]", num_theirs);
+                               else if (!num_theirs)
+                                       v->s = xstrfmt("[ahead %d]", num_ours);
+                               else
+                                       v->s = xstrfmt("[ahead %d, behind %d]",
+                                                      num_ours, num_theirs);
                                continue;
                        } else if (!strcmp(formatp, "trackshort") &&
                                   (starts_with(name, "upstream") ||
@@ -938,12 +957,8 @@ static void populate_value(struct ref_array_item *ref)
 
                if (!deref)
                        v->s = refname;
-               else {
-                       int len = strlen(refname);
-                       char *s = xmalloc(len + 4);
-                       sprintf(s, "%s^{}", refname);
-                       v->s = s;
-               }
+               else
+                       v->s = xstrfmt("%s^{}", refname);
        }
 
        for (i = 0; i < used_atom_cnt; i++) {
@@ -1118,11 +1133,35 @@ static int commit_contains(struct ref_filter *filter, struct commit *commit)
        return is_descendant_of(commit, filter->with_commit);
 }
 
+/*
+ * Return 1 if the refname matches one of the patterns, otherwise 0.
+ * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
+ * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
+ * matches "refs/heads/mas*", too).
+ */
+static int match_pattern(const char **patterns, const char *refname)
+{
+       /*
+        * When no '--format' option is given we need to skip the prefix
+        * for matching refs of tags and branches.
+        */
+       (void)(skip_prefix(refname, "refs/tags/", &refname) ||
+              skip_prefix(refname, "refs/heads/", &refname) ||
+              skip_prefix(refname, "refs/remotes/", &refname) ||
+              skip_prefix(refname, "refs/", &refname));
+
+       for (; *patterns; patterns++) {
+               if (!wildmatch(*patterns, refname, 0, NULL))
+                       return 1;
+       }
+       return 0;
+}
+
 /*
  * Return 1 if the refname matches one of the patterns, otherwise 0.
  * A pattern can be path prefix (e.g. a refname "refs/heads/master"
- * matches a pattern "refs/heads/") or a wildcard (e.g. the same ref
- * matches "refs/heads/m*",too).
+ * matches a pattern "refs/heads/" but not "refs/heads/m") or a
+ * wildcard (e.g. the same ref matches "refs/heads/m*", too).
  */
 static int match_name_as_path(const char **pattern, const char *refname)
 {
@@ -1143,6 +1182,16 @@ static int match_name_as_path(const char **pattern, const char *refname)
        return 0;
 }
 
+/* Return 1 if the refname matches one of the patterns, otherwise 0. */
+static int filter_pattern_match(struct ref_filter *filter, const char *refname)
+{
+       if (!*filter->name_patterns)
+               return 1; /* No pattern always matches */
+       if (filter->match_as_path)
+               return match_name_as_path(filter->name_patterns, refname);
+       return match_pattern(filter->name_patterns, refname);
+}
+
 /*
  * Given a ref (sha1, refname), check if the ref belongs to the array
  * of sha1s. If the given ref is a tag, check if the given tag points
@@ -1245,7 +1294,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
        if (!(kind & filter->kind))
                return 0;
 
-       if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
+       if (!filter_pattern_match(filter, refname))
                return 0;
 
        if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
@@ -1256,7 +1305,7 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
         * obtain the commit using the 'oid' available and discard all
         * non-commits early. The actual filtering is done later.
         */
-       if (filter->merge_commit || filter->with_commit) {
+       if (filter->merge_commit || filter->with_commit || filter->verbose) {
                commit = lookup_commit_reference_gently(oid->hash, 1);
                if (!commit)
                        return 0;
@@ -1400,19 +1449,19 @@ static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, stru
 
        get_ref_atom_value(a, s->atom, &va);
        get_ref_atom_value(b, s->atom, &vb);
-       switch (cmp_type) {
-       case FIELD_STR:
+       if (s->version)
+               cmp = versioncmp(va->s, vb->s);
+       else if (cmp_type == FIELD_STR)
                cmp = strcmp(va->s, vb->s);
-               break;
-       default:
+       else {
                if (va->ul < vb->ul)
                        cmp = -1;
                else if (va->ul == vb->ul)
                        cmp = 0;
                else
                        cmp = 1;
-               break;
        }
+
        return (s->reverse) ? -cmp : cmp;
 }
 
@@ -1545,6 +1594,9 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
                s->reverse = 1;
                arg++;
        }
+       if (skip_prefix(arg, "version:", &arg) ||
+           skip_prefix(arg, "v:", &arg))
+               s->version = 1;
        len = strlen(arg);
        s->atom = parse_ref_filter_atom(arg, arg+len);
        return 0;