TarranJones TarranJones - 25 days ago 6
Bash Question

Is extending a shell script by creating a sub-command file unique to git and can it be mimicked for other scripts?

I am aware that

git
allows you to place a executable file in the
bin
directory which when given the correct naming convention acts as a sub command.
bin/git-flow
,
/bin/git-fire


$ git flow


$ git fire


Is this functionality within the git executable or can any executable be overridden by just using this file naming convention.

I have tried this with
/bin/npm-custom
and
$ npm custom
but it executes the
/bin/npm
executable rather than
/bin/npm-custom
so i'm assuming by default this doesn't work.

If this is implemented through the original command script(
git
) then can this functionality be reproduced in another way, possible by reading the commands before executing a script.

.bash_profile

source ~/.check_for_subcommand_exacutables
;

Thanks in advance

Answer

To do this in general you have to override the precedence of the current git command, which should be easy unless you've already done it in some way. For example, you can create a function which falls back to the original command by default:

git() {
    case "$2" in
        "my_subcommand")
            …
            break
        *)
            command "$@"
    fi
}

In the case of Git specifically, you can create aliases for subcommands, such as st = status