Run script before commit and include the update in this commit?

安稳与你 提交于 2019-12-13 12:08:27

问题


I wrote a script that generates Readme.md file (for GitHub) by scanning the source code. Everytime before I make a new commit, I run this script manually to update Readme.md. It sure would be better if this job being done automatically.

Currently I'm using pre-commit git hook, which works only partly. The Readme.md file gets updated, however the update is not part of this commit. I have to include it in the next commit.

Is there a way to run this script and make the update part of this commit?


回答1:


According to this SO thread (Can a Git hook automatically add files to the commit?), git add won't work on pre-commit hook with recent version of git.

As a workaround, you can play with pre-commit and post-commit hooks to generate your Readme.md then commit it after your commit with post-commit hook then ammend the second commit with yours.

Not's my idea, follow the links for original explanations.

Answer by @bitluck on the thread I linked :

Touch a file .commit or something. (be sure to add this to .gitignore)

#!/bin/sh 
echo 
touch .commit 
exit

if .commit exists you know a commit has just taken place but a post-commit hasn't run yet. So, you can do your code generation here. Additionally, test for .commit and if it exists:

  • add the files
  • commit --amend -C HEAD --no-verify (avoid looping)
  • delete .commit file

    #!/bin/sh
    echo
    if [ -a .commit ]
    then
      rm .commit
      git add yourfile
      git commit --amend -C HEAD --no-verify
    fi
    exit
    


来源:https://stackoverflow.com/questions/30376741/run-script-before-commit-and-include-the-update-in-this-commit

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