Welcome, guest | Sign In | My Account | Store | Cart

Read and write version history using TLIB version control format.

Python, 172 lines
  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
import re, os, sys
from difflib import SequenceMatcher, context_diff
from datetime import datetime
from itertools import islice

vline = re.compile(r'\.V(?:_\S+)? (\S+) (\S+) ?(.*)').match
nline = re.compile(r'\.N (.*)').match
cline = re.compile(r'\.C (\d+) (\d+)').match
iline = re.compile(r'\.I (\d+)').match

def get_version(repo_fn, v=None):
    # Return version *v* or the last version if *v* is None.
    if not os.path.exists(repo_fn):
        return ''

    currver = 0
    curr = []
    f = iter(open(repo_fn, 'r'))
    line = next(f, '')
    while line and (v is None or currver < v):

        # Start building up next version from the last
        currver += 1
        prev, curr = curr, []

        # Process mandatory .V line and optional .N msg lines
        assert line.startswith('.V')
        for line in f:
            if not line.startswith('.N'):
                break

        # Process the .I and .C instructions
        while line and line.startswith(('.I', '.C')):
            if line.startswith('.I'):
                n = int(iline(line).group(1))
                curr.extend(islice(f, n))
            else:
                m, n = map(int, cline(line).groups())
                curr.extend(prev[m-1:n])
            line = next(f, '')

    return ''.join(curr)

def print_log(repo_fn, v=None):
    # Print log entries. *v* is a specific version number or None to print all.
    currver = 0
    f = iter(open(repo_fn, 'r'))
    line = next(f, '')
    while line:

        # Process mandatory .V line and optional .N msg lines
        assert line.startswith('.V')
        repo_fn, datetime, msg = vline(line).groups()
        for line in f:
            if line.startswith('.N'):
                msg += '\n' + nline(line).group(1)
            else:
                break

        currver += 1
        if v is None:
            print "%s %d %18s %s" % (repo_fn, currver, datetime, msg)
        elif currver == v:            
            print "%s %d %18s %s" % (repo_fn, currver, datetime, msg)
            return

        # Skip through the .I and .C instructions
        while line and line.startswith(('.I', '.C')):
            if line.startswith('.I'):
                n = int(iline(line).group(1))
                for line in islice(f, n):
                    pass
            line = next(f, '')

def diff(repo_fn, vnum1=None, vnum2=None, context=False):
    # vnum1 or vnum2 can be None to indicate last version in repository
    # vnum2 can be a filename to compare to
    v1 = get_version(repo_fn, vnum1).splitlines(True)
    if isinstance(vnum2, int) or vnum2 is None:
        v2 = get_version(repo_fn, vnum2).splitlines(True)
    else:
        v2 = open(vnum2).readlines()
    results = []
    if context:
        return ''.join(context_diff(v1, v2))
    for tag, i1, i2, j1, j2 in SequenceMatcher(None, v1, v2).get_opcodes():
        if tag in ('replace', 'insert'):
            results.append('.I %d\n' % (j2-j1))
            results.extend(v2[j1:j2])
        elif tag == 'equal':
            results.append('.C %d %d\n' % (i1+1, i2))
    return ''.join(results)

def make_header(filename, msg, create):
    first = '_,03000' if create else ''
    datestring = datetime.now().strftime('%d-%b-%y,%H:%M:%S')
    return '.V%(first)s %(filename)s %(datestring)s %(msg)s' % locals()

def get_repo_fn(filename):
    path, fullname = os.path.split(filename)
    base, ext = os.path.splitext(fullname)
    ext = ext or '.'
    newext = ext[:2] + '$' + ext[3:]
    result = os.path.join(repo_dir, base + newext)
    return result

repo_dir = os.environ.get('VCS', '.')

# ------- Command-line interface -------

help_msg = '''
Usage:
    vcs add foo.bar "Checkin message"
    vcs extract foo.bar [revnum]
    vcs log foo.bar [revnum]
    vcs diff foo.bar [revnum1 [revnum2]]

Repository:
    %s
    ''' % repo_dir

def talkback(msg, help=False, code=1):
    print >> sys.stderr, msg
    if help:
        print >> sys.stderr, '\n' + help_msg
    sys.exit(code)

def main(argv):
    # XXX add support for branching
    # XXX support .N for output
    if len(argv) <= 1:
        talkback(help_msg, code=0)
    if len(argv) < 3:
        talkback('Not enough arguments. Need a command and filename.', help=True)
    command = argv[1].lower()
    if command not in 'log extract diff add update l e d a u'.split():
        talkback('Unknown command: ' + command, help=True)
    command = command[:1]
    filename = argv[2]
    repo_fn = get_repo_fn(filename)

    if command in 'le':
        if not os.path.exists(repo_fn):
            talkback(repo_fn + ' not found')
        v = int(argv[3]) if len(argv) >= 4 else None
        if command == 'l':
            print_log(repo_fn, v)
        else:
            print get_version(repo_fn, v),
    elif command == 'd':
        v1 = int(argv[3]) if len(argv) >= 4 else None
        v2 = int(argv[4]) if len(argv) >= 5 else filename
        print diff(repo_fn, v1, v2, context=True),
    elif command in 'au':
        if not os.path.exists(filename):
            talkback('Cannot find file: ' + filename)
        d = diff(repo_fn, None, filename)
        if len(d.splitlines()) == 1:
            talkback('File is already current. There are no changes.', code=0)
        msg = ' '.join(argv[3:])
        create = not os.path.exists(repo_fn)
        repo_file = open(repo_fn, 'a+')
        print >> repo_file, make_header(filename, msg, create)
        print >> repo_file, d,
        repo_file.close()
        talkback('Added to ' + repo_fn, code=0)
    else:
        talkback('Unreachable')


if __name__ == '__main__':
    main(sys.argv)

Add a file with: vcs a myfile.py Initial check-in

Update a file with: vcs u myfile.py Make some changes

List history: vcs l myfile.py

Show differences from the current version: vcs d myfile.py

Show difference from two checked-in versions: vcs d myfile 2 4

Extract a file with: vcs e myfile.py > myfile.py

3 comments

Gary Eakins 12 years, 7 months ago  # | flag

I had to define the next() function to get this to work in Python 2.5:

def next(o, default):
    try:
        return o.next()
    except StopIteration:
        return default
Johannes 12 years, 1 month ago  # | flag

It is unfortunately useless since it does not deal with deleted lines.

I am unfortunately ignorant on the tlib format so I don't know how to fix it.

/Johannes

Raymond Hettinger (author) 11 years, 3 months ago  # | flag

Johannes, the copy and insert style does not need deletes. It copies only the parts that are re-used and ignores the rest.

If lines abcdefgh get converted to xyabczef, the transformation is recorded as Insert xy, Copy abc, Insert z, Copy ef.

The deletes are implicit in that they are the lines that are not copied :-)