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

[RFC/PATCH] Implement poor-man's submodule support in pre-commit hook

From
APAndy Parkins <andyparkins@gmail.com>
Date
Dec 20, 2006, 09:01 UTC
Message-ID
<200612200901.30584.andyparkins@gmail.com>

Make a file called .gitmodules. In it, list the paths containing a submodule. Add that file to the index.

This addition to the pre-commit hook finds that file and pulls the HEAD hash out of each of the listed submodule repositories. That hash is then listed to the .gitmodules file along with the submodule name and .gitmodules is added back to the repository.

You've now got poor-man's submodule support. Any commits to the submodule will change the hash and hence the .gitmodules file will be different and therefore will show up as "modified" to git.

It's not got any nice UI for checking out (obviously) or merging; but it does at least record the state of a project with a bit of manual work to check out the right commit in the submodule.

Problems:
 - git-prune in the submodule could destroy supermodule-referenced
   commits
 - no checkout support
 - no merge support (other than what git provides for the .gitmodule
   file)
 - no check for dirty submodule before commit
Signed-off-by: Andy Parkins <andyparkins@gmail.com>
---
 templates/hooks--pre-commit |   31 ++++++++++++++++++++++++++++++-
 1 files changed, 30 insertions(+), 1 deletions(-)
diff --git a/templates/hooks--pre-commit b/templates/hooks--pre-commit
index 723a9ef..7a71d81 100644
--- a/templates/hooks--pre-commit
+++ b/templates/hooks--pre-commit
@@ -67,5 +67,34 @@ perl -e '
 	}
     }
     exit($found_bad);
-'
+' || exit 1
+
+# Enable poor-man's submodule support when .gitmodules is present
+# Simply create a .gitmodules file listing the paths in your repository
+# that contain other git repositories; each line will be replaced with the
+# path followed by the hash of the current HEAD of the submodule.
+# When the submodule changes hash this file will be different from the
+# version in the repository and a change will be noted - voila, submodules.
+# Of course there is no checkout support, but at least the current state
+# will be accurately recorded
+if [ -f .gitmodules ]; then
+	cat .gitmodules |
+	while read subdir hash
+	do
+		# XXX: check if the line is a comment
+
+		# XXX: really need a check here and quit if the submodule is
+		# dirty
+
+		echo "$subdir $(GIT_DIR=$subdir/.git git-rev-parse HEAD)"
+	done > .gitmodules
+
+	# This relies on the .gitmodules file having already been added to
+	# the repository - perhaps this should be automated?
+	git-update-index .gitmodules ||
+	(
+	echo "Submodule tracker file is not tracked by this repository." >&2
+	exit 1
+	)
+fi
 
-- 
1.4.4.2.g95ee-dirty
Next: Andy Parkins
Message 1 of 11 in “Implement poor-man's submodule support in pre-commit hook”
  1. Implement poor-man's submodule support in pre-commit hookAndy Parkins, Dec 20, 2006
  2. Andy ParkinsDec 20, 2006
  3. Brian GernhardtDec 20, 2006
  4. Jakub NarebskiDec 20, 2006
  5. Andreas EricssonDec 20, 2006
  6. [BUG] git --git-dir dies with bus errorBrian Gernhardt, Dec 20, 2006
  7. Jakub NarebskiDec 20, 2006
  8. Andy ParkinsDec 20, 2006
  9. Junio C HamanoDec 20, 2006
  10. Andy ParkinsDec 21, 2006
  11. Junio C HamanoDec 21, 2006

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.