Not able to detect branch from Git post-receive hook

前端 未结 5 1510
面向向阳花
面向向阳花 2021-01-31 11:37

I\'ve got a post receive hook setup on the remote repo that tries to determine the branch name of the incoming push as follows:

$branch = `git rev-parse --abbrev         


        
相关标签:
5条回答
  • 2021-01-31 12:05

    You need to read the arguments that are being passed to the script. That should have the branch name and new and old revisions and run for each branch pushed

    0 讨论(0)
  • 2021-01-31 12:08

    Both these answers are correct, but I was having trouble getting stdin to the next common function post-receive-email. Here is what I ended up with:

    read oldrev newrev ref
    echo "$oldrev" "$newrev" "$ref" | . /usr/share/git-core/contrib/hooks/post-receive-email
    
    
    if [ "refs/heads/qa" == "$ref" ]; then
      # Big Tuna YO!
      wget -q -O - --connect-timeout=2 http://127.0.0.1:3000/hooks/build/qa_now
    fi
    
    0 讨论(0)
  • 2021-01-31 12:16

    The post-receive hook gets the same data as the pre-receive and not as arguments, but from stdin. The following is sent for all refs:

    oldRev (space) newRev (space) refName (Line feed)

    You could parse out the ref name with this bash script:

    while read oldrev newrev ref
    do
        echo "$ref"
    done
    
    0 讨论(0)
  • 2021-01-31 12:23

    Magnus' solution didnt work for me, but this did:

    #!/bin/bash
    
    echo "determining branch"
    
    if ! [ -t 0 ]; then
      read -a ref
    fi
    
    IFS='/' read -ra REF <<< "${ref[2]}"
    branch="${REF[2]}"
    
    if [ "master" == "$branch" ]; then
      echo 'master was pushed'
    fi
    
    if [ "staging" == "$branch" ]; then
      echo 'staging was pushed'
    fi
    
    echo "done"
    
    0 讨论(0)
  • 2021-01-31 12:24

    You could also do something like this using bash variable substitution:

    read oldrev newrev ref
    
    branchname=${ref#refs/heads/}
    
    git checkout ${branchname}
    
    0 讨论(0)
提交回复
热议问题