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>
This commit is contained in:
Jens Lehmann 2010-07-17 17:11:43 +02:00 коммит произвёл Junio C Hamano
Родитель 8fbe9b32ce
Коммит d27b876b28
3 изменённых файлов: 35 добавлений и 15 удалений

Просмотреть файл

@ -9,7 +9,7 @@ git-submodule - Initialize, update or inspect submodules
SYNOPSIS SYNOPSIS
-------- --------
[verse] [verse]
'git submodule' [--quiet] add [-b branch] 'git submodule' [--quiet] add [-b branch] [-f|--force]
[--reference <repository>] [--] <repository> [<path>] [--reference <repository>] [--] <repository> [<path>]
'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...] 'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
'git submodule' [--quiet] init [--] [<path>...] 'git submodule' [--quiet] init [--] [<path>...]
@ -187,6 +187,11 @@ OPTIONS
--branch:: --branch::
Branch of repository to add as submodule. Branch of repository to add as submodule.
-f::
--force::
This option is only valid for the add command.
Allow adding an otherwise ignored submodule path.
--cached:: --cached::
This option is only valid for status and summary commands. These This option is only valid for status and summary commands. These
commands typically use the commit found in the submodule HEAD, but commands typically use the commit found in the submodule HEAD, but

Просмотреть файл

@ -5,7 +5,7 @@
# Copyright (c) 2007 Lars Hjemli # Copyright (c) 2007 Lars Hjemli
dashless=$(basename "$0" | sed -e 's/-/ /') dashless=$(basename "$0" | sed -e 's/-/ /')
USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> [<path>] USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>]
or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...] or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
or: $dashless [--quiet] init [--] [<path>...] or: $dashless [--quiet] init [--] [<path>...]
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...] or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
@ -19,6 +19,7 @@ require_work_tree
command= command=
branch= branch=
force=
reference= reference=
cached= cached=
recursive= recursive=
@ -133,6 +134,9 @@ cmd_add()
branch=$2 branch=$2
shift shift
;; ;;
-f | --force)
force=$1
;;
-q|--quiet) -q|--quiet)
GIT_QUIET=1 GIT_QUIET=1
;; ;;
@ -201,6 +205,14 @@ cmd_add()
git ls-files --error-unmatch "$path" > /dev/null 2>&1 && git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
die "'$path' already exists in the index" die "'$path' already exists in the index"
if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
then
echo >&2 "The following path is ignored by one of your .gitignore files:" &&
echo >&2 $path &&
echo >&2 "Use -f if you really want to add it."
exit 1
fi
# perhaps the path exists and is already a git repo, else clone it # perhaps the path exists and is already a git repo, else clone it
if test -e "$path" if test -e "$path"
then then
@ -234,7 +246,7 @@ cmd_add()
) || die "Unable to checkout submodule '$path'" ) || die "Unable to checkout submodule '$path'"
fi fi
git add --force "$path" || git add $force "$path" ||
die "Failed to add submodule '$path'" die "Failed to add submodule '$path'"
git config -f .gitmodules submodule."$path".path "$path" && git config -f .gitmodules submodule."$path".path "$path" &&

Просмотреть файл

@ -86,25 +86,28 @@ test_expect_success 'submodule add' '
test_cmp empty untracked test_cmp empty untracked
' '
test_expect_success 'submodule add to .gitignored path' ' test_expect_success 'submodule add to .gitignored path fails' '
echo "refs/heads/master" >expect &&
>empty &&
( (
cd addtest-ignore && cd addtest-ignore &&
cat <<-\EOF >expect &&
The following path is ignored by one of your .gitignore files:
submod
Use -f if you really want to add it.
EOF
# Does not use test_commit due to the ignore # Does not use test_commit due to the ignore
echo "*" > .gitignore && echo "*" > .gitignore &&
git add --force .gitignore && git add --force .gitignore &&
git commit -m"Ignore everything" && git commit -m"Ignore everything" &&
git submodule add "$submodurl" submod && ! git submodule add "$submodurl" submod >actual 2>&1 &&
git submodule init test_cmp expect actual
) && )
'
rm -f heads head untracked && test_expect_success 'submodule add to .gitignored path with --force' '
inspect addtest/submod ../.. && (
test_cmp expect heads && cd addtest-ignore &&
test_cmp expect head && git submodule add --force "$submodurl" submod
test_cmp empty untracked )
' '
test_expect_success 'submodule add --branch' ' test_expect_success 'submodule add --branch' '