From 111c851055fa21c70154da6ec4c610fb0797a02b Mon Sep 17 00:00:00 2001 From: druckdev Date: Thu, 15 Jul 2021 01:20:35 +0200 Subject: [PATCH] zsh:funcs: Add git-rebase-add-stash --- .config/zsh/zshrc.d/40-functions.zsh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.config/zsh/zshrc.d/40-functions.zsh b/.config/zsh/zshrc.d/40-functions.zsh index 471ed90..a7f04dd 100644 --- a/.config/zsh/zshrc.d/40-functions.zsh +++ b/.config/zsh/zshrc.d/40-functions.zsh @@ -412,3 +412,20 @@ git-checkout-worktree() { git worktree prune command rm -rf "$TEMP_DIR" } + +# This is meant for adding a quick fix to a commit. +# It automatically rebases a given commit (defaults to HEAD), applies the given +# stash (defaults to last) and finishes the rebase. +git-rebase-add-stash() { + : ${1:=HEAD~} + [[ "$(git cat-file -t "$1")" = "commit" ]] || return 1 + (( $(git stash list | wc -l) )) || { echo "No stashes" >&2; return 1; } + + # Substitute the first 'pick' with 'edit' in the rebase todo, apply the + # stash & finish + EDITOR='sed -i "1s/^pick/edit/"' \ + git rebase -i "$1" && + git stash apply "${2:-0}" && + git add -u && + git rebase --continue +}