diff --git a/.gitconfig b/.gitconfig index 5af73a6..21621e7 100644 --- a/.gitconfig +++ b/.gitconfig @@ -11,6 +11,7 @@ autoSquash = true [alias] publish = !git pull && git push + cb = !git-tmp-switch.sh [push] default = current autoSetupRemote = true diff --git a/.local/bin/git-tmp-switch.sh b/.local/bin/git-tmp-switch.sh new file mode 100755 index 0000000..ea3a864 --- /dev/null +++ b/.local/bin/git-tmp-switch.sh @@ -0,0 +1,31 @@ +#!/bin/bash +set -euo pipefail + +if [ "$#" -lt 1 ]; then + echo "Usage: git cb " + exit 1 +fi + +TARGET_BRANCH="$1" + +if [ "$TARGET_BRANCH" != "-" ]; then + if ! git rev-parse --verify --quiet "$TARGET_BRANCH" >/dev/null; then + echo "Error: branch '$TARGET_BRANCH' does not exist." >&2 + exit 1 + fi +fi + +git add -A +if ! git diff --cached --quiet || [ -n "$(git ls-files --others --exclude-standard)" ]; then + git commit -m "tmp" +fi + +git checkout "$TARGET_BRANCH" + +LATEST_AUTHOR=$(git log -1 --pretty=format:'%an') +LATEST_MESSAGE=$(git log -1 --pretty=format:'%s') +CURRENT_USER=$(git config user.name) +if [ "$LATEST_AUTHOR" = "$CURRENT_USER" ] && [ "$LATEST_MESSAGE" = "tmp" ]; then + echo "Resetting last 'tmp' commit by $CURRENT_USER..." + git reset --mixed HEAD~1 +fi