Skip to content

Commit 29bc885

Browse files
peffgitster
authored andcommitted
for-each-ref: accept "%(push)" format
Just as we have "%(upstream)" to report the "@{upstream}" for each ref, this patch adds "%(push)" to match "@{push}". It supports the same tracking format modifiers as upstream (because you may want to know, for example, which branches have commits to push). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3dbe9db commit 29bc885

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ upstream::
9797
or "=" (in sync). Has no effect if the ref does not have
9898
tracking information associated with it.
9999

100+
push::
101+
The name of a local ref which represents the `@{push}` location
102+
for the displayed ref. Respects `:short`, `:track`, and
103+
`:trackshort` options as `upstream` does. Produces an empty
104+
string if no `@{push}` ref is configured.
105+
100106
HEAD::
101107
'*' if HEAD matches current ref (the checked out branch), ' '
102108
otherwise.

builtin/for-each-ref.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static struct {
7474
{ "contents:body" },
7575
{ "contents:signature" },
7676
{ "upstream" },
77+
{ "push" },
7778
{ "symref" },
7879
{ "flag" },
7980
{ "HEAD" },
@@ -669,6 +670,16 @@ static void populate_value(struct refinfo *ref)
669670
refname = branch_get_upstream(branch, NULL);
670671
if (!refname)
671672
continue;
673+
} else if (starts_with(name, "push")) {
674+
const char *branch_name;
675+
if (!skip_prefix(ref->refname, "refs/heads/",
676+
&branch_name))
677+
continue;
678+
branch = branch_get(branch_name);
679+
680+
refname = branch_get_push(branch, NULL);
681+
if (!refname)
682+
continue;
672683
} else if (starts_with(name, "color:")) {
673684
char color[COLOR_MAXLEN] = "";
674685

@@ -714,7 +725,8 @@ static void populate_value(struct refinfo *ref)
714725
refname = shorten_unambiguous_ref(refname,
715726
warn_ambiguous_refs);
716727
else if (!strcmp(formatp, "track") &&
717-
starts_with(name, "upstream")) {
728+
(starts_with(name, "upstream") ||
729+
starts_with(name, "push"))) {
718730
char buf[40];
719731

720732
if (stat_tracking_info(branch, &num_ours,
@@ -736,7 +748,8 @@ static void populate_value(struct refinfo *ref)
736748
}
737749
continue;
738750
} else if (!strcmp(formatp, "trackshort") &&
739-
starts_with(name, "upstream")) {
751+
(starts_with(name, "upstream") ||
752+
starts_with(name, "push"))) {
740753
assert(branch);
741754

742755
if (stat_tracking_info(branch, &num_ours,

t/t6300-for-each-ref.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ test_expect_success setup '
2828
git update-ref refs/remotes/origin/master master &&
2929
git remote add origin nowhere &&
3030
git config branch.master.remote origin &&
31-
git config branch.master.merge refs/heads/master
31+
git config branch.master.merge refs/heads/master &&
32+
git remote add myfork elsewhere &&
33+
git config remote.pushdefault myfork &&
34+
git config push.default current
3235
'
3336

3437
test_atom() {
@@ -47,6 +50,7 @@ test_atom() {
4750

4851
test_atom head refname refs/heads/master
4952
test_atom head upstream refs/remotes/origin/master
53+
test_atom head push refs/remotes/myfork/master
5054
test_atom head objecttype commit
5155
test_atom head objectsize 171
5256
test_atom head objectname $(git rev-parse refs/heads/master)
@@ -83,6 +87,7 @@ test_atom head HEAD '*'
8387

8488
test_atom tag refname refs/tags/testtag
8589
test_atom tag upstream ''
90+
test_atom tag push ''
8691
test_atom tag objecttype tag
8792
test_atom tag objectsize 154
8893
test_atom tag objectname $(git rev-parse refs/tags/testtag)
@@ -347,6 +352,12 @@ test_expect_success 'Check that :track[short] works when upstream is invalid' '
347352
test_cmp expected actual
348353
'
349354

355+
test_expect_success '%(push) supports tracking specifiers, too' '
356+
echo "[ahead 1]" >expected &&
357+
git for-each-ref --format="%(push:track)" refs/heads >actual &&
358+
test_cmp expected actual
359+
'
360+
350361
cat >expected <<EOF
351362
$(git rev-parse --short HEAD)
352363
EOF

0 commit comments

Comments
 (0)