aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.local/bin/git-fixup
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2018-05-14 13:40:05 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2018-05-14 13:40:05 +0300
commit542e603f7c3d17dd797a8c194ecd2ad4933eefef (patch)
treef8dfed98636e4bfa22ccbb10dc272e1cf0bf0e86 /%HOME%/.local/bin/git-fixup
parent.gitconfig: even smarter alias.fixup (diff)
downloadlinux-home-542e603f7c3d17dd797a8c194ecd2ad4933eefef.tar.gz
linux-home-542e603f7c3d17dd797a8c194ecd2ad4933eefef.zip
.gitconfig: move alias.fixup to a separate script
Diffstat (limited to '')
-rwxr-xr-x%HOME%/.local/bin/git-fixup53
1 files changed, 53 insertions, 0 deletions
diff --git a/%HOME%/.local/bin/git-fixup b/%HOME%/.local/bin/git-fixup
new file mode 100755
index 0000000..b721ffe
--- /dev/null
+++ b/%HOME%/.local/bin/git-fixup
@@ -0,0 +1,53 @@
+#!/usr/bin/env bash
+
+set -o errexit -o nounset -o pipefail
+
+if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then
+ echo 'Not inside a Git repository.' >&2
+ exit 1
+fi
+
+if ! git rev-parse HEAD > /dev/null 2>&1 ; then
+ echo "Where's your HEAD?" >&2
+ exit 1
+fi
+
+has_staged_changes=
+git diff --cached --ignore-submodules --quiet || has_staged_changes=1
+
+has_unstaged_changes=
+git diff --ignore-submodules --quiet || has_unstaged_changes=1
+
+if [ -z "$has_staged_changes" ] && [ -z "$has_unstaged_changes" ]; then
+ echo 'No staged or unstaged changes, seemingly?' >&2
+ exit 1
+fi
+
+add=-a
+[ -n "$has_staged_changes" ] && add=
+
+numof_parents="$( git rev-list --parents -n 1 HEAD | wc --words )"
+if [ "$numof_parents" -eq 1 ]; then
+ dest_ref=--root
+elif [ "$numof_parents" -eq 2 ]; then
+ dest_ref='HEAD^^'
+else
+ echo 'Sure you want to fixup a merge commit?' >&2
+ exit 1
+fi
+
+git commit $add --fixup=HEAD
+
+stash=
+[ -n "$has_staged_changes" ] && [ -n "$has_unstaged_changes" ] && stash=1
+
+unstash() {
+ git stash pop --quiet
+}
+
+if [ -n "$stash" ]; then
+ git stash push --quiet
+ trap unstash EXIT
+fi
+
+GIT_EDITOR=true git rebase -i --autosquash "$dest_ref"