/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
#
0.201.1 by Jelmer Vernooij
Add very small initial testsuite.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
7
#
0.201.1 by Jelmer Vernooij
Add very small initial testsuite.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
12
#
0.201.1 by Jelmer Vernooij
Add very small initial testsuite.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
17
"""The basic test suite for bzr-git."""
18
19
import subprocess
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
20
import time
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
21
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
22
from bzrlib import (
0.200.25 by David Allouche
Fix GitBranchBuilder for the new hardlink-breaking behaviour of
23
    osutils,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
24
    tests,
25
    trace,
26
    )
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
27
from  bzrlib.plugins.git.gitlib import errors
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
28
29
TestCase = tests.TestCase
30
TestCaseInTempDir = tests.TestCaseInTempDir
31
TestCaseWithTransport = tests.TestCaseWithTransport
32
33
34
class _GitCommandFeature(tests.Feature):
35
36
    def _probe(self):
37
        try:
38
            p = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE)
39
        except IOError:
40
            return False
41
        out, err = p.communicate()
42
        trace.mutter('Using: %s', out.rstrip('\n'))
43
        return True
44
45
    def feature_name(self):
46
        return 'git'
47
48
GitCommandFeature = _GitCommandFeature()
49
50
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
51
def run_git(*args):
52
    cmd = ['git'] + list(args)
53
    p = subprocess.Popen(cmd,
54
                         stdout=subprocess.PIPE,
55
                         stderr=subprocess.PIPE)
56
    out, err = p.communicate()
57
    if p.returncode != 0:
58
        raise AssertionError('Bad return code: %d for %s:\n%s'
59
                             % (p.returncode, ' '.join(cmd), err))
60
    return out
61
62
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
63
class GitBranchBuilder(object):
64
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
65
    def __init__(self, stream=None):
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
66
        self.commit_info = []
67
        self.stream = stream
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
68
        self._process = None
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
69
        self._counter = 0
70
        self._branch = 'refs/head/master'
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
71
        if stream is None:
0.200.25 by David Allouche
Fix GitBranchBuilder for the new hardlink-breaking behaviour of
72
            # Write the marks file into the git sandbox.
73
            self._marks_file_name = osutils.abspath('marks')
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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.
0.200.25 by David Allouche
Fix GitBranchBuilder for the new hardlink-breaking behaviour of
79
                 '--export-marks=' + self._marks_file_name,
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
88
89
    def set_branch(self, branch):
90
        """Set the branch we are committing."""
91
        self._branch = branch
92
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
113
    def _create_blob(self, content):
114
        self._counter += 1
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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')
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
120
        return self._counter
121
122
    def set_file(self, path, content, executable):
123
        """Create or update content at a given path."""
124
        mark = self._create_blob(content)
125
        if executable:
126
            mode = '100755'
127
        else:
128
            mode = '100644'
129
        self.commit_info.append('M %s :%d %s\n'
130
                                % (mode, mark, path.encode('utf-8')))
131
132
    def set_link(self, path, link_target):
133
        """Create or update a link at a given path."""
134
        mark = self._create_blob(link_target)
135
        self.commit_info.append('M 120000 :%d %s\n'
136
                                % (mark, path.encode('utf-8')))
137
138
    def delete_entry(self, path):
139
        """This will delete files or symlinks at the given location."""
140
        self.commit_info.append('D %s\n' % (path.encode('utf-8'),))
141
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
142
    # TODO: Author
143
    # TODO: Author timestamp+timezone
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
144
    def commit(self, committer, message, timestamp=None,
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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
        """
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
161
        self._counter += 1
162
        mark = self._counter
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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))
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
169
        message = message.encode('UTF-8')
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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')
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
180
        self.commit_info = []
181
        return mark
182
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
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())
0.200.25 by David Allouche
Fix GitBranchBuilder for the new hardlink-breaking behaviour of
192
        marks_file = open(self._marks_file_name)
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
193
        mapping = {}
0.200.25 by David Allouche
Fix GitBranchBuilder for the new hardlink-breaking behaviour of
194
        for line in marks_file:
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
195
            mark, shasum = line.split()
196
            assert mark.startswith(':')
197
            mapping[int(mark[1:])] = shasum
0.200.25 by David Allouche
Fix GitBranchBuilder for the new hardlink-breaking behaviour of
198
        marks_file.close()
0.200.23 by John Arbash Meinel
Clean up the builder, start using it for big speed gains.
199
        return mapping
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
200
201
0.201.1 by Jelmer Vernooij
Add very small initial testsuite.
202
def test_suite():
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
203
    loader = tests.TestLoader()
204
205
    suite = tests.TestSuite()
206
207
    testmod_names = [
0.200.22 by John Arbash Meinel
Initial work on a GitCommitBuilder to make the test suite run in no-glacial time.
208
        'test_builder',
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
209
        'test_git_branch',
210
        'test_git_dir',
211
        'test_git_repository',
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
212
        'test_model',
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
213
        'test_ids',
214
        ]
215
    testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
216
    suite.addTests(loader.loadTestsFromModuleNames(testmod_names))
0.201.1 by Jelmer Vernooij
Add very small initial testsuite.
217
218
    return suite