Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented git revert commit #190

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ source (curl -sSL git.io/forgit-fish | psub)

- **Interactive `git checkout <commit>` selector** (`gco`)

- **Interactive `git revert <commit>` selector** (`grc`)

- **Interactive `git stash` viewer** (`gss`)

- **Interactive `git clean` selector** (`gclean`)
Expand Down Expand Up @@ -140,6 +142,7 @@ forgit_checkout_file=gcf
forgit_checkout_branch=gcb
forgit_checkout_tag=gct
forgit_checkout_commit=gco
forgit_revert_commit=grc
forgit_clean=gclean
forgit_stash_show=gss
forgit_cherry_pick=gcp
Expand Down Expand Up @@ -220,6 +223,7 @@ Customizing fzf options for each command individually is also supported:
| `gcb` | `FORGIT_CHECKOUT_BRANCH_FZF_OPTS` |
| `gct` | `FORGIT_CHECKOUT_TAG_FZF_OPTS` |
| `gco` | `FORGIT_CHECKOUT_COMMIT_FZF_OPTS` |
| `grc` | `FORGIT_REVERT_COMMIT_OPTS` |
| `gss` | `FORGIT_STASH_FZF_OPTS` |
| `gclean` | `FORGIT_CLEAN_FZF_OPTS` |
| `grb` | `FORGIT_REBASE_FZF_OPTS` |
Expand Down
23 changes: 23 additions & 0 deletions forgit.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,28 @@ forgit::checkout::commit() {
FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git checkout % --
}

# git revert-commit selector
forgit::revert::commit() {
forgit::inside_work_tree || return 1
[[ $# -ne 0 ]] && { git revert "$@"; return $?; }
local cmd opts files preview commits
cmd="git log --graph --color=always --format='$forgit_log_format' $* $forgit_emojify"
opts="
$FORGIT_FZF_DEFAULT_OPTS
+s --tiebreak=index
$FORGIT_REVERT_COMMIT_OPTS
"
files=$(sed -nE 's/.* -- (.*)/\1/p' <<< "$*") # extract files parameters for `git show` command
preview="echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % -- $files | $forgit_show_pager"
commits="$(eval "$cmd" |
FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" -m |
grep -Eo '^\*\s[a-f0-9]+' |
cut -c 3- |
tr $'\n' ' ')"
[[ -z "$commits" ]] && return 1
eval "git revert $commits"
}

# git ignore generator
export FORGIT_GI_REPO_REMOTE=${FORGIT_GI_REPO_REMOTE:-https://github.com/dvcs/gitignore}
export FORGIT_GI_REPO_LOCAL="${FORGIT_GI_REPO_LOCAL:-${XDG_CACHE_HOME:-$HOME/.cache}/forgit/gi/repos/dvcs/gitignore}"
Expand Down Expand Up @@ -352,6 +374,7 @@ if [[ -z "$FORGIT_NO_ALIASES" ]]; then
alias "${forgit_checkout_file:-gcf}"='forgit::checkout::file'
alias "${forgit_checkout_branch:-gcb}"='forgit::checkout::branch'
alias "${forgit_checkout_commit:-gco}"='forgit::checkout::commit'
alias "${forgit_revert_commit:-grc}"='forgit::revert::commit'
alias "${forgit_checkout_tag:-gct}"='forgit::checkout::tag'
alias "${forgit_clean:-gclean}"='forgit::clean'
alias "${forgit_stash_show:-gss}"='forgit::stash::show'
Expand Down