Last active
November 25, 2024 22:37
-
-
Save mikebridge/3983cbf9c5c441048bfb9bc3d5e1ceac to your computer and use it in GitHub Desktop.
From a branch off master, create a prettified commit with only the prettified files, so that the branch-off-master can be rebased upon it, thus making the prettify-step the first commit.
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
#!/usr/bin/env bash | |
# 1) determine which files have been modified from master in the current branch | |
# 2) create a new, temporary branch from master | |
# 3) run prettier on the modified files in master | |
# 4) commit these to the temporary branch | |
# 5) print some instructions on how to rebase the current branch onto the temporary branch. | |
set -eo pipefail | |
MAIN_BRANCH=master | |
CURRENT_BRANCH=$(git branch --show-current) | |
ROOT_DIR=$(git rev-parse --show-toplevel) | |
COMMIT_MESSAGE="run prettier on modified files" | |
ensure_clean_working_directory() { | |
if [ -n "$(git status --porcelain)" ] | |
then | |
echo -e "*** Error: Git working directory is not clean.\n" | |
git status | |
exit 1 | |
fi | |
} | |
ensure_working_directory_has_changes() { | |
if [ -z "$(git status --porcelain)" ] | |
then | |
echo -e "*** The changed files have already been prettified---exiting.\n" | |
git status | |
exit 0 | |
fi | |
} | |
cleanup() { | |
git checkout "${CURRENT_BRANCH}" | |
popd > /dev/null 2>&1 || exit 0 | |
} | |
create_branch_name () { | |
counter=1 | |
ORIG_REF="${CURRENT_BRANCH}_prettier_tmp_${counter}" | |
NEW_REF=$ORIG_REF | |
while git show-ref --quiet "refs/heads/$NEW_REF" | |
do | |
((counter++)) | |
NEW_REF="${CURRENT_BRANCH}_prettier_tmp_${counter}" | |
done | |
echo "$NEW_REF" | |
exit 0 | |
} | |
checkout_prettier_branch() { | |
git checkout -b "${TMP_BRANCH}" "${MAIN_BRANCH}" | |
} | |
find_modified_files() { | |
git diff --name-only --relative "${MAIN_BRANCH}..${CURRENT_BRANCH}" --diff-filter=M | grep -e '\.ts$' | |
} | |
prettify_files () { | |
modified_files=$(echo "$1" | xargs) | |
# shellcheck disable=SC2086 | |
npx prettier --write $modified_files | |
} | |
ensure_clean_working_directory | |
trap cleanup EXIT | |
pushd "${ROOT_DIR}/Chimera" > /dev/null 2>&1 | |
MODIFIED_FILES=$(find_modified_files) | |
TMP_BRANCH=$(create_branch_name) | |
echo "...creating temp branch ${TMP_BRANCH}" | |
checkout_prettier_branch "${TMP_BRANCH}" | |
echo "...prettifying files: " | |
echo "${MODIFIED_FILES}" | |
prettify_files "${MODIFIED_FILES}" | |
echo "committing changes on branch $(git branch --show-current)" | |
cd "${ROOT_DIR}" | |
git add -A . | |
git commit -m "${COMMIT_MESSAGE}" | |
echo "*********************************************************" | |
echo "*** To complete this process, run the following command:" | |
echo "***" | |
echo "*** git rebase ${TMP_BRANCH}" | |
echo "***" | |
echo "*********************************************************" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment