Skip to content

Commit b312d8d

Browse files
committed
Extract constants; rename functions/variables for clarity
This also refactors slightly in other ways, clarifies a comment, and improves an error message.
1 parent ee95497 commit b312d8d

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

etc/copy-packetline.sh

+45-41
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
set -euC -o pipefail
44

5+
readonly source_dir='gix-packetline/src'
6+
readonly target_parent_dir='gix-packetline-blocking'
7+
readonly target_dir="$target_parent_dir/src"
8+
59
function fail () {
610
printf '%s: error: %s\n' "$0" "$1" >&2
711
exit 1
812
}
913

10-
function chdir_toplevel() {
14+
function chdir_toplevel () {
1115
local root_padded root
1216

1317
# Find the working tree's root. (Padding is for the trailing-newline case.)
@@ -29,25 +33,26 @@ function merging () {
2933
test -e "$git_dir/MERGE_HEAD"
3034
}
3135

32-
function target_status () {
33-
git status --short --ignored=traditional -- gix-packetline-blocking/src ||
36+
function target_dir_status () {
37+
git status --short --ignored=traditional -- "$target_dir" ||
3438
fail 'git-status failed'
3539
}
3640

37-
function check_target () {
38-
if ! test -e "gix-packetline-blocking/src"; then
41+
function check_target_dir () {
42+
if ! test -e "$target_dir"; then
3943
# The target does not exist on disk, so nothing will be lost. Proceed.
4044
return
4145
fi
4246

4347
if merging; then
4448
# In a merge, it would be confusing to replace anything at the target.
45-
if target_status | grep -q '^'; then
49+
if target_dir_status | grep -q '^'; then
4650
fail 'target exists, and a merge is in progress'
4751
fi
4852
else
4953
# We can lose data if anything of value at the target is not in the index.
50-
if target_status | grep -q '^.[^ ]'; then
54+
# (Even unstaged deletions, for we can forget what was and wasn't deleted.)
55+
if target_dir_status | grep -q '^.[^ ]'; then
5156
fail 'target exists, with unstaged changes or ignored files'
5257
fi
5358
fi
@@ -73,74 +78,73 @@ function first_line_ends_crlf () {
7378
}
7479

7580
function make_header () {
76-
local source endline
81+
local source_file endline
7782

78-
source="$1"
83+
source_file="$1"
7984
endline="$2"
8085

8186
# shellcheck disable=SC2016 # The backticks are intentionally literal.
8287
printf '//! DO NOT EDIT - this is a copy of %s. Run `just copy-packetline` to update it.%s%s' \
83-
"$source" "$endline" "$endline"
88+
"$source_file" "$endline" "$endline"
8489
}
8590

8691
function copy_with_header () {
87-
local source target endline
92+
local source_file target_file endline
8893

89-
source="$1"
90-
target="$2"
94+
source_file="$1"
95+
target_file="$2"
9196

92-
if first_line_ends_crlf "$source"; then
97+
if first_line_ends_crlf "$source_file"; then
9398
endline=$'\r\n'
9499
else
95100
endline=$'\n'
96101
fi
97102

98-
make_header "$source" "$endline" | cat - -- "$source" >"$target"
103+
make_header "$source_file" "$endline" |
104+
cat - -- "$source_file" >"$target_file"
99105
}
100106

101107
function generate_one () {
102-
local source target
108+
local source_file target_file
103109

104-
source="$1"
105-
target="gix-packetline-blocking/src/${source#gix-packetline/src/}"
110+
source_file="$1"
111+
target_file="$target_dir/${source_file#"$source_dir"/}"
106112

107-
if test -d "$source"; then
108-
mkdir -p -- "$target"
109-
elif test -L "$source"; then
113+
if test -d "$source_file"; then
114+
mkdir -p -- "$target_file"
115+
elif test -L "$source_file"; then
110116
# Cover this case separately, for more useful error messages.
111-
fail "source file is symbolic link: $source"
112-
elif ! test -f "$source"; then
117+
fail "source file is symbolic link: $source_file"
118+
elif ! test -f "$source_file"; then
113119
# This covers less common kinds of files we can't or shouldn't process.
114-
fail "source file neither regular file nor directory: $source"
115-
elif [[ "$source" =~ \.rs$ ]]; then
116-
copy_with_header "$source" "$target"
120+
fail "source file neither regular file nor directory: $source_file"
121+
elif [[ "$source_file" =~ \.rs$ ]]; then
122+
copy_with_header "$source_file" "$target_file"
117123
else
118-
fail "source file not named as Rust source code: $source"
124+
fail "source file not named as Rust source code: $source_file"
119125
fi
120126
}
121127

122128
function generate_all () {
123-
local source
124-
125-
chdir_toplevel
129+
local source_file
126130

127-
if ! test -d gix-packetline/src; then
128-
fail 'no source directory: gix-packetline/src'
131+
if ! test -d "$source_dir"; then
132+
fail "no source directory: $source_dir"
129133
fi
130-
if ! test -d gix-packetline-blocking; then
131-
fail 'no target parent directory: gix-packetline-blocking'
134+
if ! test -d "$target_parent_dir"; then
135+
fail "no target parent directory: $target_parent_dir"
132136
fi
137+
check_target_dir
133138

134-
check_target
135-
136-
rm -rf gix-packetline-blocking/src # No trailing "/" as it may be a symlink.
137-
if test -e gix-packetline-blocking/src; then
138-
fail 'unable to remove target'
139+
rm -rf -- "$target_dir" # It may be a directory, symlink, or regular file.
140+
if test -e "$target_dir"; then
141+
fail 'unable to remove target location'
139142
fi
140143

141-
find gix-packetline/src/ -print0 | while IFS= read -r -d '' source; do
142-
generate_one "$source"
144+
find "$source_dir/" -print0 | while IFS= read -r -d '' source_file; do
145+
generate_one "$source_file"
143146
done
144147
}
145148

149+
chdir_toplevel
146150
generate_all

0 commit comments

Comments
 (0)