-
Notifications
You must be signed in to change notification settings - Fork 4
/
_fiddle_seq_editor
executable file
·285 lines (244 loc) · 6.62 KB
/
_fiddle_seq_editor
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
#!/bin/bash
#
# Enhace the git-rebase(1) todo file with additional information about each
# commit, such as the author date, the author name and e-mail, as well as the
# commit message. The user then has the chance to edit any of this information
# when 'GIT_SEQUENCE_EDITOR' is invoked on the decorated git-rebase(1) todo
# file. Upon successful execution of 'GIT_SEQUENCE_EDITOR', parse the user's
# changes and prepare a 'git commit --amend' 'exec' step to be called after each
# 'pick'ed commit.
GIT_TODO=$1
GIT_FIDDLE_TODO=${1}.fiddle
GIT_FIDDLE_WORK=${1}.fiddle.work
trap '{
exit_code=$?
rm -f "$GIT_FIDDLE_TODO" || :
exit $exit_code
}' EXIT
FIDDLE_MESSAGE="$(cat <<-'EOF'
#
# GIT-FIDDLE(1) HELP:
# -------------------
#
# The format employed by git-fiddle is:
#
# <action> <sha> [<author>] [@ <timestamp>] [> <subject>]
# [<body>]
#
# where '<action>' needs to be 'fiddle' for anything to happen.
EOF
)"
function trim {
echo "$1" | sed -e '
s/^[[:space:]]*//
s/[[:space:]]*$//
'
}
function create_git_pretty_format {
local format=
# --[no-]fiddle-author
if ! [ "$GIT_FIDDLE_AUTHOR" = false ]; then
format="$(trim "$format %an")"
if ! [ "$GIT_FIDDLE_AUTHOR_EMAIL" = false ]; then
format="$format <%ae>"
fi
fi
# --[no-]fiddle-date
if ! [ "$GIT_FIDDLE_AUTHOR_DATE" = false ]; then
format="$(trim "$format @ %ad")"
fi
# --[no-]fiddle-subject
# note: always show this, only apply if 'GIT_FIDDLE_SUBJECT'
format="$(trim "$format> %s")"
# --[no-]fiddle-body
if ! [ "$GIT_FIDDLE_BODY" = false ]; then
if [ "$GIT_FIDDLE_SUBJECT" = false ]; then
format="$(trim "$format"$'%s\n%b')"
else
format="$(trim "$format"$'\n%b')"
fi
fi
echo "$format"
}
function prepare {
if [ "$GIT_FIDDLE_SUBJECT" = false ]; then
echo '# git-fiddle: subject lines are *read-only*'
echo ''
fi > "$GIT_FIDDLE_TODO"
local -r git_pretty_format="--format=$(create_git_pretty_format)"
while read -r line; do
if [ -z "$line" ] || [[ $line =~ ^[[:space:]]?# ]]; then
printf "%s\n" "$line"
else
read -r action sha rest <<< "$line"
if [[ "$action" == pick ]]; then
action=fiddle
fi
printf "%s %s %s\n" "$action" "$sha" \
"$(git --no-pager show -s "$sha" "$git_pretty_format" | \
git stripspace)"
fi
done < "$GIT_TODO" >> "$GIT_FIDDLE_TODO"
echo "$FIDDLE_MESSAGE" >> "$GIT_FIDDLE_TODO"
}
function fiddle_sequence_editor {
if [ -z "$GIT_FIDDLE_SEQUENCE_EDITOR" ]; then
GIT_FIDDLE_SEQUENCE_EDITOR="$(git config sequence.editor)"
if [ -z "$GIT_FIDDLE_SEQUENCE_EDITOR" ]; then
GIT_FIDDLE_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
fi
fi
eval "$GIT_FIDDLE_SEQUENCE_EDITOR" "$GIT_FIDDLE_TODO"
}
function esc { echo "$1" | sed -e "s/'/'\"'\"$'/g"; }
function emit {
local -r action="$1"
local -r sha="$2"
local -r author="$3"
local -r date="$4"
local subject="$5"
local message="$6"
local has_message=false
local -r message_file=${GIT_FIDDLE_WORK}/${sha}.message
if [[ "$action" == fiddle ]]; then
echo "pick $sha"
if [ -z "$message" ]; then
if [ -n "$subject" ] && ! [ "$GIT_FIDDLE_SUBJECT" = false ]; then
if [ "$GIT_FIDDLE_BODY" = false ]; then
has_message=true
echo "$subject"
echo
git --no-pager show -s "$sha" --format=$'%b'
else
has_message=true
echo "$subject"
fi
fi
else
if [ -n "$subject" ] && ! [ "$GIT_FIDDLE_SUBJECT" = false ]; then
has_message=true
echo "$subject"
echo
printf "$message"
fi
fi > "$message_file"
local c=
if [ -n "$date" ]; then
c="GIT_AUTHOR_DATE='$date'"
fi
c="$c git commit --amend --no-edit"
if [ -n "$date" ]; then
c="$c --date='$(esc "$date")'"
fi
if [ -n "$author" ]; then
c="$c --author='$(esc "$author")'"
fi
if [ "$has_message" = true ]; then
c="$c --file='$message_file'"
fi
echo "exec bash -c \"$(echo "$c" | sed \
-e 's/\\/\\\\/g' \
-e 's/"/\\"/g' \
-e 's/`/\\`/g' \
-e ':a;N;$!ba;s/\n/\\n/g'
)\""
else
echo "$action $sha"
fi
}
function apply {
set -eo pipefail
local cur_sha=
local cur_action=
local scanning_body=false
local _author _date _subject _message
while read -r line; do
read -r action sha rest <<< "$line"
if echo "$action" \
| grep -E -q '^(fiddle|pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)$'
then
if [[ "$sha" =~ ^[0-9a-z]{6,}$ ]]; then
# flush the current scan
if $scanning_body; then
emit "$cur_action" "$cur_sha" "$_author" "$_date" "$_subject" "$_message"
fi
# reset state
scanning_body=false
_author=
_date=
_subject=
_message=
declare -a output
# begin a fresh scan for 'fiddle' actions
cur_sha="$sha"
cur_action="$action"
OLD_IFS="$IFS"
IFS=$'\n' output=($(awk -v FS='' '{
# constants
FALSE = 0
TRUE = 1
STATE_AUTHOR = 1
STATE_DATE = 2
STATE_SUBJECT = 3
# intermediate/output state
author = ""
date = ""
subject = ""
in_parens = FALSE
in_email_angles = FALSE
state = STATE_AUTHOR
# [author [<email>]] [@ <author_date> ] [><subject>]
# | STATE_AUTHOR | | STATE_DATE | STATE_SUBJECT
for (i = 1; i <= NF; i++) {
# process state transitions
if ($i == "@" && in_email_angles == FALSE && state < STATE_DATE) {
state = STATE_DATE
} else if ($i == ">" && in_email_angles == FALSE && state < STATE_SUBJECT) {
state = STATE_SUBJECT
} else if (state == STATE_AUTHOR) {
if ($i == "<") {
in_email_angles = TRUE
} else if ($i == ">") {
in_email_angles = FALSE
}
author = author $i
} else if (state == STATE_DATE) {
date = date $i
} else if (state == STATE_SUBJECT) {
subject = subject $i
}
}
# avoid field collapsing in bash, by printing a blank
# which is not present in $IFS
if (length(author) == 0) { author = " "; }
if (length(date) == 0) { date = " "; }
if (length(subject) == 0) { subject = " "; }
print author
print date
print subject
}' <<< "$(trim "$rest")"))
IFS="$OLD_IFS"
_author="$(trim "${output[0]}")"
_date="$(trim "${output[1]}")"
_subject="$(trim "${output[2]}")"
scanning_body=true
continue
fi
fi
if $scanning_body; then
_message="$_message\n$line"
else
echo "$line" # not our issue! let git-rebase fail
fi
done <<< "$(git stripspace --strip-comments < "$GIT_FIDDLE_TODO")"
# flush the current scan
if $scanning_body; then
emit "$cur_action" "$cur_sha" "$_author" "$_date" "$_subject" "$_message"
fi
}
# kick off
rm -rf "$GIT_FIDDLE_WORK" && \
mkdir -p "$GIT_FIDDLE_WORK" && \
prepare && \
fiddle_sequence_editor && \
apply > "$GIT_TODO" || exit $?;