Skip to content

Commit 49292b4

Browse files
committed
Merge branch 'mingw/ref-filter-remote-name'
This reflects v2 of the ref-filter-remote-name branch, submitted as: https://public-inbox.org/git/[email protected] The only addition is a fourth patch that marks all of this shebang as experimental, subject to design changes without prior warning. By exposing this as an experimental feature to users, hopefully this patch series will mature quicker (not so much in the "beautiful code" sense, but more in the "does it work in practice" sense). Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 84740c1 + 021df78 commit 49292b4

File tree

5 files changed

+117
-15
lines changed

5 files changed

+117
-15
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,22 @@ upstream::
145145
(behind), "<>" (ahead and behind), or "=" (in sync). `:track`
146146
also prints "[gone]" whenever unknown upstream ref is
147147
encountered. Append `:track,nobracket` to show tracking
148-
information without brackets (i.e "ahead N, behind M"). Has
149-
no effect if the ref does not have tracking information
150-
associated with it. All the options apart from `nobracket`
151-
are mutually exclusive, but if used together the last option
152-
is selected.
148+
information without brackets (i.e "ahead N, behind M").
149+
+
150+
(EXPERIMENTAL) Also respects `:remotename` to state the name of the
151+
*remote* instead of the ref, and `:remoteref` to state the name of
152+
the *reference* as locally known by the remote.
153+
+
154+
Has no effect if the ref does not have tracking information associated
155+
with it. All the options apart from `nobracket` are mutually exclusive,
156+
but if used together the last option is selected.
153157

154158
push::
155159
The name of a local ref which represents the `@{push}`
156160
location for the displayed ref. Respects `:short`, `:lstrip`,
157-
`:rstrip`, `:track`, and `:trackshort` options as `upstream`
158-
does. Produces an empty string if no `@{push}` ref is
159-
configured.
161+
`:rstrip`, `:track`, `:trackshort`, (EXPERIMENTAL) `:remotename`,
162+
and (EXPERIMENTAL) `:remoteref` options as `upstream` does.
163+
Produces an empty string if no `@{push}` ref is configured.
160164

161165
HEAD::
162166
'*' if HEAD matches current ref (the checked out branch), ' '

ref-filter.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ static struct used_atom {
7676
char color[COLOR_MAXLEN];
7777
struct align align;
7878
struct {
79-
enum { RR_REF, RR_TRACK, RR_TRACKSHORT } option;
79+
enum {
80+
RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
81+
} option;
8082
struct refname_atom refname;
81-
unsigned int nobracket : 1;
83+
unsigned int nobracket : 1, push : 1, push_remote : 1;
8284
} remote_ref;
8385
struct {
8486
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
@@ -137,6 +139,9 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
137139
struct string_list params = STRING_LIST_INIT_DUP;
138140
int i;
139141

142+
if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
143+
atom->u.remote_ref.push = 1;
144+
140145
if (!arg) {
141146
atom->u.remote_ref.option = RR_REF;
142147
refname_atom_parser_internal(&atom->u.remote_ref.refname,
@@ -156,7 +161,13 @@ static void remote_ref_atom_parser(const struct ref_format *format, struct used_
156161
atom->u.remote_ref.option = RR_TRACKSHORT;
157162
else if (!strcmp(s, "nobracket"))
158163
atom->u.remote_ref.nobracket = 1;
159-
else {
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 {
160171
atom->u.remote_ref.option = RR_REF;
161172
refname_atom_parser_internal(&atom->u.remote_ref.refname,
162173
arg, atom->name);
@@ -1255,6 +1266,25 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
12551266
*s = ">";
12561267
else
12571268
*s = "<>";
1269+
} else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
1270+
int explicit;
1271+
const char *remote = atom->u.remote_ref.push ?
1272+
pushremote_for_branch(branch, &explicit) :
1273+
remote_for_branch(branch, &explicit);
1274+
if (explicit)
1275+
*s = xstrdup(remote);
1276+
else
1277+
*s = "";
1278+
} else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
1279+
int explicit;
1280+
const char *merge;
1281+
1282+
merge = remote_ref_for_branch(branch, atom->u.remote_ref.push,
1283+
&explicit);
1284+
if (explicit)
1285+
*s = xstrdup(merge);
1286+
else
1287+
*s = "";
12581288
} else
12591289
die("BUG: unhandled RR_* enum");
12601290
}
@@ -1365,16 +1395,20 @@ static void populate_value(struct ref_array_item *ref)
13651395
if (refname)
13661396
fill_remote_ref_details(atom, refname, branch, &v->s);
13671397
continue;
1368-
} else if (starts_with(name, "push")) {
1398+
} else if (atom->u.remote_ref.push) {
13691399
const char *branch_name;
13701400
if (!skip_prefix(ref->refname, "refs/heads/",
13711401
&branch_name))
13721402
continue;
13731403
branch = branch_get(branch_name);
13741404

1375-
refname = branch_get_push(branch, NULL);
1376-
if (!refname)
1377-
continue;
1405+
if (atom->u.remote_ref.push_remote)
1406+
refname = NULL;
1407+
else {
1408+
refname = branch_get_push(branch, NULL);
1409+
if (!refname)
1410+
continue;
1411+
}
13781412
fill_remote_ref_details(atom, refname, branch, &v->s);
13791413
continue;
13801414
} else if (starts_with(name, "color:")) {

remote.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,36 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
672672
return remote_for_branch(branch, explicit);
673673
}
674674

675+
const char *remote_ref_for_branch(struct branch *branch, int for_push,
676+
int *explicit)
677+
{
678+
if (branch) {
679+
if (!for_push) {
680+
if (branch->merge_nr) {
681+
if (explicit)
682+
*explicit = 1;
683+
return branch->merge_name[0];
684+
}
685+
} else {
686+
const char *dst, *remote_name =
687+
pushremote_for_branch(branch, NULL);
688+
struct remote *remote = remote_get(remote_name);
689+
690+
if (remote && remote->push_refspec_nr &&
691+
(dst = apply_refspecs(remote->push,
692+
remote->push_refspec_nr,
693+
branch->refname))) {
694+
if (explicit)
695+
*explicit = 1;
696+
return dst;
697+
}
698+
}
699+
}
700+
if (explicit)
701+
*explicit = 0;
702+
return "";
703+
}
704+
675705
static struct remote *remote_get_1(const char *name,
676706
const char *(*get_default)(struct branch *, int *))
677707
{

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 *);

t/t6300-for-each-ref.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,4 +680,36 @@ test_expect_success 'Verify usage of %(symref:rstrip) atom' '
680680
test_cmp expected actual
681681
'
682682

683+
test_expect_success ':remotename and :remoteref' '
684+
git init remote-tests &&
685+
(
686+
cd remote-tests &&
687+
test_commit initial &&
688+
git remote add from fifth.coffee:blub &&
689+
git config branch.master.remote from &&
690+
git config branch.master.merge refs/heads/stable &&
691+
git remote add to southridge.audio:repo &&
692+
git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
693+
git config branch.master.pushRemote to &&
694+
for pair in "%(upstream)=refs/remotes/from/stable" \
695+
"%(upstream:remotename)=from" \
696+
"%(upstream:remoteref)=refs/heads/stable" \
697+
"%(push)=refs/remotes/to/pushed/master" \
698+
"%(push:remotename)=to" \
699+
"%(push:remoteref)=refs/heads/pushed/master"
700+
do
701+
echo "${pair#*=}" >expect &&
702+
git for-each-ref --format="${pair%=*}" \
703+
refs/heads/master >actual &&
704+
test_cmp expect actual
705+
done &&
706+
git branch push-simple &&
707+
git config branch.push-simple.pushRemote from &&
708+
actual="$(git for-each-ref \
709+
--format="%(push:remotename),%(push:remoteref)" \
710+
refs/heads/push-simple)" &&
711+
test from, = "$actual"
712+
)
713+
'
714+
683715
test_done

0 commit comments

Comments
 (0)