git: empty arguments in post-receive hook

假如想象 提交于 2019-11-28 08:26:48

The post-receive hook doesn't take any arguments. Quoth the manual (emphasis added):

This hook is invoked by git-receive-pack on the remote repository, which happens when a git push is done on a local repository. It executes on the remote repository once after all the refs have been updated.

This hook executes once for the receive operation. It takes no arguments, but gets the same information as the pre-receive hook does on its standard input.

This hook does not affect the outcome of git-receive-pack, as it is called after the real work is done.

This supersedes the post-update hook in that it gets both old and new values of all the refs in addition to their names.

Both standard output and standard error output are forwarded to git send-pack on the other end, so you can simply echo messages for the user.

The default post-receive hook is empty, but there is a sample script post-receive-email provided in the contrib/hooks directory in git distribution, which implements sending commit emails.

I stumbled into this problem while setting up a continuous integration server. Since the arguments are not passed to post-receive via the command line, you have to use the read command. Here is how I did it:

#!/bin/sh
read oldrev newrev refname
BRANCH=${refname#refs/heads/} 
curl --request POST "http://my.ci.server/hooks/build/myproject_$BRANCH"

There are no arguments though the information is passed over STDIN. To read that information from bash simply do this:

read oldrev newrev refname
echo "Old revision: $oldrev"
echo "New revision: $newrev"
echo "Reference name: $refname"

I'm just summarizing the answers already posted.

aanno

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" <<END
wget -q -O - "$JENKINS_URL/git/notifyCommit?url=$GIT_URL&branches=$BRANCHES"
END

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

Actually, I don't accept the "it takes no arguments", because the sample script post-receive.sample is having the following comment:

# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated.  It is passed arguments in through
# stdin in the form
#  <oldrev> <newrev> <refname>
# For example:
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!