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 <jwyman@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

J Wyman committed Nov 7, 2017 at 17:31 UTC 9700fae5ee93dd0928cb82afb7b2fd49295e28d2
4 files changed +55 -6
Documentation/git-for-each-ref.txt
+9 -5
@@ -142,8 +142,12 @@ upstream::
142 encountered. Append `:track,nobracket` to show tracking
143 information without brackets (i.e "ahead N, behind M").
144 +
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)`.
151 +
152 Has no effect if the ref does not have tracking information associated
153 with it. All the options apart from `nobracket` are mutually exclusive,
@@ -152,9 +156,9 @@ but if used together the last option is selected.
156 push::
157 The name of a local ref which represents the `@{push}`
158 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.
162
163 HEAD::
164 '*' if HEAD matches current ref (the checked out branch), ' '
ref-filter.c
+14 -1
@@ -77,7 +77,7 @@ static struct used_atom {
77 struct align align;
78 struct {
79 enum {
80 - RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME
80 + RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
81 } option;
82 struct refname_atom refname;
83 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_
164 else if (!strcmp(s, "remotename")) {
165 atom->u.remote_ref.option = RR_REMOTE_NAME;
166 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;
170 } else {
171 atom->u.remote_ref.option = RR_REF;
172 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,
1265 *s = xstrdup(remote);
1266 else
1267 *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 = "";
1278 } else
1279 die("BUG: unhandled RR_* enum");
1280 }
remote.c
+30
@@ -675,6 +675,36 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
675 return remote_for_branch(branch, explicit);
676 }
677
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 +
708 static struct remote *remote_get_1(const char *name,
709 const char *(*get_default)(struct branch *, int *))
710 {
remote.h
+2
@@ -223,6 +223,8 @@ struct branch {
223 struct branch *branch_get(const char *name);
224 const char *remote_for_branch(struct branch *branch, int *explicit);
225 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);
228
229 int branch_has_merge_config(struct branch *branch);
230 int branch_merge_matches(struct branch *, int n, const char *);