在获取裸存储库时执行钩子

时间:2015-12-08 15:59:11

标签: git

有一个简单的配置,其中一个裸中央存储库有一个更新'钩脚本。

现在它不再是中心了。它应该轮询中央存储库以进行更改。

如何使钩子脚本在这种情况下工作?

一个解决方案是创建一个中间存储库,它将从中央移植并将推送到旧中心。其他想法?

2 个答案:

答案 0 :(得分:0)

我知道你没有提到在你的问题中使用Jenkins作为CI服务器,但是这类事情的一个常见解决方案是使用Git plugin将Jenkins作业连接到集中式Git仓库。< / p>

当新的提交被推送到回购时,您可以触发脚本。

有关如何在推送时触发作业的详细信息,请参阅此帖子:How can I make Jenkins CI with git trigger on pushes to master?

答案 1 :(得分:0)

local git hook for “git remote update”问题中获得了一些有用的线索。

我们的想法是将git remote update更新此存储库,并将其包装到启动post-receive挂钩的脚本中。 remote-update.sh

#!/bin/bash

git_dir=$(git rev-parse --git-dir) || exit
cd "$git_dir"

declare -A before after all

# create the 'before' list
while read commit_hash refspec; do
    before[$refspec]="$commit_hash"
    all[$refspec]=''
done < <(git show-ref --heads)

# do the remote update
git remote update --prune

# create the 'after' list
while read commit_hash refspec; do
    after[$refspec]="$commit_hash"
    all[$refspec]=''
done < <(git show-ref --heads)

# see if there were any changes, and if so, run the post-receive hook
changes=0
for refspec in "${!all[@]}"; do
    [ "${before[$refspec]}" != "${after[$refspec]}" ] && { changes=1; break; }
done

if [ "$changes" == "1" ]; then
    none="$(printf "%0.s0" {1..40})" # forty zeroes, or git's "don't care" ref
    for refspec in "${!all[@]}"; do
        # if the refspec changed, pass it to the post-receive hook
        [ "${before[$refspec]}" != "${after[$refspec]}" ] && \
            echo "${before[$refspec]:-$none} ${after[$refspec]:-$none} $refspec"
    done | GIT_DIR="$git_dir" hooks/post-receive
fi

由于我只有post-receiveupdate个钩子,部分(因为它忽略了错误)update内部post-receive钩子的模拟可以像那样:

#!/bin/bash

while read oldrev newrev refname; do
    GIT_DIR="$GIT_DIR" hooks/update "$refname" "$oldrev" "$newrev"
done

当然,它可以正确完成并防止在失败时获取。

任何清洁解决方案?

相关问题