@matheusbsbs / matheus-bagliano / commits / 2ce53f9b77
Fork 0

git add: do not add files from a submodule

It comes quite as a surprise to an unsuspecting Git user that calling "git add submodule/file" (which is a mistake, alright) _removes_ the submodule in the index, and adds the file. Instead, complain loudly. While at it, be nice when the user said "git add submodule/" which is most likely the consequence of tab-completion, and stage the submodule, instead of trying to add the contents of that directory. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 2, 2009 at 19:08 UTC 2ce53f9b77eb36c6d1286f55fba65f8dc66b2564
2 files changed +53
builtin-add.c
+28
@@ -68,6 +68,33 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
68 free(seen);
69 }
70
71 +static void treat_gitlinks(const char **pathspec)
72 +{
73 + int i;
74 +
75 + if (!pathspec || !*pathspec)
76 + return;
77 +
78 + for (i = 0; i < active_nr; i++) {
79 + struct cache_entry *ce = active_cache[i];
80 + if (S_ISGITLINK(ce->ce_mode)) {
81 + int len = ce_namelen(ce), j;
82 + for (j = 0; pathspec[j]; j++) {
83 + int len2 = strlen(pathspec[j]);
84 + if (len2 <= len || pathspec[j][len] != '/' ||
85 + memcmp(ce->name, pathspec[j], len))
86 + continue;
87 + if (len2 == len + 1)
88 + /* strip trailing slash */
89 + pathspec[j] = xstrndup(ce->name, len);
90 + else
91 + die ("Path '%s' is in submodule '%.*s'",
92 + pathspec[j], len, ce->name);
93 + }
94 + }
95 + }
96 +}
97 +
98 static void fill_directory(struct dir_struct *dir, const char **pathspec,
99 int ignored_too)
100 {
@@ -261,6 +288,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
288
289 if (read_cache() < 0)
290 die("index file corrupt");
291 + treat_gitlinks(pathspec);
292
293 if (add_new_files)
294 /* This picks up the paths that are not tracked */
t/t7400-submodule-basic.sh
+25
@@ -209,4 +209,29 @@ test_expect_success 'update --init' '
209
210 '
211
212 +test_expect_success 'do not add files from a submodule' '
213 +
214 + git reset --hard &&
215 + test_must_fail git add init/a
216 +
217 +'
218 +
219 +test_expect_success 'gracefully add submodule with a trailing slash' '
220 +
221 + git reset --hard &&
222 + git commit -m "commit subproject" init &&
223 + (cd init &&
224 + echo b > a) &&
225 + git add init/ &&
226 + git diff --exit-code --cached init &&
227 + commit=$(cd init &&
228 + git commit -m update a >/dev/null &&
229 + git rev-parse HEAD) &&
230 + git add init/ &&
231 + test_must_fail git diff --exit-code --cached init &&
232 + test $commit = $(git ls-files --stage |
233 + sed -n "s/^160000 \([^ ]*\).*/\1/p")
234 +
235 +'
236 +
237 test_done