From f6902f3e50b24e955c2aa0c57dbd511f9f195008 Mon Sep 17 00:00:00 2001 From: Simon C Date: Thu, 29 Apr 2021 21:55:43 +0200 Subject: [PATCH] fix: Error when image file is on first commit... why? --- scripts/run | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/scripts/run b/scripts/run index bbcd0d9..0b3bcf5 100755 --- a/scripts/run +++ b/scripts/run @@ -88,20 +88,25 @@ optimizer() { } optimizer_git_image() { + first_commit_hash=$(git rev-list --max-parents=0 HEAD) + + # https://stackoverflow.com/a/48999882/6429616 + # install git alias amend-to with auto stash + check_git_command=`git config --global --list | grep amend-to | wc -c` + if [ "$check_git_command" -ne 187 ]; then + git config --global alias.amend-to '!f() { SHA=`git rev-parse "$1"`; git stash -k && git commit --fixup "$SHA" && GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^" && git stash pop; }; f' + fi + for file in $(git diff --name-only --diff-filter=M --ignore-submodules); do - last_commit=`git log -n 1 --pretty=format:%H -- $file` + last_file_update_commit_hash=`git log -n 1 --pretty=format:%H -- $file` current_size=`cat $file | wc -c` - old_size=`git cat-file -p $last_commit:$file | wc -c` + old_size=`git cat-file -p $last_file_update_commit_hash:$file | wc -c` - # https://stackoverflow.com/a/48999882/6429616 - check_git_command=`git config --global --list | grep amend-to | wc -c` - if [ "$check_git_command" -ne 187 ]; then - git config --global alias.amend-to '!f() { SHA=`git rev-parse "$1"`; git stash -k && git commit --fixup "$SHA" && GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^" && git stash pop; }; f' - fi - - if [ "$current_size" -lt "$old_size" ]; then + if [ "$first_commit_hash" == "$last_file_update_commit_hash" ]; then + echo "💩 file '$file' is on first commit..." + elif [ "$current_size" -lt "$old_size" ]; then git add $file - git amend-to $last_commit + git amend-to $last_file_update_commit_hash fi done }