/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/commitfromnews/committemplate.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Logic to create commit templates."""
18
18
 
19
 
import patiencediff
 
19
from __future__ import absolute_import
20
20
 
21
 
from ... import bugtracker, osutils
 
21
from ... import bugtracker, osutils, patiencediff
22
22
import re
23
23
 
24
24
_BUG_MATCH = re.compile(r'lp:(\d+)')
26
26
 
27
27
class CommitTemplate(object):
28
28
 
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.
31
31
 
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
35
35
        """
36
36
        self.commit = commit
37
37
        self.message = message
38
 
        self.filespec = filespec
 
38
        self.file_matches = file_matches
39
39
 
40
40
    def make(self):
41
41
        """Make the template.
48
48
        found_old_path = None
49
49
        found_entry = 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
54
54
                break
56
56
            return self.message
57
57
        if found_old_path is None:
58
58
            # New file
59
 
            _, new_chunks = next(
 
59
            _, new_chunks = list(
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)
64
64
        else:
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
                found_entry.file_id)
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)
75
76
            lines = {}
76
77
            for name, chunks in contents:
77
 
                lines[name] = osutils.chunks_to_lines(list(chunks))
 
78
                lines[name] = osutils.chunks_to_lines(chunks)
78
79
            new = lines['new']
79
80
            sequence_matcher = patiencediff.PatienceSequenceMatcher(
80
81
                None, lines['old'], new)
85
86
                    continue
86
87
                if tag == 'delete':
87
88
                    continue
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
 
                         for bugid in bugids])
 
99
                        [bt.get_bug_url(bugid) for bugid in bugids])
100
100
            return self.merge_message(''.join(new_lines))
101
101
 
102
102
    def merge_message(self, new_message):