grb: rebase on parent of the selected commit

The git rebase command needs to be passed the parent commit of the one
you want to edit. When selecting a commit interactively via fzf, it
makes sense to automatically pass the parent commit of the selected one.
Within fzf it's otherwise quite hard to find the parent commit by hand
if you have found your commit via an fzf search string.
This commit is contained in:
carlfriedrich 2022-07-22 14:41:38 +02:00
parent bd7942f6f7
commit d343300625
2 changed files with 20 additions and 6 deletions

View file

@ -437,10 +437,16 @@ function forgit::rebase -d "git rebase"
--preview=\"$preview\"
$FORGIT_REBASE_FZF_OPTS
"
set commit (eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf | eval "$forgit_extract_sha")
set target_commit (eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf | eval "$forgit_extract_sha")
if test $commit
git rebase -i "$commit"
if test $target_commit
# "$target_commit~" is invalid when the commit is the first commit, but we can use "--root" instead
set prev_commit "$target_commit~"
if test "(git rev-parse '$target_commit')" = "(git rev-list --max-parents=0 HEAD)"
set prev_commit "--root"
end
git rebase -i "$prev_commit"
end
end

View file

@ -177,7 +177,7 @@ forgit::cherry::pick() {
forgit::rebase() {
forgit::inside_work_tree || return 1
local cmd preview opts graph files commit
local cmd preview opts graph files target_commit prev_commit
graph=--graph
[[ $FORGIT_LOG_GRAPH_ENABLE == false ]] && graph=
cmd="git log $graph --color=always --format='$forgit_log_format' $* $forgit_emojify"
@ -190,8 +190,16 @@ forgit::rebase() {
--preview=\"$preview\"
$FORGIT_REBASE_FZF_OPTS
"
commit=$(eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf | eval "$forgit_extract_sha")
[[ -n "$commit" ]] && git rebase -i "$commit"
target_commit=$(eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf | eval "$forgit_extract_sha")
if [[ -n "$target_commit" ]]; then
# "$target_commit~" is invalid when the commit is the first commit, but we can use "--root" instead
if [[ "$(git rev-parse "$target_commit")" == "$(git rev-list --max-parents=0 HEAD)" ]]; then
prev_commit="--root"
else
prev_commit="$target_commit~"
fi
git rebase -i "$prev_commit"
fi
}
forgit::fixup() {