diff options
author | Karel Kočí <cynerd@email.cz> | 2022-09-20 09:55:27 +0200 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2022-09-20 09:55:27 +0200 |
commit | 11340b9cfec893a455350c27518f1c04fc28af77 (patch) | |
tree | 7bdc3f6f247232f59de90f9f643733a8d121e487 | |
parent | 342ba9f696861a19ab728571589019174db173cc (diff) | |
download | shellrc-11340b9cfec893a455350c27518f1c04fc28af77.tar.gz shellrc-11340b9cfec893a455350c27518f1c04fc28af77.tar.bz2 shellrc-11340b9cfec893a455350c27518f1c04fc28af77.zip |
git: gitbmerge validates existence and remote status
-rw-r--r-- | shellrc.d/git | 24 |
1 files 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 |