Skip to content

Commit 2e83766

Browse files
committed
Fix script bug where running from a subdirectory didn't work
This was always intended to work, but didn't because script subprocesses always used the original command name/path, but this was after changing directory. The script already relies on the repository being organized a particular way, so relying on itself (or another script suitable to do its work) being at the known location is no worse. This also makes it unnecessary to guard against the rare {}-in-path case. This change has the further benefit that if the script is ever successfully invoked via a Windows-style path with "\" separators (which can happen on Windows), its subprocesses can still execute. This also removes an altogether ineffective attempt to support the rare case of a working tree whole top-level directory name ends in a newline character, and instead changes the comment to note that this does work. A few other comments throughout are reworded for clarity as well. It may make sense to change the script more deeply than this (even before replacing part of it with a Rust tool). By sacrificing a small amount of portability, a loop could be used, as find recognizes -print0 in GNU/Linux (via GNU findutils), most BSD systems including macOS, and busybox (thus also Alpine Linux). Unfortunately a recursive glob, which would be even nicer, is not supported by versions of bash that ship with macOS.
1 parent c957ab8 commit 2e83766

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

etc/copy-packetline.sh

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function indent () {
3333
function generate_all () {
3434
local root failures
3535

36-
root="$(git rev-parse --show-toplevel)/." # /. in case name ends in newline.
36+
# Find the working tree. (NOTE: Wrong if the directory name ends in newline.)
37+
root="$(git rev-parse --show-toplevel)"
3738
cd -- "$root"
3839

3940
if ! test -d gix-packetline/src; then
@@ -53,7 +54,11 @@ function generate_all () {
5354
fail 'unable to remove target'
5455
fi
5556

56-
failures="$(find gix-packetline/src/ -exec "$0" --file {} \; -o -print)"
57+
failures="$(
58+
find gix-packetline/src/ \
59+
-exec etc/copy-packetline.sh --file {} \; \
60+
-o -print
61+
)"
5762

5863
# If we get here, traversal succeeded, but perhaps some generations failed.
5964
if test -n "$failures"; then
@@ -74,7 +79,7 @@ function first_line_ends_crlf () {
7479
# avoid false positives with unexpected characters, or with \r not before \n.
7580

7681
head -n 1 -- "$1" | # Get the longest prefix with no non-trailing \n byte.
77-
od -An -ta | # Show all bytes symbolically, without addresses.
82+
od -An -ta | # Represent all bytes symbolically, without addresses.
7883
tr -sd '\n' ' ' | # Scrunch into one line, so "cr nl" appears as such.
7984
grep -q 'cr nl$' # Check if the result signifies a \r\n line ending.
8085
}
@@ -120,7 +125,7 @@ function generate_one () {
120125
# Cover this case separately, for more useful error messages.
121126
fail "source file is symbolic link: $source"
122127
elif ! test -f "$source"; then
123-
# This covers less common kinds of files we can't/shouldn't process.
128+
# This covers less common kinds of files we can't or shouldn't process.
124129
fail "source file neither regular file nor directory: $source"
125130
elif [[ "$source" =~ \.rs$ ]]; then
126131
copy_with_header "$source" "$target"
@@ -129,13 +134,6 @@ function generate_one () {
129134
fi
130135
}
131136

132-
case "$0" in
133-
*{}*)
134-
# Some "find" implementations expand "{}" even inside a larger argument.
135-
fail "can't operate portably with literal {} in command name"
136-
;;
137-
esac
138-
139137
if { test "$#" -eq 1 && test "$1" = '--all'; } || test "$#" -eq 0; then
140138
generate_all
141139
elif test "$#" -eq 2 && test "$1" = '--file'; then

0 commit comments

Comments
 (0)