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 16<!-- more --> 17 18I 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! 19 20{{ img(id="https://cdn.hackclubber.dev/slackcdn/9049d20038cc3058acee1bbe58c5ac3f.png" alt="the commit hook finally working!" caption="phew") }} 21 22Is 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! 23 24> hooks/pre-commit 25```bash 26#!/usr/bin/env bash 27 28# Check if exiftool is installed 29if ! command -v exiftool &> /dev/null; then 30 echo "Error: exiftool is not installed. Please install it." >&2 31 exit 1 32fi 33 34while read -r file; do 35 case "$file" in 36 *.jpg|*.jpeg|*.png|*.gif|*.tiff|*.bmp) 37 echo "Removing EXIF data from: $file" >&2 38 exiftool -all= --icc_profile:all -tagsfromfile @ -orientation -overwrite_original "$file" 39 if [ $? -ne 0 ]; then 40 echo "Error: exiftool failed to process $file" >&2 41 exit 1 42 fi 43 git add "$file" 44 ;; 45 *) 46 ;; 47 esac 48done < <(git diff --cached --name-only --diff-filter=ACMR) 49 50exit -0 51``` 52 53> 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)