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>
Jens Lehmann committed
Jul 17, 2010 at 17:11 UTC
d27b876b284b8fb82db971bc7cea8c9782614ab2
3 files changed
+35
-15
Documentation/git-submodule.txt
+6
-1
@@ -9,7 +9,7 @@ git-submodule - Initialize, update or inspect submodules
9
SYNOPSIS
10
--------
11
[verse]
12
-'git submodule' [--quiet] add [-b branch]
12
+'git submodule' [--quiet] add [-b branch] [-f|--force]
13
[--reference <repository>] [--] <repository> [<path>]
14
'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
15
'git submodule' [--quiet] init [--] [<path>...]
@@ -187,6 +187,11 @@ OPTIONS
187
--branch::
188
Branch of repository to add as submodule.
189
190
+-f::
191
+--force::
192
+ This option is only valid for the add command.
193
+ Allow adding an otherwise ignored submodule path.
194
+
195
--cached::
196
This option is only valid for status and summary commands. These
197
commands typically use the commit found in the submodule HEAD, but
git-submodule.sh
+14
-2
@@ -5,7 +5,7 @@
5
# Copyright (c) 2007 Lars Hjemli
6
7
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>]
9
or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10
or: $dashless [--quiet] init [--] [<path>...]
11
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
@@ -19,6 +19,7 @@ require_work_tree
19
20
command=
21
branch=
22
+force=
23
reference=
24
cached=
25
recursive=
@@ -133,6 +134,9 @@ cmd_add()
134
branch=$2
135
shift
136
;;
137
+ -f | --force)
138
+ force=$1
139
+ ;;
140
-q|--quiet)
141
GIT_QUIET=1
142
;;
@@ -201,6 +205,14 @@ cmd_add()
205
git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
206
die "'$path' already exists in the index"
207
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
+
216
# perhaps the path exists and is already a git repo, else clone it
217
if test -e "$path"
218
then
@@ -234,7 +246,7 @@ cmd_add()
246
) || die "Unable to checkout submodule '$path'"
247
fi
248
237
- git add --force "$path" ||
249
+ git add $force "$path" ||
250
die "Failed to add submodule '$path'"
251
252
git config -f .gitmodules submodule."$path".path "$path" &&
t/t7400-submodule-basic.sh
+15
-12
@@ -86,25 +86,28 @@ test_expect_success 'submodule add' '
86
test_cmp empty untracked
87
'
88
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' '
90
(
91
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
97
# Does not use test_commit due to the ignore
98
echo "*" > .gitignore &&
99
git add --force .gitignore &&
100
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
+'
105
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
+ )
111
'
112
113
test_expect_success 'submodule add --branch' '