1
# Copyright (C) 2006, 2007 Canonical Ltd
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.
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.
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
17
"""The basic test suite for bzr-git."""
27
from bzrlib.plugins.git import errors
29
TestCase = tests.TestCase
30
TestCaseInTempDir = tests.TestCaseInTempDir
31
TestCaseWithTransport = tests.TestCaseWithTransport
32
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
34
class _GitCommandFeature(tests.Feature):
38
p = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE)
41
out, err = p.communicate()
42
trace.mutter('Using: %s', out.rstrip('\n'))
45
def feature_name(self):
48
GitCommandFeature = _GitCommandFeature()
52
cmd = ['git'] + list(args)
53
p = subprocess.Popen(cmd,
54
stdout=subprocess.PIPE,
55
stderr=subprocess.PIPE)
56
out, err = p.communicate()
58
raise AssertionError('Bad return code: %d for %s:\n%s'
59
% (p.returncode, ' '.join(cmd), err))
63
class GitBranchBuilder(object):
65
def __init__(self, stream=None):
70
self._branch = 'refs/heads/master'
72
# Write the marks file into the git sandbox.
73
self._marks_file_name = osutils.abspath('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,
81
stdout=subprocess.PIPE,
82
stderr=subprocess.PIPE,
83
stdin=subprocess.PIPE,
85
self.stream = self._process.stdin
89
def set_branch(self, branch):
90
"""Set the branch we are committing."""
93
def _write(self, text):
95
self.stream.write(text)
97
if self._process is None:
99
raise errors.GitCommandError(self._process.returncode,
101
self._process.stderr.read())
103
def _writelines(self, lines):
105
self.stream.writelines(lines)
107
if self._process is None:
109
raise errors.GitCommandError(self._process.returncode,
111
self._process.stderr.read())
113
def _create_blob(self, content):
115
self._write('blob\n')
116
self._write('mark :%d\n' % (self._counter,))
117
self._write('data %d\n' % (len(content),))
122
def set_file(self, path, content, executable):
123
"""Create or update content at a given path."""
124
mark = self._create_blob(content)
129
self.commit_info.append('M %s :%d %s\n'
130
% (mode, mark, self._encode_path(path)))
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, self._encode_path(path)))
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' % (self._encode_path(path),))
143
def _encode_path(path):
144
if '\n' in path or path[0] == '"':
145
path = path.replace('\\', '\\\\')
146
path = path.replace('\n', '\\n')
147
path = path.replace('"', '\\"')
148
path = '"' + path + '"'
149
return path.encode('utf-8')
152
# TODO: Author timestamp+timezone
153
def commit(self, committer, message, timestamp=None,
154
timezone='+0000', author=None,
155
merge=None, base=None):
156
"""Commit the new content.
158
:param committer: The name and address for the committer
159
:param message: The commit message
160
:param timestamp: The timestamp for the commit
161
:param timezone: The timezone of the commit, such as '+0000' or '-1000'
162
:param author: The name and address of the author (if different from
164
:param merge: A list of marks if this should merge in another commit
165
:param base: An id for the base revision (primary parent) if that
166
is not the last commit.
167
:return: A mark which can be used in the future to reference this
172
if timestamp is None:
173
timestamp = int(time.time())
174
self._write('commit %s\n' % (self._branch,))
175
self._write('mark :%d\n' % (mark,))
176
self._write('committer %s %s %s\n'
177
% (committer, timestamp, timezone))
178
message = message.encode('UTF-8')
179
self._write('data %d\n' % (len(message),))
183
self._write('from :%d\n' % (base,))
184
if merge is not None:
186
self._write('merge :%d\n' % (m,))
187
self._writelines(self.commit_info)
189
self.commit_info = []
192
def reset(self, ref=None, mark=None):
193
"""Create or recreate the named branch.
195
:param ref: branch name, defaults to the current branch.
196
:param mark: commit the branch will point to.
200
self._write('reset %s\n' % (ref,))
202
self._write('from :%d\n' % mark)
206
"""We are finished building, close the stream, get the id mapping"""
208
if self._process is None:
210
if self._process.wait() != 0:
211
raise errors.GitCommandError(self._process.returncode,
213
self._process.stderr.read())
214
marks_file = open(self._marks_file_name)
216
for line in marks_file:
217
mark, shasum = line.split()
218
assert mark.startswith(':')
219
mapping[int(mark[1:])] = shasum
225
loader = tests.TestLoader()
227
suite = tests.TestSuite()
237
testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
238
suite.addTests(loader.loadTestsFromModuleNames(testmod_names))