From 60ae39e09cfc64765f32d580e5c6854ed0cccabb Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 28 Oct 2022 21:35:07 -0700 Subject: [PATCH] lint with shellcheck --- git-extra | 69 ++++++++++++++++++++++++--------------- git-extra.bash-completion | 6 ++-- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/git-extra b/git-extra index 6cec469..adb3e96 100644 --- a/git-extra +++ b/git-extra @@ -12,13 +12,12 @@ TEMPLATE= # helper variables LONG_OUTPUT=false POSITIONAL_ARGUMENTS= -PREFIX= PREV_COMMIT= -UTILITY=ls +UTILITY="ls" WORKING_BRANCH= detect_git() { - if [[ -z "`git --version`" ]]; then + if [[ -z "$(git --version)" ]]; then log_err "git not found in path" exit 1 fi @@ -26,8 +25,7 @@ detect_git() { detect_commits() { set +e - git rev-list --count HEAD > /dev/null 2>&1 - if [[ $? -ne 0 ]]; then + if ! git rev-list --count HEAD > /dev/null 2>&1; then log_err "no commits found, create initial commit first" exit 1 fi @@ -133,24 +131,29 @@ parse_arguments() { esac done - shift $(expr $OPTIND - 1) # remove options + shift $(( OPTIND - 1 )) # remove options if [[ "$#" -gt 0 ]]; then UTILITY="$1" shift - POSITIONAL_ARGUMENTS="$@" + POSITIONAL_ARGUMENTS="$*" fi } branch_create() { + local HASH + local TREE_HASH + log_info "creating branch $BRANCH" git check-ref-format --branch "$BRANCH" > /dev/null - local HASH=`echo "" | git mktree --batch` - local TREE_HASH=`git commit-tree -m "init" "$HASH"` + + HASH=$(echo "" | git mktree --batch) + TREE_HASH=$(git commit-tree -m "init" "$HASH") + git update-ref "refs/heads/$BRANCH" "$TREE_HASH" } branch_exists() { - if test $(git branch --list "$BRANCH" | wc -c) == 0; then + if test "$(git branch --list "$BRANCH" | wc -c)" == 0; then branch_create fi } @@ -159,12 +162,12 @@ branch_hash() { # changes to extra branch and stores prevous commit hash in PREV_COMMIT branch_exists - WORKING_BRANCH=`git rev-parse --verify HEAD --abbrev-ref` + WORKING_BRANCH=$(git rev-parse --verify HEAD --abbrev-ref) git symbolic-ref HEAD "refs/heads/$BRANCH" git reset > /dev/null - PREV_COMMIT=`git rev-parse --verify HEAD` + PREV_COMMIT=$(git rev-parse --verify HEAD) } branch_reset() { @@ -175,17 +178,21 @@ branch_reset() { } add_to_index() { + local commit_hash + local hash + local tree_hash + log_info "adding $2 to index" # fills PREV_COMMIT with $BRANCH's previous commit hash branch_hash - local hash=`git hash-object "$1" -w` + hash=$(git hash-object "$1" -w) rm "$1" git update-index --add --cacheinfo 100644 "$hash" "$2" - local tree_hash=`git write-tree` - local commit_hash=`git commit-tree -m "${MESSAGE:-updated using git-extra}" -p "$PREV_COMMIT" "$tree_hash"` + tree_hash=$(git write-tree) + commit_hash=$(git commit-tree -m "${MESSAGE:-updated using git-extra}" -p "$PREV_COMMIT" "$tree_hash") git update-ref "refs/heads/$BRANCH" "$commit_hash" @@ -193,6 +200,7 @@ add_to_index() { } add() { + local tmpfile branch_exists if [[ "$#" -ne 1 ]]; then @@ -200,12 +208,12 @@ add() { usage fi - if test $(git ls-tree "$BRANCH" "$1" | wc -c) != 0; then + if test "$(git ls-tree "$BRANCH" "$1" | wc -c)" != 0; then log_err "file '$1' already exists; did you mean to edit '$1' instead?" usage fi - local tmpfile=`mktemp` + tmpfile=$(mktemp) if [[ -n "$TEMPLATE" ]]; then if [[ -r "$TEMPLATE" ]]; then @@ -225,8 +233,9 @@ add() { } cat_file() { + local HASH branch_exists - local HASH=`git ls-tree "$BRANCH" "$1" | awk -F ' ' '{print $3}'` + HASH=$(git ls-tree "$BRANCH" "$1" | awk -F ' ' '{print $3}') if [[ -z "$HASH" ]]; then log_err "$1 doesn't exist" exit 1 @@ -240,13 +249,14 @@ cat_file() { } edit() { - local tmpfile=`mktemp` + local tmpfile + tmpfile=$(mktemp) log_info "editing $1 using $tmpfile" cat_file "$1" "$tmpfile" - editor $tmpfile + editor "$tmpfile" add_to_index "$tmpfile" "$1" @@ -254,14 +264,18 @@ edit() { } editor() { + local CMD + local pre_edit_hash + local post_edit_hash + log_info "opening temporary file '$1'" - local pre_edit_hash=`b2sum $1` + pre_edit_hash=$(b2sum "$1") if [[ -z "$EDITOR" ]]; then log_info "no editor found, checking git config" set +e - EDITOR="`git config core.editor`" + EDITOR="$(git config core.editor)" set -e if [[ -z "$EDITOR" ]]; then log_err "no editor defined; use --editor option, set EDITOR environmental variable, or set git config core.editor." @@ -269,10 +283,10 @@ editor() { fi fi - local CMD=`printf "$EDITOR %s\n" "$1"` + CMD=$(printf "$EDITOR %s\n" "$1") eval "$CMD" - local post_edit_hash=`b2sum $1` + post_edit_hash=$(b2sum "$1") if [[ "$pre_edit_hash" == "$post_edit_hash" ]]; then log_err "no changes detected" @@ -283,10 +297,13 @@ editor() { ls() { branch_exists + local SEARCH + SEARCH="${*:-.}" + if [[ "$LONG_OUTPUT" == "true" ]]; then - git ls-tree "$BRANCH" -r $@ + git ls-tree "$BRANCH" -r "$SEARCH" else - git ls-tree "$BRANCH" -r --name-only $@ + git ls-tree "$BRANCH" -r --name-only "$SEARCH" fi } diff --git a/git-extra.bash-completion b/git-extra.bash-completion index 55c28af..211ad50 100644 --- a/git-extra.bash-completion +++ b/git-extra.bash-completion @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + # bash completions when called as own excutable, as 'git-ex' _git-ex() { IFS=$'\n' @@ -5,10 +7,10 @@ _git-ex() { local commands=$'add\ncat\nedit\nls' case "$3" in cat|edit|ls) - COMPREPLY+=($(git-ex ls | grep "$2")) + read -r -a COMPREPLY <<< "$(git-ex ls | grep "$2")" ;; git-ex|ex) - COMPREPLY+=($(compgen -W "${commands}" -- "$2")) + read -r -a COMPREPLY <<< "$(compgen -W "${commands}" -- "$2")" ;; esac } -- 2.39.5