168
177
:param match: Dictionary of search lists to use when matching revision
180
# Convert old-style parameters to new-style parameters
181
if specific_fileid is not None:
182
file_ids = [specific_fileid]
187
delta_type = 'partial'
174
191
delta_type = None
194
diff_type = 'partial'
180
200
if isinstance(start_revision, int):
182
202
start_revision = revisionspec.RevisionInfo(branch, start_revision)
183
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
203
except errors.NoSuchRevision:
184
204
raise errors.InvalidRevisionNumber(start_revision)
186
206
if isinstance(end_revision, int):
188
208
end_revision = revisionspec.RevisionInfo(branch, end_revision)
189
except (errors.NoSuchRevision, errors.RevnoOutOfBounds):
209
except errors.NoSuchRevision:
190
210
raise errors.InvalidRevisionNumber(end_revision)
192
212
if end_revision is not None and end_revision.revno == 0:
193
213
raise errors.InvalidRevisionNumber(end_revision.revno)
195
215
# Build the request and execute it
196
rqst = make_log_request_dict(
198
start_revision=start_revision, end_revision=end_revision,
199
limit=limit, delta_type=delta_type, diff_type=diff_type)
216
rqst = make_log_request_dict(direction=direction, specific_fileids=file_ids,
217
start_revision=start_revision, end_revision=end_revision,
218
limit=limit, message_search=search,
219
delta_type=delta_type, diff_type=diff_type)
200
220
Logger(branch, rqst).show(lf)
410
430
Subclasses may wish to override this.
412
return _DefaultLogGenerator(branch, **rqst)
415
def _log_revision_iterator_using_per_file_graph(
416
branch, delta_type, match, levels, path, start_rev_id, end_rev_id,
417
direction, exclude_common_ancestry):
418
# Get the base revisions, filtering by the revision range.
419
# Note that we always generate the merge revisions because
420
# filter_revisions_touching_path() requires them ...
421
view_revisions = _calc_view_revisions(
422
branch, start_rev_id, end_rev_id,
423
direction, generate_merge_revisions=True,
424
exclude_common_ancestry=exclude_common_ancestry)
425
if not isinstance(view_revisions, list):
426
view_revisions = list(view_revisions)
427
view_revisions = _filter_revisions_touching_path(
428
branch, path, view_revisions,
429
include_merges=levels != 1)
430
return make_log_rev_iterator(
431
branch, view_revisions, delta_type, match)
434
def _log_revision_iterator_using_delta_matching(
435
branch, delta_type, match, levels, specific_files, start_rev_id, end_rev_id,
436
direction, exclude_common_ancestry, limit):
437
# Get the base revisions, filtering by the revision range
438
generate_merge_revisions = levels != 1
439
delayed_graph_generation = not specific_files and (
440
limit or start_rev_id or end_rev_id)
441
view_revisions = _calc_view_revisions(
442
branch, start_rev_id, end_rev_id,
444
generate_merge_revisions=generate_merge_revisions,
445
delayed_graph_generation=delayed_graph_generation,
446
exclude_common_ancestry=exclude_common_ancestry)
448
# Apply the other filters
449
return make_log_rev_iterator(branch, view_revisions,
451
files=specific_files,
455
def _format_diff(branch, rev, diff_type, files=None):
458
:param branch: Branch object
459
:param rev: Revision object
460
:param diff_type: Type of diff to generate
461
:param files: List of files to generate diff for (or None for all)
463
repo = branch.repository
464
if len(rev.parent_ids) == 0:
465
ancestor_id = _mod_revision.NULL_REVISION
467
ancestor_id = rev.parent_ids[0]
468
tree_1 = repo.revision_tree(ancestor_id)
469
tree_2 = repo.revision_tree(rev.revision_id)
470
if diff_type == 'partial' and files is not None:
471
specific_files = files
473
specific_files = None
475
path_encoding = get_diff_header_encoding()
476
diff.show_diff_trees(tree_1, tree_2, s, specific_files, old_label='',
477
new_label='', path_encoding=path_encoding)
432
return _DefaultLogGenerator(branch, rqst)
481
435
class _StartNotLinearAncestor(Exception):
485
439
class _DefaultLogGenerator(LogGenerator):
486
440
"""The default generator of log revisions."""
489
self, branch, levels=None, limit=None, diff_type=None,
490
delta_type=None, show_signature=None, omit_merges=None,
491
generate_tags=None, specific_files=None, match=None,
492
start_revision=None, end_revision=None, direction=None,
493
exclude_common_ancestry=None, _match_using_deltas=None,
442
def __init__(self, branch, rqst):
495
443
self.branch = branch
498
self.diff_type = diff_type
499
self.delta_type = delta_type
500
self.show_signature = signature
501
self.omit_merges = omit_merges
502
self.specific_files = specific_files
504
self.start_revision = start_revision
505
self.end_revision = end_revision
506
self.direction = direction
507
self.exclude_common_ancestry = exclude_common_ancestry
508
self._match_using_deltas = _match_using_deltas
509
if generate_tags and branch.supports_tags():
445
if rqst.get('generate_tags') and branch.supports_tags():
510
446
self.rev_tag_dict = branch.tags.get_reverse_tag_dict()
512
448
self.rev_tag_dict = {}
517
453
:return: An iterator yielding LogRevision objects.
456
levels = rqst.get('levels')
457
limit = rqst.get('limit')
458
diff_type = rqst.get('diff_type')
459
show_signature = rqst.get('signature')
460
omit_merges = rqst.get('omit_merges')
520
462
revision_iterator = self._create_log_revision_iterator()
521
463
for revs in revision_iterator:
522
464
for (rev_id, revno, merge_depth), rev, delta in revs:
523
465
# 0 levels means show everything; merge_depth counts from 0
524
if (self.levels != 0 and merge_depth is not None and
525
merge_depth >= self.levels):
466
if (levels != 0 and merge_depth is not None and
467
merge_depth >= levels):
527
if self.omit_merges and len(rev.parent_ids) > 1:
469
if omit_merges and len(rev.parent_ids) > 1:
530
472
raise errors.GhostRevisionUnusableHere(rev_id)
531
if self.diff_type is None:
473
if diff_type is None:
535
self.branch, rev, self.diff_type,
537
if self.show_signature:
476
diff = self._format_diff(rev, rev_id, diff_type)
538
478
signature = format_signature_validity(rev_id, self.branch)
541
481
yield LogRevision(
542
482
rev, revno, merge_depth, delta,
543
483
self.rev_tag_dict.get(rev_id), diff, signature)
546
if log_count >= self.limit:
486
if log_count >= limit:
489
def _format_diff(self, rev, rev_id, diff_type):
490
repo = self.branch.repository
491
if len(rev.parent_ids) == 0:
492
ancestor_id = _mod_revision.NULL_REVISION
494
ancestor_id = rev.parent_ids[0]
495
tree_1 = repo.revision_tree(ancestor_id)
496
tree_2 = repo.revision_tree(rev_id)
497
file_ids = self.rqst.get('specific_fileids')
498
if diff_type == 'partial' and file_ids is not None:
499
specific_files = [tree_2.id2path(id) for id in file_ids]
501
specific_files = None
503
path_encoding = get_diff_header_encoding()
504
diff.show_diff_trees(tree_1, tree_2, s, specific_files, old_label='',
505
new_label='', path_encoding=path_encoding)
549
508
def _create_log_revision_iterator(self):
550
509
"""Create a revision iterator for log.
552
511
:return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
555
start_rev_id, end_rev_id = _get_revision_limits(
556
self.branch, self.start_revision, self.end_revision)
557
if self._match_using_deltas:
558
return _log_revision_iterator_using_delta_matching(
560
delta_type=self.delta_type,
563
specific_files=self.specific_files,
564
start_rev_id=start_rev_id, end_rev_id=end_rev_id,
565
direction=self.direction,
566
exclude_common_ancestry=self.exclude_common_ancestry,
514
self.start_rev_id, self.end_rev_id = _get_revision_limits(
515
self.branch, self.rqst.get('start_revision'),
516
self.rqst.get('end_revision'))
517
if self.rqst.get('_match_using_deltas'):
518
return self._log_revision_iterator_using_delta_matching()
569
520
# We're using the per-file-graph algorithm. This scales really
570
521
# well but only makes sense if there is a single file and it's
571
522
# not a directory
572
file_count = len(self.specific_files)
523
file_count = len(self.rqst.get('specific_fileids'))
573
524
if file_count != 1:
574
525
raise errors.BzrError(
575
526
"illegal LogRequest: must match-using-deltas "
576
527
"when logging %d files" % file_count)
577
return _log_revision_iterator_using_per_file_graph(
579
delta_type=self.delta_type,
582
path=self.specific_files[0],
583
start_rev_id=start_rev_id, end_rev_id=end_rev_id,
584
direction=self.direction,
585
exclude_common_ancestry=self.exclude_common_ancestry
528
return self._log_revision_iterator_using_per_file_graph()
530
def _log_revision_iterator_using_delta_matching(self):
531
# Get the base revisions, filtering by the revision range
533
generate_merge_revisions = rqst.get('levels') != 1
534
delayed_graph_generation = not rqst.get('specific_fileids') and (
535
rqst.get('limit') or self.start_rev_id or self.end_rev_id)
536
view_revisions = _calc_view_revisions(
537
self.branch, self.start_rev_id, self.end_rev_id,
538
rqst.get('direction'),
539
generate_merge_revisions=generate_merge_revisions,
540
delayed_graph_generation=delayed_graph_generation,
541
exclude_common_ancestry=rqst.get('exclude_common_ancestry'))
543
# Apply the other filters
544
return make_log_rev_iterator(self.branch, view_revisions,
545
rqst.get('delta_type'), rqst.get('match'),
546
file_ids=rqst.get('specific_fileids'),
547
direction=rqst.get('direction'))
549
def _log_revision_iterator_using_per_file_graph(self):
550
# Get the base revisions, filtering by the revision range.
551
# Note that we always generate the merge revisions because
552
# filter_revisions_touching_file_id() requires them ...
554
view_revisions = _calc_view_revisions(
555
self.branch, self.start_rev_id, self.end_rev_id,
556
rqst.get('direction'), generate_merge_revisions=True,
557
exclude_common_ancestry=rqst.get('exclude_common_ancestry'))
558
if not isinstance(view_revisions, list):
559
view_revisions = list(view_revisions)
560
view_revisions = _filter_revisions_touching_file_id(self.branch,
561
rqst.get('specific_fileids')[
563
include_merges=rqst.get('levels') != 1)
564
return make_log_rev_iterator(self.branch, view_revisions,
565
rqst.get('delta_type'), rqst.get('match'))
589
568
def _calc_view_revisions(branch, start_rev_id, end_rev_id, direction,
978
951
:param search: A user text search string.
979
952
:param log_rev_iterator: An input iterator containing all revisions that
980
953
could be displayed, in lists.
981
:param files: If non empty, only revisions matching one or more of
982
the files are to be kept.
954
:param fileids: If non empty, only revisions matching one or more of
955
the file-ids are to be kept.
983
956
:param direction: the direction in which view_revisions is sorted
984
957
:return: An iterator over lists of ((rev_id, revno, merge_depth), rev,
987
if not generate_delta and not files:
960
if not generate_delta and not fileids:
988
961
return log_rev_iterator
989
962
return _generate_deltas(branch.repository, log_rev_iterator,
990
generate_delta, files, direction)
993
def _generate_deltas(repository, log_rev_iterator, delta_type, files,
963
generate_delta, fileids, direction)
966
def _generate_deltas(repository, log_rev_iterator, delta_type, fileids,
995
968
"""Create deltas for each batch of revisions in log_rev_iterator.
997
970
If we're only generating deltas for the sake of filtering against
998
files, we stop generating deltas once all files reach the
971
file-ids, we stop generating deltas once all file-ids reach the
999
972
appropriate life-cycle point. If we're receiving data newest to
1000
973
oldest, then that life-cycle point is 'add', otherwise it's 'remove'.
1002
check_files = files is not None and len(files) > 0
1004
file_set = set(files)
975
check_fileids = fileids is not None and len(fileids) > 0
977
fileid_set = set(fileids)
1005
978
if direction == 'reverse':
1008
981
stop_on = 'remove'
1011
984
for revs in log_rev_iterator:
1012
# If we were matching against files and we've run out,
985
# If we were matching against fileids and we've run out,
1013
986
# there's nothing left to do
1014
if check_files and not file_set:
987
if check_fileids and not fileid_set:
1016
989
revisions = [rev[1] for rev in revs]
1018
if delta_type == 'full' and not check_files:
1019
deltas = repository.get_revision_deltas(revisions)
991
if delta_type == 'full' and not check_fileids:
992
deltas = repository.get_deltas_for_revisions(revisions)
1020
993
for rev, delta in zip(revs, deltas):
1021
994
new_revs.append((rev[0], rev[1], delta))
1023
deltas = repository.get_revision_deltas(
1024
revisions, specific_files=file_set)
996
deltas = repository.get_deltas_for_revisions(revisions, fileid_set)
1025
997
for rev, delta in zip(revs, deltas):
1027
999
if delta is None or not delta.has_changed():
1030
_update_files(delta, file_set, stop_on)
1002
_update_fileids(delta, fileid_set, stop_on)
1031
1003
if delta_type is None:
1033
1005
elif delta_type == 'full':
1047
def _update_files(delta, files, stop_on):
1048
"""Update the set of files to search based on file lifecycle events.
1019
def _update_fileids(delta, fileids, stop_on):
1020
"""Update the set of file-ids to search based on file lifecycle events.
1050
:param files: a set of files to update
1051
:param stop_on: either 'add' or 'remove' - take files out of the
1052
files set once their add or remove entry is detected respectively
1022
:param fileids: a set of fileids to update
1023
:param stop_on: either 'add' or 'remove' - take file-ids out of the
1024
fileids set once their add or remove entry is detected respectively
1054
1026
if stop_on == 'add':
1055
1027
for item in delta.added:
1056
if item.path[1] in files:
1057
files.remove(item.path[1])
1058
for item in delta.copied + delta.renamed:
1059
if item.path[1] in files:
1060
files.remove(item.path[1])
1061
files.add(item.path[0])
1062
if item.kind[1] == 'directory':
1063
for path in list(files):
1064
if is_inside(item.path[1], path):
1066
files.add(item.path[0] + path[len(item.path[1]):])
1028
if item[1] in fileids:
1029
fileids.remove(item[1])
1067
1030
elif stop_on == 'delete':
1068
1031
for item in delta.removed:
1069
if item.path[0] in files:
1070
files.remove(item.path[0])
1071
for item in delta.copied + delta.renamed:
1072
if item.path[0] in files:
1073
files.remove(item.path[0])
1074
files.add(item.path[1])
1075
if item.kind[0] == 'directory':
1076
for path in list(files):
1077
if is_inside(item.path[0], path):
1079
files.add(item.path[1] + path[len(item.path[0]):])
1032
if item[1] in fileids:
1033
fileids.remove(item[1])
1082
1036
def _make_revision_objects(branch, generate_delta, search, log_rev_iterator):
2102
2062
:param file_list: the list of paths given on the command line;
2103
2063
the first of these can be a branch location or a file path,
2104
2064
the remainder must be file paths
2105
:param exit_stack: When the branch returned is read locked,
2106
an unlock call will be queued to the exit stack.
2065
:param add_cleanup: When the branch returned is read locked,
2066
an unlock call will be queued to the cleanup.
2107
2067
:return: (branch, info_list, start_rev_info, end_rev_info) where
2108
info_list is a list of (relative_path, found, kind) tuples where
2068
info_list is a list of (relative_path, file_id, kind) tuples where
2109
2069
kind is one of values 'directory', 'file', 'symlink', 'tree-reference'.
2110
2070
branch will be read-locked.
2112
2072
from breezy.builtins import _get_revision_range
2113
2073
tree, b, path = controldir.ControlDir.open_containing_tree_or_branch(
2115
exit_stack.enter_context(b.lock_read())
2075
add_cleanup(b.lock_read().unlock)
2116
2076
# XXX: It's damn messy converting a list of paths to relative paths when
2117
2077
# those paths might be deleted ones, they might be on a case-insensitive
2118
2078
# filesystem and/or they might be in silly locations (like another branch).
2135
2095
tree = b.basis_tree()
2137
2097
for fp in relpaths:
2138
kind = _get_kind_for_file(tree, fp)
2098
file_id = tree.path2id(fp)
2099
kind = _get_kind_for_file_id(tree, fp, file_id)
2140
2101
# go back to when time began
2141
2102
if tree1 is None:
2143
2104
rev1 = b.get_rev_id(1)
2144
2105
except errors.NoSuchRevision:
2145
2106
# No history at all
2148
2110
tree1 = b.repository.revision_tree(rev1)
2150
kind = _get_kind_for_file(tree1, fp)
2151
info_list.append((fp, kind))
2112
file_id = tree1.path2id(fp)
2113
kind = _get_kind_for_file_id(tree1, fp, file_id)
2114
info_list.append((fp, file_id, kind))
2153
2116
elif start_rev_info == end_rev_info:
2154
2117
# One revision given - file must exist in it
2155
2118
tree = b.repository.revision_tree(end_rev_info.rev_id)
2156
2119
for fp in relpaths:
2157
kind = _get_kind_for_file(tree, fp)
2158
info_list.append((fp, kind))
2120
file_id = tree.path2id(fp)
2121
kind = _get_kind_for_file_id(tree, fp, file_id)
2122
info_list.append((fp, file_id, kind))
2161
2125
# Revision range given. Get the file-id from the end tree.