Git pre-commit hook

前端 未结 1 622
后悔当初
后悔当初 2021-01-07 12:10

I\'m new to git hooks. I\'m not able to understand below pre-commit hook. Can anyone tell me how this works please.Here my doubt is how grep will be happened in committed f

相关标签:
1条回答
  • 2021-01-07 13:00

    You should rather grep on indexed (cached) files, instead of your working tree.
    Otherwise, your grep could find debug instructions in files (or part of files) which aren't part of the next commit.

    See "Git pre-commit hook : changed/added files":

    git diff --cached --name-only --diff-filter=ACM
    

    As explained in "Why You Need a Git Pre-Commit Hook and Why Most Are Wrong":

    Most test against whatever files are currently on disk, not what is in the staging area (the files actually being committed).

    The approach if that hook is a bit different: it stashes every work in progress before searching the files.

    def main(all_files):
        # Stash any changes to the working tree that are not going to be committed
        subprocess.call(['git', 'stash', '-u', '--keep-index'], stdout=subprocess.PIPE)
    
    0 讨论(0)
提交回复
热议问题