-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-init
executable file
·170 lines (150 loc) · 4.13 KB
/
git-init
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
#!/bin/bash
# SPDX-FileCopyrightText: 2021 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command bc git timedatectl && exit 3
#
# Global functions.
#
make_random_alias() {
local range
local string
range="$(date -d "1970-01-01 UTC ${RAND_RANGE}" "+%s")"
string='! NEW_DATE="$('"${TZ:+TZ=${TZ@Q} }"'date -d "1970-01-01 UTC $(echo $(date +%s) - '"${range}"' + $(shuf -i '"0-${range}"' -n 1) | bc -l) seconds" "+%c")"'
string="${string}"' && export GIT_AUTHOR_DATE="${NEW_DATE}" && export GIT_COMMITTER_DATE="${NEW_DATE}" &&'
echo "${string}"
}
make_workday_alias() {
local start_s
local end_s
local scale
local string
start_s="$(date -d "1970-01-01 UTC ${START}" '+%s')"
end_s="$(date -d "1970-01-01 UTC ${END}" '+%s')"
scale="$(echo "(${end_s} - ${start_s}) / 86400" | bc -l)"
string='! NEW_DATE="$('"${TZ:+TZ=${TZ@Q} }"'date -d "00:00 $(echo '"${start_s}"' + '"${scale}"' \* "$(date -d "1970-01-01 UTC $(date +%T)" +%s)" | bc -l) seconds" "+%c")"'
string="${string}"' && export GIT_AUTHOR_DATE="${NEW_DATE}" && export GIT_COMMITTER_DATE="${NEW_DATE}" &&'
echo "${string}"
}
set_aliases() {
case "${SCHEDULE}" in
workday)
if [[ -v START ]] && [[ -v END ]]; then
alias="$(make_workday_alias)"
else
exit 3
fi
;;
random)
if [[ -v RAND_RANGE ]]; then
alias="$(make_random_alias)"
else
exit 3
fi
;;
none)
return
;;
*)
return
;;
esac
git config --${MODE} alias.alt-commit "${alias} git commit"
git config --${MODE} alias.alt-revert "${alias} git revert"
}
print_usage() {
show_header "Usage:"
show_listitem "\
-n --name author name
-e --email author email
-s --schedule schedule (workday, random, or none)
-r --range workday schedule range (e.g. 9AM-5PM)
-o --roffset random schedule offset (e.g. '3 days', '1 week')
-t --timezone time zone (e.g. 'America/Toronto')
-g --global flag to write settings globally
-h --help print (this) help message"
}
#
# Main
#
OPTIONS=n:e:s:r:o:t:hg
LONGOPTIONS=name:,email:,schedule:,range:,roffset:,timezone:,help,global
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
MODE=local
while [ ${#} -ge 1 ]; do
case ${1} in
-n | --name)
NAME="${2}"
shift 2
;;
-e | --email)
EMAIL="${2}"
shift 2
;;
-s | --schedule)
SCHEDULE="${2}"
shift 2
;;
-r | --range)
START="$(echo "${2}" | cut -d"-" -f1)"
END="$(echo "${2}" | cut -d"-" -f2)"
shift 2
;;
-o | --roffset)
RAND_RANGE="${2}"
shift 2
;;
-t | --timezone)
TZ="${2}"
shift 2
;;
-g | --global)
MODE=global
shift
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
print_usage
show_error "ERROR: invalid flag."
exit 3
;;
esac
done
if [[ -v TZ ]] && [ "${TZ}" = default ]; then
TZ="$(timedatectl show -p Timezone --value)"
fi
if [[ "${MODE}" = local ]]; then
if git rev-parse 2> /dev/null; then
show_warning "WARNING: Git repo already exists."
else
show_info "Initializing Git repo."
git init
fi
fi
${NAME:+git config --"${MODE}" user.name "${NAME}"}
${EMAIL:+git config --"${MODE}" user.email "${EMAIL}"}
${SCHEDULE:+set_aliases}