-
Notifications
You must be signed in to change notification settings - Fork 3
/
vcommit
executable file
·290 lines (278 loc) · 6.02 KB
/
vcommit
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
286
287
288
289
290
#!/bin/bash --noprofile
unset LC_ALL
export LC_TIME=C
export TZ=JST-9
abort() {
echo "${0##*/}: $*" 1>&2
exit 1
}
unset fullname mailaddr
LOGFILE=ChangeLog
nocommit=
nochangelog=
logfile=
dcommit=yes
keeplog=no
gitlog=
amend=
tmpdir=
trap '[ -d "$tmpdir" ] && rm -rf "$tmpdir"' 0 2
until [ "$#" = 0 ]; do
arg="$1"
case "$arg" in
--amend)
amend=--amend;;
--git-log)
gitlog=yes
tmpdir=`mktemp -d ${TMPDIR-/tmp}/vcommit.XXXXXXXXXX`
logfile=`git format-patch -1 -o "$tmpdir"`;;
--no-commit)
nocommit=1;;
--no-log)
nochangelog=1;;
--git-only)
dcommit=no;;
--log-file=*)
logfile="${1#*=}";;
--log-file)
shift
[ "$#" = 0 ] && abort "missing argument for $arg"
logfile="$1";;
--full-name=*|--fullname=*)
fullname="${arg#*=}";;
--full-name|--fullname)
shift
[ "$#" = 0 ] && abort "missing argument for $arg"
fullname="${arg#*=}";;
--mail-address=*|--mail-addr=*|--mailaddr=*|--mail=*)
mailaddr="${arg#*=}";;
--mail-address|--mail-addr|--mailaddr|--mail)
[ "$#" = 0 ] && abort "missing argument for $arg"
mailaddr="${arg#*=}";;
--full-name|--fullname)
shift
[ "$#" = 0 ] && abort "missing argument for $arg"
fullname="${arg#*=}";;
--keep-log)
keeplog=yes;;
--)
shift
break;;
--*)
break;;
-*)
arg1=1 arg="${arg#-}"
until [ -z "$arg" ]; do
opt="${arg:0:1}" arg="${arg:1}"
case "$opt" in
n)
nocommit=1;;
l)
nochangelog=1;;
g)
dcommit=no;;
F)
logfile="$arg"
if [ -z "$logfile" ]; then
shift
[ "$#" = 0 ] && abort "missing argument for -$opt"
logfile="$1"
fi
break;;
C)
logfile="${logfile:+$PWD/$logfile}"
if [ -z "$arg" ]; then
shift
[ "$#" = 0 ] && abort "missing argument for -$opt"
cd "$1"
else
cd "$arg"
fi
break;;
k)
keeplog=yes
break;;
*)
[ $arg1 ] || abort "unknown switch -$opt"
break 2
;;
esac
arg1=
done;;
*)
break
;;
esac
shift
done
update_created() {
declare time="$1" f
shift
[ "$#" = 0 ] && return
set -- `grep -r -l '^ *created at: .*' "$@"`
[ "$#" = 0 ] && return
for f; do
mv "$f" "$f"~
sed 's|^\( *created at: \).*|\1'"$time|" "$f"~ > "$f"
done
}
if [ -d .git ]; then
vcs=git
elif [ -d .svn -o -d ../.svn ]; then
vcs=svn
else
echo "$0: unknown vcs"
exit 1
fi
if test -z "${logfile}"; then
if test -f change.log; then
logfile=change.log
else
logfile=log
fi
fi
if [ x"$fullname" = x ]; then
if [ x"$vcs" = xgit ]; then
fullname=`git log -n1 --format=%aN`
else
fullname=${FULLNAME:-`sed -n /"^$USER:"/'s/^[^:]*:[^:]*:[^:]*:[^:]*:\([^,:]*\).*/\1/p' /etc/passwd`}
fi
fi
if [ x"$mailaddr" = x ]; then
if [ x"$vcs" = xgit ]; then
mailaddr=`git log -n1 --format=%aE`
else
mailaddr=${MAILADDR:-`sed -n '/^default/s/.*password //p' ~/.netrc`}
fi
fi
: ${fullname:?No FULLNAME} ${mailaddr:?No MAILADDR}
export GIT_AUTHOR_NAME="$fullname"
export GIT_AUTHOR_EMAIL="$mailaddr"
prepend_changelog() {
declare file="$1"
declare line
read line < "$file"
case "$line" in
[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*)
time="`date +%Y-%m-%d`"
[ -n "$nochangelog" -o -f "$file.orig" ] ||
sed "1{/$time $fullname /{N;/\n$/d;D;};}" "$file" > "$file.orig"
;;
*)
time="`date +%c`"
[ -n "$nochangelog" -o -f "$file.orig" ] ||
mv "$file" "$file.orig"
;;
esac
{
ruby -e '
class String
def purify
unicode_normalize(:nfd).
encode("us-ascii", replace: "")
end
end
h = ARGV.shift.purify
s = open(ARGV.shift) {|f|f.read}
orig = open(ARGV.shift) {|f|f.read}
output = ARGV.shift
s.sub!(/\Amerge.*\d:$/, "")
if log = s[/\AFrom .*^Subject: \[PATCH(?: \d*\/\d*)?\] *(.*)^---$/m, 1]
if log.gsub!(/\A[ \t]*([^*].*)\n(.+\n)*\n+/, "")
puts $1
puts
end
log.gsub!(/^ /, "") if /\A \*/ =~ log
else
(log = s).sub!(/^\f.*/m, "")
end
log.gsub!(/[ \t]+$/, "")
log.gsub!(/^ *(?=\t)/, "")
log.gsub!(/(\S\n)(?=\*)/m, "\\1\n")
puts log.gsub(/^\n/, "")
log.sub!(/\s*\[ci\s+skip\]\s*\Z/im, "")
log.gsub!(/^(?!\t|$)/, "\t")
log.sub!(/\n*\Z/m, "\n")
log = log.purify
open(output, "w") {|f|f.puts h, "", log, "", orig}
' "$time $fullname <$mailaddr>" \
"$logfile" "$file.orig" "$file"
} > changelog.tmp
}
if [ "$#" = 0 ]; then
if [ "${gitlog}" = yes ]; then
set ChangeLog
: ${amend:=--amend}
else
set */
fi
fi
changelogs=()
for d; do
d="${d%/}"
if [ -d "$d" ]; then
[ -f "$d"/$LOGFILE ] || continue
elif [ -f "$d" ]; then
[ "${d##*/}" = $LOGFILE ] || continue
case "$d" in
*/*) d="${d%/*}";;
*) d=.;;
esac
else
continue
fi
for c in "${changelogs[@]%/*}"; do
case "${d}" in
"${c#./}/"*) continue 2;;
esac
done
prepend_changelog "$d/$LOGFILE"
changelogs=("${changelogs[@]}" "$d/$LOGFILE.orig")
[ "$d" = . ] && break
done
if [ ${#changelogs[0]} = 0 ]; then
[ -f $LOGFILE ] || { echo "${0##*/}: nothing to commit" 1>&2; exit 1; }
prepend_changelog "$LOGFILE"
changelogs=($LOGFILE.orig)
fi
case "$vcs" in
svn)
added=`svn status "$@" | sed -n 's/^A //p'`
update_created "$time" $added
;;
git*) git add "$@";;
esac
if [ $nocommit ]; then
echo $vcs commit -F "$logfile" "$@"
${tmpdir:+rm -fr "$tmpdir"}
cat changelog.tmp
[ -d .git/svn -a "$dcommit" = yes ] && echo git svn dcommit "$@"
if test x"$PAGER" != x -a -t; then
case "$vcs" in
svn)
exec $PAGER +/'^Index:.*' < <($vcs diff --no-diff-deleted "$@")
;;
git*)
exec $vcs diff --staged "$@"
;;
esac
fi
exit
fi
if $vcs commit -F changelog.tmp ${amend} "$@"; then
if ! [ -d .git/svn -a "$dcommit" = yes ] || git svn dcommit "$@"; then
if [ $keeplog != yes ]; then
if [ -z "$tmpdir" ]; then
mv -f "$logfile" "$logfile"~
else
rm -f "$logfile"
rmdir "$tmpdir"
tmpdir=
fi
fi
fi
[ "$vcs" = "svn" ] && $vcs up
exec rm -fr changelog.tmp revision.h "${changelogs[@]}" ${tmpdir:+"$tmpdir"}
else
[ -f $LOGFILE.orig ] && mv -f $LOGFILE.orig $LOGFILE
fi