diff --git a/.gitignore b/.gitignore index 7e77c9d..b770d78 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ git-subtree.xml git-subtree.1 mainline subproj +submodule* diff --git a/git-subtree.sh b/git-subtree.sh index ce94d36..d5f4b7e 100755 --- a/git-subtree.sh +++ b/git-subtree.sh @@ -8,11 +8,12 @@ if [ $# -eq 0 ]; then set -- -h fi OPTS_SPEC="\ -git subtree add --prefix= -git subtree merge --prefix= -git subtree pull --prefix= -git subtree push --prefix= -git subtree split --prefix= +git subtree add --prefix= +git subtree merge --prefix= +git subtree pull --prefix= +git subtree push --prefix= +git subtree split --prefix= +git subtree from-submodule --prefix= -- h,help show the help q quiet @@ -100,7 +101,7 @@ done command="$1" shift case "$command" in - add|merge|pull) default= ;; + add|merge|pull|from-submodule) default= ;; split|push) default="--default HEAD" ;; *) die "Unknown command '$command'" ;; esac @@ -554,7 +555,8 @@ cmd_split() eval "$grl" | while read rev parents; do revcount=$(($revcount + 1)) - say -n "$revcount/$revmax ($createcount) " + say -n "$revcount/$revmax ($createcount) +" debug "Processing commit: $rev" exists=$(cache_get $rev) if [ -n "$exists" ]; then @@ -680,4 +682,31 @@ cmd_push() fi } +cmd_from-submodule() +{ + ensure_clean + + local submodule_sha=$(git submodule status $prefix | cut -d ' ' -f 2) + + # Remove references to submodule. + git config --remove-section submodule.$prefix + git config --file .gitmodules --remove-section submodule.$prefix + git add .gitmodules + + # Move submodule aside. + local tmp_repo="$(mktemp -d /tmp/git-subtree.XXXXX)" + rm -r $tmp_repo + mv $prefix $tmp_repo + git rm $prefix + + # Commit changes. + git commit -m "Remove '$prefix/' submodule" + + # subtree add from submodule repo. + cmd_add_repository $tmp_repo HEAD + + # Remove submodule repo. + rm -rf $tmp_repo +} + "cmd_$command" "$@" diff --git a/test.sh b/test.sh index 45237c3..d696a6e 100755 --- a/test.sh +++ b/test.sh @@ -338,5 +338,43 @@ git log --pretty=format:'commit: %H' | joincommits | check_equal "$x" 1 ) || exit 1 +# Return to mainline +cd ../.. + + +# from-submodule + +make_submodule() +{ + cd .. + local prefix=$1 + + local submodule_path=$(mktemp -d submodule.XXX) + cd $submodule_path + local submodule_abs_path=$(pwd) + + git init > /dev/null + create "sub-file" + git commit -m "sub-commit" > /dev/null + local submodule_sha=$(git rev-parse HEAD) + cd ../mainline + + git submodule add $submodule_abs_path $prefix > /dev/null + git submodule update --init > /dev/null + git commit -m "Added $prefix." > /dev/null + + echo $submodule_sha +} + +subA_sha=$(make_submodule submodules/subA) + +git subtree from-submodule --prefix submodules/subA + +check_equal "$(last_commit_message)" "Add 'submodules/subA/' from commit '${subA_sha}'" +# Submodule should be gone. +check_equal "$(git submodule status)" "" + +rm -rf ../submodule.* + echo echo 'ok'