3

I feel like this should be straighforward to some degree...

Basically, for some command, I want to be able to invoke another command that will scope all params to that command. so...

git commit -am "test"

would become...

./shell_comand git
>commit -am "test"
>status

so, obviously things like autocomplete and whatnot wouldn't work, but I would expect otherwise this should be doable to some degree? I'm just not quite clear on how...

1

I assume this should be doable in git itself, but since I'm no git expert I would use at shell functions. In a sh-compatible one (e.g. bash) you can easily write a function called git which will be invoked instead of the git binary (be careful to use full path to the binary inside of the function - or command git in BASH - to prevent unwanted recursion):

function git () {
    case "$1" in
    "commit")
        # do your stuff
        /path/to/git ...
        ;;
     *)
        # call git binary with the same arguments
        /path/to/git "$@"
        ;;
}

You could use just command aliasing (alias), but functions give you way more flexibility.

As for the completion, check your shell completion functionality (for bash look for complete in the man page) - it's certainly possible to make it work and it doesn't even have to be too complicated.

  • 2
    Instead of /path/to/git you could write command git – glenn jackman Jan 1 '13 at 20:55
0

One way would be to define aliases for every git command. You could still run ordinary shell commands; if this is not desired, set PATH to a nonexistent directory. If you want to run the shell command and not the git command, put a backslash before the command (e.g. \rm foo to remove a file).

git_commands=$(COLUMNS=2 git --help --all | sed -n -e '1,/^----/d' -e '1,/^$/p')
for c in $git_commands; do
  alias "$c=git $c"
done

In zsh, you'll even have completion if you have the complete_aliases option set. I don't know about bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.