Skip to content

Commit 1c0fa76

Browse files
committed
Merge branch 'hv/submodule-path-unmatch'
* hv/submodule-path-unmatch: Let submodule command exit with error status if path does not exist
2 parents 97349a2 + be9d0a3 commit 1c0fa76

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

git-submodule.sh

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,48 @@ resolve_relative_url ()
109109
#
110110
module_list()
111111
{
112-
git ls-files --error-unmatch --stage -- "$@" |
112+
(
113+
git ls-files --error-unmatch --stage -- "$@" ||
114+
echo "unmatched pathspec exists"
115+
) |
113116
perl -e '
114117
my %unmerged = ();
115118
my ($null_sha1) = ("0" x 40);
119+
my @out = ();
120+
my $unmatched = 0;
116121
while (<STDIN>) {
122+
if (/^unmatched pathspec/) {
123+
$unmatched = 1;
124+
next;
125+
}
117126
chomp;
118127
my ($mode, $sha1, $stage, $path) =
119128
/^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
120129
next unless $mode eq "160000";
121130
if ($stage ne "0") {
122131
if (!$unmerged{$path}++) {
123-
print "$mode $null_sha1 U\t$path\n";
132+
push @out, "$mode $null_sha1 U\t$path\n";
124133
}
125134
next;
126135
}
127-
print "$_\n";
136+
push @out, "$_\n";
137+
}
138+
if ($unmatched) {
139+
print "#unmatched\n";
140+
} else {
141+
print for (@out);
128142
}
129143
'
130144
}
131145

146+
die_if_unmatched ()
147+
{
148+
if test "$1" = "#unmatched"
149+
then
150+
exit 1
151+
fi
152+
}
153+
132154
#
133155
# Map submodule path to submodule name
134156
#
@@ -385,6 +407,7 @@ cmd_foreach()
385407
module_list |
386408
while read mode sha1 stage sm_path
387409
do
410+
die_if_unmatched "$mode"
388411
if test -e "$sm_path"/.git
389412
then
390413
say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
@@ -437,6 +460,7 @@ cmd_init()
437460
module_list "$@" |
438461
while read mode sha1 stage sm_path
439462
do
463+
die_if_unmatched "$mode"
440464
name=$(module_name "$sm_path") || exit
441465

442466
# Copy url setting when it is not set yet
@@ -537,6 +561,7 @@ cmd_update()
537561
err=
538562
while read mode sha1 stage sm_path
539563
do
564+
die_if_unmatched "$mode"
540565
if test "$stage" = U
541566
then
542567
echo >&2 "Skipping unmerged submodule $sm_path"
@@ -932,6 +957,7 @@ cmd_status()
932957
module_list "$@" |
933958
while read mode sha1 stage sm_path
934959
do
960+
die_if_unmatched "$mode"
935961
name=$(module_name "$sm_path") || exit
936962
url=$(git config submodule."$name".url)
937963
displaypath="$prefix$sm_path"
@@ -1000,6 +1026,7 @@ cmd_sync()
10001026
module_list "$@" |
10011027
while read mode sha1 stage sm_path
10021028
do
1029+
die_if_unmatched "$mode"
10031030
name=$(module_name "$sm_path")
10041031
url=$(git config -f .gitmodules --get submodule."$name".url)
10051032

t/t7400-submodule-basic.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,27 @@ test_expect_success 'init should register submodule url in .git/config' '
258258
test_cmp expect url
259259
'
260260

261+
test_failure_with_unknown_submodule () {
262+
test_must_fail git submodule $1 no-such-submodule 2>output.err &&
263+
grep "^error: .*no-such-submodule" output.err
264+
}
265+
266+
test_expect_success 'init should fail with unknown submodule' '
267+
test_failure_with_unknown_submodule init
268+
'
269+
270+
test_expect_success 'update should fail with unknown submodule' '
271+
test_failure_with_unknown_submodule update
272+
'
273+
274+
test_expect_success 'status should fail with unknown submodule' '
275+
test_failure_with_unknown_submodule status
276+
'
277+
278+
test_expect_success 'sync should fail with unknown submodule' '
279+
test_failure_with_unknown_submodule sync
280+
'
281+
261282
test_expect_success 'update should fail when path is used by a file' '
262283
echo hello >expect &&
263284
@@ -418,10 +439,7 @@ test_expect_success 'moving to a commit without submodule does not leave empty d
418439
'
419440

420441
test_expect_success 'submodule <invalid-path> warns' '
421-
422-
git submodule no-such-submodule 2> output.err &&
423-
grep "^error: .*no-such-submodule" output.err
424-
442+
test_failure_with_unknown_submodule
425443
'
426444

427445
test_expect_success 'add submodules without specifying an explicit path' '

0 commit comments

Comments
 (0)