Skip to content

Instantly share code, notes, and snippets.

@simonwagner
Created May 5, 2017 10:23
Show Gist options
  • Save simonwagner/e24b0d4cf72df36bf352d716c223d0c1 to your computer and use it in GitHub Desktop.
Save simonwagner/e24b0d4cf72df36bf352d716c223d0c1 to your computer and use it in GitHub Desktop.
Replay commits from submodule in main git repository
#!/bin/sh
#Replay commits from submodule in main git repository
#For each commit in the submodule in range, create a commit in the
#main repository that adds the commit of the submodule with the same
#commit message as the submodule
SUBMODULE=$1
RANGE=$2
function is_submodule() {
true #TODO: Can we implement this?
}
if [ -z $SUBMODULE ] || [ -z $RANGE ]; then
echo "usage: $0 SUBMODULE RANGE" >&2
exit 1
fi
if [ ! -d "$SUBMODULE" ]; then
echo "$SUBMODULE: No such directory" >&2
exit 2
fi
if [ $( cd $SUBMODULE && is_submodule )]; then
echo "$SUBMODULE: Not a submodule" >&2
exit 2
fi
if [ -n "$( git status --untracked-files=no --ignore-submodules --porcelain )" ]; then
echo "Working directory or index is not clean, aborting!" >&2
exit 3
fi
if [ -n "$( cd $SUBMODULE && git status --untracked-files=no --ignore-submodules --porcelain )" ]; then
echo "Working directory or index of submodule is not clean, aborting!" >&2
exit 3
fi
SUBMODULE_HEAD=$( cd "${SUBMODULE}" && git rev-parse --abbrev-ref --verify HEAD)
SUBMODULE_COMMITS=$( cd "${SUBMODULE}" && git rev-list "${RANGE}")
#reverse list of commits
REVERSED_SUBMODULE_COMMITS=$( echo "$SUBMODULE_COMMITS" | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' - )
echo "Replaying commits of submodule ${SUBMODULE} ${RANGE}..."
echo
for COMMIT in $REVERSED_SUBMODULE_COMMITS ; do
SUBMODULE_COMMIT_MSG=$( cd "$SUBMODULE" && git show -s --format=%B $COMMIT )
SUBMODULE_COMMIT_SUBJECT=$( cd "$SUBMODULE" && git show -s --format=%s $COMMIT )
echo "[${COMMIT}] ${SUBMODULE_COMMIT_SUBJECT}"
$( cd "$SUBMODULE" && git checkout -q $COMMIT ) && \
git add "${SUBMODULE}" && ( echo "$SUBMODULE_COMMIT_MSG" | git commit -q --file - )
done
echo
echo "Resetting HEAD of submodule to previous value '$SUBMODULE_HEAD'"
( cd "$SUBMODULE" && git checkout -q "$SUBMODULE_HEAD" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment