/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.5.1 by John Arbash Meinel
Just an initial working step.
1
#!/usr/bin/env python
2
"""\
3
Just some work for generating a changeset.
4
"""
5
6
import bzrlib, bzrlib.errors
7
8
def _canonicalize_revision(branch, revision=None):
9
    """Turn some sort of revision information into a single
10
    set of from-to revision ids.
11
12
    A revision id can be none if there is no associated revison.
13
    """
14
    # This is a little clumsy because revision parsing may return
15
    # a single entry, or a list
16
    if revision is None:
17
        old, new = None, None
18
    elif isinstance(revision, (list, tuple)):
19
        if len(revision) == 0:
20
            old, new = None, None
21
        elif len(revision) == 1:
22
            old = revision[0]
23
            new = None
24
        elif len(revision) == 2:
25
            old = revision[0]
26
            new = revision[1]
27
        else:
28
            raise bzrlib.errors.BzrCommandError('revision can be'
29
                    ' at most 2 entries.')
30
    else:
31
        old = revision
32
        new = None
33
34
    if new is not None:
35
        new = branch.lookup_revision(new)
36
        if old is None:
37
            # Get the ancestor previous version
38
            rev = branch.get_revision(new)
39
            old = rev.precursor
40
        else:
41
            old = branch.lookup_revision(old)
42
    else:
43
        if old is None:
44
            old = branch.last_patch()
45
        else:
46
            old = branch.lookup_revision(old)
47
48
    return old, new
49
50
def _get_trees(branch, revisions):
51
    """Get the old and new trees based on revision.
52
    """
53
    if revisions[0] is None:
54
        old_tree = branch.basis_tree()
55
    else:
56
        old_tree = branch.revision_tree(revisions[0])
57
58
    if revisions[1] is None:
59
        new_tree = branch.working_tree()
60
    else:
61
        new_tree = branch.revision_tree(revisions[1])
62
    return old_tree, new_tree
63
64
def _fake_working_revision(branch):
65
    """Fake a Revision object for the working tree."""
66
    from bzrlib.revision import Revision
67
    import time
68
    from bzrlib.osutils import local_time_offset, \
69
            username
70
71
    precursor = branch.last_patch()
72
    precursor_sha1 = branch.get_revision_sha1(precursor)
73
74
    return Revision(timestamp=time.time(),
75
            timezone=local_time_offset(),
76
            committer=username(),
77
            precursor=precursor,
78
            precursor_sha1=precursor_sha1)
79
80
81
class MetaInfoHeader(object):
82
    """Maintain all of the header information about this
83
    changeset.
84
    """
85
86
    def __init__(self, branch, revisions, delta):
87
        self.branch = branch
88
        self.delta = delta
89
        self._get_revision_list(revisions)
90
        self._get_all_meta_info()
91
92
    def _get_revision_list(self, revisions):
93
        """This generates the list of all revisions from->to.
94
        """
95
        old_revno = None
96
        new_revno = None
97
        rh = self.branch.revision_history()
98
        for revno, rev in enumerate(rh):
99
            if rev == revisions[0]:
100
                old_revno = revno
101
            if rev == revisions[1]:
102
                new_revno = revno
103
104
        if old_revno is None:
105
            raise bzrlib.errors.BzrError('Could not find revision for %s' % revisions[0])
106
107
        if new_revno is not None:
108
            for rev_id in rh[old_revno:new_revno+1]:
109
                self.revision_list.append(self.branch.get_revision(rev_id))
110
        else:
111
            for rev_id in rh[old_revno:]:
112
                self.revision_list.append(self.branch.get_revision(rev_id))
113
            self.revision_list.append(_fake_working_revision(self.branch))
114
115
    def write_meta_info(self, to_file):
116
        """Write out the meta-info portion to the supplied file."""
117
        from bzrlib.osutils import username
118
        def write(key, value):
119
            to_file.write('# ' + key + ': ' + value + '\n')
120
121
        write('committer', username())
122
123
124
def show_changeset(branch, revision=None, specific_files=None,
125
        external_diff_options=None, to_file=None):
126
    from bzrlib.diff import compare_trees
127
128
    if to_file is None:
129
        import sys
130
        to_file = sys.stdout
131
    revisions = _canonicalize_revision(branch, revision)
132
    print "Canonicalized revisions: %s" % (revisions,)
133
134
    old_tree, new_tree = _get_trees(branch, revisions)
135
136
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
137
                          specific_files=specific_files)
138
139
    meta = MetaInfoHeader(branch, revisions, delta)
140
    meta.write_meta_info(to_file)
141
142