git: empty arguments in post-receive hook

前端 未结 5 1870
有刺的猬
有刺的猬 2020-12-09 18:08

I\'m writing post-receive hook basing on the post-receive-email script from the contrib dir, but it seems that the oldrev and ne

5条回答
  •  萌比男神i
    2020-12-09 18:30

    A more elaborated version of François script would be

    #!/bin/bash
    
    JENKINS_URL="http://192.168.1.116/jenkins"
    GIT_URL="git@bitbucket.org:nuclos/nuclos.git"
    
    # remove all spaces and newlines from ARG
    trim() {
      local ARG="$1"
      shift
      echo -e "$ARG" | tr -d "[:space:]\n" 
    }
    
    # unique sort ARG items separated by newlines
    unique() {
      local ARG="$1"
      shift
      echo -e "$ARG" | sort -u -i
    }
    
    # cut first and last character from ARG
    cutfl() {
      local ARG="$1"
      shift
      local LEN="${#ARG}"
      let LEN="$LEN - 2"
      echo "${ARG:1:$LEN}"
    }
    BRANCHES=""
    while read oldrev newrev refname; do
      BRANCH=`trim ${refname#refs/heads/}`
      if [ -n "$BRANCH" ]; then
        BRANCHES+="${BRANCH}\n"
      fi
    done
    
    BRANCHES=`unique "$BRANCHES" | tr '\n' ','`
    BRANCHES=`cutfl "$BRANCHES"`
    echo wget -q -O - "$JENKINS_URL/git/notifyCommit?url=$GIT_URL&branches=$BRANCHES"
    at "now + 5 minutes" <

    This version could cope with more than one branch and only triggers one build for each one.

提交回复
热议问题