1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
# Copyright (C) 2005 Canonical Ltd
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## XXX: Can we do any better about making interrupted commits change
## nothing?
## XXX: If we merged two versions of a file then we still need to
## create a new version representing that merge, even if it didn't
## change from the parent.
## TODO: Read back the just-generated changeset, and make sure it
## applies and recreates the right state.
import os
import sys
import time
import tempfile
import sha
from binascii import hexlify
from cStringIO import StringIO
from bzrlib.osutils import (local_time_offset, username,
rand_bytes, compact_date, user_email,
kind_marker, is_inside_any, quotefn,
sha_string, sha_file, isdir, isfile)
from bzrlib.branch import gen_file_id
from bzrlib.errors import BzrError, PointlessCommit
from bzrlib.revision import Revision, RevisionReference
from bzrlib.trace import mutter, note
from bzrlib.xml5 import serializer_v5
from bzrlib.inventory import Inventory
from bzrlib.delta import compare_trees
from bzrlib.weave import Weave
from bzrlib.weavefile import read_weave, write_weave_v5
from bzrlib.atomicfile import AtomicFile
def commit(*args, **kwargs):
"""Commit a new revision to a branch.
Function-style interface for convenience of old callers.
New code should use the Commit class instead.
"""
Commit().commit(*args, **kwargs)
class NullCommitReporter(object):
"""I report on progress of a commit."""
def added(self, path):
pass
def removed(self, path):
pass
def renamed(self, old_path, new_path):
pass
class ReportCommitToLog(NullCommitReporter):
def added(self, path):
note('added %s', path)
def removed(self, path):
note('removed %s', path)
def renamed(self, old_path, new_path):
note('renamed %s => %s', old_path, new_path)
class Commit(object):
"""Task of committing a new revision.
This is a MethodObject: it accumulates state as the commit is
prepared, and then it is discarded. It doesn't represent
historical revisions, just the act of recording a new one.
missing_ids
Modified to hold a list of files that have been deleted from
the working directory; these should be removed from the
working inventory.
"""
def __init__(self,
reporter=None):
if reporter is not None:
self.reporter = reporter
else:
self.reporter = NullCommitReporter()
def commit(self,
branch, message,
timestamp=None,
timezone=None,
committer=None,
specific_files=None,
rev_id=None,
allow_pointless=True):
"""Commit working copy as a new revision.
The basic approach is to add all the file texts into the
store, then the inventory, then make a new revision pointing
to that inventory and store that.
This is not quite safe if the working copy changes during the
commit; for the moment that is simply not allowed. A better
approach is to make a temporary copy of the files before
computing their hashes, and then add those hashes in turn to
the inventory. This should mean at least that there are no
broken hash pointers. There is no way we can get a snapshot
of the whole directory at an instant. This would also have to
be robust against files disappearing, moving, etc. So the
whole thing is a bit hard.
This raises PointlessCommit if there are no changes, no new merges,
and allow_pointless is false.
timestamp -- if not None, seconds-since-epoch for a
postdated/predated commit.
specific_files
If true, commit only those files.
rev_id
If set, use this as the new revision id.
Useful for test or import commands that need to tightly
control what revisions are assigned. If you duplicate
a revision id that exists elsewhere it is your own fault.
If null (default), a time/random revision id is generated.
"""
self.branch = branch
self.branch.lock_write()
self.rev_id = rev_id
self.specific_files = specific_files
self.allow_pointless = allow_pointless
if timestamp is None:
self.timestamp = time.time()
else:
self.timestamp = long(timestamp)
if committer is None:
self.committer = username(self.branch)
else:
assert isinstance(committer, basestring), type(committer)
self.committer = committer
if timezone is None:
self.timezone = local_time_offset()
else:
self.timezone = int(timezone)
assert isinstance(message, basestring), type(message)
self.message = message
try:
# First walk over the working inventory; and both update that
# and also build a new revision inventory. The revision
# inventory needs to hold the text-id, sha1 and size of the
# actual file versions committed in the revision. (These are
# not present in the working inventory.) We also need to
# detect missing/deleted files, and remove them from the
# working inventory.
self.work_tree = self.branch.working_tree()
self.work_inv = self.work_tree.inventory
self.basis_tree = self.branch.basis_tree()
self.basis_inv = self.basis_tree.inventory
self.pending_merges = self.branch.pending_merges()
if self.pending_merges:
raise NotImplementedError("sorry, can't commit merges to the weave format yet")
if self.rev_id is None:
self.rev_id = _gen_revision_id(self.branch, time.time())
# todo: update hashcache
self.delta = compare_trees(self.basis_tree, self.work_tree,
specific_files=self.specific_files)
if not (self.delta.has_changed()
or self.allow_pointless
or self.pending_merges):
raise PointlessCommit()
self.new_inv = self.basis_inv.copy()
self.delta.show(sys.stdout)
self._remove_deleted()
self._store_files()
self.branch._write_inventory(self.work_inv)
self._record_inventory()
self._make_revision()
note('committted r%d {%s}', (self.branch.revno() + 1),
self.rev_id)
self.branch.append_revision(self.rev_id)
self.branch.set_pending_merges([])
finally:
self.branch.unlock()
def _record_inventory(self):
inv_tmp = StringIO()
serializer_v5.write_inventory(self.new_inv, inv_tmp)
self.inv_sha1 = sha_string(inv_tmp.getvalue())
inv_tmp.seek(0)
self.branch.inventory_store.add(inv_tmp, self.rev_id)
def _make_revision(self):
"""Record a new revision object for this commit."""
self.rev = Revision(timestamp=self.timestamp,
timezone=self.timezone,
committer=self.committer,
message=self.message,
inventory_sha1=self.inv_sha1,
revision_id=self.rev_id)
self.rev.parents = []
precursor_id = self.branch.last_patch()
if precursor_id:
self.rev.parents.append(RevisionReference(precursor_id))
for merge_rev in self.pending_merges:
rev.parents.append(RevisionReference(merge_rev))
rev_tmp = tempfile.TemporaryFile()
serializer_v5.write_revision(self.rev, rev_tmp)
rev_tmp.seek(0)
self.branch.revision_store.add(rev_tmp, self.rev_id)
mutter('new revision_id is {%s}', self.rev_id)
def _remove_deleted(self):
"""Remove deleted files from the working and stored inventories."""
for path, id, kind in self.delta.removed:
if self.work_inv.has_id(id):
del self.work_inv[id]
if self.new_inv.has_id(id):
del self.new_inv[id]
def _store_files(self):
"""Store new texts of modified/added files."""
# We must make sure that directories are added before anything
# inside them is added. the files within the delta report are
# sorted by path so we know the directory will come before its
# contents.
for path, file_id, kind in self.delta.added:
if kind != 'file':
ie = self.work_inv[file_id].copy()
self.new_inv.add(ie)
else:
self._store_file_text(file_id)
for path, file_id, kind in self.delta.modified:
if kind != 'file':
continue
self._store_file_text(file_id)
for old_path, new_path, file_id, kind, text_modified in self.delta.renamed:
if kind != 'file':
continue
if not text_modified:
continue
self._store_file_text(file_id)
def _store_file_text(self, file_id):
"""Store updated text for one modified or added file."""
note('store new text for {%s} in revision {%s}',
file_id, self.rev_id)
new_lines = self.work_tree.get_file(file_id).readlines()
if file_id in self.new_inv: # was in basis inventory
ie = self.new_inv[file_id]
assert ie.file_id == file_id
assert file_id in self.basis_inv
assert self.basis_inv[file_id].kind == 'file'
old_version = self.basis_inv[file_id].text_version
file_parents = [old_version]
else: # new in this revision
ie = self.work_inv[file_id].copy()
self.new_inv.add(ie)
assert file_id not in self.basis_inv
file_parents = []
assert ie.kind == 'file'
self._add_text_to_weave(file_id, new_lines, file_parents)
# make a new inventory entry for this file, using whatever
# it had in the working copy, plus details on the new text
ie.text_sha1 = _sha_strings(new_lines)
ie.text_size = sum(map(len, new_lines))
ie.text_version = self.rev_id
ie.entry_version = self.rev_id
def _add_text_to_weave(self, file_id, new_lines, parents):
weave_fn = self.branch.controlfilename(['weaves', file_id+'.weave'])
if os.path.exists(weave_fn):
w = read_weave(file(weave_fn, 'rb'))
else:
w = Weave()
# XXX: Should set the appropriate parents by looking for this file_id
# in all revision parents
parent_idxs = map(w.lookup, parents)
w.add(self.rev_id, parent_idxs, new_lines)
af = AtomicFile(weave_fn)
try:
write_weave_v5(w, af)
af.commit()
finally:
af.close()
def _gen_revision_id(branch, when):
"""Return new revision-id."""
s = '%s-%s-' % (user_email(branch), compact_date(when))
s += hexlify(rand_bytes(8))
return s
def _sha_strings(strings):
"""Return the sha-1 of concatenation of strings"""
s = sha.new()
map(s.update, strings)
return s.hexdigest()
|