/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Jelmer Vernooij
  • Date: 2009-03-12 00:34:43 UTC
  • mto: This revision was merged to the branch mainline in revision 4120.
  • Revision ID: jelmer@samba.org-20090312003443-j9a71s94kbu4kx04
Mention check-newsbugs.py in the release document.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2009 Canonical Ltd
 
1
#! /usr/bin/python
 
2
 
 
3
# Copyright (C) 2005 Canonical Ltd
2
4
#
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
12
14
#
13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
18
 
17
19
# Author: Martin Pool <mbp@canonical.com>
18
20
 
69
71
from copy import copy
70
72
from cStringIO import StringIO
71
73
import os
 
74
import time
 
75
import warnings
72
76
 
73
77
from bzrlib.lazy_import import lazy_import
74
78
lazy_import(globals(), """
77
81
from bzrlib import (
78
82
    errors,
79
83
    osutils,
 
84
    progress,
80
85
    )
81
86
from bzrlib.errors import (WeaveError, WeaveFormatError, WeaveParentMismatch,
82
87
        RevisionAlreadyPresent,
83
88
        RevisionNotPresent,
84
89
        UnavailableRepresentation,
 
90
        WeaveRevisionAlreadyPresent,
 
91
        WeaveRevisionNotPresent,
85
92
        )
86
93
from bzrlib.osutils import dirname, sha, sha_strings, split_lines
87
94
import bzrlib.patiencediff
92
99
    AbsentContentFactory,
93
100
    adapter_registry,
94
101
    ContentFactory,
95
 
    sort_groupcompress,
96
102
    VersionedFile,
97
103
    )
98
104
from bzrlib.weavefile import _read_weave_v5, write_weave_v5
315
321
            new_versions = tsort.topo_sort(parents)
316
322
            new_versions.extend(set(versions).difference(set(parents)))
317
323
            versions = new_versions
318
 
        elif ordering == 'groupcompress':
319
 
            parents = self.get_parent_map(versions)
320
 
            new_versions = sort_groupcompress(parents)
321
 
            new_versions.extend(set(versions).difference(set(parents)))
322
 
            versions = new_versions
323
324
        for version in versions:
324
325
            if version in self:
325
326
                yield WeaveContentFactory(version, self)
403
404
        version_id
404
405
            Symbolic name for this version.
405
406
            (Typically the revision-id of the revision that added it.)
406
 
            If None, a name will be allocated based on the hash. (sha1:SHAHASH)
407
407
 
408
408
        parents
409
409
            List or set of direct parent version numbers.
419
419
            sha1 = sha_strings(lines)
420
420
        if sha1 == nostore_sha:
421
421
            raise errors.ExistingContent
422
 
        if version_id is None:
423
 
            version_id = "sha1:" + sha1
424
422
        if version_id in self._name_map:
425
423
            return self._check_repeated_add(version_id, parents, lines, sha1)
426
424
 
968
966
        super(WeaveFile, self).insert_record_stream(stream)
969
967
        self._save()
970
968
 
 
969
    @deprecated_method(one_five)
 
970
    def join(self, other, pb=None, msg=None, version_ids=None,
 
971
             ignore_missing=False):
 
972
        """Join other into self and save."""
 
973
        super(WeaveFile, self).join(other, pb, msg, version_ids, ignore_missing)
 
974
        self._save()
 
975
 
971
976
 
972
977
def _reweave(wa, wb, pb=None, msg=None):
973
978
    """Combine two weaves and return the result.
1018
1023
        wr._add(name, lines, [wr._lookup(i) for i in combined_parents[name]])
1019
1024
    return wr
1020
1025
 
1021
 
 
1022
1026
def _reweave_parent_graphs(wa, wb):
1023
1027
    """Return combined parent ancestry for two weaves.
1024
1028
 
1029
1033
            p = combined.setdefault(name, set())
1030
1034
            p.update(map(weave._idx_to_name, weave._parents[idx]))
1031
1035
    return combined
 
1036
 
 
1037
 
 
1038
def weave_toc(w):
 
1039
    """Show the weave's table-of-contents"""
 
1040
    print '%6s %50s %10s %10s' % ('ver', 'name', 'sha1', 'parents')
 
1041
    for i in (6, 50, 10, 10):
 
1042
        print '-' * i,
 
1043
    print
 
1044
    for i in range(w.num_versions()):
 
1045
        sha1 = w._sha1s[i]
 
1046
        name = w._names[i]
 
1047
        parent_str = ' '.join(map(str, w._parents[i]))
 
1048
        print '%6d %-50.50s %10.10s %s' % (i, name, sha1, parent_str)
 
1049
 
 
1050
 
 
1051
 
 
1052
def weave_stats(weave_file, pb):
 
1053
    from bzrlib.weavefile import read_weave
 
1054
 
 
1055
    wf = file(weave_file, 'rb')
 
1056
    w = read_weave(wf)
 
1057
    # FIXME: doesn't work on pipes
 
1058
    weave_size = wf.tell()
 
1059
 
 
1060
    total = 0
 
1061
    vers = len(w)
 
1062
    for i in range(vers):
 
1063
        pb.update('checking sizes', i, vers)
 
1064
        for origin, lineno, line in w._extract([i]):
 
1065
            total += len(line)
 
1066
 
 
1067
    pb.clear()
 
1068
 
 
1069
    print 'versions          %9d' % vers
 
1070
    print 'weave file        %9d bytes' % weave_size
 
1071
    print 'total contents    %9d bytes' % total
 
1072
    print 'compression ratio %9.2fx' % (float(total) / float(weave_size))
 
1073
    if vers:
 
1074
        avg = total/vers
 
1075
        print 'average size      %9d bytes' % avg
 
1076
        print 'relative size     %9.2fx' % (float(weave_size) / float(avg))
 
1077
 
 
1078
 
 
1079
def usage():
 
1080
    print """bzr weave tool
 
1081
 
 
1082
Experimental tool for weave algorithm.
 
1083
 
 
1084
usage:
 
1085
    weave init WEAVEFILE
 
1086
        Create an empty weave file
 
1087
    weave get WEAVEFILE VERSION
 
1088
        Write out specified version.
 
1089
    weave check WEAVEFILE
 
1090
        Check consistency of all versions.
 
1091
    weave toc WEAVEFILE
 
1092
        Display table of contents.
 
1093
    weave add WEAVEFILE NAME [BASE...] < NEWTEXT
 
1094
        Add NEWTEXT, with specified parent versions.
 
1095
    weave annotate WEAVEFILE VERSION
 
1096
        Display origin of each line.
 
1097
    weave merge WEAVEFILE VERSION1 VERSION2 > OUT
 
1098
        Auto-merge two versions and display conflicts.
 
1099
    weave diff WEAVEFILE VERSION1 VERSION2
 
1100
        Show differences between two versions.
 
1101
 
 
1102
example:
 
1103
 
 
1104
    % weave init foo.weave
 
1105
    % vi foo.txt
 
1106
    % weave add foo.weave ver0 < foo.txt
 
1107
    added version 0
 
1108
 
 
1109
    (create updated version)
 
1110
    % vi foo.txt
 
1111
    % weave get foo.weave 0 | diff -u - foo.txt
 
1112
    % weave add foo.weave ver1 0 < foo.txt
 
1113
    added version 1
 
1114
 
 
1115
    % weave get foo.weave 0 > foo.txt       (create forked version)
 
1116
    % vi foo.txt
 
1117
    % weave add foo.weave ver2 0 < foo.txt
 
1118
    added version 2
 
1119
 
 
1120
    % weave merge foo.weave 1 2 > foo.txt   (merge them)
 
1121
    % vi foo.txt                            (resolve conflicts)
 
1122
    % weave add foo.weave merged 1 2 < foo.txt     (commit merged version)
 
1123
 
 
1124
"""
 
1125
 
 
1126
 
 
1127
 
 
1128
def main(argv):
 
1129
    import sys
 
1130
    import os
 
1131
    try:
 
1132
        import bzrlib
 
1133
    except ImportError:
 
1134
        # in case we're run directly from the subdirectory
 
1135
        sys.path.append('..')
 
1136
        import bzrlib
 
1137
    from bzrlib.weavefile import write_weave, read_weave
 
1138
    from bzrlib.progress import ProgressBar
 
1139
 
 
1140
    try:
 
1141
        import psyco
 
1142
        psyco.full()
 
1143
    except ImportError:
 
1144
        pass
 
1145
 
 
1146
    if len(argv) < 2:
 
1147
        usage()
 
1148
        return 0
 
1149
 
 
1150
    cmd = argv[1]
 
1151
 
 
1152
    def readit():
 
1153
        return read_weave(file(argv[2], 'rb'))
 
1154
 
 
1155
    if cmd == 'help':
 
1156
        usage()
 
1157
    elif cmd == 'add':
 
1158
        w = readit()
 
1159
        # at the moment, based on everything in the file
 
1160
        name = argv[3]
 
1161
        parents = map(int, argv[4:])
 
1162
        lines = sys.stdin.readlines()
 
1163
        ver = w.add(name, parents, lines)
 
1164
        write_weave(w, file(argv[2], 'wb'))
 
1165
        print 'added version %r %d' % (name, ver)
 
1166
    elif cmd == 'init':
 
1167
        fn = argv[2]
 
1168
        if os.path.exists(fn):
 
1169
            raise IOError("file exists")
 
1170
        w = Weave()
 
1171
        write_weave(w, file(fn, 'wb'))
 
1172
    elif cmd == 'get': # get one version
 
1173
        w = readit()
 
1174
        sys.stdout.writelines(w.get_iter(int(argv[3])))
 
1175
 
 
1176
    elif cmd == 'diff':
 
1177
        w = readit()
 
1178
        fn = argv[2]
 
1179
        v1, v2 = map(int, argv[3:5])
 
1180
        lines1 = w.get(v1)
 
1181
        lines2 = w.get(v2)
 
1182
        diff_gen = bzrlib.patiencediff.unified_diff(lines1, lines2,
 
1183
                                '%s version %d' % (fn, v1),
 
1184
                                '%s version %d' % (fn, v2))
 
1185
        sys.stdout.writelines(diff_gen)
 
1186
 
 
1187
    elif cmd == 'annotate':
 
1188
        w = readit()
 
1189
        # newline is added to all lines regardless; too hard to get
 
1190
        # reasonable formatting otherwise
 
1191
        lasto = None
 
1192
        for origin, text in w.annotate(int(argv[3])):
 
1193
            text = text.rstrip('\r\n')
 
1194
            if origin == lasto:
 
1195
                print '      | %s' % (text)
 
1196
            else:
 
1197
                print '%5d | %s' % (origin, text)
 
1198
                lasto = origin
 
1199
 
 
1200
    elif cmd == 'toc':
 
1201
        weave_toc(readit())
 
1202
 
 
1203
    elif cmd == 'stats':
 
1204
        weave_stats(argv[2], ProgressBar())
 
1205
 
 
1206
    elif cmd == 'check':
 
1207
        w = readit()
 
1208
        pb = ProgressBar()
 
1209
        w.check(pb)
 
1210
        pb.clear()
 
1211
        print '%d versions ok' % w.num_versions()
 
1212
 
 
1213
    elif cmd == 'inclusions':
 
1214
        w = readit()
 
1215
        print ' '.join(map(str, w.inclusions([int(argv[3])])))
 
1216
 
 
1217
    elif cmd == 'parents':
 
1218
        w = readit()
 
1219
        print ' '.join(map(str, w._parents[int(argv[3])]))
 
1220
 
 
1221
    elif cmd == 'plan-merge':
 
1222
        # replaced by 'bzr weave-plan-merge'
 
1223
        w = readit()
 
1224
        for state, line in w.plan_merge(int(argv[3]), int(argv[4])):
 
1225
            if line:
 
1226
                print '%14s | %s' % (state, line),
 
1227
    elif cmd == 'merge':
 
1228
        # replaced by 'bzr weave-merge-text'
 
1229
        w = readit()
 
1230
        p = w.plan_merge(int(argv[3]), int(argv[4]))
 
1231
        sys.stdout.writelines(w.weave_merge(p))
 
1232
    else:
 
1233
        raise ValueError('unknown command %r' % cmd)
 
1234
 
 
1235
 
 
1236
if __name__ == '__main__':
 
1237
    import sys
 
1238
    sys.exit(main(sys.argv))