pre-receive hook on server-side that refuse any push to master which has any non-linear history

前端 未结 3 1661
一向
一向 2020-12-06 08:31

I am looking for a shell script (sh, not bash possibly) that would refuse any push to master which has any non-linear history.

I t

3条回答
  •  情书的邮戳
    2020-12-06 08:42

    After much struggle on the sourceforge server here is what I got:

    #!/bin/sh
    # Author: Mathieu Malaterre
    # This code is free software. It can be used and distributed under the
    # same terms as git itself.
    # http://stackoverflow.com/questions/5482887/how-to-avoid-merge-branch-master-of-ssh-gdcm-git-sourceforge-net-gitroot-gdc
    read rev_old rev_new refname
    
    ref_to_check="refs/heads/master"
    
    if [ "$refname" = "$ref_to_check" ]
    then
      merge_bases=`git merge-base ${rev_old} ${rev_new}`
      if [ "$merge_bases" != "$rev_old" ]
      then
        echo "Non fastward is disallowed"
        exit 1
      fi
      # non-fast-forward case:
      git rev-list --parents $merge_bases..$rev_new \
        | while read x; do
          set -- $x
          if [ "$#" != "2" ]
          then
            echo "Multiple parents: $x";
            exit 1
          fi;
        done
      [ $? -ne 0 ] && exit 1
    fi
    exit 0
    

提交回复
热议问题