在提交之前运行脚本并在此提交中包含更新? [英] Run script before commit and include the update in this commit?

查看:92
本文介绍了在提交之前运行脚本并在此提交中包含更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个脚本,通过扫描源代码生成 Readme.md 文件(用于GitHub)。每次我做一个新的提交之前,我手动运行这个脚本来更新 Readme.md 。它肯定会更好,如果这项工作自动完成。



目前我使用 pre-commit git钩子,这只是部分工作。 Readme.md 文件被更新,但更新不是此提交的一部分。我必须将其包含在下一次提交中。



有没有一种方法可以运行这个脚本并将更新作为此次提交的一部分?


根据这个SO线程(可以一个Git钩子自动添加文件)

解决方案

到提交?) git add 在最近版本的git上无法用于预提交钩子。



作为一种解决方法,您可以使用预提交和提交后挂接来生成 Readme.md ,然后在提交后提交后进行提交钩然后推荐你的第二次提交。



不是我的想法,请按照原始解释的链接。



通过 @bitluck 链接我的线索:


触摸文件.commit或其他内容。 (一定要把它添加到.gitignore)

 #!/ bin / sh 
echo
touch .commit
exit b $ b

如果.commit存在,您知道一个提交已经发生,但是
提交后尚未运行。所以,你可以在这里做你的代码生成。
此外,测试.commit,如果存在:


  • 添加文件

  • commit --amend -C HEAD --no-verify(避免循环)

  • 删除.commit文件

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




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?

解决方案

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
    

这篇关于在提交之前运行脚本并在此提交中包含更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆