Skip to content

Instantly share code, notes, and snippets.

@sileht
Last active September 15, 2020 13:02
Show Gist options
  • Save sileht/ab39fb2848dec5311ae1f9c15e88079c to your computer and use it in GitHub Desktop.
Save sileht/ab39fb2848dec5311ae1f9c15e88079c to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
set -o pipefail
format_file() {
file="${1}"
[ ${file#src/} == ${file} -a ${file#tests/} == ${file} ] && return # don't startswith src/ or tests
[ ${file#src/ext} != ${file} ] && return # startswith src/ext
if [ -f $file ]; then
echo "Update $file"
clang-format -style file -i ${1}
fi
}
REMOTE_PERSO="github-sileht"
REMOTE="origin"
BASE="${REMOTE}/master"
#BASE="clangformat"
before_clangformat_commit_id="c72327d2fcbc9fbc2b65f49df7e583a8263524ea"
current_branch_name=$(git rev-parse --abbrev-ref HEAD)
tidy_branch_name=${current_branch_name}-clangformatted
backup_branch_name=${current_branch_name}-before-clangformat-backup
echo "* Rebasing current branch to last commit before clang-format"
git fetch $REMOTE
git rebase $before_clangformat_commit_id || {
git rebase --abort
cat <<EOF
Manually rebase the branch to the commit before clangformat with:
git rebase $before_clangformat_commit_id
EOF
exit 1
}
cleanup() {
git checkout $current_branch_name
}
trap cleanup EXIT
echo
echo "* Retrieve clang-format config"
rm -f .clang-format .clang-format-ref
git checkout ${BASE}
cp .clang-format .clang-format-ref
git checkout ${current_branch_name}
# Ensure we don't have pre-commit installed yet and we havethe clang-format config
rm .git/hooks/pre-commit -f
cp .clang-format-ref .clang-format
LAST=$current_branch_name
LCA=$(git merge-base $BASE $LAST)
COMMITS=($(git rev-list --abbrev-commit $LCA..$LAST))
COUNT=${#COMMITS[@]}
echo "COMMITS ${COMMITS[@]}"
echo "COUNT $COUNT"
echo "LCA $LCA"
echo
git log --reverse --oneline $LCA..$LAST | nl
echo
echo
echo "* Preparing temporary branches"
git branch -D ${tidy_branch_name}-c0 || true
git branch ${tidy_branch_name}-c0 "$LCA"
for ((i = 0; i < COUNT; i++)); do
git branch -D ${tidy_branch_name}-c$((COUNT-i)) || true
git branch ${tidy_branch_name}-c$((COUNT-i)) $LAST~$i
done
echo
echo "* Format all temporary branches"
for ((i = 0; i <= COUNT; i++)); do
git checkout ${tidy_branch_name}-c$i
for file in `find . -type f | grep -iE '\.(cpp|cc|h|hpp)$'` ; do
file=${file:2}
format_file "${file}"
done
git commit -m "sync source" -a
done
echo
echo "* Generate diff of each branches"
for ((i = 0; i < COUNT; i++)); do
git diff ${tidy_branch_name}-c$i ${tidy_branch_name}-c$((i+1)) > ${tidy_branch_name}-c$((i+1)).diff
done
echo
echo "* Apply all diff on top of $BASE"
rm -f .clang-format
git branch -D ${tidy_branch_name} || true
git checkout -b ${tidy_branch_name} ${BASE}
for ((i = 1; i <= COUNT; i++)); do
echo "-------------------------------------------------------------------------------"
git log --oneline -n 1 ${COMMITS[COUNT-i]}
echo
patch -p1 < ${tidy_branch_name}-c$i.diff
# May add unwated files, but should work for us
git add cmake/ datasets/ demo/ docker/ examples/ m4/ main/ patches/ src/ templates/ tests/ tools/
git commit -a -C ${COMMITS[COUNT-i]}
done
echo
echo "* New branch is ready, renaming branches"
git branch -M $current_branch_name $backup_branch_name
git branch -M $tidy_branch_name $current_branch_name
git branch -u ${REMOTE}/master
echo "* Finished, you can push your branch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment