Why is the index not updated after doing a git add in a pre-commit hook?

纵饮孤独 提交于 2019-12-22 11:35:37

问题


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

回答1:


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

(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.




回答2:


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


来源:https://stackoverflow.com/questions/29362796/why-is-the-index-not-updated-after-doing-a-git-add-in-a-pre-commit-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!