退出状态为0,但自动存储需要手动合并 [英] Exit status is 0 but autostash requires manual merging

查看:49
本文介绍了退出状态为0,但自动存储需要手动合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做时:

git pull --rebase --autostash

有时我会收到一条消息,指出存储该存储存在冲突,我需要手动对其进行合并.

Sometimes I get a message that there was a conflict applying the stash and I'll need to merge it manually.

与我有关的是退出状态为0.

What's concerning to me is that the exit status is 0.

如果自动隐藏功能没有完全重新应用,如何获得非零退出状态?

How do I get a non-zero exit status if the autostash didn't reapply cleanly?

推荐答案

要解决此问题,我的脚本很简单.我将其发布在这里既可以帮助他人,也可以希望提出改进的意见.

My script to work around this issue is non-trivial. I post it here to both help others and in the hope for comments for improvement.

调用此~/bin/git-pull-autostash并将其作为git pull-autostash调用:

Call this ~/bin/git-pull-autostash and invoke it as git pull-autostash:

#!/bin/bash

# Work around 0 exit if autostash doesn't apply
# https://stackoverflow.com/questions/52538050/exit-status-is-0-but-autostash-requires-manual-merging
# Work around 0 exit if no stash is created
# https://stackoverflow.com/questions/52568548/git-stash-exits-0-but-no-stash-created

set -euo pipefail

if [ -z "$(git status --porcelain  --untracked-files=no)" ]; then
  # Working directory and index are clean
  git pull --rebase "$@"
  exit 0
fi

# If we get to here, a stash is required.

exit_code_autostash_unpopped=4
exit_code_autostash_error=70  # https://unix.stackexchange.com/a/254747/143394

stash_msg="pull-autostash on $(git rev-parse --short @)"

get_stash_top() {
  local top
  if top=$(git rev-parse stash@\{0\} 2>/dev/null); then
    echo "$top"
  fi
  # Null output if there is no stash
}

prev_stash_top=$(get_stash_top)
git stash push -m "$stash_msg" > /dev/null
new_stash_top=$(get_stash_top)
stash_desc="${new_stash_top:0:7}: \"$stash_msg\""

# Minimise race conditions - have trap function ready before it is required
warn_pop_required() {
  local exit_code=$?  # Non-zero if invoked from trap
  if [[ -n ${pop_required-} ]]; then
    git stash list >&2
    printf '\nWARNING: autostash %s remains to be applied\n' "$stash_desc" >&2
    exit "$exit_code_autostash_unpopped"
  fi
  exit "$exit_code"
}
trap warn_pop_required EXIT


if [[ $new_stash_top != "$prev_stash_top" ]]; then
  # A stash was created
  pop_required=true  # flag for trap function
  printf 'Created stash %s\n' "$stash_desc"
fi


pop_stash() {
  local exit_code=$?
  if [[ $(get_stash_top) != "$new_stash_top" ]]; then
    printf 'WARNING: autostash %s is no longer at the top of the stack\n' "$stash_desc" >&2
    exit "$exit_code_autostash_error"
  else
    git stash pop --quiet
    unset pop_required
    printf 'Successfully popped stash %s\n' "$stash_desc"
  fi
  if [[ $exit_code -ne 0 ]]; then  # We were called from a signal
    exit "$exit_code"
  fi
}
trap pop_stash INT ERR TERM QUIT  # Pop stash on termination during pull

git pull --no-autostash --rebase "$@"
trap - INT ERR TERM QUIT  # Don't try to pop stash twice
pop_stash

这篇关于退出状态为0,但自动存储需要手动合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆