Skip to content

Commit 684a93d

Browse files
committed
Merge branch 'ar/wildcardpush'
* ar/wildcardpush: Test wildcard push/fetch Fix push with refspecs containing wildcards
2 parents 52912cc + bcdb34f commit 684a93d

File tree

2 files changed

+109
-12
lines changed

2 files changed

+109
-12
lines changed

remote.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -501,16 +501,16 @@ static struct ref *find_ref_by_name(struct ref *list, const char *name)
501501
return NULL;
502502
}
503503

504-
static int check_pattern_match(struct refspec *rs, int rs_nr, struct ref *src)
504+
static const struct refspec *check_pattern_match(const struct refspec *rs,
505+
int rs_nr,
506+
const struct ref *src)
505507
{
506508
int i;
507-
if (!rs_nr)
508-
return 1;
509509
for (i = 0; i < rs_nr; i++) {
510510
if (rs[i].pattern && !prefixcmp(src->name, rs[i].src))
511-
return 1;
511+
return rs + i;
512512
}
513-
return 0;
513+
return NULL;
514514
}
515515

516516
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
@@ -525,29 +525,44 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
525525
/* pick the remainder */
526526
for ( ; src; src = src->next) {
527527
struct ref *dst_peer;
528+
const struct refspec *pat = NULL;
529+
char *dst_name;
528530
if (src->peer_ref)
529531
continue;
530-
if (!check_pattern_match(rs, nr_refspec, src))
531-
continue;
532+
if (nr_refspec) {
533+
pat = check_pattern_match(rs, nr_refspec, src);
534+
if (!pat)
535+
continue;
536+
}
532537

533-
dst_peer = find_ref_by_name(dst, src->name);
538+
if (pat) {
539+
dst_name = xmalloc(strlen(pat->dst) +
540+
strlen(src->name) -
541+
strlen(pat->src) + 2);
542+
strcpy(dst_name, pat->dst);
543+
strcat(dst_name, src->name + strlen(pat->src));
544+
} else
545+
dst_name = strdup(src->name);
546+
dst_peer = find_ref_by_name(dst, dst_name);
534547
if (dst_peer && dst_peer->peer_ref)
535548
/* We're already sending something to this ref. */
536-
continue;
549+
goto free_name;
537550
if (!dst_peer && !nr_refspec && !all)
538551
/* Remote doesn't have it, and we have no
539552
* explicit pattern, and we don't have
540553
* --all. */
541-
continue;
554+
goto free_name;
542555
if (!dst_peer) {
543556
/* Create a new one and link it */
544-
int len = strlen(src->name) + 1;
557+
int len = strlen(dst_name) + 1;
545558
dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
546-
memcpy(dst_peer->name, src->name, len);
559+
memcpy(dst_peer->name, dst_name, len);
547560
hashcpy(dst_peer->new_sha1, src->new_sha1);
548561
link_dst_tail(dst_peer, dst_tail);
549562
}
550563
dst_peer->peer_ref = src;
564+
free_name:
565+
free(dst_name);
551566
}
552567
return 0;
553568
}

t/t5516-fetch-push.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
3+
test_description='fetching and pushing, with or without wildcard'
4+
5+
. ./test-lib.sh
6+
7+
D=`pwd`
8+
9+
mk_empty () {
10+
rm -fr testrepo &&
11+
mkdir testrepo &&
12+
(
13+
cd testrepo &&
14+
git init
15+
)
16+
}
17+
18+
test_expect_success setup '
19+
20+
: >path1 &&
21+
git add path1 &&
22+
test_tick &&
23+
git commit -a -m repo &&
24+
the_commit=$(git show-ref -s --verify refs/heads/master)
25+
26+
'
27+
28+
test_expect_success 'fetch without wildcard' '
29+
mk_empty &&
30+
(
31+
cd testrepo &&
32+
git fetch .. refs/heads/master:refs/remotes/origin/master &&
33+
34+
r=$(git show-ref -s --verify refs/remotes/origin/master) &&
35+
test "z$r" = "z$the_commit" &&
36+
37+
test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
38+
)
39+
'
40+
41+
test_expect_success 'fetch with wildcard' '
42+
mk_empty &&
43+
(
44+
cd testrepo &&
45+
git config remote.up.url .. &&
46+
git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
47+
git fetch up &&
48+
49+
r=$(git show-ref -s --verify refs/remotes/origin/master) &&
50+
test "z$r" = "z$the_commit" &&
51+
52+
test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
53+
)
54+
'
55+
56+
test_expect_success 'push without wildcard' '
57+
mk_empty &&
58+
59+
git push testrepo refs/heads/master:refs/remotes/origin/master &&
60+
(
61+
cd testrepo &&
62+
r=$(git show-ref -s --verify refs/remotes/origin/master) &&
63+
test "z$r" = "z$the_commit" &&
64+
65+
test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
66+
)
67+
'
68+
69+
test_expect_success 'push with wildcard' '
70+
mk_empty &&
71+
72+
git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
73+
(
74+
cd testrepo &&
75+
r=$(git show-ref -s --verify refs/remotes/origin/master) &&
76+
test "z$r" = "z$the_commit" &&
77+
78+
test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
79+
)
80+
'
81+
82+
test_done

0 commit comments

Comments
 (0)