Skip to content

Commit 47e9402

Browse files
authored
fix: Improve Bash hygiene (#1056)
* fix: Improve Bash hygiene * Update quoting to account for new bugs * quotes * fix: Remove extra shellcheck comment
1 parent 8af4389 commit 47e9402

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

bin/git-authors

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ done
2222

2323
if ! $LIST; then
2424
FILE=$1
25-
if test "$FILE" = ""; then
26-
FILE=$(ls | grep -E 'authors|contributors' -i|head -n1)
27-
if test "$FILE" = ""; then
25+
if [ -z "$FILE" ]; then
26+
FILE=$(find . -mindepth 1 -maxdepth 1 -iregex '.*\(authors\|contributors\).*' | head -n1)
27+
if [ -z "$FILE" ]; then
2828
FILE='AUTHORS'
2929
fi
3030
fi

bin/git-changelog

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ commitList() {
280280
for (( i=0; i<"${_tags_list_keys_length}"; i++ )); do
281281
local __curr_tag="${tags_list_keys[$i]}"
282282
local __prev_tag="${tags_list_keys[$i+1]:-null}"
283-
local __curr_date="$(_valueForKeyFakeAssocArray "${__curr_tag}" "${tags_list[*]}")"
283+
local __curr_date
284+
__curr_date="$(_valueForKeyFakeAssocArray "${__curr_tag}" "${tags_list[*]}")"
284285
__curr_date="${__curr_date##*=>}"
285286

286287
# output latest commits, up until the most-recent tag, these are all
@@ -528,9 +529,10 @@ main() {
528529
#
529530
# generate changelog
530531
#
531-
local tmpfile="$(git_extra_mktemp)"
532-
local changelog="$(_valueForKeyFakeAssocArray "output_file" "${option[*]}")"
533-
local title_tag="$(_valueForKeyFakeAssocArray "title_tag" "${option[*]}")"
532+
local tmpfile changelog title_tag
533+
tmpfile="$(git_extra_mktemp)"
534+
changelog="$(_valueForKeyFakeAssocArray "output_file" "${option[*]}")"
535+
title_tag="$(_valueForKeyFakeAssocArray "title_tag" "${option[*]}")"
534536

535537
if [[ "$(_valueForKeyFakeAssocArray "list_style" "${option[*]}")" == true ]]; then
536538
if [[ "$(_valueForKeyFakeAssocArray "list_all" "${option[*]}")" == true ]]; then
@@ -549,7 +551,7 @@ main() {
549551
fi
550552

551553
if [[ -z "$changelog" ]]; then
552-
changelog="$(ls | grep -E 'change|history' -i | head -n1)"
554+
changelog="$(find . -mindepth 1 -maxdepth 1 -iregex '.*\(change\|history\).*' | head -n1)"
553555
if [[ -z "$changelog" ]]; then
554556
changelog="History.md";
555557
fi

bin/git-guilt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ do
8787
do
8888
printf "+"
8989
done
90-
printf "(%s)" $num
90+
printf "(%s)" "$num"
9191
else
9292
for (( i = 0; i < num; i++ ))
9393
do

bin/git-rebase-patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cleanup() {
2020
rm "$index"
2121
exit 2
2222
}
23-
trap cleanup 2
23+
trap cleanup INT
2424

2525
# Go back in history while parent commits are available.
2626
echo "Trying to find a commit the patch applies to..."

bin/git-scp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ function scp_and_stage
8080
set_remote "$1"
8181
shift
8282

83-
local refhead="$(git rev-parse --quiet --verify "$1")"
83+
local refhead
84+
refhead="$(git rev-parse --quiet --verify "$1")"
8485
if [ -n "$refhead" ]
8586
then
8687
shift

bin/git-summary

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,28 @@ if [ -n "$MERGES_ARG" ] && [ -n "$SUMMARY_BY_LINE" ]; then
4545
exit 1
4646
fi
4747

48-
48+
commit="HEAD"
4949
if [ -n "$SUMMARY_BY_LINE" ]; then
5050
paths=( "$@" )
5151
else
52-
commit="HEAD"
5352
[ $# -ne 0 ] && commit=$*
5453
fi
5554
project=${PWD##*/}
5655

5756
#
5857
# get date for the given <commit>
5958
#
60-
date() {
59+
commit_date() {
6160
# the $1 can be empty
6261
# shellcheck disable=SC2086
63-
git log $MERGES_ARG --pretty='format: %ai' $1 | cut -d ' ' -f 2
62+
git log $MERGES_ARG --pretty='format: %ai' "$1" | cut -d ' ' -f 2
6463
}
6564

6665
#
6766
# get active days for the given <commit>
6867
#
6968
active_days() {
70-
# shellcheck disable=SC2086
71-
date $1 | sort -r | uniq | awk '
69+
commit_date "$1" | sort -r | uniq | awk '
7270
{ sum += 1 }
7371
END { print sum }
7472
'
@@ -79,7 +77,7 @@ active_days() {
7977
#
8078
commit_count() {
8179
# shellcheck disable=SC2086
82-
git log $MERGES_ARG --oneline $commit | wc -l | tr -d ' '
80+
git log $MERGES_ARG --oneline "$commit" | wc -l | tr -d ' '
8381
}
8482

8583
#
@@ -216,19 +214,17 @@ print_summary_by_line() {
216214
print_summary() {
217215
if [ "$OUTPUT_STYLE" == "tabular" ]; then
218216
tabular_headers="# Repo $SP Age $SP Last active $SP Active on $SP Commits $SP Uncommitted $SP Branch"
219-
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) $SP $(current_branch_name)" | column -t -s "$COLUMN_CMD_DELIMTER"
217+
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) $SP $(current_branch_name)" | column -t -s "$COLUMN_CMD_DELIMTER"
220218
elif [ "$OUTPUT_STYLE" == "oneline" ]; then
221-
echo "$project / age: $(repository_age) / last active: $(last_active) / active on $(active_days $commit) days / commits: $(commit_count $commit) / uncommitted: $(uncommitted_changes_count) / branch: $(current_branch_name)"
219+
echo "$project / age: $(repository_age) / last active: $(last_active) / active on $(active_days "$commit") days / commits: $(commit_count "$commit") / uncommitted: $(uncommitted_changes_count) / branch: $(current_branch_name)"
222220
else
223221
echo
224222
echo " project : $project"
225223
echo " repo age : $(repository_age)"
226224
echo " branch: : $(current_branch_name)"
227225
echo " last active : $(last_active)"
228-
# shellcheck disable=SC2086
229-
echo " active on : $(active_days $commit) days"
230-
# shellcheck disable=SC2086
231-
echo " commits : $(commit_count $commit)"
226+
echo " active on : $(active_days "$commit") days"
227+
echo " commits : $(commit_count "$commit")"
232228

233229
# The file count doesn't support passing a git ref so ignore it if a ref is given
234230
if [ "$commit" == "HEAD" ]; then
@@ -239,10 +235,10 @@ print_summary() {
239235
if [ -n "$DEDUP_BY_EMAIL" ]; then
240236
# the $commit can be empty
241237
# shellcheck disable=SC2086
242-
git shortlog $MERGES_ARG -n -s -e $commit | dedup_by_email | format_authors
238+
git shortlog $MERGES_ARG -n -s -e "$commit" | dedup_by_email | format_authors
243239
else
244240
# shellcheck disable=SC2086
245-
git shortlog $MERGES_ARG -n -s $commit | format_authors
241+
git shortlog $MERGES_ARG -n -s "$commit" | format_authors
246242
fi
247243
fi
248244
}

0 commit comments

Comments
 (0)