2

I got 2 commands (which are not related to git) which I want to be executed both on commits (doesn't matter whether before or after) and after pulls.

However, these commands take some time to run, so I don't want them to be executed on every commit or pull. They only do useful work if files in a specific folder changed since they last were run.

They create or update a file ignored by git in that same folder. Of course, this file being changed shouldn't trigger execution of the commands the next time a commit or pull is performed. All files in that folder are on the top level of it (there are no subdirectories). All the files whose changes are relevant to whether the commands should be executed are tracked by git. When new files appear in the folder and aren't tracked by git yet, I don't care whether the commands are executed already. But they must be executed when a commit which adds them to the repo is created.

How can this be tackled?

UTF-8
  • 575
  • 1
  • 5
  • 23

1 Answers1

1

I think this does what I want. I came up with it after larsks gave me a hint. There is no hook which only executes after a pull but doing at after any merge is fine, too.

The hooks post-commit and post-merge now have this content:

#!/bin/sh
./.hooks/conditionallyMergeChangedPdfs
exit 0

.hooks/conditionallyMergeChangedPdfs contains this:

#!/bin/bash
for folder in "Vorlesungsfolien" "Übungsfolien"
do
    diff="$(git diff HEAD^ --name-only --diff-filter=ACMR "$folder")"
    if [ ! -z "$diff" ]
    then
    rm -f "$folder/All.pdf"
    pdftk "$folder/"* cat output "$folder/All.pdf"
    fi
done

Unfortunately, this isn't thread safe because apparently one cannot get the hash of the new commit but has to rely on what the repo contains when the script is executed instead.

UTF-8
  • 575
  • 1
  • 5
  • 23