简体   繁体   English

所有先前版本中的git:grep文件

[英]git: grep file in all previous versions

Consider a Python configuration file with the following line: 考虑具有以下行的Python配置文件:

THEME = 'gum'

The THEME key appears only once, so when I want to know what the THEME is, I grep the file: THEME键只出现一次,所以当我想知道THEME是什么时,我grep文件:

grep THEME pelicanconf.py

The file is kept in git, and I would like to grep for THEME in all previous git commits, in order to tell when was this line changed. 该文件保存在git中,我想在之前的所有git提交中grep for THEME ,以便知道此行何时更改。

Is there an elegant way to grep the entire history of a git file? 是否有一种优雅的方式来grep git文件的整个历史记录?

git log -S'THEME' -- pelicanconf.py | xargs -n 1 git show git log -S'THEME' -- pelicanconf.py | xargs -n 1 git show shows the content of every commit that changed THEME git log -S'THEME' -- pelicanconf.py | xargs -n 1 git show显示了每次更改THEME提交内容

However, it prints out full commits, not only changes. 但是,它打印出完整的提交,而不仅仅是更改。

var="THEME"; git log -S"$var" -p -- pelicanconf.py | egrep "$var|commit|Date:"

would show you all variants of THEME with commit hashes and dates. 会告诉你THEME所有变种与提交哈希和日期。

Thx @knittl for -p option. Thx @knittl for -p选项。

Also, found a solution with gitk : see here . 另外,找到了一个gitk的解决方案: 见这里

The way to do what you want is to use the bisect run <script> . 执行所需操作的方法是使用bisect run <script>
Use the bisect as you did so far and use the bisect with the script option. 像目前为止一样使用bisect,并使用带有脚本选项的bisect。

The bisect search all your commits (in the given range) and with the suitable exit code you can tell if the code is good or bad. bisect搜索所有提交(在给定范围内)并使用合适的退出代码,您可以判断代码是好还是坏。

The script will return the appropriate code for skipping (or 125 for not testable - much more suitable in your case). 该脚本将返回相应的跳过代码(或125不可测试 - 在您的情况下更合适)。

Note that the script (my_script in the above example) should exit with code 0 if the current source code is good, and exit with a code between 1 and 127 (inclusive), except 125, if the current source code is bad. 请注意,如果当前源代码良好,脚本(上例中的my_script)应该以代码0退出,如果当前源代码不好,则退出时使用1到127(含)之间的代码,125除外。

Any other exit code will abort the bisect process. 任何其他退出代码都将中止bisect进程。 It should be noted that a program that terminates via "exit(-1)" leaves $? 应该注意的是,通过“exit(-1)”终止的程序会留下$? = 255, (see the exit(3) manual page), as the value is chopped with "& 0377". = 255,(参见exit(3)手册页),因为该值用“&0377”切断。

The special exit code 125 should be used when the current source code cannot be tested. 当无法测试当前源代码时,应使用特殊退出代码125。 If the script exits with this code, the current revision will be skipped (see git bisect skip above). 如果脚本以此代码退出,则将跳过当前修订(请参阅上面的git bisect)。 125 was chosen as the highest sensible value to use for this purpose, because 126 and 127 are used by POSIX shells to signal specific error status (127 is for command not found, 126 is for command found but not executable---these details do not matter, as they are normal errors in the script, as far as "bisect run" is concerned). 选择125作为用于此目的的最高敏感值,因为POSIX shell使用126和127来指示特定的错误状态(127表示未找到命令,126表示找到命令但不可执行---这些详细信息无所谓,因为它们是脚本中的正常错误,就“bisect run”而言。

Sample project for bisect bisect的示例项目


Using git log 使用git log

You can pass -p flag to the log to follow a specific file 您可以将-p标志传递给日志以跟踪特定文件

git log -p filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM