Skip to content

Commit 01611d5

Browse files
committed
Merge branch 'dr/push-remoteref-fix' into seen
The "%(push:remoteref)" placeholder in the "--format=" argument of "git format-patch" (and friends) only showed what got explicitly configured, not what ref at the receiving end would be updated when "git push" was used, as it ignored the default behaviour (e.g. update the same ref as the source). * dr/push-remoteref-fix: remote.c: fix handling of %(push:remoteref)
2 parents 6ab1754 + 812a588 commit 01611d5

File tree

2 files changed

+111
-27
lines changed

2 files changed

+111
-27
lines changed

remote.c

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -505,28 +505,6 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
505505
return remote_for_branch(branch, explicit);
506506
}
507507

508-
const char *remote_ref_for_branch(struct branch *branch, int for_push)
509-
{
510-
if (branch) {
511-
if (!for_push) {
512-
if (branch->merge_nr) {
513-
return branch->merge_name[0];
514-
}
515-
} else {
516-
const char *dst, *remote_name =
517-
pushremote_for_branch(branch, NULL);
518-
struct remote *remote = remote_get(remote_name);
519-
520-
if (remote && remote->push.nr &&
521-
(dst = apply_refspecs(&remote->push,
522-
branch->refname))) {
523-
return dst;
524-
}
525-
}
526-
}
527-
return NULL;
528-
}
529-
530508
static struct remote *remote_get_1(const char *name,
531509
const char *(*get_default)(struct branch *, int *))
532510
{
@@ -1645,6 +1623,64 @@ static const char *tracking_for_push_dest(struct remote *remote,
16451623
return ret;
16461624
}
16471625

1626+
/**
1627+
* Return the local name of the remote tracking branch, as in
1628+
* %(push:remoteref), that corresponds to the ref we would push to given a
1629+
* bare `git push` while `branch` is checked out.
1630+
* See also branch_get_push_1 below.
1631+
*/
1632+
static const char *branch_get_push_remoteref(struct branch *branch)
1633+
{
1634+
struct remote *remote;
1635+
1636+
remote = remote_get(pushremote_for_branch(branch, NULL));
1637+
if (!remote)
1638+
return NULL;
1639+
1640+
if (remote->push.nr) {
1641+
return apply_refspecs(&remote->push, branch->refname);
1642+
}
1643+
1644+
if (remote->mirror)
1645+
return branch->refname;
1646+
1647+
switch (push_default) {
1648+
case PUSH_DEFAULT_NOTHING:
1649+
return NULL;
1650+
1651+
case PUSH_DEFAULT_MATCHING:
1652+
case PUSH_DEFAULT_CURRENT:
1653+
return branch->refname;
1654+
1655+
case PUSH_DEFAULT_UPSTREAM:
1656+
if (branch && branch->merge && branch->merge[0] &&
1657+
branch->merge[0]->dst)
1658+
return branch->merge[0]->src;
1659+
else
1660+
return NULL;
1661+
1662+
case PUSH_DEFAULT_UNSPECIFIED:
1663+
case PUSH_DEFAULT_SIMPLE:
1664+
{
1665+
const char *up, *cur;
1666+
1667+
up = branch_get_upstream(branch, NULL);
1668+
cur = tracking_for_push_dest(remote, branch->refname, NULL);
1669+
if (up && cur && !strcmp(cur, up))
1670+
return branch->refname;
1671+
else
1672+
return NULL;
1673+
1674+
}
1675+
}
1676+
BUG("unhandled push situation");
1677+
}
1678+
1679+
/**
1680+
* Return the tracking branch, as in %(push), that corresponds to the ref we
1681+
* would push to given a bare `git push` while `branch` is checked out.
1682+
* See also branch_get_push_remoteref above.
1683+
*/
16481684
static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
16491685
{
16501686
struct remote *remote;
@@ -1724,6 +1760,20 @@ static int ignore_symref_update(const char *refname)
17241760
return (flag & REF_ISSYMREF);
17251761
}
17261762

1763+
const char *remote_ref_for_branch(struct branch *branch, int for_push)
1764+
{
1765+
if (branch) {
1766+
if (!for_push) {
1767+
if (branch->merge_nr) {
1768+
return branch->merge_name[0];
1769+
}
1770+
} else {
1771+
return branch_get_push_remoteref(branch);
1772+
}
1773+
}
1774+
return NULL;
1775+
}
1776+
17271777
/*
17281778
* Create and return a list of (struct ref) consisting of copies of
17291779
* each remote_ref that matches refspec. refspec must be a pattern.

t/t6300-for-each-ref.sh

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,46 @@ test_expect_success ':remotename and :remoteref' '
955955
git for-each-ref --format="${pair%=*}" \
956956
refs/heads/master >actual &&
957957
test_cmp expect actual
958-
done &&
959-
git branch push-simple &&
960-
git config branch.push-simple.pushRemote from &&
961-
actual="$(git for-each-ref \
958+
done
959+
)
960+
'
961+
962+
test_expect_success ':push:remoteref' '
963+
git init push-tests &&
964+
(
965+
cd push-tests &&
966+
test_commit initial &&
967+
git remote add from fifth.coffee:blub &&
968+
git config branch.master.remote from &&
969+
actual="$(git -c push.default=simple for-each-ref \
970+
--format="%(push:remotename),%(push:remoteref)" \
971+
refs/heads/master)" &&
972+
test from, = "$actual" &&
973+
git config branch.master.merge refs/heads/master &&
974+
actual="$(git -c push.default=simple for-each-ref \
975+
--format="%(push:remotename),%(push:remoteref)" \
976+
refs/heads/master)" &&
977+
test from,refs/heads/master = "$actual" &&
978+
git config branch.master.merge refs/heads/other &&
979+
actual="$(git -c push.default=simple for-each-ref \
980+
--format="%(push:remotename),%(push:remoteref)" \
981+
refs/heads/master)" &&
982+
test from, = "$actual" &&
983+
actual="$(git -c push.default=upstream for-each-ref \
984+
--format="%(push:remotename),%(push:remoteref)" \
985+
refs/heads/master)" &&
986+
test from,refs/heads/other = "$actual" &&
987+
actual="$(git -c push.default=current for-each-ref \
988+
--format="%(push:remotename),%(push:remoteref)" \
989+
refs/heads/master)" &&
990+
test from,refs/heads/master = "$actual" &&
991+
actual="$(git -c push.default=matching for-each-ref \
992+
--format="%(push:remotename),%(push:remoteref)" \
993+
refs/heads/master)" &&
994+
test from,refs/heads/master = "$actual" &&
995+
actual="$(git -c push.default=nothing for-each-ref \
962996
--format="%(push:remotename),%(push:remoteref)" \
963-
refs/heads/push-simple)" &&
997+
refs/heads/master)" &&
964998
test from, = "$actual"
965999
)
9661000
'

0 commit comments

Comments
 (0)