Skip to content

Commit 7c317cd

Browse files
committed
Address ShellCheck issues
1 parent a639493 commit 7c317cd

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

bin/mkchlog

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@ EOF
3333
}
3434

3535
# Out of the box macOS ships with BSD getopt which doesn't support long args
36-
if [ "$(uname -s)" == "Darwin" ] && $(man getopt | grep -i -q "bsd"); then
36+
if [ "$(uname -s)" == "Darwin" ] && (man getopt | grep -i -q "bsd"); then
3737
echo "Error: This tool requires GNU getopt, but your system is using BSD getopt. Please install GNU getopt and add it to your PATH."
3838
exit 1
3939
fi
4040

41-
ARGS=$(getopt -n mkchlog -o rhncf:u: --long remove,help,no-wrap,commit,feature:username: -- "$@")
42-
if [ $? -ne 0 ]; then
41+
if ! ARGS=$(getopt -n mkchlog -o rhncf:u: --long remove,help,no-wrap,commit,feature:username: -- "$@"); then
4342
exit 1
4443
fi
4544

4645
eval set -- "$ARGS"
47-
while [ : ]; do
46+
while [ $# -gt 0 ]; do
4847
case "$1" in
4948
-h|--help)
5049
usage
@@ -87,22 +86,22 @@ if [[ -z "$1" && -n "$COMMIT" ]]; then
8786
exit 1
8887
fi
8988

90-
GITROOT=`git rev-parse --show-toplevel 2>/dev/null`
91-
if [ $? -ne 0 ]; then
89+
90+
if ! GITROOT=$(git rev-parse --show-toplevel 2>/dev/null); then
9291
echo "Error: not in a git repository."
9392
exit 1
9493
fi
9594

96-
PKG_DIR=`ls -d $GITROOT/{.tito,rel-eng}/packages 2>/dev/null`
97-
if [ -z "$PKG_DIR" ]; then
95+
PKG_DIR=$(ls -d "$GITROOT"/{.tito,rel-eng}/packages 2>/dev/null)
96+
if [ -z "$PKG_DIR" ] ; then
9897
echo "Error: Not in Uyuni working directory."
9998
exit 1
10099
fi
101100

102-
CURDIR=`git rev-parse --show-prefix` || exit 1
101+
CURDIR=$(git rev-parse --show-prefix) || exit 1
103102

104103
if [ -z "$USER" ]; then
105-
GITMAIL=`git config --get user.email 2>/dev/null`
104+
GITMAIL=$(git config --get user.email 2>/dev/null)
106105
if [ -n "$GITMAIL" ]; then
107106
USER=${GITMAIL%@*}
108107
else
@@ -111,14 +110,14 @@ if [ -z "$USER" ]; then
111110
fi
112111

113112
if [ -z "$FEATURE" ]; then
114-
FEATURE=`git rev-parse --abbrev-ref HEAD 2>/dev/null | tr '/' '-'`
113+
FEATURE=$(git rev-parse --abbrev-ref HEAD 2>/dev/null | tr '/' '-')
115114
if [ -z "$FEATURE" ] || [ "HEAD" == "$FEATURE" ]; then
116115
echo "Cannot read the branch name from the current HEAD. Omitting the feature name part."
117116
unset FEATURE
118117
fi
119118
fi
120119

121-
if [ -z $USER ] && [ -z $FEATURE ]; then
120+
if [ -z "$USER" ] && [ -z "$FEATURE" ]; then
122121
echo "Error: Neither username nor branch name could be read. Please specify the values using --feature and --username options or create a changelog file manually."
123122
exit 1
124123
fi
@@ -127,15 +126,18 @@ fi
127126
# Loops through the base directories of each package and tries to match one with the current directory.
128127
# Returns 1 if no match found, which means the user is outside of a package directory.
129128
function getChangelogFile() {
130-
for pkg in $(cat $PKG_DIR/* | cut -d' ' -f2)
129+
for pkg in $(cat "$PKG_DIR"/* | cut -d' ' -f2)
131130
do
132131
if [ "$pkg" = "./" ]; then
133132
pkg=""
134133
fi
135134

136-
if echo $CURDIR | grep -q "^$pkg"; then
137-
local chfile=$(ls $GITROOT/$pkg*.changes)
138-
echo $chfile${USER:+.$USER}${FEATURE:+.$FEATURE}
135+
if echo "$CURDIR" | grep -q "^$pkg"; then
136+
local chfile
137+
138+
chfile=$(ls "$GITROOT"/"$pkg"*.changes)
139+
echo "$chfile${USER:+.$USER}${FEATURE:+.$FEATURE}"
140+
139141
return
140142
fi
141143
done
@@ -148,9 +150,9 @@ function getEditorCmd() {
148150
if [ -n "$EDITOR" ]; then
149151
cmd=$EDITOR
150152
elif command -v vim &>/dev/null; then
151-
cmd=vim
153+
cmd="vim"
152154
else
153-
cmd=vi
155+
cmd="vi"
154156
fi
155157

156158
# Specific CLI options for common text editors
@@ -163,20 +165,19 @@ function getEditorCmd() {
163165
;;
164166
esac
165167

166-
echo $cmd
168+
echo "$cmd"
167169
}
168170

169-
CHFILE=$(getChangelogFile)
170-
if [ $? -ne 0 ]; then
171+
if ! CHFILE=$(getChangelogFile); then
171172
echo "Error: Not in a package directory."
172173
exit 1
173174
fi
174175

175176
# Remove option
176-
if ! [ -z $REMOVE ]; then
177-
if [ -f $CHFILE ]; then
178-
git restore --staged $CHFILE 2>/dev/null
179-
rm $CHFILE 2>/dev/null
177+
if [ -n "$REMOVE" ]; then
178+
if [ -f "$CHFILE" ]; then
179+
git restore --staged "$CHFILE" 2>/dev/null
180+
rm "$CHFILE" 2>/dev/null
180181
exit
181182
else
182183
echo "Error: '$CHFILE' does not exist."
@@ -185,31 +186,31 @@ if ! [ -z $REMOVE ]; then
185186
fi
186187

187188
# Add the new entry
188-
if [ -z $NO_WRAP ]; then
189-
echo "$1" | xargs | fold -s -w 65 | sed '1s/^\(.*\S\)\s*$/- \1/;2,$s/^\(.*\S\)\s*$/ \1/' > $CHFILE.new
189+
if [ -z "$NO_WRAP" ]; then
190+
echo "$1" | xargs | fold -s -w 65 | sed '1s/^\(.*\S\)\s*$/- \1/;2,$s/^\(.*\S\)\s*$/ \1/' > "$CHFILE.new"
190191
else
191-
echo "$1" | xargs | sed 's/^/- /' > $CHFILE.new
192+
echo "$1" | xargs | sed 's/^/- /' > "$CHFILE.new"
192193
fi
193194

194195
# Append older entries
195-
cat $CHFILE >> $CHFILE.new 2>/dev/null
196+
cat "$CHFILE" >> "$CHFILE.new" 2>/dev/null
196197

197198
# Open file for edit
198199
if [ -z "$1" ]; then
199-
$(getEditorCmd) $CHFILE.new
200+
$(getEditorCmd) "$CHFILE.new"
200201
fi
201202

202203
# Move file into place
203-
if [ -s $CHFILE.new ]; then
204-
mv $CHFILE.new $CHFILE
204+
if [ -s "$CHFILE.new" ]; then
205+
mv "$CHFILE.new" "$CHFILE"
205206
# Stage in git
206-
git add $CHFILE
207+
git add "$CHFILE"
207208
if [[ -n "$COMMIT" && -n "$1" ]]; then
208209
git commit -m "$1"
209210
fi
210211
else
211212
# Unstage and remove
212213
echo "No entries written. Discarding the changelog file."
213-
git restore --staged $CHFILE 2>/dev/null
214-
rm $CHFILE.new $CHFILE 2>/dev/null
214+
git restore --staged "$CHFILE" 2>/dev/null
215+
rm "$CHFILE.new" "$CHFILE" 2>/dev/null
215216
fi

0 commit comments

Comments
 (0)