Reputation: 65795
Basically I want to type show
and it checks if there is a show
command or alias is defined and fire it and it is not defined fires git show
.
For example rm
should do rm
but checkout
should do git checkout
.
Is it possible to program this in bashrc
?
Upvotes: 2
Views: 1081
Reputation: 531355
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; }
}
Upvotes: 2
Reputation: 20799
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
}
Upvotes: 0
Reputation: 185254
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+(?=\()'
Upvotes: 2
Reputation: 171323
This is surprisingly easy:
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
...
This runs the usual touch
and rm
commands, but because there is no add
command it runs git add foo
and because there is no branch
command it runs 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.
Upvotes: 4
Reputation: 8303
Add to your ~/.bashrc
:
alias show='git show'
alias checkout='git checkout'
...
Be careful not to make an alias for a command that already does something else. This may break others programs.
Upvotes: 0