这是正常的吗?
“正常”是一个有趣的词。很难定义这是否正常。有一个遥控器但没有命名有点不寻常origin
,但这不是错误,只是奇怪。
如果有 2 个或更多遥控器会怎样?
随你(由你决定。 git remote -v
将列出所有这些,因此当然可以以这种方式提取它们。您还可以使用:
git config --get-regexp 'remote\..*'
列出所有这些。您甚至可能想要添加--local
以确保仅.git/config
列出本地文件中定义的遥控器,以防某些疯狂的用户在她的全局配置中定义了遥控器。(我假设您不想尝试更新任何此类条目。)
还要注意,每个远程都可以定义 URL 和推送 URL。这些配置名称不区分大小写,因此您应该在“手动”查找任何和设置时折叠大小写(如果您编写自己的代码来读取文件),但在使用或作为那些为你做折叠的事情。remote.remote.url
remote.remote.pushurl
config
git config --get-regexp
git remote -v
因此,用于读取git config --get-regexp
输出的 sh 或 bash 脚本可能会使用:
# fix - called with URLs that may need fixing. Argument $1 is the
# type (url or pushurl), argument $2 is the remote, $3 is the URL itself.
fix() {
local urltype=$1 remote="$2" url="$3"
local newurl
case $3 in
...) newurl="echo $url | sed ...";;
*) return;;
esac
git config "remote.$remote.$urltype" "$newurl"
}
git config --local --get-regexp 'remote\..*' | sort | while read -r lhs rhs; do
set -- $(echo $lhs | sed 's/\./ /g')
case $3 in
url|pushurl) fix $3 "$2" "$rhs";;
esac
done
(在 bash 中可能有点花哨;以上在 POSIX sh 中有效——尽管整个事情还没有经过测试。当然你也需要填写这些...
部分。)
通常,远程名称和 URL 中不应包含空格。我使用上面的引号来处理可能的情况。(该git remote
命令不允许您添加名称中带有空格的遥控器,但允许在 URL 中使用空格。)“排序”的明显无用用途是处理极端情况:如果.git/config
文件非常大,我们可以尝试运行git config
以更改配置,同时git config --get-regexp
仍在读取和打印旧配置。在实践中,它可能不需要。