Skip to content

Commit 18f9c98

Browse files
nipunn1313gitster
authored andcommitted
negative-refspec: fix segfault on : refspec
The logic added to check for negative pathspec match by c0192df (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c0192df commit 18f9c98

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

remote.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,13 @@ static int query_matches_negative_refspec(struct refspec *rs, struct refspec_ite
755755

756756
if (match_name_with_pattern(key, needle, value, &expn_name))
757757
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);
761765
}
762766
}
763767

t/t5582-fetch-negative-refspec.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,55 @@ test_expect_success "fetch --prune with negative refspec" '
186186
)
187187
'
188188

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+
189240
test_done

0 commit comments

Comments
 (0)