From 7a48b8321934384098a8392b3c4c814ec5058d00 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Mon, 18 Nov 2013 23:09:10 +0530 Subject: for-each-ref: introduce %(HEAD) asterisk marker 'git branch' shows which branch you are currently on with an '*', but 'git for-each-ref' misses this feature. So, extend its format with %(HEAD) for the same effect. Now you can use the following format in for-each-ref: %(HEAD) %(refname:short) to display an asterisk next to the current ref. Signed-off-by: Ramkumar Ramachandra Signed-off-by: Junio C Hamano --- builtin/for-each-ref.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'builtin/for-each-ref.c') diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 1d4083c2d..5f1842f48 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -75,6 +75,7 @@ static struct { { "upstream" }, { "symref" }, { "flag" }, + { "HEAD" }, }; /* @@ -675,8 +676,16 @@ static void populate_value(struct refinfo *ref) v->s = xstrdup(buf + 1); } continue; - } - else + } else if (!strcmp(name, "HEAD")) { + const char *head; + unsigned char sha1[20]; + head = resolve_ref_unsafe("HEAD", sha1, 1, NULL); + if (!strcmp(ref->refname, head)) + v->s = "*"; + else + v->s = " "; + continue; + } else continue; formatp = strchr(name, ':'); -- cgit v1.2.1 From b28061ce0d2e31d390fee5a5724b8a061d0492a7 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Mon, 18 Nov 2013 23:09:11 +0530 Subject: for-each-ref: introduce %(upstream:track[short]) Introduce %(upstream:track) to display "[ahead M, behind N]" and %(upstream:trackshort) to display "=", ">", "<", or "<>" appropriately (inspired by contrib/completion/git-prompt.sh). Now you can use the following format in for-each-ref: %(refname:short)%(upstream:trackshort) to display refs with terse tracking information. Note that :track and :trackshort only work with "upstream", and error out when used with anything else. Signed-off-by: Ramkumar Ramachandra Signed-off-by: Junio C Hamano --- builtin/for-each-ref.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'builtin/for-each-ref.c') diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 5f1842f48..9e45abd40 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -641,6 +641,7 @@ static void populate_value(struct refinfo *ref) int deref = 0; const char *refname; const char *formatp; + struct branch *branch = NULL; if (*name == '*') { deref = 1; @@ -652,7 +653,6 @@ static void populate_value(struct refinfo *ref) else if (!prefixcmp(name, "symref")) refname = ref->symref ? ref->symref : ""; else if (!prefixcmp(name, "upstream")) { - struct branch *branch; /* only local branches may have an upstream */ if (prefixcmp(ref->refname, "refs/heads/")) continue; @@ -679,6 +679,7 @@ static void populate_value(struct refinfo *ref) } else if (!strcmp(name, "HEAD")) { const char *head; unsigned char sha1[20]; + head = resolve_ref_unsafe("HEAD", sha1, 1, NULL); if (!strcmp(ref->refname, head)) v->s = "*"; @@ -689,13 +690,46 @@ static void populate_value(struct refinfo *ref) continue; formatp = strchr(name, ':'); - /* look for "short" refname format */ if (formatp) { + int num_ours, num_theirs; + formatp++; if (!strcmp(formatp, "short")) refname = shorten_unambiguous_ref(refname, warn_ambiguous_refs); - else + else if (!strcmp(formatp, "track") && + !prefixcmp(name, "upstream")) { + char buf[40]; + + stat_tracking_info(branch, &num_ours, &num_theirs); + 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); + } + continue; + } else if (!strcmp(formatp, "trackshort") && + !prefixcmp(name, "upstream")) { + assert(branch); + stat_tracking_info(branch, &num_ours, &num_theirs); + 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", (int)(formatp - name), name, formatp); } -- cgit v1.2.1 From fddb74c94777351d549b2ddaa36612c41b2176f1 Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Mon, 18 Nov 2013 23:09:12 +0530 Subject: for-each-ref: introduce %(color:...) for color Enhance 'git for-each-ref' with color formatting options. You can now use the following format in for-each-ref: %(color:green)%(refname:short)%(color:reset) where color names are described in color.branch.*. Signed-off-by: Ramkumar Ramachandra Signed-off-by: Junio C Hamano --- builtin/for-each-ref.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'builtin/for-each-ref.c') diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 9e45abd40..bc655b504 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -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" }, }; /* @@ -662,8 +664,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"); -- cgit v1.2.1 From db64eb655b48ea2c635480c6cc992b0156817aeb Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Mon, 18 Nov 2013 23:09:13 +0530 Subject: for-each-ref: avoid color leakage To make sure that an invocation like the following doesn't leak color, $ git for-each-ref --format='%(subject)%(color:green)' auto-reset at the end of the format string when the last color token seen in the format string isn't a color-reset. Signed-off-by: Ramkumar Ramachandra Signed-off-by: Junio C Hamano --- builtin/for-each-ref.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'builtin/for-each-ref.c') diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index bc655b504..5ff51d1d3 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -93,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 @@ -179,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; } @@ -914,11 +923,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); @@ -985,15 +992,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'); } -- cgit v1.2.1