Skip to content

Commit bad1467

Browse files
J Wymandscho
authored andcommitted
for-each-ref: let upstream/push optionally report the remote ref name
There are times when scripts want to know not only the name of the push branch on the remote, but also the name of the branch as known by the remote repository. An example of this is when a tool wants to push to the very same branch from which it would pull automatically, i.e. the `<remote>` and the `<to>` in `git push <remote> <from>:<to>` would be provided by `%(upstream:remotename)` and `%(upstream:remoteref)`, respectively. This patch offers the new suffix :remoteref for the `upstream` and `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/* [branch "master"] remote = origin merge = refs/heads/master [branch "develop/with/topics"] remote = origin merge = refs/heads/develop/with/topics ... $ git for-each-ref \ --format='%(push) %(push:remoteref)' \ refs/heads refs/remotes/origin/master refs/heads/master refs/remotes/origin/develop/with/topics refs/heads/develop/with/topics Signed-off-by: J Wyman <jwyman@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 7a032b7 commit bad1467

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ upstream::
147147
encountered. Append `:track,nobracket` to show tracking
148148
information without brackets (i.e "ahead N, behind M").
149149
+
150-
Also respects `:remotename` to state the name of the *remote* instead of
151-
the ref.
150+
Also respects `:remotename` to state the name of the *remote* instead
151+
of the ref, and `:remoteref` to state the name of the *reference* as
152+
locally known by the remote.
152153
+
153154
Has no effect if the ref does not have tracking information associated
154155
with it. All the options apart from `nobracket` are mutually exclusive,
@@ -157,9 +158,9 @@ but if used together the last option is selected.
157158
push::
158159
The name of a local ref which represents the `@{push}`
159160
location for the displayed ref. Respects `:short`, `:lstrip`,
160-
`:rstrip`, `:track`, `:trackshort` and `:remotename` options as
161-
`upstream` does. Produces an empty string if no `@{push}` ref is
162-
configured.
161+
`:rstrip`, `:track`, `:trackshort`, `:remotename`, and `:remoteref`
162+
options as `upstream` does. Produces an empty string if no `@{push}`
163+
ref is configured.
163164

164165
HEAD::
165166
'*' if HEAD matches current ref (the checked out branch), ' '

ref-filter.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static struct used_atom {
7777
struct align align;
7878
struct {
7979
enum {
80-
RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME
80+
RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
8181
} option;
8282
struct refname_atom refname;
8383
unsigned int nobracket : 1, push : 1, push_remote : 1;
@@ -165,6 +165,9 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
165165
else if (!strcmp(s, "remotename")) {
166166
atom->u.remote_ref.option = RR_REMOTE_NAME;
167167
atom->u.remote_ref.push_remote = 1;
168+
} else if (!strcmp(s, "remoteref")) {
169+
atom->u.remote_ref.option = RR_REMOTE_REF;
170+
atom->u.remote_ref.push_remote = 1;
168171
} else {
169172
atom->u.remote_ref.option = RR_REF;
170173
refname_atom_parser_internal(&atom->u.remote_ref.refname,
@@ -1285,6 +1288,16 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
12851288
*s = xstrdup(remote);
12861289
else
12871290
*s = "";
1291+
} else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
1292+
int explicit;
1293+
const char *merge;
1294+
1295+
merge = remote_ref_for_branch(branch, atom->u.remote_ref.push,
1296+
&explicit);
1297+
if (explicit)
1298+
*s = xstrdup(merge);
1299+
else
1300+
*s = "";
12881301
} else
12891302
die("BUG: unhandled RR_* enum");
12901303
}

remote.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,36 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
675675
return remote_for_branch(branch, explicit);
676676
}
677677

678+
const char *remote_ref_for_branch(struct branch *branch, int for_push,
679+
int *explicit)
680+
{
681+
if (branch) {
682+
if (!for_push) {
683+
if (branch->merge_nr) {
684+
if (explicit)
685+
*explicit = 1;
686+
return branch->merge_name[0];
687+
}
688+
} else {
689+
const char *dst, *remote_name =
690+
pushremote_for_branch(branch, NULL);
691+
struct remote *remote = remote_get(remote_name);
692+
693+
if (remote && remote->push_refspec_nr &&
694+
(dst = apply_refspecs(remote->push,
695+
remote->push_refspec_nr,
696+
branch->refname))) {
697+
if (explicit)
698+
*explicit = 1;
699+
return dst;
700+
}
701+
}
702+
}
703+
if (explicit)
704+
*explicit = 0;
705+
return "";
706+
}
707+
678708
static struct remote *remote_get_1(const char *name,
679709
const char *(*get_default)(struct branch *, int *))
680710
{

remote.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ struct branch {
223223
struct branch *branch_get(const char *name);
224224
const char *remote_for_branch(struct branch *branch, int *explicit);
225225
const char *pushremote_for_branch(struct branch *branch, int *explicit);
226+
const char *remote_ref_for_branch(struct branch *branch, int for_push,
227+
int *explicit);
226228

227229
int branch_has_merge_config(struct branch *branch);
228230
int branch_merge_matches(struct branch *, int n, const char *);

0 commit comments

Comments
 (0)