当我这样做时:
git pull --rebase --autostash
有时我会收到一条消息,指出存储该存储存在冲突,我需要手动对其进行合并。
与我有关的是退出状态为0
。
如果自动隐藏功能没有完全重新应用,如何获得非零退出状态?
答案 0 :(得分:1)
使用非零退出代码,您无法区分from urllib.parse import urljoin
class MyFileStorage(FileSystemStorage):
def url(self, name):
url = name
return urljoin(self.base_url, url)
错误和pull
错误。
我的建议是避免自动存储。当它工作时似乎很方便,但当它不工作时就很成问题。如果你做类似的事情
stash pop
您可以创建bash脚本或git别名:
git stash push
git pull --rebase
git stash pop
用法:
git alias.pull-autostash '!git stash push && git pull --rebase && git stash pop'
答案 1 :(得分:1)
我要解决此问题的脚本很简单。我将其发布在这里既可以帮助他人,也可以希望提出改进的意见。
将此~/bin/git-pull-autostash
命名为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