@matheusbsbs / matheus-bagliano / commits / 6125796f7d
Fork 0

remote.c: fix "git push" weak match disambiguation

When "git push A:B" is given, and A (or B) is not a full refname that begins with refs/, we require an unambiguous match with an existing ref. For this purpose, a match with a local branch or a tag (i.e. refs/heads/A and refs/tags/A) is called a "strong match", and any other match is called a "weak match". A partial refname is unambiguous when there is only one strong match with any number of weak matches, or when there is only one weak match and no other match. However, as reported by Sparse with Ramsay Jones recently, count_refspec_match() function had a bug where a variable in an inner block masked a different variable of the same name, which caused the weak matches to be ignored. This fixes it, and adds tests for the fix. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 9, 2007 at 01:23 UTC 6125796f7d6e8b84431f92c13d7e79bd30f94f53
2 files changed +112 -1
remote.c
-1
@@ -333,7 +333,6 @@ static int count_refspec_match(const char *pattern,
333 for (weak_match = match = 0; refs; refs = refs->next) {
334 char *name = refs->name;
335 int namelen = strlen(name);
336 - int weak_match;
336
337 if (namelen < patlen ||
338 memcmp(name + namelen - patlen, pattern, patlen))
t/t5516-fetch-push.sh
+112
@@ -15,12 +15,58 @@ mk_empty () {
15 )
16 }
17
18 +mk_test () {
19 + mk_empty &&
20 + (
21 + for ref in "$@"
22 + do
23 + git push testrepo $the_first_commit:refs/$ref || {
24 + echo "Oops, push refs/$ref failure"
25 + exit 1
26 + }
27 + done &&
28 + cd testrepo &&
29 + for ref in "$@"
30 + do
31 + r=$(git show-ref -s --verify refs/$ref) &&
32 + test "z$r" = "z$the_first_commit" || {
33 + echo "Oops, refs/$ref is wrong"
34 + exit 1
35 + }
36 + done &&
37 + git fsck --full
38 + )
39 +}
40 +
41 +check_push_result () {
42 + (
43 + cd testrepo &&
44 + it="$1" &&
45 + shift
46 + for ref in "$@"
47 + do
48 + r=$(git show-ref -s --verify refs/$ref) &&
49 + test "z$r" = "z$it" || {
50 + echo "Oops, refs/$ref is wrong"
51 + exit 1
52 + }
53 + done &&
54 + git fsck --full
55 + )
56 +}
57 +
58 test_expect_success setup '
59
60 : >path1 &&
61 git add path1 &&
62 test_tick &&
63 git commit -a -m repo &&
64 + the_first_commit=$(git show-ref -s --verify refs/heads/master) &&
65 +
66 + : >path2 &&
67 + git add path2 &&
68 + test_tick &&
69 + git commit -a -m second &&
70 the_commit=$(git show-ref -s --verify refs/heads/master)
71
72 '
@@ -79,4 +125,70 @@ test_expect_success 'push with wildcard' '
125 )
126 '
127
128 +test_expect_success 'push with matching heads' '
129 +
130 + mk_test heads/master &&
131 + git push testrepo &&
132 + check_push_result $the_commit heads/master
133 +
134 +'
135 +
136 +test_expect_success 'push with no ambiguity (1)' '
137 +
138 + mk_test heads/master &&
139 + git push testrepo master:master &&
140 + check_push_result $the_commit heads/master
141 +
142 +'
143 +
144 +test_expect_success 'push with no ambiguity (2)' '
145 +
146 + mk_test remotes/origin/master &&
147 + git push testrepo master:master &&
148 + check_push_result $the_commit remotes/origin/master
149 +
150 +'
151 +
152 +test_expect_success 'push with weak ambiguity (1)' '
153 +
154 + mk_test heads/master remotes/origin/master &&
155 + git push testrepo master:master &&
156 + check_push_result $the_commit heads/master &&
157 + check_push_result $the_first_commit remotes/origin/master
158 +
159 +'
160 +
161 +test_expect_success 'push with weak ambiguity (2)' '
162 +
163 + mk_test heads/master remotes/origin/master remotes/another/master &&
164 + git push testrepo master:master &&
165 + check_push_result $the_commit heads/master &&
166 + check_push_result $the_first_commit remotes/origin/master remotes/another/master
167 +
168 +'
169 +
170 +test_expect_success 'push with ambiguity (1)' '
171 +
172 + mk_test remotes/origin/master remotes/frotz/master &&
173 + if git push testrepo master:master
174 + then
175 + echo "Oops, should have failed"
176 + false
177 + else
178 + check_push_result $the_first_commit remotes/origin/master remotes/frotz/master
179 + fi
180 +'
181 +
182 +test_expect_success 'push with ambiguity (2)' '
183 +
184 + mk_test heads/frotz tags/frotz &&
185 + if git push testrepo master:frotz
186 + then
187 + echo "Oops, should have failed"
188 + false
189 + else
190 + check_push_result $the_first_commit heads/frotz tags/frotz
191 + fi
192 +'
193 +
194 test_done