[英]Git commit with pre-commit hook, why it get different results?
I have a pre-commit hook to run a python script that will modify the staged files and re-add those files with git add.
我有一个预提交挂钩来运行 python 脚本,该脚本将修改暂存文件并使用
git add.
at the end of the script.在脚本的末尾。
The pre-commit look like this:预提交如下所示:
#!/bin/sh
python2.7 .git/hooks/editfile.py
The python script look like this: python 脚本如下所示:
import os
import mmap
import sys
import subprocess
def getmodifiedfiles():
files = []
args = ['git', 'diff', 'HEAD', '--name-only', '-r', '--diff-filter=M']
with open(os.devnull, 'w') as b:
files = subprocess.check_output(args, stderr=b).splitlines()
files = [i for i in files if os.path.isfile(i)]
return files
def updaterevision(line):
strVer = line.split("$Revision: ")[1].split()[0]
version = [x for x in strVer.split('.')]
ver = [int(i) for i in version]
if ver[1] == 99:
ver[0] += 1
ver[1] = 0
else:
ver[1] += 1
strVer = "%d.%02d" % (ver[0], ver[1])
return str.replace(line, line.split("$Revision: ")[1].split()[0], strVer)
def main(args):
filelist = getmodifiedfiles()
for file in filelist :
lines = open(file).readlines()
i = 0
for line in lines:
if '$Revision:' in line:
lines[idx] = updaterevision(line)
i += 1
with open(file, 'w') as r:
r.writelines(lines)
args = ['git', 'add', '.']
subprocess.call(args)
if __name__ == '__main__':
main(sys.argv)
It works as expected with git commit -m "msg"
command, when git status I got the following result:它与
git commit -m "msg"
命令按预期工作,当 git 状态时,我得到以下结果:
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
nothing to commit (working directory clean)
But if commit using git commit <filename>
or git commit -m "msg" <filename>
, I got the following result which I don't want:但是如果使用
git commit <filename>
或git commit -m "msg" <filename>
,我会得到以下我不想要的结果:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: filename.py
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: filename.py
What are the different?有什么不同? I don't want to fix user to only use the first command to commit.
我不想让用户只使用第一个命令来提交。 Any ideas?
有任何想法吗?
Unfortunately, with your file-rewriting pre-commit hook, the only thing you can do is to stay away from git commit <files...>
and only use git-add
-then- git-commit
.不幸的是,使用你的文件重写预提交钩子,你唯一能做的就是远离
git commit <files...>
并且只使用git-add
-then- git-commit
。
That said, you're not completely out of hope .也就是说,您并没有完全失去希望。 Since your hook script is intended for rewriting files (setting "version information"), using filters is a better idea.
由于您的挂钩脚本旨在重写文件(设置“版本信息”),因此使用过滤器是一个更好的主意。
Check this answer out: https://stackoverflow.com/a/17360528/5958455检查这个答案: https://stackoverflow.com/a/17360528/5958455
Basically you set your script as a "clean filter" so it gets applied whenever a matching file is staged .基本上,您将脚本设置为“干净的过滤器”,以便在暂存匹配文件时应用它。 This luckily includes your use case of
git commit <file...>
.幸运的是,这包括您的
git commit <file...>
用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.