Git subcommands are standalone executables that live in the Git exec path, normally /usr/lib/git-core. The git executable itself is a thin wrapper that knows where the subcommands live, and runs them by passing command-line arguments to them.

(If “git foo” is not found in the Git exec path, the wrapper will look in the rest of your $PATH for it. Thus, it’s possible to write local Git extensions that don’t live in system space.)

可以编写以git-cmdname命名的C、shell、Perl程序(比如git-foo.sh)放在Git的执行路径或$Path中,当使用git cmdname时,Git会查找路径执行相应程序。

也可以通过修改Makefile中的BUILTIN_OBJS,EXTRA_PROGRAMS, SCRIPT_SH, SCRIPT_PERL or SCRIPT_PYTHON选项等操作,将自己的git子命令集成到Git tree中

详见:

new-command.txt

CodingGuidelines

Implementing a new “git …” subcommand as a Python script

Write Your Own Git Subcommands

Extending git

模仿Write Your Own Git Subcommands的一个例子:

$ mkdir ~/subcmd; 
$ touch ~/subcmd/git-custom
$ chmod u+x ~/subcmd/git-custom
$ export PATH="$HOME/subcmd/git-custom:$PATH"

$ git custom.sh
#!/bin/sh

. git-sh-setup
. git-parse-remote
require_work_tree
wt_prefix=$(git rev-parse --show-prefix)
cd_to_toplevel

git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} status

**具体可以参考Git自带的git-subcmd.sh脚本程序

一个可以借鉴的Github项目:tj/git-extras

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐