From afe8845c14d7e02b36bcc64e3cdd91b38fa86456 Mon Sep 17 00:00:00 2001 From: artemis Date: Mon, 25 May 2026 03:52:25 +0530 Subject: [PATCH] Initial git hook pre-commit Initialised a git hook for pre commit in client side Need to establish some sort of server connection to make it available on the server side and need to prepare so that it also checks if the server is indeed capable with FFMPEG(libopus) and IMAGEMAGICK (6 preferably) to intialise the image and provide a reasonable way to implementation --- hooks/install.sh | 177 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100755 hooks/install.sh diff --git a/hooks/install.sh b/hooks/install.sh new file mode 100755 index 0000000..b004c58 --- /dev/null +++ b/hooks/install.sh @@ -0,0 +1,177 @@ +#!/bin/sh +GITDIR=$(git rev-parse --show-toplevel) + +# TODO : MIGHT need a failsafe to skip extremely large files, maybe less than 100mb is fine +# TODO : MIGHT also implement a filename "uncompressed-*" to end up without any compression +# TODO : MIGHT need to implement replacement of files with spaces to dashes +# TODO : MIGHT need to implement same file realiser, two filenames with the same will lead to some errors +# TODO : MIGHT be able to export with functions and eval parameters to reduce this weird repitition (PRIORITY) + +# Installing webp images with assets/images +WEBP_IMAGES=$(git diff --name-only --cached | grep -E "\.(webp|WEBP)$") +NON_WEBP_IMAGES=$(git diff --name-only --cached | grep -E "\.(jpg|png|JPG|PNG)$") +IMG_LIMIT=150 + +for filename in $WEBP_IMAGES; do + QUALITY=80 + SIZE=100 + UNSUFFIXED_FILENAME=${filename%.*} + + git restore --staged "$GITDIR/$filename" + + # One conversion atleast + convert $GITDIR/$filename \ + -resize $SIZE% \ + -quality $QUALITY \ + -format "webp" \ + $GITDIR/$UNSUFFIXED_FILENAME.webp; + + while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.webp" ] && \ + [ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.webp" | \ + awk -F' ' '{print $1}') -ge $IMG_LIMIT ] && \ + [ $(($SIZE > 0)) ]; do + + SIZE=$(($SIZE - 20)) + + convert $GITDIR/$UNSUFFIXED_FILENAME.webp \ + -resize $SIZE% \ + -quality $QUALITY \ + -format "webp" \ + $GITDIR/$UNSUFFIXED_FILENAME.webp; + done; +done; + + + +for filename in $NON_WEBP_IMAGES; do + QUALITY=80 + SIZE=100 + UNSUFFIXED_FILENAME=${filename%.*} + + git restore --staged "$GITDIR/$filename" + + if [ -e "$GITDIR/$UNSUFFIXED_FILENAME.webp" ] + then + rm "$GITDIR/$filename" + echo "SKIPPING $filename : the filename already exists" + continue + fi + + # One conversion atleast + convert $GITDIR/$filename \ + -resize $SIZE% \ + -quality $QUALITY \ + -format "webp" \ + $GITDIR/$UNSUFFIXED_FILENAME.webp; + + rm "$GITDIR/$filename" + + while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.webp" ] && \ + [ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.webp" | \ + awk -F' ' '{print $1}') -ge $IMG_LIMIT ] && \ + [ $(($SIZE > 20)) ]; do + + SIZE=$(($SIZE - 20)) + + convert $GITDIR/$UNSUFFIXED_FILENAME.webp \ + -resize $SIZE% \ + -quality $QUALITY \ + -format "webp" \ + $GITDIR/$UNSUFFIXED_FILENAME.webp; + done; +done; + +# Installing opus audio with assets/audio +NON_OPUS_AUDIOS=$(git diff --name-only --cached | grep -E "\.(mp3|wav|flac|m4a|ogg|wma)") +OPUS_AUDIOS=$(git diff --name-only --cached | grep -E "\.opus") +AUDIO_LIMIT=64 + + +for filename in $OPUS_AUDIOS ; do + COMPRESSION_LEVEL=10 + UNSUFFIXED_FILENAME=${filename%.*} + BITRATE=128 + + git restore --staged "$GITDIR/$filename" + + # One conversion atleast + # This will do opus to opus with decoder + # Degrading once quality but we tank it + ffmpeg -hide_banner -y -loglevel warning \ + -i "$GITDIR/$filename" \ + -codec:a libopus \ + -compression_level $COMPRESSION_LEVEL \ + -vbr on \ + "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus"; + + mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus" + + while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.opus" ] && \ + [ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.opus" | \ + awk -F' ' '{print $1}') -ge $AUDIO_LIMIT ] && \ + [ $COMPRESSION_LEVEL -ge 3 ]; do + + COMPRESSION_LEVEL=$(($COMPRESSION_LEVEL - 3)) + BITRATE=$(($BITRATE / 2)) + + ffmpeg -hide_banner -y -loglevel warning \ + -y -i "$GITDIR/$UNSUFFIXED_FILENAME.opus" \ + -codec:a libopus \ + -compression_level $COMPRESSION_LEVEL \ + -vbr constrained \ + -b:a "${BITRATE}k" \ + "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus"; + + mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus"; + done; +done; + + +for filename in $NON_OPUS_AUDIOS ; do + COMPRESSION_LEVEL=10 + UNSUFFIXED_FILENAME=${filename%.*} + BITRATE=128 + + git restore --staged "$GITDIR/$filename" + + if [ -e "$GITDIR/$UNSUFFIXED_FILENAME.opus" ] + then + rm "$GITDIR/$filename" + echo "SKIPPING $filename : the filename already exists" + continue + fi + + # One conversion atleast + # This will do opus to opus with decoder + # Degrading once quality but we tank it + ffmpeg -hide_banner -y -loglevel warning \ + -i "$GITDIR/$filename" \ + -codec:a libopus \ + -compression_level $COMPRESSION_LEVEL \ + -vbr on \ + "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus"; + + mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus" + rm "$GITDIR/$filename" + + while [ -e "$GITDIR/$UNSUFFIXED_FILENAME.opus" ] && \ + [ $(du -a "$GITDIR/$UNSUFFIXED_FILENAME.opus" | \ + awk -F' ' '{print $1}') -ge $AUDIO_LIMIT ] && \ + [ $COMPRESSION_LEVEL -ge 3 ]; do + + COMPRESSION_LEVEL=$(($COMPRESSION_LEVEL - 3)) + BITRATE=$(($BITRATE / 2)) + + ffmpeg -hide_banner -y -loglevel warning \ + -y -i "$GITDIR/$UNSUFFIXED_FILENAME.opus" \ + -codec:a libopus \ + -compression_level $COMPRESSION_LEVEL \ + -vbr constrained \ + -b:a "${BITRATE}k" \ + "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus"; + + mv "$GITDIR/$UNSUFFIXED_FILENAME-temp.opus" "$GITDIR/$UNSUFFIXED_FILENAME.opus"; + done; +done; + +git add $GITDIR