17
17
"""Logic to create commit templates."""
19
from __future__ import absolute_import
21
from ... import bugtracker, osutils
21
from ... import bugtracker, osutils, patiencediff
24
24
_BUG_MATCH = re.compile(r'lp:(\d+)')
27
27
class CommitTemplate(object):
29
def __init__(self, commit, message, filespec):
29
def __init__(self, commit, message, file_matches):
30
30
"""Create a commit template for commit with initial message message.
32
32
:param commit: A Commit object for the in progress commit.
33
33
:param message: The current message (which may be None).
34
:param filespec: List of files to match
34
:param file_matches: Check whether file matches
36
36
self.commit = commit
37
37
self.message = message
38
self.filespec = filespec
38
self.file_matches = file_matches
41
41
"""Make the template.
48
48
found_old_path = None
50
50
for old_path, new_path, fileid, entry in delta:
51
if new_path in self.filespec:
51
if self.file_matches(new_path):
52
52
found_entry = entry
53
53
found_old_path = old_path
56
56
return self.message
57
57
if found_old_path is None:
60
60
self.commit.builder.repository.iter_files_bytes(
61
[(found_entry.file_id, found_entry.revision, None)]))
62
content = b''.join(new_chunks).decode('utf-8')
61
[(found_entry.file_id, found_entry.revision, None)]))[0]
62
content = ''.join(new_chunks)
63
63
return self.merge_message(content)
65
65
# Get a diff. XXX Is this hookable? I thought it was, can't find it
66
# though.... add DiffTree.diff_factories. Sadly thats not at the
66
# though.... add DiffTree.diff_factories. Sadly thats not at the
67
67
# right level: we want to identify the changed lines, not have the
68
# final diff: because we want to grab the sections for regions
68
# final diff: because we want to grab the sections for regions
69
69
# changed in new version of the file. So for now a direct diff
70
70
# using patiencediff is done.
71
old_revision = self.commit.basis_tree.get_file_revision(old_path)
71
old_revision = self.commit.basis_tree.get_file_revision(
72
73
needed = [(found_entry.file_id, found_entry.revision, 'new'),
73
74
(found_entry.file_id, old_revision, 'old')]
74
75
contents = self.commit.builder.repository.iter_files_bytes(needed)
76
77
for name, chunks in contents:
77
lines[name] = osutils.chunks_to_lines(list(chunks))
78
lines[name] = osutils.chunks_to_lines(chunks)
79
80
sequence_matcher = patiencediff.PatienceSequenceMatcher(
80
81
None, lines['old'], new)
86
87
if tag == 'delete':
88
new_lines.extend([l.decode('utf-8') for l in new[j1:j2]])
89
new_lines.extend(new[j1:j2])
89
90
if not self.commit.revprops.get('bugs'):
90
91
# TODO: Allow the user to configure the bug tracker to use
91
92
# rather than hardcoding Launchpad.
95
96
bugids.extend(_BUG_MATCH.findall(line))
96
97
self.commit.revprops['bugs'] = \
97
98
bugtracker.encode_fixes_bug_urls(
98
[(bt.get_bug_url(bugid), bugtracker.FIXED)
99
[bt.get_bug_url(bugid) for bugid in bugids])
100
100
return self.merge_message(''.join(new_lines))
102
102
def merge_message(self, new_message):