git - 在提交中只获取新添加/修改的行的 git 命令是什么?
问题描述
我想编写shell脚本来解析java源文件并根据一些规则找出源代码中的常见违规行为。这个脚本应该在添加/修改的新代码上运行,这不应该得到旧代码。我的代码库是 git。
如何从 git 获取新添加/修改的行。然后我可以在上面执行我的脚本。
解决方案
如果您想对代码应用一些解析操作,您最好解析整个文件,而不是只发现修改过的行。最重要的是,“附加线”的概念很大程度上取决于在 中实施的启发式方法diff
,并且通常会给您提供与您期望的结果不符的结果。
我没有从您的问题中获得上下文,尤其是您打算比较的内容:
- 列出修改过的文件,使用
git diff --name-only
orgit diff --name-status
:
# list of tracked files modified on disk since last commit :
git diff --name-status HEAD
# list of files modified between 2 commits :
git diff --name-status <commit1> <commit2>
# list of files modified in branch 'feature' since it forked from 'master' :
# (this will give you the same list as in a PR view)
git diff --name-status master...feature # 3 dots, not a typo
# list files modified in a specific commit :
# compare it to its parent (add a '^' at the end)
git diff --name-status <commit>^ <commit>
如果您需要针对合并提交(具有多个父级),git show
请尝试变得聪明并可能会给您带来更好的结果:
git show --format="" --name-only <commit>
如果您真的只想要修改/添加的行:只需 grep 它们git diff
对于每个修改过的文件,运行类似:
# get lines starting with '+', but drop the extra '+++ the/file' line added
# by git
git diff <a> <b> -- the/file | grep '^+' | grep -v '^+++'
推荐阅读
- java - 使用 jquery 调用控制器方法
- r - 根据条件计算持续时间,同时分组但不聚合
- c++ - C++'系统无法执行指定的程序'错误信息
- regex - 正则表达式从字符串中提取所有有效的 IPv4
- r - 在 R 中,我有一列带有文本。如何在 R 中编写一个脚本来计算特定单词的频率?
- reactjs - Apollo Client React 和当地最佳实践
- oauth-2.0 - 如果范围是 graph.microsoft.com,则访问令牌验证失败
- c++ - 在 C++ 中使用 inline 和 constexpr
- wpf - 迭代期间的WPF列表修改?
- python - Selenium Chrome tap 在从 werkzeug 服务器执行后崩溃