-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.sh
executable file
·424 lines (355 loc) · 11.7 KB
/
config.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env bash
# Echos with marking
function script_echo() {
local TAG="config.sh"
echo -e "[\e[0;32m${TAG}\e[0m]: $1"
}
# Prints help
function print_help() {
# Print help
echo "$(basename "$0") [<OPTIONS>] <PATH>
Installs automated timestamping into a git repository
Options:
-d, --default install with default options
-f, --force don't check files when copying/deleting
-h, --help show this help text
-r, --remove removes automated timestamping, but keeps the generated timestamps
-p, --purge removes automated timestamping and the generated timestamps"
}
# Prompts all git config options
function prompt_options() {
read -r -p "Enter the branch prefix [sig]: " TS_BRANCH_PREFIX
read -r -p "Enter how remote and local signing branches should be managed [merge]: " TS_BRANCH_REMOTE
read -r -p "Enter whether timestamps should be withheld from remotes [false]: " TS_BRANCH_WITHHOLD
read -r -p "Enter the commit prefix [Timestamp for: ]: " TS_COMMIT_PREFIX
read -r -p "Enter the commit options []: " TS_COMMIT_OPTIONS
read -r -p "Enter whether timestamp commits should be related to actual [true]: " TS_COMMIT_RELATE
read -r -p "Enter the diff notice [Written by $(git config --get user.name)]: " TS_DIFF_NOTICE
read -r -p "Enter the diff file name [.diff]: " TS_DIFF_FILE
read -r -p "Enter the diff type [staged]: " TS_DIFF_TYPE
read -r -p "Enter the TSA configuration directory name [rfc3161]: " TS_SERVER_DIRECTORY
read -r -p "Enter the TSA certificate bundle file name [cacert.pem]: " TS_SERVER_CERTIFICATE
read -r -p "Enter the TSA url file name [url]: " TS_SERVER_URL
read -r -p "Enter whether existing timestamp should be updated on TSA config changes [true]: " TS_SERVER_UPDATE
read -r -p "Enter the timestamp request file name [request.tsq]: " TS_REQUEST_FILE
read -r -p "Enter the timestamp request options [-cert -sha256 -no_nonce]: " TS_REQUEST_OPTIONS
read -r -p "Enter the timestamp response file name [response.tsr]: " TS_RESPONSE_FILE
read -r -p "Enter the timestamp response options []: " TS_RESPONSE_OPTIONS
read -r -p "Enter whether timestamps should be verified [true]: " TS_RESPONSE_VERIFY
}
# Sets all git config options
function set_options() {
git config ts.enabled "true"
git config --local ts.branch.prefix "${TS_BRANCH_PREFIX:-"sig"}"
git config --local ts.branch.remote "${TS_BRANCH_REMOTE:-"merge"}"
git config --local ts.branch.withhold "${TS_BRANCH_WITHHOLD:-"false"}"
git config --local ts.commit.prefix "${TS_COMMIT_PREFIX:-"Timestamp for: "}"
git config --local ts.commit.options "${TS_COMMIT_OPTIONS:-""}"
git config --local ts.commit.relate "${TS_COMMIT_RELATE:-""true}"
git config --local ts.diff.notice "${TS_DIFF_NOTICE:-"Written by $(git config --get user.name)"}"
git config --local ts.diff.file "${TS_DIFF_FILE:-".diff"}"
git config --local ts.diff.type "${TS_DIFF_TYPE:-"staged"}"
git config --local ts.server.directory "${TS_SERVER_DIRECTORY:-"rfc3161"}"
git config --local ts.server.certificate "${TS_SERVER_CERTIFICATE:-"cacert.pem"}"
git config --local ts.server.url "${TS_SERVER_URL:-"url"}"
git config --local ts.server.update "${TS_SERVER_UPDATE:-"true"}"
git config --local ts.request.file "${TS_REQUEST_FILE:-"request.tsq"}"
git config --local ts.request.options "${TS_REQUEST_OPTIONS:-"-cert -sha256 -no_nonce"}"
git config --local ts.respone.file "${TS_RESPONSE_FILE:-"response.tsr"}"
git config --local ts.respone.options "${TS_RESPONSE_OPTIONS:-""}"
git config --local ts.respone.verify "${TS_RESPONSE_VERIFY:-"true"}"
}
# Copy the hooks into the repository
function copy_hooks() {
local HOOKS_PATH=$1
local FILES=("commit-msg" "post-commit" "pre-push" "timestamping.sh")
for f in "${FILES[@]}"; do
maybe_copy "./hooks" "${HOOKS_PATH}" "${f}" || {
script_echo "Could not copy file ${f}"
exit 4
}
done
}
# Copy the hooks from the repository
function remove_hooks() {
local HOOKS_PATH=$1
local FILES=("commit-msg" "post-commit" "pre-push" "timestamping.sh")
for f in "${FILES[@]}"; do
maybe_remove "./hooks" "${HOOKS_PATH}" "${f}" || {
script_echo "Could not remove file ${f}"
exit 4
}
done
}
function add_to_gitattributes() {
local LINE=$1
touch ".gitattributes"
if ! grep -q -F "${LINE}" < <(tr -s ' ' <.gitattributes); then
echo "${LINE}" >>.gitattributes
fi
}
function file_is_also_object() {
local FILE_PATH=$1
local FILE_HASH
if FILE_HASH=$(git hash-object "${FILE_PATH}"); then
git show "${FILE_HASH}" 2>/dev/null | diff --strip-trailing-cr "${FILE_PATH}" - >/dev/null 2>/dev/null
return $?
else
return 1
fi
}
function files_are_same() {
local LHP=$1
local RHP=$2
if [[ -f ${LHP} && -f ${RHP} ]]; then
local LHH
local RHH
LHH=$(git hash-object "${LHP}")
RHH=$(git hash-object "${RHP}")
test "${LHH}" == "${RHH}" || diff --strip-trailing-cr "${LHP}" "${RHP}" >/dev/null 2>/dev/null
return $?
else
return 1
fi
}
function maybe_copy() {
local LOCAL_PATH=$1
local REPO_PATH=$2
local FILE=$3
local LHP="${LOCAL_PATH}/${FILE}"
local RHP="${REPO_PATH}/${FILE}"
if [[ ! -f "${RHP}" || ${OPT_FORCE} == "true" ]]; then
cp "${LHP}" "${RHP}"
elif [[ -d .git ]]; then
file_is_also_object "${RHP}" && cp "${LHP}" "${RHP}"
fi
return $?
}
function maybe_remove() {
local LOCAL_PATH=$1
local REPO_PATH=$2
local FILE=$3
local LHP="${LOCAL_PATH}/${FILE}"
local RHP="${REPO_PATH}/${FILE}"
if [[ -f "${RHP}" ]]; then
if [[ -d .git ]] && file_is_also_object "${RHP}" || [[ ${OPT_FORCE} == "true" ]]; then
rm "${RHP}"
else
files_are_same "${LHP}" "${RHP}" && rm "${RHP}"
fi
fi
return $?
}
# Create initial timestamping objects
function configure_repo() {
local TS_BRANCH_PREFIX
TS_BRANCH_PREFIX=$(git config --get ts.branch.prefix)
if git rev-parse --verify "${TS_BRANCH_PREFIX}/root" >/dev/null 2>/dev/null; then
script_echo "Root signing branch already present"
return 0
fi
# Get currently checked out branch
if ! BRANCH=$(git symbolic-ref --short HEAD); then
if ! BRANCH=$(git config init.defaultBranch); then
BRANCH="master"
fi
fi
# Stash staged changed
if git rev-parse HEAD >/dev/null 2>/dev/null; then
if ! git diff-index --quiet HEAD --; then
script_echo "Stashing changes"
git stash push >/dev/null 2>/dev/null
STASHED=$?
fi
fi
# Checkout root signing branch
script_echo "Checking out root signing branch"
if ! git checkout "${TS_BRANCH_PREFIX}/root" >/dev/null 2>/dev/null; then
git checkout --orphan "${TS_BRANCH_PREFIX}/root" >/dev/null 2>/dev/null
# Create initial commit in root signing branch
script_echo "Creating TSA config directory"
TS_SERVER_DIRECTORY=$(git config --get ts.server.directory)
mkdir -p "./${TS_SERVER_DIRECTORY}"
echo "Place your TSA configuration in this directory." >"./${TS_SERVER_DIRECTORY}/PLACE_TSA_CONFIGS_HERE"
add_to_gitattributes "*.diff binary"
add_to_gitattributes "*.pem binary"
add_to_gitattributes "*.tsq binary"
add_to_gitattributes "*.tsr binary"
add_to_gitattributes "*.sh text eol=lf"
git add "./${TS_SERVER_DIRECTORY}/PLACE_TSA_CONFIGS_HERE" >/dev/null 2>/dev/null
git add "./.gitattributes" >/dev/null 2>/dev/null
git commit -m "Initial timestamping commit" -- "./.gitattributes" "./${TS_SERVER_DIRECTORY}/PLACE_TSA_CONFIGS_HERE" >/dev/null 2>/dev/null
else
script_echo "Root signing branch already present"
fi
# Return to previous branch
script_echo "Checking out actual branch"
git checkout "${BRANCH}" >/dev/null 2>/dev/null || {
git checkout --orphan "${BRANCH}" >/dev/null 2>/dev/null
# Remove timestamping files
git rm -rf -- "./${TS_SERVER_DIRECTORY}/PLACE_TSA_CONFIGS_HERE" "./.gitattributes" >/dev/null 2>/dev/null
}
# Pop stashed changes
if [[ ${STASHED} -eq 0 ]]; then
script_echo "Unstashing changes"
git stash pop >/dev/null 2>/dev/null
else
# Remove timestamping files
script_echo "Removing timestamping files"
rm -rf "./${TS_SERVER_DIRECTORY}/" "./.gitattributes" >/dev/null 2>/dev/null
fi
}
# Install automated timestamping to target repository
function install_timestamping() {
local REPO_PATH=$1
local HOOKS_PATH
if ! HOOKS_PATH=$(git --git-dir "${REPO_PATH}" config --get core.hooksPath); then
HOOKS_PATH="${REPO_PATH}/.git/hooks"
fi
local TS_BRANCH_PREFIX
local TS_BRANCH_REMOTE
local TS_COMMIT_PREFIX
local TS_COMMIT_OPTIONS
local TS_COMMIT_RELATE
local TS_DIFF_NOTICE
local TS_DIFF_FILE
local TS_DIFF_TYPE
local TS_SERVER_DIRECTORY
local TS_SERVER_CERTIFICATE
local TS_SERVER_URL
local TS_SERVER_UPDATE
local TS_REQUEST_FILE
local TS_REQUEST_OPTIONS
local TS_RESPONSE_FILE
local TS_RESPONSE_OPTIONS
local TS_RESPONSE_VERIFY
copy_hooks "${HOOKS_PATH}"
(
cd "${REPO_PATH}" || {
script_echo "ERROR: Could not enter the specified directory"
exit 2
}
if ! [[ -d ".git" ]]; then
script_echo "ERROR: Specified directory is not a git repository"
exit 1
fi
if [[ ${OPT_DEFAULT} == false ]]; then
script_echo "Specify options:"
prompt_options
else
script_echo "Assuming default options"
fi
script_echo "Setting options for repository"
set_options
script_echo "Configuring repository"
configure_repo
script_echo "Installation complete."
)
}
function uninstall_timestamping() {
local REPO_PATH=$1
local HOOKS_PATH
if ! HOOKS_PATH=$(git --git-dir "${REPO_PATH}" config --get core.hooksPath); then
HOOKS_PATH="${REPO_PATH}/.git/hooks"
fi
script_echo "Removing hooks"
remove_hooks "${HOOKS_PATH}"
if [[ ${OPT_PURGE} == true ]]; then
(
cd "${REPO_PATH}" || {
script_echo "ERROR: Could not enter the specified directory"
exit 2
}
local TS_BRANCH_PREFIX
if ! TS_BRANCH_PREFIX=$(git config --get ts.branch.prefix); then
script_echo "ERROR: Could not get option 'ts.branch.prefix'"
fi
local BRANCHES=()
mapfile -t BRANCHES < <(git branch --list | tr -d ' *')
for b in "${BRANCHES[@]}"; do
if [[ ${b} == "${TS_BRANCH_PREFIX}/root" || ${b} == "${TS_BRANCH_PREFIX}/b/"* ]]; then
script_echo "Removing branch ${b}"
git branch -D "${b}"
fi
done
git config --unset --local ts.branch.prefix
)
fi
script_echo "Unsetting options"
git config --unset --local ts.enabled
git config --unset --local ts.branch.prefix
git config --unset --local ts.branch.remote
git config --unset --local ts.branch.withhold
git config --unset --local ts.commit.prefix
git config --unset --local ts.commit.options
git config --unset --local ts.commit.relate
git config --unset --local ts.diff.notice
git config --unset --local ts.diff.file
git config --unset --local ts.diff.type
git config --unset --local ts.server.directory
git config --unset --local ts.server.certificate
git config --unset --local ts.server.url
git config --unset --local ts.server.update
git config --unset --local ts.request.file
git config --unset --local ts.request.options
git config --unset --local ts.respone.file
git config --unset --local ts.respone.options
git config --unset --local ts.respone.verify
script_echo "Uninstall complete."
}
# Short options
for arg in "$@"; do
shift
case "${arg}" in
"--default") set -- "$@" "-d" ;;
"--force") set -- "$@" "-f" ;;
"--help") set -- "$@" "-h" ;;
"--remove") set -- "$@" "-r" ;;
"--purge") set -- "$@" "-p" ;;
*) set -- "$@" "${arg}" ;;
esac
done
OPT_DEFAULT=false
OPT_FORCE=false
OPT_REMOVE=false
OPT_PURGE=false
# Parse options
OPTIND=1
while getopts "dfhrp" opt; do
case "${opt}" in
"d")
OPT_DEFAULT=true
;;
"f")
OPT_FORCE=true
;;
"h")
print_help
exit 0
;;
"r")
OPT_REMOVE=true
;;
"p")
OPT_PURGE=true
;;
"?")
print_help
exit 4
;;
esac
done
shift $((OPTIND - 1))
if [ $# -eq 0 ]; then
print_help
exit 4
fi
(
cd "$(dirname "$0")" || exit 255
if [[ ${OPT_REMOVE} == false && ${OPT_PURGE} == false ]]; then
install_timestamping "$(realpath "$1")"
else
uninstall_timestamping "$(realpath "$1")"
fi
)
exit 0