~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weavefile.py

  • Committer: Martin Pool
  • Date: 2005-07-11 03:40:02 UTC
  • Revision ID: mbp@sourcefrog.net-20050711034002-575d84b4c7514542
- commit command refuses unless something is changed or --unchanged is given

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
1
3
# Copyright (C) 2005 Canonical Ltd
2
4
 
3
5
# This program is free software; you can redistribute it and/or modify
24
26
There is one format marker followed by a blank line, followed by a
25
27
series of version headers, followed by the weave itself.
26
28
 
27
 
Each version marker has
28
 
 
29
 
 'i'   parent version indexes
30
 
 '1'   SHA-1 of text
31
 
 'n'   name
32
 
 
33
 
The inclusions do not need to list versions included by a parent.
 
29
Each version marker has 'i' and the included previous versions, then
 
30
'1' and the SHA-1 of the text, if known.  The inclusions do not need
 
31
to list versions included by a parent.
34
32
 
35
33
The weave is bracketed by 'w' and 'W' lines, and includes the '{}[]'
36
34
processing instructions.  Lines of text are prefixed by '.' if the
38
36
"""
39
37
 
40
38
# TODO: When extracting a single version it'd be enough to just pass
41
 
# an iterator returning the weave lines...  We don't really need to
42
 
# deserialize it into memory.
 
39
# an iterator returning the weave lines...
43
40
 
44
 
FORMAT_1 = '# bzr weave file v5\n'
 
41
FORMAT_1 = '# bzr weave file v3\n'
45
42
 
46
43
 
47
44
def write_weave(weave, f, format=None):
48
45
    if format == None or format == 1:
49
 
        return write_weave_v5(weave, f)
 
46
        return write_weave_v1(weave, f)
50
47
    else:
51
48
        raise ValueError("unknown weave format %r" % format)
52
49
 
53
50
 
54
 
def write_weave_v5(weave, f):
 
51
def write_weave_v1(weave, f):
55
52
    """Write weave to file f."""
56
53
    print >>f, FORMAT_1,
57
54
 
58
 
    for version, included in enumerate(weave._parents):
 
55
    for version, included in enumerate(weave._v):
59
56
        if included:
60
 
            # mininc = weave.minimal_parents(version)
61
 
            mininc = included
 
57
            # find a minimal expression of it; bias towards using
 
58
            # later revisions
 
59
            li = list(included)
 
60
            li.sort()
 
61
            li.reverse()
 
62
 
 
63
            mininc = []
 
64
            gotit = set()
 
65
 
 
66
            for pv in li:
 
67
                if pv not in gotit:
 
68
                    mininc.append(pv)
 
69
                    gotit.update(weave._v[pv])
 
70
 
 
71
            assert mininc[0] >= 0
 
72
            assert mininc[-1] < version
62
73
            print >>f, 'i',
63
74
            for i in mininc:
64
75
                print >>f, i,
66
77
        else:
67
78
            print >>f, 'i'
68
79
        print >>f, '1', weave._sha1s[version]
69
 
        print >>f, 'n', weave._names[version]
70
80
        print >>f
71
81
 
72
82
    print >>f, 'w'
73
83
 
74
 
    for l in weave._weave:
 
84
    for l in weave._l:
75
85
        if isinstance(l, tuple):
76
86
            assert l[0] in '{}[]'
77
 
            if l[0] == '}':
78
 
                print >>f, '}'
79
 
            else:
80
 
                print >>f, '%s %d' % l
 
87
            print >>f, '%s %d' % l
81
88
        else: # text line
82
89
            if not l:
83
90
                print >>f, ', '
93
100
 
94
101
 
95
102
def read_weave(f):
96
 
    return read_weave_v5(f)
97
 
 
98
 
 
99
 
def read_weave_v5(f):
 
103
    return read_weave_v1(f)
 
104
 
 
105
 
 
106
def read_weave_v1(f):
100
107
    from weave import Weave, WeaveFormatError
101
 
    w = Weave(getattr(f, 'name', None))
 
108
    w = Weave()
102
109
 
 
110
    wfe = WeaveFormatError
103
111
    l = f.readline()
104
112
    if l != FORMAT_1:
105
113
        raise WeaveFormatError('invalid weave file header: %r' % l)
108
116
    while True:
109
117
        l = f.readline()
110
118
        if l[0] == 'i':
 
119
            ver += 1
 
120
 
111
121
            if len(l) > 2:
112
 
                w._parents.append(map(int, l[2:].split(' ')))
 
122
                included = map(int, l[2:].split(' '))
 
123
                full = set()
 
124
                for pv in included:
 
125
                    full.add(pv)
 
126
                    full.update(w._v[pv])
 
127
                w._addversion(full)
113
128
            else:
114
 
                w._parents.append([])
 
129
                w._addversion(None)
115
130
 
116
131
            l = f.readline()[:-1]
117
132
            assert l.startswith('1 ')
118
133
            w._sha1s.append(l[2:])
119
134
                
120
135
            l = f.readline()
121
 
            assert l.startswith('n ')
122
 
            name = l[2:-1]
123
 
            assert name not in w._name_map
124
 
            w._names.append(name)
125
 
            w._name_map[name] = ver
126
 
                
127
 
            l = f.readline()
128
136
            assert l == '\n'
129
 
 
130
 
            ver += 1
131
137
        elif l == 'w\n':
132
138
            break
133
139
        else:
138
144
        if l == 'W\n':
139
145
            break
140
146
        elif l.startswith('. '):
141
 
            w._weave.append(l[2:])  # include newline
 
147
            w._l.append(intern(l[2:]))  # include newline
142
148
        elif l.startswith(', '):
143
 
            w._weave.append(l[2:-1])        # exclude newline
144
 
        elif l == '}\n':
145
 
            w._weave.append(('}', None))
 
149
            w._l.append(l[2:-1])        # exclude newline
146
150
        else:
147
 
            assert l[0] in '{[]', l
 
151
            assert l[0] in '{}[]', l
148
152
            assert l[1] == ' ', l
149
 
            w._weave.append((intern(l[0]), int(l[2:])))
 
153
            w._l.append((intern(l[0]), int(l[2:])))
150
154
 
151
155
    return w
152
156