我的存储库中有此配置:
echo '*.java diff=java' >> .gitattributes
当我这样做时git diff ...
我得到了预期的结果。
但是,我正在编写一个脚本来分析其他 git 存储库,所以我通常会这样做:
git --git-dir='/path/to/repo/.git' diff ...
我想做的是像这样运行脚本,并获得与属性存在相同的结果。我不想设置全局属性或将 .gitattributes 添加到我想要分析的 git 项目中。我不介意类似的事情
git --git-dir='/path/to/repo/.git' diff --word-diff-regex=<java-rexeg>
但我不知道如何获取 git 使用的 java 正则表达式。
最佳答案
来自git帮助配置
:
core.attributesfile
In addition to
.gitattributes
(per-directory) and .git/info/attributes, git looks into this file for attributes (seegitattributes(5)
). Path expansions are made the same way as forcore.excludesfile
. Its default value is $XDG_CONFIG_HOME/git/attributes. If$XDG_CONFIG_HOME
is either not set or empty,$HOME/.config/git/attributes
is used instead.
换句话说,您可以在 $XDG_CONFIG_HOME/git/attributes
创建自己的属性文件,并将 XDG_CONFIG_HOME
传递给您的 git 调用,如下所示:
XDG_CONFIG_HOME=config_dir git --git-dir='/path/to/repo/.git' diff ...
因为您可以准确控制 XDG_CONFIG_HOME
,所以它不会是真正的全局 - 您可以完全即时完成。
或者,在 $HOME/.config/git/attributes
中创建全局 git 属性文件可能会更简单。
关于regex - 从命令行使用 git 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950604/