0.5.7
by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information. |
1 |
#!/usr/bin/env python
|
2 |
"""\
|
|
3 |
Common entries, like strings, etc, for the changeset reading + writing code.
|
|
4 |
"""
|
|
5 |
||
0.5.57
by John Arbash Meinel
Simplified the header, only output base if it is not the expected one. |
6 |
header_str = 'Bazaar-NG changeset v' |
0.5.7
by John Arbash Meinel
Added a bunch more information about changesets. Can now read back in all of the meta information. |
7 |
version = (0, 0, 5) |
8 |
||
9 |
def get_header(): |
|
10 |
return [ |
|
11 |
header_str + '.'.join([str(v) for v in version]), |
|
12 |
''
|
|
13 |
]
|
|
14 |
||
0.5.36
by John Arbash Meinel
Updated so that read_changeset is able to parse the output |
15 |
def canonicalize_revision(branch, revnos): |
16 |
"""Turn some sort of revision information into a single
|
|
17 |
set of from-to revision ids.
|
|
18 |
||
19 |
A revision id can be None if there is no associated revison.
|
|
20 |
||
21 |
:param revnos: A list of revisions to lookup, should be at most 2 long
|
|
22 |
:return: (old, new)
|
|
23 |
"""
|
|
24 |
# If only 1 entry is given, then we assume we want just the
|
|
25 |
# changeset between that entry and it's base (we assume parents[0])
|
|
26 |
if len(revnos) == 0: |
|
27 |
revnos = [None, None] |
|
28 |
elif len(revnos) == 1: |
|
29 |
revnos = [None, revnos[0]] |
|
30 |
||
31 |
if revnos[1] is None: |
|
32 |
new = branch.last_patch() |
|
33 |
else: |
|
34 |
new = branch.lookup_revision(revnos[1]) |
|
35 |
if revnos[0] is None: |
|
0.5.58
by John Arbash Meinel
Fixed a bug in the case that there are no revision committed yet. |
36 |
if new is None: |
37 |
old = None |
|
38 |
else: |
|
0.5.59
by John Arbash Meinel
Several fixes for handling the case where you are doing a changeset against revno=0 (Null base) |
39 |
oldrev = branch.get_revision(new) |
40 |
if len(oldrev.parents) == 0: |
|
41 |
old = None |
|
42 |
else: |
|
43 |
old = oldrev.parents[0].revision_id |
|
0.5.36
by John Arbash Meinel
Updated so that read_changeset is able to parse the output |
44 |
else: |
45 |
old = branch.lookup_revision(revnos[0]) |
|
46 |
||
47 |
return old, new |
|
48 |
||
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
49 |
class ChangesetTree(object): |
50 |
"""This class is designed to take a base tree, and re-create
|
|
51 |
a final tree based on the information contained within a
|
|
52 |
changeset.
|
|
53 |
"""
|
|
54 |
||
55 |
def __init__(self, branch, changeset_info): |
|
56 |
"""Initialize this ChangesetTree.
|
|
57 |
||
58 |
:param branch: This is where information will be acquired
|
|
59 |
and updated.
|
|
60 |
:param changeset_info: Information about a given changeset,
|
|
61 |
so that we can identify the base,
|
|
62 |
and other information.
|
|
63 |
"""
|
|
64 |
self.branch = branch |
|
65 |
self.changeset_info = changeset_info |
|
66 |
||
67 |
self._build_tree() |
|
68 |
||
69 |
def _build_tree(self): |
|
70 |
"""Build the final description of the tree, based on
|
|
71 |
the changeset_info object.
|
|
72 |
"""
|
|
0.5.40
by John Arbash Meinel
Added some highres formatting of datestamps. |
73 |
self.base_tree = self.branch.revision_tree(self.changeset_info.base) |
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
74 |
|
0.5.55
by John Arbash Meinel
Lots of updates. Using a minimized annotations for changesets. |
75 |
def guess_text_id(tree, file_id, rev_id, modified=True): |
76 |
"""This returns the estimated text_id for a given file.
|
|
77 |
The idea is that in general the text_id should be the id last
|
|
78 |
revision which modified the file.
|
|
79 |
||
80 |
:param tree: This should be the base tree for a changeset, since that
|
|
81 |
is all the target has for guessing.
|
|
82 |
:param file_id: The file id to guess the text_id for.
|
|
83 |
:param rev_id: The target revision id
|
|
84 |
:param modified: Was the file modified between base and target?
|
|
85 |
"""
|
|
86 |
from bzrlib.errors import BzrError |
|
87 |
if modified: |
|
88 |
# If the file was modified in an intermediate stage
|
|
89 |
# (not in the final target), this won't be correct
|
|
90 |
# but it is our best guess.
|
|
91 |
# TODO: In the current code, text-ids are randomly generated
|
|
92 |
# using the filename as the base. In the future they will
|
|
93 |
# probably follow this format.
|
|
94 |
return file_id + '-' + rev_id |
|
95 |
# The file was not actually modified in this changeset
|
|
96 |
# so the text_id should be equal to it's previous value
|
|
97 |
if not file_id in tree.inventory: |
|
98 |
raise BzrError('Unable to generate text_id for file_id {%s}' |
|
99 |
', file does not exist in tree.' % file_id) |
|
100 |
# This is the last known text_id for this file
|
|
101 |
# so assume that it is being used.
|
|
102 |
text_id = tree.inventory[file_id].text_id |
|
103 |
||
104 |
def encode(s): |
|
105 |
"""Take a unicode string, and make sure to escape it for
|
|
106 |
use in a changeset.
|
|
107 |
||
108 |
Note: It can be either a normal, or a unicode string
|
|
109 |
||
110 |
>>> encode(u'abcdefg')
|
|
111 |
'abcdefg'
|
|
112 |
>>> encode(u'a b\tc\\nd\\\\e')
|
|
113 |
'a b\\\\tc\\\\nd\\\\e'
|
|
114 |
>>> encode('a b\tc\\nd\\e')
|
|
115 |
'a b\\\\tc\\\\nd\\\\e'
|
|
116 |
>>> encode(u'\\u1234\\u0020')
|
|
117 |
'\\\\u1234 '
|
|
118 |
>>> encode('abcdefg')
|
|
119 |
'abcdefg'
|
|
120 |
>>> encode(u'')
|
|
121 |
''
|
|
122 |
>>> encode('')
|
|
123 |
''
|
|
124 |
"""
|
|
125 |
return s.encode('unicode_escape') |
|
126 |
||
127 |
def decode(s): |
|
128 |
"""Undo the encode operation, returning a unicode string.
|
|
129 |
||
130 |
>>> decode('abcdefg')
|
|
131 |
u'abcdefg'
|
|
132 |
>>> decode('a b\\\\tc\\\\nd\\\\e')
|
|
133 |
u'a b\tc\\nd\\\\e'
|
|
134 |
>>> decode('\\\\u1234\\\\u0020')
|
|
135 |
u'\\u1234 '
|
|
136 |
>>> for s in ('test', 'strings'):
|
|
137 |
... if decode(encode(s)) != s:
|
|
138 |
... print 'Failed: %r' % s # There should be no failures
|
|
139 |
||
140 |
"""
|
|
141 |
return s.decode('unicode_escape') |
|
142 |
||
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
143 |
def format_highres_date(t, offset=0): |
144 |
"""Format a date, such that it includes higher precision in the
|
|
145 |
seconds field.
|
|
146 |
||
0.5.40
by John Arbash Meinel
Added some highres formatting of datestamps. |
147 |
:param t: The local time in fractional seconds since the epoch
|
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
148 |
:type t: float
|
149 |
:param offset: The timezone offset in integer seconds
|
|
150 |
:type offset: int
|
|
151 |
||
0.5.40
by John Arbash Meinel
Added some highres formatting of datestamps. |
152 |
Example: format_highres_date(time.time(), -time.timezone)
|
153 |
this will return a date stamp for right now,
|
|
154 |
formatted for the local timezone.
|
|
155 |
||
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
156 |
>>> from bzrlib.osutils import format_date
|
157 |
>>> format_date(1120153132.350850105, 0)
|
|
158 |
'Thu 2005-06-30 17:38:52 +0000'
|
|
159 |
>>> format_highres_date(1120153132.350850105, 0)
|
|
160 |
'Thu 2005-06-30 17:38:52.350850105 +0000'
|
|
161 |
>>> format_date(1120153132.350850105, -5*3600)
|
|
162 |
'Thu 2005-06-30 12:38:52 -0500'
|
|
163 |
>>> format_highres_date(1120153132.350850105, -5*3600)
|
|
164 |
'Thu 2005-06-30 12:38:52.350850105 -0500'
|
|
0.5.40
by John Arbash Meinel
Added some highres formatting of datestamps. |
165 |
>>> format_highres_date(1120153132.350850105, 7200)
|
166 |
'Thu 2005-06-30 19:38:52.350850105 +0200'
|
|
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
167 |
"""
|
168 |
import time |
|
169 |
assert isinstance(t, float) |
|
170 |
||
171 |
# This has to be formatted for "original" date, so that the
|
|
172 |
# revision XML entry will be reproduced faithfully.
|
|
173 |
if offset == None: |
|
174 |
offset = 0 |
|
175 |
tt = time.gmtime(t + offset) |
|
176 |
||
177 |
return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt) |
|
178 |
+ ('%.9f' % (t - int(t)))[1:] # Get the high-res seconds, but ignore the 0 |
|
179 |
+ ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)) |
|
180 |
||
181 |
def unpack_highres_date(date): |
|
182 |
"""This takes the high-resolution date stamp, and
|
|
183 |
converts it back into the tuple (timestamp, timezone)
|
|
184 |
Where timestamp is in real seconds, and timezone is an integer
|
|
185 |
number of seconds offset.
|
|
186 |
||
187 |
:param date: A date formated by format_highres_date
|
|
188 |
:type date: string
|
|
189 |
||
0.5.40
by John Arbash Meinel
Added some highres formatting of datestamps. |
190 |
>>> import time, random
|
191 |
>>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
|
|
192 |
(1120153132.3508501, -18000)
|
|
193 |
>>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
|
|
194 |
(1120153132.3508501, 0)
|
|
195 |
>>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
|
|
196 |
(1120153132.3508501, 7200)
|
|
197 |
>>> from bzrlib.osutils import local_time_offset
|
|
198 |
>>> t = time.time()
|
|
199 |
>>> o = local_time_offset()
|
|
200 |
>>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
|
|
201 |
>>> t == t2
|
|
202 |
True
|
|
203 |
>>> o == o2
|
|
204 |
True
|
|
205 |
>>> for count in xrange(500):
|
|
206 |
... t += random.random()*24*3600*365*2 - 24*3600*364 # Random time within +/- 1 year
|
|
207 |
... o = random.randint(-12,12)*3600 # Random timezone
|
|
208 |
... date = format_highres_date(t, o)
|
|
209 |
... t2, o2 = unpack_highres_date(date)
|
|
210 |
... if t != t2 or o != o2:
|
|
211 |
... print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
|
|
212 |
||
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
213 |
"""
|
0.5.40
by John Arbash Meinel
Added some highres formatting of datestamps. |
214 |
#from bzrlib.errors import BzrError
|
215 |
from bzrlib.osutils import local_time_offset |
|
216 |
import time |
|
217 |
# Up until the first period is a datestamp that is generated
|
|
218 |
# as normal from time.strftime, so use time.strptime to
|
|
219 |
# parse it
|
|
220 |
dot_loc = date.find('.') |
|
221 |
if dot_loc == -1: |
|
222 |
raise ValueError('Date string does not contain high-precision seconds: %r' % date) |
|
223 |
base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S") |
|
224 |
fract_seconds, offset = date[dot_loc:].split() |
|
225 |
fract_seconds = float(fract_seconds) |
|
226 |
offset = int(offset) |
|
227 |
offset = int(offset / 100) * 3600 + offset % 100 |
|
228 |
||
229 |
# mktime returns the a local timestamp, not the timestamp based
|
|
230 |
# on the offset given in the file, so we need to adjust based
|
|
231 |
# on what the local offset is, and then re-adjust based on
|
|
232 |
# offset read
|
|
233 |
timestamp = time.mktime(base_time) |
|
234 |
timestamp += local_time_offset(timestamp) - offset |
|
235 |
# Add back in the fractional seconds
|
|
236 |
timestamp += fract_seconds |
|
237 |
return (timestamp, offset) |
|
0.5.39
by John Arbash Meinel
(broken) Working on changing the processing to use a ChangesetTree. |
238 |
|
239 |
if __name__ == '__main__': |
|
240 |
import doctest |
|
241 |
doctest.testmod() |
|
0.5.40
by John Arbash Meinel
Added some highres formatting of datestamps. |
242 |