Skip to content

Commit 90968e3

Browse files
committed
for-each-ref: let upstream/push optionally report the remote name
There are times when e.g. scripts want to know not only the name of the upstream branch on the remote repository, but also the name of the remote. This patch offers the new suffix :remotename for the upstream and for the push atoms, allowing to show exactly that. Example: $ cat .git/config ... [remote "origin"] url = https://where.do.we.come/from fetch = refs/heads/*:refs/remote/origin/* [remote "hello-world"] url = https://hello.world/git fetch = refs/heads/*:refs/remote/origin/* pushURL = hello.world:git push = refs/heads/*:refs/heads/* [branch "master"] remote = origin pushRemote = hello-world ... $ git for-each-ref \ --format='%(upstream) %(upstream:remotename) %(push:remotename)' \ refs/heads/master refs/remotes/origin/master origin hello-world The implementation chooses *not* to DWIM the push remote if no explicit push remote was configured; The reason is that it is possible to DWIM this by using %(if)%(push:remotename)%(then) %(push:remotename) %(else) %(upstream:remotename) %(end) while it would be impossible to "un-DWIM" the information in case the caller is really only interested in explicit push remotes. While `:remote` would be shorter, it would also be a bit more ambiguous, and it would also shut the door e.g. for `:remoteref` (which would obviously refer to the corresponding ref in the remote repository). Note: the dashless, non-CamelCased form `:remotename` follows the example of the `:trackshort` example. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0cf6f62 commit 90968e3

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,20 @@ upstream::
145145
(behind), "<>" (ahead and behind), or "=" (in sync). `:track`
146146
also prints "[gone]" whenever unknown upstream ref is
147147
encountered. Append `:track,nobracket` to show tracking
148-
information without brackets (i.e "ahead N, behind M"). Has
149-
no effect if the ref does not have tracking information
150-
associated with it. All the options apart from `nobracket`
151-
are mutually exclusive, but if used together the last option
152-
is selected.
148+
information without brackets (i.e "ahead N, behind M").
149+
+
150+
Also respects `:remotename` to state the name of the *remote* instead of
151+
the ref.
152+
+
153+
Has no effect if the ref does not have tracking information associated
154+
with it. All the options apart from `nobracket` are mutually exclusive,
155+
but if used together the last option is selected.
153156

154157
push::
155158
The name of a local ref which represents the `@{push}`
156159
location for the displayed ref. Respects `:short`, `:lstrip`,
157-
`:rstrip`, `:track`, and `:trackshort` options as `upstream`
158-
does. Produces an empty string if no `@{push}` ref is
160+
`:rstrip`, `:track`, `:trackshort` and `:remotename` options as
161+
`upstream` does. Produces an empty string if no `@{push}` ref is
159162
configured.
160163

161164
HEAD::

ref-filter.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ static struct used_atom {
7676
char color[COLOR_MAXLEN];
7777
struct align align;
7878
struct {
79-
enum { RR_REF, RR_TRACK, RR_TRACKSHORT } option;
79+
enum {
80+
RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME
81+
} option;
8082
struct refname_atom refname;
81-
unsigned int nobracket : 1;
83+
unsigned int nobracket : 1, push : 1, push_remote : 1;
8284
} remote_ref;
8385
struct {
8486
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
@@ -138,6 +140,9 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
138140
struct string_list params = STRING_LIST_INIT_DUP;
139141
int i;
140142

143+
if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
144+
atom->u.remote_ref.push = 1;
145+
141146
if (!arg) {
142147
atom->u.remote_ref.option = RR_REF;
143148
refname_atom_parser_internal(&atom->u.remote_ref.refname,
@@ -157,7 +162,10 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
157162
atom->u.remote_ref.option = RR_TRACKSHORT;
158163
else if (!strcmp(s, "nobracket"))
159164
atom->u.remote_ref.nobracket = 1;
160-
else {
165+
else if (!strcmp(s, "remotename")) {
166+
atom->u.remote_ref.option = RR_REMOTE_NAME;
167+
atom->u.remote_ref.push_remote = 1;
168+
} else {
161169
atom->u.remote_ref.option = RR_REF;
162170
refname_atom_parser_internal(&atom->u.remote_ref.refname,
163171
arg, atom->name);
@@ -1268,6 +1276,15 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
12681276
*s = ">";
12691277
else
12701278
*s = "<>";
1279+
} else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
1280+
int explicit;
1281+
const char *remote = atom->u.remote_ref.push ?
1282+
pushremote_for_branch(branch, &explicit) :
1283+
remote_for_branch(branch, &explicit);
1284+
if (explicit)
1285+
*s = xstrdup(remote);
1286+
else
1287+
*s = "";
12711288
} else
12721289
die("BUG: unhandled RR_* enum");
12731290
}
@@ -1377,16 +1394,20 @@ static void populate_value(struct ref_array_item *ref)
13771394
if (refname)
13781395
fill_remote_ref_details(atom, refname, branch, &v->s);
13791396
continue;
1380-
} else if (starts_with(name, "push")) {
1397+
} else if (atom->u.remote_ref.push) {
13811398
const char *branch_name;
13821399
if (!skip_prefix(ref->refname, "refs/heads/",
13831400
&branch_name))
13841401
continue;
13851402
branch = branch_get(branch_name);
13861403

1387-
refname = branch_get_push(branch, NULL);
1388-
if (!refname)
1389-
continue;
1404+
if (atom->u.remote_ref.push_remote)
1405+
refname = NULL;
1406+
else {
1407+
refname = branch_get_push(branch, NULL);
1408+
if (!refname)
1409+
continue;
1410+
}
13901411
fill_remote_ref_details(atom, refname, branch, &v->s);
13911412
continue;
13921413
} else if (starts_with(name, "color:")) {

0 commit comments

Comments
 (0)