{"thread":{"id":"16970","subject":"[RESEND PATCH] git add: do not add files from a submodule","startedAt":"2009-01-02T18:08:40Z","lastAt":"2009-01-06T08:17:56Z","messageCount":2,"participants":["Johannes Schindelin","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"99181","messageId":"alpine.DEB.1.00.0901021715370.27818@racer","threadId":"16970","inReplyTo":null,"subject":"[RESEND PATCH] git add: do not add files from a submodule","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2009-01-02T18:08:40Z","receivedAt":"2009-01-02T18:08:40Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nIt comes quite as a surprise to an unsuspecting Git user that calling\n\"git add submodule/file\" (which is a mistake, alright) _removes_\nthe submodule in the index, and adds the file.  Instead, complain loudly.\n\nWhile at it, be nice when the user said \"git add submodule/\" which is\nmost likely the consequence of tab-completion, and stage the submodule,\ninstead of trying to add the contents of that directory.\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n\n\tOkay, so I let this slip for way too long.  AFAIR I waited for\n\ta few patches to go into 'next', and it was in an -rc cycle.\n\n\tHappily, the patches seem to be in, and we're out of -rc.\n\n\tLooking over the patch again, I do not find anything obviously\n\twrong, but certainly somebody will.\n\n builtin-add.c              |   28 ++++++++++++++++++++++++++++\n t/t7400-submodule-basic.sh |   25 +++++++++++++++++++++++++\n 2 files changed, 53 insertions(+), 0 deletions(-)\n\ndiff --git a/builtin-add.c b/builtin-add.c\nindex 719de8b..ac98c83 100644\n--- a/builtin-add.c\n+++ b/builtin-add.c\n@@ -68,6 +68,33 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p\n         free(seen);\n }\n \n+static void treat_gitlinks(const char **pathspec)\n+{\n+\tint i;\n+\n+\tif (!pathspec || !*pathspec)\n+\t\treturn;\n+\n+\tfor (i = 0; i < active_nr; i++) {\n+\t\tstruct cache_entry *ce = active_cache[i];\n+\t\tif (S_ISGITLINK(ce->ce_mode)) {\n+\t\t\tint len = ce_namelen(ce), j;\n+\t\t\tfor (j = 0; pathspec[j]; j++) {\n+\t\t\t\tint len2 = strlen(pathspec[j]);\n+\t\t\t\tif (len2 <= len || pathspec[j][len] != '/' ||\n+\t\t\t\t    memcmp(ce->name, pathspec[j], len))\n+\t\t\t\t\tcontinue;\n+\t\t\t\tif (len2 == len + 1)\n+\t\t\t\t\t/* strip trailing slash */\n+\t\t\t\t\tpathspec[j] = xstrndup(ce->name, len);\n+\t\t\t\telse\n+\t\t\t\t\tdie (\"Path '%s' is in submodule '%.*s'\",\n+\t\t\t\t\t\tpathspec[j], len, ce->name);\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n static void fill_directory(struct dir_struct *dir, const char **pathspec,\n \t\tint ignored_too)\n {\n@@ -261,6 +288,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \n \tif (read_cache() < 0)\n \t\tdie(\"index file corrupt\");\n+\ttreat_gitlinks(pathspec);\n \n \tif (add_new_files)\n \t\t/* This picks up the paths that are not tracked */\ndiff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh\nindex be73f7b..2ec7ac6 100755\n--- a/t/t7400-submodule-basic.sh\n+++ b/t/t7400-submodule-basic.sh\n@@ -209,4 +209,29 @@ test_expect_success 'update --init' '\n \n '\n \n+test_expect_success 'do not add files from a submodule' '\n+\n+\tgit reset --hard &&\n+\ttest_must_fail git add init/a\n+\n+'\n+\n+test_expect_success 'gracefully add submodule with a trailing slash' '\n+\n+\tgit reset --hard &&\n+\tgit commit -m \"commit subproject\" init &&\n+\t(cd init &&\n+\t echo b > a) &&\n+\tgit add init/ &&\n+\tgit diff --exit-code --cached init &&\n+\tcommit=$(cd init &&\n+\t git commit -m update a >/dev/null &&\n+\t git rev-parse HEAD) &&\n+\tgit add init/ &&\n+\ttest_must_fail git diff --exit-code --cached init &&\n+\ttest $commit = $(git ls-files --stage |\n+\t\tsed -n \"s/^160000 \\([^ ]*\\).*/\\1/p\")\n+\n+'\n+\n test_done\n-- \n1.6.1.rc3.224.g95ac9\n"},{"id":"99430","messageId":"7vy6xoluff.fsf@gitster.siamese.dyndns.org","threadId":"16970","inReplyTo":"alpine.DEB.1.00.0901021715370.27818@racer","subject":"Re: [RESEND PATCH] git add: do not add files from a submodule","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-01-06T08:17:56Z","receivedAt":"2009-01-06T08:17:56Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Makes sense, thanks; queued for maint.\n"}]}