Skip to content

Commit 7743fcc

Browse files
KarthikNayakgitster
authored andcommitted
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>
1 parent ffd921d commit 7743fcc

3 files changed

Lines changed: 53 additions & 26 deletions

File tree

Documentation/git-for-each-ref.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ upstream::
120120
`refname` above. Additionally respects `:track` to show
121121
"[ahead N, behind M]" and `:trackshort` to show the terse
122122
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.
126130

127131
push::
128132
The name of a local ref which represents the `@{push}` location

ref-filter.c

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ static struct used_atom {
4848
union {
4949
char color[COLOR_MAXLEN];
5050
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;
5355
struct {
5456
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
5557
unsigned int nlines;
@@ -77,16 +79,33 @@ static void color_atom_parser(struct used_atom *atom, const char *color_value)
7779

7880
static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
7981
{
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);
90109
}
91110

92111
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,
10691088
struct branch *branch, const char **s)
10701089
{
10711090
int num_ours, num_theirs;
1072-
if (atom->u.remote_ref == RR_SHORTEN)
1091+
if (atom->u.remote_ref.option == RR_SHORTEN)
10731092
*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) {
10751094
if (stat_tracking_info(branch, &num_ours,
10761095
&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)
10821098
*s = "";
10831099
else if (!num_ours)
1084-
*s = xstrfmt("[behind %d]", num_theirs);
1100+
*s = xstrfmt("behind %d", num_theirs);
10851101
else if (!num_theirs)
1086-
*s = xstrfmt("[ahead %d]", num_ours);
1102+
*s = xstrfmt("ahead %d", num_ours);
10871103
else
1088-
*s = xstrfmt("[ahead %d, behind %d]",
1104+
*s = xstrfmt("ahead %d, behind %d",
10891105
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) {
10911112
if (stat_tracking_info(branch, &num_ours,
10921113
&num_theirs, NULL))
10931114
return;

t/t6300-for-each-ref.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ test_expect_success 'setup for upstream:track[short]' '
372372

373373
test_atom head upstream:track '[ahead 1]'
374374
test_atom head upstream:trackshort '>'
375+
test_atom head upstream:track,nobracket 'ahead 1'
376+
test_atom head upstream:nobracket,track 'ahead 1'
375377
test_atom head push:track '[ahead 1]'
376378
test_atom head push:trackshort '>'
377379

0 commit comments

Comments
 (0)