Merge branch 'jk/remove-deprecated'
[gitweb.git] / builtin / for-each-ref.c
index 9e45abd40a72049881b585d9cee127f75958ab09..875bd813d5ed6d09622fc3473fdb574e9714bf7f 100644 (file)
@@ -9,6 +9,7 @@
 #include "quote.h"
 #include "parse-options.h"
 #include "remote.h"
+#include "color.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -76,6 +77,7 @@ static struct {
        { "symref" },
        { "flag" },
        { "HEAD" },
+       { "color" },
 };
 
 /*
@@ -91,6 +93,7 @@ static struct {
 static const char **used_atom;
 static cmp_type *used_atom_type;
 static int used_atom_cnt, sort_atom_limit, need_tagged, need_symref;
+static int need_color_reset_at_eol;
 
 /*
  * Used to parse format string and sort specifiers
@@ -177,13 +180,21 @@ static const char *find_next(const char *cp)
 static int verify_format(const char *format)
 {
        const char *cp, *sp;
+       static const char color_reset[] = "color:reset";
+
+       need_color_reset_at_eol = 0;
        for (cp = format; *cp && (sp = find_next(cp)); ) {
                const char *ep = strchr(sp, ')');
+               int at;
+
                if (!ep)
                        return error("malformed format string %s", sp);
                /* sp points at "%(" and ep points at the closing ")" */
-               parse_atom(sp + 2, ep);
+               at = parse_atom(sp + 2, ep);
                cp = ep + 1;
+
+               if (!memcmp(used_atom[at], "color:", 6))
+                       need_color_reset_at_eol = !!strcmp(used_atom[at], color_reset);
        }
        return 0;
 }
@@ -206,6 +217,22 @@ static void *get_obj(const unsigned char *sha1, struct object **obj, unsigned lo
        return buf;
 }
 
+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;
+               return 1;
+       }
+       if (!strcmp(name, "objectname:short")) {
+               v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
+               return 1;
+       }
+       return 0;
+}
+
 /* See grab_values */
 static void grab_common_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
 {
@@ -226,15 +253,8 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
                        v->ul = sz;
                        v->s = s;
                }
-               else if (!strcmp(name, "objectname")) {
-                       char *s = xmalloc(41);
-                       strcpy(s, sha1_to_hex(obj->sha1));
-                       v->s = s;
-               }
-               else if (!strcmp(name, "objectname:short")) {
-                       v->s = xstrdup(find_unique_abbrev(obj->sha1,
-                                                         DEFAULT_ABBREV));
-               }
+               else if (deref)
+                       grab_objectname(name, obj->sha1, v);
        }
 }
 
@@ -662,8 +682,13 @@ static void populate_value(struct refinfo *ref)
                            !branch->merge[0]->dst)
                                continue;
                        refname = branch->merge[0]->dst;
-               }
-               else if (!strcmp(name, "flag")) {
+               } else if (!prefixcmp(name, "color:")) {
+                       char color[COLOR_MAXLEN] = "";
+
+                       color_parse(name + 6, "--format", color);
+                       v->s = xstrdup(color);
+                       continue;
+               } else if (!strcmp(name, "flag")) {
                        char buf[256], *cp = buf;
                        if (ref->flag & REF_ISSYMREF)
                                cp = copy_advance(cp, ",symref");
@@ -676,6 +701,8 @@ static void populate_value(struct refinfo *ref)
                                v->s = xstrdup(buf + 1);
                        }
                        continue;
+               } else if (!deref && grab_objectname(name, ref->objectname, v)) {
+                       continue;
                } else if (!strcmp(name, "HEAD")) {
                        const char *head;
                        unsigned char sha1[20];
@@ -907,11 +934,9 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs
        qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
 }
 
-static void print_value(struct refinfo *ref, int atom, int quote_style)
+static void print_value(struct atom_value *v, int quote_style)
 {
-       struct atom_value *v;
        struct strbuf sb = STRBUF_INIT;
-       get_value(ref, atom, &v);
        switch (quote_style) {
        case QUOTE_NONE:
                fputs(v->s, stdout);
@@ -978,15 +1003,26 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
        const char *cp, *sp, *ep;
 
        for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
+               struct atom_value *atomv;
+
                ep = strchr(sp, ')');
                if (cp < sp)
                        emit(cp, sp);
-               print_value(info, parse_atom(sp + 2, ep), quote_style);
+               get_value(info, parse_atom(sp + 2, ep), &atomv);
+               print_value(atomv, quote_style);
        }
        if (*cp) {
                sp = cp + strlen(cp);
                emit(cp, sp);
        }
+       if (need_color_reset_at_eol) {
+               struct atom_value resetv;
+               char color[COLOR_MAXLEN] = "";
+
+               color_parse("reset", "--format", color);
+               resetv.s = color;
+               print_value(&resetv, quote_style);
+       }
        putchar('\n');
 }