我正在尝试在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
答案 0 :(得分:1)
pre-receive挂钩在标准输入上获取引用及其旧版本和新版本的列表。因此,您可以执行以下操作:
#!/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
请注意,这假设您只关心最新引用的head提交,并且您也可以对标签执行此操作。如果只希望分支,并且想遍历所有提交,则可以执行以下操作:
#!/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