Skip to content

Instantly share code, notes, and snippets.

@AltGr
Created May 10, 2022 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AltGr/2891a61f721c8fd85b1da71e10c691b6 to your computer and use it in GitHub Desktop.
Save AltGr/2891a61f721c8fd85b1da71e10c691b6 to your computer and use it in GitHub Desktop.
Small script used to reformat a local branch commit by commit, using the ocamlformat configuration from origin/master
#!/bin/bash -uex
if [ $# -gt 0 ]; then
echo "Reformats the current branch patch by patch, according to the ocamlformat"
echo "config at origin/master. Use before rebase."
exit 1
fi
reformat() {
git checkout origin/master .ocamlformat
dune build @fmt 2>/dev/null || true
dune promote
}
BRANCH=$(git branch --show-current)
if [ -z "$BRANCH" ]; then echo 'Please be on a branch' >&2; exit 2; fi
TMP_BRANCH=tmp-reformat-$BRANCH
trap "git checkout --force $BRANCH; git branch -D $TMP_BRANCH" EXIT
BASE=$(git merge-base origin/master "$BRANCH")
COMMITS=$(git log --format=%H --reverse "$BASE".."$BRANCH")
git checkout -B "$TMP_BRANCH" "$BASE"
reformat
git diff --quiet || git commit -am "Formatting: sync settings with master"
for COMMIT in $COMMITS; do
git checkout "$COMMIT"
reformat
git diff --quiet || git commit -am "tmp-reformat"
C=$(git rev-parse HEAD)
git checkout "$TMP_BRANCH"
git diff --binary HEAD..$C | git apply --index -
git commit -C "$COMMIT"
done
git checkout "$BRANCH"
git reset "$TMP_BRANCH" --hard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment