Skip to content

Commit 6b5d628

Browse files
hyperupcallvanpipy
authored andcommitted
chore: Improve Bash variously (tj#1032)
1 parent 9636333 commit 6b5d628

20 files changed

+75
-85
lines changed

bin/git-browse

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ if [[ $remote == "" ]]; then
1717
exit 1
1818
fi
1919

20-
# get remote url
21-
remote_url=$(git remote get-url "$remote")
22-
23-
if [[ $? -ne 0 ]]; then
24-
exit $?
25-
fi
20+
remote_url=$(git remote get-url "$remote") || exit $?
2621

2722
if [[ $remote_url = git@* ]]; then
2823
url=$(echo "$remote_url" | sed -E -e 's/:/\//' -e 's/\.git$//' -e 's/.*@(.*)/http:\/\/\1/')

bin/git-browse-ci

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ then
1515
exit 1
1616
fi
1717

18-
remote_url=$(git remote get-url "${remote}")
19-
20-
if [[ $? -ne 0 ]]
21-
then
22-
exit $?
23-
fi
18+
remote_url=$(git remote get-url "${remote}") || exit $?
2419

2520
if [[ $remote_url = git@* ]]
2621
then

bin/git-brv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ for b in "${ordered[@]}"; do
5959
"$color_branch_upstream" "-$uwidth" "${upstream[$b]}" "$reset" \
6060
"$color_diff_commit" "-$hwidth" "${hash[$b]}" "$reset" \
6161
"$msg"
62-
done
62+
done

bin/git-bulk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ usage() {
2525
# add another workspace to global git config
2626
function addworkspace {
2727
git config --global bulkworkspaces."$wsname" "$wsdir";
28-
if [ ! -z "$source" ]; then
28+
if [ -n "$source" ]; then
2929
if [ ! -d "$wsdir" ]; then echo 1>&2 "Path of workspace doesn't exist, make it first."; exit 1; fi
3030
regex='http(s)?://|ssh://|(git@)?.*:.*/.*'
3131
if [[ "$source" =~ $regex ]]; then
@@ -118,7 +118,7 @@ function wsnameToCurrent () {
118118

119119
# helper to check number of arguments.
120120
function allowedargcount () {
121-
if [ $paramcount -ne $1 ] && [ $paramcount -ne $2 ]; then
121+
if [ "$paramcount" -ne "$1" ] && [ "$paramcount" -ne "$2" ]; then
122122
echo 1>&2 "error: wrong number of arguments" && usage;
123123
exit 1;
124124
fi
@@ -137,11 +137,11 @@ function executBulkOp () {
137137

138138
allGitFolders=( $(eval find -L . -name ".git") )
139139

140-
for line in ${allGitFolders[@]}; do
140+
for line in "${allGitFolders[@]}"; do
141141
local gitrepodir=${line::${#line}-5} # cut the .git part of find results to have the root git directory of that repository
142142
eval cd "\"$gitrepodir\"" # into git repo location
143143
local curdir=$PWD
144-
local leadingpath=${curdir#${actual}}
144+
local leadingpath=${curdir#"${actual}"}
145145
guardedExecution "$@"
146146
eval cd "\"$rwsdir\"" # back to origin location of last find command
147147
done

bin/git-changelog

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ commitList() {
233233
# fetch our tags
234234
local _ref _date _tag _tab='%x09'
235235
local _tag_regex='tag: *'
236-
while IFS=$'\t' read _ref _date _tag; do
236+
while IFS=$'\t' read -r _ref _date _tag; do
237237
[[ -z "${_tag}" ]] && continue
238238
# strip out tags form ()
239239
# git v2.2.0+ supports '%D', like '%d' without the " (", ")" wrapping. One day we should use it instead.
@@ -376,7 +376,7 @@ _exit() {
376376
# so we need to calculate the total column number(COL_NUM) of header first.
377377
# Why don't we just use the last column?
378378
# Because the body of CMD column may contain space and be treated as multiple fields.
379-
pid_list=( $(ps -f |
379+
pid_list=( $(ps -f |
380380
awk -v ppid=$$ 'NR == 1 {
381381
COL_NUM = NF
382382
}
@@ -391,7 +391,7 @@ _exit() {
391391
local _pid
392392
for _pid in "${pid_list[@]}"; do
393393
echo "killing: ${_pid}"
394-
kill -TERM ${_pid}
394+
kill -TERM "${_pid}"
395395
done
396396

397397
wait; stty sane; exit 1
@@ -474,10 +474,10 @@ main() {
474474
esac
475475
shift
476476
done
477-
477+
478478
# The default log format unless already set
479479
[[ -z "$CUR_GIT_LOG_FORMAT" ]] && CUR_GIT_LOG_FORMAT="$GIT_LOG_FORMAT"
480-
480+
481481
local _tag="$(_valueForKeyFakeAssocArray "start_tag" "${option[*]}")"
482482
local start_commit="$(_valueForKeyFakeAssocArray "start_commit" "${option[*]}")"
483483

@@ -487,7 +487,6 @@ main() {
487487
return 1
488488
fi
489489

490-
start_commit="$start_commit"
491490
start_tag="$(git describe --tags --contains "$start_commit" 2>/dev/null || echo 'null')"
492491
if [[ -z "$start_tag" ]]; then
493492
_error "Could find the associative tag for the start-commit!"
@@ -569,12 +568,11 @@ main() {
569568
else
570569
cp -f "$tmpfile" "$changelog"
571570
rm -f "$tmpfile"
572-
# Use `eval` to handle GIT_EDITOR which contains space and options,
573-
# like ""C:\Program Files (x86)\Notepad++\notepad++.exe" -multiInst ".
574-
# Thanks @JanSchulz to inspire me this solution
575-
[[ -n "$GIT_EDITOR" ]] && eval $GIT_EDITOR "$changelog"
576-
if [[ $? -ne 0 ]]; then
577-
_exit
571+
572+
if [[ -n "$GIT_EDITOR" ]]; then
573+
$GIT_EDITOR "$changelog" || _exit
574+
else
575+
less "$changelog" || _exit
578576
fi
579577
fi
580578

bin/git-delete-merged-branches

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
branches=$(git branch --no-color --merged | grep -vE "^(\*|\+)" | grep -v $(git_extra_default_branch) | grep -v svn)
3+
branches=$(git branch --no-color --merged | grep -vE "^(\*|\+)" | grep -v "$(git_extra_default_branch)" | grep -v svn)
44
if [ -n "$branches" ]
55
then
66
echo "$branches" | xargs -n 1 git branch -d

bin/git-delete-squashed-branches

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fi
1212
git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do
1313
mergeBase=$(git merge-base "$targetBranch" "$branch")
1414

15-
if [[ $(git cherry "$targetBranch" $(git commit-tree $(git rev-parse $branch\^{tree}) -p $mergeBase -m _)) == "-"* ]]; then
15+
if [[ $(git cherry "$targetBranch" "$(git commit-tree "$(git rev-parse "$branch^{tree}")" -p "$mergeBase" -m _)") == "-"* ]]; then
1616
git branch -D "$branch"
1717
fi
1818
done

bin/git-delete-tag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
# Assert there is at least one tag provided
4-
test -z $1 && echo "tag required." 1>&2 && exit 1
4+
test -z "$1" && echo "tag required." 1>&2 && exit 1
55

66
# Detect the default remote exists or not
77
default_remote=$(git config git-extras.default-remote)

bin/git-effort

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ active_days() {
5959

6060
color_for() {
6161
if [ "$to_tty" = true ]; then
62-
if [ $1 -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
63-
elif [ $1 -gt 150 ]; then color="$(tputq setaf 1)" # red
64-
elif [ $1 -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
65-
elif [ $1 -gt 100 ]; then color="$(tputq setaf 2)" # green
66-
elif [ $1 -gt 75 ]; then color="$(tputq setaf 5)$(tputq bold)"
67-
elif [ $1 -gt 50 ]; then color="$(tputq setaf 5)" # purplish
68-
elif [ $1 -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
69-
elif [ $1 -gt 10 ]; then color="$(tputq setaf 3)" # yellow
62+
if [ "$1" -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
63+
elif [ "$1" -gt 150 ]; then color="$(tputq setaf 1)" # red
64+
elif [ "$1" -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
65+
elif [ "$1" -gt 100 ]; then color="$(tputq setaf 2)" # green
66+
elif [ "$1" -gt 75 ]; then color="$(tputq setaf 5)$(tputq bold)"
67+
elif [ "$1" -gt 50 ]; then color="$(tputq setaf 5)" # purplish
68+
elif [ "$1" -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
69+
elif [ "$1" -gt 10 ]; then color="$(tputq setaf 3)" # yellow
7070
else color="$(tputq sgr0)" # default color
7171
fi
7272
else
@@ -84,20 +84,21 @@ effort() {
8484
local color reset_color commits len dot f_dot i msg active
8585
reset_color=""
8686
test "$to_tty" = true && reset_color="$(tputq sgr0)"
87-
commit_dates=$(dates "$path")
88-
[ $? -gt 0 ] && exit 255
87+
if ! commit_dates=$(dates "$path"); then
88+
exit 255
89+
fi
8990

9091
# Ensure it's not just an empty line
91-
if [ -z "$(head -c 1 <<<$(echo $commit_dates))" ]
92+
if [ -z "$(head -c 1 <<<"$commit_dates")" ]
9293
then
9394
exit 0
9495
fi
9596

96-
commits=$(wc -l <<<"$(echo "$commit_dates")")
97+
commits=$(wc -l <<<"$commit_dates")
9798
color='90'
9899

99100
# ignore <= --above
100-
test $commits -le $above && exit 0
101+
test "$commits" -le "$above" && exit 0
101102

102103
# commits
103104
color_for $(( commits - above ))
@@ -109,12 +110,12 @@ effort() {
109110
i=$((i+1))
110111
done
111112

112-
msg=$(printf " ${color}%s %-10d" "$f_dot" $commits)
113+
msg=$(printf " ${color}%s %-10d" "$f_dot" "$commits")
113114

114115
# active days
115116
active=$(active_days "$commit_dates")
116117
color_for $(( active - above ))
117-
msg="$msg $(printf "${color} %d${reset_color}\n" $active)"
118+
msg="$msg $(printf "${color} %d${reset_color}\n" "$active")"
118119
echo "$msg"
119120
}
120121

bin/git-feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ do
1717
shift # shift -a|-alias
1818
branch_prefix=$1
1919
else
20-
argv+=($1) # treat tail '-a' as <name>
20+
argv+=("$1") # treat tail '-a' as <name>
2121
fi
2222
;;
2323
-r|--remote )
@@ -37,7 +37,7 @@ do
3737
shift
3838
;;
3939
* )
40-
argv+=($1)
40+
argv+=("$1")
4141
;;
4242
esac
4343
shift

0 commit comments

Comments
 (0)