Skip to content

Added from-submodule #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ git-subtree.xml
git-subtree.1
mainline
subproj
submodule*
43 changes: 36 additions & 7 deletions git-subtree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ if [ $# -eq 0 ]; then
set -- -h
fi
OPTS_SPEC="\
git subtree add --prefix=<prefix> <commit>
git subtree merge --prefix=<prefix> <commit>
git subtree pull --prefix=<prefix> <repository> <refspec...>
git subtree push --prefix=<prefix> <repository> <refspec...>
git subtree split --prefix=<prefix> <commit...>
git subtree add --prefix=<prefix> <commit>
git subtree merge --prefix=<prefix> <commit>
git subtree pull --prefix=<prefix> <repository> <refspec...>
git subtree push --prefix=<prefix> <repository> <refspec...>
git subtree split --prefix=<prefix> <commit...>
git subtree from-submodule --prefix=<prefix>
--
h,help show the help
q quiet
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" "$@"
38 changes: 38 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'