如何在Bash脚本中检查空的cherry-picks?

3

我有一个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

我有两个问题:

  1. 为什么我的字符串没有完整的输出?
  2. 如何解决这个问题并成功地检查cherry-pick是否导致空提交?
1个回答

3
git会将错误信息发送到stderr而不是stdout。READ_STATUS=$(git cherry-pick -x 001e6beda)会捕获stdout并在git失败时将READ_STATUS设置为空。
您可以按照以下方式重写代码:
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

另请参阅:


2
谢谢。功能完美! - Gaurav N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接