diff --git a/forgit.plugin.sh b/forgit.plugin.sh index 99854a9..ee55c39 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,63 @@ __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() { + 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 } diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index 64d6e28..ff655be 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,61 @@ 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() { + 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() { setopt localoptions rmstarsilent [[ -d $FORGIT_GI_CACHE ]] && rm -rf $FORGIT_GI_CACHE/*