英文:
GIT - invalid revision range in local repositories
问题
I apologize for the misunderstanding. It seems you provided a detailed explanation of a Git-related issue along with some code. Here's the translated code section you provided:
我正在尝试使用 pre-receive 钩子强制提交包含 Jira 任务 ID。为了在本地测试它,我正在将仓库用作服务器端,所以我运行了以下命令:
`git init --bare server` - 在 `hooks/pre-receive` 中,我添加了逻辑来检查提交是否以 [TICKET_ID] 或 [NA] 开头。当我克隆仓库 `git clone server client` 并且提交不包含这个任务时,我需要编辑提交消息,所以我使用: `git rebase -i COMMIT_HASH^`,然后我需要将 `pick` 更改为 `reword`,编辑提交消息并使用 `git push`。因为这可能会很烦人,我想添加 `prepare-commit-msg` 钩子以自动从分支名称中添加任务,格式如下:
```bash
#!/bin/bash
# 包括您希望禁用此脚本的任何分支
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop staging test)
fi
# 获取当前分支名称并检查是否被排除
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
# 简化分支名称以获取我们感兴趣的部分
TRIMMED=$(echo $BRANCH_NAME | sed -e 's:^\([^-]*-[^-]*\)-.*::' -e \
'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
# 如果它没有被排除,则在给定消息前添加简化的分支标识符
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s/^/$TRIMMED /" $1
fi
当我想要推送更改时: git push --set-upstream origin AZ-0987-test
,我遇到了错误:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 5 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 302 bytes | 302.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: fatal: Invalid revision range 0000000000000000000000000000000000000000..c15f3a1cb5875d944c8affcdec31f8b06a7e3ac4
To /home/user/just-testing/server/
* [new branch] AZ-0987-test -> AZ-0987-test
Branch 'AZ-0987-test' set up to track remote branch 'AZ-0987-test' from 'origin'.
并且 git log --oneline
返回了以下内容:
有人可以告诉我出了什么问题或我做错了什么吗?
编辑 - 重现问题的步骤
- 运行
git init --bare server
- 配置
server/hooks/pre-receive
如下:
#!/bin/bash
# Jira 任务模式
# JIRA_TICKET_PATTERN="[A-Z]+-[0-9]+"
# (^\[NA\])|(^\[[A-Z]{2,}-\d+\])
JIRA_TICKET_PATTERN="(^\[NA\])|(\[[A-Z]+-[0-9]+\])"
# 从标准输入读取新引用
while read oldrev newrev refname; do
# 遍历新的提交
for commit in $(git rev-list ${oldrev}..${newrev}); do
# 获取提交消息
message=$(git log --format=%B -n 1 ${commit})
# 检查提交消息是否已包含 Jira 任务编号
if [[ ${message} =~ ${JIRA_TICKET_PATTERN} ]]; then
echo "提交 ${commit} 已包含 [Jira 任务编号] 或 [NA]:${BASH_REMATCH[0]}"
else
echo "消息 ${message}"
echo "错误:"
echo "错误:由于提交"
echo "错误:$commit 在 ${refname#refs/heads/}"
echo "错误:缺少 JIRA 任务,例如:'[MM-1234] msg' 或 '[NA] msg'。"
echo "错误:"
echo "错误:请修复提交消息后再次推送。"
echo "错误:https://help.github.com/en/articles/changing-a-commit-message"
echo "错误"
exit 1
fi
done
- 使用
git clone server client
克隆仓库 - 创建文件,添加文件,提交带有错误消息 - 您应该收到错误
- 更改提交消息 - 运行
git rebase -i COMMIT^
,更改pick > reword
,保存,更改提交消息,[AB-1234] 消息,保存 - git push - 现在应该正常工作
- 在 .git/hooks/prepare-commit-msg(在克隆的仓库中)粘贴问题中提到的脚本
- 运行
git branch -b SO-987654-test-brach
- 添加文件,提交消息中没有任何任务编号 - 任务编号应该自动添加,推送更改
- 您应该看到一个错误
请注意,以上内容已经被翻译为中文。
英文:
I'm trying to force commits to include Jira ticket ID with pre-receive hook. To test it locally I'm using repository as server-side, so I ran:
git init --bare server
- in hooks/pre-receive
I added logic to check if commit starts with [TICKET_ID] or [NA]. When I clone repository git clone server client
and if commit doesn't include this ticket I need to edit commit message, so I use: git rebase -i COMMIT_HASH^
, then I need to change pick
to reword
, edit commit message and git push
. As it can be really annoying I want to add prepare-commit-msg
hook to automatically add ticket from branch name , which looks like:
#!/bin/bash
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop staging test)
fi
# Get the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
# Trim it down to get the parts we're interested in
TRIMMED=$(echo $BRANCH_NAME | sed -e 's:^\([^-]*-[^-]*\)-.*::' -e \
'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s/^/$TRIMMED /" $1
fi
When I want to push changes: git push --set-upstream origin AZ-0987-test
I'm getting error:
Counting objects: 100% (5/5), done.
Delta compression using up to 5 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 302 bytes | 302.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: fatal: Invalid revision range 0000000000000000000000000000000000000000..c15f3a1cb5875d944c8affcdec31f8b06a7e3ac4
To /home/user/just-testing/server/
* [new branch] AZ-0987-test -> AZ-0987-test
Branch 'AZ-0987-test' set up to track remote branch 'AZ-0987-test' from 'origin'.
And git log --oneline
return this:
Could somebody tell me what went wrong or what I'm doing wrong?
EDIT - Steps to reproduce my problem
- run
git init --bare server
- configure
server/hooks/pre-receive
with:
#!/bin/bash
# Jira ticket pattern
# JIRA_TICKET_PATTERN="[A-Z]+-[0-9]+"
# (^\[NA\])|(^\[[A-Z]{2,}-\d+\])
JIRA_TICKET_PATTERN="(^\[NA\])|(\[[A-Z]+-[0-9]+\])"
# Read the new references from standard input
while read oldrev newrev refname; do
# Iterate over the new commits
for commit in $(git rev-list ${oldrev}..${newrev}); do
# Get the commit message
message=$(git log --format=%B -n 1 ${commit})
# Check if the commit message already contains a Jira ticket number
if [[ ${message} =~ ${JIRA_TICKET_PATTERN} ]]; then
echo "Commit ${commit} already contains a [Jira ticket number] or [NA]: ${BASH_REMATCH[0]}"
else
echo "MESSAGE ${message}"
echo "ERROR:"
echo "ERROR: Your push was rejected because the commit"
echo "ERROR: $commit in ${refname#refs/heads/}"
echo "ERROR: is missing the JIRA Issue, for example: '[MM-1234] msg' or '[NA] msg'."
echo "ERROR:"
echo "ERROR: Please fix the commit message and push again."
echo "ERROR: https://help.github.com/en/articles/changing-a-commit-message"
echo "ERROR"
exit 1
fi
done
- clone repository with:
git clone server client
- create file, add file, commit with wrong message - you should get error
- change commit message - run
git rebase -i COMMIT^
, changepick>reword
, save, change commit message , [AB-1234] message, save - git push - it should work now
- in .git/hooks/prepare-commit-msg (in cloned repository) paste script from my question
- run
git branch -b SO-987654-test-brach
- add file, commit without any ticker in msg - ticker should be added automatically, push changes
- you should see an error
答案1
得分: 3
你的 pre-receive 钩子没有检查新分支,只是使用了 $old..$new
或类似的内容,除非在受限制的情况下,否则无法工作。
#!/bin/bash --
# pre-receive 钩子用于审核所有不在现有历史中的入站提交
# 收集所有未删除的提示,*[^0]* 表示任何具有非零字符的字符串
while read old new ref; do
incoming+=("$old $new $ref")
[[ $new = *[^0]* ]] && news+=($new)
done
# 审核每个提交,通过 here-doc 来获取 shell 环境的访问权限
while read commit; do
check $commit here || hookrc=1
done <<EOD
$(git rev-list ${news[@]} --not --all)
EOD
exit $hookrc
(注:Mac 自带的 bash 安装版本被严重忽视,你可能需要使用 brew install bash
进行升级。)
英文:
Your pre-receive isn't checking for new branches, you're just using $old..$new
or some equivalent, which won't work except in restricted situations.
#!/bin/bash --
# pre-receive hook to vet all inbound commits not in existing history
# collect all non-deleted tips, *[^0]* is any string w/ a nonzero
while read old new ref; do
incoming+=("$old $new $ref")
[[ $new = *[^0]* ]] && news+=($new)
done
# vet each commit, id's via here-doc to keep shell environ access
while read commit; do
check $commit here || hookrc=1
done <<EOD
$( git rev-list ${news[@]} --not --all )
EOD
exit $hookrc
(note: the Mac ships with a criminally-neglected bash install, you'll need the brew install bash
version.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论