Skip to content

Commit 3ba308c

Browse files
KarthikNayakgitster
authored andcommitted
ref-filter: make remote_ref_atom_parser() use refname_atom_parser_internal()
Use the recently introduced refname_atom_parser_internal() within remote_ref_atom_parser(), this provides a common base for all the ref printing atoms, allowing %(upstream) and %(push) to also use the ':strip' option. The atoms '%(push)' and '%(upstream)' will retain the ':track' and ':trackshort' atom modifiers to themselves as they have no meaning in context to the '%(refname)' and '%(symref)' atoms. Update the documentation and tests to reflect the same. Signed-off-by: Karthik Nayak <Karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a798410 commit 3ba308c

3 files changed

Lines changed: 33 additions & 26 deletions

File tree

Documentation/git-for-each-ref.txt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,24 @@ objectname::
116116

117117
upstream::
118118
The name of a local ref which can be considered ``upstream''
119-
from the displayed ref. Respects `:short` in the same way as
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). `: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.
119+
from the displayed ref. Respects `:short` and `:strip` in the
120+
same way as `refname` above. Additionally respects `:track`
121+
to show "[ahead N, behind M]" and `:trackshort` to show the
122+
terse version: ">" (ahead), "<" (behind), "<>" (ahead and
123+
behind), or "=" (in sync). `:track` also prints "[gone]"
124+
whenever unknown upstream ref is encountered. Append
125+
`:track,nobracket` to show tracking information without
126+
brackets (i.e "ahead N, behind M"). Has no effect if the ref
127+
does not have tracking information associated with it. All
128+
the options apart from `nobracket` are mutually exclusive, but
129+
if used together the last option is selected.
130130

131131
push::
132-
The name of a local ref which represents the `@{push}` location
133-
for the displayed ref. Respects `:short`, `:track`, and
134-
`:trackshort` options as `upstream` does. Produces an empty
135-
string if no `@{push}` ref is configured.
132+
The name of a local ref which represents the `@{push}`
133+
location for the displayed ref. Respects `:short`, `:strip`,
134+
`:track`, and `:trackshort` options as `upstream`
135+
does. Produces an empty string if no `@{push}` ref is
136+
configured.
136137

137138
HEAD::
138139
'*' if HEAD matches current ref (the checked out branch), ' '

ref-filter.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ static struct used_atom {
5454
char color[COLOR_MAXLEN];
5555
struct align align;
5656
struct {
57-
enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT } option;
57+
enum { RR_REF, RR_TRACK, RR_TRACKSHORT } option;
58+
struct refname_atom refname;
5859
unsigned int nobracket : 1;
5960
} remote_ref;
6061
struct {
@@ -104,7 +105,9 @@ static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
104105
int i;
105106

106107
if (!arg) {
107-
atom->u.remote_ref.option = RR_NORMAL;
108+
atom->u.remote_ref.option = RR_REF;
109+
refname_atom_parser_internal(&atom->u.remote_ref.refname,
110+
arg, atom->name);
108111
return;
109112
}
110113

@@ -114,16 +117,17 @@ static void remote_ref_atom_parser(struct used_atom *atom, const char *arg)
114117
for (i = 0; i < params.nr; i++) {
115118
const char *s = params.items[i].string;
116119

117-
if (!strcmp(s, "short"))
118-
atom->u.remote_ref.option = RR_SHORTEN;
119-
else if (!strcmp(s, "track"))
120+
if (!strcmp(s, "track"))
120121
atom->u.remote_ref.option = RR_TRACK;
121122
else if (!strcmp(s, "trackshort"))
122123
atom->u.remote_ref.option = RR_TRACKSHORT;
123124
else if (!strcmp(s, "nobracket"))
124125
atom->u.remote_ref.nobracket = 1;
125-
else
126-
die(_("unrecognized format: %%(%s)"), atom->name);
126+
else {
127+
atom->u.remote_ref.option = RR_REF;
128+
refname_atom_parser_internal(&atom->u.remote_ref.refname,
129+
arg, atom->name);
130+
}
127131
}
128132

129133
string_list_clear(&params, 0);
@@ -1119,8 +1123,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
11191123
struct branch *branch, const char **s)
11201124
{
11211125
int num_ours, num_theirs;
1122-
if (atom->u.remote_ref.option == RR_SHORTEN)
1123-
*s = shorten_unambiguous_ref(refname, warn_ambiguous_refs);
1126+
if (atom->u.remote_ref.option == RR_REF)
1127+
*s = show_ref(&atom->u.remote_ref.refname, refname);
11241128
else if (atom->u.remote_ref.option == RR_TRACK) {
11251129
if (stat_tracking_info(branch, &num_ours,
11261130
&num_theirs, NULL)) {
@@ -1152,8 +1156,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
11521156
*s = ">";
11531157
else
11541158
*s = "<>";
1155-
} else /* RR_NORMAL */
1156-
*s = refname;
1159+
} else
1160+
die("BUG: unhandled RR_* enum");
11571161
}
11581162

11591163
char *get_head_description(void)

t/t6300-for-each-ref.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ test_atom head refname:strip=1 heads/master
5555
test_atom head refname:strip=2 master
5656
test_atom head upstream refs/remotes/origin/master
5757
test_atom head upstream:short origin/master
58+
test_atom head upstream:strip=2 origin/master
5859
test_atom head push refs/remotes/myfork/master
5960
test_atom head push:short myfork/master
61+
test_atom head push:strip=1 remotes/myfork/master
6062
test_atom head objecttype commit
6163
test_atom head objectsize 171
6264
test_atom head objectname $(git rev-parse refs/heads/master)

0 commit comments

Comments
 (0)