Created
June 27, 2017 16:09
-
-
Save dmnd/23d3ff1c1aacd6090a72267e0ed45267 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e # error early | |
REPO_LOCATION=/Users/desmondbrand/github/flexport | |
prettierify_branch() { | |
branch=$1 | |
echo '' | |
echo '' | |
# Don't run on already processed branches | |
if git rev-parse $branch-prettied >/dev/null 2>&1; then | |
echo "skipping already prettied branch $branch" | |
return | |
fi | |
if git rev-parse $branch-conflicts >/dev/null 2>&1; then | |
echo "skipping due to conflicts on branch $branch" | |
return | |
fi | |
# Don't run on post-prettier branches | |
if [ "$branch" = "run-prettier" ] || [ "$branch" = "prettier" ] || [ "$branch" = "prepare-prettier" ]; then | |
echo "refusing to run on branch ${branch}" | |
return | |
fi | |
# Don't run if there are no Javascript changes | |
njsfiles=$(git log --name-only --pretty="format:" origin/master..origin/${branch} | sort -u | grep . | grep -c 'jsx\?$') || true | |
if [ $njsfiles = "0" ]; then | |
echo "no js changes on branch ${branch}" | |
return | |
fi | |
echo "prettying $branch" | |
# get the fork point | |
branchroot=$(git merge-base origin/master origin/$branch) || { | |
echo "no fork point for branch $branch; skipping" | |
return | |
} | |
# checkout the branch | |
git branch --force $branch origin/$branch | |
git checkout $branch | |
git reset --hard | |
# make a backup label | |
backup_branch=$branch-pre-prettier | |
git branch -f "$backup_branch" "$branch" | |
# squash to one commit | |
author=`git log --format="%aN <%aE>" $backup_branch^!` | |
git reset --hard $branchroot | |
git merge --squash $backup_branch | |
git commit --no-verify --no-edit --author="$author" | |
# now rebase the branch onto pre-prettier master | |
git rebase origin/add-prettier || { | |
echo "conflicts during rebase for branch $branch; skipping" | |
git rebase --abort | |
git checkout --detach | |
git branch -D $branch $backup_branch | |
# git tag $branch-conflicts origin/$branch | |
return | |
} | |
squashed_commit=`git rev-parse HEAD` | |
# yarn install to get prettier | |
yarn | |
# run prettier on this branch | |
run_prettier | |
# now make new commit with respect to origin/run-prettier | |
git reset --soft origin/run-prettier | |
git commit --no-verify --reuse-message=$squashed_commit | |
# git tag $branch-prettied | |
echo "successfully prettied up $branch!" | |
} | |
run_prettier() { | |
# run prettier on everything | |
git ls-files --ignored --exclude-from=./script/prettier/included-files \ | |
| xargs yarn run prettier -- --write | |
# commit! | |
GIT_COMMITTER_NAME="prettier" GIT_COMMITTER_EMAIL="[email protected]" \ | |
git commit \ | |
--all \ | |
--no-verify \ | |
-m "✨ prettier!" \ | |
--author="prettier <[email protected]>" | |
} | |
cd $REPO_LOCATION | |
prettierify_branch $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment