Skip to content

Instantly share code, notes, and snippets.

@Tblue
Created August 30, 2015 18:45
Show Gist options
  • Save Tblue/bb21bb8af909e37a9d45 to your computer and use it in GitHub Desktop.
Save Tblue/bb21bb8af909e37a9d45 to your computer and use it in GitHub Desktop.
Git subtree splitter
#!/bin/sh
#
# Git subtree splitter.
#
# Copyright (c) 2014-2015, Tilman Blumenbach
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
if [ $# -lt 2 ]; then
echo "Usage:"
echo " $0 export-dir subtree..."
echo
echo "This exports all named subtrees as separate git repositories"
echo "to export-dir. Each repository directory under export-dir has"
echo "the same name as the corresponding subtree."
echo
echo "This command needs to be executed in a git repository."
exit 1
fi
export_dir=$1
shift
set -e
for subtree; do
dest_dir=${export_dir}/${subtree}
echo "Exporting '${subtree}' to '${dest_dir}'..."
if [ ! -d "${subtree}" ]; then
echo "Error: '${subtree}' does not exist. Skipping."
continue
fi
# Else we can proceed.
# 1. Create the destination directory.
mkdir -p "${dest_dir}"
# 2. Now, we can split the subtree:
subtree_branch="exported_subtree_${subtree}__$(uuidgen)"
git subtree split -q -b "${subtree_branch}" -P "${subtree}" >/dev/null
# 3. Clone only the subtree.
cd "${dest_dir}"
git clone -q "${OLDPWD}" -b "${subtree_branch}" .
# 4. Clean up the repository: Create a new master branch and delete the origin remote.
git remote remove origin
git checkout -q -b master
git branch -q -d "${subtree_branch}"
# 5. Finally, remove the temporary subtree branch in the source repository.
cd - >/dev/null
git branch -q -D "${subtree_branch}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment