Merge branch 'tb/avoid-gcc-on-darwin-10-6'
[gitweb.git] / ref-filter.c
index 797f9fe2d883d9568259507e394cfe290fc4adde..bc551a752c460cd4751034b3d69d7e9b5dfc775e 100644 (file)
@@ -37,6 +37,13 @@ static struct used_atom {
        union {
                char color[COLOR_MAXLEN];
                struct align align;
+               enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT }
+                       remote_ref;
+               struct {
+                       enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB } option;
+                       unsigned int nlines;
+               } contents;
+               enum { O_FULL, O_SHORT } objectname;
        } u;
 } *used_atom;
 static int used_atom_cnt, need_tagged, need_symref;
@@ -50,6 +57,62 @@ static void color_atom_parser(struct used_atom *atom, const char *color_value)
                die(_("unrecognized color: %%(color:%s)"), color_value);
 }
 
+static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (!arg)
+               atom->u.remote_ref = RR_NORMAL;
+       else if (!strcmp(arg, "short"))
+               atom->u.remote_ref = RR_SHORTEN;
+       else if (!strcmp(arg, "track"))
+               atom->u.remote_ref = RR_TRACK;
+       else if (!strcmp(arg, "trackshort"))
+               atom->u.remote_ref = RR_TRACKSHORT;
+       else
+               die(_("unrecognized format: %%(%s)"), atom->name);
+}
+
+static void body_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (arg)
+               die(_("%%(body) does not take arguments"));
+       atom->u.contents.option = C_BODY_DEP;
+}
+
+static void subject_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (arg)
+               die(_("%%(subject) does not take arguments"));
+       atom->u.contents.option = C_SUB;
+}
+
+static void contents_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (!arg)
+               atom->u.contents.option = C_BARE;
+       else if (!strcmp(arg, "body"))
+               atom->u.contents.option = C_BODY;
+       else if (!strcmp(arg, "signature"))
+               atom->u.contents.option = C_SIG;
+       else if (!strcmp(arg, "subject"))
+               atom->u.contents.option = C_SUB;
+       else if (skip_prefix(arg, "lines=", &arg)) {
+               atom->u.contents.option = C_LINES;
+               if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
+                       die(_("positive value expected contents:lines=%s"), arg);
+       } else
+               die(_("unrecognized %%(contents) argument: %s"), arg);
+}
+
+static void objectname_atom_parser(struct used_atom *atom, const char *arg)
+{
+       if (!arg)
+               atom->u.objectname = O_FULL;
+       else if (!strcmp(arg, "short"))
+               atom->u.objectname = O_SHORT;
+       else
+               die(_("unrecognized %%(objectname) argument: %s"), arg);
+}
+
 static align_type parse_align_position(const char *s)
 {
        if (!strcmp(s, "right"))
@@ -78,7 +141,15 @@ static void align_atom_parser(struct used_atom *atom, const char *arg)
                const char *s = params.items[i].string;
                int position;
 
-               if (!strtoul_ui(s, 10, &width))
+               if (skip_prefix(s, "position=", &s)) {
+                       position = parse_align_position(s);
+                       if (position < 0)
+                               die(_("unrecognized position:%s"), s);
+                       align->position = position;
+               } else if (skip_prefix(s, "width=", &s)) {
+                       if (strtoul_ui(s, 10, &width))
+                               die(_("unrecognized width:%s"), s);
+               } else if (!strtoul_ui(s, 10, &width))
                        ;
                else if ((position = parse_align_position(s)) >= 0)
                        align->position = position;
@@ -100,7 +171,7 @@ static struct {
        { "refname" },
        { "objecttype" },
        { "objectsize", FIELD_ULONG },
-       { "objectname" },
+       { "objectname", FIELD_STR, objectname_atom_parser },
        { "tree" },
        { "parent" },
        { "numparent", FIELD_ULONG },
@@ -121,11 +192,11 @@ static struct {
        { "taggerdate", FIELD_TIME },
        { "creator" },
        { "creatordate", FIELD_TIME },
-       { "subject" },
-       { "body" },
-       { "contents" },
-       { "upstream" },
-       { "push" },
+       { "subject", FIELD_STR, subject_atom_parser },
+       { "body", FIELD_STR, body_atom_parser },
+       { "contents", FIELD_STR, contents_atom_parser },
+       { "upstream", FIELD_STR, remote_ref_atom_parser },
+       { "push", FIELD_STR, remote_ref_atom_parser },
        { "symref" },
        { "flag" },
        { "HEAD" },
@@ -136,11 +207,6 @@ static struct {
 
 #define REF_FORMATTING_STATE_INIT  { 0, NULL }
 
-struct contents {
-       unsigned int lines;
-       struct object_id oid;
-};
-
 struct ref_formatting_stack {
        struct ref_formatting_stack *prev;
        struct strbuf output;
@@ -157,7 +223,6 @@ 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 */
@@ -176,7 +241,7 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        if (*sp == '*' && sp < ep)
                sp++; /* deref */
        if (ep <= sp)
-               die("malformed field name: %.*s", (int)(ep-atom), atom);
+               die(_("malformed field name: %.*s"), (int)(ep-atom), atom);
 
        /* Do we have the atom already used elsewhere? */
        for (i = 0; i < used_atom_cnt; i++) {
@@ -202,7 +267,7 @@ int parse_ref_filter_atom(const char *atom, const char *ep)
        }
 
        if (ARRAY_SIZE(valid_atom) <= i)
-               die("unknown field name: %.*s", (int)(ep-atom), atom);
+               die(_("unknown field name: %.*s"), (int)(ep-atom), atom);
 
        /* Add it in, including the deref prefix */
        at = used_atom_cnt;
@@ -356,7 +421,7 @@ int verify_ref_format(const char *format)
                int at;
 
                if (!ep)
-                       return error("malformed format string %s", sp);
+                       return error(_("malformed format string %s"), sp);
                /* sp points at "%(" and ep points at the closing ")" */
                at = parse_ref_filter_atom(sp + 2, ep);
                cp = ep + 1;
@@ -386,15 +451,17 @@ static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned lo
 }
 
 static int grab_objectname(const char *name, const unsigned char *sha1,
-                           struct atom_value *v)
+                          struct atom_value *v, struct used_atom *atom)
 {
-       if (!strcmp(name, "objectname")) {
-               v->s = xstrdup(sha1_to_hex(sha1));
-               return 1;
-       }
-       if (!strcmp(name, "objectname:short")) {
-               v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
-               return 1;
+       if (starts_with(name, "objectname")) {
+               if (atom->u.objectname == O_SHORT) {
+                       v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
+                       return 1;
+               } else if (atom->u.objectname == O_FULL) {
+                       v->s = xstrdup(sha1_to_hex(sha1));
+                       return 1;
+               } else
+                       die("BUG: unknown %%(objectname) option");
        }
        return 0;
 }
@@ -418,7 +485,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
                        v->s = xstrfmt("%lu", sz);
                }
                else if (deref)
-                       grab_objectname(name, obj->oid.hash, v);
+                       grab_objectname(name, obj->oid.hash, v, &used_atom[i]);
        }
 }
 
@@ -709,20 +776,16 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
        unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
 
        for (i = 0; i < used_atom_cnt; i++) {
-               const char *name = used_atom[i].name;
+               struct used_atom *atom = &used_atom[i];
+               const char *name = atom->name;
                struct atom_value *v = &val[i];
-               const char *valp = NULL;
                if (!!deref != (*name == '*'))
                        continue;
                if (deref)
                        name++;
                if (strcmp(name, "subject") &&
                    strcmp(name, "body") &&
-                   strcmp(name, "contents") &&
-                   strcmp(name, "contents:subject") &&
-                   strcmp(name, "contents:body") &&
-                   strcmp(name, "contents:signature") &&
-                   !starts_with(name, "contents:lines="))
+                   !starts_with(name, "contents"))
                        continue;
                if (!subpos)
                        find_subpos(buf, sz,
@@ -730,28 +793,23 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
                                    &bodypos, &bodylen, &nonsiglen,
                                    &sigpos, &siglen);
 
-               if (!strcmp(name, "subject"))
-                       v->s = copy_subject(subpos, sublen);
-               else if (!strcmp(name, "contents:subject"))
+               if (atom->u.contents.option == C_SUB)
                        v->s = copy_subject(subpos, sublen);
-               else if (!strcmp(name, "body"))
+               else if (atom->u.contents.option == C_BODY_DEP)
                        v->s = xmemdupz(bodypos, bodylen);
-               else if (!strcmp(name, "contents:body"))
+               else if (atom->u.contents.option == C_BODY)
                        v->s = xmemdupz(bodypos, nonsiglen);
-               else if (!strcmp(name, "contents:signature"))
+               else if (atom->u.contents.option == C_SIG)
                        v->s = xmemdupz(sigpos, siglen);
-               else if (!strcmp(name, "contents"))
-                       v->s = xstrdup(subpos);
-               else if (skip_prefix(name, "contents:lines=", &valp)) {
+               else if (atom->u.contents.option == C_LINES) {
                        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);
+                       append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
                        v->s = strbuf_detach(&s, NULL);
-               }
+               } else if (atom->u.contents.option == C_BARE)
+                       v->s = xstrdup(subpos);
        }
 }
 
@@ -817,12 +875,12 @@ static const char *strip_ref_components(const char *refname, const char *nr_arg)
        const char *start = refname;
 
        if (nr < 1 || *end != '\0')
-               die(":strip= requires a positive integer argument");
+               die(_(":strip= requires a positive integer argument"));
 
        while (remaining) {
                switch (*start++) {
                case '\0':
-                       die("ref '%s' does not have %ld components to :strip",
+                       die(_("ref '%s' does not have %ld components to :strip"),
                            refname, nr);
                case '/':
                        remaining--;
@@ -832,6 +890,43 @@ static const char *strip_ref_components(const char *refname, const char *nr_arg)
        return start;
 }
 
+static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
+                                   struct branch *branch, const char **s)
+{
+       int num_ours, num_theirs;
+       if (atom->u.remote_ref == RR_SHORTEN)
+               *s = shorten_unambiguous_ref(refname, warn_ambiguous_refs);
+       else if (atom->u.remote_ref == RR_TRACK) {
+               if (stat_tracking_info(branch, &num_ours,
+                                      &num_theirs, NULL))
+                       return;
+
+               if (!num_ours && !num_theirs)
+                       *s = "";
+               else if (!num_ours)
+                       *s = xstrfmt("[behind %d]", num_theirs);
+               else if (!num_theirs)
+                       *s = xstrfmt("[ahead %d]", num_ours);
+               else
+                       *s = xstrfmt("[ahead %d, behind %d]",
+                                    num_ours, num_theirs);
+       } else if (atom->u.remote_ref == RR_TRACKSHORT) {
+               if (stat_tracking_info(branch, &num_ours,
+                                      &num_theirs, NULL))
+                       return;
+
+               if (!num_ours && !num_theirs)
+                       *s = "=";
+               else if (!num_ours)
+                       *s = "<";
+               else if (!num_theirs)
+                       *s = ">";
+               else
+                       *s = "<>";
+       } else /* RR_NORMAL */
+               *s = refname;
+}
+
 /*
  * Parse the object referred by ref, and grab needed value.
  */
@@ -883,8 +978,9 @@ static void populate_value(struct ref_array_item *ref)
                        branch = branch_get(branch_name);
 
                        refname = branch_get_upstream(branch, NULL);
-                       if (!refname)
-                               continue;
+                       if (refname)
+                               fill_remote_ref_details(atom, refname, branch, &v->s);
+                       continue;
                } else if (starts_with(name, "push")) {
                        const char *branch_name;
                        if (!skip_prefix(ref->refname, "refs/heads/",
@@ -895,6 +991,8 @@ static void populate_value(struct ref_array_item *ref)
                        refname = branch_get_push(branch, NULL);
                        if (!refname)
                                continue;
+                       fill_remote_ref_details(atom, refname, branch, &v->s);
+                       continue;
                } else if (starts_with(name, "color:")) {
                        v->s = atom->u.color;
                        continue;
@@ -911,7 +1009,7 @@ static void populate_value(struct ref_array_item *ref)
                                v->s = xstrdup(buf + 1);
                        }
                        continue;
-               } else if (!deref && grab_objectname(name, ref->objectname, v)) {
+               } else if (!deref && grab_objectname(name, ref->objectname, v, atom)) {
                        continue;
                } else if (!strcmp(name, "HEAD")) {
                        const char *head;
@@ -936,7 +1034,6 @@ static void populate_value(struct ref_array_item *ref)
 
                formatp = strchr(name, ':');
                if (formatp) {
-                       int num_ours, num_theirs;
                        const char *arg;
 
                        formatp++;
@@ -945,44 +1042,8 @@ static void populate_value(struct ref_array_item *ref)
                                                      warn_ambiguous_refs);
                        else if (skip_prefix(formatp, "strip=", &arg))
                                refname = strip_ref_components(refname, arg);
-                       else if (!strcmp(formatp, "track") &&
-                                (starts_with(name, "upstream") ||
-                                 starts_with(name, "push"))) {
-
-                               if (stat_tracking_info(branch, &num_ours,
-                                                      &num_theirs, NULL))
-                                       continue;
-
-                               if (!num_ours && !num_theirs)
-                                       v->s = "";
-                               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") ||
-                                   starts_with(name, "push"))) {
-                               assert(branch);
-
-                               if (stat_tracking_info(branch, &num_ours,
-                                                       &num_theirs, NULL))
-                                       continue;
-
-                               if (!num_ours && !num_theirs)
-                                       v->s = "=";
-                               else if (!num_ours)
-                                       v->s = "<";
-                               else if (!num_theirs)
-                                       v->s = ">";
-                               else
-                                       v->s = "<>";
-                               continue;
-                       } else
-                               die("unknown %.*s format %s",
+                       else
+                               die(_("unknown %.*s format %s"),
                                    (int)(formatp - name), name, formatp);
                }
 
@@ -1002,10 +1063,10 @@ static void populate_value(struct ref_array_item *ref)
  need_obj:
        buf = get_obj(ref->objectname, &obj, &size, &eaten);
        if (!buf)
-               die("missing object %s for %s",
+               die(_("missing object %s for %s"),
                    sha1_to_hex(ref->objectname), ref->refname);
        if (!obj)
-               die("parse_object_buffer failed on %s for %s",
+               die(_("parse_object_buffer failed on %s for %s"),
                    sha1_to_hex(ref->objectname), ref->refname);
 
        grab_values(ref->value, 0, obj, buf, size);
@@ -1033,10 +1094,10 @@ static void populate_value(struct ref_array_item *ref)
         */
        buf = get_obj(tagged, &obj, &size, &eaten);
        if (!buf)
-               die("missing object %s for %s",
+               die(_("missing object %s for %s"),
                    sha1_to_hex(tagged), ref->refname);
        if (!obj)
-               die("parse_object_buffer failed on %s for %s",
+               die(_("parse_object_buffer failed on %s for %s"),
                    sha1_to_hex(tagged), ref->refname);
        grab_values(ref->value, 1, obj, buf, size);
        if (!eaten)
@@ -1260,10 +1321,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
                                                 const unsigned char *objectname,
                                                 int flag)
 {
-       size_t len = strlen(refname);
-       struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
-       memcpy(ref->refname, refname, len);
-       ref->refname[len] = '\0';
+       struct ref_array_item *ref;
+       FLEX_ALLOC_STR(ref, refname, refname);
        hashcpy(ref->objectname, objectname);
        ref->flag = flag;
 
@@ -1311,12 +1370,12 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
        unsigned int kind;
 
        if (flag & REF_BAD_NAME) {
-               warning("ignoring ref with broken name %s", refname);
+               warning(_("ignoring ref with broken name %s"), refname);
                return 0;
        }
 
        if (flag & REF_ISBROKEN) {
-               warning("ignoring broken ref %s", refname);
+               warning(_("ignoring broken ref %s"), refname);
                return 0;
        }