From 11340b9cfec893a455350c27518f1c04fc28af77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Tue, 20 Sep 2022 09:55:27 +0200 Subject: git: gitbmerge validates existence and remote status --- shellrc.d/git | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/shellrc.d/git b/shellrc.d/git index 2b2b441..d219db3 100644 --- a/shellrc.d/git +++ b/shellrc.d/git @@ -11,21 +11,32 @@ _gitbworktree() { # Expects name of the branch as argument # It fails if it's not fast forward merge and if there is fixup! commit. gitbmerge() ( + local branch="$1" set -e - [ -z "$(git log --grep="^fixup\!" HEAD.."$1")" ] || { + if git rev-parse --quiet --verify "$branch" >/dev/null; then + echo "No such branch: $branch" >&2 + return 1 + fi + local upstream + upstream="$(git rev-parse "$branch@{u}")" || true + if [ -n "$upstream" ] && [ "$upstream" != "$(git rev-parse "$branch")" ]; then + echo "First push your changes to the server!" >&2 + return 1 + fi + [ -z "$(git log --grep="^fixup\!" HEAD.."$branch")" ] || { echo "First squash fixups!" >&2 return 1 } - [ -z "$(git log --grep="^Apply .* suggestion(s) to " HEAD.."$1")" ] || { + [ -z "$(git log --grep="^Apply .* suggestion(s) to " HEAD.."$branch")" ] || { echo "First squash suggestions!" >&2 return 1 } - [ "$(git rev-parse HEAD)" != "$(git rev-parse "$1")" ] || { + [ "$(git rev-parse HEAD)" != "$(git rev-parse "$branch")" ] || { echo "Nothing to merge! 🤷" >&2 return 1 } local wt - wt="$(_gitbworktree "$1")" + wt="$(_gitbworktree "$branch")" if [ -n "$wt" ]; then if [ -d "$wd/.git" ]; then echo "Branch is checked out in root repository!" @@ -34,7 +45,10 @@ gitbmerge() ( rm -r "$wt" git worktree prune fi - git merge --ff-only "$1" && git push && git branch -d "$1" && git push origin :"$1" + git merge --ff-only "$branch" \ + && git push \ + && git branch -d "$branch" \ + && { if [ -n "$upstream" ]; then git push origin :"$branch"; fi; } ) # Checkout branch to new work tree -- cgit v1.2.3