Состояние выхода 0, но автозагрузка требует ручного объединения - PullRequest
0 голосов
/ 27 сентября 2018

Когда я делаю:

git pull --rebase --autostash

Иногда я получаю сообщение о том, что возник конфликт при применении тайника, и мне нужно объединить его вручную.

Что касается меня, так эточто статус выхода - 0.

Как получить ненулевой статус выхода, если автозагрузка не была выполнена повторно?

Ответы [ 2 ]

0 голосов
/ 18 октября 2018

Мой сценарий для решения этой проблемы нетривиален.Я публикую его здесь, чтобы помочь другим и в надежде получить комментарии для улучшения.

Назовите это ~/bin/git-pull-autostash и вызовите его как git pull-autostash:


#!/bin/bash

# Work around 0 exit if autostash doesn't apply
# /11524596/sostoyanie-vyhoda-0-no-avtozagruzka-trebuet-ruchnogo-obedineniya
# 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 голосов
/ 27 сентября 2018

С ненулевым кодом выхода вы не можете отличить pull error от stash pop error.

Мой совет - избегать автозагрузки.Это кажется удобным, когда это работает, но проблематично, когда это не так.А если вы делаете что-то вроде

git stash push
git pull --rebase
git stash pop

, вы можете создать bash-скрипт или псевдоним git:

git alias.pull-autostash '!git stash push && git pull --rebase && git stash pop'

Использование:

git pull-autostash
...