Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Consolidate a bunch of CVS or RCS logs read from stdin. |
| 4 | |
| 5 | Input should be the output of a CVS or RCS logging command, e.g. |
| 6 | |
Guido van Rossum | 9971f68 | 1997-10-06 21:09:32 +0000 | [diff] [blame] | 7 | cvs log -rrelease14: |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 8 | |
| 9 | which dumps all log messages from release1.4 upwards (assuming that |
Guido van Rossum | 9971f68 | 1997-10-06 21:09:32 +0000 | [diff] [blame] | 10 | release 1.4 was tagged with tag 'release14'). Note the trailing |
| 11 | colon! |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 12 | |
| 13 | This collects all the revision records and outputs them sorted by date |
| 14 | rather than by file, collapsing duplicate revision record, i.e., |
| 15 | records with the same message for different files. |
| 16 | |
| 17 | The -t option causes it to truncate (discard) the last revision log |
| 18 | entry; this is useful when using something like the above cvs log |
| 19 | command, which shows the revisions including the given tag, while you |
| 20 | probably want everything *since* that tag. |
| 21 | |
Guido van Rossum | 9971f68 | 1997-10-06 21:09:32 +0000 | [diff] [blame] | 22 | XXX This code was created by reverse engineering CVS 1.9 and RCS 5.7 |
| 23 | from their output. |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 24 | |
| 25 | """ |
| 26 | |
| 27 | import os, sys, getopt, string, re |
| 28 | |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 29 | sep1 = '='*77 + '\n' # file separator |
| 30 | sep2 = '-'*28 + '\n' # revision separator |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 31 | |
| 32 | def main(): |
| 33 | """Main program""" |
| 34 | truncate_last = 0 |
Guido van Rossum | d962878 | 2000-02-14 21:41:50 +0000 | [diff] [blame] | 35 | reverse = 0 |
| 36 | opts, args = getopt.getopt(sys.argv[1:], "tr") |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 37 | for o, a in opts: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 38 | if o == '-t': |
| 39 | truncate_last = 1 |
Guido van Rossum | d962878 | 2000-02-14 21:41:50 +0000 | [diff] [blame] | 40 | elif o == '-r': |
| 41 | reverse = 1 |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 42 | database = [] |
| 43 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 44 | chunk = read_chunk(sys.stdin) |
| 45 | if not chunk: |
| 46 | break |
| 47 | records = digest_chunk(chunk) |
| 48 | if truncate_last: |
| 49 | del records[-1] |
| 50 | database[len(database):] = records |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 51 | database.sort() |
Guido van Rossum | d962878 | 2000-02-14 21:41:50 +0000 | [diff] [blame] | 52 | if not reverse: |
| 53 | database.reverse() |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 54 | format_output(database) |
| 55 | |
| 56 | def read_chunk(fp): |
| 57 | """Read a chunk -- data for one file, ending with sep1. |
| 58 | |
| 59 | Split the chunk in parts separated by sep2. |
| 60 | |
| 61 | """ |
| 62 | chunk = [] |
| 63 | lines = [] |
| 64 | while 1: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 65 | line = fp.readline() |
| 66 | if not line: |
| 67 | break |
| 68 | if line == sep1: |
| 69 | if lines: |
| 70 | chunk.append(lines) |
| 71 | break |
| 72 | if line == sep2: |
| 73 | if lines: |
| 74 | chunk.append(lines) |
| 75 | lines = [] |
| 76 | else: |
| 77 | lines.append(line) |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 78 | return chunk |
| 79 | |
| 80 | def digest_chunk(chunk): |
| 81 | """Digest a chunk -- extrach working file name and revisions""" |
| 82 | lines = chunk[0] |
| 83 | key = 'Working file:' |
| 84 | keylen = len(key) |
| 85 | for line in lines: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 86 | if line[:keylen] == key: |
| 87 | working_file = string.strip(line[keylen:]) |
| 88 | break |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 89 | else: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 90 | working_file = None |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 91 | records = [] |
| 92 | for lines in chunk[1:]: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 93 | revline = lines[0] |
| 94 | dateline = lines[1] |
| 95 | text = lines[2:] |
| 96 | words = string.split(dateline) |
| 97 | author = None |
| 98 | if len(words) >= 3 and words[0] == 'date:': |
| 99 | dateword = words[1] |
| 100 | timeword = words[2] |
| 101 | if timeword[-1:] == ';': |
| 102 | timeword = timeword[:-1] |
| 103 | date = dateword + ' ' + timeword |
| 104 | if len(words) >= 5 and words[3] == 'author:': |
| 105 | author = words[4] |
| 106 | if author[-1:] == ';': |
| 107 | author = author[:-1] |
| 108 | else: |
| 109 | date = None |
| 110 | text.insert(0, revline) |
| 111 | words = string.split(revline) |
| 112 | if len(words) >= 2 and words[0] == 'revision': |
| 113 | rev = words[1] |
| 114 | else: |
| 115 | rev = None |
| 116 | text.insert(0, revline) |
| 117 | records.append((date, working_file, rev, author, text)) |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 118 | return records |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 120 | def format_output(database): |
| 121 | prevtext = None |
| 122 | prev = [] |
Guido van Rossum | 9971f68 | 1997-10-06 21:09:32 +0000 | [diff] [blame] | 123 | database.append((None, None, None, None, None)) # Sentinel |
| 124 | for (date, working_file, rev, author, text) in database: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 125 | if text != prevtext: |
| 126 | if prev: |
| 127 | print sep2, |
| 128 | for (p_date, p_working_file, p_rev, p_author) in prev: |
| 129 | print p_date, p_author, p_working_file |
| 130 | sys.stdout.writelines(prevtext) |
| 131 | prev = [] |
| 132 | prev.append((date, working_file, rev, author)) |
| 133 | prevtext = text |
Guido van Rossum | 6f0cf7e | 1997-08-14 22:04:00 +0000 | [diff] [blame] | 134 | |
| 135 | main() |