Skip to content

Instantly share code, notes, and snippets.

@austo
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save austo/7350bb4164dd80ec2f08 to your computer and use it in GitHub Desktop.

Select an option

Save austo/7350bb4164dd80ec2f08 to your computer and use it in GitHub Desktop.
flatten dk submodules
#!/bin/bash
set -o nounset
set -o errexit
# Needed for sed (Mac OS X sed does not understand \t)
TAB="$(printf '\t')"
TEMP='temp'
SUB='sub'
GIT='/usr/local/bin/git' # set to git executable
# DK repo and path
DKP='dk'
DKU='git@github.com:surveysampling/dk.git'
# Submodule repos and paths
DYNAMIXP='Dynamix'
DYNAMIXU='git@github.com:surveysampling/dynamix.git'
DKDYNAMIXP='DubKnowledgeWeb/modules/dkdynamix'
DKDYNAMIXU='git@github.com:surveysampling/dk-dynamix.git'
DKMOBILEP='DubKnowledgeWeb/modules/dkmobile'
DKMOBILEU='git@github.com:surveysampling/dkmobile.git'
# Rewrite commit history to move files to submodule path (could take a while...)
function filter_branch { # path
UPDATED_PATH=$(echo -n "$1" | sed -e 's/[\/&]/\\&/g')
$GIT ls-files -s | sed "s/${TAB}/${TAB}$UPDATED_PATH\//"
echo "Proceeding to edit commit history for the preceding files..."
CMD="$GIT ls-files -s | sed \"s/${TAB}/${TAB}$UPDATED_PATH\//\" \
| GIT_INDEX_FILE=\${GIT_INDEX_FILE}.new $GIT update-index \
--index-info && if test -f \"\${GIT_INDEX_FILE}.new\"; \
then mv \${GIT_INDEX_FILE}.new \${GIT_INDEX_FILE}; fi"
$GIT filter-branch --index-filter "$CMD" HEAD
}
function flatten { # path, url
rm -fR $1
$GIT commit -am "remove $1 submodule"
LOCAL_REPO=$(echo $2 | sed -e 's/^.*\/\(.*\)\.git/..\/\1/')
cd $LOCAL_REPO
filter_branch $1
cd ../$DKP
$GIT remote add $SUB $LOCAL_REPO
$GIT fetch $SUB
$GIT merge -s ours --no-commit $SUB/master
$GIT clone $2 $1 # clone fresh submodule to get latest files
rm -fR $1/.git # remove git repo (now have only files and updated history)
$GIT add $1
$GIT commit -m "flatten $1 submodule"
$GIT remote rm $SUB
printf "\n:) successfully flattened %s :)\n\n" "$1"
}
# ---- "Main" ---- #
# Fresh clone of submodule repos
$GIT clone $DYNAMIXU
$GIT clone $DKDYNAMIXU
$GIT clone $DKMOBILEU
# Begin main module processing
$GIT clone $DKU # do not recurse-submodules to avoid having to edit .git/config
cd $DKP
rm .gitmodules
$GIT commit -am 'remove .gitmodules' # TODO: read .gitmodules for path and url
flatten "$DYNAMIXP" "$DYNAMIXU"
flatten "$DKDYNAMIXP" "$DKDYNAMIXU"
flatten "$DKMOBILEP" "$DKMOBILEU"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment