3

I have a pre-commit hook that:

  • checks if my version file needs to be updated
  • modifies said file
  • performs a git add on this file

This allows me to add the updated version file to the same commit. The only problem I have is, after the hook runs the working tree and HEAD are updated with the new version file but the index isn't. I need to manually stage the version file for the index to reflect the changes.

I updated git to 2.3.4 but that doesn't solve anything.

Am i missing anything here?

UPDATE

The current setup for version files:

  • Every module contains a version file
  • they are stored here: /module/<name of module>/version.txt
  • they contain a manually entered version number (eg 1.5.2) and the parent commit hash
Community
  • 1
  • 1
diesonne
  • 126
  • 10
  • Based on the answers to [Can a Git hook automatically add files to the commit?](https://stackoverflow.com/questions/3284292/can-a-git-hook-automatically-add-files-to-the-commit) comments under those answers, different versions of Git behave differently for pre-commit hooks. There seems to be a lot of inconsistency. – Inigo Dec 16 '22 at 11:31

2 Answers2

1

You would be better off with a content filter driver, more specifically a clean filter.

clean

(image shown in "Customizing Git - Git Attributes", from "Pro Git book")

That filter (associated to the version file through a .gitattributes declaration) would run automatically on git diff and git commit, and would:

  • detect if the version needs to change
  • update the version file if needed

That way, a regular commit would end up committing an updated version file. Automatically.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have updated my question. Is this possible with a content filter driver? is setting `version.txt filter=versionupdate` correct here ? How do I tell the versionupdate script to only check the files for the current module? – diesonne Mar 31 '15 at 08:27
  • @diesonne I am not familiar with the structure of your project, but the idea of the script is to detect at all time the version of what you need in order to replace the right value in the version.txt. – VonC Mar 31 '15 at 08:34
  • @diesonne the filter will apply to all `version.txt` files (one execution of the script per file), and the script needs to be able to detect which `version.txt` it operates on in order to get the right information. – VonC Mar 31 '15 at 08:36
  • If I understand this then I would need to manually update the `version.txt` file every time in order for the filter to pick it up and modify it? – diesonne Mar 31 '15 at 09:06
  • @diesonne the idea is for the script to detect the new version and update the right file (which would then be part of the commit automatically). I don't know how it detects the new version number or from where. That's specific to your project. But if you can detect that version number, then my point is that a version.txt can then be committed automatically through a clean content filter driver. – VonC Mar 31 '15 at 09:09
  • thats the problem ... the version number doesn't change unless I need it to. That is why I wanted to automatically add the parent commit hash to the version file. I guess the content filter driver is not an option then. – diesonne Mar 31 '15 at 09:13
  • @diesonne add the parent commit hash? The filter clean script can do that without any issue then. – VonC Mar 31 '15 at 09:14
  • if I put the filter on version.txt => the version file needs manual changes in order to be picked up by the filter if I put the filter on modules/* => I can modify the version file but it is not added to the commit... back to square one – diesonne Mar 31 '15 at 11:31
  • @diesonne again, the all idea is for the script to *detect* that the version has changed. I have no idea how it is supposed to detect it (that is specific to your project), but if it does, then the clean script can automatically, on git commit, update a file. That is all my answer allows. – VonC Mar 31 '15 at 11:42
0

I have added a post-commit hook that will automatically do a git add of the version file (if there is one). If anyone has a better/easier/cleaner fix for this issue let me know.

For those interested this is the post-commit:

for fn in `git diff-tree -r --name-only --no-commit-id develop`; do
    if [[ $fn == *"version.txt" ]];then #prevent infinite loops that will eventually fill up the entire harddrive
        echo "post-commit hook found a version file at $fn"
        git add $fn
    fi
done
diesonne
  • 126
  • 10