2
2
3
3
set -euC -o pipefail
4
4
5
+ readonly source_dir=' gix-packetline/src'
6
+ readonly target_parent_dir=' gix-packetline-blocking'
7
+ readonly target_dir=" $target_parent_dir /src"
8
+
5
9
function fail () {
6
10
printf ' %s: error: %s\n' " $0 " " $1 " >&2
7
11
exit 1
8
12
}
9
13
10
- function chdir_toplevel() {
14
+ function chdir_toplevel () {
11
15
local root_padded root
12
16
13
17
# Find the working tree's root. (Padding is for the trailing-newline case.)
@@ -29,25 +33,26 @@ function merging () {
29
33
test -e " $git_dir /MERGE_HEAD"
30
34
}
31
35
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 " ||
34
38
fail ' git-status failed'
35
39
}
36
40
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
39
43
# The target does not exist on disk, so nothing will be lost. Proceed.
40
44
return
41
45
fi
42
46
43
47
if merging; then
44
48
# 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
46
50
fail ' target exists, and a merge is in progress'
47
51
fi
48
52
else
49
53
# 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
51
56
fail ' target exists, with unstaged changes or ignored files'
52
57
fi
53
58
fi
@@ -73,74 +78,73 @@ function first_line_ends_crlf () {
73
78
}
74
79
75
80
function make_header () {
76
- local source endline
81
+ local source_file endline
77
82
78
- source =" $1 "
83
+ source_file =" $1 "
79
84
endline=" $2 "
80
85
81
86
# shellcheck disable=SC2016 # The backticks are intentionally literal.
82
87
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 "
84
89
}
85
90
86
91
function copy_with_header () {
87
- local source target endline
92
+ local source_file target_file endline
88
93
89
- source =" $1 "
90
- target =" $2 "
94
+ source_file =" $1 "
95
+ target_file =" $2 "
91
96
92
- if first_line_ends_crlf " $source " ; then
97
+ if first_line_ends_crlf " $source_file " ; then
93
98
endline=$' \r\n '
94
99
else
95
100
endline=$' \n '
96
101
fi
97
102
98
- make_header " $source " " $endline " | cat - -- " $source " > " $target "
103
+ make_header " $source_file " " $endline " |
104
+ cat - -- " $source_file " > " $target_file "
99
105
}
100
106
101
107
function generate_one () {
102
- local source target
108
+ local source_file target_file
103
109
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 " / }"
106
112
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
110
116
# 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
113
119
# 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 "
117
123
else
118
- fail " source file not named as Rust source code: $source "
124
+ fail " source file not named as Rust source code: $source_file "
119
125
fi
120
126
}
121
127
122
128
function generate_all () {
123
- local source
124
-
125
- chdir_toplevel
129
+ local source_file
126
130
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 "
129
133
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 "
132
136
fi
137
+ check_target_dir
133
138
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'
139
142
fi
140
143
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 "
143
146
done
144
147
}
145
148
149
+ chdir_toplevel
146
150
generate_all
0 commit comments