From 91080dd6d60da54c974951c90041925987a3f477 Mon Sep 17 00:00:00 2001 From: druckdev <63563978+druckdev@users.noreply.github.com> Date: Fri, 28 Aug 2020 02:33:38 +0200 Subject: [PATCH] Improve glog alias by creating a function for it Improve glog alias by creating a function for it that uses fzf to directly preview the full commit using git-show. The function also allows to select one of the commits and putting its abbreviated commit hash into the clipboard. --- .config/zsh/plugins/alias.zsh | 1 - .config/zsh/plugins/functionsPost.zsh | 42 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/.config/zsh/plugins/alias.zsh b/.config/zsh/plugins/alias.zsh index 71ea8c2..98fcd73 100644 --- a/.config/zsh/plugins/alias.zsh +++ b/.config/zsh/plugins/alias.zsh @@ -70,7 +70,6 @@ alias gpll='git pull' alias gpull='git pull' alias gdiff='git diff' alias gd='git diff' -alias glog="git log --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --graph --date=format:'%F %T' --color=always | less -Rn" ## Navigation alias ls='_ls_show_hidden --color=auto --group-directories-first -p -v' diff --git a/.config/zsh/plugins/functionsPost.zsh b/.config/zsh/plugins/functionsPost.zsh index fa57845..7379fdf 100644 --- a/.config/zsh/plugins/functionsPost.zsh +++ b/.config/zsh/plugins/functionsPost.zsh @@ -322,6 +322,48 @@ function urldec() { python3 -c "from urllib import parse; print(parse.unquote('$@'), end='')" } +glog() { + # Display yellow abbreviated commit hash, red author date, blue author name, + # green ref names, and the subject in no color. + local format='format:%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' + # Before being able to operate on the string itself we need to remove all + # ansi color escape sequences to not confuse sed. + local ansi_escape='s/\[[0-9]{0,2}m//g' + # Ignore the graph part at the beginning, then capture the commit hash and + # throw away the rest of the line. + local commit_hash='s/^[ */\\|]*([a-z0-9]*).*$/\1/' + + local date="--date=format:%F %T" + local colors="--color=always" + + # Display a colorful ascii graph of the commits in the above format and pipe + # that into fzf. + # Display ansi colors, reverse the layout so that the newest commit is at + # the top, and add a preview command, that: + # Takes the commit line, extracts the commit hash by using both patterns + # from above and saves that in a variable. When the variable is not empty + # (we are not on a line that contains only graph elements), execute git-show + # on the commit hash. + commit="$(\ + git log --pretty="$format" --graph "$date" $colors\ + | fzf --ansi --reverse --preview=" + out=\"\$(echo {} | sed -Ee \"$ansi_escape\" -e \"$commit_hash\")\" + if [ \"\$out\" ]; then + git show --pretty=fuller \"${date} %z\" $colors \"\$out\" + fi + " + )" + # If fzf exits successfully, put the abbreviated commit hash into the + # clipboard and write it into stdout. + if ! (( $? )); then + commit="$(sed -Ee "$ansi_escape" -e "$commit_hash" <<<"$commit")" + if command -v xclip &>/dev/null; then + echo -n "$commit" | xclip -selection clip + fi + echo "$commit" + fi +} + safe-remove() { [ $# -gt 0 ] || return 1 [ -e "$1" ] || return 1