X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=git-submodule.sh;h=e2082fd1492fe34e69c59f0a46cbaf7ddd2e2a8e;hb=9b0aa728705439ca4b4e7ec845f79f8487059320;hp=2f47e065fe8b7ca856f4527d6a507a28f1b2a06b;hpb=96aa7adda3b0254e4b9904f53bb38cd76bfea7bb;p=git.git diff --git a/git-submodule.sh b/git-submodule.sh index 2f47e065f..e2082fd14 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -4,9 +4,14 @@ # # Copyright (c) 2007 Lars Hjemli -USAGE="[--quiet] [--cached] \ -[add [-b branch] ]|[status|init|update [-i|--init]|summary [-n|--summary-limit ] []] \ -[--] [...]|[foreach ]|[sync [--] [...]]" +dashless=$(basename "$0" | sed -e 's/-/ /') +USAGE="[--quiet] add [-b branch] [--reference ] [--] [] + or: $dashless [--quiet] status [--cached] [--recursive] [--] [...] + or: $dashless [--quiet] init [--] [...] + or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference ] [--merge] [--recursive] [--] [...] + or: $dashless [--quiet] summary [--cached|--files] [--summary-limit ] [commit] [--] [...] + or: $dashless [--quiet] foreach [--recursive] + or: $dashless [--quiet] sync [--] [...]" OPTIONS_SPEC= . git-sh-setup . git-parse-remote @@ -14,19 +19,12 @@ require_work_tree command= branch= -quiet= +reference= cached= - -# -# print stuff on stdout unless -q was specified -# -say() -{ - if test -z "$quiet" - then - echo "$@" - fi -} +files= +nofetch= +update= +prefix= # Resolve relative url by appending to parent's url resolve_relative_url () @@ -59,7 +57,7 @@ resolve_relative_url () # module_list() { - git ls-files --stage -- "$@" | grep '^160000 ' + git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 ' } # @@ -90,6 +88,7 @@ module_clone() { path=$1 url=$2 + reference="$3" # If there already is a directory at the submodule path, # expect it to be empty (since that is the default checkout @@ -99,13 +98,18 @@ module_clone() if test -d "$path" then rmdir "$path" 2>/dev/null || - die "Directory '$path' exist, but is neither empty nor a git repository" + die "Directory '$path' exists, but is neither empty nor a git repository" fi test -e "$path" && die "A file already exist at path '$path'" - git-clone -n "$url" "$path" || + if test -n "$reference" + then + git-clone "$reference" -n "$url" "$path" + else + git-clone -n "$url" "$path" + fi || die "Clone of '$url' into submodule path '$path' failed" } @@ -128,7 +132,16 @@ cmd_add() shift ;; -q|--quiet) - quiet=1 + GIT_QUIET=1 + ;; + --reference) + case "$2" in '') usage ;; esac + reference="--reference=$2" + shift + ;; + --reference=*) + reference="$1" + shift ;; --) shift @@ -147,6 +160,11 @@ cmd_add() repo=$1 path=$2 + if test -z "$path"; then + path=$(echo "$repo" | + sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') + fi + if test -z "$repo" -o -z "$path"; then usage fi @@ -166,9 +184,18 @@ cmd_add() ;; esac - # strip trailing slashes from path - path=$(echo "$path" | sed -e 's|/*$||') - + # normalize path: + # multiple //; leading ./; /./; /../; trailing / + path=$(printf '%s/\n' "$path" | + sed -e ' + s|//*|/|g + s|^\(\./\)*|| + s|/\./|/|g + :start + s|\([^/]*\)/\.\./|| + tstart + s|/*$|| + ') git ls-files --error-unmatch "$path" > /dev/null 2>&1 && die "'$path' already exists in the index" @@ -193,9 +220,16 @@ cmd_add() git config submodule."$path".url "$url" else - module_clone "$path" "$realrepo" || exit - (unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b "$branch" "origin/$branch"}) || - die "Unable to checkout submodule '$path'" + module_clone "$path" "$realrepo" "$reference" || exit + ( + clear_local_git_env + cd "$path" && + # ash fails to wordsplit ${branch:+-b "$branch"...} + case "$branch" in + '') git checkout -f -q ;; + ?*) git checkout -f -q -b "$branch" "origin/$branch" ;; + esac + ) || die "Unable to checkout submodule '$path'" fi git add "$path" || @@ -215,13 +249,43 @@ cmd_add() # cmd_foreach() { + # parse $args after "submodule ... foreach". + while test $# -ne 0 + do + case "$1" in + -q|--quiet) + GIT_QUIET=1 + ;; + --recursive) + recursive=1 + ;; + -*) + usage + ;; + *) + break + ;; + esac + shift + done + module_list | while read mode sha1 stage path do if test -e "$path"/.git then - say "Entering '$path'" - (cd "$path" && eval "$@") || + say "Entering '$prefix$path'" + name=$(module_name "$path") + ( + prefix="$prefix$path/" + clear_local_git_env + cd "$path" && + eval "$@" && + if test -n "$recursive" + then + cmd_foreach "--recursive" "$@" + fi + ) || die "Stopping at '$path'; script returned non-zero status." fi done @@ -239,7 +303,7 @@ cmd_init() do case "$1" in -q|--quiet) - quiet=1 + GIT_QUIET=1 ;; --) shift @@ -277,6 +341,11 @@ cmd_init() git config submodule."$name".url "$url" || die "Failed to register url for submodule path '$path'" + upd="$(git config -f .gitmodules submodule."$name".update)" + test -z "$upd" || + git config submodule."$name".update "$upd" || + die "Failed to register update mode for submodule path '$path'" + say "Submodule '$name' ($url) registered for path '$path'" done } @@ -289,16 +358,42 @@ cmd_init() cmd_update() { # parse $args after "submodule ... update". + orig_args="$@" while test $# -ne 0 do case "$1" in -q|--quiet) shift - quiet=1 + GIT_QUIET=1 ;; -i|--init) + init=1 + shift + ;; + -N|--no-fetch) shift - cmd_init "$@" || return + nofetch=1 + ;; + -r|--rebase) + shift + update="rebase" + ;; + --reference) + case "$2" in '') usage ;; esac + reference="--reference=$2" + shift 2 + ;; + --reference=*) + reference="$1" + shift + ;; + -m|--merge) + shift + update="merge" + ;; + --recursive) + shift + recursive=1 ;; --) shift @@ -313,11 +408,17 @@ cmd_update() esac done + if test -n "$init" + then + cmd_init "--" "$@" || return + fi + module_list "$@" | while read mode sha1 stage path do name=$(module_name "$path") || exit url=$(git config submodule."$name".url) + update_module=$(git config submodule."$name".update) if test -z "$url" then # Only mention uninitialized submodules when its @@ -330,14 +431,19 @@ cmd_update() if ! test -d "$path"/.git -o -f "$path"/.git then - module_clone "$path" "$url" || exit + module_clone "$path" "$url" "$reference"|| exit subsha1= else - subsha1=$(unset GIT_DIR; cd "$path" && + subsha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD) || die "Unable to find current revision in submodule path '$path'" fi + if ! test -z "$update" + then + update_module=$update + fi + if test "$subsha1" != "$sha1" then force= @@ -345,18 +451,48 @@ cmd_update() then force="-f" fi - (unset GIT_DIR; cd "$path" && git-fetch && - git-checkout $force -q "$sha1") || - die "Unable to checkout '$sha1' in submodule path '$path'" - say "Submodule path '$path': checked out '$sha1'" + if test -z "$nofetch" + then + (clear_local_git_env; cd "$path" && + git-fetch) || + die "Unable to fetch in submodule path '$path'" + fi + + case "$update_module" in + rebase) + command="git rebase" + action="rebase" + msg="rebased onto" + ;; + merge) + command="git merge" + action="merge" + msg="merged in" + ;; + *) + command="git checkout $force -q" + action="checkout" + msg="checked out" + ;; + esac + + (clear_local_git_env; cd "$path" && $command "$sha1") || + die "Unable to $action '$sha1' in submodule path '$path'" + say "Submodule path '$path': $msg '$sha1'" + fi + + if test -n "$recursive" + then + (clear_local_git_env; cd "$path" && cmd_update $orig_args) || + die "Failed to recurse into submodule path '$path'" fi done } set_name_rev () { revname=$( ( - unset GIT_DIR + clear_local_git_env cd "$1" && { git describe "$2" 2>/dev/null || git describe --tags "$2" 2>/dev/null || @@ -377,6 +513,7 @@ set_name_rev () { cmd_summary() { summary_limit=-1 for_status= + diff_cmd=diff-index # parse $args after "submodule ... summary". while test $# -ne 0 @@ -385,6 +522,9 @@ cmd_summary() { --cached) cached="$1" ;; + --files) + files="$1" + ;; --for-status) for_status="$1" ;; @@ -421,10 +561,18 @@ cmd_summary() { head=HEAD fi + if [ -n "$files" ] + then + test -n "$cached" && + die "--cached cannot be used with --files" + diff_cmd=diff-files + head= + fi + cd_to_toplevel # Get modified modules cared by user - modules=$(git diff-index $cached --raw $head -- "$@" | - egrep '^:([0-7]* )?160000' | + modules=$(git $diff_cmd $cached --raw $head -- "$@" | + sane_egrep '^:([0-7]* )?160000' | while read mod_src mod_dst sha1_src sha1_dst status name do # Always show modules deleted or type-changed (blob<->module) @@ -437,8 +585,8 @@ cmd_summary() { test -z "$modules" && return - git diff-index $cached --raw $head -- $modules | - egrep '^:([0-7]* )?160000' | + git $diff_cmd $cached --raw $head -- $modules | + sane_egrep '^:([0-7]* )?160000' | cut -c2- | while read mod_src mod_dst sha1_src sha1_dst status name do @@ -540,7 +688,11 @@ cmd_summary() { echo done | if test -n "$for_status"; then - echo "# Modified submodules:" + if [ -n "$files" ]; then + echo "# Submodules changed but not updated:" + else + echo "# Submodule changes to be committed:" + fi echo "#" sed -e 's|^|# |' -e 's|^# $|#|' else @@ -560,15 +712,19 @@ cmd_summary() { cmd_status() { # parse $args after "submodule ... status". + orig_args="$@" while test $# -ne 0 do case "$1" in -q|--quiet) - quiet=1 + GIT_QUIET=1 ;; --cached) cached=1 ;; + --recursive) + recursive=1 + ;; --) shift break @@ -588,22 +744,34 @@ cmd_status() do name=$(module_name "$path") || exit url=$(git config submodule."$name".url) + displaypath="$prefix$path" if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git then - say "-$sha1 $path" + say "-$sha1 $displaypath" continue; fi set_name_rev "$path" "$sha1" if git diff-files --quiet -- "$path" then - say " $sha1 $path$revname" + say " $sha1 $displaypath$revname" else if test -z "$cached" then - sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD) + sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD) set_name_rev "$path" "$sha1" fi - say "+$sha1 $path$revname" + say "+$sha1 $displaypath$revname" + fi + + if test -n "$recursive" + then + ( + prefix="$displaypath/" + clear_local_git_env + cd "$path" && + cmd_status $orig_args + ) || + die "Failed to recurse into submodule path '$path'" fi done } @@ -618,7 +786,7 @@ cmd_sync() do case "$1" in -q|--quiet) - quiet=1 + GIT_QUIET=1 shift ;; --) @@ -650,7 +818,7 @@ cmd_sync() if test -e "$path"/.git then ( - unset GIT_DIR + clear_local_git_env cd "$path" remote=$(get_default_remote) say "Synchronizing submodule url for '$name'" @@ -673,7 +841,7 @@ do command=$1 ;; -q|--quiet) - quiet=1 + GIT_QUIET=1 ;; -b|--branch) case "$2" in