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>
This commit is contained in:
J Wyman
2017-11-07 17:31:08 +01:00
committed by Junio C Hamano
parent cc72385fe3
commit 9700fae5ee
4 changed files with 55 additions and 6 deletions

View File

@ -675,6 +675,36 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
return remote_for_branch(branch, explicit);
}
const char *remote_ref_for_branch(struct branch *branch, int for_push,
int *explicit)
{
if (branch) {
if (!for_push) {
if (branch->merge_nr) {
if (explicit)
*explicit = 1;
return branch->merge_name[0];
}
} else {
const char *dst, *remote_name =
pushremote_for_branch(branch, NULL);
struct remote *remote = remote_get(remote_name);
if (remote && remote->push_refspec_nr &&
(dst = apply_refspecs(remote->push,
remote->push_refspec_nr,
branch->refname))) {
if (explicit)
*explicit = 1;
return dst;
}
}
}
if (explicit)
*explicit = 0;
return "";
}
static struct remote *remote_get_1(const char *name,
const char *(*get_default)(struct branch *, int *))
{