From: imyousuf@gmail.com Date: Wed, 09 Jan 2008 05:51:46 GMT Subject: [PATCH] - Added recurse command to git submodule Message-ID: <1199857906-26321-1-git-send-email-imyousuf@gmail.com> From: Imran M Yousuf - The purpose of the recurse command in the git submodule is to recurse a command in its submodule at depths specified. For example if one wants to do a diff on its project with submodules at once, one can simply do git-submodule recurse diff HEAD and would see the diff for all the modules it contains. If during recursion it is found that a module has not been initialized or updated than the command also initializes and updates them. It is to be noted that argument specification to the command intended (in above example diff) to recurse will be same as its original command (i.e. git diff). If the original command spec changes it will have no effect on the recurse command. Depth of recursion can be specified simply by mentioning the -d argument to recurse command. If not specified or specified to 0, it will be comprehended to recurse at all depth; if a positive number is specified than maximum recursion depth will never cross that depth. In order to see some information -v option may be used Signed-off-by: Imran M Yousuf --- git-submodule.sh | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 111 insertions(+), 1 deletions(-) diff --git a/git-submodule.sh b/git-submodule.sh index 8a29382..5cb931e 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -4,7 +4,7 @@ # # Copyright (c) 2007 Lars Hjemli -USAGE='[--quiet] [--cached] [add [-b branch]|status|init|update] [--] [...]' +USAGE='[[--quiet] [--cached] [add [-b branch]|status|init|update] [--] [...]|[recurse [-v] [-d ] ...]]' OPTIONS_SPEC= . git-sh-setup require_work_tree @@ -17,6 +17,9 @@ status= quiet= cached= command= +recurse_verbose=0 +recursion_depth=0 +current_recursion_depth=0 # # print stuff on stdout unless -q was specified @@ -294,6 +297,82 @@ modules_list() done } +# Initializes the submodule if already not initialized +initialize_sub_module() { + if [ ! -d "$1"/.git ]; then + if [ $recurse_verbose -eq 1 ]; then + echo Initializing and updating "$1" + fi + git-submodule init "$1"; git-submodule update "$1" + fi +} + +# This actually traverses the module; checks +# whether the module is initialized or not. +# if not initialized, then done so and then the +# intended command is evaluated. Then it +# recursively goes into it modules. +traverse_module() { + if [ $recurse_verbose -eq 1 ]; then + echo Current recursion depth: "$current_recursion_depth" + fi + initialize_sub_module "$1" + ( + submod_path="$1" + shift + cd "$submod_path" + if [ $recurse_verbose -eq 1 ]; then + echo Working in mod "$submod_path" @ `pwd` with "$@" \("$#"\) + fi + git "$@" + # Check whether submodules exists only if recursion to that depth + # was requested by user + if test "$recursion_depth" -eq 0 || test "$current_recursion_depth" -lt "$recursion_depth" + then + if [ -f .gitmodules ]; then + for mod_path in `sed -n -e 's/path = //p' .gitmodules`; do + export current_recursion_depth=$(echo "$current_recursion_depth+1" | bc) + traverse_module "$mod_path" "$@" + export current_recursion_depth=$(echo "$current_recursion_depth-1" | bc) + done + fi + fi + ) +} + +# Propagates or recurses over all the submodules at any +# depth with any git command, e.g. git-clone, git-status, +# git-commit etc., with the arguments supplied exactly as +# it would have been supplied to the command otherwise. +# This actually starts the recursive propagation +modules_recurse() { + project_home=`pwd` + echo Project Home: "$project_home" + if [ $recurse_verbose -eq 1 ]; then + if [ $recursion_depth = 0 ]; then + echo Recursion will go to all depths + else + echo Recursion depth specified to "$recursion_depth" + fi + fi + if [ -d "$project_home"/.git/ ]; then + if [ $recurse_verbose -eq 1 ]; then + echo Command to recurse: git "$@" + fi + git "$@" + if [ -f .gitmodules ]; then + for mod_path in `sed -n -e 's/path = //p' .gitmodules`; do + export current_recursion_depth=1 + traverse_module $mod_path "$@" + done + fi + else + echo "$project_home" not a git repo thus exiting + exit + fi + unset current_recursion_depth +} + # command specifies the whole function name since # one of theirs prefix is module not modules while test $# != 0 @@ -326,6 +405,37 @@ do --cached) command="modules_list" ;; + recurse) + command="modules_$1" + repeat=1 + while test $repeat = 1 + do + case "$2" in + -v) + recurse_verbose=1 + shift + ;; + -d) + if [ -z $3 ]; then + echo No \ specified + usage + elif [ `expr "$3" : '[1-9][0-9]*.*'` -eq `expr "$3" : '.*'` ]; then + recursion_depth="$3" + shift + shift + else + echo \ not an integer + usage + fi + ;; + *) + repeat=0 + ;; + esac + done + shift + break + ;; --) break ;; -- 1.5.3.7