-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit-msg
126 lines (102 loc) · 3.2 KB
/
commit-msg
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
#!/usr/bin/env bash
# Automated Timestamping
#
# Copyright (c) 2021 - 2022 Johannes Milczewski
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
function hook_echo() {
local TAG="commit-msg"
echo -e "[\e[0;32m${TAG}\e[0m]: $1"
}
function hook_error() {
local TAG="commit-msg"
echo -e "[\e[0;31m${TAG}\e[0m]: $1"
}
function ts-commit-msg_impl() {
if [[ -z ${GIT_DIR+x} || $# -lt 1 ]]; then
die 'Call this script only through "git commit".'
fi
# Get current branch
local BRANCH
BRANCH=$(get_branch)
# Return if already on signing branch
if on_signing_branch; then
return 0
fi
hook_echo "Branch is ${BRANCH}"
local TSA_LIST=()
mapfile -t TSA_LIST < <(get_tsa_list "${BRANCH}")
# Get applicable diff of staged files
hook_echo "Creating diff"
local DIFF
DIFF=$(get_diff)
if [[ $? -eq 3 ]]; then
hook_echo 'ERROR: ts.diff.type must be either "staged" or "full"'
fi
# Get TSA-specific diffs
local TSA_DIFF=()
for i in "${!TSA_LIST[@]}"; do
if TSA_DIFF[${i}]=$(get_tsa_diff "${BRANCH}" "${TSA_LIST[${i}]}"); then
hook_echo "Getting diff for ${TSA_LIST[${i}]}"
fi
done
# Get commit message
local COMMIT_MSG_FILE=$1
local COMMIT_MSG
COMMIT_MSG=$(cat "${COMMIT_MSG_FILE}")
# Stash changes to be committed
local STASH
maybe_stash
STASH=$?
# Create signing branch if non-existent
checkout_signing "${BRANCH}" || {
local RETURN=$?
hook_echo "ERROR: Could not check out signing branch"
exit $?
}
# Write diff file
write_diff "${DIFF}"
# Write TSA-specific diffs
for i in "${!TSA_LIST[@]}"; do
if [[ -n "${TSA_DIFF[${i}]}" ]]; then
write_tsa_diff "${TSA_DIFF[${i}]}" "${TSA_LIST[${i}]}"
fi
done
create_timestamps "${BRANCH}"
RETURN=$?
# Checkout original branch
checkout_actual "${BRANCH}"
# Restore original commit message
echo "${COMMIT_MSG}" >"${COMMIT_MSG_FILE}"
# Unstash changes to be committed
maybe_unstash "${STASH}"
# Wait one second for different commit timestamps
sleep 1s
return ${RETURN}
}
function ts-commit-msg() {
# shellcheck source=timestamping.sh
source "$(dirname "$(realpath "$0")")/timestamping.sh" || return 128
if_enabled in_environment ts-commit-msg_impl "$@"
return $?
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
ts-commit-msg "$1"
exit $?
fi