*** empty log message ***
[bpt/emacs.git] / lib-src / rcs2log
1 #! /bin/sh
2
3 # RCS to ChangeLog generator
4
5 # Generate a change log prefix from RCS files (perhaps in the CVS repository)
6 # and the ChangeLog (if any).
7 # Output the new prefix to standard output.
8 # You can edit this prefix by hand, and then prepend it to ChangeLog.
9
10 # Ignore log entries that start with `#'.
11 # Clump together log entries that start with `{topic} ',
12 # where `topic' contains neither white space nor `}'.
13
14 Help='The default FILEs are the files registered under the working directory.
15 Options:
16
17 -c CHANGELOG Output a change log prefix to CHANGELOG (default ChangeLog).
18 -h HOSTNAME Use HOSTNAME in change log entries (default current host).
19 -i INDENT Indent change log lines by INDENT spaces (default 8).
20 -l LENGTH Try to limit log lines to LENGTH characters (default 79).
21 -R If no FILEs are given and RCS is used, recurse through working directory.
22 -r OPTION Pass OPTION to subsidiary log command.
23 -t TABWIDTH Tab stops are every TABWIDTH characters (default 8).
24 -u "LOGIN<tab>FULLNAME<tab>MAILADDR" Assume LOGIN has FULLNAME and MAILADDR.
25 -v Append RCS revision to file names in log lines.
26 --help Output help.
27 --version Output version number.
28
29 Report bugs to <bug-gnu-emacs@gnu.org>.'
30
31 Id='$Id: rcs2log,v 1.46 2001/01/02 18:50:14 eggert Exp $'
32
33 # Copyright 1992, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
34
35 # This program is free software; you can redistribute it and/or modify
36 # it under the terms of the GNU General Public License as published by
37 # the Free Software Foundation; either version 2, or (at your option)
38 # any later version.
39 #
40 # This program is distributed in the hope that it will be useful,
41 # but WITHOUT ANY WARRANTY; without even the implied warranty of
42 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 # GNU General Public License for more details.
44 #
45 # You should have received a copy of the GNU General Public License
46 # along with this program; see the file COPYING. If not, write to the
47 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
48 # Boston, MA 02111-1307, USA.
49
50 Copyright='Copyright 1998 Free Software Foundation, Inc.
51 This program comes with NO WARRANTY, to the extent permitted by law.
52 You may redistribute copies of this program
53 under the terms of the GNU General Public License.
54 For more information about these matters, see the files named COPYING.
55 Author: Paul Eggert <eggert@twinsun.com>'
56
57 tab=' '
58 nl='
59 '
60
61 # Parse options.
62
63 # defaults
64 : ${AWK=awk}
65 : ${TMPDIR=/tmp}
66 changelog=ChangeLog # change log file name
67 datearg= # rlog date option
68 hostname= # name of local host (if empty, will deduce it later)
69 indent=8 # indent of log line
70 length=79 # suggested max width of log line
71 logins= # login names for people we know fullnames and mailaddrs of
72 loginFullnameMailaddrs= # login<tab>fullname<tab>mailaddr triplets
73 logTZ= # time zone for log dates (if empty, use local time)
74 recursive= # t if we want recursive rlog
75 revision= # t if we want revision numbers
76 rlog_options= # options to pass to rlog
77 tabwidth=8 # width of horizontal tab
78
79 while :
80 do
81 case $1 in
82 -c) changelog=${2?}; shift;;
83 -i) indent=${2?}; shift;;
84 -h) hostname=${2?}; shift;;
85 -l) length=${2?}; shift;;
86 -[nu]) # -n is obsolescent; it is replaced by -u.
87 case $1 in
88 -n) case ${2?}${3?}${4?} in
89 *"$tab"* | *"$nl"*)
90 echo >&2 "$0: -n '$2' '$3' '$4': tabs, newlines not allowed"
91 exit 1
92 esac
93 case $loginFullnameMailaddrs in
94 '') loginFullnameMailaddrs=$2$tab$3$tab$4;;
95 ?*) loginFullnameMailaddrs=$loginFullnameMailaddrs$nl$2$tab$3$tab$4
96 esac
97 shift; shift; shift;;
98 -u)
99 # If $2 is not tab-separated, use colon for separator.
100 case ${2?} in
101 *"$nl"*)
102 echo >&2 "$0: -u '$2': newlines not allowed"
103 exit 1;;
104 *"$tab"*)
105 t=$tab;;
106 *)
107 t=:
108 esac
109 case $2 in
110 *"$t"*"$t"*"$t"*)
111 echo >&2 "$0: -u '$2': too many fields"
112 exit 1;;
113 *"$t"*"$t"*)
114 ;;
115 *)
116 echo >&2 "$0: -u '$2': not enough fields"
117 exit 1
118 esac
119 case $loginFullnameMailaddrs in
120 '') loginFullnameMailaddrs=$2;;
121 ?*) loginFullnameMailaddrs=$loginFullnameMailaddrs$nl$2
122 esac
123 shift
124 esac
125 case $logins in
126 '') logins=$login;;
127 ?*) logins=$logins$nl$login
128 esac
129 ;;
130 -r)
131 case $rlog_options in
132 '') rlog_options=${2?};;
133 ?*) rlog_options=$rlog_options$nl${2?}
134 esac
135 shift;;
136 -R) recursive=t;;
137 -t) tabwidth=${2?}; shift;;
138 -v) revision=t;;
139 --version)
140 set $Id
141 rcs2logVersion=$3
142 echo >&2 "rcs2log (GNU Emacs) $rcs2logVersion$nl$Copyright"
143 exit 0;;
144 -*) echo >&2 "Usage: $0 [OPTION]... [FILE ...]$nl$Help"
145 case $1 in
146 --help) exit 0;;
147 *) exit 1
148 esac;;
149 *) break
150 esac
151 shift
152 done
153
154 month_data='
155 m[0]="Jan"; m[1]="Feb"; m[2]="Mar"
156 m[3]="Apr"; m[4]="May"; m[5]="Jun"
157 m[6]="Jul"; m[7]="Aug"; m[8]="Sep"
158 m[9]="Oct"; m[10]="Nov"; m[11]="Dec"
159 '
160
161
162 # Put rlog output into $rlogout.
163
164 # If no rlog options are given,
165 # log the revisions checked in since the first ChangeLog entry.
166 # Since ChangeLog is only by date, some of these revisions may be duplicates of
167 # what's already in ChangeLog; it's the user's responsibility to remove them.
168 case $rlog_options in
169 '')
170 if test -s "$changelog"
171 then
172 e='
173 /^[0-9]+-[0-9][0-9]-[0-9][0-9]/{
174 # ISO 8601 date
175 print $1
176 exit
177 }
178 /^... ... [ 0-9][0-9] [ 0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9]+ /{
179 # old-fashioned date and time (Emacs 19.31 and earlier)
180 '"$month_data"'
181 year = $5
182 for (i=0; i<=11; i++) if (m[i] == $2) break
183 dd = $3
184 printf "%d-%02d-%02d\n", year, i+1, dd
185 exit
186 }
187 '
188 d=`$AWK "$e" <"$changelog"` || exit
189 case $d in
190 ?*) datearg="-d>$d"
191 esac
192 fi
193 esac
194
195 # Use TZ specified by ChangeLog local variable, if any.
196 if test -s "$changelog"
197 then
198 extractTZ='
199 /^.*change-log-time-zone-rule['"$tab"' ]*:['"$tab"' ]*"\([^"]*\)".*/{
200 s//\1/; p; q
201 }
202 /^.*change-log-time-zone-rule['"$tab"' ]*:['"$tab"' ]*t.*/{
203 s//UTC0/; p; q
204 }
205 '
206 logTZ=`tail "$changelog" | sed -n "$extractTZ"`
207 case $logTZ in
208 ?*) TZ=$logTZ; export TZ
209 esac
210 fi
211
212 # If CVS is in use, examine its repository, not the normal RCS files.
213 if test ! -f CVS/Repository
214 then
215 rlog=rlog
216 repository=
217 else
218 rlog='cvs -q log'
219 repository=`sed 1q <CVS/Repository` || exit
220 test ! -f CVS/Root || CVSROOT=`cat <CVS/Root` || exit
221 case $CVSROOT in
222 *:/*)
223 # remote repository
224 ;;
225 *)
226 # local repository
227 case $repository in
228 /*) ;;
229 *) repository=${CVSROOT?}/$repository
230 esac
231 if test ! -d "$repository"
232 then
233 echo >&2 "$0: $repository: bad repository (see CVS/Repository)"
234 exit 1
235 fi
236 esac
237 fi
238
239 # Use $rlog's -zLT option, if $rlog supports it.
240 case `$rlog -zLT 2>&1` in
241 *' option'*) ;;
242 *)
243 case $rlog_options in
244 '') rlog_options=-zLT;;
245 ?*) rlog_options=-zLT$nl$rlog_options
246 esac
247 esac
248
249 # With no arguments, examine all files under the RCS directory.
250 case $# in
251 0)
252 case $repository in
253 '')
254 oldIFS=$IFS
255 IFS=$nl
256 case $recursive in
257 t)
258 RCSdirs=`find . -name RCS -type d -print`
259 filesFromRCSfiles='s|,v$||; s|/RCS/|/|; s|^\./||'
260 files=`
261 {
262 case $RCSdirs in
263 ?*) find $RCSdirs \
264 -type f \
265 ! -name '*_' \
266 ! -name ',*,' \
267 ! -name '.*_' \
268 ! -name .rcsfreeze.log \
269 ! -name .rcsfreeze.ver \
270 -print
271 esac
272 find . -name '*,v' -print
273 } |
274 sort -u |
275 sed "$filesFromRCSfiles"
276 `;;
277 *)
278 files=
279 for file in RCS/.* RCS/* .*,v *,v
280 do
281 case $file in
282 RCS/. | RCS/.. | RCS/,*, | RCS/*_) continue;;
283 RCS/.rcsfreeze.log | RCS/.rcsfreeze.ver) continue;;
284 RCS/.\* | RCS/\* | .\*,v | \*,v) test -f "$file" || continue;;
285 RCS/*,v | RCS/.*,v) ;;
286 RCS/* | RCS/.*) test -f "$file" || continue
287 esac
288 case $files in
289 '') files=$file;;
290 ?*) files=$files$nl$file
291 esac
292 done
293 case $files in
294 '') exit 0
295 esac
296 esac
297 set x $files
298 shift
299 IFS=$oldIFS
300 esac
301 esac
302
303 logdir=$TMPDIR/rcs2log$$
304 llogout=$logdir/l
305 rlogout=$logdir/r
306 trap exit 1 2 13 15
307 trap "rm -fr $logdir 2>/dev/null" 0
308 (umask 077 && exec mkdir $logdir) || exit
309
310 case $datearg in
311 ?*) $rlog $rlog_options "$datearg" ${1+"$@"} >$rlogout;;
312 '') $rlog $rlog_options ${1+"$@"} >$rlogout
313 esac || exit
314
315
316 # Get the full name of each author the logs mention, and set initialize_fullname
317 # to awk code that initializes the `fullname' awk associative array.
318 # Warning: foreign authors (i.e. not known in the passwd file) are mishandled;
319 # you have to fix the resulting output by hand.
320
321 initialize_fullname=
322 initialize_mailaddr=
323
324 case $loginFullnameMailaddrs in
325 ?*)
326 case $loginFullnameMailaddrs in
327 *\"* | *\\*)
328 sed 's/["\\]/\\&/g' >$llogout <<EOF || exit
329 $loginFullnameMailaddrs
330 EOF
331 loginFullnameMailaddrs=`cat $llogout`
332 esac
333
334 oldIFS=$IFS
335 IFS=$nl
336 for loginFullnameMailaddr in $loginFullnameMailaddrs
337 do
338 case $loginFullnameMailaddr in
339 *"$tab"*) IFS=$tab;;
340 *) IFS=:
341 esac
342 set x $loginFullnameMailaddr
343 login=$2
344 fullname=$3
345 mailaddr=$4
346 initialize_fullname="$initialize_fullname
347 fullname[\"$login\"] = \"$fullname\""
348 initialize_mailaddr="$initialize_mailaddr
349 mailaddr[\"$login\"] = \"$mailaddr\""
350 done
351 IFS=$oldIFS
352 esac
353
354 case $llogout in
355 ?*) sort -u -o $llogout <<EOF || exit
356 $logins
357 EOF
358 esac
359 output_authors='/^date: / {
360 if ($2 ~ /^[0-9]*[-\/][0-9][0-9][-\/][0-9][0-9]$/ && $3 ~ /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9][-+0-9:]*;$/ && $4 == "author:" && $5 ~ /^[^;]*;$/) {
361 print substr($5, 1, length($5)-1)
362 }
363 }'
364 authors=`
365 $AWK "$output_authors" <$rlogout |
366 case $llogout in
367 '') sort -u;;
368 ?*) sort -u | comm -23 - $llogout
369 esac
370 `
371 case $authors in
372 ?*)
373 cat >$llogout <<EOF || exit
374 $authors
375 EOF
376 initialize_author_script='s/["\\]/\\&/g; s/.*/author[\"&\"] = 1/'
377 initialize_author=`sed -e "$initialize_author_script" <$llogout`
378 awkscript='
379 BEGIN {
380 alphabet = "abcdefghijklmnopqrstuvwxyz"
381 ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
382 '"$initialize_author"'
383 }
384 {
385 if (author[$1]) {
386 fullname = $5
387 if (fullname ~ /[0-9]+-[^(]*\([0-9]+\)$/) {
388 # Remove the junk from fullnames like "0000-Admin(0000)".
389 fullname = substr(fullname, index(fullname, "-") + 1)
390 fullname = substr(fullname, 1, index(fullname, "(") - 1)
391 }
392 if (fullname ~ /,[^ ]/) {
393 # Some sites put comma-separated junk after the fullname.
394 # Remove it, but leave "Bill Gates, Jr" alone.
395 fullname = substr(fullname, 1, index(fullname, ",") - 1)
396 }
397 abbr = index(fullname, "&")
398 if (abbr) {
399 a = substr($1, 1, 1)
400 A = a
401 i = index(alphabet, a)
402 if (i) A = substr(ALPHABET, i, 1)
403 fullname = substr(fullname, 1, abbr-1) A substr($1, 2) substr(fullname, abbr+1)
404 }
405
406 # Quote quotes and backslashes properly in full names.
407 # Do not use gsub; traditional awk lacks it.
408 quoted = ""
409 rest = fullname
410 for (;;) {
411 p = index(rest, "\\")
412 q = index(rest, "\"")
413 if (p) {
414 if (q && q<p) p = q
415 } else {
416 if (!q) break
417 p = q
418 }
419 quoted = quoted substr(rest, 1, p-1) "\\" substr(rest, p, 1)
420 rest = substr(rest, p+1)
421 }
422
423 printf "fullname[\"%s\"] = \"%s%s\"\n", $1, quoted, rest
424 author[$1] = 0
425 }
426 }
427 '
428
429 initialize_fullname=`
430 {
431 (getent passwd $authors) ||
432 (
433 cat /etc/passwd
434 for author in $authors
435 do NIS_PATH= nismatch $author passwd.org_dir
436 done
437 ypmatch $authors passwd
438 )
439 } 2>/dev/null |
440 $AWK -F: "$awkscript"
441 `$initialize_fullname
442 esac
443
444
445 # Function to print a single log line.
446 # We don't use awk functions, to stay compatible with old awk versions.
447 # `Log' is the log message (with \n replaced by \001).
448 # `files' contains the affected files.
449 printlogline='{
450
451 # Following the GNU coding standards, rewrite
452 # * file: (function): comment
453 # to
454 # * file (function): comment
455 if (Log ~ /^\([^)]*\): /) {
456 i = index(Log, ")")
457 files = files " " substr(Log, 1, i)
458 Log = substr(Log, i+3)
459 }
460
461 # If "label: comment" is too long, break the line after the ":".
462 sep = " "
463 if ('"$length"' <= '"$indent"' + 1 + length(files) + index(Log, SOH)) sep = "\n" indent_string
464
465 # Print the label.
466 printf "%s*%s:", indent_string, files
467
468 # Print each line of the log, transliterating \001 to \n.
469 while ((i = index(Log, SOH)) != 0) {
470 logline = substr(Log, 1, i-1)
471 if (logline ~ /[^'"$tab"' ]/) {
472 printf "%s%s\n", sep, logline
473 } else {
474 print ""
475 }
476 sep = indent_string
477 Log = substr(Log, i+1)
478 }
479 }'
480
481 # Pattern to match the `revision' line of rlog output.
482 rlog_revision_pattern='^revision [0-9]+\.[0-9]+(\.[0-9]+\.[0-9]+)*(['"$tab"' ]+locked by: [^'"$tab"' $,.0-9:;@]*[^'"$tab"' $,:;@][^'"$tab"' $,.0-9:;@]*;)?['"$tab"' ]*$'
483
484 case $hostname in
485 '')
486 hostname=`(
487 hostname || uname -n || uuname -l || cat /etc/whoami
488 ) 2>/dev/null` || {
489 echo >&2 "$0: cannot deduce hostname"
490 exit 1
491 }
492
493 case $hostname in
494 *.*) ;;
495 *)
496 domainname=`(domainname) 2>/dev/null` &&
497 case $domainname in
498 *.*) hostname=$hostname.$domainname
499 esac
500 esac
501 esac
502
503
504 # Process the rlog output, generating ChangeLog style entries.
505
506 # First, reformat the rlog output so that each line contains one log entry.
507 # Transliterate \n to \001 so that multiline entries fit on a single line.
508 # Discard irrelevant rlog output.
509 $AWK <$rlogout '
510 BEGIN { repository = "'"$repository"'" }
511 /^RCS file:/ {
512 if (repository != "") {
513 filename = $3
514 if (substr(filename, 1, length(repository) + 1) == repository "/") {
515 filename = substr(filename, length(repository) + 2)
516 }
517 if (filename ~ /,v$/) {
518 filename = substr(filename, 1, length(filename) - 2)
519 }
520 if (filename ~ /(^|\/)Attic\/[^\/]*$/) {
521 i = length(filename)
522 while (substr(filename, i, 1) != "/") i--
523 filename = substr(filename, 1, i - 6) substr(filename, i + 1)
524 }
525 }
526 rev = "?"
527 }
528 /^Working file:/ { if (repository == "") filename = $3 }
529 /'"$rlog_revision_pattern"'/, /^(-----------*|===========*)$/ {
530 line = $0
531 if (line ~ /'"$rlog_revision_pattern"'/) {
532 rev = $2
533 next
534 }
535 if (line ~ /^date: [0-9][- +\/0-9:]*;/) {
536 date = $2
537 if (date ~ /\//) {
538 # This is a traditional RCS format date YYYY/MM/DD.
539 # Replace "/"s with "-"s to get ISO format.
540 newdate = ""
541 while ((i = index(date, "/")) != 0) {
542 newdate = newdate substr(date, 1, i-1) "-"
543 date = substr(date, i+1)
544 }
545 date = newdate date
546 }
547 time = substr($3, 1, length($3) - 1)
548 author = substr($5, 1, length($5)-1)
549 printf "%s %s %s %s %s %c", filename, rev, date, time, author, 1
550 rev = "?"
551 next
552 }
553 if (line ~ /^branches: /) { next }
554 if (line ~ /^(-----------*|===========*)$/) { print ""; next }
555 if (line == "Initial revision" || line ~ /^file .+ was initially added on branch .+\.$/) {
556 line = "New file."
557 }
558 printf "%s%c", line, 1
559 }
560 ' |
561
562 # Now each line is of the form
563 # FILENAME REVISION YYYY-MM-DD HH:MM:SS[+-TIMEZONE] AUTHOR \001LOG
564 # where \001 stands for a carriage return,
565 # and each line of the log is terminated by \001 instead of \n.
566 # Sort the log entries, first by date+time (in reverse order),
567 # then by author, then by log entry, and finally by file name and revision
568 # (just in case).
569 sort +2 -4r +4 +0 |
570
571 # Finally, reformat the sorted log entries.
572 $AWK '
573 BEGIN {
574 logTZ = "'"$logTZ"'"
575 revision = "'"$revision"'"
576
577 # Some awk variants do not understand "\001", so we have to
578 # put the char directly in the file.
579 SOH="\ 1" # <-- There is a single SOH (octal code 001) here.
580
581 # Initialize the fullname and mailaddr associative arrays.
582 '"$initialize_fullname"'
583 '"$initialize_mailaddr"'
584
585 # Initialize indent string.
586 indent_string = ""
587 i = '"$indent"'
588 if (0 < '"$tabwidth"')
589 for (; '"$tabwidth"' <= i; i -= '"$tabwidth"')
590 indent_string = indent_string "\t"
591 while (1 <= i--)
592 indent_string = indent_string " "
593 }
594
595 {
596 newlog = substr($0, 1 + index($0, SOH))
597
598 # Ignore log entries prefixed by "#".
599 if (newlog ~ /^#/) { next }
600
601 if (Log != newlog || date != $3 || author != $5) {
602
603 # The previous log and this log differ.
604
605 # Print the old log.
606 if (date != "") '"$printlogline"'
607
608 # Logs that begin with "{clumpname} " should be grouped together,
609 # and the clumpname should be removed.
610 # Extract the new clumpname from the log header,
611 # and use it to decide whether to output a blank line.
612 newclumpname = ""
613 sep = "\n"
614 if (date == "") sep = ""
615 if (newlog ~ /^\{[^'"$tab"' }]*}['"$tab"' ]/) {
616 i = index(newlog, "}")
617 newclumpname = substr(newlog, 1, i)
618 while (substr(newlog, i+1) ~ /^['"$tab"' ]/) i++
619 newlog = substr(newlog, i+1)
620 if (clumpname == newclumpname) sep = ""
621 }
622 printf sep
623 clumpname = newclumpname
624
625 # Get ready for the next log.
626 Log = newlog
627 if (files != "")
628 for (i in filesknown)
629 filesknown[i] = 0
630 files = ""
631 }
632 if (date != $3 || author != $5) {
633 # The previous date+author and this date+author differ.
634 # Print the new one.
635 date = $3
636 time = $4
637 author = $5
638
639 zone = ""
640 if (logTZ && ((i = index(time, "-")) || (i = index(time, "+"))))
641 zone = " " substr(time, i)
642
643 # Print "date[ timezone] fullname <email address>".
644 # Get fullname and email address from associative arrays;
645 # default to author and author@hostname if not in arrays.
646 if (fullname[author])
647 auth = fullname[author]
648 else
649 auth = author
650 printf "%s%s %s ", date, zone, auth
651 if (mailaddr[author])
652 printf "<%s>\n\n", mailaddr[author]
653 else
654 printf "<%s@%s>\n\n", author, "'"$hostname"'"
655 }
656 if (! filesknown[$1]) {
657 filesknown[$1] = 1
658 if (files == "") files = " " $1
659 else files = files ", " $1
660 if (revision && $2 != "?") files = files " " $2
661 }
662 }
663 END {
664 # Print the last log.
665 if (date != "") {
666 '"$printlogline"'
667 printf "\n"
668 }
669 }
670 ' &&
671
672
673 # Exit successfully.
674
675 exec rm -fr $logdir
676
677 # Local Variables:
678 # tab-width:4
679 # End: