我有大约40个git存储库,其中大多数都有一个.git-blame-ignore-revs文件,可用于git blame。因此,我没有使用blame.ignorerevsfile=.git-blame-ignore-revs在本地配置它们,而是将它们应用到全局git配置中。
但是,没有这样一个文件的少数存储库会受到它的影响:在它们上运行git blame将导致fatal: could not open object name list: .git-blame-ignore-revs。
是否有一种方法可以告诉git读取这样的文件,如果它存在,否则忽略它?这将是理想的:简单的设置,但不突出,如果不存在这样的文件。
注意:有一个可选的.git-blame-ignore-revs文件很有用的另一个原因是,几乎所有的git存储库都是在没有这样一个文件的情况下创建的,而且只是在以后才添加。但是,当对旧提交进行二分法时,我们可能会返回到文件存在之前的某个点,然后我们必须对其进行touch,完成对齐,然后再删除它,以便能够返回到头提交。即使我使用本地配置文件,这种极其恼人的情况仍然会发生。因此,我确实认为,如果文件不存在,默认行为应该是忽略它。
发布于 2021-12-21 13:28:18
是否有一种方法可以告诉git读取这样的文件,如果它存在,否则忽略它?
没有。要么使用
git config --local blame.ignoreRevsFile .git-blame-ignore-revs而不是--global。或使用
git config --local blame.ignoreRevsFile ""在那些不存在文件的存储库中。空文件名意思是“不要使用任何文件”。
或者创建空的.git-blame-ignore-revs文件。
Upd。使用新的临时存储库进行测试:
git init test-git
cd test-git
git config --global blame.ignoreRevsFile test
git config --show-origin blame.ignoreRevsFile
file:/home/phd/.gitconfig test
git config --local blame.ignoreRevsFile ""
git config --show-origin blame.ignoreRevsFile
file:.git/configUpd2
git blame testfile
fatal: could not open object name list: test唉哟!
发布于 2022-06-29 17:40:26
嗯,这不是一个完美的解决方案,因为它不适用于原始的git指责,但我使用了shell命令别名。显然,根据需要更改命令,但是FWIW我确实获得了更好的结果--用'-w‘(忽略空格)提供的用例:
git config --global alias.bl "!myGitBlame.sh"以及相应的shell脚本(假定该脚本位于前面的git config命令的路径上):
#!/usr/bin/env bash
if [[ -z $GIT_PREFIX ]]; then
GIT_PREFIX=./
fi
# Assume (and impose limitation) that the last argument to this script is the
# file being blamed. Pull it out of the args list and make it relative to the
# root of the git repo to match the execution context of git shell aliases
LAST_ARG="${@: -1}"
set -- "${@:1:$(($#-1))}"
FILE_PATH=$GIT_PREFIX$LAST_ARG
# optional ignore revs file, specified relative to the root of the git repo
OPTIONAL_GIT_BLAME_REVS_FILE=.git-blame-ignore-revs
# if the revs file does not exist, ignore-revs-file accepts empty values
# without issue
if [[ ! -f $OPTIONAL_GIT_BLAME_REVS_FILE ]]; then
OPTIONAL_GIT_BLAME_REVS_FILE=""
fi
git blame -w --ignore-revs-file=$OPTIONAL_GIT_BLAME_REVS_FILE "$@" $FILE_PATH如果你想轻易地把它传递给同事,那就像一条单线:
git config --global alias.bl '!if [[ -z $GIT_PREFIX ]]; then GIT_PREFIX=./; fi; f() { LAST_ARG="${@: -1}"; set -- "${@:1:$(($#-1))}"; FILE_PATH=$GIT_PREFIX$LAST_ARG; GIT_BLAME_REVS_FILE=.git-blame-ignore-revs; if [[ ! -f $GIT_BLAME_REVS_FILE ]]; then GIT_BLAME_REVS_FILE=""; fi; git blame -w --ignore-revs-file=$GIT_BLAME_REVS_FILE "$@" $FILE_PATH; }; f'注意:这就假设git的最后一个参数是文件;这意味着这与普通的git非命令行所接受的不是1:1,因为在简单的情况下可以从技术上说是git blame <commit_hash> <file>或git blame <file> <commit_hash>。
我肯定会在shell脚本形式中使用它,但是我知道1-liner的优点,它可以将它传播给同事,并避免一般的路径。
我也假设这里有一个bash可用的环境。
我最初只是尝试做一个直接指向文件的本地配置,但这会遇到您所描述的各种问题,而且它与工作树设置的交互也很差。这应该更普遍,虽然不是完全完美。
https://stackoverflow.com/questions/70435937
复制相似问题