2018-08-24 13:38:05 -06:00
|
|
|
#!/usr/bin/env bash
|
2018-05-18 15:35:32 +01:00
|
|
|
|
2018-08-29 10:31:15 +01:00
|
|
|
# Run prettier on each staged file that needs it without touching the working tree
|
|
|
|
|
2018-05-18 15:35:32 +01:00
|
|
|
source ./scripts/common
|
|
|
|
|
2018-08-29 10:31:15 +01:00
|
|
|
set -Eeuo pipefail
|
|
|
|
|
|
|
|
echoe() {
|
|
|
|
echo "$@" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
lock() {
|
|
|
|
local lockfile="$1"
|
|
|
|
if [ -e "$lockfile" ]; then
|
|
|
|
echoe "Lockfile '$lockfile' already exists. Check no other operation is occuring or delete lockfile"
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
touch "$lockfile"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock() {
|
|
|
|
rm "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
trap "unlock $(git rev-parse --show-toplevel)/.git/index.lock || true" ERR
|
|
|
|
|
|
|
|
main() {
|
|
|
|
local stagedFiles=$(cachedFiles)
|
|
|
|
|
|
|
|
if [ -n "$stagedFiles" ]; then
|
|
|
|
# Could use git-update-index --cacheinfo to add a file without creating directories and stuff.
|
|
|
|
local tmpdir=$(mktemp -p . -d "pretty.XXXXXXXXX")
|
|
|
|
IFS=$'\n'
|
|
|
|
for file in $stagedFiles; do
|
|
|
|
(
|
|
|
|
cd "$tmpdir"
|
|
|
|
mkdir -p $(dirname $file)
|
|
|
|
lock ../.git/index.lock
|
|
|
|
staged "$file" | prettier --stdin-filepath "$file" > "$file"
|
|
|
|
chmod --reference="../$file" "$file" # match permissions
|
|
|
|
# Can't hold lock while git add occurs. Hopefully release and reacquire happen fast enough to prevent race.
|
|
|
|
unlock ../.git/index.lock
|
|
|
|
GIT_WORK_TREE=. git add "$file"
|
|
|
|
)
|
|
|
|
done
|
|
|
|
rm -rf "$tmpdir"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
main
|