【发布时间】:2021-08-14 21:55:30
【问题描述】:
我已阅读 git-config 文档并检查了 SO 问题 here 和 here,但我似乎找不到答案。
我正在为 Git 编写一个自定义工具,我想对其进行测试。我想备份我的全局和系统 Git 配置文件,然后在我的测试中写入新的配置文件,这样我就不会覆盖我已经拥有的内容(仅供参考,我正在测试多个不同的便携式(本地构建)版本我项目的子目录中的 Linux 和 Windows 版 Git)。
我最初的想法只是使用cp:
# Backup the system config file
# NOTE: How do I test this automatically? (I have to enter root password)...
sudo cp ${PREFIX}/etc/gitconfig ${PREFIX}/etc/gitconfig.bak
# Backup the global config file
cp ~/.gitconfig ~/.gitconfig.bak
# DO MY TESTS HERE
# Restore system config file and delete backup
sudo cp ${PREFIX}/etc/gitconfig.bak ${PREFIX}/etc/gitconfig
sudo rm ${PREFIX}/etc/gitconfig.bak
# Restore global config file and delete backup
cp ~/.gitconfig.bak ~/.gitconfig
rm ~/.gitconfig.bak
但是,我没有看到在 t/t1300-config.sh 中执行此操作(在撰写本文时,主存储库的当前版本是 v2.32)。我看到的最接近的事情是(第 2147-2158 行):
test_expect_success 'write to overridden global and system config' '
cat >expect <<EOF &&
[config]
key = value
EOF
GIT_CONFIG_GLOBAL=write-to-global git config --global config.key value &&
test_cmp expect write-to-global &&
GIT_CONFIG_SYSTEM=write-to-system git config --system config.key value &&
test_cmp expect write-to-system
'
我不明白这里发生了什么。 Git Docs 解释 GIT_CONFIG 是一个环境变量,可以设置为覆盖 git config 文件,但如果我在终端中输入上述命令,则不会创建文件 write-to-global 或 write-to-system(这些不是测试脚本中的函数)。
有人可以解释如何正确使用GIT_CONFIG 环境变量以及在测试脚本中执行此操作的正确方法是什么?
【问题讨论】: