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."""
28
from bzrlib.plugins.git import (
33
TestCase = tests.TestCase
34
TestCaseInTempDir = tests.TestCaseInTempDir
35
TestCaseWithTransport = tests.TestCaseWithTransport
36
TestCaseWithMemoryTransport = tests.TestCaseWithMemoryTransport
38
class _DulwichFeature(tests.Feature):
43
except bzr_errors.DependencyNotPresent:
47
def feature_name(self):
51
DulwichFeature = _DulwichFeature()
54
class GitBranchBuilder(object):
56
def __init__(self, stream=None):
61
self._branch = 'refs/heads/master'
63
# Write the marks file into the git sandbox.
64
self._marks_file_name = osutils.abspath('marks')
65
self._process = subprocess.Popen(
66
['git', 'fast-import', '--quiet',
67
# GIT doesn't support '--export-marks foo'
68
# it only supports '--export-marks=foo'
69
# And gives a 'unknown option' otherwise.
70
'--export-marks=' + self._marks_file_name,
72
stdout=subprocess.PIPE,
73
stderr=subprocess.PIPE,
74
stdin=subprocess.PIPE,
76
self.stream = self._process.stdin
80
def set_branch(self, branch):
81
"""Set the branch we are committing."""
84
def _write(self, text):
86
self.stream.write(text)
88
if self._process is None:
90
raise errors.GitCommandError(self._process.returncode,
92
self._process.stderr.read())
94
def _writelines(self, lines):
96
self.stream.writelines(lines)
98
if self._process is None:
100
raise errors.GitCommandError(self._process.returncode,
102
self._process.stderr.read())
104
def _create_blob(self, content):
106
self._write('blob\n')
107
self._write('mark :%d\n' % (self._counter,))
108
self._write('data %d\n' % (len(content),))
113
def set_symlink(self, path, content):
114
"""Create or update symlink at a given path."""
115
mark = self._create_blob(content)
117
self.commit_info.append('M %s :%d %s\n'
118
% (mode, mark, self._encode_path(path)))
120
def set_file(self, path, content, executable):
121
"""Create or update content at a given path."""
122
mark = self._create_blob(content)
127
self.commit_info.append('M %s :%d %s\n'
128
% (mode, mark, self._encode_path(path)))
130
def set_link(self, path, link_target):
131
"""Create or update a link at a given path."""
132
mark = self._create_blob(link_target)
133
self.commit_info.append('M 120000 :%d %s\n'
134
% (mark, self._encode_path(path)))
136
def delete_entry(self, path):
137
"""This will delete files or symlinks at the given location."""
138
self.commit_info.append('D %s\n' % (self._encode_path(path),))
141
def _encode_path(path):
142
if '\n' in path or path[0] == '"':
143
path = path.replace('\\', '\\\\')
144
path = path.replace('\n', '\\n')
145
path = path.replace('"', '\\"')
146
path = '"' + path + '"'
147
return path.encode('utf-8')
150
# TODO: Author timestamp+timezone
151
def commit(self, committer, message, timestamp=None,
152
timezone='+0000', author=None,
153
merge=None, base=None):
154
"""Commit the new content.
156
:param committer: The name and address for the committer
157
:param message: The commit message
158
:param timestamp: The timestamp for the commit
159
:param timezone: The timezone of the commit, such as '+0000' or '-1000'
160
:param author: The name and address of the author (if different from
162
:param merge: A list of marks if this should merge in another commit
163
:param base: An id for the base revision (primary parent) if that
164
is not the last commit.
165
:return: A mark which can be used in the future to reference this
170
if timestamp is None:
171
timestamp = int(time.time())
172
self._write('commit %s\n' % (self._branch,))
173
self._write('mark :%d\n' % (mark,))
174
self._write('committer %s %s %s\n'
175
% (committer, timestamp, timezone))
176
message = message.encode('UTF-8')
177
self._write('data %d\n' % (len(message),))
181
self._write('from :%d\n' % (base,))
182
if merge is not None:
184
self._write('merge :%d\n' % (m,))
185
self._writelines(self.commit_info)
187
self.commit_info = []
190
def reset(self, ref=None, mark=None):
191
"""Create or recreate the named branch.
193
:param ref: branch name, defaults to the current branch.
194
:param mark: commit the branch will point to.
198
self._write('reset %s\n' % (ref,))
200
self._write('from :%d\n' % mark)
204
"""We are finished building, close the stream, get the id mapping"""
206
if self._process is None:
208
if self._process.wait() != 0:
209
raise errors.GitCommandError(self._process.returncode,
211
self._process.stderr.read())
212
marks_file = open(self._marks_file_name)
214
for line in marks_file:
215
mark, shasum = line.split()
216
assert mark.startswith(':')
217
mapping[int(mark[1:])] = shasum
223
loader = tests.TestLoader()
225
suite = tests.TestSuite()
244
testmod_names = ['%s.%s' % (__name__, t) for t in testmod_names]
245
suite.addTests(loader.loadTestsFromModuleNames(testmod_names))