无法使用正则表达式提取子字符串
can't extract a substring with regex
我正在尝试为 git 编写 prepare-commit-msg 挂钩。该脚本应执行以下步骤:
- 获取当前git分支名称(有效)
- 提取
issue-id
(不工作)
- 检查
issue-id
是否已经在提交消息中
- 如果没有,在提交信息前插入
[issue-id]
issue-id
具有此模式 [a-zA-Z]+-\d+
,分支名称应类似于 feature/issue-id-my-small-description
。
但是现在,提取部分还不行...
这是我的 prepare-commit-msg 脚本:
# Regex used to extract the issue id
REGEX_ISSUE_ID="s/([a-zA-Z]+-\d+)//"
# Find current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Extract issue id from branch name
ISSUE_ID= $BRANCH_NAME | sed -r $REGEX_ISSUE_ID
# Check if the issue id is already in the msg
ISSUE_IN_COMMIT=$(grep -c "\[$ISSUE_ID\]" )
# Check if branch name is not null and if the issue id is already in the commit msg
if [ -n "$BRANCH_NAME" ] && ! [[ $ISSUE_IN_COMMIT -ge 1 ]]; then
# Prefix with the issue id surrounded with brackets
sed -i.bak -e "1s/^/[$ISSUE_ID] /"
fi
编辑以添加 in-/output 示例
输入 </code> 是 git 提交消息,类似于 </p>
<pre><code>fix bug on login
或
fix MyIssue-234 which is a bug on login
输出应该是带有问题 ID 的输入,即:
[MyIssue-123] fix bug on login
我不确定你这样做是什么以及为什么这样做,但这是我通过修复我认为在你的代码中需要更正的最接近的结果:
# Regex used to extract the issue id
REGEX_ISSUE_ID="s/\[([a-zA-Z]+-[0-9]+)\].*//"
# Find current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
if [[ -z "$BRANCH_NAME" ]]; then
echo "No brach name... "; exit 1
fi
# Extract issue id from branch name
ISSUE_ID=$(echo "$BRANCH_NAME" | sed -r "$REGEX_ISSUE_ID")
# Check if the issue id is already in the msg
ISSUE_IN_COMMIT=$(echo "$@" | grep -c "^\[*$ISSUE_ID\]*")
# Check if branch name is not null and if the issue id is already in the commit msg
if [[ -n "$BRANCH_NAME" ]]; then
if [[ $ISSUE_IN_COMMIT -gt 0 ]]; then
shift # Drop the issue if from the msg
fi
# Prefix with the issue id surrounded with brackets
MESSAGE="[$ISSUE_ID] $@"
fi
echo "$MESSAGE"
其中 $@
是您在 "fix" 之后提供的所有字词(例如 "$@" = "bug" "on" "login"
)。剩下的希望大家对照自己的原代码来理解。
我正在尝试为 git 编写 prepare-commit-msg 挂钩。该脚本应执行以下步骤:
- 获取当前git分支名称(有效)
- 提取
issue-id
(不工作) - 检查
issue-id
是否已经在提交消息中 - 如果没有,在提交信息前插入
[issue-id]
issue-id
具有此模式 [a-zA-Z]+-\d+
,分支名称应类似于 feature/issue-id-my-small-description
。
但是现在,提取部分还不行...
这是我的 prepare-commit-msg 脚本:
# Regex used to extract the issue id
REGEX_ISSUE_ID="s/([a-zA-Z]+-\d+)//"
# Find current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Extract issue id from branch name
ISSUE_ID= $BRANCH_NAME | sed -r $REGEX_ISSUE_ID
# Check if the issue id is already in the msg
ISSUE_IN_COMMIT=$(grep -c "\[$ISSUE_ID\]" )
# Check if branch name is not null and if the issue id is already in the commit msg
if [ -n "$BRANCH_NAME" ] && ! [[ $ISSUE_IN_COMMIT -ge 1 ]]; then
# Prefix with the issue id surrounded with brackets
sed -i.bak -e "1s/^/[$ISSUE_ID] /"
fi
编辑以添加 in-/output 示例
输入 </code> 是 git 提交消息,类似于 </p>
<pre><code>fix bug on login
或
fix MyIssue-234 which is a bug on login
输出应该是带有问题 ID 的输入,即:
[MyIssue-123] fix bug on login
我不确定你这样做是什么以及为什么这样做,但这是我通过修复我认为在你的代码中需要更正的最接近的结果:
# Regex used to extract the issue id
REGEX_ISSUE_ID="s/\[([a-zA-Z]+-[0-9]+)\].*//"
# Find current branch name
BRANCH_NAME=$(git symbolic-ref --short HEAD)
if [[ -z "$BRANCH_NAME" ]]; then
echo "No brach name... "; exit 1
fi
# Extract issue id from branch name
ISSUE_ID=$(echo "$BRANCH_NAME" | sed -r "$REGEX_ISSUE_ID")
# Check if the issue id is already in the msg
ISSUE_IN_COMMIT=$(echo "$@" | grep -c "^\[*$ISSUE_ID\]*")
# Check if branch name is not null and if the issue id is already in the commit msg
if [[ -n "$BRANCH_NAME" ]]; then
if [[ $ISSUE_IN_COMMIT -gt 0 ]]; then
shift # Drop the issue if from the msg
fi
# Prefix with the issue id surrounded with brackets
MESSAGE="[$ISSUE_ID] $@"
fi
echo "$MESSAGE"
其中 $@
是您在 "fix" 之后提供的所有字词(例如 "$@" = "bug" "on" "login"
)。剩下的希望大家对照自己的原代码来理解。