submodule add: extend force flag to add existing repos
authorStefan Beller <sbeller@google.com>
Thu, 6 Oct 2016 19:37:24 +0000 (12:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 29 Nov 2016 22:25:09 +0000 (14:25 -0800)
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>
git-submodule.sh
t/t7400-submodule-basic.sh
index a024a135d6663c8a8d5ceb926e3f42df1f577411..3762616b5cf2cb0c5bc02abc30cc2ae83741770c 100755 (executable)
@@ -207,8 +207,14 @@ cmd_add()
                        tstart
                        s|/*$||
                ')
-       git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
-       die "$(eval_gettext "'\$sm_path' already exists in the index")"
+       if test -z "$force"
+       then
+               git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
+               die "$(eval_gettext "'\$sm_path' already exists in the index")"
+       else
+               git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
+               die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
+       fi
 
        if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
        then
index b77cce8e4066ac7f46ba1a02eab7215a4cdeaaae..c09ce0d4c1dbd8bc49595be4e3928032fbdc3c8c 100755 (executable)
@@ -152,6 +152,20 @@ test_expect_success 'submodule add to .gitignored path with --force' '
        )
 '
 
+test_expect_success 'submodule add to reconfigure existing submodule with --force' '
+       (
+               cd addtest-ignore &&
+               git submodule add --force bogus-url submod &&
+               git submodule add -b initial "$submodurl" submod-branch &&
+               test "bogus-url" = "$(git config -f .gitmodules submodule.submod.url)" &&
+               test "bogus-url" = "$(git config submodule.submod.url)" &&
+               # Restore the url
+               git submodule add --force "$submodurl" submod
+               test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
+               test "$submodurl" = "$(git config submodule.submod.url)"
+       )
+'
+
 test_expect_success 'submodule add --branch' '
        echo "refs/heads/initial" >expect-head &&
        cat <<-\EOF >expect-heads &&