Extending Git
As a follow-up to my cherry pick UI post, here’s a quick tutorial how you
can extend Git with new sub-commands. If you used the cherry pick tool, you
noticed that it is not very well integrated and you needed to remember the exact
name of the script. I suggested to add an alias to your .gitconfig but there
is more “natural” way to do it.
Luckily1, Git uses any program prefixed with git- and located in $PATH as
a sub-command. That means, moving the pick-from to /usr/local/bin/git-pick,
allows us to use it with
$ git pick <feature-branch>
Calling git pick --help spawns man for a git-pick.7 manpage. Hence, you
could for example generate one from Markdown with Pandoc and copy that into your
local man search directory ~/.local/share/man/man7.
Git’s sub-command completion is implemented in a similar pragmatic way: simply
provide a bash function _git_command_name() that completes to whatever you
like. Git already provides helper functions for typical situations, which is why
we can complete branches for git pick simply with
function _git_pick () {
__gitcomp_nl "$(__git_refs)"
}
… and probably a reminiscence of Git’s past as a huge collection of porcelain Perl scripts around the Git core.