什麼是git subcommand,如何建立git子命令?

來源:互聯網
上載者:User

標籤:

  大多數git使用者知道如何在git中建立一個alias以便更便利地使用相關命令。很少有人知道至少不會好好利用的是:你實際上可以為Git建立擴充或者plugin,以便上git完成任何你希望完成的工作。這就是Git subcommand!

  應該如何建立git子命令呢?

1.建立一個shell或者bash指令碼來完成你希望它做的工作;

2.將你的指令檔命名為git-name,這樣name就將成為git name中的子命令了!

3.將該指令碼放到/usr/local/bin或其他任何$PATH指定的路徑中;

4.運行git name命令調用上述指令碼

通過上述方法雖然可以建立多個指令檔來完成一個個小的功能擴充,但是一旦功能變多,則會顯得混亂。一個比較好的組織sub command所完成任務的方式是可以將一堆工作指令碼通過一個wrapper指令碼來整合:

1.建立一個wrapper或者access point

2.為你希望執行的每一個子命令建立一個file/script

3.使用你的wrapper來載入並且運行你的sub-command scripts;

#!/usr/bin/env shversion() {    echo "adamcodes git plugin v0.1.0"    echo}usage() {    echo "usage: git adamcodes <subcommand>"    echo    echo "Available subcommands are:"    echo "hello <name>  Print out Hello World or Hello <name> (if provided)"}main() {    if [ "$#" -lt 1 ]; then  //if the number of options is less than one        usage; exit 1    fi    local subcommand="$1"; shift    case $subcommand in        "-h"|"--help")            usage; exit 0            ;;        "-v"|"--version")            version; exit 0            ;;    esac    export WORKINGDIR=$(dirname "$(echo "$0" | sed -e ‘s,\\,/,g‘)")    if [ ! -e "$WORKINGDIR/git-adamcodes-$subcommand" ]; then        usage; exit 1    fi    source "$WORKINGDIR/git-adamcodes-$subcommand"    if [ ! type "cmd_$subcommand" ]; then        usage; exit 1    fi    cmd_$subcommand "[email protected]"}

上面內容可以參考: https://adamcod.es/2013/07/12/how-to-create-git-plugin.html

https://adamcod.es/2013/07/19/how-to-create-git-plugin-part2.html

git 子命令在release管理中的應用案例:

在網站的release管理中,可能現在實用的是這樣一個流程:

git checkout mastergit merge target_branchgit push origin mastergit push origin :target_branch

或許你希望通過子命令簡化一下上述流程為:

git checkout mastergit validate target_branch

這個可以通過建立一個git-validate的指令檔來實現:

#!/bin/shbranch=$1test -z $branch && echo "branch required." 1>&2 && exit 1git checkout mastergit merge $branchgit push origin mastergit push origin :$branch

注意:為了實現一個git 子命令,我們並不一定被局限於使用shell指令碼來編寫代碼哦,你可以使用其他比如Ruby,python等更強大的語言來實現@!

 

http://www.davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/

 

什麼是git subcommand,如何建立git子命令?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.