From: alex Date: Thu, 23 Sep 2021 03:32:12 +0000 (-0700) Subject: minor updates X-Git-Tag: v1.0.0^0 X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=d462d20baa4906c8cf8984604ed325eee39aca59;p=git-extra minor updates added support for bash completion cosmetic improvements to git-extra --- diff --git a/Makefile b/Makefile index c36ab6c..51dc802 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ PREFIX ?= /usr/local BINDIR ?= $(PREFIX)/bin +BASHCOMPPREFIX ?= /usr +BASHCOMPDIR ?= $(BASHCOMPPREFIX)/share/bash-completion/completions + all: @echo "git-extra is a shell script; nothing to make. Try 'make install' instead." @@ -13,6 +16,7 @@ clean: install: @install -v -d "$(BINDIR)/" && install -m 0755 -v git-extra "$(BINDIR)/git-ex" + @install -v -d "$(BASHCOMPDIR)/" && install -m 0644 -v git-extra.bash-completion "$(BASHCOMPDIR)/git-ex" uninstall: @rm -vrf \ diff --git a/git-extra b/git-extra index f566e17..a581b82 100644 --- a/git-extra +++ b/git-extra @@ -144,8 +144,8 @@ parse_arguments() { branch_create() { log_info "creating branch $BRANCH" git check-ref-format --branch "$BRANCH" > /dev/null - HASH=`echo "" | git mktree --batch` - TREE_HASH=`git commit-tree -m "init" "$HASH"` + local HASH=`echo "" | git mktree --batch` + local TREE_HASH=`git commit-tree -m "init" "$HASH"` git update-ref "refs/heads/$BRANCH" "$TREE_HASH" } @@ -180,12 +180,12 @@ add_to_index() { # fills PREV_COMMIT with $BRANCH's previous commit hash branch_hash - hash=`git hash-object "$1" -w` + local hash=`git hash-object "$1" -w` rm "$1" git update-index --add --cacheinfo 100644 "$hash" "$2" - tree_hash=`git write-tree` - commit_hash=`git commit-tree -m "${MESSAGE:-updated using git-extra}" -p "$PREV_COMMIT" "$tree_hash"` + local tree_hash=`git write-tree` + local commit_hash=`git commit-tree -m "${MESSAGE:-updated using git-extra}" -p "$PREV_COMMIT" "$tree_hash"` git update-ref "refs/heads/$BRANCH" "$commit_hash" @@ -205,7 +205,7 @@ add() { usage fi - tmpfile=`mktemp` + local tmpfile=`mktemp` if [[ -n "$TEMPLATE" ]]; then if [[ -r "$TEMPLATE" ]]; then @@ -226,7 +226,7 @@ add() { cat_file() { branch_exists - HASH=`git ls-tree "$BRANCH" "$1" | awk -F ' ' '{print $3}'` + local HASH=`git ls-tree "$BRANCH" "$1" | awk -F ' ' '{print $3}'` if [[ -z "$HASH" ]]; then log_err "$1 doesn't exist" exit 1 @@ -240,7 +240,7 @@ cat_file() { } edit() { - tmpfile=`mktemp` + local tmpfile=`mktemp` log_info "editing $1 using $tmpfile" @@ -256,7 +256,7 @@ edit() { editor() { log_info "opening temporary file '$1'" - pre_edit_hash=`b2sum $1` + local pre_edit_hash=`b2sum $1` if [[ -z "$EDITOR" ]]; then log_info "no editor found, checking git config" @@ -269,10 +269,10 @@ editor() { fi fi - CMD=`printf "$EDITOR %s\n" "$1"` + local CMD=`printf "$EDITOR %s\n" "$1"` eval "$CMD" - post_edit_hash=`b2sum $1` + local post_edit_hash=`b2sum $1` if [[ "$pre_edit_hash" == "$post_edit_hash" ]]; then log_err "no changes detected" diff --git a/git-extra.bash-completion b/git-extra.bash-completion new file mode 100644 index 0000000..55c28af --- /dev/null +++ b/git-extra.bash-completion @@ -0,0 +1,24 @@ +# bash completions when called as own excutable, as 'git-ex' +_git-ex() { + IFS=$'\n' + COMPREPLY=() + local commands=$'add\ncat\nedit\nls' + case "$3" in + cat|edit|ls) + COMPREPLY+=($(git-ex ls | grep "$2")) + ;; + git-ex|ex) + COMPREPLY+=($(compgen -W "${commands}" -- "$2")) + ;; + esac +} + +complete -F _git-ex git-ex + +# completions called by git's completions script +_git_ex() { + compopt +o nospace + _git-ex "git-ex" "${COMP_WORDS[$COMP_CWORD]}" "${COMP_WORDS[($COMP_CWORD-1)]}" +} + +