3
Just some work for generating a changeset.
6
import bzrlib, bzrlib.errors
8
def _canonicalize_revision(branch, revision=None):
9
"""Turn some sort of revision information into a single
10
set of from-to revision ids.
12
A revision id can be none if there is no associated revison.
14
# This is a little clumsy because revision parsing may return
15
# a single entry, or a list
18
elif isinstance(revision, (list, tuple)):
19
if len(revision) == 0:
21
elif len(revision) == 1:
24
elif len(revision) == 2:
28
raise bzrlib.errors.BzrCommandError('revision can be'
29
' at most 2 entries.')
35
new = branch.lookup_revision(new)
37
# Get the ancestor previous version
38
rev = branch.get_revision(new)
41
old = branch.lookup_revision(old)
44
old = branch.last_patch()
46
old = branch.lookup_revision(old)
50
def _get_trees(branch, revisions):
51
"""Get the old and new trees based on revision.
53
if revisions[0] is None:
54
old_tree = branch.basis_tree()
56
old_tree = branch.revision_tree(revisions[0])
58
if revisions[1] is None:
59
new_tree = branch.working_tree()
61
new_tree = branch.revision_tree(revisions[1])
62
return old_tree, new_tree
64
def _fake_working_revision(branch):
65
"""Fake a Revision object for the working tree."""
66
from bzrlib.revision import Revision
68
from bzrlib.osutils import local_time_offset, \
71
precursor = branch.last_patch()
72
precursor_sha1 = branch.get_revision_sha1(precursor)
74
return Revision(timestamp=time.time(),
75
timezone=local_time_offset(),
78
precursor_sha1=precursor_sha1)
81
class MetaInfoHeader(object):
82
"""Maintain all of the header information about this
86
def __init__(self, branch, revisions, delta):
89
self._get_revision_list(revisions)
90
self._get_all_meta_info()
92
def _get_revision_list(self, revisions):
93
"""This generates the list of all revisions from->to.
97
rh = self.branch.revision_history()
98
for revno, rev in enumerate(rh):
99
if rev == revisions[0]:
101
if rev == revisions[1]:
104
if old_revno is None:
105
raise bzrlib.errors.BzrError('Could not find revision for %s' % revisions[0])
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))
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))
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')
121
write('committer', username())
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
131
revisions = _canonicalize_revision(branch, revision)
132
print "Canonicalized revisions: %s" % (revisions,)
134
old_tree, new_tree = _get_trees(branch, revisions)
136
delta = compare_trees(old_tree, new_tree, want_unchanged=False,
137
specific_files=specific_files)
139
meta = MetaInfoHeader(branch, revisions, delta)
140
meta.write_meta_info(to_file)