基本上我想输入show
并检查是否show
定义了命令或别名并触发它并且未定义触发git show
。
例如rm
应该做rm
但checkout
应该做git checkout
。
是否可以在其中编程bashrc
?
基本上我想输入show
并检查是否show
定义了命令或别名并触发它并且未定义触发git show
。
例如rm
应该做rm
但checkout
应该做git checkout
。
是否可以在其中编程bashrc
?
这非常容易:
master tmp$ trap 'git $BASH_COMMAND' ERR
master tmp$ touch foo
master tmp$ rm foo
master tmp$ add foo
bash: add: command not found
fatal: pathspec 'tmp/foo' did not match any files
master tmp$ branch
bash: branch: command not found
aix
allocators
...
这运行通常的touch
和rm
命令,但是因为没有add
命令它运行git add foo
并且因为没有branch
命令它运行git branch
The trap command is run on any error, so not only when a command isn't found. You would probably want to do something smarter e.g. run a script which checks whether $?
is 127 (the code bash sets when a command is not found) and then checks if running it with git instead would work (e.g. by checking for a command called git-xxx
where xxx
is the first word of $BASH_COMMAND
). I leave that as an exercise for the reader.
There's no simple and proper way to achieve what you need. I think the best to do is to make an alias in ~/.bashrc
for every git
commands.
But on many distros, if you check man git
, there's some Main porcelain commands that looks like aliases.
You can list all of them using
PAGER= man git | grep -oP 'git-\w+(?=\()'
When bash
cannot find a command, it calls command_not_found_handle
(if defined). You can define it to look something like this:
command_not_found_handle () {
git "$@" || { echo "$1 not a 'git' command either"; exit 127; }
}
添加到您的~/.bashrc
:
alias show='git show'
alias checkout='git checkout'
...
注意不要为已经执行其他操作的命令创建别名。这可能会破坏其他程序。
Here's one way of getting a list of all your git commands:
git help -a | egrep '^ [a-zA-Z0-9]' | xargs -n1 | sed 's/--.*//' | sort -u
Or you could use what's in contrib/completion/git-completion.sh
: (This is probably better since you'll probably be looping anyway. Note: You'll need to consider for duplicates, but they don't really matter for alias
)
__git_list_all_commands ()
{
local i IFS=" "$'\n'
for i in $(git help -a|egrep '^ [a-zA-Z0-9]')
do
case $i in
*--*) : helper pattern;;
*) echo $i;;
esac
done
}