如何在别名 git 命令时防止 "fatal" 错误输出
How to prevent a "fatal" error output while aliasing a git command
如何防止输出致命错误消息?我打算在没有 STDOUT 或 STDERR 的情况下为一些 git 命令设置别名。附加 2>&1 >/dev/null
后,我在为命令设置别名时继续收到致命错误消息。
一些命令的别名是 z-shell 的 (zsh) git 插件。作为我的 .zshrc 文件的一部分,我获取了一个 aliases.zsh 文件,我在其中取消别名,然后使用新命令重新应用别名。
From aliases.zsh
# -- Git related --------------------------------------------------
unalias grmci 2>&1 >/dev/null
alias grmci 2>&1 >/dev/null || alias grmci="git rm --cached `git ls-files -i --exclude-from=.gitignore`" 2>&1 >/dev/null
如有疑问,请改用函数。假设你有 GNU 而不是 BSD xargs
:
# this runs not just on zsh, but also on ksh/bash/etc with GNU xargs.
# (would be POSIX-compliant but for $'\n' syntax for literal newlines)
grmci() {
{
git ls-files -i --exclude-from=.gitignore |
xargs --no-run-if-empty -d $'\n' \
git rm --cached
} >/dev/null 2>&1
}
...或者,依靠 shell 来完成更多的工作而不依赖于 xargs:
# this is zsh-only code; 1-indexed arrays and `(@f)...` parameterization are zsh-isms
grmci() {
{
array_of_lines=("${(@f)$(git ls-files -i --exclude-from=.gitignore)}")
{ (( ${#array_of_lines[@]} )) && [[ ${array_of_lines[1]} != '' ]]; } ||
return 0
git rm --cached -- "${array_of_lines[@]}"
} >/dev/null 2>&1
}
请注意,在这两种情况下,我们都不会直接使用 $(git ls-files ...)
作为 git rm --cached
的参数;当文件名称中包含空格、glob 字符等时,这样做会出现错误。 (我们现在的代码不允许文件名带有文字换行符,但 git
也不允许)。
使用@CharlesDuffy 共享的信息的 BSD 兼容解决方案。
以下函数检查别名是否存在,如果存在则将其删除。然后可以使用grmci
函数来删除被忽略的文件。
removeAlias () {
CMDTYPE=$(type "")
case $CMDTYPE in
*"alias"*)
unalias 2> >(grep -Ev 'no such hash table element' >&2)
;;
*"function"*)
echo -e "\u001b[34mError: alias is a function\u001b[0m"
;;
*"not found"*)
;;
*)
echo -e "Error: unknown command type for return message is '$CMDTYPE'"
;;
esac
}
# -- Git related-----------------------------
removeAlias grmci
grmci () {
git ls-files --others |
xargs -n1 git rm --cached
}
如何防止输出致命错误消息?我打算在没有 STDOUT 或 STDERR 的情况下为一些 git 命令设置别名。附加 2>&1 >/dev/null
后,我在为命令设置别名时继续收到致命错误消息。
一些命令的别名是 z-shell 的 (zsh) git 插件。作为我的 .zshrc 文件的一部分,我获取了一个 aliases.zsh 文件,我在其中取消别名,然后使用新命令重新应用别名。
From aliases.zsh
# -- Git related --------------------------------------------------
unalias grmci 2>&1 >/dev/null
alias grmci 2>&1 >/dev/null || alias grmci="git rm --cached `git ls-files -i --exclude-from=.gitignore`" 2>&1 >/dev/null
如有疑问,请改用函数。假设你有 GNU 而不是 BSD xargs
:
# this runs not just on zsh, but also on ksh/bash/etc with GNU xargs.
# (would be POSIX-compliant but for $'\n' syntax for literal newlines)
grmci() {
{
git ls-files -i --exclude-from=.gitignore |
xargs --no-run-if-empty -d $'\n' \
git rm --cached
} >/dev/null 2>&1
}
...或者,依靠 shell 来完成更多的工作而不依赖于 xargs:
# this is zsh-only code; 1-indexed arrays and `(@f)...` parameterization are zsh-isms
grmci() {
{
array_of_lines=("${(@f)$(git ls-files -i --exclude-from=.gitignore)}")
{ (( ${#array_of_lines[@]} )) && [[ ${array_of_lines[1]} != '' ]]; } ||
return 0
git rm --cached -- "${array_of_lines[@]}"
} >/dev/null 2>&1
}
请注意,在这两种情况下,我们都不会直接使用 $(git ls-files ...)
作为 git rm --cached
的参数;当文件名称中包含空格、glob 字符等时,这样做会出现错误。 (我们现在的代码不允许文件名带有文字换行符,但 git
也不允许)。
使用@CharlesDuffy 共享的信息的 BSD 兼容解决方案。
以下函数检查别名是否存在,如果存在则将其删除。然后可以使用grmci
函数来删除被忽略的文件。
removeAlias () {
CMDTYPE=$(type "")
case $CMDTYPE in
*"alias"*)
unalias 2> >(grep -Ev 'no such hash table element' >&2)
;;
*"function"*)
echo -e "\u001b[34mError: alias is a function\u001b[0m"
;;
*"not found"*)
;;
*)
echo -e "Error: unknown command type for return message is '$CMDTYPE'"
;;
esac
}
# -- Git related-----------------------------
removeAlias grmci
grmci () {
git ls-files --others |
xargs -n1 git rm --cached
}