/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 tests/__init__.py

Clean up the builder, start using it for big speed gains.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import subprocess
20
20
import time
 
21
import tempfile
21
22
 
22
23
from bzrlib import (
23
24
    tests,
24
25
    trace,
25
26
    )
 
27
from  bzrlib.plugins.git.gitlib import errors
26
28
 
27
29
TestCase = tests.TestCase
28
30
TestCaseInTempDir = tests.TestCaseInTempDir
60
62
 
61
63
class GitBranchBuilder(object):
62
64
 
63
 
    def __init__(self, stream):
 
65
    def __init__(self, stream=None):
64
66
        self.commit_info = []
65
67
        self.stream = stream
 
68
        self._process = None
66
69
        self._counter = 0
67
70
        self._branch = 'refs/head/master'
 
71
        if stream is None:
 
72
            self._marks_file = tempfile.NamedTemporaryFile(
 
73
                prefix='tmp-git-marks')
 
74
            self._process = subprocess.Popen(
 
75
                ['git', 'fast-import', '--quiet',
 
76
                 # GIT doesn't support '--export-marks foo'
 
77
                 # it only supports '--export-marks=foo'
 
78
                 # And gives a 'unknown option' otherwise.
 
79
                 '--export-marks='+self._marks_file.name,
 
80
                ],
 
81
                stdout=subprocess.PIPE,
 
82
                stderr=subprocess.PIPE,
 
83
                stdin=subprocess.PIPE,
 
84
                )
 
85
            self.stream = self._process.stdin
 
86
        else:
 
87
            self._process = None
68
88
 
69
89
    def set_branch(self, branch):
70
90
        """Set the branch we are committing."""
71
91
        self._branch = branch
72
92
 
 
93
    def _write(self, text):
 
94
        try:
 
95
            self.stream.write(text)
 
96
        except IOError, e:
 
97
            if self._process is None:
 
98
                raise
 
99
            raise errors.GitCommandError(self._process.returncode,
 
100
                                         'git fast-import',
 
101
                                         self._process.stderr.read())
 
102
 
 
103
    def _writelines(self, lines):
 
104
        try:
 
105
            self.stream.writelines(lines)
 
106
        except IOError, e:
 
107
            if self._process is None:
 
108
                raise
 
109
            raise errors.GitCommandError(self._process.returncode,
 
110
                                         'git fast-import',
 
111
                                         self._process.stderr.read())
 
112
 
73
113
    def _create_blob(self, content):
74
114
        self._counter += 1
75
 
        self.stream.write('blob\n')
76
 
        self.stream.write('mark :%d\n' % (self._counter,))
77
 
        self.stream.write('data %d\n' % (len(content),))
78
 
        self.stream.write(content)
79
 
        self.stream.write('\n')
 
115
        self._write('blob\n')
 
116
        self._write('mark :%d\n' % (self._counter,))
 
117
        self._write('data %d\n' % (len(content),))
 
118
        self._write(content)
 
119
        self._write('\n')
80
120
        return self._counter
81
121
 
82
122
    def set_file(self, path, content, executable):
99
139
        """This will delete files or symlinks at the given location."""
100
140
        self.commit_info.append('D %s\n' % (path.encode('utf-8'),))
101
141
 
 
142
    # TODO: Author
 
143
    # TODO: Author timestamp+timezone
102
144
    def commit(self, committer, message, timestamp=None,
103
 
               timezone='+0000', author=None):
 
145
               timezone='+0000', author=None,
 
146
               merge=None, base=None):
 
147
        """Commit the new content.
 
148
 
 
149
        :param committer: The name and address for the committer
 
150
        :param message: The commit message
 
151
        :param timestamp: The timestamp for the commit
 
152
        :param timezone: The timezone of the commit, such as '+0000' or '-1000'
 
153
        :param author: The name and address of the author (if different from
 
154
            committer)
 
155
        :param merge: A list of marks if this should merge in another commit
 
156
        :param base: An id for the base revision (primary parent) if that
 
157
            is not the last commit.
 
158
        :return: A mark which can be used in the future to reference this
 
159
            commit.
 
160
        """
104
161
        self._counter += 1
105
162
        mark = self._counter
106
 
        self.stream.write('commit %s\n' % (branch,))
107
 
        self.stream.write('mark :%d\n' % (mark,))
108
 
        self.stream.write('committer %s %s %s\n'
109
 
                          % (committer, timestamp, timezone))
 
163
        if timestamp is None:
 
164
            timestamp = int(time.time())
 
165
        self._write('commit %s\n' % (self._branch,))
 
166
        self._write('mark :%d\n' % (mark,))
 
167
        self._write('committer %s %s %s\n'
 
168
                    % (committer, timestamp, timezone))
110
169
        message = message.encode('UTF-8')
111
 
        self.stream.write('data %d\n' % (len(message),))
112
 
        self.stream.write(message)
113
 
        self.stream.write('\n')
114
 
        self.stream.writelines(self.commit_info)
115
 
        self.stream.write('\n')
 
170
        self._write('data %d\n' % (len(message),))
 
171
        self._write(message)
 
172
        self._write('\n')
 
173
        if base is not None:
 
174
            self._write('from :%d\n' % (base,))
 
175
        if merge is not None:
 
176
            for m in merge:
 
177
                self._write('merge :%d\n' % (m,))
 
178
        self._writelines(self.commit_info)
 
179
        self._write('\n')
116
180
        self.commit_info = []
117
181
        return mark
118
182
 
119
 
 
120
 
class GitBranchBuilder(object):
121
 
    """This uses git-fast-import to build up something directly."""
122
 
 
123
 
    def __init__(self, git_dir):
124
 
        self.git_dir = git_dir
 
183
    def finish(self):
 
184
        """We are finished building, close the stream, get the id mapping"""
 
185
        self.stream.close()
 
186
        if self._process is None:
 
187
            return {}
 
188
        if self._process.wait() != 0:
 
189
            raise errors.GitCommandError(self._process.returncode,
 
190
                                         'git fast-import',
 
191
                                         self._process.stderr.read())
 
192
        self._marks_file.seek(0)
 
193
        mapping = {}
 
194
        for line in self._marks_file:
 
195
            mark, shasum = line.split()
 
196
            assert mark.startswith(':')
 
197
            mapping[int(mark[1:])] = shasum
 
198
        self._marks_file.close()
 
199
        return mapping
125
200
 
126
201
 
127
202
def test_suite():