Skip to content

Commit 9700fae

Browse files
J Wymangitster
authored andcommitted
for-each-ref: let upstream/push 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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc72385 commit 9700fae

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ upstream::
142142
encountered. Append `:track,nobracket` to show tracking
143143
information without brackets (i.e "ahead N, behind M").
144144
+
145-
Also respects `:remotename` to state the name of the *remote* instead of
146-
the ref.
145+
For any remote-tracking branch `%(upstream)`, `%(upstream:remotename)`
146+
and `%(upstream:remoteref)` refer to the name of the remote and the
147+
name of the tracked remote ref, respectively. In other words, the
148+
remote-tracking branch can be updated explicitly and individually by
149+
using the refspec `%(upstream:remoteref):%(upstream)` to fetch from
150+
`%(upstream:remotename)`.
147151
+
148152
Has no effect if the ref does not have tracking information associated
149153
with it. All the options apart from `nobracket` are mutually exclusive,
@@ -152,9 +156,9 @@ but if used together the last option is selected.
152156
push::
153157
The name of a local ref which represents the `@{push}`
154158
location for the displayed ref. Respects `:short`, `:lstrip`,
155-
`:rstrip`, `:track`, `:trackshort` and `:remotename` options as
156-
`upstream` does. Produces an empty string if no `@{push}` ref is
157-
configured.
159+
`:rstrip`, `:track`, `:trackshort`, `:remotename`, and `:remoteref`
160+
options as `upstream` does. Produces an empty string if no `@{push}`
161+
ref is configured.
158162

159163
HEAD::
160164
'*' 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;
@@ -164,6 +164,9 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
164164
else if (!strcmp(s, "remotename")) {
165165
atom->u.remote_ref.option = RR_REMOTE_NAME;
166166
atom->u.remote_ref.push_remote = 1;
167+
} else if (!strcmp(s, "remoteref")) {
168+
atom->u.remote_ref.option = RR_REMOTE_REF;
169+
atom->u.remote_ref.push_remote = 1;
167170
} else {
168171
atom->u.remote_ref.option = RR_REF;
169172
refname_atom_parser_internal(&atom->u.remote_ref.refname,
@@ -1262,6 +1265,16 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
12621265
*s = xstrdup(remote);
12631266
else
12641267
*s = "";
1268+
} else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
1269+
int explicit;
1270+
const char *merge;
1271+
1272+
merge = remote_ref_for_branch(branch, atom->u.remote_ref.push,
1273+
&explicit);
1274+
if (explicit)
1275+
*s = xstrdup(merge);
1276+
else
1277+
*s = "";
12651278
} else
12661279
die("BUG: unhandled RR_* enum");
12671280
}

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)