git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Fix 'git checkout <submodule>' to update the index

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Apr 2, 2009, 13:30 UTC
Message-ID
<c267451c12c4ead4c222eebe6df460b46edd59c1.1238678601u.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1238678601u.git.johannes.schindelin@gmx.de>

While 'git checkout <submodule>' should not update the submodule's working directory, it should update the index. This is in line with how submodules are handled in the rest of Git.

While at it, test 'git reset [<commit>] <submodule>', too.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	I have no idea who thought that it would be a good idea to leave 
	the submodules alone, but I think it is highly counterintuitive 
	that
		git checkout <submodule>
	does not touch the index.
	This patch fixes it.
	I'm really swamped with work these days, but happily, this fix was 
	part of it: one of my users had a merge conflict in a submodule 
	and wanted to use the same procedure as for files.
	In the meantime, "git reset <submodule>" did the job, but this 
	rather limits the user's options:
		git checkout --theirs <submodule>
	is a much desired function here.  (Note: I did not test that this 
	works, actually, but then, I do not see why it should not, after
	this patch...)
 builtin-checkout.c            |    3 --
 t/t2013-checkout-submodule.sh |   42 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)
 create mode 100755 t/t2013-checkout-submodule.sh
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 33d1fec..2e4fe1a 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -53,9 +53,6 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,
 	int len;
 	struct cache_entry *ce;
 
-	if (S_ISGITLINK(mode))
-		return 0;
-
 	if (S_ISDIR(mode))
 		return READ_TREE_RECURSIVE;
 
diff --git a/t/t2013-checkout-submodule.sh b/t/t2013-checkout-submodule.sh
new file mode 100755
index 0000000..fda3f0a
--- /dev/null
+++ b/t/t2013-checkout-submodule.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='checkout can handle submodules'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	mkdir submodule &&
+	(cd submodule &&
+	 git init &&
+	 test_commit first) &&
+	git add submodule &&
+	test_tick &&
+	git commit -m superproject &&
+	(cd submodule &&
+	 test_commit second) &&
+	git add submodule &&
+	test_tick &&
+	git commit -m updated.superproject
+'
+
+test_expect_success '"reset <submodule>" updates the index' '
+	git update-index --refresh &&
+	git diff-files --quiet &&
+	git diff-index --quiet --cached HEAD &&
+	test_must_fail git reset HEAD^ submodule &&
+	test_must_fail git diff-files --quiet &&
+	git reset submodule &&
+	git diff-files --quiet
+'
+
+test_expect_success '"checkout <submodule>" updates the index only' '
+	git update-index --refresh &&
+	git diff-files --quiet &&
+	git diff-index --quiet --cached HEAD &&
+	git checkout HEAD^ submodule &&
+	test_must_fail git diff-files --quiet &&
+	git checkout HEAD submodule &&
+	git diff-files --quiet
+'
+
+test_done
-- 
1.6.2.1.493.g67cf3
Next: Junio C Hamano
Message 1 of 3 in “Fix 'git checkout <submodule>' to update the index”
  1. Fix 'git checkout <submodule>' to update the indexJohannes Schindelin, Apr 2, 2009
  2. Junio C HamanoApr 2, 2009
  3. Johannes SchindelinApr 2, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.