From c1a8bd71dc771bc607acf866b7e209f0955e4dcd Mon Sep 17 00:00:00 2001 From: Laggard Kernel Date: Thu, 17 Jan 2019 14:56:01 +0800 Subject: [PATCH] Feature: use gitignore templates from local repo directly The gitignore templates used here is from dvcs/gitignore, which is the same one used by gitignore.io. Environment variable `FORGIT_GI_USE_LOCAL` is used as a switch to enable this feature, or the original method is used by default. `forgit::ignore::update` and `forgit::ignore::get` are rewritten to download the repo, to get templates from the local repo. local variable `preview` in `forgit:ignore` is also rewritten to get the preview from there. --- forgit.plugin.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++ forgit.plugin.zsh | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/forgit.plugin.sh b/forgit.plugin.sh index 99854a9..cab940d 100755 --- a/forgit.plugin.sh +++ b/forgit.plugin.sh @@ -127,6 +127,8 @@ __forgit_stash_show() { # 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/%)'" @@ -163,6 +165,57 @@ __forgit_ignore_get() { 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() { + for item in "$@"; do + echo "### $item" + cat "$FORGIT_GI_CACHE/gitignore/templates/${item}.gitignore" + echo "" + done +} +fi + __forgit_ignore_clean() { [[ -d $FORGIT_GI_CACHE ]] && rm -rf $FORGIT_GI_CACHE } diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index 64d6e28..bb15124 100644 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -126,6 +126,8 @@ forgit::clean() { # 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/%)'" @@ -159,6 +161,55 @@ forgit::ignore::get() { 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 + 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() { + for item in "$@"; do + echo "### $item" + cat "$FORGIT_GI_CACHE/gitignore/templates/${item}.gitignore" + echo "" + done +} +fi + forgit::ignore::clean() { setopt localoptions rmstarsilent [[ -d $FORGIT_GI_CACHE ]] && rm -rf $FORGIT_GI_CACHE/*