X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=git-submodule.sh;h=19a3a840fdc7eed4994741e13306b71a3ffd2ada;hb=974e6e42f7a66b69bd684af4f637ab064acfdf92;hp=2f47e065fe8b7ca856f4527d6a507a28f1b2a06b;hpb=ff6e93fe605b748055267fd325c760d0f32dcf92;p=git.git diff --git a/git-submodule.sh b/git-submodule.sh index 2f47e065f..19a3a840f 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -5,7 +5,7 @@ # Copyright (c) 2007 Lars Hjemli USAGE="[--quiet] [--cached] \ -[add [-b branch] ]|[status|init|update [-i|--init]|summary [-n|--summary-limit ] []] \ +[add [-b branch] ]|[status|init|update [-i|--init] [-N|--no-fetch]|summary [-n|--summary-limit ] []] \ [--] [...]|[foreach ]|[sync [--] [...]]" OPTIONS_SPEC= . git-sh-setup @@ -15,7 +15,10 @@ require_work_tree command= branch= quiet= +reference= cached= +nofetch= +update= # # print stuff on stdout unless -q was specified @@ -59,7 +62,7 @@ resolve_relative_url () # module_list() { - git ls-files --stage -- "$@" | grep '^160000 ' + git ls-files --error-unmatch --stage -- "$@" | grep '^160000 ' } # @@ -90,6 +93,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 @@ -105,7 +109,12 @@ module_clone() 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" } @@ -130,6 +139,15 @@ cmd_add() -q|--quiet) quiet=1 ;; + --reference) + case "$2" in '') usage ;; esac + reference="--reference=$2" + shift + ;; + --reference=*) + reference="$1" + shift + ;; --) shift break @@ -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 + ( + unset GIT_DIR + 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" || @@ -277,6 +311,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 } @@ -297,8 +336,25 @@ cmd_update() quiet=1 ;; -i|--init) + init=1 + shift + ;; + -N|--no-fetch) + shift + nofetch=1 + ;; + -r|--rebase) + shift + update="rebase" + ;; + --reference) + case "$2" in '') usage ;; esac + reference="--reference=$2" + shift 2 + ;; + --reference=*) + reference="$1" shift - cmd_init "$@" || return ;; --) shift @@ -313,11 +369,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,7 +392,7 @@ 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" && @@ -338,6 +400,11 @@ cmd_update() 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,11 +412,30 @@ 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 + (unset GIT_DIR; 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" + ;; + *) + command="git checkout $force -q" + action="checkout" + msg="checked out" + ;; + esac + + (unset GIT_DIR; cd "$path" && $command "$sha1") || + die "Unable to $action '$sha1' in submodule path '$path'" + say "Submodule path '$path': $msg '$sha1'" fi done }