the home site for me: also iteration 3 or 4 of my site
1+++ 2title = "Cleaning exif data with git pre-commit" 3date = 2025-02-15T19:57:01 4slug = "remove-exif-git-hook" 5description = "took longer then it probably should have 😊" 6 7[taxonomies] 8tags = ["mildrant", "tutorial"] 9 10[extra] 11has_toc = true 12+++ 13 14I saw this [post](https://jade.fyi/blog/pre-commit-exif-safety/) from [jade.fyi](https://jade.fyi) on using a git hook to clear exif data from your images before you commit them and realized I should probably implement that too lol. Interestingly jade also uses zola for her site but she used pre-commit hooks whereas I wanted to do something that used native git hooks. 15 16I started with the naive method of just having a `.git/hooks/pre-commit` file that would run `exiftool` on the input but after realizing that hooks placed there wouldn't be synced to the repo decided that wasn't the best way. I moved to using a script that would symlink files from the `hooks` directory to `.git/hooks`. It worked moderately well but due to the fact that I used (yes I feel the shame admitting this [:uw_embarrassed:](https://cachet.dunkirk.sh/emojis/uw_embarrassed/r)) `#!/bin/bash` instead of `#!/usr/bin/env bash`. Not realizing my mistake and believing it to be related to the symlink I found [this stack overflow](https://stackoverflow.com/questions/4592838/symbolic-link-to-a-hook-in-git/#:~:text=While%20you%20can%20use%20symbolic%20links) answer which taught me that you can use `git config core.hooksPath hooks` to move the hooks directory to `./hooks` in the root of your repo! After doing that and it still not working (i feel very dense writing this lol) I finally realized that the shebang was wrong and then it worked! 17 18{{ img(id="https://cdn.hackclubber.dev/slackcdn/9049d20038cc3058acee1bbe58c5ac3f.png" alt="the commit hook finally working!" caption="phew") }} 19 20Is there anything at all to learn from this? Well yes actually! You can use the script below and the `git config core.hooksPath hooks` setting to scrub your own images! 21 22> hooks/pre-commit 23```bash 24#!/usr/bin/env bash 25 26# Check if exiftool is installed 27if ! command -v exiftool &> /dev/null; then 28 echo "Error: exiftool is not installed. Please install it." >&2 29 exit 1 30fi 31 32while read -r file; do 33 case "$file" in 34 *.jpg|*.jpeg|*.png|*.gif|*.tiff|*.bmp) 35 echo "Removing EXIF data from: $file" >&2 36 exiftool -all= --icc_profile:all -tagsfromfile @ -orientation -overwrite_original "$file" 37 if [ $? -ne 0 ]; then 38 echo "Error: exiftool failed to process $file" >&2 39 exit 1 40 fi 41 git add "$file" 42 ;; 43 *) 44 ;; 45 esac 46done < <(git diff --cached --name-only --diff-filter=ACMR) 47 48exit -0 49``` 50 51> if you want to add something or comment on the post then I posted about it on bluesky: [https://bsky.app/profile/dunkirk.sh/post/3liaybkkas226](https://bsky.app/profile/dunkirk.sh/post/3liaybkkas226)