-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.sh
241 lines (206 loc) · 7.58 KB
/
git.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
#!/usr/bin/env bash
# debug calls
shared_debug=${shared_debug:-0}
shared_git_sh_loaded=${shared_git_sh_loaded:-0}
if [ "x0" != "x${shared_git_sh_loaded}" ]; then
if [ "x1" == "x${shared_debug}" ]; then
echo "--> git.sh already included"
fi
else
shared_git_sh_loaded=1
pkg::import logger
git_result_new_commits=${git_result_new_commits:-}
# Sets: git_result_new_commits
# Assumes: $oldrev $newrev $refname
#
# This is for use in post receive hooks, as it assumes the refname has moved and
# is now newrev, we need to discard it. This is down with bash string replace,
# as it will replace only the first match, compared to the canonical "grep -v"
# approach which will throw out multiple matches if the same commit is referred
# to by multiple branches.
#
# Excellent, excellent docs from Andy Parkin's email script
#
##################################################
#
# Consider this:
# 1 --- 2 --- O --- X --- 3 --- 4 --- N
#
# O is $oldrev for $refname
# N is $newrev for $refname
# X is a revision pointed to by some other ref, for which we may
# assume that an email has already been generated.
# In this case we want to issue an email containing only revisions
# 3, 4, and N. Given (almost) by
#
# git rev-list N ^O --not --all
#
# The reason for the "almost", is that the "--not --all" will take
# precedence over the "N", and effectively will translate to
#
# git rev-list N ^O ^X ^N
#
# So, we need to build up the list more carefully. git rev-parse
# will generate a list of revs that may be fed into git rev-list.
# We can get it to make the "--not --all" part and then filter out
# the "^N" with:
#
# git rev-parse --not --all | grep -v N
#
# Then, using the --stdin switch to git rev-list we have effectively
# manufactured
#
# git rev-list N ^O ^X
#
# This leaves a problem when someone else updates the repository
# while this script is running. Their new value of the ref we're
# working on would be included in the "--not --all" output; and as
# our $newrev would be an ancestor of that commit, it would exclude
# all of our commits. What we really want is to exclude the current
# value of $refname from the --not list, rather than N itself. So:
#
# git rev-parse --not --all | grep -v $(git rev-parse $refname)
#
# Get's us to something pretty safe (apart from the small time
# between refname being read, and git rev-parse running - for that,
# I give up)
#
#
# Next problem, consider this:
# * --- B --- * --- O ($oldrev)
# \
# * --- X --- * --- N ($newrev)
#
# That is to say, there is no guarantee that oldrev is a strict
# subset of newrev (it would have required a --force, but that's
# allowed). So, we can't simply say rev-list $oldrev..$newrev.
# Instead we find the common base of the two revs and list from
# there.
#
# As above, we need to take into account the presence of X; if
# another branch is already in the repository and points at some of
# the revisions that we are about to output - we don't want them.
# The solution is as before: git rev-parse output filtered.
#
# Finally, tags: 1 --- 2 --- O --- T --- 3 --- 4 --- N
#
# Tags pushed into the repository generate nice shortlog emails that
# summarise the commits between them and the previous tag. However,
# those emails don't include the full commit messages that we output
# for a branch update. Therefore we still want to output revisions
# that have been output on a tag email.
#
# Luckily, git rev-parse includes just the tool. Instead of using
# "--all" we use "--branches"; this has the added benefit that
# "remotes/" will be ignored as well.
#
##################################################
git::set_new_commits() {
logger::header "git::set_new_commits"
refname=${refname:-}
newrev=${newrev:-}
oldrev=${oldrev:-}
logger::log "refname: [${refname}]"
logger::log "newrev: [${newrev}]"
logger::log "oldrev: [${oldrev}]"
nl=$'\n'
# Get all the current branches, not'd as we want only new ones
git_result_new_commits=$(git rev-parse --not --branches || exit 0)
# Strip off the not current branch
new_hash=$(git rev-parse "${refname}" || exit 0)
git_result_new_commits=${git_result_new_commits/^$new_hash/}
# Put back newrev without the not
git_result_new_commits="${git_result_new_commits}${nl}${newrev}"
# Put in ^oldrev if it's not a new branch
if [ "${oldrev}" != "0000000000000000000000000000000000000000" ] ; then
git_result_new_commits="${git_result_new_commits}${nl}^${oldrev}"
fi
git_result_new_commits="${git_result_new_commits/$nl$nl/$nl}"
git_result_new_commits="${git_result_new_commits/#$nl/}"
}
git_result_change_type=${git_result_change_type:-}
# Sets: $git_result_change_type
# Assumes: $oldrev $newrev
#
# --- Interpret
# 0000->1234 (create)
# 1234->2345 (update)
# 2345->0000 (delete)
git::set_change_type() {
logger::header "git::set_change_type"
newrev=${newrev:-}
oldrev=${oldrev:-}
logger::log "newrev: [${newrev}]"
logger::log "oldrev: [${oldrev}]"
if [ "${oldrev}" == "0000000000000000000000000000000000000000" ] ; then
git_result_change_type="create"
else
if [ "${newrev}" == "0000000000000000000000000000000000000000" ] ; then
git_result_change_type="delete"
else
git_result_change_type="update"
fi
fi
}
git_result_newrev_type=${git_result_newrev_type:-}
git_result_oldrev_type=${git_result_oldrev_type:-}
git_result_rev=${git_result_rev:-}
git_result_rev_type=${git_result_rev_type:-}
# Sets: $git_result_newrev_type $git_result_oldrev_type $git_result_rev $git_result_rev_type
# Assumes: $newrev $oldrev
# --- Get the revision types
git::set_rev_types() {
logger::header "git::set_rev_types"
newrev=${newrev:-}
oldrev=${oldrev:-}
logger::log "newrev: [${newrev}]"
logger::log "oldrev: [${oldrev}]"
git_result_newrev_type=$(git cat-file -t "${newrev}" 2> /dev/null || exit 0)
git_result_oldrev_type=$(git cat-file -t "${oldrev}" 2> /dev/null || exit 0)
if [ "${newrev}" == "0000000000000000000000000000000000000000" ] ; then
git_result_rev_type="${git_result_oldrev_type}"
git_result_rev="${oldrev}"
else
git_result_rev_type="${git_result_newrev_type}"
git_result_rev="${newrev}"
fi
}
git_result_describe=${git_result_describe:-}
# Sets: $git_result_describe
# Assumes: $rev
#
# The email subject will contain the best description of the ref that we can build from the parameters
git::set_describe() {
logger::header "git::set_describe"
rev=${rev:-}
logger::log "rev: [${rev}]"
param=${1:-}
rev_to_describe="$rev"
if [ "${param}" != "" ] ; then
rev_to_describe="${param}"
fi
describe=$(git describe "${rev_to_describe}" 2>/dev/null || exit 0)
if [ -z "$describe" ]; then
git_result_describe=$rev_to_describe
fi
}
git_result_describe_tags=${git_result_describe_tags:-}
# Sets: $git_result_describe_tags
# Assumes: $rev
#
# The email subject will contain the best description of the ref that we can build from the parameters
git::set_describe_tags() {
logger::header "git::set_describe_tags"
rev=${rev:-}
logger::log "rev: [${rev}]"
param=${1:-}
rev_to_describe="${rev}"
if [ "${param}" != "" ] ; then
rev_to_describe="${param}"
fi
git_result_describe_tags=$(git describe --tags "${rev_to_describe}" 2>/dev/null || exit 0)
if [ -z "${git_result_describe_tags}" ]; then
git_result_describe_tags=$rev_to_describe
fi
}
fi