简体   繁体   English

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

[英]How to get commit message in gitlab server side pre-receive hooks

I am trying to develop a server side pre-receive hook on gitlab. 我正在尝试在gitlab上开发服务器端预接收钩子。 where I should get the commit message from the new commits which are getting added. 我应该从正在添加的新提交中获取提交消息。

I tried using the git log --pretty=%B -n 1 . 我尝试使用git log --pretty=%B -n 1 This is returning the old committed message. 这将返回旧的已提交消息。 How can I get the commit message form the new unaccepted changes ? 如何从新的未接受的更改中获取提交消息?

When I tried to get the refname or argument in to the script it did not hold any values. 当我尝试将refname或参数添加到脚本中时,它没有任何值。 (thinking that might be of help) (认为​​可能会有帮助)

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

Result: 结果:

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

The pre-receive hook gets a list of references and their old and new versions on standard input. pre-receive挂钩在标准输入上获取引用及其旧版本和新版本的列表。 So you can do something like this: 因此,您可以执行以下操作:

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

Note that this assumes that you only care about the head commit at the latest ref and that you're okay with doing this for tags as well. 请注意,这是假设您只关心最新引用的头提交,并且您也可以对标签执行此操作。 If you only want branches, and you want to traverse all the commits, then you can do something like this: 如果只希望分支,并且想遍历所有提交,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM