Skip to content

Commit ee95497

Browse files
committed
Simplify script by assuming find supports -print0
`find -print0` is not POSIX, but supported systems include: - GNU/Linux systems, using GNU findutils - Other Linux-based systems using BusyBox find (e.g. Alpine Linux) - BSD systems, including macOS (all versions, since 10) Since this is a Bash script, and even the old versions of Bash on macOS systems support `read -d ''` to read null-byte-delimited input, assuming `find -print0` is supported everywhere needed, this is highly likely to work everywhere needed. It allows filenames to be safely read even if they contain weird characters including newlines, as can happen accidentally (hopefully not intentionally). It would be nicer to loop through the result of a recursive glob (globstar, **), but Bash only supports that starting in version 4, which no version of macOS ships (or is likely to ship). The benefit of this approach, compared to the `find -exec` way used before, is that the code is simpler, no longer needs to call itself as a subprocess through find, and no longer needs a help to clarify a mode that is not meant to be used from the outside. (It may also be slightly faster.) This makes some behavioral changes, in areas where the design had been driven by the implementation rather than the other way around: - We stop on the first failure. - Because of that, there is no need to restate what files could not be generated (it is at most one, and the failure should show it). - As noted above, this script can no longer be invoked to process an individual file (which as not a design goal), and it therefore no longer accepts any command-line arguments (they are ignored). This also fixes a misspelling the big first_line_ends_crlf comment.
1 parent 1125ca0 commit ee95497

File tree

1 file changed

+29
-60
lines changed

1 file changed

+29
-60
lines changed

etc/copy-packetline.sh

+29-60
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
set -euC -o pipefail
44

5-
function usage() {
6-
local name
7-
8-
name="$(basename -- "$0")"
9-
printf '%s [--all] regenerate gix-packetline-blocking source\n' "$name"
10-
printf '%s --file {path} regenerate a single file (avoid; prefer --all)\n' "$name"
11-
printf '%s --help print this message\n' "$name"
12-
}
13-
145
function fail () {
156
printf '%s: error: %s\n' "$0" "$1" >&2
167
exit 1
@@ -62,47 +53,12 @@ function check_target () {
6253
fi
6354
}
6455

65-
function indent () {
66-
sed 's/^/ /'
67-
}
68-
69-
function generate_all () {
70-
local failures
71-
72-
chdir_toplevel
73-
74-
if ! test -d gix-packetline/src; then
75-
fail 'no source directory: gix-packetline/src'
76-
fi
77-
if ! test -d gix-packetline-blocking; then
78-
fail 'no target parent directory: gix-packetline-blocking'
79-
fi
80-
81-
check_target
82-
83-
rm -rf gix-packetline-blocking/src # No trailing /. It may be a symlink.
84-
if test -e gix-packetline-blocking/src; then
85-
fail 'unable to remove target'
86-
fi
87-
88-
failures="$(
89-
find gix-packetline/src/ \
90-
-exec etc/copy-packetline.sh --file {} \; \
91-
-o -print
92-
)"
93-
94-
# If we get here, traversal succeeded, but perhaps some generations failed.
95-
if test -n "$failures"; then
96-
fail $'failed to generate from:\n'"$(indent <<<"$failures")"
97-
fi
98-
}
99-
10056
function first_line_ends_crlf () {
10157
# This is tricky to check portably. In Cygwin-like environments including
10258
# MSYS2 and Git Bash, most text processing tools, including awk, sed, and
10359
# grep, automatically ignore \r before \n. Some ignore \r everywhere. Some
10460
# can be told to keep \r, but in non-portable ways that may affect other
105-
# implementations. Bash ignores \r in some places even without "-o icncr",
61+
# implementations. Bash ignores \r in some places even without "-o igncr",
10662
# and ignores \r even more with it, including in all text from command
10763
# substitution. Simple checks may be non-portable to other OSes. Fortunately,
10864
# tools that treat input as binary data are exempt (even cat, but "-v" is
@@ -118,6 +74,7 @@ function first_line_ends_crlf () {
11874

11975
function make_header () {
12076
local source endline
77+
12178
source="$1"
12279
endline="$2"
12380

@@ -142,14 +99,10 @@ function copy_with_header () {
14299
}
143100

144101
function generate_one () {
145-
local source shared target
102+
local source target
146103

147104
source="$1"
148-
shared="${source#gix-packetline/src/}"
149-
if test "$source" = "$shared"; then
150-
fail "source path seems to be outside gix-packetline/src/: $source"
151-
fi
152-
target="gix-packetline-blocking/src/$shared"
105+
target="gix-packetline-blocking/src/${source#gix-packetline/src/}"
153106

154107
if test -d "$source"; then
155108
mkdir -p -- "$target"
@@ -166,12 +119,28 @@ function generate_one () {
166119
fi
167120
}
168121

169-
if { test "$#" -eq 1 && test "$1" = '--all'; } || test "$#" -eq 0; then
170-
generate_all
171-
elif test "$#" -eq 2 && test "$1" = '--file'; then
172-
generate_one "$2"
173-
elif test "$#" -eq 1 && test "$1" = '--help'; then
174-
usage
175-
else
176-
fail 'unrecognized syntax, try passing only --help for usage'
177-
fi
122+
function generate_all () {
123+
local source
124+
125+
chdir_toplevel
126+
127+
if ! test -d gix-packetline/src; then
128+
fail 'no source directory: gix-packetline/src'
129+
fi
130+
if ! test -d gix-packetline-blocking; then
131+
fail 'no target parent directory: gix-packetline-blocking'
132+
fi
133+
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+
fi
140+
141+
find gix-packetline/src/ -print0 | while IFS= read -r -d '' source; do
142+
generate_one "$source"
143+
done
144+
}
145+
146+
generate_all

0 commit comments

Comments
 (0)