-1

I've got a bare repo on a production server, which is updated (utilising a webhook in Gitlab) whenever a merge request is approved.

The repo is updated like so:

git fetch origin master:master

git --work-tree=/home/prod/git/ --git-dir/=/path/to/bare-repo.git checkout -f

I would like to use the post-checkout hook to verify whether a conda environment file was changed; Content of the hook:

#!/bin/sh

prevHEAD=$1
newHEAD=$2
checkoutType=$3

changed_files="$(git diff-tree -r --name-only --no-commit-id  $prevHEAD $newHEAD)"

Unfortunately everytime the checkout is triggered, and I'm not clear on why this happens, prevHEAD and newHEAD variables are always the same. The values themselves differ on every checkout, however, they always match each other.

Assuming this method can't be used, what would be the best way to identify whether a particular file was changed?

Elsewhere I've read to use push-to deploy model. This would require replacing the fetch & checkout commands with a git push instead, and utilising post-receive hook. I'll look into this, but given that the fetch & checkout already work fine, I'd prefer not changing that setup if possible at all.

Maikol
  • 195
  • 2
  • 2
  • 10
  • Have you looked at [this answer](https://stackoverflow.com/a/56787188/570918)? – merv Mar 21 '22 at 20:44
  • They are of course the same. Where do you expect Gt to store the hash ID of the *previous* checkout, in this one-time-usage `git --work-tree=... checkout -f`? (Not just a rhetorical question: it's the key to making this work. One potential answer to that question might be "the index", and if Git stored the hash ID there, it could be made to work, but Git does not store the hash ID there.) You will definitely need *some* other or more code, but you may have to come up with it on your own here. – torek Mar 22 '22 at 07:14
  • Sorry, I don't really understand why in my case the id's are the same, when would they not be the same? I see offcourse that the id changes as soon as i run git fetch..., but given that the post-checkout hook is supposed to return the previous id as the first argument, why doesn't it do that in my particular case? – Maikol Mar 22 '22 at 11:00

1 Answers1

0

As a work around I used the git log to retrieve current and previous head id's:

prevHead=$(git log --pretty="%h" --merges -1 --skip=1)
newHead=$(git log --pretty="%h" --merges -1)
Maikol
  • 195
  • 2
  • 2
  • 10