git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Heiko Voigt <hvoigt@hvoigt.net>
To: Jens Lehmann <Jens.Lehmann@web.de>
Cc: Peter Krefting <peter@softwolves.pp.se>,
	Git Mailing List <git@vger.kernel.org>
Subject: [RFC PATCH] implement sample post-checkout hook to checkout new/unchanged submodules
Date: Wed, 14 Oct 2009 22:12:57 +0200	[thread overview]
Message-ID: <20091014201257.GA5788@book.hvoigt.net> (raw)
In-Reply-To: <4AD5F0B1.4000507@web.de>

Currently gits default behaviour is not to recursively update submodules
when they are unchanged in the working copy. This hook implements this
by comparing whether the current HEAD in the submodule is the same as
recorded in the commit we are coming from. It then initializes or
updates the submodule as necessary.

Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net>
---

On Wed, Oct 14, 2009 at 05:39:29PM +0200, Jens Lehmann wrote:
> Peter Krefting schrieb:
> > Jens Lehmann:
> > 
> >> just calling "git submodule update" every time you want the submodule
> >> to be updated according to the state committed in the superproject
> >> will do the trick (but keep in mind that all referenced commits have
> >> to be accessible in the local clone of your submodule, so you might
> >> have to do a fetch there once in a while).
> 
> BTW: unless you use the -N or --no-fetch option, git submodule update
> will do the fetch for you.
> 
> 
> > Is it possible to automate this from a hook or something else?
> 
> Yep, you can use the post-checkout hook for that, just put a "git
> submodule update" in it.

Incidentially I have just been working on such a hook. Here is a patch
that implements it as a sample hook.

I tested most cases, but I have not worked with it productively so it
might have strange results in some cases. One I already found is that
post-checkout is not called after a merge so you need to add a
submodule update there as well.

cheers Heiko


 templates/hooks--post-checkout.sample |   50 +++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)
 create mode 100644 templates/hooks--post-checkout.sample

diff --git a/templates/hooks--post-checkout.sample b/templates/hooks--post-checkout.sample
new file mode 100644
index 0000000..9ffffa0
--- /dev/null
+++ b/templates/hooks--post-checkout.sample
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+# if this is a file checkout we do nothing
+flag=$3
+if [ ! $flag ]
+then
+	exit 0
+fi
+
+# if this is the initial checkout also intialize submodules
+if [ $1 == "0000000000000000000000000000000000000000" ]; then
+	git submodule init
+	git submodule update
+	# after initial checkout we are done now
+	exit 0
+fi
+
+# update all submodules that where not modified beforehand
+# or did not exist
+prev_sha1=$1
+new_sha1=$2
+
+git ls-tree $new_sha1 | grep ^160000 |cut -f2 |
+while read submodule
+do
+	prev_submodule_sha1=$(git ls-tree $prev_sha1 |
+		grep "	$submodule$" | cut -f1 | cut -d' ' -f3)
+	curr_submodule_sha1=$(cd "$submodule"; git rev-parse HEAD \
+		2>/dev/null)
+
+	if [ -z "$prev_submodule_sha1" ]
+	then
+		git submodule init "$submodule"
+		git submodule update "$submodule"
+		continue
+	fi
+
+	if [ "$prev_submodule_sha1" = "$curr_submodule_sha1" ]
+	then
+		if [ -z "$(git config submodule."$submodule".url)" ]
+		then
+			git submodule init "$submodule"
+		fi
+
+		if [ "$(git diff $prev_sha1 $new_sha1 -- "$submodule")" ]
+		then
+			git submodule update "$submodule"
+		fi
+	fi
+done
-- 
1.6.5.rc1.12.gc72fe

  reply	other threads:[~2009-10-14 20:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-13 10:29 Changing branches in supermodule does not affect submodule? Peter Krefting
2009-10-13 13:11 ` Jens Lehmann
2009-10-14  6:31   ` Peter Krefting
2009-10-14 15:39     ` Jens Lehmann
2009-10-14 20:12       ` Heiko Voigt [this message]
2009-10-14 20:02     ` 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

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

  git send-email \
    --in-reply-to=20091014201257.GA5788@book.hvoigt.net \
    --to=hvoigt@hvoigt.net \
    --cc=Jens.Lehmann@web.de \
    --cc=git@vger.kernel.org \
    --cc=peter@softwolves.pp.se \
    /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.
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 NNTP newsgroup(s).