Skip to content

Commit

Permalink
Feature: Add interactive git reflog (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandr01d authored Sep 4, 2024
1 parent 8b60a89 commit be82c47
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Install `forgit` in just one click.

- **Interactive `git rebase -i` selector** (`grb`)

- **Interactive `git reflog` viewer** (`grl`)

- **Interactive `git blame` selector** (`gbl`)

- **Interactive `git commit --fixup && git rebase -i --autosquash` selector** (`gfu`)
Expand Down Expand Up @@ -176,6 +178,7 @@ You can change the default aliases by defining these variables below.

``` bash
forgit_log=glo
forgit_reflog=grl
forgit_diff=gd
forgit_add=ga
forgit_reset_head=grh
Expand Down Expand Up @@ -235,6 +238,7 @@ These are passed to the according `git` calls.
| -------- | --------------------------------------------------------------------------- |
| `ga` | `FORGIT_ADD_GIT_OPTS` |
| `glo` | `FORGIT_LOG_GIT_OPTS` |
| `grl` | `FORGIT_REFLOG_GIT_OPTS` |
| `gd` | `FORGIT_DIFF_GIT_OPTS` |
| `grh` | `FORGIT_RESET_HEAD_GIT_OPTS` |
| `gcf` | `FORGIT_CHECKOUT_FILE_GIT_OPTS` |
Expand Down Expand Up @@ -287,6 +291,7 @@ Customizing fzf options for each command individually is also supported:
|----------|-----------------------------------|
| `ga` | `FORGIT_ADD_FZF_OPTS` |
| `glo` | `FORGIT_LOG_FZF_OPTS` |
| `grl` | `FORGIT_REFLOG_FZF_OPTS` |
| `gi` | `FORGIT_IGNORE_FZF_OPTS` |
| `gd` | `FORGIT_DIFF_FZF_OPTS` |
| `grh` | `FORGIT_RESET_HEAD_FZF_OPTS` |
Expand Down
26 changes: 26 additions & 0 deletions bin/git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,31 @@ _forgit_log() {
return $fzf_exit_code
}

# git reflog viewer
_forgit_reflog() {
_forgit_inside_work_tree || return 1
_forgit_contains_non_flags "$@" && { git reflog "$@"; return $?; }
local opts reflog_format
opts="
$FORGIT_FZF_DEFAULT_OPTS
+s +m --tiebreak=index
--bind=\"enter:execute($FORGIT log_enter {})\"
--bind=\"ctrl-y:execute-silent($FORGIT yank_sha {})\"
--preview=\"$FORGIT log_preview {}\"
$FORGIT_REFLOG_FZF_OPTS
"
reflog_format=${FORGIT_GRL_FORMAT:-$_forgit_log_format}
_forgit_reflog_git_opts=()
_forgit_parse_array _forgit_reflog_git_opts "$FORGIT_REFLOG_GIT_OPTS"
git reflog show --color=always --format="$reflog_format" "${_forgit_reflog_git_opts[@]}" "$@" |
_forgit_emojify |
FZF_DEFAULT_OPTS="$opts" fzf
fzf_exit_code=$?
# exit successfully on 130 (ctrl-c/esc)
[[ $fzf_exit_code == 130 ]] && return 0
return $fzf_exit_code
}

_forgit_get_files_from_diff_line() {
# Construct a null-terminated list of the filenames
# The input looks like one of these lines:
Expand Down Expand Up @@ -992,6 +1017,7 @@ public_commands=(
"fixup"
"ignore"
"log"
"reflog"
"rebase"
"reset_head"
"revert_commit"
Expand Down
9 changes: 9 additions & 0 deletions completions/_git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ _git-staged() {
_alternative "files:filename:($(git diff --name-only --staged))"
}

_git-forgit-reflog() {
declare -a cmds
cmds=('expire:prune old reflog entries' 'delete:delete entries from reflog' 'show:show log of ref' 'exists:check whether a ref has a reflog')
_alternative 'cmds:: _describe -t cmds cmd cmds' 'refs:: __git_references'
}

_git-forgit() {
local subcommand cmd
subcommand="${words[1]}"
Expand Down Expand Up @@ -65,6 +71,7 @@ _git-forgit() {
'fixup:git fixup'
'ignore:git ignore generator'
'log:git commit viewer'
'reflog:git reflog viewer'
'rebase:git rebase'
'reset_head:git reset HEAD (unstage) selector'
'revert_commit:git revert commit selector'
Expand All @@ -85,6 +92,7 @@ _git-forgit() {
diff) _git-forgit-diff ;;
fixup) __git_branch_names ;;
log) _git-log ;;
reflog) _git-forgit-reflog ;;
rebase) _git-rebase ;;
reset_head) _git-staged ;;
revert_commit) __git_recent_commits ;;
Expand All @@ -109,6 +117,7 @@ compdef _git-clean forgit::clean
compdef _git-forgit-diff forgit::diff
compdef __git_branch_names forgit::fixup
compdef _git-log forgit::log
compdef _git-reflog forgit::reflog
compdef _git-rebase forgit::rebase
compdef _git-staged forgit::reset::head
compdef __git_recent_commits forgit::revert::commit
Expand Down
4 changes: 4 additions & 0 deletions completions/git-forgit.bash
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ _git_forgit()
fixup
ignore
log
reflog
rebase
reset_head
revert_commit
Expand All @@ -96,6 +97,7 @@ _git_forgit()
diff) _git_diff ;;
fixup) _git_branch ;;
log) _git_log ;;
reflog) _git_reflog ;;
rebase) _git_rebase ;;
reset_head) _git_reset ;;
revert_commit) _git_revert ;;
Expand Down Expand Up @@ -129,6 +131,7 @@ then
__git_complete forgit::diff _git_diff
__git_complete forgit::fixup _git_branch
__git_complete forgit::log _git_log
__git_complete forgit::reflog _git_reflog
__git_complete forgit::rebase _git_rebase
__git_complete forgit::reset::head _git_reset
__git_complete forgit::revert::commit _git_revert
Expand All @@ -147,6 +150,7 @@ then
__git_complete "${forgit_diff}" _git_diff
__git_complete "${forgit_fixup}" _git_branch
__git_complete "${forgit_log}" _git_log
__git_complete "${forgit_reflog}" _git_reflog
__git_complete "${forgit_rebase}" _git_rebase
__git_complete "${forgit_reset_head}" _git_reset
__git_complete "${forgit_revert_commit}" _git_revert
Expand Down
6 changes: 4 additions & 2 deletions completions/git-forgit.fish
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

function __fish_forgit_needs_subcommand
for subcmd in add blame branch_delete checkout_branch checkout_commit checkout_file checkout_tag \
cherry_pick cherry_pick_from_branch clean diff fixup ignore log rebase reset_head revert_commit \
stash_show stash_push
cherry_pick cherry_pick_from_branch clean diff fixup ignore log reflog rebase reset_head \
revert_commit stash_show stash_push
if contains -- $subcmd (commandline -opc)
return 1
end
Expand Down Expand Up @@ -36,6 +36,7 @@ complete -c git-forgit -n __fish_forgit_needs_subcommand -a diff -d 'git diff vi
complete -c git-forgit -n __fish_forgit_needs_subcommand -a fixup -d 'git fixup'
complete -c git-forgit -n __fish_forgit_needs_subcommand -a ignore -d 'git ignore generator'
complete -c git-forgit -n __fish_forgit_needs_subcommand -a log -d 'git commit viewer'
complete -c git-forgit -n __fish_forgit_needs_subcommand -a reflog -d 'git reflog viewer'
complete -c git-forgit -n __fish_forgit_needs_subcommand -a rebase -d 'git rebase'
complete -c git-forgit -n __fish_forgit_needs_subcommand -a reset_head -d 'git reset HEAD (unstage) selector'
complete -c git-forgit -n __fish_forgit_needs_subcommand -a revert_commit -d 'git revert commit selector'
Expand All @@ -52,6 +53,7 @@ complete -c git-forgit -n '__fish_seen_subcommand_from cherry_pick' -a "(complet
complete -c git-forgit -n '__fish_seen_subcommand_from clean' -a "(__fish_git_files untracked ignored)"
complete -c git-forgit -n '__fish_seen_subcommand_from fixup' -a "(__fish_git_local_branches)"
complete -c git-forgit -n '__fish_seen_subcommand_from log' -a "(complete -C 'git log ')"
complete -c git-forgit -n '__fish_seen_subcommand_from reflog' -a "(complete -C 'git reflog ')"
complete -c git-forgit -n '__fish_seen_subcommand_from rebase' -a "(complete -C 'git rebase ')"
complete -c git-forgit -n '__fish_seen_subcommand_from reset_head' -a "(__fish_git_files all-staged)"
complete -c git-forgit -n '__fish_seen_subcommand_from revert_commit' -a "(__fish_git_commits)"
Expand Down
1 change: 1 addition & 0 deletions conf.d/forgit.plugin.fish
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ if test -z "$FORGIT_NO_ALIASES"
abbr -a -- (string collect $forgit_add; or string collect "ga") git-forgit add
abbr -a -- (string collect $forgit_reset_head; or string collect "grh") git-forgit reset_head
abbr -a -- (string collect $forgit_log; or string collect "glo") git-forgit log
abbr -a -- (string collect $forgit_reflog; or string collect "grl") git-forgit reflog
abbr -a -- (string collect $forgit_diff; or string collect "gd") git-forgit diff
abbr -a -- (string collect $forgit_ignore; or string collect "gi") git-forgit ignore
abbr -a -- (string collect $forgit_checkout_file; or string collect "gcf") git-forgit checkout_file
Expand Down
6 changes: 6 additions & 0 deletions forgit.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ forgit::log() {
"$FORGIT" log "$@"
}

forgit::reflog() {
"$FORGIT" reflog "$@"
}

forgit::diff() {
"$FORGIT" diff "$@"
}
Expand Down Expand Up @@ -140,6 +144,7 @@ if [[ -z "$FORGIT_NO_ALIASES" ]]; then
export forgit_add="${forgit_add:-ga}"
export forgit_reset_head="${forgit_reset_head:-grh}"
export forgit_log="${forgit_log:-glo}"
export forgit_reflog="${forgit_reflog:-grl}"
export forgit_diff="${forgit_diff:-gd}"
export forgit_ignore="${forgit_ignore:-gi}"
export forgit_checkout_file="${forgit_checkout_file:-gcf}"
Expand All @@ -159,6 +164,7 @@ if [[ -z "$FORGIT_NO_ALIASES" ]]; then
alias "${forgit_add}"='forgit::add'
alias "${forgit_reset_head}"='forgit::reset::head'
alias "${forgit_log}"='forgit::log'
alias "${forgit_reflog}"='forgit::reflog'
alias "${forgit_diff}"='forgit::diff'
alias "${forgit_ignore}"='forgit::ignore'
alias "${forgit_checkout_file}"='forgit::checkout::file'
Expand Down

0 comments on commit be82c47

Please sign in to comment.