-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyright
executable file
·106 lines (101 loc) · 2.3 KB
/
copyright
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
#!/bin/sh
# vi:et lbr noet sw=2 ts=2 tw=79 wrap
# Copyright 2022-2024 David Rabkin
# The script uses local variables which are not POSIX but supported by most
# shells. See:
# https://stackoverflow.com/q/18597697
# shellcheck disable=SC2039,SC3043 # Uses local variables.
# shellcheck disable=SC1091,SC2034 # File not following, appears unused.
BASE_APP_USAGE="$(
cat <<-EOM 2>&1
-a, --action Replaces copyright years in files if needed.
-o, --owner A string for an owner, e.g. -o 'David Rabkin'.
EOM
)" || {
local err=$?
printf >&2 %s\\n "$BASE_APP_USAGE"
exit $err
}
readonly \
BASE_APP_USAGE \
BASE_APP_VERSION=0.9.20240208 \
BASE_MIN_VERSION=0.9.20231212
. base.sh
# Analyzes input files, fixes copyright years if needed.
fix_outdated() {
local arg dst ret=0 src
for arg; do
dst="$(git log -1 --pretty=format:'%aD' "$arg" | cut -d \ -f 4)"
src="$(
grep -E "$PATTERN" <"$arg" |
grep -Eo '[0-9]{4}' |
tail -n 1
)"
[ "$dst" = "$src" ] && {
log "$arg: copyrighted $src."
continue
}
[ "$ACTION" = false ] && {
ret=1
logw "$arg: copyrighted $src, modified $dst."
continue
}
sed -i.bak \
"s/^\(.*\)-$src\(.*\)$OWNER/\1-$dst\2$OWNER/g" \
"$arg"
sed -i.bak \
"s/^\(.*\)$src\(.*\)$OWNER/\1$src-$dst\2$OWNER/g" \
"$arg"
rm "$arg".bak
log "$arg: copyrighted $src, modified $dst, fixed."
done
return $ret
}
# Searches for Git files in the current directory.
main() {
validate "$@"
local fil
fil="$(
git ls-files . 2>&1 |
xargs grep -E "$PATTERN" /dev/null 2>&1 |
cut -d : -f 1 2>&1 |
sort -V 2>&1 |
uniq 2>&1 |
xargs 2>&1
)" || {
local err=$?
[ "$fil" = "" ] || die Unable to continue, err=$err, msg="$fil".
printf '%s is not found.\n' "$PATTERN"
cya Unable to continue, err=$err.
}
# shellcheck disable=SC2086 # No need for double quates.
fix_outdated $fil
}
# Validates the environment.
validate() {
validate_cmd git
local arg
for arg; do
shift
case "$arg" in
-a | --action)
ACTION=true
;;
-o | --owner)
set +o nounset
[ -n "$1" ] || cya --owner requires a non-empty argument.
set -o nounset
OWNER="$1"
;;
esac
done
var_exists ACTION || ACTION=false
var_exists OWNER || OWNER=David\ Rabkin
PATTERN='Copyright .* '$OWNER
readonly \
ACTION \
PATTERN \
OWNER
}
# Starting point.
main "$@"