pretty: Improve readability

This commit is contained in:
Colin Caine 2019-06-01 18:47:14 +01:00
parent 3921de48df
commit 7c1c906003

View file

@ -32,8 +32,6 @@ main() {
if [ -n "$stagedFiles" ]; then if [ -n "$stagedFiles" ]; then
# Could use git-update-index --cacheinfo to add a file without creating directories and stuff. # Could use git-update-index --cacheinfo to add a file without creating directories and stuff.
local tmpdir
tmpdir=$(mktemp -d "pretty.XXXXXXXXX")
IFS=$'\n' IFS=$'\n'
for file in $stagedFiles; do for file in $stagedFiles; do
if cmp -s <(staged "$file") "$file"; then if cmp -s <(staged "$file") "$file"; then
@ -48,24 +46,29 @@ main() {
else else
echo "WARN: Staged copy of '$file' does not match working copy: only prettifying staged copy." echo "WARN: Staged copy of '$file' does not match working copy: only prettifying staged copy."
( (
local tmpdir
tmpdir=$(mktemp -d "pretty.XXXXXXXXX")
cd "$tmpdir" cd "$tmpdir"
mkdir -p "$(dirname "$file")" mkdir -p "$(dirname "$file")"
lock ../.git/index.lock lock ../.git/index.lock
tmpfile=$(mktemp "pretty.XXXXXXXXX") tmpfile=$(mktemp "pretty.XXXXXXXXX")
case "$file" in case "$file" in
*.md | *.css) staged "$file" | prettier --stdin-filepath "$file" > "$tmpfile" && mv "$tmpfile" "$file";; *.md | *.css)
*) staged "$file" > "$tmpfile" staged "$file" | prettier --stdin-filepath "$file" > "$tmpfile" &&
tslint -c ../tslint.json --fix "$tmpfile" 2>/dev/null mv "$tmpfile" "$file";;
mv "$tmpfile" "$file";; *)
staged "$file" > "$tmpfile"
tslint -c ../tslint.json --fix "$tmpfile" 2>/dev/null &&
mv "$tmpfile" "$file";;
esac esac
chmod --reference="../$file" "$file" # match permissions chmod --reference="../$file" "$file" # match permissions
# Can't hold lock while git add occurs. Hopefully release and reacquire happen fast enough to prevent race. # Can't hold lock while git add occurs. Hopefully release and reacquire happen fast enough to prevent race.
unlock ../.git/index.lock unlock ../.git/index.lock
GIT_WORK_TREE=. git add "$file" GIT_WORK_TREE=. git add "$file"
rm -rf "$tmpdir"
) )
fi fi
done done
rm -rf "$tmpdir"
fi fi
} }