git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH/RFC] git add: do not add files from a submodule
Date: Mon, 21 Jul 2008 02:58:40 +0200 (CEST)	[thread overview]
Message-ID: <alpine.DEB.1.00.0807210256510.3305@eeepc-johanness> (raw)
In-Reply-To: <alpine.DEB.1.00.0807201529150.3305@eeepc-johanness>


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>
---

	This would need a companion patch for "git diff submodule/",
	obviously.  However, revision.c does not need a valid cache,
	usually.  So I am hesitant.

	Oh, and I am sure somebody will come up with a more elegant 
	solution to this problem.  I sure do not, having smashed my head 
	against the wall for a few hours.

 builtin-add.c              |   42 +++++++++++++++++++++++++++++++++++++++---
 t/t7400-submodule-basic.sh |   18 ++++++++++++++++++
 2 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/builtin-add.c b/builtin-add.c
index 6f5672a..9453557 100644
--- a/builtin-add.c
+++ b/builtin-add.c
@@ -50,6 +50,33 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
         free(seen);
 }
 
+static void treat_gitlinks(const char **pathspec, struct index_state *index)
+{
+	int i;
+
+	if (!pathspec || !*pathspec)
+		return;
+
+	for (i = 0; i < index->cache_nr; i++) {
+		struct cache_entry *ce = index->cache[i];
+		if (S_ISGITLINK(ce->ce_mode)) {
+			int len = ce_namelen(ce), j;
+			for (j = 0; pathspec[j]; j++) {
+				int len2 = strlen(pathspec[j]);
+				if (len2 <= len || pathspec[j][len] != '/' ||
+				    memcmp(ce->name, pathspec[j], len))
+					continue;
+				if (len2 == len + 1)
+					/* strip trailing slash */
+					pathspec[j] = xstrndup(ce->name, len);
+				else
+					die ("Path '%s' is in submodule '%.*s'",
+						pathspec[j], len, ce->name);
+			}
+		}
+	}
+}
+
 static void fill_directory(struct dir_struct *dir, const char **pathspec,
 		int ignored_too)
 {
@@ -245,6 +272,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	int flags;
 	int add_new_files;
 	int require_pathspec;
+	struct index_state index;
 
 	argc = parse_options(argc, argv, builtin_add_options,
 			  builtin_add_usage, 0);
@@ -283,12 +311,20 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	 * If we are adding new files, we need to scan the working
 	 * tree to find the ones that match pathspecs; this needs
 	 * to be done before we read the index.
+	 *
+	 * However, to avoid adding files from submodules, we have to
+	 * read the index first.  So read the index into a local
+	 * variable, and set the global index after fill_directory().
 	 */
-	if (add_new_files)
-		fill_directory(&dir, pathspec, ignored_too);
 
-	if (read_cache() < 0)
+	memset(&index, 0, sizeof(index));
+	if (read_index(&index) < 0)
 		die("index file corrupt");
+	treat_gitlinks(pathspec, &index);
+
+	if (add_new_files)
+		fill_directory(&dir, pathspec, ignored_too);
+	the_index = index;
 
 	if (refresh_only) {
 		refresh(verbose, pathspec);
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
index cbc0c34..6da2545 100755
--- a/t/t7400-submodule-basic.sh
+++ b/t/t7400-submodule-basic.sh
@@ -209,4 +209,22 @@ test_expect_success 'update --init' '
 
 '
 
+test_expect_success 'do not add files from a submodule' '
+
+	git reset --hard &&
+	test_must_fail git add init/a
+
+'
+
+test_expect_success 'gracefully add submodule with a trailing slash' '
+
+	commit=$(cd init &&
+	 echo b > a &&
+	 git commit -m update a >/dev/null &&
+	 git rev-parse HEAD) &&
+	git add init/ &&
+	test_must_fail git diff --exit-code --cached init
+
+'
+
 test_done
-- 
1.5.6.2.516.g22071

  reply	other threads:[~2008-07-21  0:58 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-20  6:09 [PATCH v2 1/4] builtin-add.c: restructure the code for maintainability Junio C Hamano
2008-07-20  6:09 ` [PATCH v2 2/4] git-add --all: add all files Junio C Hamano
2008-07-20  6:09   ` [PATCH v2 3/4] git-add --all: tests Junio C Hamano
2008-07-20  6:09     ` [PATCH v2 4/4] git-add --all: documentation Junio C Hamano
2008-07-21  0:56 ` [PATCH v2 1/4] builtin-add.c: restructure the code for maintainability Johannes Schindelin
2008-07-21  0:58   ` Johannes Schindelin [this message]
2008-07-22 21:32     ` [PATCH/RFC] git add: do not add files from a submodule Johannes Schindelin
2008-07-23  6:40       ` Junio C Hamano
2008-07-23  8:13         ` Pierre Habouzit
2008-07-23 18:31           ` Junio C Hamano
2008-07-23 19:02             ` Pierre Habouzit
2008-07-23 19:10               ` Johannes Schindelin
2008-07-23 19:11                 ` Pierre Habouzit
2008-07-21  5:22   ` [PATCH v2 1/4] builtin-add.c: restructure the code for maintainability Junio C Hamano
2008-07-21  7:52     ` Junio C Hamano
2008-07-21  8:24       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.1.00.0807210256510.3305@eeepc-johanness \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).