commit 4b4455f04e03b44bded3df4be12387e461e0e158
parent a31589537c3231b6233a23a55bf0e23ffa064d09
Author: Lucas Burns <burnsac@me.com>
Date:   Sun,  9 May 2021 09:39:55 -0500

modified script to print git status, declare arrays properly

Diffstat:
MREADME.md | 2+-
Mfadd | 10++++++----
Mfcheckout | 4++--
Mfgit_helper.sh | 20++++++++++----------
Mflog | 4++--
Mfreset | 4++--
Mfstash | 4++--
Mfstat | 18+++++++++++++-----
Mfuntrack | 4++--
Mfzgrep | 6+++---
Mpreview.sh | 5+++--
11 files changed, 46 insertions(+), 35 deletions(-)

diff --git a/README.md b/README.md @@ -7,7 +7,7 @@ Installation: ------------ ```sh -git clone https://github.com/burnsac5040/fzfgit.git +git clone https://github.com/lmburns/fzfgit.git cd fzfgit make install ``` diff --git a/fadd b/fadd @@ -37,14 +37,14 @@ Optional arguments: # $1: array of files to stage ####################################### -function stage_file() { - local files=("$@") - [[ "${#files[@]}" -eq 0 ]] && exit 1 +stage_file() { + local -a files && files=("$@") + [[ "${#files[@]}" -eq 0 ]] && { printf "%s" "$(tput setaf 1)Nothing added$(tput sgr0)"; exit 1; } git add "${files[@]}" } stage_type="modified" -selected_files=() +typeset -a selected_files && selected_files=() while [[ "$#" -gt 0 ]]; do case "$1" in @@ -68,3 +68,5 @@ done < <( ) stage_file "${selected_files[@]}" + +[[ "${#selected_files[@]}" -ne 0 ]] && git status -sb diff --git a/fcheckout b/fcheckout @@ -21,7 +21,7 @@ set -ef mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/fgit_helper.sh -function usage() { +usage() { echo -e "Usage: fcheckout [-h] [-s] [-b] [-c] [-y] ... Select files/commit/branch through fzf and checkout the selected objects. @@ -40,7 +40,7 @@ Optional arguments: } action_type="modified" -selected_files=() +typeset -a kselected_files=() confirm="" selected_commit="" selected_branch="" diff --git a/fgit_helper.sh b/fgit_helper.sh @@ -27,7 +27,7 @@ FZF_DEFAULT_OPTS=" # Arguments: # $1: if exists, disable multi, set single ####################################### -function set_fzf_multi() { +set_fzf_multi() { local no_multi="$1" if [[ -z "${no_multi}" ]]; then export FZF_DEFAULT_OPTS="${FZF_DEFAULT_OPTS} --multi" @@ -47,7 +47,7 @@ function set_fzf_multi() { # Outputs: # ${confirm}: y or n indicating user response ####################################### -function get_confirmation() { +get_confirmation() { local confirm local message="${1:-Confirm?}" while [ "${confirm}" != 'y' ] && [ "${confirm}" != 'n' ]; do @@ -67,7 +67,7 @@ function get_confirmation() { # the selected commit 6 char code # e.g. b60b330 ####################################### -function get_commit() { +get_commit() { local header="${1:-select a commit}" local files=("${@:2}") if [[ "${#files[@]}" -eq 0 ]]; then @@ -100,7 +100,7 @@ function get_commit() { # the selected branch name # e.g. master ####################################### -function get_branch() { +get_branch() { local header="${1:-select a branch}" git branch -a \ | awk '{ @@ -140,7 +140,7 @@ function get_branch() { # the selected file path # e.g.$HOME/.config/nvim/init.vim ####################################### -function get_git_file() { +get_git_file() { local mydir local header="${1:-select tracked file}" local print_opt="${2:-full}" @@ -177,14 +177,14 @@ function get_git_file() { # e.g.$HOME/.config/nvim/init.vim ####################################### -function get_modified_file() { +get_modified_file() { local header="${1:-select a modified file}" local display_mode="${2:-all}" local output_format="${3:-name}" set_fzf_multi "$4" git status --porcelain \ | awk -v display_mode="${display_mode}" '{ - if ($0 ~ /^[A-Za-z][A-Za-z].*$/) { + if ($0 ~ /^[[[:alpha:]]{2}.*$/) { print "\033[32m" substr($0, 1, 1) "\033[31m" substr($0, 2) "\033[0m" } else if ($0 ~ /^[A-Za-z][ \t].*$/) { if (display_mode == "all" || display_mode == "staged") { @@ -222,7 +222,7 @@ function get_modified_file() { # the selected stash identifier # e.g. stash@{0} ####################################### -function get_stash() { +get_stash() { local header="${1:-select a stash}" set_fzf_multi "$2" git \ @@ -252,7 +252,7 @@ function get_stash() { # the selected file name with it's line number and line, separated by ":" # e.g. .bash_profile:1:echo hello ####################################### -function grep_words() { +grep_words() { local header="${1:-select matches to edit}" local delimiter="${2:-3}" mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" @@ -274,7 +274,7 @@ function grep_words() { # Outputs: # A user selected file path ####################################### -function search_file() { +search_file() { local search_type="$1" mydir mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" if [[ "${search_type}" == "f" ]]; then diff --git a/flog b/flog @@ -21,7 +21,7 @@ set -ef mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/fgit_helper.sh -function usage() { +usage() { echo -e "Usage: flog [-h] [-r] [-R] [-e] [-c] [-y] ... Interactive log viewer with action menu. @@ -46,7 +46,7 @@ Optional arguments: # Outputs: # ${selected_action}: user selected action ####################################### -function draw_menu() { +draw_menu() { local selected_commit="$1" local selected_action="$2" local menu header message diff --git a/freset b/freset @@ -23,7 +23,7 @@ set -ef mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/fgit_helper.sh -function usage() { +usage() { echo -e "Usage: freset [-h] [-c] [-S] [-H] [-y] ... Reset(unstage) the selected staged files. @@ -41,7 +41,7 @@ Optional arguments: reset_option="--mixed" reset_type="modified" -selected_files=() +typeset -a selected_files=() confirm="" selected_commit="" diff --git a/fstash b/fstash @@ -15,7 +15,7 @@ set -ef mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/fgit_helper.sh -function usage() { +usage() { echo -e "Usage: fstash [-h] [-s] [-d] [-p] ... View and manage stash interactively. @@ -30,7 +30,7 @@ Optional arguments: } stash_command="apply" -selected_files=() +typeset -a selected_files=() selected_stash="" confirm="" diff --git a/fstat b/fstat @@ -15,7 +15,7 @@ set -ef mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/fgit_helper.sh -function usage() { +usage() { echo -e "Usage: fstat [-h] ... Display interactive git status menu. @@ -32,23 +32,25 @@ while [[ "$#" -gt 0 ]]; do esac done +typeset -a mod_files=() + while :; do # reset all variable and arrays for each loop - selected_files=() - selected_filenames=() + typeset -a selected_files=() + typeset -a selected_filenames=() stage_file="" while IFS= read -r line; do selected_files+=("${line}") done < <(get_modified_file "select files to stage/unstage" "all" "raw") - [[ "${#selected_files[@]}" -eq 0 ]] && break + [[ "${#selected_files[@]}" -eq 0 ]] && break; # check if current operation should stage file or unstage file # if any file start with M but has char immediately follow it, new changes are made, stage file # if any file start with a space or tab, the file is not staged, stage file # otherwise, we unstage stage_file=$(printf '%s\n' "${selected_files[@]}" | awk '{ - if ($0 ~ /^[A-Za-z][A-Za-z].*$/) { + if ($0 ~ /^[[:alpha:]]{2}.*$/) { print "stage" } else if ($0 ~ /^[ \t].*$/) { print "stage" @@ -57,6 +59,10 @@ while :; do while IFS= read -r line; do selected_filenames+=("${line}") + # may be a bit much to have two arrays, inside/outside loop + # workaround to have a loop continuously ask + # as well as display git status when finished + mod_files+=("${line}") done < <( printf '%s\n' "${selected_files[@]}" \ | awk -v home="${GTREE}" '{ @@ -73,3 +79,5 @@ while :; do git add "${selected_filenames[@]}" fi done + +[[ "${#mod_files[@]}" -ne 0 ]] && git status -sb || printf "%s" "$(tput setaf 1)Nothing to add$(tput sgr0)" diff --git a/funtrack b/funtrack @@ -14,7 +14,7 @@ set -ef mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/fgit_helper.sh -function usage() { +usage() { echo -e "Usage: funtrack [-h] [-t] [-r] [-y] ... Untrack selected files from git. @@ -36,7 +36,7 @@ Optional arguments: } track_type="untrack" -selected_files=() +typeset -a selected_files=() confirm="" while [[ "$#" -gt 0 ]]; do diff --git a/fzgrep b/fzgrep @@ -13,7 +13,7 @@ set -ef mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/fgit_helper.sh -function usage() { +usage() { echo -e "Usage: fgrep [-h] [-c] [-f] ... Grep words within tracked files and select to edit matches. @@ -26,8 +26,8 @@ Optional arguments: -f, --full\t\tinclude all column during search, as if using '--col 1'." } -selected_lines=() -fzf_search_delimiter=3 +typeset -a selected_lines=() +typeset -i fzf_search_delimiter=3 while [[ "$#" -gt 0 ]]; do case "$1" in diff --git a/preview.sh b/preview.sh @@ -34,7 +34,8 @@ # file will be printed. # Otherwise only the $2-$4 parts of the file will be printed and $3 will be highlighted ####################################### -function display_preview() { + +display_preview() { local fallback_cmd preview_cmd preview_file preview_first preview_last preview_center preview_file="$1" preview_first="$2" @@ -105,7 +106,7 @@ if [ -n "${FZF_PREVIEW_LINES}" ]; then preview_lines="${FZF_PREVIEW_LINES}" else if [ -r /dev/tty ]; then - preview_lines=$(stty size < /dev/tty | awk '{print $1}') + preview_lines=$(stty size < /dev/tty | cut -d' ' -f1) else preview_lines=40 fi