301
250
# represented in well-formed XML; escape characters that
302
251
# aren't listed in the XML specification
303
252
# (http://www.w3.org/TR/REC-xml/#NT-Char).
253
if isinstance(self.message, unicode):
254
char_pattern = u'[^\x09\x0A\x0D\u0020-\uD7FF\uE000-\uFFFD]'
256
# Use a regular 'str' as pattern to avoid having re.subn
257
# return 'unicode' results.
258
char_pattern = '[^x09\x0A\x0D\x20-\xFF]'
304
259
self.message, escape_count = re.subn(
305
u'[^\x09\x0A\x0D\u0020-\uD7FF\uE000-\uFFFD]+',
306
261
lambda match: match.group(0).encode('unicode_escape'),
309
self.reporter.escaped(escape_count, self.message)
264
note("replaced %d control characters in message", escape_count)
266
def _record_ancestry(self):
267
"""Append merged revision ancestry to the ancestry file.
269
This should be the merged ancestry of all parents, plus the
271
s = self.branch.control_weaves
272
w = s.get_weave_or_empty('ancestry')
273
lines = self._make_ancestry(w)
274
w.add(self.rev_id, self.parents, lines)
275
s.put_weave('ancestry', w)
278
def _make_ancestry(self, ancestry_weave):
279
"""Return merged ancestry lines.
281
The lines are revision-ids followed by newlines."""
282
parent_ancestries = [ancestry_weave.get(p) for p in self.parents]
283
new_lines = merge_ancestry_lines(self.rev_id, parent_ancestries)
284
mutter('merged ancestry of {%s}:\n%s', self.rev_id, ''.join(new_lines))
311
288
def _gather_parents(self):
312
"""Record the parents of a merge for merge detection."""
313
pending_merges = self.work_tree.pending_merges()
289
pending_merges = self.branch.pending_merges()
314
290
self.parents = []
315
self.parent_invs = []
316
self.present_parents = []
291
self.parent_trees = []
317
292
precursor_id = self.branch.last_revision()
319
294
self.parents.append(precursor_id)
295
self.parent_trees.append(self.basis_tree)
320
296
self.parents += pending_merges
321
for revision in self.parents:
322
if self.branch.has_revision(revision):
323
self.parent_invs.append(self.branch.get_inventory(revision))
324
self.present_parents.append(revision)
297
self.parent_trees.extend(map(self.branch.revision_tree, pending_merges))
326
300
def _check_parents_present(self):
327
301
for parent_id in self.parents:
328
302
mutter('commit parent revision {%s}', parent_id)
329
303
if not self.branch.has_revision(parent_id):
330
if parent_id == self.branch.last_revision():
331
warning("parent is missing %r", parent_id)
332
raise HistoryMissing(self.branch, 'revision', parent_id)
334
mutter("commit will ghost revision %r", parent_id)
304
warning("can't commit a merge from an absent parent")
305
raise HistoryMissing(self.branch, 'revision', parent_id)
336
308
def _make_revision(self):
337
309
"""Record a new revision object for this commit."""
370
338
if specific and not is_inside_any(specific, path):
372
340
if not self.work_tree.has_filename(path):
373
self.reporter.missing(path)
374
deleted_ids.append((path, ie.file_id))
341
note('missing %s', path)
342
deleted_ids.append(ie.file_id)
376
deleted_ids.sort(reverse=True)
377
for path, file_id in deleted_ids:
344
for file_id in deleted_ids:
378
345
del self.work_inv[file_id]
379
self.work_tree._write_inventory(self.work_inv)
381
def _store_snapshot(self):
382
"""Pass over inventory and record a snapshot.
384
Entries get a new revision when they are modified in
385
any way, which includes a merge with a new set of
386
parents that have the same entry.
346
self.branch._write_inventory(self.work_inv)
349
def _find_file_parents(self, file_id):
350
"""Return the text versions and hashes for all file parents.
352
Returned as a map from text version to inventory entry.
354
This is a set containing the file versions in all parents
355
revisions containing the file. If the file is new, the set
358
for tree in self.parent_trees:
359
if file_id in tree.inventory:
360
ie = tree.inventory[file_id]
361
assert ie.kind == 'file'
362
assert ie.file_id == file_id
363
if ie.text_version in r:
364
assert r[ie.text_version] == ie
366
r[ie.text_version] = ie
370
def _set_name_versions(self):
371
"""Pass over inventory and mark new entry version as needed.
373
Files get a new name version when they are new, have a
374
different parent, or a different name from in the
375
basis inventory, or if the file is in a different place
376
to any of the parents."""
388
377
# XXX: Need to think more here about when the user has
389
378
# made a specific decision on a particular value -- c.f.
391
380
for path, ie in self.new_inv.iter_entries():
392
previous_entries = ie.find_previous_heads(
394
self.weave_store.get_weave_or_empty(ie.file_id,
395
self.branch.get_transaction()))
396
if ie.revision is None:
397
change = ie.snapshot(self.rev_id, path, previous_entries,
398
self.work_tree, self.weave_store,
399
self.branch.get_transaction())
383
for parent_tree in self.parent_trees:
384
parent_inv = parent_tree.inventory
385
if file_id not in parent_inv:
387
parent_ie = parent_inv[file_id]
388
if parent_ie.parent_id != ie.parent_id:
391
elif parent_ie.name != ie.name:
394
elif old_version is None:
395
old_version = parent_ie.name_version
396
elif old_version != parent_ie.name_version:
400
pass # so far so good
401
if old_version is None:
402
mutter('new name_version for {%s}', file_id)
403
ie.name_version = self.rev_id
402
self.reporter.snapshot_change(change, path)
404
def _populate_new_inv(self):
405
"""Build revision inventory.
407
This creates a new empty inventory. Depending on
408
which files are selected for commit, and what is present in the
409
current tree, the new inventory is populated. inventory entries
410
which are candidates for modification have their revision set to
411
None; inventory entries that are carried over untouched have their
412
revision set to their prior value.
405
mutter('name_version for {%s} inherited as {%s}',
406
file_id, old_version)
407
ie.name_version = old_version
410
def _store_entries(self):
411
"""Build revision inventory and store modified files.
413
This is called with new_inv a new empty inventory. Depending on
414
which files are selected for commit, and which ones have
415
been modified or merged, new inventory entries are built
416
based on the working and parent inventories.
418
As a side-effect this stores new text versions for committed
419
files with text changes or merges.
421
Each entry can have one of several things happen:
423
carry_file -- carried from the previous version (if not
426
commit_nonfile -- no text to worry about
428
commit_old_text -- same text, may have moved
430
commit_file -- new text version
414
mutter("Selecting files for commit with filter %s", self.specific_files)
415
self.new_inv = Inventory()
416
432
for path, new_ie in self.work_inv.iter_entries():
417
433
file_id = new_ie.file_id
418
434
mutter('check %s {%s}', path, new_ie.file_id)
419
435
if self.specific_files:
420
436
if not is_inside_any(self.specific_files, path):
421
437
mutter('%s not selected for commit', path)
422
self._carry_entry(file_id)
425
# this is selected, ensure its parents are too.
426
parent_id = new_ie.parent_id
427
while parent_id != ROOT_ID:
428
if not self.new_inv.has_id(parent_id):
429
ie = self._select_entry(self.work_inv[parent_id])
430
mutter('%s selected for commit because of %s',
431
self.new_inv.id2path(parent_id), path)
433
ie = self.new_inv[parent_id]
434
if ie.revision is not None:
436
mutter('%s selected for commit because of %s',
437
self.new_inv.id2path(parent_id), path)
438
parent_id = ie.parent_id
439
mutter('%s selected for commit', path)
440
self._select_entry(new_ie)
442
def _select_entry(self, new_ie):
443
"""Make new_ie be considered for committing."""
449
def _carry_entry(self, file_id):
438
self._carry_file(file_id)
440
if new_ie.kind != 'file':
441
self._commit_nonfile(file_id)
444
file_parents = self._find_file_parents(file_id)
445
mutter('parents of %s are %r', path, file_parents)
446
if len(file_parents) == 1:
447
parent_ie = file_parents.values()[0]
448
wc_sha1 = self.work_tree.get_file_sha1(file_id)
449
if parent_ie.text_sha1 == wc_sha1:
450
# text not changed or merged
451
self._commit_old_text(file_id, parent_ie)
453
# file is either new, or a file merge; need to record
455
if len(file_parents) > 1:
456
note('merged %s', path)
457
elif len(file_parents) == 0:
458
note('added %s', path)
460
note('modified %s', path)
461
self._commit_file(new_ie, file_id, file_parents)
464
def _commit_nonfile(self, file_id):
465
self.new_inv.add(self.work_inv[file_id].copy())
468
def _carry_file(self, file_id):
450
469
"""Carry the file unchanged from the basis revision."""
451
470
if self.basis_inv.has_id(file_id):
452
471
self.new_inv.add(self.basis_inv[file_id].copy())
474
def _commit_old_text(self, file_id, parent_ie):
475
"""Keep the same text as last time, but possibly a different name."""
476
ie = self.work_inv[file_id].copy()
477
ie.text_version = parent_ie.text_version
478
ie.text_size = parent_ie.text_size
479
ie.text_sha1 = parent_ie.text_sha1
454
483
def _report_deletes(self):
455
484
for file_id in self.basis_inv:
456
485
if file_id not in self.new_inv:
457
self.reporter.deleted(self.basis_inv.id2path(file_id))
459
def _gen_revision_id(config, when):
486
note('deleted %s', self.basis_inv.id2path(file_id))
489
def _commit_file(self, new_ie, file_id, file_parents):
490
mutter('store new text for {%s} in revision {%s}',
491
file_id, self.rev_id)
492
new_lines = self.work_tree.get_file(file_id).readlines()
493
self._add_text_to_weave(file_id, new_lines, file_parents)
494
new_ie.text_version = self.rev_id
495
new_ie.text_sha1 = sha_strings(new_lines)
496
new_ie.text_size = sum(map(len, new_lines))
497
self.new_inv.add(new_ie)
500
def _add_text_to_weave(self, file_id, new_lines, parents):
501
self.weave_store.add_text(file_id, self.rev_id, new_lines, parents)
504
def _gen_revision_id(branch, when):
460
505
"""Return new revision-id."""
461
s = '%s-%s-' % (config.user_email(), compact_date(when))
506
s = '%s-%s-' % (user_email(branch), compact_date(when))
462
507
s += hexlify(rand_bytes(8))
513
def merge_ancestry_lines(rev_id, ancestries):
514
"""Return merged ancestry lines.
516
rev_id -- id of the new revision
518
ancestries -- a sequence of ancestries for parent revisions,
519
as newline-terminated line lists.
521
if len(ancestries) == 0:
522
return [rev_id + '\n']
523
seen = set(ancestries[0])
524
ancs = ancestries[0][:]
525
for parent_ancestry in ancestries[1:]:
526
for line in parent_ancestry:
527
assert line[-1] == '\n'