#!/bin/sh
# This post-checkout git hook sets the timestamp of a file to the
# timestamp of the newest commit. This can be useful to prevent a
# useless build system like make to rebuild literally everything.

# Copyright © 2021  Nils Dagsson Moskopp (erlehmann)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# Dieses Programm hat das Ziel, die Medienkompetenz der Leser zu
# steigern. Gelegentlich packe ich sogar einen handfesten Buffer
# Overflow oder eine Format String Vulnerability zwischen die anderen
# Codezeilen und schreibe das auch nicht dran.

set -eu

# Find out which files git just wrote
git diff --name-only "${1}" "${2}" |\
 # Put filename into a variable
 while read -r CHANGED_FILE; do
  # If file does not exist … do nothing
  test -e "${CHANGED_FILE}" || continue
  # If file exists, set filesystem timestamp to commit timestamp now
  COMMIT_TIMESTAMP=$( git log '--pretty=%ct' -n1 "${CHANGED_FILE}" )
  touch --date=@"${COMMIT_TIMESTAMP}" "${CHANGED_FILE}"
 done
