From 3d6208f5b5b2877f277fc80419020427ab03b978 Mon Sep 17 00:00:00 2001 From: Tony Duckles Date: Wed, 23 Jan 2013 19:17:09 -0600 Subject: [PATCH] bin/git-subup: Init child submodules, optionally pull Refactor the 'git-subup' (submodule update) script to only conditionally pull changes from each submodule's origin remote. "git subup" will just ensure all submodules are checked-out. "git subup pull" will additionally do a pull on each submodule to look for updates. * Use '--recursive' flag for update/sync/foreach subcommands to update any child submodules of first-level submodules. * Conditionalize 'git pull' handling only if $1 == "pull". * Don't assume that "remotes/origin/HEAD" is a valid ref; not all repo's will have an "origin/HEAD" ref. Instead, just force-update local "master" branch based on the current checked-out rev, so that when 'git submodule update --checkout' creates a detached HEAD we'll update our local 'master' branch to jive with HEAD so that any later 'git pull' will show all new revisions compared to the previous HEAD. --- bin/git-subup | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/bin/git-subup b/bin/git-subup index aec9632..52d4f09 100755 --- a/bin/git-subup +++ b/bin/git-subup @@ -1,8 +1,22 @@ -#!/bin/sh -# Checkout and update source for all registered submodules (.gitmodules) +#!/bin/bash +# Checkout and update source for all registered submodules (.gitmodules). +# Optionally 'git pull' on each submodule to check for updates. +# Change to root directory cd $(git rev-parse --show-toplevel) -git submodule update --init --checkout --quiet -git submodule sync --quiet -git submodule foreach --quiet git checkout --quiet -B master origin/HEAD -git submodule foreach git pull --prune origin + +# Ensure all submodules (and child submodules) are checked-out +git submodule update --init --checkout --recursive + +# Update submodules' remotes based on .gitmodules +git submodule sync --quiet --recursive + +# Forcibly update each submodule's "master" branch to be the current revision +# in case HEAD is detached (e.g. due to "git submodule update --checkout") +git submodule foreach --quiet --recursive 'git checkout --quiet -B master' + +if [ "$1" = "pull" ]; then + # Fetch & pull any new updates from submodule's origin (but *NOT* any child + # submodule updates -- those should come from the submodule proper) + git submodule foreach --quiet 'echo "\n\033[32m${path}:\033[0m" && git-up origin' +fi -- 2.45.2