首页 > 系统 > Linux > 正文

多master/develop分支如何使用gitflow版本控制

2024-08-27 23:58:40
字体:大 中 小
来源:转载
供稿:网友

在使用 gitflow 做版本控制系统,发现gitflow的时候只能指定一个master/develop,如果要多分支使用要如何操作呢?那么来看看我是如何给gitflow加料的。

公司都是git作为版本控制,公司一些项目组在用gitflow,但是我们组没有强制,但是我上月出了一次事故,总结就是分支管理问题,所以开始强迫自己使用gitflow,以前的项目是一个master和一个develop,自己checkout一个分支,然后merge(不理解的可以看看a-successful-git-branching-model).

问题出现了:项目有几个主分支和开发分支,比如master_sina,master_qq. master_buzz ,而gitflow的时候只能指定一个master/develop, 这样你start一个feature/hotfix之前就要去.git/config里面修改 [gitflow “branch”]项的相关主分支和开发分支,so不方便,看了下源码,给gitflow加点料.

添加功能

当你打开了feature/hotfix分支,但是你不想要它了(当然你可以直接git branch -D xx),使用git flow hotfix/feature delete,自动帮你删除这个分支,以便你新建其他分支(git flow只容许你一次存在一个hotfix/feature分支).

你想使用gitflow删除其它存在分支嘛?不需要 git branch -D,你还可以git flow hotfix/feature delete XX.

比如我在init的时候指定了master为master_sina,而当我想创建master_qq的hotfix,我只需要在start的是否给它取名字是’qq_‘开头的即可,要是有其它的需要你可以直接在源码里面添加对应的内容.

例子 git-flow-hotfix 我主要标记我修改的部分,代码如下:

  1. init() { 
  2.   require_git_repo 
  3.   require_gitflow_initialized 
  4.   gitflow_load_settings 
  5.   VERSION_PREFIX=$(eval "echo `git config --get gitflow.prefix.versiontag`") 
  6.   PREFIX=$(git config --get gitflow.prefix.hotfix) 
  7. } 
  8. # 增加help的选项说明 
  9. usage() { 
  10.     echo "usage: git flow hotfix [list] [-v]" 
  11.     echo "       git flow hotfix start [-F] <version> [<base>]" 
  12.     echo "       git flow hotfix finish [-Fsumpk] <version>" 
  13.     echo "       git flow hotfix publish <version>" 
  14.     echo "       git flow hotfix delete [branch]" 
  15.     echo "       git flow hotfix track <version>" 
  16. } 
  17. cmd_default() { 
  18.     cmd_list "$@" 
  19. } 
  20.  
  21. cmd_list() { 
  22.     DEFINE_boolean verbose false 'verbose (more) output' v 
  23.     parse_args "$@" 
  24.  
  25.     local hotfix_branches 
  26.     local current_branch 
  27.     local short_names 
  28.     hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX") 
  29.     if [ -z "$hotfix_branches" ]; then 
  30.         warn "No hotfix branches exist." 
  31.                 warn "" 
  32.                 warn "You can start a new hotfix branch:" 
  33.                 warn "" 
  34.                 warn "    git flow hotfix start <version> [<base>]" 
  35.                 warn "" 
  36.         exit 0 
  37.     fi 
  38.     current_branch=$(git branch --no-color | grep '^* ' | grep -v 'no branch' | sed 's/^* //g') 
  39.     short_names=$(echo "$hotfix_branches" | sed "s ^$PREFIX  g") 
  40.  
  41.     # determine column width first 
  42.     local width=0 
  43.     local branch 
  44.     for branch in $short_names; do 
  45.         local len=${#branch} 
  46.         width=$(max $width $len) 
  47.     done 
  48.     width=$(($width+3)) 
  49.  
  50.     local branch 
  51.     for branch in $short_names; do 
  52.         local fullname=$PREFIX$branch 
  53.         local base=$(git merge-base "$fullname" "$MASTER_BRANCH") 
  54.         local master_sha=$(git rev-parse "$MASTER_BRANCH") 
  55.         local branch_sha=$(git rev-parse "$fullname") 
  56.         if [ "$fullname" = "$current_branch" ]; then 
  57.             printf "* " 
  58.         else 
  59.             printf "  " 
  60.         fi 
  61.         if flag verbose; then 
  62.             printf "%-${width}s" "$branch" 
  63.             if [ "$branch_sha" = "$master_sha" ]; then 
  64.                 printf "(no commits yet)" 
  65.             else  --Vevb.com 
  66.                 local tagname=$(git name-rev --tags --no-undefined --name-only "$base") 
  67.                 local nicename 
  68.                 if [ "$tagname" != "" ]; then 
  69.                     nicename=$tagname 
  70.                 else 
  71.                     nicename=$(git rev-parse --short "$base") 
  72.                 fi 
  73.                 printf "(based on $nicename)" 
  74.             fi 
  75.         else 
  76.             printf "%s" "$branch" 
  77.         fi 
  78.         echo 
  79.     done 
  80. } 
  81.  
  82. cmd_help() { 
  83.     usage 
  84.     exit 0 
  85. } 
  86.  
  87. parse_args() { 
  88.     # parse options 
  89.     FLAGS "$@" || exit $? 
  90.     eval set -- "${FLAGS_ARGV}" 
  91.     # read arguments into global variables 
  92.     VERSION=$1 
  93.     BRANCH=$PREFIX$VERSION 

这里就是我多master/develop的技巧,我这里会判断要新建的分支的前缀,要是qq_开头就会基于master_qq和develop_qq创建分支,所以你可以根据你的需要在这里加一些方法.

  1. test `expr match "$@" "qq_"` -ne 0 && MASTER_BRANCH="$MASTER_BRANCH"_qq && 
  2.     DEVELOP_BRANCH="$DEVELOP_BRANCH"_qq 
  3. } 
  4.  
  5. require_version_arg() { 
  6.     if [ "$VERSION" = "" ]; then 
  7.         warn "Missing argument <version>" 
  8.         usage 
  9.         exit 1 
  10.     fi 
  11. } 
  12.  
  13. require_base_is_on_master() { 
  14.     if ! git branch --no-color --contains "$BASE" 2>/dev/null  
  15.             | sed 's/[* ] //g'  
  16.             | grep -q "^$MASTER_BRANCH$"; then 
  17.         die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'." 
  18.     fi 
  19. } 
  20.  
  21. require_no_existing_hotfix_branches() { 
  22.     local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX") 
  23.     local first_branch=$(echo ${hotfix_branches} | head -n1) 
  24.     first_branch=${first_branch#$PREFIX} 
  25.     [ -z "$hotfix_branches" ] ||  
  26.         die "There is an existing hotfix branch ($first_branch). Finish that one first." 
  27. } 
  28. # 添加delete 参数,函数需要cmd_开头 
  29. cmd_delete() { 
  30.     if [ "$1" = "" ]; then 
  31.         # 当不指定参数自动去找存在的未关闭的gitflow分支 
  32.         local hotfix_branches=$(echo "$(git_local_branches)" | grep "^$PREFIX") 
  33.         test "$hotfix_branches" = "" && die "There has not existing hotfix branch can delete" && exit 1 
  34.     else 
  35.         # 指定参数先判断参数是不是的数量格式 
  36.         test $# != 1 && die "There only need one parameter indicates the branch to be deleted" && exit 1 
  37.         hotfix_branches="$1" 
  38.     fi 
  39.     # 当要删除的分支就是当前分支,先checkout到develop分支 
  40.     test "$hotfix_branches" = "$(git_current_branch)" && echo 'First checkout develp branch'; git_do checkout "$DEVELOP_BRANCH" 
  41.     git branch -D  ${hotfix_branches} > /dev/null 2>&1&& echo 'Delete Successed'|| die "Did not find branch: [$hotfix_branches]" 
  42.  
  43. } 
  44. cmd_start() { 
  45.     DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F 
  46.     parse_args "$@" 
  47.     BASE=${2:-$MASTER_BRANCH} 
  48.     require_version_arg 
  49.     require_base_is_on_master 
  50.     require_no_existing_hotfix_branches 
  51.  
  52.     # sanity checks 
  53.     require_clean_working_tree 
  54.     require_branch_absent "$BRANCH" 
  55.     require_tag_absent "$VERSION_PREFIX$VERSION" 
  56.     if flag fetch; then 
  57.         git_do fetch -q "$ORIGIN" "$MASTER_BRANCH" 
  58.     fi 
  59.     if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then 
  60.         require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH" 
  61.     fi 
  62.  
  63.     # create branch 
  64.     git_do checkout -b "$BRANCH" "$BASE" 
  65.  
  66.     echo 
  67.     echo "Summary of actions:" 
  68.     echo "- A new branch '$BRANCH' was created, based on '$BASE'" 
  69.     echo "- You are now on branch '$BRANCH'" 
  70.     echo 
  71.     echo "Follow-up actions:" 
  72.     echo "- Bump the version number now!" 
  73.     echo "- Start committing your hot fixes" 
  74.     echo "- When done, run:" 
  75.     echo 
  76.     echo "     git flow hotfix finish '$VERSION'" 
  77.     echo 
  78. } 
  79.  
  80. cmd_publish() { 
  81.     parse_args "$@" 
  82.     require_version_arg 
  83.  
  84.     # sanity checks 
  85.     require_clean_working_tree 
  86.     require_branch "$BRANCH" 
  87.     git_do fetch -q "$ORIGIN" 
  88.     require_branch_absent "$ORIGIN/$BRANCH" 
  89.  
  90.     # create remote branch 
  91.     git_do push "$ORIGIN" "$BRANCH:refs/heads/$BRANCH" 
  92.     git_do fetch -q "$ORIGIN" 
  93.  
  94.     # configure remote tracking 
  95.     git config "branch.$BRANCH.remote" "$ORIGIN" 
  96.     git config "branch.$BRANCH.merge" "refs/heads/$BRANCH" 
  97.     git_do checkout "$BRANCH" 
  98.  
  99.     echo 
  100.     echo "Summary of actions:" 
  101.     echo "- A new remote branch '$BRANCH' was created" 
  102.     echo "- The local branch '$BRANCH' was configured to track the remote branch" 
  103.     echo "- You are now on branch '$BRANCH'" 
  104.     echo 
  105. } 
  106.  
  107. cmd_track() { 
  108.     parse_args "$@" 
  109.     require_version_arg 
  110.  
  111.     # sanity checks 
  112.     require_clean_working_tree 
  113.     require_branch_absent "$BRANCH" 
  114.     git_do fetch -q "$ORIGIN" 
  115.     require_branch "$ORIGIN/$BRANCH" 
  116.  
  117.     # create tracking branch 
  118.     git_do checkout -b "$BRANCH" "$ORIGIN/$BRANCH" 
  119.  
  120.     echo 
  121.     echo "Summary of actions:" 
  122.     echo "- A new remote tracking branch '$BRANCH' was created" 
  123.     echo "- You are now on branch '$BRANCH'" 
  124.     echo 
  125. } 
  126.  
  127. cmd_finish() { 
  128.     DEFINE_boolean fetch false "fetch from $ORIGIN before performing finish" F 
  129.     DEFINE_boolean sign false "sign the release tag cryptographically" s 
  130.     DEFINE_string signingkey "" "use the given GPG-key for the digital signature (implies -s)" u 
  131.     DEFINE_string message "" "use the given tag message" m 
  132.     DEFINE_string messagefile "" "use the contents of the given file as tag message" f 
  133.     DEFINE_boolean push false "push to $ORIGIN after performing finish" p 
  134.     DEFINE_boolean keep false "keep branch after performing finish" k 
  135.     DEFINE_boolean notag false "don't tag this release" n 
  136.     parse_args "$@" 
  137.     require_version_arg 
  138.  
  139.     # handle flags that imply other flags 
  140.     if [ "$FLAGS_signingkey" != "" ]; then 
  141.         FLAGS_sign=$FLAGS_TRUE 
  142.     fi 
  143.  
  144.     # sanity checks 
  145.     require_branch "$BRANCH" 
  146.     require_clean_working_tree 
  147.     if flag fetch; then 
  148.         git_do fetch -q "$ORIGIN" "$MASTER_BRANCH" ||  
  149.           die "Could not fetch $MASTER_BRANCH from $ORIGIN." 
  150.         git_do fetch -q "$ORIGIN" "$DEVELOP_BRANCH" ||  
  151.           die "Could not fetch $DEVELOP_BRANCH from $ORIGIN." 
  152.     fi 
  153.     if has "$ORIGIN/$MASTER_BRANCH" $(git_remote_branches); then 
  154.         require_branches_equal "$MASTER_BRANCH" "$ORIGIN/$MASTER_BRANCH" 
  155.     fi 
  156.     if has "$ORIGIN/$DEVELOP_BRANCH" $(git_remote_branches); then 
  157.         require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH" 
  158.     fi 
  159.  
  160.     # try to merge into master 
  161.     # in case a previous attempt to finish this release branch has failed, 
  162.     # but the merge into master was successful, we skip it now 
  163.     if ! git_is_branch_merged_into "$BRANCH" "$MASTER_BRANCH"; then 
  164.         git_do checkout "$MASTER_BRANCH" ||  
  165.           die "Could not check out $MASTER_BRANCH." 
  166.         git_do merge --no-ff "$BRANCH" ||  
  167.           die "There were merge conflicts." 
  168.           # TODO: What do we do now? 
  169.     fi 
  170.  
  171.     if noflag notag; then 
  172.         # try to tag the release 
  173.         # in case a previous attempt to finish this release branch has failed, 
  174.         # but the tag was set successful, we skip it now 
  175.         local tagname=$VERSION_PREFIX$VERSION 
  176.         if ! git_tag_exists "$tagname"; then 
  177.             local opts="-a" 
  178.             flag sign && opts="$opts -s" 
  179.             [ "$FLAGS_signingkey" != "" ] && opts="$opts -u '$FLAGS_signingkey'" 
  180.             [ "$FLAGS_message" != "" ] && opts="$opts -m '$FLAGS_message'" 
  181.             [ "$FLAGS_messagefile" != "" ] && opts="$opts -F '$FLAGS_messagefile'" 
  182.             eval git_do tag $opts "$VERSION_PREFIX$VERSION" "$BRANCH" ||  
  183.             die "Tagging failed. Please run finish again to retry." 
  184.         fi 
  185.     fi 
  186.  
  187.     # try to merge into develop 
  188.     # in case a previous attempt to finish this release branch has failed, 
  189.     # but the merge into develop was successful, we skip it now 
  190.     if ! git_is_branch_merged_into "$BRANCH" "$DEVELOP_BRANCH"; then 
  191.         git_do checkout "$DEVELOP_BRANCH" ||  
  192.           die "Could not check out $DEVELOP_BRANCH." 
  193.  
  194.         # TODO: Actually, accounting for 'git describe' pays, so we should 
  195.         # ideally git merge --no-ff $tagname here, instead! 
  196.         git_do merge --no-ff "$BRANCH" ||  
  197.           die "There were merge conflicts." 
  198.           # TODO: What do we do now? 
  199.     fi 
  200.  
  201.     # delete branch 
  202.     if noflag keep; then 
  203.         # 这个问题很奇怪,在完成分支删除它也会存在当前分支是 
  204.         # 要删除的分支删除报错的问题,所以先切换走 
  205.         test "$BRANCH" = "$(git_current_branch)" && git_do checkout "$DEVELOP_BRANCH" 
  206.         git_do branch -d "$BRANCH" 
  207.     fi 
  208.  
  209.     if flag push; then 
  210.         git_do push "$ORIGIN" "$DEVELOP_BRANCH" ||  
  211.             die "Could not push to $DEVELOP_BRANCH from $ORIGIN." 
  212.         git_do push "$ORIGIN" "$MASTER_BRANCH" ||  
  213.             die "Could not push to $MASTER_BRANCH from $ORIGIN." 
  214.         if noflag notag; then 
  215.             git_do push --tags "$ORIGIN" ||  
  216.                 die "Could not push tags to $ORIGIN." 
  217.         fi 
  218.     fi 
  219.  
  220.     echo 
  221.     echo "Summary of actions:" 
  222.     echo "- Latest objects have been fetched from '$ORIGIN'" 
  223.     echo "- Hotfix branch has been merged into '$MASTER_BRANCH'" 
  224.     if noflag notag; then 
  225.         echo "- The hotfix was tagged '$VERSION_PREFIX$VERSION'" 
  226.     fi 
  227.     echo "- Hotfix branch has been back-merged into '$DEVELOP_BRANCH'" 
  228.     if flag keep; then 
  229.         echo "- Hotfix branch '$BRANCH' is still available" 
  230.     else 
  231.         echo "- Hotfix branch '$BRANCH' has been deleted" 
  232.     fi 
  233.     if flag push; then 
  234.         echo "- '$DEVELOP_BRANCH', '$MASTER_BRANCH' and tags have been pushed to '$ORIGIN'" 
  235.     fi 
  236.     echo 
  237.     }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表