gpt4 book ai didi

python - 是否可以让所有 "git diff"命令在所有 git 项目中使用 "Python diff"?

转载 作者:IT王子 更新时间:2023-10-29 00:38:32 25 4
gpt4 key购买 nike

包含行时

*.py diff=python

在本地 .gitattributes 文件中,git diff 为 Python 文件的不同 diff hunk 生成漂亮的标签(带有更改所在的函数的名称等)。

是否可以要求 git 对所有 git 项目中的所有 Python 文件使用这种差异模式?我尝试设置全局 ~/.gitattributes,但本地 git 存储库不使用它。有没有比使用 ln -s ~/.gitattributes 初始化每个新 git 项目更方便的方法?

最佳答案

引自gitattributes(5) :

Attributes that should affect all repositories for a single user should be placed in a file specified by the core.attributesfile configuration option (see git-config(1)). 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. Attributes for all users on a system should be placed in the $(prefix)/etc/gitattributes file.

TL;DR: echo '*.py diff=python' >> "${XDG_CONFIG_HOME:-$HOME/.config}"/git/attributes


7年后更新

好吧,没有必要为 *.py 文件配置 diff=python——这是很久以前的默认设置。

但一般 Gist 仍然存在:您可以在本地(每个存储库).gitattributes 中设置的任何内容,您也可以设置为全局(每个机器)。

man 5 gitattributes 中有很多很好的例子本身,所以使用 RTFM。

让我们只做一个自定义设置:--word-diff 所有 Markdown 文件(感谢 @RayLuo 在评论中提出这个建议)。

首先,我们添加一个外部差异驱动程序:

git config --global diff.stackoverflow-word-diff.command ~/.local/bin/stackoverflow-word-diff

API 是这样的,我们必须制作一个独立的包装器可执行文件。

cat > ~/.local/bin/stackoverflow-word-diff << 'EOF'
#!/bin/bash -eu

#-- uncomment for debug:
#echo >&2 "$(basename $0) args: $@"; set -x

FILENAME="$1"
OLDFILE="$2"
OLDHASH="$3"
OLDMODE="$4"
NEWFILE="$5"
NEWHASH="$6"
NEWMODE="$7"

git diff --no-ext-diff --word-diff "$OLDFILE" "$NEWFILE" || exit 0

#-- from https://stackoverflow.com/a/18948381/531179
#-- see `man 1 git` /EXTERNAL_DIFF, or https://www.git-scm.com/docs/git
EOF
chmod +x ~/.local/bin/stackoverflow-word-diff

最后,我们通过全局 gitattributes 将其绑定(bind)到 *.md*.markdown:

mkdir -vp "${XDG_CONFIG_HOME:-$HOME/.config}"/git

{ echo '*.md diff=stackoverflow-word-diff'; \
echo '*.markdown diff=stackoverflow-word-diff; \
} \
>> "${XDG_CONFIG_HOME:-$HOME/.config}"/git/attributes

这就是所有人!测试一下。

关于python - 是否可以让所有 "git diff"命令在所有 git 项目中使用 "Python diff"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2788649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com