Git等同于/何时

问题描述 投票:2回答:1

[当我运行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 shell alias
1个回答
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; }

看起来像一个不错的开始。

© www.soinside.com 2019 - 2024. All rights reserved.