编写 git post-receive hook 来处理特定分支

发布于 2024-12-03 21:16:24 字数 176 浏览 1 评论 0原文

这是我当前在公司服务器中的裸存储库中的钩子: git push origin master 这个钩子会推送到 Assembla。 我需要的是,当有人将更改推送到我们服务器上的该分支时,仅推送一个分支(理想情况下是主分支),并忽略对其他分支的推送。是否可以从裸存储库中选择分支并仅将该分支推送到 Assembla?

Here's my current hook in a bare repo that lives in the company's server:
git push origin master
This hooks pushes to Assembla.
What i need is to push only one branch (master, ideally) when someone pushes changes to that branch on our server, and ignore pushes to other branches. Is it possible to select the branch from a bare repo and push only that branch to Assembla?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最美的太阳 2024-12-10 21:16:24

post-receive 钩子从 stdin 获取参数,格式为:

<oldrev> <newrev> <refname>

由于这些参数来自 stdin,而不是命令行参数,因此您需要使用 read 而不是 $1 $2 $3

post-receive 钩子可以一次接收多个分支(例如,如果有人执行 git push --all ),因此我们还需要将 read 包装在 <代码> while循环。

一个工作片段看起来像这样:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" = "$branch" ]; then
        # Do something
    fi
done

A post-receive hook gets its arguments from stdin, in the form:

<oldrev> <newrev> <refname>

Since these arguments are coming from stdin, not from a command line argument, you need to use read instead of $1 $2 $3.

The post-receive hook can receive multiple branches at once (for example if someone does a git push --all), so we also need to wrap the read in a while loop.

A working snippet looks something like this:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" = "$branch" ]; then
        # Do something
    fi
done
债姬 2024-12-10 21:16:24

post-receive hook 在 stdin 上获取的最后一个参数是 ref 被更改的内容,因此我们可以使用它来检查该值是否为“refs/heads/master”。有点类似于我在接收后挂钩中使用的 ruby​​:

STDIN.each do |line|
    (old_rev, new_rev, ref_name) = line.split
    if ref_name =~ /master/
         # do your push
    end
end

请注意,它会为每个推送的 ref 获取一行,因此如果您推送的不仅仅是 master,它仍然可以工作。

The last parameter that a post-receive hook gets on stdin is what ref was changed, so we can use that to check if that value was "refs/heads/master." A bit of ruby similar to what I use in a post-receive hook:

STDIN.each do |line|
    (old_rev, new_rev, ref_name) = line.split
    if ref_name =~ /master/
         # do your push
    end
end

Note that it gets a line for each ref that was pushed, so if you pushed more than just master, it will still work.

太阳哥哥 2024-12-10 21:16:24

Stefan的答案对我不起作用,但是 这个 做到了:

#!/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"

Stefan's answer didn't 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"
慵挽 2024-12-10 21:16:24

上述解决方案都不适合我。经过多次调试后,事实证明使用“read”命令不起作用——相反,以通常的方式解析命令行参数可以正常工作。

这是我刚刚在 CentOS 6.3 上成功测试的确切更新后挂钩。

#!/bin/bash

echo "determining branch"

branch=`echo $1 | cut -d/ -f3`

if [ "master" == "$branch" ]; then
    echo "master branch selected"
fi

if [ "staging" == "$branch" ]; then
    echo "staging branch selected"
fi

exec git update-server-info

更新:更奇怪的是,预接收钩子通过标准输入获取输入,因此用“read”读取(哇,从来没想过我会这么说)。对于我来说,更新后的钩子仍然可以使用 1 美元。

Neither of the solutions above worked for me. After much, much debugging, it turns out that using the 'read' command doesn't work -- instead, parsing command line arguments the usual way works fine.

Here is the exact post-update hook that I just successfully tested now on CentOS 6.3.

#!/bin/bash

echo "determining branch"

branch=`echo $1 | cut -d/ -f3`

if [ "master" == "$branch" ]; then
    echo "master branch selected"
fi

if [ "staging" == "$branch" ]; then
    echo "staging branch selected"
fi

exec git update-server-info

UPDATE: on an even stranger note, the pre-receive hook takes its input via stdin, therefore read with 'read' (wow, never thought I'd say that). The post-update hook still works with $1 for me.

对岸观火 2024-12-10 21:16:24

@pauljz 的答案对于某些 git 挂钩(例如 pre-push)效果很好,但 pre-commit 无法访问这些变量 oldrev newrev refname

所以我创建了这个替代版本,它适用于预提交,或者真正的钩子。这是一个预提交挂钩,如果我们不在 master 分支上,它将运行 husky 脚本。

#!/bin/bash
# git 'commit' does not have access to these variables: oldrev newrev refname
# So get the branch name off the head

branchPath=$(git symbolic-ref -q HEAD) # Something like refs/heads/myBranchName
branch=${branchPath##*/}      # Get text behind the last / of the branch path

echo "Head: $branchPath";
echo "Current Branch: $branch";

if [ "master" != "$branch" ]; then

   # If we're NOT on the Master branch, then Do something
   # Original Pre-push script from husky 0.14.3

   command_exists () {
     command -v "$1" >/dev/null 2>&1
   }

   has_hook_script () {
     [ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"
   }

   cd "frontend" # change to your project directory, if .git is a level higher

   # Check if precommit script is defined, skip if not
   has_hook_script precommit || exit 0

   # Node standard installation
   export PATH="$PATH:/c/Program Files/nodejs"

   # Check that npm exists
   command_exists npm || {
     echo >&2 "husky > can't find npm in PATH, skipping precommit script in package.json"
     exit 0
   }

   # Export Git hook params
   export GIT_PARAMS="$*"

   # Run npm script
   echo "husky > npm run -s precommit (node `node -v`)"
   echo

   npm run -s precommit || {
     echo
     echo "husky > pre-commit hook failed (add --no-verify to bypass)"
     exit 1
   }
fi

我希望这对某人有帮助。您可以根据需要轻松修改 iffi 语句之间的任何内容。

The answer from @pauljz works fine for certain git hooks like pre-push, but pre-commit does not have access to those variables oldrev newrev refname

So I created this alternate version which works for pre-commit, or really and hook. This is a pre-commit hook that will run a husky script if we're NOT on the master branch.

#!/bin/bash
# git 'commit' does not have access to these variables: oldrev newrev refname
# So get the branch name off the head

branchPath=$(git symbolic-ref -q HEAD) # Something like refs/heads/myBranchName
branch=${branchPath##*/}      # Get text behind the last / of the branch path

echo "Head: $branchPath";
echo "Current Branch: $branch";

if [ "master" != "$branch" ]; then

   # If we're NOT on the Master branch, then Do something
   # Original Pre-push script from husky 0.14.3

   command_exists () {
     command -v "$1" >/dev/null 2>&1
   }

   has_hook_script () {
     [ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"
   }

   cd "frontend" # change to your project directory, if .git is a level higher

   # Check if precommit script is defined, skip if not
   has_hook_script precommit || exit 0

   # Node standard installation
   export PATH="$PATH:/c/Program Files/nodejs"

   # Check that npm exists
   command_exists npm || {
     echo >&2 "husky > can't find npm in PATH, skipping precommit script in package.json"
     exit 0
   }

   # Export Git hook params
   export GIT_PARAMS="$*"

   # Run npm script
   echo "husky > npm run -s precommit (node `node -v`)"
   echo

   npm run -s precommit || {
     echo
     echo "husky > pre-commit hook failed (add --no-verify to bypass)"
     exit 1
   }
fi

I hope that helps someone. You can easily modify for your needs, anything in between the if and fi statements.

情绪 2024-12-10 21:16:24

我为自己编写了一个 PHP 脚本来执行此功能。

https://github.com/fotuzlab/githubdump-php

最好将此文件托管在您的服务器上repo root 并在 github webhooks 中定义 url。将第 8 行的“allcommits”更改为您的分支名称,并在第 18 行添加您的代码/函数。

例如

function githubdump($payload_object) {
    // Write your code here.
    exec('git push origin master');
}

I had written a PHP script for myself to do this functionality.

https://github.com/fotuzlab/githubdump-php

Host this file on your server, preferably repo root and define the url in github webhooks. Change 'allcommits' on line 8 with your branch name and add your code/function at line 18.

e.g.

function githubdump($payload_object) {
    // Write your code here.
    exec('git push origin master');
}
ι不睡觉的鱼゛ 2024-12-10 21:16:24

简单的方法,在 git hook 中编写

read refname
echo $refname

Simple - 有关此链接的更多信息 挂钩系统

Simple approach, in git hook write

read refname
echo $refname

Simple - more info on this great link hooking system

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文