Skip to content

Commit d27b876

Browse files
jlehmanngitster
authored andcommitted
git submodule add: Require the new --force option to add ignored paths
To make the behavior of "git submodule add" more consistent with "git add" ignored submodule paths should not be silently added when they match an entry in a .gitignore file. To be able to override that default behavior in the same way as we can do that for "git add", the new option "--force" is introduced. Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Acked-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8fbe9b3 commit d27b876

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

Documentation/git-submodule.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-submodule - Initialize, update or inspect submodules
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git submodule' [--quiet] add [-b branch]
12+
'git submodule' [--quiet] add [-b branch] [-f|--force]
1313
[--reference <repository>] [--] <repository> [<path>]
1414
'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
1515
'git submodule' [--quiet] init [--] [<path>...]
@@ -187,6 +187,11 @@ OPTIONS
187187
--branch::
188188
Branch of repository to add as submodule.
189189

190+
-f::
191+
--force::
192+
This option is only valid for the add command.
193+
Allow adding an otherwise ignored submodule path.
194+
190195
--cached::
191196
This option is only valid for status and summary commands. These
192197
commands typically use the commit found in the submodule HEAD, but

git-submodule.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Copyright (c) 2007 Lars Hjemli
66

77
dashless=$(basename "$0" | sed -e 's/-/ /')
8-
USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> [<path>]
8+
USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
99
or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1010
or: $dashless [--quiet] init [--] [<path>...]
1111
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
@@ -19,6 +19,7 @@ require_work_tree
1919

2020
command=
2121
branch=
22+
force=
2223
reference=
2324
cached=
2425
recursive=
@@ -133,6 +134,9 @@ cmd_add()
133134
branch=$2
134135
shift
135136
;;
137+
-f | --force)
138+
force=$1
139+
;;
136140
-q|--quiet)
137141
GIT_QUIET=1
138142
;;
@@ -201,6 +205,14 @@ cmd_add()
201205
git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
202206
die "'$path' already exists in the index"
203207

208+
if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
209+
then
210+
echo >&2 "The following path is ignored by one of your .gitignore files:" &&
211+
echo >&2 $path &&
212+
echo >&2 "Use -f if you really want to add it."
213+
exit 1
214+
fi
215+
204216
# perhaps the path exists and is already a git repo, else clone it
205217
if test -e "$path"
206218
then
@@ -234,7 +246,7 @@ cmd_add()
234246
) || die "Unable to checkout submodule '$path'"
235247
fi
236248

237-
git add --force "$path" ||
249+
git add $force "$path" ||
238250
die "Failed to add submodule '$path'"
239251

240252
git config -f .gitmodules submodule."$path".path "$path" &&

t/t7400-submodule-basic.sh

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,28 @@ test_expect_success 'submodule add' '
8686
test_cmp empty untracked
8787
'
8888

89-
test_expect_success 'submodule add to .gitignored path' '
90-
echo "refs/heads/master" >expect &&
91-
>empty &&
92-
89+
test_expect_success 'submodule add to .gitignored path fails' '
9390
(
9491
cd addtest-ignore &&
92+
cat <<-\EOF >expect &&
93+
The following path is ignored by one of your .gitignore files:
94+
submod
95+
Use -f if you really want to add it.
96+
EOF
9597
# Does not use test_commit due to the ignore
9698
echo "*" > .gitignore &&
9799
git add --force .gitignore &&
98100
git commit -m"Ignore everything" &&
99-
git submodule add "$submodurl" submod &&
100-
git submodule init
101-
) &&
101+
! git submodule add "$submodurl" submod >actual 2>&1 &&
102+
test_cmp expect actual
103+
)
104+
'
102105

103-
rm -f heads head untracked &&
104-
inspect addtest/submod ../.. &&
105-
test_cmp expect heads &&
106-
test_cmp expect head &&
107-
test_cmp empty untracked
106+
test_expect_success 'submodule add to .gitignored path with --force' '
107+
(
108+
cd addtest-ignore &&
109+
git submodule add --force "$submodurl" submod
110+
)
108111
'
109112

110113
test_expect_success 'submodule add --branch' '

0 commit comments

Comments
 (0)