chore: Improve Bash variously (#1032)

This commit is contained in:
Edwin Kofler 2023-02-20 18:03:56 -08:00 committed by GitHub
parent a008308267
commit b372bab52a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 75 additions and 85 deletions

View file

@ -17,12 +17,7 @@ if [[ $remote == "" ]]; then
exit 1
fi
# get remote url
remote_url=$(git remote get-url "$remote")
if [[ $? -ne 0 ]]; then
exit $?
fi
remote_url=$(git remote get-url "$remote") || exit $?
if [[ $remote_url = git@* ]]; then
url=$(echo "$remote_url" | sed -E -e 's/:/\//' -e 's/\.git$//' -e 's/.*@(.*)/http:\/\/\1/')

View file

@ -15,12 +15,7 @@ then
exit 1
fi
remote_url=$(git remote get-url "${remote}")
if [[ $? -ne 0 ]]
then
exit $?
fi
remote_url=$(git remote get-url "${remote}") || exit $?
if [[ $remote_url = git@* ]]
then

View file

@ -59,4 +59,4 @@ for b in "${ordered[@]}"; do
"$color_branch_upstream" "-$uwidth" "${upstream[$b]}" "$reset" \
"$color_diff_commit" "-$hwidth" "${hash[$b]}" "$reset" \
"$msg"
done
done

View file

@ -25,7 +25,7 @@ usage() {
# add another workspace to global git config
function addworkspace {
git config --global bulkworkspaces."$wsname" "$wsdir";
if [ ! -z "$source" ]; then
if [ -n "$source" ]; then
if [ ! -d "$wsdir" ]; then echo 1>&2 "Path of workspace doesn't exist, make it first."; exit 1; fi
regex='http(s)?://|ssh://|(git@)?.*:.*/.*'
if [[ "$source" =~ $regex ]]; then
@ -118,7 +118,7 @@ function wsnameToCurrent () {
# helper to check number of arguments.
function allowedargcount () {
if [ $paramcount -ne $1 ] && [ $paramcount -ne $2 ]; then
if [ "$paramcount" -ne "$1" ] && [ "$paramcount" -ne "$2" ]; then
echo 1>&2 "error: wrong number of arguments" && usage;
exit 1;
fi
@ -137,11 +137,11 @@ function executBulkOp () {
allGitFolders=( $(eval find -L . -name ".git") )
for line in ${allGitFolders[@]}; do
for line in "${allGitFolders[@]}"; do
local gitrepodir=${line::${#line}-5} # cut the .git part of find results to have the root git directory of that repository
eval cd "\"$gitrepodir\"" # into git repo location
local curdir=$PWD
local leadingpath=${curdir#${actual}}
local leadingpath=${curdir#"${actual}"}
guardedExecution "$@"
eval cd "\"$rwsdir\"" # back to origin location of last find command
done

View file

@ -233,7 +233,7 @@ commitList() {
# fetch our tags
local _ref _date _tag _tab='%x09'
local _tag_regex='tag: *'
while IFS=$'\t' read _ref _date _tag; do
while IFS=$'\t' read -r _ref _date _tag; do
[[ -z "${_tag}" ]] && continue
# strip out tags form ()
# git v2.2.0+ supports '%D', like '%d' without the " (", ")" wrapping. One day we should use it instead.
@ -376,7 +376,7 @@ _exit() {
# so we need to calculate the total column number(COL_NUM) of header first.
# Why don't we just use the last column?
# Because the body of CMD column may contain space and be treated as multiple fields.
pid_list=( $(ps -f |
pid_list=( $(ps -f |
awk -v ppid=$$ 'NR == 1 {
COL_NUM = NF
}
@ -391,7 +391,7 @@ _exit() {
local _pid
for _pid in "${pid_list[@]}"; do
echo "killing: ${_pid}"
kill -TERM ${_pid}
kill -TERM "${_pid}"
done
wait; stty sane; exit 1
@ -474,10 +474,10 @@ main() {
esac
shift
done
# The default log format unless already set
[[ -z "$CUR_GIT_LOG_FORMAT" ]] && CUR_GIT_LOG_FORMAT="$GIT_LOG_FORMAT"
local _tag="$(_valueForKeyFakeAssocArray "start_tag" "${option[*]}")"
local start_commit="$(_valueForKeyFakeAssocArray "start_commit" "${option[*]}")"
@ -487,7 +487,6 @@ main() {
return 1
fi
start_commit="$start_commit"
start_tag="$(git describe --tags --contains "$start_commit" 2>/dev/null || echo 'null')"
if [[ -z "$start_tag" ]]; then
_error "Could find the associative tag for the start-commit!"
@ -569,12 +568,11 @@ main() {
else
cp -f "$tmpfile" "$changelog"
rm -f "$tmpfile"
# Use `eval` to handle GIT_EDITOR which contains space and options,
# like ""C:\Program Files (x86)\Notepad++\notepad++.exe" -multiInst ".
# Thanks @JanSchulz to inspire me this solution
[[ -n "$GIT_EDITOR" ]] && eval $GIT_EDITOR "$changelog"
if [[ $? -ne 0 ]]; then
_exit
if [[ -n "$GIT_EDITOR" ]]; then
$GIT_EDITOR "$changelog" || _exit
else
less "$changelog" || _exit
fi
fi

View file

@ -1,6 +1,6 @@
#!/usr/bin/env bash
branches=$(git branch --no-color --merged | grep -vE "^(\*|\+)" | grep -v $(git_extra_default_branch) | grep -v svn)
branches=$(git branch --no-color --merged | grep -vE "^(\*|\+)" | grep -v "$(git_extra_default_branch)" | grep -v svn)
if [ -n "$branches" ]
then
echo "$branches" | xargs -n 1 git branch -d

View file

@ -12,7 +12,7 @@ fi
git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do
mergeBase=$(git merge-base "$targetBranch" "$branch")
if [[ $(git cherry "$targetBranch" $(git commit-tree $(git rev-parse $branch\^{tree}) -p $mergeBase -m _)) == "-"* ]]; then
if [[ $(git cherry "$targetBranch" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]]; then
git branch -D "$branch"
fi
done

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Assert there is at least one tag provided
test -z $1 && echo "tag required." 1>&2 && exit 1
test -z "$1" && echo "tag required." 1>&2 && exit 1
# Detect the default remote exists or not
default_remote=$(git config git-extras.default-remote)

View file

@ -59,14 +59,14 @@ active_days() {
color_for() {
if [ "$to_tty" = true ]; then
if [ $1 -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
elif [ $1 -gt 150 ]; then color="$(tputq setaf 1)" # red
elif [ $1 -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
elif [ $1 -gt 100 ]; then color="$(tputq setaf 2)" # green
elif [ $1 -gt 75 ]; then color="$(tputq setaf 5)$(tputq bold)"
elif [ $1 -gt 50 ]; then color="$(tputq setaf 5)" # purplish
elif [ $1 -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
elif [ $1 -gt 10 ]; then color="$(tputq setaf 3)" # yellow
if [ "$1" -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
elif [ "$1" -gt 150 ]; then color="$(tputq setaf 1)" # red
elif [ "$1" -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
elif [ "$1" -gt 100 ]; then color="$(tputq setaf 2)" # green
elif [ "$1" -gt 75 ]; then color="$(tputq setaf 5)$(tputq bold)"
elif [ "$1" -gt 50 ]; then color="$(tputq setaf 5)" # purplish
elif [ "$1" -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
elif [ "$1" -gt 10 ]; then color="$(tputq setaf 3)" # yellow
else color="$(tputq sgr0)" # default color
fi
else
@ -84,20 +84,21 @@ effort() {
local color reset_color commits len dot f_dot i msg active
reset_color=""
test "$to_tty" = true && reset_color="$(tputq sgr0)"
commit_dates=$(dates "$path")
[ $? -gt 0 ] && exit 255
if ! commit_dates=$(dates "$path"); then
exit 255
fi
# Ensure it's not just an empty line
if [ -z "$(head -c 1 <<<$(echo $commit_dates))" ]
if [ -z "$(head -c 1 <<<"$commit_dates")" ]
then
exit 0
fi
commits=$(wc -l <<<"$(echo "$commit_dates")")
commits=$(wc -l <<<"$commit_dates")
color='90'
# ignore <= --above
test $commits -le $above && exit 0
test "$commits" -le "$above" && exit 0
# commits
color_for $(( commits - above ))
@ -109,12 +110,12 @@ effort() {
i=$((i+1))
done
msg=$(printf " ${color}%s %-10d" "$f_dot" $commits)
msg=$(printf " ${color}%s %-10d" "$f_dot" "$commits")
# active days
active=$(active_days "$commit_dates")
color_for $(( active - above ))
msg="$msg $(printf "${color} %d${reset_color}\n" $active)"
msg="$msg $(printf "${color} %d${reset_color}\n" "$active")"
echo "$msg"
}

View file

@ -17,7 +17,7 @@ do
shift # shift -a|-alias
branch_prefix=$1
else
argv+=($1) # treat tail '-a' as <name>
argv+=("$1") # treat tail '-a' as <name>
fi
;;
-r|--remote )
@ -37,7 +37,7 @@ do
shift
;;
* )
argv+=($1)
argv+=("$1")
;;
esac
shift

View file

@ -67,17 +67,17 @@ main() {
# Delete all remotes
for remote in $(git remote); do
git remote rm ${remote}
git remote rm "${remote}"
done
# Add origin
git remote add origin ${remote_url}
git remote add origin "${remote_url}"
git fetch origin
# Set default branch
if [ -z "${branch:-}" ]; then
branch=$(LC_ALL=C git remote show origin | grep -oP '(?<=HEAD branch: )[^ ]+$')
git remote set-head origin ${branch}
git remote set-head origin "${branch}"
else
git remote set-head origin -a
fi
@ -87,8 +87,8 @@ main() {
git reset --hard HEAD
# Get on the desired branch
git checkout ${branch}
git reset --hard origin/${branch}
git checkout "${branch}"
git reset --hard "origin/${branch}"
# Delete all other branches
branches=$(git branch | grep -v \* | xargs)
@ -97,7 +97,7 @@ main() {
fi
)
elif [ -n "${branch:-}" ]; then
git clone -b ${branch} "${remote_url}" "${destination_path}"
git clone -b "${branch}" "${remote_url}" "${destination_path}"
else
git clone "${remote_url}" "${destination_path}"
fi

View file

@ -35,13 +35,14 @@ fi
[[ -z "$project" || -z "$owner" ]] && abort "github repo needs to be specified as an argument"
# create fork
curl -qsf \
if ! curl -qsf \
-X POST \
-u "$user:$github_personal_access_token" \
-H "X-GitHub-OTP: $MFA_CODE" \
"https://api.github.com/repos/$owner/$project/forks"
[ $? = 0 ] || abort "fork failed"
then
abort "fork failed"
fi
echo "Add GitHub remote branch via SSH (you will be prompted to verify the server's credentials)? (y/n)"
read -r use_ssh

View file

@ -46,12 +46,11 @@ search() {
}
print_last_modified_time() {
gi_list_date=$(stat -c "%y" "$default_path" 2> /dev/null)
if [ "$?" -ne 0 ]; then
gi_list_date=$(stat -f "%t%Sm" "$default_path" 2> /dev/null)
if [ "$?" -ne 0 ]; then
gi_list_date=$(date -r "$default_path" +%s 2> /dev/null)
[ "$?" -ne 0 ] && gi_list_date=0
if ! gi_list_date=$(stat -c "%y" "$default_path" 2> /dev/null); then
if ! gi_list_date=$(stat -f "%t%Sm" "$default_path" 2> /dev/null); then
if ! gi_list_date=$(date -r "$default_path" +%s 2> /dev/null); then
gi_list_date=0
fi
fi
fi
echo "Last update time: $gi_list_date"

View file

@ -12,10 +12,10 @@ pull() {
ref="refs/pull/$id/merge"
fi
git fetch -fu $remote $ref:$branch && \
git checkout $branch && \
git config --local --replace branch.$branch.merge $ref && \
git config --local --replace branch.$branch.remote $remote;
git fetch -fu "$remote" "$ref:$branch" && \
git checkout "$branch" && \
git config --local --replace "branch.$branch.merge" "$ref" && \
git config --local --replace "branch.$branch.remote" "$remote"
}
pull_pr_if_matched() {
@ -54,8 +54,8 @@ done
test -z "$1" && echo "pr number required." 1>&2 && exit 1
if test "$1" = "clean"; then
git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref; do
git branch -D ${ref#refs/heads/}
git for-each-ref refs/heads/pr/* --format='%(refname)' | while read -r ref; do
git branch -D "${ref#refs/heads/}"
done
elif [[ "$1" =~ ^[0-9]+$ ]]; then

View file

@ -50,7 +50,7 @@ fi
if [[ "$CONTINUE" == "yes" ]]; then
TARGET_BRANCH=$(current_branch)
set $(echo "$TARGET_BRANCH" | sed -e "s/-rebased-on-top-of-/\n/g")
set "$(echo "$TARGET_BRANCH" | sed -e "s/-rebased-on-top-of-/\n/g")"
if [[ $# != 2 ]]; then
echo "Couldn't continue rebasing on ${TARGET_BRANCH}"
exit 30 # Impossible to detect PRIMARY_BRANCH AND SECONDARY_BRANCH

View file

@ -18,7 +18,7 @@ function _test_git_scp()
function set_remote()
{
remote=$1
if [ $(git remote | grep -c -- ^$remote$) -eq 0 ]
if [ "$(git remote | grep -c -- "^$remote$")" -eq 0 ]
then
COLOR_RED
echo "Remote $remote does not exist in your git config"
@ -39,8 +39,9 @@ function php_lint()
test ! -f "$i" && continue
case "$i" in
*\.php|*\.phtml)
php -l "$i" > /dev/null
[ $? -gt 0 ] && error_count[${#error_count[@]}]="$i"
if ! php -l "$i" > /dev/null; then
error_count[${#error_count[@]}]="$i"
fi
;;
esac
done
@ -83,7 +84,7 @@ function scp_and_stage
if [ -n "$refhead" ]
then
shift
[ $(git branch --contains "$refhead" | grep -c '\*') -eq 0 ] &&
[ "$(git branch --contains "$refhead" | grep -c '\*')" -eq 0 ] &&
_error "refhead provided is not part of current branch"
fi
@ -99,8 +100,8 @@ function scp_and_stage
list=$(git diff --name-only)
fi
deleted=$(for i in $list; do [ -f $i ] || echo $i; done)
list=$(for i in $list; do [ -f $i ] && echo $i; done)
deleted=$(for i in $list; do [ -f "$i" ] || echo "$i"; done)
list=$(for i in $list; do [ -f "$i" ] && echo "$i"; done)
if [ -n "$list" ]
then
@ -113,12 +114,12 @@ function scp_and_stage
rm "$_TMP"
fi
deleted=$(for i in $deleted; do echo $(git config remote.$remote.url | cut -d: -f2)/$i; done)
deleted=$(for i in $deleted; do echo "$(git config "remote.$remote.url" | cut -d: -f2)/$i"; done)
[ -n "$deleted" ] &&
COLOR_RED &&
echo Deleted remote files &&
ssh $(git config remote.$remote.url | cut -d: -f1) -t "rm $deleted" &&
ssh "$(git config "remote.$remote.url" | cut -d: -f1)" -t "rm $deleted" &&
echo "$deleted"
COLOR_RESET
}

View file

@ -210,7 +210,7 @@ GIT_LOG_COMMAND="git --no-pager log \
--color=$COLOR
--pretty=format:'$GIT_PRETTY_FORMAT'
--date='$GIT_DATE_FORMAT'"
if [[ ! -z "$MAX_COMMIT_NUM" ]]; then
if [[ -n "$MAX_COMMIT_NUM" ]]; then
GIT_LOG_COMMAND="$GIT_LOG_COMMAND --max-count=$MAX_COMMIT_NUM"
fi
@ -221,7 +221,7 @@ git_output() {
for branch in $branches; do
# shellcheck disable=SC2086
if output=$(eval $GIT_LOG_COMMAND "$branch"); then
if [[ ! -z "$output" ]] ; then
if [[ -n "$output" ]] ; then
echo "${GREEN}${branch}${NORMAL}"
echo "$output"
echo ""

View file

@ -212,9 +212,9 @@ print_summary_by_line() {
print_summary() {
if [ "$OUTPUT_STYLE" == "tabular" ]; then
tabular_headers="# Repo $SP Age $SP Last active $SP Active on $SP Commits $SP Uncommitted"
echo -e "$tabular_headers\n$project $SP $(repository_age) $SP $(last_active) $SP $(active_days $commit) days $SP $(commit_count $commit) $SP $(uncommitted_changes_count)" | column -t -s "$COLUMN_CMD_DELIMTER"
echo -e "$tabular_headers\n$project $SP $(repository_age) $SP $(last_active) $SP $(active_days "$commit") days $SP $(commit_count "$commit") $SP $(uncommitted_changes_count)" | column -t -s "$COLUMN_CMD_DELIMTER"
elif [ "$OUTPUT_STYLE" == "oneline" ]; then
echo "$project / age: $(repository_age) / last active: $(last_active) / active on $(active_days $commit) days / commits: $(commit_count $commit) / uncommitted: $(uncommitted_changes_count)"
echo "$project / age: $(repository_age) / last active: $(last_active) / active on $(active_days "$commit") days / commits: $(commit_count "$commit") / uncommitted: $(uncommitted_changes_count)"
else
echo
echo " project : $project"

View file

@ -69,4 +69,4 @@ if [[ ! $parm2 =~ ^[1-9][0-9]*$ ]]; then
exit 1
fi
_undo $parm1 $parm2 $parm3
_undo "$parm1" "$parm2" "$parm3"

View file

@ -39,5 +39,5 @@ else
<(git status -z --porcelain | tr '\0' '\n' | cut -c 4- | sort) \
| cut -c ${#prefix}- \
| tr '\n' '\0' \
| xargs -0 -P${NPROC:-1} -n 24 $opt_r git utimes --touch "$1"
| xargs -0 -P"${NPROC:-1}" -n 24 $opt_r git utimes --touch "$1"
fi