@matheusbsbs / matheus-bagliano / commits / 31991b0260
Fork 0

git submodule: add submodules with git add -f <path>

Change `git submodule add' to add the new submodule <path> with `git add --force'. I keep my /etc in .git with a .gitignore that contains just "*". I.e. `git status' will ignore everything that isn't in the tree already. When I do: git submodule add <url> hlagh git-submodule will get as far as checking out the remote repository into hlagh, but it'll die right afterwards when it fails to add the new path: The following paths are ignored by one of your .gitignore files: hlagh Use -f if you really want to add them. fatal: no files added Failed to add submodule 'hlagh' Currently there's no way to add a submodule in this situation other than to remove the ignored path from the .gitignore while I'm at it. That's silly, when you run `git submodule add' you're explicitly saying that you want to add something *new* to the repository. Instead it should just add the path with `git add --force'. Initially I implemented this by adding new -f and --force options to `git submodule add'. But if the --force option isn't supplied it'll get as far as cloning `hlagh', but won't add it. So the first thing the user has to do is to remove `hlagh' and then try again with the --force option. That sucks, it should just add the path to begin with. I can't think of any usecase where you've gone through the trouble of typing out `git submodule add ..', but wish to be overriden by a `gitignore'. The submodule semantics should be more like `git init', not `git add'. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jul 5, 2010 at 17:33 UTC 31991b0260216d16f2e447e68bcabd9d9cce26ba
3 files changed +29 -3
Documentation/git-submodule.txt
+4
@@ -95,6 +95,10 @@ is the superproject and submodule repositories will be kept
95 together in the same relative location, and only the
96 superproject's URL needs to be provided: git-submodule will correctly
97 locate the submodule using the relative URL in .gitmodules.
98 ++
99 +The submodule will be added with "git add --force <path>". I.e. git
100 +doesn't care if the new path is in a `gitignore`. Your invocation of
101 +"git submodule add" is considered enough to override it.
102
103 status::
104 Show the status of the submodules. This will print the SHA-1 of the
git-submodule.sh
+2 -2
@@ -234,12 +234,12 @@ cmd_add()
234 ) || die "Unable to checkout submodule '$path'"
235 fi
236
237 - git add "$path" ||
237 + git add --force "$path" ||
238 die "Failed to add submodule '$path'"
239
240 git config -f .gitmodules submodule."$path".path "$path" &&
241 git config -f .gitmodules submodule."$path".url "$repo" &&
242 - git add .gitmodules ||
242 + git add --force .gitmodules ||
243 die "Failed to register submodule '$path'"
244 }
245
t/t7400-submodule-basic.sh
+23 -1
@@ -42,7 +42,8 @@ test_expect_success 'setup - hide init subdirectory' '
42 '
43
44 test_expect_success 'setup - repository to add submodules to' '
45 - git init addtest
45 + git init addtest &&
46 + git init addtest-ignore
47 '
48
49 # The 'submodule add' tests need some repository to add as a submodule.
@@ -85,6 +86,27 @@ 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 +
93 + (
94 + cd addtest-ignore &&
95 + # Does not use test_commit due to the ignore
96 + echo "*" > .gitignore &&
97 + git add --force .gitignore &&
98 + git commit -m"Ignore everything" &&
99 + git submodule add "$submodurl" submod &&
100 + git submodule init
101 + ) &&
102 +
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
108 +'
109 +
110 test_expect_success 'submodule add --branch' '
111 echo "refs/heads/initial" >expect-head &&
112 cat <<-\EOF >expect-heads &&