ref-filter: add support for %(upstream:track,nobracket)

Add support for %(upstream:track,nobracket) which will print the tracking information without the brackets (i.e. "ahead N, behind M"). This is needed when we port branch.c to use ref-filter's printing APIs. Add test and documentation for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jan 10, 2017 at 14:19 UTC 7743fcca5be6854a1b9a5a0f7e60b79c15fee4b9
3 files changed +53 -26
Documentation/git-for-each-ref.txt
+7 -3
@@ -120,9 +120,13 @@ upstream::
120 `refname` above. Additionally respects `:track` to show
121 "[ahead N, behind M]" and `:trackshort` to show the terse
122 version: ">" (ahead), "<" (behind), "<>" (ahead and behind),
123 - or "=" (in sync). Has no effect if the ref does not have
124 - tracking information associated with it. `:track` also prints
125 - "[gone]" whenever unknown upstream ref is encountered.
123 + or "=" (in sync). `:track` also prints "[gone]" whenever
124 + unknown upstream ref is encountered. Append `:track,nobracket`
125 + to show tracking information without brackets (i.e "ahead N,
126 + behind M"). Has no effect if the ref does not have tracking
127 + information associated with it. All the options apart from
128 + `nobracket` are mutually exclusive, but if used together the
129 + last option is selected.
130
131 push::
132 The name of a local ref which represents the `@{push}` location
ref-filter.c
+44 -23
@@ -48,8 +48,10 @@ static struct used_atom {
48 union {
49 char color[COLOR_MAXLEN];
50 struct align align;
51 - enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT }
52 - remote_ref;
51 + struct {
52 + enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT } option;
53 + unsigned int nobracket : 1;
54 + } remote_ref;
55 struct {
56 enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
57 unsigned int nlines;
@@ -77,16 +79,33 @@ static void color_atom_parser(struct used_atom *atom, const char *color_value)
79
80 static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
81 {
80 - if (!arg)
81 - atom->u.remote_ref = RR_NORMAL;
82 - else if (!strcmp(arg, "short"))
83 - atom->u.remote_ref = RR_SHORTEN;
84 - else if (!strcmp(arg, "track"))
85 - atom->u.remote_ref = RR_TRACK;
86 - else if (!strcmp(arg, "trackshort"))
87 - atom->u.remote_ref = RR_TRACKSHORT;
88 - else
89 - die(_("unrecognized format: %%(%s)"), atom->name);
82 + struct string_list params = STRING_LIST_INIT_DUP;
83 + int i;
84 +
85 + if (!arg) {
86 + atom->u.remote_ref.option = RR_NORMAL;
87 + return;
88 + }
89 +
90 + atom->u.remote_ref.nobracket = 0;
91 + string_list_split(&params, arg, ',', -1);
92 +
93 + for (i = 0; i < params.nr; i++) {
94 + const char *s = params.items[i].string;
95 +
96 + if (!strcmp(s, "short"))
97 + atom->u.remote_ref.option = RR_SHORTEN;
98 + else if (!strcmp(s, "track"))
99 + atom->u.remote_ref.option = RR_TRACK;
100 + else if (!strcmp(s, "trackshort"))
101 + atom->u.remote_ref.option = RR_TRACKSHORT;
102 + else if (!strcmp(s, "nobracket"))
103 + atom->u.remote_ref.nobracket = 1;
104 + else
105 + die(_("unrecognized format: %%(%s)"), atom->name);
106 + }
107 +
108 + string_list_clear(&params, 0);
109 }
110
111 static void body_atom_parser(struct used_atom *atom, const char *arg)
@@ -1069,25 +1088,27 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1088 struct branch *branch, const char **s)
1089 {
1090 int num_ours, num_theirs;
1072 - if (atom->u.remote_ref == RR_SHORTEN)
1091 + if (atom->u.remote_ref.option == RR_SHORTEN)
1092 *s = shorten_unambiguous_ref(refname, warn_ambiguous_refs);
1074 - else if (atom->u.remote_ref == RR_TRACK) {
1093 + else if (atom->u.remote_ref.option == RR_TRACK) {
1094 if (stat_tracking_info(branch, &num_ours,
1095 &num_theirs, NULL)) {
1077 - *s = "[gone]";
1078 - return;
1079 - }
1080 -
1081 - if (!num_ours && !num_theirs)
1096 + *s = xstrdup("gone");
1097 + } else if (!num_ours && !num_theirs)
1098 *s = "";
1099 else if (!num_ours)
1084 - *s = xstrfmt("[behind %d]", num_theirs);
1100 + *s = xstrfmt("behind %d", num_theirs);
1101 else if (!num_theirs)
1086 - *s = xstrfmt("[ahead %d]", num_ours);
1102 + *s = xstrfmt("ahead %d", num_ours);
1103 else
1088 - *s = xstrfmt("[ahead %d, behind %d]",
1104 + *s = xstrfmt("ahead %d, behind %d",
1105 num_ours, num_theirs);
1090 - } else if (atom->u.remote_ref == RR_TRACKSHORT) {
1106 + if (!atom->u.remote_ref.nobracket && *s[0]) {
1107 + const char *to_free = *s;
1108 + *s = xstrfmt("[%s]", *s);
1109 + free((void *)to_free);
1110 + }
1111 + } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
1112 if (stat_tracking_info(branch, &num_ours,
1113 &num_theirs, NULL))
1114 return;
t/t6300-for-each-ref.sh
+2
@@ -372,6 +372,8 @@ test_expect_success 'setup for upstream:track[short]' '
372
373 test_atom head upstream:track '[ahead 1]'
374 test_atom head upstream:trackshort '>'
375 +test_atom head upstream:track,nobracket 'ahead 1'
376 +test_atom head upstream:nobracket,track 'ahead 1'
377 test_atom head push:track '[ahead 1]'
378 test_atom head push:trackshort '>'
379