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