website/hooks/pre-commit

23 lines
868 B
Python

#!/usr/bin/env python3
import sys
import os
import subprocess
from pathlib import Path
staged_fnames = subprocess.check_output(["git", "diff", "--name-only", "--cached"]).decode().splitlines()
contains_gps = []
for fname in staged_fnames:
try:
os.stat(fname)
except FileNotFoundError: # deleted in this commit
continue
cont = subprocess.check_output(["exiftool", "-gpslatitude", "-gpslongitude", "-T", fname]).decode()
if cont != "-\t-\n": # output where there is no result
contains_gps.append((fname, cont))
if len(contains_gps) != 0:
print("The following files contain GPS EXIF data hence the commit has been aborted:" + "\n - " + ("\n - ".join(map(lambda x: f"{x[0]} ({x[1].strip()})", contains_gps))))
print("\nYou can use the following command to strip GPS EXIF data from an image:\n\texiftool -gps:all= -overwrite_original $FILE")
sys.exit(1)