[当我运行git help -a
时,它会向我显示内部命令,所有别名和所有外部git命令(即,路径中以git-
开头的任何可执行文件)的列表。我想要的是一个别名或脚本,可以以git which
的身份运行,它将告诉我以下情况之一:
git which notacommand
)git which checkout
)git which pwd
将显示/usr/local/bin/git-pwd
)git which wtf
将显示alias.wtf blame -w
)我可以很容易地编写脚本来使用git help -a
的输出并生成此脚本,但是我缺少一些已经提供了部分或全部此功能的git命令吗?
感谢@jthill的评论和回答,我想出了以下git-which
脚本:
#!/bin/sh
if test $# -ne 1
then
echo "Usage: $0 <git command>" >&2
exit 1
fi
CMD=git-"$1"
if PATH="$(git --exec-path)" command -v "$CMD" >/dev/null
then
echo "$1: git build-in command"
exit 0
elif command -v "$CMD"
then
exit 0
elif git config --get-regexp '^alias\.'"$1"'$' |\
cut -d. -f2- | sed -e 's/ /: aliased to /'
then
exit 0
fi
echo "$1 not found"
exit 1
git help
将为您显示别名,例如git help wtf
,它将显示为'wtf' is aliased to 'blame -w'
。对于其余部分,搜寻libexec / git-core并不困难,例如git --exec-path
,并且which
已经搜寻了命令,因此
PATH=$PATH:$(git --exec-path) which git-checkout
将为您提供帮助,别名不能覆盖内置函数,因此(手指到文本框警告:)
f() { PATH=${PATH+$PATH:}$(git --exec-path) which git-$1 2>&- || git help $1; }
看起来像一个不错的开始。