From 80fe6ac4a6ba350a874e0f5c7c28b0b12e3b4b90 Mon Sep 17 00:00:00 2001 From: Wenxuan Date: Thu, 17 Jan 2019 19:20:03 +0800 Subject: [PATCH] Refactor & Share code between zsh & sh. Former-commit-id: 268c6282cbe1aaf13e81a551361778f7c9813fa9 --- forgit.plugin.sh | 240 +--------------------------------------------- forgit.plugin.zsh | 98 ++++++------------- 2 files changed, 32 insertions(+), 306 deletions(-) mode change 100755 => 120000 forgit.plugin.sh mode change 100644 => 100755 forgit.plugin.zsh diff --git a/forgit.plugin.sh b/forgit.plugin.sh deleted file mode 100755 index ee55c39..0000000 --- a/forgit.plugin.sh +++ /dev/null @@ -1,239 +0,0 @@ -#!/usr/bin/env bash - -__forgit_fzf() { - FZF_DEFAULT_OPTS=" - $FZF_DEFAULT_OPTS - --ansi - --height '80%' - --bind='alt-k:preview-up,alt-p:preview-up' - --bind='alt-j:preview-down,alt-n:preview-down' - --bind='ctrl-r:toggle-all' - --bind='ctrl-s:toggle-sort' - --bind='?:toggle-preview' - --preview-window='right:60%' - --bind='alt-w:toggle-preview-wrap' - $FORGIT_FZF_DEFAULT_OPTS - " fzf "$@" -} - -__forgit_color_to_grep_code() { - case "$1" in - black) - echo -E '\[30m' - ;; - red) - echo -E '\[31m' - ;; - green) - echo -E '\[32m' - ;; - yellow) - echo -E '\[33m' - ;; - blue) - echo -E '\[34m' - ;; - magenta) - echo -E '\[35m' - ;; - cyan) - echo -E '\[36m' - ;; - white) - echo -E '\[97m' - ;; - esac -} - -# diff is fancy with diff-so-fancy! -command -v diff-so-fancy >/dev/null 2>&1 && forgit_fancy='|diff-so-fancy' -command -v emojify >/dev/null 2>&1 && forgit_emojify='|emojify' - -__forgit_inside_work_tree() { - git rev-parse --is-inside-work-tree >/dev/null 2>&1 -} - -__forgit_get_git_color() { - local color=$(git config $1); - color=${color:-$2} - echo $(__forgit_color_to_grep_code $color) -} - -# git commit browser -__forgit_log() { - __forgit_inside_work_tree || return 1 - local cmd="echo {} |grep -o '[a-f0-9]\{7\}' |head -1 |xargs -I% git show --color=always % $@ $forgit_emojify $forgit_fancy" - eval "git log --graph --color=always --format='%C(auto)%h%d %s %C(black)%C(bold)%cr' $@ $forgit_emojify" | - __forgit_fzf +s +m --tiebreak=index \ - --bind="enter:execute($cmd |LESS='-R' less)" \ - --bind="ctrl-y:execute-silent(echo {} |grep -o '[a-f0-9]\{7\}' |pbcopy)+abort" \ - --preview="$cmd" -} - -__forgit_diff() { - __forgit_inside_work_tree || return 1 - local cmd="git diff --no-ext-diff --color=always -- {} $forgit_emojify $forgit_fancy" - [[ $# -eq 0 ]] && local files=$(git rev-parse --show-toplevel) || local files="$@" - git ls-files --modified "$files"| - __forgit_fzf +m -0 \ - --bind="enter:execute($cmd |LESS='-R' less)" \ - --preview="$cmd" -} - -__forgit_add() { - __forgit_inside_work_tree || return 1 - added=$(__forgit_get_git_color "color.status.untracked" "red") - changed=$(__forgit_get_git_color "color.status.changed" "red") - unmerged=$(__forgit_get_git_color "color.status.unmerged" "red") - - local files=$(git -c color.status=always status --short | - grep -e "$added" -e "$changed" -e "$unmerged" | - awk '{printf "[%10s] ", $1; $1=""; print $0}' | - __forgit_fzf -0 -m --nth 2..,.. \ - --preview="git diff --no-ext-diff --color=always -- {-1} $forgit_emojify $forgit_fancy" | - cut -d] -f2 | - sed 's/.* -> //') # for rename case - [[ -n "$files" ]] && echo "$files" |xargs -I{} git add {} && git status --short && return - echo 'Nothing to add.' -} - - -__forgit_restore() { - __forgit_inside_work_tree || return 1 - local cmd="git diff --no-ext-diff --color=always -- {} $forgit_emojify $forgit_fancy" - local files=$(git ls-files --modified $(git rev-parse --show-toplevel)| - __forgit_fzf -m -0 --preview="$cmd") - [[ -n "$files" ]] && echo "$files" |xargs -I{} git checkout {} && git status --short && return - echo 'Nothing to restore.' -} - -__forgit_clean() { - __forgit_inside_work_tree || return 1 - # Note: Postfix '/' in directory path should be removed. Otherwise the directory itself will not be removed. - local files=$(git clean -xdfn "$@"| awk '{print $3}'| __forgit_fzf -m -0 |sed 's#/$##') - [[ -n "$files" ]] && echo "$files" |xargs -I{} git clean -xdf {} && return - echo 'Nothing to clean.' -} - -__forgit_stash_show() { - __forgit_inside_work_tree || return 1 - local cmd="git stash show \$(echo {}| cut -d: -f1) --color=always --ext-diff $forgit_fancy" - git stash list | - __forgit_fzf +s +m -0 --tiebreak=index \ - --bind="enter:execute($cmd |LESS='-R' less)" \ - --preview="$cmd" -} - -# git ignore generator -export FORGIT_GI_CACHE=~/.gicache -export FORGIT_GI_INDEX=${FORGIT_GI_CACHE}/.index - -if [[ -z "$FORGIT_GI_USE_LOCAL" ]]; then -__forgit_ignore() { - [ -f $FORGIT_GI_INDEX ] || __forgit_ignore_update - local preview="echo {} |awk '{print \$2}' |xargs -I% bash -c 'cat $FORGIT_GI_CACHE/% 2>/dev/null || (curl -sL https://www.gitignore.io/api/% |tee $FORGIT_GI_CACHE/%)'" - ${IFS+"false"} && unset oldifs || oldifs="$IFS" #store IFS - IFS=$'\n' - [[ $# -gt 0 ]] && args=($@) || args=($(cat $FORGIT_GI_INDEX |nl -nrn -w4 -s' ' |__forgit_fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}')) - ${oldifs+"false"} && unset IFS || IFS="$oldifs" # restore IFS - test -z "$args" && return 1 - local options=( - '(1) Output to stdout' - '(2) Append to .gitignore' - '(3) Overwrite .gitignore') - opt=$(printf '%s\n' "${options[@]}" |__forgit_fzf +m |awk '{print $1}') - case "$opt" in - '(1)' ) - __forgit_ignore_get ${args[@]} - ;; - '(2)' ) - __forgit_ignore_get ${args[@]} >> .gitignore - ;; - '(3)' ) - __forgit_ignore_get ${args[@]} > .gitignore - ;; - esac -} - -__forgit_ignore_update() { - mkdir -p $FORGIT_GI_CACHE - curl -sL https://www.gitignore.io/api/list |tr ',' '\n' > $FORGIT_GI_INDEX -} -__forgit_ignore_get() { - mkdir -p $FORGIT_GI_CACHE - for item in "$@"; do - cat "$FORGIT_GI_CACHE/$item" 2>/dev/null || (curl -sL "https://www.gitignore.io/api/$item" |tee "$FORGIT_GI_CACHE/$item") - done -} - -else - -__forgit_ignore() { - [ -f $FORGIT_GI_INDEX ] || __forgit_ignore_update - local preview="echo {} |awk '{print \$2}' |xargs -I% bash -c 'cat $FORGIT_GI_CACHE/gitignore/templates/%.gitignore 2>/dev/null'" - ${IFS+"false"} && unset oldifs || oldifs="$IFS" #store IFS - IFS=$'\n' - [[ $# -gt 0 ]] && args=($@) || args=($(cat $FORGIT_GI_INDEX |nl -nrn -w4 -s' ' |__forgit_fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}')) - ${oldifs+"false"} && unset IFS || IFS="$oldifs" # restore IFS - test -z "$args" && return 1 - local options=( - '(1) Output to stdout' - '(2) Append to .gitignore' - '(3) Overwrite .gitignore') - opt=$(printf '%s\n' "${options[@]}" |__forgit_fzf +m |awk '{print $1}') - case "$opt" in - '(1)' ) - __forgit_ignore_get ${args[@]} - ;; - '(2)' ) - __forgit_ignore_get ${args[@]} >> .gitignore - ;; - '(3)' ) - __forgit_ignore_get ${args[@]} > .gitignore - ;; - esac -} -__forgit_ignore_update() { - mkdir -p "$FORGIT_GI_CACHE" 2>/dev/null - if [[ -d "$FORGIT_GI_CACHE/gitignore" ]]; then - pushd "$FORGIT_GI_CACHE/gitignore" >/dev/null || exit - git pull --no-rebase --ff - popd >/dev/null - else - rm -rf "$FORGIT_GI_CACHE/gitignore" 2>/dev/null - git clone https://github.com/dvcs/gitignore.git "$FORGIT_GI_CACHE/gitignore" - fi - (for item in $FORGIT_GI_CACHE/gitignore/templates/*.gitignore; do - echo "${item##*/}" - done) | sed 's/.gitignore$//' | sort -f -u >| "$FORGIT_GI_INDEX" -} -__forgit_ignore_get() { - local item filename header - for item in "$@"; do - filename=$(find "$FORGIT_GI_CACHE/gitignore/templates" -type f -iname "${item}.gitignore" -print -quit) - if [[ -n "$filename" ]]; then - header="${filename##*/}" && header="${header%.gitignore}" - echo "### $header" - cat "$filename" - echo "" - filename= - fi - done -} -fi - -__forgit_ignore_clean() { - [[ -d $FORGIT_GI_CACHE ]] && rm -rf $FORGIT_GI_CACHE -} - - -# add aliases -if [[ -z "$FORGIT_NO_ALIASES" ]]; then - alias ${forgit_add:-ga}=__forgit_add - alias ${forgit_log:-glo}=__forgit_log - alias ${forgit_diff:-gd}=__forgit_diff - alias ${forgit_ignore:-gi}=__forgit_ignore - alias ${forgit_restore:-gcf}=__forgit_restore - alias ${forgit_clean:-gclean}=__forgit_clean - alias ${forgit_stash_show:-gss}=__forgit_stash_show -fi diff --git a/forgit.plugin.sh b/forgit.plugin.sh new file mode 120000 index 0000000..5547d6e --- /dev/null +++ b/forgit.plugin.sh @@ -0,0 +1 @@ +forgit.plugin.zsh \ No newline at end of file diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh old mode 100644 new mode 100755 index ff655be..32243e3 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -14,6 +14,9 @@ forgit::fzf() { " fzf "$@" } +COLOR_YELLOW='\e[1;33m' +COLOR_NC='\e[0m' # No Color + forgit::color_to_grep_code() { case "$1" in black) @@ -45,8 +48,8 @@ forgit::color_to_grep_code() { # diff is fancy with diff-so-fancy! -(( $+commands[diff-so-fancy] )) && forgit_fancy='|diff-so-fancy' -(( $+commands[emojify] )) && forgit_emojify='|emojify' +hash diff-so-fancy &>/dev/null && forgit_fancy='|diff-so-fancy' +hash emojify &>/dev/null && forgit_emojify='|emojify' forgit::inside_work_tree() { git rev-parse --is-inside-work-tree >/dev/null @@ -124,20 +127,19 @@ forgit::clean() { } # git ignore generator -export FORGIT_GI_CACHE=~/.gicache -export FORGIT_GI_INDEX=$FORGIT_GI_CACHE/.index +export FORGIT_GI_REPO=~/.forgit/gi/repos/dvcs/gitignore # https://github.com/dvcs/gitignore.git -if [[ -z "$FORGIT_GI_USE_LOCAL" ]]; then forgit:ignore() { - [ -f $FORGIT_GI_INDEX ] || forgit::ignore::update - local preview="echo {} |awk '{print \$2}' |xargs -I% bash -c 'cat $FORGIT_GI_CACHE/% 2>/dev/null || (curl -sL https://www.gitignore.io/api/% |tee $FORGIT_GI_CACHE/%)'" - IFS=$'\n' - [[ $# -gt 0 ]] && args=($@) || args=($(cat $FORGIT_GI_INDEX |nl -nrn -w4 -s' ' |forgit::fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}')) + [ -d $FORGIT_GI_REPO ] || forgit::ignore::update + local preview="cat $FORGIT_GI_REPO/templates/{2}.gitignore 2>/dev/null" + local IFS=$'\n' + [[ $# -gt 0 ]] && args=($@) || + args=($(forgit::ignore::list | nl -nrn -w4 -s' ' | + forgit::fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}')) test -z "$args" && return 1 - local options=( - '(1) Output to stdout' - '(2) Append to .gitignore' - '(3) Overwrite .gitignore') + local options=('(1) Output to stdout' + '(2) Append to .gitignore' + '(3) Overwrite .gitignore') opt=$(printf '%s\n' "${options[@]}" |forgit::fzf +m |awk '{print $1}') case "$opt" in '(1)' ) @@ -152,73 +154,35 @@ forgit:ignore() { esac } forgit::ignore::update() { - mkdir -p $FORGIT_GI_CACHE - curl -sL https://www.gitignore.io/api/list |tr ',' '\n' > $FORGIT_GI_INDEX -} -forgit::ignore::get() { - mkdir -p $FORGIT_GI_CACHE - for item in "$@"; do - cat "$FORGIT_GI_CACHE/$item" 2>/dev/null || (curl -sL "https://www.gitignore.io/api/$item" |tee "$FORGIT_GI_CACHE/$item") - done -} - -else - -forgit:ignore() { - [ -f $FORGIT_GI_INDEX ] || forgit::ignore::update - local preview="echo {} |awk '{print \$2}' |xargs -I% bash -c 'cat $FORGIT_GI_CACHE/gitignore/templates/%.gitignore 2>/dev/null'" - IFS=$'\n' - [[ $# -gt 0 ]] && args=($@) || args=($(cat $FORGIT_GI_INDEX |nl -nrn -w4 -s' ' |forgit::fzf -m --preview="$preview" --preview-window="right:70%" |awk '{print $2}')) - test -z "$args" && return 1 - local options=( - '(1) Output to stdout' - '(2) Append to .gitignore' - '(3) Overwrite .gitignore') - opt=$(printf '%s\n' "${options[@]}" |forgit::fzf +m |awk '{print $1}') - case "$opt" in - '(1)' ) - forgit::ignore::get ${args[@]} - ;; - '(2)' ) - forgit::ignore::get ${args[@]} >> .gitignore - ;; - '(3)' ) - forgit::ignore::get ${args[@]} > .gitignore - ;; - esac -} -forgit::ignore::update() { - mkdir -p "$FORGIT_GI_CACHE" 2>/dev/null - if [[ -d "$FORGIT_GI_CACHE/gitignore" ]]; then - pushd "$FORGIT_GI_CACHE/gitignore" >/dev/null || exit - git pull --no-rebase --ff - popd >/dev/null + if [[ -d "$FORGIT_GI_REPO" ]]; then + (cd $FORGIT_GI_REPO && git pull --no-rebase --ff) || return 1 else - rm -rf "$FORGIT_GI_CACHE/gitignore" 2>/dev/null - git clone https://github.com/dvcs/gitignore.git "$FORGIT_GI_CACHE/gitignore" + echo 'Initializing gitignore repo...' + git clone --depth=1 https://github.com/dvcs/gitignore.git "$FORGIT_GI_REPO" fi - (for item in $FORGIT_GI_CACHE/gitignore/templates/*.gitignore; do - echo "${item##*/}" - done) | sed 's/.gitignore$//' | sort -f -u >| "$FORGIT_GI_INDEX" } forgit::ignore::get() { local item filename header for item in "$@"; do - filename=$(find "$FORGIT_GI_CACHE/gitignore/templates" -type f -iname "${item}.gitignore" -print -quit) - if [[ -n "$filename" ]]; then + if filename=$(find "$FORGIT_GI_REPO/templates" -type f -iname "${item}.gitignore" -print -quit); then + [[ -z "$filename" ]] && forgit::warn "No gitignore template found for '$item'." >&2 && continue header="${filename##*/}" && header="${header%.gitignore}" echo "### $header" - cat "$filename" - echo "" - filename= + cat "$filename" && echo fi done } -fi - +forgit::ignore::list() { + (for item in $FORGIT_GI_REPO/templates/*.gitignore; do + echo "${item##*/}" + done) | sed 's/.gitignore$//' | sort -f -u +} forgit::ignore::clean() { setopt localoptions rmstarsilent - [[ -d $FORGIT_GI_CACHE ]] && rm -rf $FORGIT_GI_CACHE/* + [[ -d $FORGIT_GI_REPO ]] && rm -rf $FORGIT_GI_REPO +} +forgit::warn() { + printf "${COLOR_YELLOW}Warn${COLOR_NC}: $@\n" } # add aliases