(TAGS): Use $(srcdir) in deps.
[emacs.git] / lib-src / rcs2log
blob16d7a4cd1d15e55b85b868794676b7042073c6ed
1 #! /bin/sh
3 # RCS to ChangeLog generator
5 # Generate a change log prefix from RCS files and the ChangeLog (if any).
6 # Output the new prefix to standard output.
7 # You can edit this prefix by hand, and then prepend it to ChangeLog.
9 # Ignore log entries that start with `#'.
10 # Clump together log entries that start with `{topic} ',
11 # where `topic' contains neither white space nor `}'.
13 # Author: Paul Eggert <eggert@twinsun.com>
15 # $Id: rcs2log,v 1.29 1996/08/24 21:11:14 erik Exp eggert $
17 # Copyright 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2, or (at your option)
22 # any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; see the file COPYING. If not, write to the
31 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
32 # Boston, MA 02111-1307, USA.
34 tab=' '
35 nl='
38 # Parse options.
40 # defaults
41 : ${AWK=awk}
42 : ${TMPDIR=/tmp}
43 changelog=ChangeLog # change log file name
44 datearg= # rlog date option
45 hostname= # name of local host (if empty, will deduce it later)
46 indent=8 # indent of log line
47 length=79 # suggested max width of log line
48 logins= # login names for people we know fullnames and mailaddrs of
49 loginFullnameMailaddrs= # login<tab>fullname<tab>mailaddr triplets
50 logTZ= # time zone for log dates (if empty, use local time)
51 recursive= # t if we want recursive rlog
52 revision= # t if we want revision numbers
53 rlog_options= # options to pass to rlog
54 tabwidth=8 # width of horizontal tab
56 while :
58 case $1 in
59 -c) changelog=${2?}; shift;;
60 -i) indent=${2?}; shift;;
61 -h) hostname=${2?}; shift;;
62 -l) length=${2?}; shift;;
63 -[nu]) # -n is obsolescent; it is replaced by -u.
64 case $1 in
65 -n) case ${2?}${3?}${4?} in
66 *"$tab"* | *"$nl"*)
67 echo >&2 "$0: -n '$2' '$3' '$4': tabs, newlines not allowed"
68 exit 1
69 esac
70 loginFullnameMailaddrs=$loginFullnameMailaddrs$nl$2$tab$3$tab$4
71 shift; shift; shift;;
72 -u)
73 # If $2 is not tab-separated, use colon for separator.
74 case ${2?} in
75 *"$nl"*)
76 echo >&2 "$0: -u '$2': newlines not allowed"
77 exit 1;;
78 *"$tab"*)
79 t=$tab;;
81 t=:
82 esac
83 case $2 in
84 *"$t"*"$t"*"$t"*)
85 echo >&2 "$0: -u '$2': too many fields"
86 exit 1;;
87 *"$t"*"$t"*)
90 echo >&2 "$0: -u '$2': not enough fields"
91 exit 1
92 esac
93 loginFullnameMailaddrs=$loginFullnameMailaddrs$nl$2
94 shift
95 esac
96 logins=$logins$nl$login
98 -r) rlog_options=$rlog_options$nl${2?}; shift;;
99 -R) recursive=t;;
100 -t) tabwidth=${2?}; shift;;
101 -v) revision=t;;
102 -*) echo >&2 "$0: usage: $0 [options] [file ...]
103 Options:
104 [-c changelog] [-h hostname] [-i indent] [-l length] [-R]
105 [-r rlog_option] [-t tabwidth] [-v]
106 [-u 'login<TAB>fullname<TAB>mailaddr']..."
107 exit 1;;
108 *) break
109 esac
110 shift
111 done
113 month_data='
114 m[0]="Jan"; m[1]="Feb"; m[2]="Mar"
115 m[3]="Apr"; m[4]="May"; m[5]="Jun"
116 m[6]="Jul"; m[7]="Aug"; m[8]="Sep"
117 m[9]="Oct"; m[10]="Nov"; m[11]="Dec"
121 # Put rlog output into $rlogout.
123 # If no rlog options are given,
124 # log the revisions checked in since the first ChangeLog entry.
125 # Since ChangeLog is only by date, some of these revisions may be duplicates of
126 # what's already in ChangeLog; it's the user's responsibility to remove them.
127 case $rlog_options in
129 date=1970
130 if test -s "$changelog"
131 then
133 /^[0-9]+-[0-9][0-9]-[0-9][0-9]/{
134 # ISO 8601 date
135 print $1
136 exit
138 /^... ... [ 0-9][0-9] [ 0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9]+ /{
139 # old-fashioned date and time (Emacs 19.31 and earlier)
140 '"$month_data"'
141 year = $5
142 for (i=0; i<=11; i++) if (m[i] == $2) break
143 dd = $3
144 printf "%d-%02d-%02d\n", year, i+1, dd
145 exit
148 d=`$AWK "$e" <"$changelog"` || exit
149 case $d in
150 ?*) date=$d
151 esac
153 datearg="-d>$date"
154 esac
156 # Use rlog's -zLT option, if rlog supports it.
157 case `rlog -zLT 2>&1` in
158 *'unknown option'*) ;;
159 *) rlog_options=-zLT$nl$rlog_options
160 esac
162 # Use TZ specified by ChangeLog local variable, if any.
163 if test -s "$changelog"
164 then
165 extractTZ='
166 /^.*change-log-time-zone-rule['"$tab"' ]*:['"$tab"' ]*"\([^"]*\)".*/{
167 s//\1/; p; q
169 /^.*change-log-time-zone-rule['"$tab"' ]*:['"$tab"' ]*t.*/{
170 s//UTC0/; p; q
173 logTZ=`tail "$changelog" | sed -n "$extractTZ"`
174 case $logTZ in
175 ?*) TZ=$logTZ; export TZ
176 esac
179 # If CVS is in use, examine its repository, not the normal RCS files.
180 if test ! -f CVS/Repository
181 then
182 rlog=rlog
183 repository=
184 else
185 rlog='cvs log'
186 repository=`sed 1q <CVS/Repository` || exit
187 test ! -f CVS/Root || CVSROOT=`cat <CVS/Root` || exit
188 case $CVSROOT in
189 *:/*)
190 # remote repository
193 # local repository
194 case $repository in
195 /*) ;;
196 *) repository=${CVSROOT?}/$repository
197 esac
198 if test ! -d "$repository"
199 then
200 echo >&2 "$0: $repository: bad repository (see CVS/Repository)"
201 exit 1
203 esac
206 # With no arguments, examine all files under the RCS directory.
207 case $# in
209 case $repository in
211 oldIFS=$IFS
212 IFS=$nl
213 case $recursive in
215 RCSdirs=`find . -name RCS -type d -print`
216 filesFromRCSfiles='s|,v$||; s|/RCS/|/|; s|^\./||'
217 files=`
219 case $RCSdirs in
220 ?*) find $RCSdirs -type f -print
221 esac
222 find . -name '*,v' -print
224 sort -u |
225 sed "$filesFromRCSfiles"
228 files=
229 for file in RCS/.* RCS/* .*,v *,v
231 case $file in
232 RCS/. | RCS/..) continue;;
233 RCS/.\* | RCS/\* | .\*,v | \*,v) test -f "$file" || continue
234 esac
235 files=$files$nl$file
236 done
237 case $files in
238 '') exit 0
239 esac
240 esac
241 set x $files
242 shift
243 IFS=$oldIFS
244 esac
245 esac
247 llogout=$TMPDIR/rcs2log$$l
248 rlogout=$TMPDIR/rcs2log$$r
249 trap exit 1 2 13 15
250 trap "rm -f $llogout $rlogout; exit 1" 0
252 case $datearg in
253 ?*) $rlog $rlog_options "$datearg" ${1+"$@"} >$rlogout;;
254 '') $rlog $rlog_options ${1+"$@"} >$rlogout
255 esac || exit
258 # Get the full name of each author the logs mention, and set initialize_fullname
259 # to awk code that initializes the `fullname' awk associative array.
260 # Warning: foreign authors (i.e. not known in the passwd file) are mishandled;
261 # you have to fix the resulting output by hand.
263 initialize_fullname=
264 initialize_mailaddr=
266 case $loginFullnameMailaddrs in
268 case $loginFullnameMailaddrs in
269 *\"* | *\\*)
270 sed 's/["\\]/\\&/g' >$llogout <<EOF || exit
271 $loginFullnameMailaddrs
273 loginFullnameMailaddrs=`cat $llogout`
274 esac
276 oldIFS=$IFS
277 IFS=$nl
278 for loginFullnameMailaddr in $loginFullnameMailaddrs
280 case $loginFullnameMailaddr in
281 *"$tab"*) IFS=$tab;;
282 *) IFS=:
283 esac
284 set x $loginFullnameMailaddr
285 login=$2
286 fullname=$3
287 mailaddr=$4
288 initialize_fullname="$initialize_fullname
289 fullname[\"$login\"] = \"$fullname\""
290 initialize_mailaddr="$initialize_mailaddr
291 mailaddr[\"$login\"] = \"$mailaddr\""
292 done
293 IFS=$oldIFS
294 esac
296 case $llogout in
297 ?*) sort -u -o $llogout <<EOF || exit
298 $logins
300 esac
301 output_authors='/^date: / {
302 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 ~ /^[^;]*;$/) {
303 print substr($5, 1, length($5)-1)
306 authors=`
307 $AWK "$output_authors" <$rlogout |
308 case $llogout in
309 '') sort -u;;
310 ?*) sort -u | comm -23 - $llogout
311 esac
313 case $authors in
315 cat >$llogout <<EOF || exit
316 $authors
318 initialize_author_script='s/["\\]/\\&/g; s/.*/author[\"&\"] = 1/'
319 initialize_author=`sed -e "$initialize_author_script" <$llogout`
320 awkscript='
321 BEGIN {
322 alphabet = "abcdefghijklmnopqrstuvwxyz"
323 ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
324 '"$initialize_author"'
327 if (author[$1]) {
328 fullname = $5
329 if (fullname ~ /[0-9]+-[^(]*\([0-9]+\)$/) {
330 # Remove the junk from fullnames like "0000-Admin(0000)".
331 fullname = substr(fullname, index(fullname, "-") + 1)
332 fullname = substr(fullname, 1, index(fullname, "(") - 1)
334 if (fullname ~ /,[^ ]/) {
335 # Some sites put comma-separated junk after the fullname.
336 # Remove it, but leave "Bill Gates, Jr" alone.
337 fullname = substr(fullname, 1, index(fullname, ",") - 1)
339 abbr = index(fullname, "&")
340 if (abbr) {
341 a = substr($1, 1, 1)
342 A = a
343 i = index(alphabet, a)
344 if (i) A = substr(ALPHABET, i, 1)
345 fullname = substr(fullname, 1, abbr-1) A substr($1, 2) substr(fullname, abbr+1)
348 # Quote quotes and backslashes properly in full names.
349 # Do not use gsub; traditional awk lacks it.
350 quoted = ""
351 rest = fullname
352 for (;;) {
353 p = index(rest, "\\")
354 q = index(rest, "\"")
355 if (p) {
356 if (q && q<p) p = q
357 } else {
358 if (!q) break
359 p = q
361 quoted = quoted substr(rest, 1, p-1) "\\" substr(rest, p, 1)
362 rest = substr(rest, p+1)
365 printf "fullname[\"%s\"] = \"%s%s\"\n", $1, quoted, rest
366 author[$1] = 0
371 initialize_fullname=`
373 cat /etc/passwd
374 for author in $authors
375 do nismatch $author passwd.org_dir
376 done
377 ypmatch $authors passwd
378 ) 2>/dev/null |
379 $AWK -F: "$awkscript"
380 `$initialize_fullname
381 esac
384 # Function to print a single log line.
385 # We don't use awk functions, to stay compatible with old awk versions.
386 # `Log' is the log message (with \n replaced by \r).
387 # `files' contains the affected files.
388 printlogline='{
390 # Following the GNU coding standards, rewrite
391 # * file: (function): comment
392 # to
393 # * file (function): comment
394 if (Log ~ /^\([^)]*\): /) {
395 i = index(Log, ")")
396 files = files " " substr(Log, 1, i)
397 Log = substr(Log, i+3)
400 # If "label: comment" is too long, break the line after the ":".
401 sep = " "
402 if ('"$length"' <= '"$indent"' + 1 + length(files) + index(Log, CR)) sep = "\n" indent_string
404 # Print the label.
405 printf "%s*%s:", indent_string, files
407 # Print each line of the log, transliterating \r to \n.
408 while ((i = index(Log, CR)) != 0) {
409 logline = substr(Log, 1, i-1)
410 if (logline ~ /[^'"$tab"' ]/) {
411 printf "%s%s\n", sep, logline
412 } else {
413 print ""
415 sep = indent_string
416 Log = substr(Log, i+1)
420 # Pattern to match the `revision' line of rlog output.
421 rlog_revision_pattern='^revision [0-9]+\.[0-9]+(\.[0-9]+\.[0-9]+)*(['"$tab"' ]+locked by: [^'"$tab"' $,.0-9:;@]*[^'"$tab"' $,:;@][^'"$tab"' $,.0-9:;@]*;)?['"$tab"' ]*$'
423 case $hostname in
425 hostname=`(
426 hostname || uname -n || uuname -l || cat /etc/whoami
427 ) 2>/dev/null` || {
428 echo >&2 "$0: cannot deduce hostname"
429 exit 1
432 case $hostname in
433 *.*) ;;
435 domainname=`(domainname) 2>/dev/null` &&
436 case $domainname in
437 *.*) hostname=$hostname.$domainname
438 esac
439 esac
440 esac
443 # Process the rlog output, generating ChangeLog style entries.
445 # First, reformat the rlog output so that each line contains one log entry.
446 # Transliterate \n to \r so that multiline entries fit on a single line.
447 # Discard irrelevant rlog output.
448 $AWK <$rlogout '
449 BEGIN { repository = "'"$repository"'" }
450 /^RCS file:/ {
451 if (repository != "") {
452 filename = $3
453 if (substr(filename, 1, length(repository) + 1) == repository "/") {
454 filename = substr(filename, length(repository) + 2)
456 if (filename ~ /,v$/) {
457 filename = substr(filename, 1, length(filename) - 2)
460 rev = "?"
462 /^Working file:/ { if (repository == "") filename = $3 }
463 /'"$rlog_revision_pattern"'/, /^(-----------*|===========*)$/ {
464 if ($0 ~ /'"$rlog_revision_pattern"'/) {
465 rev = $2
466 next
468 if ($0 ~ /^date: [0-9][- +\/0-9:]*;/) {
469 date = $2
470 if (date ~ /\//) {
471 # This is a traditional RCS format date YYYY/MM/DD.
472 # Replace "/"s with "-"s to get ISO format.
473 newdate = ""
474 while ((i = index(date, "/")) != 0) {
475 newdate = newdate substr(date, 1, i-1) "-"
476 date = substr(date, i+1)
478 date = newdate date
480 time = substr($3, 1, length($3) - 1)
481 author = substr($5, 1, length($5)-1)
482 printf "%s %s %s %s %s %c", filename, rev, date, time, author, 13
483 rev = "?"
484 next
486 if ($0 ~ /^branches: /) { next }
487 if ($0 ~ /^(-----------*|===========*)$/) { print ""; next }
488 printf "%s%c", $0, 13
492 # Now each line is of the form
493 # FILENAME REVISION YYYY-MM-DD HH:MM:SS[+-TIMEZONE] AUTHOR \rLOG
494 # where \r stands for a carriage return,
495 # and each line of the log is terminated by \r instead of \n.
496 # Sort the log entries, first by date+time (in reverse order),
497 # then by author, then by log entry, and finally by file name and revision
498 # (just in case).
499 sort +2 -4r +4 +0 |
501 # Finally, reformat the sorted log entries.
502 $AWK '
503 BEGIN {
504 logTZ = "'"$logTZ"'"
505 revision = "'"$revision"'"
507 # Some awk variants do not understand "\r" or "\013", so we have to
508 # put a carriage return directly in the file.
509 CR=" " # <-- There is a single CR between the " chars here.
511 # Initialize the fullname and mailaddr associative arrays.
512 '"$initialize_fullname"'
513 '"$initialize_mailaddr"'
515 # Initialize indent string.
516 indent_string = ""
517 i = '"$indent"'
518 if (0 < '"$tabwidth"')
519 for (; '"$tabwidth"' <= i; i -= '"$tabwidth"')
520 indent_string = indent_string "\t"
521 while (1 <= i--)
522 indent_string = indent_string " "
526 newlog = substr($0, 1 + index($0, CR))
528 # Ignore log entries prefixed by "#".
529 if (newlog ~ /^#/) { next }
531 if (Log != newlog || date != $3 || author != $5) {
533 # The previous log and this log differ.
535 # Print the old log.
536 if (date != "") '"$printlogline"'
538 # Logs that begin with "{clumpname} " should be grouped together,
539 # and the clumpname should be removed.
540 # Extract the new clumpname from the log header,
541 # and use it to decide whether to output a blank line.
542 newclumpname = ""
543 sep = "\n"
544 if (date == "") sep = ""
545 if (newlog ~ /^\{[^'"$tab"' }]*}['"$tab"' ]/) {
546 i = index(newlog, "}")
547 newclumpname = substr(newlog, 1, i)
548 while (substr(newlog, i+1) ~ /^['"$tab"' ]/) i++
549 newlog = substr(newlog, i+1)
550 if (clumpname == newclumpname) sep = ""
552 printf sep
553 clumpname = newclumpname
555 # Get ready for the next log.
556 Log = newlog
557 if (files != "")
558 for (i in filesknown)
559 filesknown[i] = 0
560 files = ""
562 if (date != $3 || author != $5) {
563 # The previous date+author and this date+author differ.
564 # Print the new one.
565 date = $3
566 time = $4
567 author = $5
569 zone = ""
570 if (logTZ && ((i = index(time, "-")) || (i = index(time, "+"))))
571 zone = " " substr(time, i)
573 # Print "date[ timezone] fullname <email address>".
574 # Get fullname and email address from associative arrays;
575 # default to author and author@hostname if not in arrays.
576 if (fullname[author])
577 auth = fullname[author]
578 else
579 auth = author
580 printf "%s%s %s ", date, zone, auth
581 if (mailaddr[author])
582 printf "<%s>\n\n", mailaddr[author]
583 else
584 printf "<%s@%s>\n\n", author, "'"$hostname"'"
586 if (! filesknown[$1]) {
587 filesknown[$1] = 1
588 if (files == "") files = " " $1
589 else files = files ", " $1
590 if (revision && $2 != "?") files = files " " $2
593 END {
594 # Print the last log.
595 if (date != "") {
596 '"$printlogline"'
597 printf "\n"
600 ' &&
603 # Exit successfully.
605 exec rm -f $llogout $rlogout
607 # Local Variables:
608 # tab-width:4
609 # End: