如何在gitlab服务器端预接收挂钩中获取提交消息

问题描述 投票:0回答:1

我正在尝试在gitlab上开发服务器端预接收挂钩。我应该从新添加的提交中获取提交消息。

我尝试使用git log --pretty=%B -n 1。这将返回旧的已提交消息。如何从新的未接受的更改中获取提交消息?

当我试图在脚本中获取refname或参数时,它没有保存任何值。 (想想可能有帮助)

#!/bin/bash
ref_name=$refname
echo $ref_name
ref_name=$1
echo $ref_name
echo "refname"
issue=`git log --pretty=%B -n 1`
echo $issue #this is printing old commit message

结果:

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 306 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote:
remote:
remote:
remote: refname
git gitlab githooks
1个回答
1
投票

预接收挂钩在标准输入上获取引用列表及其旧版本和新版本。所以你可以这样做:

#!/bin/sh

while read old new ref
do
    # ref deleted; skip
    echo "$new" | grep -qsE '^0+$' && continue

    issue=$(git log --pretty=%B -n 1 "$new")
    echo "issue is $issue"
done

请注意,这假设您只关心最新引用的头部提交,并且您也可以为标记执行此操作。如果您只想要分支,并且想要遍历所有提交,那么您可以执行以下操作:

#!/bin/sh

while read old new ref
do
    case $ref in
        refs/heads/*)
            if echo "$new" | grep -qsE '^0+$'
            then
                # ref deleted; skip
                :
            elif echo "$old" | grep -qsE '^0+$'
            then
                # new branch
                # do something with this output
                git log --pretty=%B "$new"
            else
                # update
                # do something with this output
                git log --pretty=%B "$old".."$new"
            fi;;
        *)
            continue;;
    esac
done
© www.soinside.com 2019 - 2024. All rights reserved.