negative-refspec: fix segfault on : refspec

The logic added to check for negative pathspec match by c0192df630 (refspec: add support for negative refspecs, 2020-09-30) looks at refspec->src assuming it is never NULL, however when remote.origin.push is set to ":", then refspec->src is NULL, causing a segfault within strcmp. Tell git to handle matching refspec by adding the needle to the set of positively matched refspecs, since matching ":" refspecs match anything as src. Add test for matching refspec pushes fetch-negative-refspec both individually and in combination with a negative refspec. Signed-off-by: Nipunn Koorapati <nipunn@dropbox.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nipunn Koorapati committed Dec 22, 2020 at 03:58 UTC 18f9c9884582c743d8ba04ef5cbbe647947d2578
2 files changed +58 -3
remote.c
+7 -3
@@ -755,9 +755,13 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite
755
756 if (match_name_with_pattern(key, needle, value, &expn_name))
757 string_list_append_nodup(&reversed, expn_name);
758 - } else {
759 - if (!strcmp(needle, refspec->src))
760 - string_list_append(&reversed, refspec->src);
758 + } else if (refspec->matching) {
759 + /* For the special matching refspec, any query should match */
760 + string_list_append(&reversed, needle);
761 + } else if (!refspec->src) {
762 + BUG("refspec->src should not be null here");
763 + } else if (!strcmp(needle, refspec->src)) {
764 + string_list_append(&reversed, refspec->src);
765 }
766 }
767
t/t5582-fetch-negative-refspec.sh
+51
@@ -186,4 +186,55 @@ test_expect_success "fetch --prune with negative refspec" '
186 )
187 '
188
189 +test_expect_success "push with matching : and negative refspec" '
190 + # Manually handle cleanup, since test_config is not
191 + # prepared to take arbitrary options like --add
192 + test_when_finished "test_unconfig -C two remote.one.push" &&
193 +
194 + # For convenience, we use "master" to refer to the name of
195 + # the branch created by default in the following.
196 + #
197 + # Repositories two and one have branches other than "master"
198 + # but they have no overlap---"master" is the only one that
199 + # is shared between them. And the master branch at two is
200 + # behind the master branch at one by one commit.
201 + git -C two config --add remote.one.push : &&
202 +
203 + # A matching push tries to update master, fails due to non-ff
204 + test_must_fail git -C two push one &&
205 +
206 + # "master" may actually not be "master"---find it out.
207 + current=$(git symbolic-ref HEAD) &&
208 +
209 + # If master is in negative refspec, then the command will not attempt
210 + # to push and succeed.
211 + git -C two config --add remote.one.push "^$current" &&
212 +
213 + # With "master" excluded, this push is a no-op. Nothing gets
214 + # pushed and it succeeds.
215 + git -C two push -v one
216 +'
217 +
218 +test_expect_success "push with matching +: and negative refspec" '
219 + test_when_finished "test_unconfig -C two remote.one.push" &&
220 +
221 + # The same set-up as above, whose side-effect was a no-op.
222 + git -C two config --add remote.one.push +: &&
223 +
224 + # The push refuses to update the "master" branch that is checked
225 + # out in the "one" repository, even when it is forced with +:
226 + test_must_fail git -C two push one &&
227 +
228 + # "master" may actually not be "master"---find it out.
229 + current=$(git symbolic-ref HEAD) &&
230 +
231 + # If master is in negative refspec, then the command will not attempt
232 + # to push and succeed
233 + git -C two config --add remote.one.push "^$current" &&
234 +
235 + # With "master" excluded, this push is a no-op. Nothing gets
236 + # pushed and it succeeds.
237 + git -C two push -v one
238 +'
239 +
240 test_done