Skip to content

Commit 9e3248e

Browse files
committed
Merge branch 'js/add-not-submodule' into maint
* js/add-not-submodule: git add: do not add files from a submodule
2 parents 5cb0f27 + 2ce53f9 commit 9e3248e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

builtin-add.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,33 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
6868
free(seen);
6969
}
7070

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+
7198
static void fill_directory(struct dir_struct *dir, const char **pathspec,
7299
int ignored_too)
73100
{
@@ -261,6 +288,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
261288

262289
if (read_cache() < 0)
263290
die("index file corrupt");
291+
treat_gitlinks(pathspec);
264292

265293
if (add_new_files)
266294
/* This picks up the paths that are not tracked */

t/t7400-submodule-basic.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,29 @@ test_expect_success 'update --init' '
209209
210210
'
211211

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+
212237
test_done

0 commit comments

Comments
 (0)