submodule add: extend force flag to add existing repos

Currently the force flag in `git submodule add` takes care of possibly ignored files or when a name collision occurs. However there is another situation where submodule add comes in handy: When you already have a gitlink recorded, but no configuration was done (i.e. no .gitmodules file nor any entry in .git/config) and you want to generate these config entries. For this situation allow `git submodule add` to proceed if there is already a submodule at the given path in the index. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed Oct 6, 2016 at 12:37 UTC 619acfc78c526bc9df17b7704e60d0512ab7a84c
2 files changed +22 -2
git-submodule.sh
+8 -2
@@ -207,8 +207,14 @@ cmd_add()
207 tstart
208 s|/*$||
209 ')
210 - git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
211 - die "$(eval_gettext "'\$sm_path' already exists in the index")"
210 + if test -z "$force"
211 + then
212 + git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
213 + die "$(eval_gettext "'\$sm_path' already exists in the index")"
214 + else
215 + git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
216 + die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
217 + fi
218
219 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
220 then
t/t7400-submodule-basic.sh
+14
@@ -152,6 +152,20 @@ test_expect_success 'submodule add to .gitignored path with --force' '
152 )
153 '
154
155 +test_expect_success 'submodule add to reconfigure existing submodule with --force' '
156 + (
157 + cd addtest-ignore &&
158 + git submodule add --force bogus-url submod &&
159 + git submodule add -b initial "$submodurl" submod-branch &&
160 + test "bogus-url" = "$(git config -f .gitmodules submodule.submod.url)" &&
161 + test "bogus-url" = "$(git config submodule.submod.url)" &&
162 + # Restore the url
163 + git submodule add --force "$submodurl" submod
164 + test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
165 + test "$submodurl" = "$(git config submodule.submod.url)"
166 + )
167 +'
168 +
169 test_expect_success 'submodule add --branch' '
170 echo "refs/heads/initial" >expect-head &&
171 cat <<-\EOF >expect-heads &&