在Windows bash中查找字符串的一部分

时间:2019-09-09 09:38:41

标签: bash git shell githooks

我需要将分支名称中的任务代码添加到每个提交中。我们发现这应该通过钩子完成。

代码如下:

#!/bin/bash
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop)
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:\([a-z]\+\/\)*\([A-Z]\+-[0-9]\+\).\+:\2:')
# 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

现在发生了什么事

  

LG-​​132:LG-132:LG-132:LG-132:LG-132:LG-132:LG-132:将https://git.dw.com/scm/lg/webapp的分支“开发”合并为开发

因此,每次执行修订时,都会添加分支代码。现在,我正在尝试检查提交消息中是否包含分支代码,如果是,则将其中断/返回0。

这是我到目前为止所拥有的:

#!/bin/bash
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP=(master develop)
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:\([a-z]\+\/\)*\([A-Z]\+-[0-9]\+\).\+:\2:')
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
  set BRANCH_NAME|find "$TRIMMED" >nul && shouldAddCode=true || shouldAddCode=false
  if $shouldAddCode; then
    echo "test"
  else
    sed -i.bak -e "1s,^,$TRIMMED: ," "$1"
  fi
fi

我在控制台中得到了这个

  

找到:“ LG-132”:没有这样的文件或目录

现在此代码不起作用。我使用此代码作为参考:

set "i=hello world"
set i|find "world" >nul && set test=yes || set test=no
echo %test%
pause

我想念什么?

2 个答案:

答案 0 :(得分:1)

您的参考文献看起来不像bash代码,而是像CMD脚本。

要在bash脚本中检查某个变量是否以特定字符串开头,通常的方法是使用case ... esac构造,例如:

case "$BRANCHNAME" in
"$TRIMMED"*)
  # yes, it's there
  echo "test"
  ;;
*)
  # no, it's not there; add it
  if ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
    sed -i.bak -e "1s,^,$TRIMMED: ," "$1"
  fi
  ;;
esac

请注意奇怪的case arm语法,该语法在模式后仅带有右括号,并以双分号结尾。

答案 1 :(得分:0)

最终变得有点混乱,但这就是我想出的

#!/bin/bash
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
  BRANCHES_TO_SKIP=(master develop)
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:\([a-zA-Z]\+\/\)*\([a-zA-Z]\+-[0-9]\+\).\+:\2:')
TRIMMED=${TRIMMED^^}
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
  COMMIT_MESSAGE="$(cat "$1" | sed -E "s,^#.*|$TRIMMED: *,,g")"
  echo "$COMMIT_MESSAGE" >"$1"
  sed -i.bak -e "1s,^,$TRIMMED: ," "$1"
fi