Git 如何检查bash脚本中的空樱桃选择?

Git 如何检查bash脚本中的空樱桃选择?,git,bash,cherry-pick,git-cherry-pick,Git,Bash,Cherry Pick,Git Cherry Pick,我有一个bash脚本,它运行以下cherry pick命令: if git cherry-pick -x "$commitId"; then ammed_commit "$BRANCH" else #here I want to check if this resulted in empty commit fi 我尝试再次运行该命令并以字符串形式获取输出,然后比较字符串,但输出不是我在控制台中看到的。我正在使用以下脚本进行测试: #!/bin/bash RE

我有一个bash脚本,它运行以下cherry pick命令:

if git cherry-pick -x "$commitId"; then
        ammed_commit "$BRANCH" 
  else
      #here I want to check if this resulted in empty commit
fi
我尝试再次运行该命令并以字符串形式获取输出,然后比较字符串,但输出不是我在控制台中看到的。我正在使用以下脚本进行测试:

#!/bin/bash

READ_STATUS=$(git cherry-pick -x 001e6beda)
SUB_STRING="The previous cherry-pick is now empty"

echo $READ_STATUS

stringContain() { [ -z "${2##*$1*}" ]; }

if stringContain "$READ_STATUS" 'The previous cherry-pick is now empty';then 
  echo yes
else 
  echo no
fi

exit
下面是我运行此脚本时收到的输出:

The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

git commit --allow-empty

Otherwise, please use 'git reset'
//READ_STATUS gets this value and not the one above this line???
On branch test-stage Your branch is ahead of 'upstream/test-stage' by 1 commit. (use "git push" to publish your local commits) You are currently cherry-picking commit 001e6beda. nothing to commit, working tree clean
no
所以我有两个问题:

  • 为什么我的字符串没有得到完整的输出
  • 如何解决此问题并成功检查cherry pick是否导致空提交

  • git
    将其错误消息发送到stderr,而不是stdout
    READ_STATUS=$(git cherry pick-x 001e6beda)
    捕获标准输出,并在git失败时将READ_STATUS设置为nothing

    您可以通过以下方式重写代码:

    read_status=$(git cherry-pick -x 001e6beda 2>&1)
    empty_message="The previous cherry-pick is now empty"
    if [[ $read_status = *"$empty_message"* ]]; then
      echo yes
    else
      echo no
    fi
    

    另见:


    谢谢。很好用!