Show More
@@ -1,928 +1,928 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2014-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | GIT repository module |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | import logging |
|
26 | 26 | import os |
|
27 | 27 | import re |
|
28 | 28 | import shutil |
|
29 | 29 | import time |
|
30 | 30 | |
|
31 | 31 | from zope.cachedescriptors.property import Lazy as LazyProperty |
|
32 | 32 | |
|
33 | 33 | from rhodecode.lib.compat import OrderedDict |
|
34 | 34 | from rhodecode.lib.datelib import makedate, utcdate_fromtimestamp |
|
35 | 35 | from rhodecode.lib.utils import safe_unicode, safe_str |
|
36 | 36 | from rhodecode.lib.vcs import connection, path as vcspath |
|
37 | 37 | from rhodecode.lib.vcs.backends.base import ( |
|
38 | 38 | BaseRepository, CollectionGenerator, Config, MergeResponse, |
|
39 | 39 | MergeFailureReason, Reference) |
|
40 | 40 | from rhodecode.lib.vcs.backends.git.commit import GitCommit |
|
41 | 41 | from rhodecode.lib.vcs.backends.git.diff import GitDiff |
|
42 | 42 | from rhodecode.lib.vcs.backends.git.inmemory import GitInMemoryCommit |
|
43 | 43 | from rhodecode.lib.vcs.exceptions import ( |
|
44 | 44 | CommitDoesNotExistError, EmptyRepositoryError, |
|
45 | 45 | RepositoryError, TagAlreadyExistError, TagDoesNotExistError, VCSError) |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | SHA_PATTERN = re.compile(r'^[[0-9a-fA-F]{12}|[0-9a-fA-F]{40}]$') |
|
49 | 49 | |
|
50 | 50 | log = logging.getLogger(__name__) |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | class GitRepository(BaseRepository): |
|
54 | 54 | """ |
|
55 | 55 | Git repository backend. |
|
56 | 56 | """ |
|
57 | 57 | DEFAULT_BRANCH_NAME = 'master' |
|
58 | 58 | |
|
59 | 59 | contact = BaseRepository.DEFAULT_CONTACT |
|
60 | 60 | |
|
61 | 61 | def __init__(self, repo_path, config=None, create=False, src_url=None, |
|
62 | 62 | update_after_clone=False, with_wire=None, bare=False): |
|
63 | 63 | |
|
64 | 64 | self.path = safe_str(os.path.abspath(repo_path)) |
|
65 | 65 | self.config = config if config else Config() |
|
66 | 66 | self._remote = connection.Git( |
|
67 | 67 | self.path, self.config, with_wire=with_wire) |
|
68 | 68 | |
|
69 | 69 | self._init_repo(create, src_url, update_after_clone, bare) |
|
70 | 70 | |
|
71 | 71 | # caches |
|
72 | 72 | self._commit_ids = {} |
|
73 | 73 | |
|
74 | 74 | self.bookmarks = {} |
|
75 | 75 | |
|
76 | 76 | @LazyProperty |
|
77 | 77 | def bare(self): |
|
78 | 78 | return self._remote.bare() |
|
79 | 79 | |
|
80 | 80 | @LazyProperty |
|
81 | 81 | def head(self): |
|
82 | 82 | return self._remote.head() |
|
83 | 83 | |
|
84 | 84 | @LazyProperty |
|
85 | 85 | def commit_ids(self): |
|
86 | 86 | """ |
|
87 | 87 | Returns list of commit ids, in ascending order. Being lazy |
|
88 | 88 | attribute allows external tools to inject commit ids from cache. |
|
89 | 89 | """ |
|
90 | 90 | commit_ids = self._get_all_commit_ids() |
|
91 | 91 | self._rebuild_cache(commit_ids) |
|
92 | 92 | return commit_ids |
|
93 | 93 | |
|
94 | 94 | def _rebuild_cache(self, commit_ids): |
|
95 | 95 | self._commit_ids = dict((commit_id, index) |
|
96 | 96 | for index, commit_id in enumerate(commit_ids)) |
|
97 | 97 | |
|
98 | 98 | def run_git_command(self, cmd, **opts): |
|
99 | 99 | """ |
|
100 | 100 | Runs given ``cmd`` as git command and returns tuple |
|
101 | 101 | (stdout, stderr). |
|
102 | 102 | |
|
103 | 103 | :param cmd: git command to be executed |
|
104 | 104 | :param opts: env options to pass into Subprocess command |
|
105 | 105 | """ |
|
106 | 106 | if not isinstance(cmd, list): |
|
107 | 107 | raise ValueError('cmd must be a list, got %s instead' % type(cmd)) |
|
108 | 108 | |
|
109 | 109 | out, err = self._remote.run_git_command(cmd, **opts) |
|
110 | 110 | log.debug('Stderr output of git command "%s":\n%s', cmd, err) |
|
111 | 111 | return out, err |
|
112 | 112 | |
|
113 | 113 | @staticmethod |
|
114 | 114 | def check_url(url, config): |
|
115 | 115 | """ |
|
116 | 116 | Function will check given url and try to verify if it's a valid |
|
117 | 117 | link. Sometimes it may happened that git will issue basic |
|
118 | 118 | auth request that can cause whole API to hang when used from python |
|
119 | 119 | or other external calls. |
|
120 | 120 | |
|
121 | 121 | On failures it'll raise urllib2.HTTPError, exception is also thrown |
|
122 | 122 | when the return code is non 200 |
|
123 | 123 | """ |
|
124 | 124 | # check first if it's not an url |
|
125 | 125 | if os.path.isdir(url) or url.startswith('file:'): |
|
126 | 126 | return True |
|
127 | 127 | |
|
128 | 128 | if '+' in url.split('://', 1)[0]: |
|
129 | 129 | url = url.split('+', 1)[1] |
|
130 | 130 | |
|
131 | 131 | # Request the _remote to verify the url |
|
132 | 132 | return connection.Git.check_url(url, config.serialize()) |
|
133 | 133 | |
|
134 | 134 | @staticmethod |
|
135 | 135 | def is_valid_repository(path): |
|
136 | 136 | if os.path.isdir(os.path.join(path, '.git')): |
|
137 | 137 | return True |
|
138 | 138 | # check case of bare repository |
|
139 | 139 | try: |
|
140 | 140 | GitRepository(path) |
|
141 | 141 | return True |
|
142 | 142 | except VCSError: |
|
143 | 143 | pass |
|
144 | 144 | return False |
|
145 | 145 | |
|
146 | 146 | def _init_repo(self, create, src_url=None, update_after_clone=False, |
|
147 | 147 | bare=False): |
|
148 | 148 | if create and os.path.exists(self.path): |
|
149 | 149 | raise RepositoryError( |
|
150 | 150 | "Cannot create repository at %s, location already exist" |
|
151 | 151 | % self.path) |
|
152 | 152 | |
|
153 | 153 | try: |
|
154 | 154 | if create and src_url: |
|
155 | 155 | GitRepository.check_url(src_url, self.config) |
|
156 | 156 | self.clone(src_url, update_after_clone, bare) |
|
157 | 157 | elif create: |
|
158 | 158 | os.makedirs(self.path, mode=0755) |
|
159 | 159 | |
|
160 | 160 | if bare: |
|
161 | 161 | self._remote.init_bare() |
|
162 | 162 | else: |
|
163 | 163 | self._remote.init() |
|
164 | 164 | else: |
|
165 | 165 | self._remote.assert_correct_path() |
|
166 | 166 | # TODO: johbo: check if we have to translate the OSError here |
|
167 | 167 | except OSError as err: |
|
168 | 168 | raise RepositoryError(err) |
|
169 | 169 | |
|
170 | 170 | def _get_all_commit_ids(self, filters=None): |
|
171 | 171 | # we must check if this repo is not empty, since later command |
|
172 | 172 | # fails if it is. And it's cheaper to ask than throw the subprocess |
|
173 | 173 | # errors |
|
174 | 174 | try: |
|
175 | 175 | self._remote.head() |
|
176 | 176 | except KeyError: |
|
177 | 177 | return [] |
|
178 | 178 | |
|
179 | 179 | rev_filter = ['--branches', '--tags'] |
|
180 | 180 | extra_filter = [] |
|
181 | 181 | |
|
182 | 182 | if filters: |
|
183 | 183 | if filters.get('since'): |
|
184 | 184 | extra_filter.append('--since=%s' % (filters['since'])) |
|
185 | 185 | if filters.get('until'): |
|
186 | 186 | extra_filter.append('--until=%s' % (filters['until'])) |
|
187 | 187 | if filters.get('branch_name'): |
|
188 | 188 | rev_filter = ['--tags'] |
|
189 | 189 | extra_filter.append(filters['branch_name']) |
|
190 | 190 | rev_filter.extend(extra_filter) |
|
191 | 191 | |
|
192 | 192 | # if filters.get('start') or filters.get('end'): |
|
193 | 193 | # # skip is offset, max-count is limit |
|
194 | 194 | # if filters.get('start'): |
|
195 | 195 | # extra_filter += ' --skip=%s' % filters['start'] |
|
196 | 196 | # if filters.get('end'): |
|
197 | 197 | # extra_filter += ' --max-count=%s' % (filters['end'] - (filters['start'] or 0)) |
|
198 | 198 | |
|
199 | 199 | cmd = ['rev-list', '--reverse', '--date-order'] + rev_filter |
|
200 | 200 | try: |
|
201 | 201 | output, __ = self.run_git_command(cmd) |
|
202 | 202 | except RepositoryError: |
|
203 | 203 | # Can be raised for empty repositories |
|
204 | 204 | return [] |
|
205 | 205 | return output.splitlines() |
|
206 | 206 | |
|
207 | 207 | def _get_commit_id(self, commit_id_or_idx): |
|
208 | 208 | def is_null(value): |
|
209 | 209 | return len(value) == commit_id_or_idx.count('0') |
|
210 | 210 | |
|
211 | 211 | if self.is_empty(): |
|
212 | 212 | raise EmptyRepositoryError("There are no commits yet") |
|
213 | 213 | |
|
214 | 214 | if commit_id_or_idx in (None, '', 'tip', 'HEAD', 'head', -1): |
|
215 | 215 | return self.commit_ids[-1] |
|
216 | 216 | |
|
217 | 217 | is_bstr = isinstance(commit_id_or_idx, (str, unicode)) |
|
218 | 218 | if ((is_bstr and commit_id_or_idx.isdigit() and len(commit_id_or_idx) < 12) |
|
219 | 219 | or isinstance(commit_id_or_idx, int) or is_null(commit_id_or_idx)): |
|
220 | 220 | try: |
|
221 | 221 | commit_id_or_idx = self.commit_ids[int(commit_id_or_idx)] |
|
222 | 222 | except Exception: |
|
223 | 223 | msg = "Commit %s does not exist for %s" % ( |
|
224 | 224 | commit_id_or_idx, self) |
|
225 | 225 | raise CommitDoesNotExistError(msg) |
|
226 | 226 | |
|
227 | 227 | elif is_bstr: |
|
228 | 228 | # check full path ref, eg. refs/heads/master |
|
229 | 229 | ref_id = self._refs.get(commit_id_or_idx) |
|
230 | 230 | if ref_id: |
|
231 | 231 | return ref_id |
|
232 | 232 | |
|
233 | 233 | # check branch name |
|
234 | 234 | branch_ids = self.branches.values() |
|
235 | 235 | ref_id = self._refs.get('refs/heads/%s' % commit_id_or_idx) |
|
236 | 236 | if ref_id: |
|
237 | 237 | return ref_id |
|
238 | 238 | |
|
239 | 239 | # check tag name |
|
240 | 240 | ref_id = self._refs.get('refs/tags/%s' % commit_id_or_idx) |
|
241 | 241 | if ref_id: |
|
242 | 242 | return ref_id |
|
243 | 243 | |
|
244 | 244 | if (not SHA_PATTERN.match(commit_id_or_idx) or |
|
245 | 245 | commit_id_or_idx not in self.commit_ids): |
|
246 | 246 | msg = "Commit %s does not exist for %s" % ( |
|
247 | 247 | commit_id_or_idx, self) |
|
248 | 248 | raise CommitDoesNotExistError(msg) |
|
249 | 249 | |
|
250 | 250 | # Ensure we return full id |
|
251 | 251 | if not SHA_PATTERN.match(str(commit_id_or_idx)): |
|
252 | 252 | raise CommitDoesNotExistError( |
|
253 | 253 | "Given commit id %s not recognized" % commit_id_or_idx) |
|
254 | 254 | return commit_id_or_idx |
|
255 | 255 | |
|
256 | 256 | def get_hook_location(self): |
|
257 | 257 | """ |
|
258 | 258 | returns absolute path to location where hooks are stored |
|
259 | 259 | """ |
|
260 | 260 | loc = os.path.join(self.path, 'hooks') |
|
261 | 261 | if not self.bare: |
|
262 | 262 | loc = os.path.join(self.path, '.git', 'hooks') |
|
263 | 263 | return loc |
|
264 | 264 | |
|
265 | 265 | @LazyProperty |
|
266 | 266 | def last_change(self): |
|
267 | 267 | """ |
|
268 | 268 | Returns last change made on this repository as |
|
269 | 269 | `datetime.datetime` object. |
|
270 | 270 | """ |
|
271 | 271 | return utcdate_fromtimestamp(self._get_mtime(), makedate()[1]) |
|
272 | 272 | |
|
273 | 273 | def _get_mtime(self): |
|
274 | 274 | try: |
|
275 | 275 | return time.mktime(self.get_commit().date.timetuple()) |
|
276 | 276 | except RepositoryError: |
|
277 | 277 | idx_loc = '' if self.bare else '.git' |
|
278 | 278 | # fallback to filesystem |
|
279 | 279 | in_path = os.path.join(self.path, idx_loc, "index") |
|
280 | 280 | he_path = os.path.join(self.path, idx_loc, "HEAD") |
|
281 | 281 | if os.path.exists(in_path): |
|
282 | 282 | return os.stat(in_path).st_mtime |
|
283 | 283 | else: |
|
284 | 284 | return os.stat(he_path).st_mtime |
|
285 | 285 | |
|
286 | 286 | @LazyProperty |
|
287 | 287 | def description(self): |
|
288 | 288 | description = self._remote.get_description() |
|
289 | 289 | return safe_unicode(description or self.DEFAULT_DESCRIPTION) |
|
290 | 290 | |
|
291 | 291 | def _get_refs_entries(self, prefix='', reverse=False, strip_prefix=True): |
|
292 | 292 | if self.is_empty(): |
|
293 | 293 | return OrderedDict() |
|
294 | 294 | |
|
295 | 295 | result = [] |
|
296 | 296 | for ref, sha in self._refs.iteritems(): |
|
297 | 297 | if ref.startswith(prefix): |
|
298 | 298 | ref_name = ref |
|
299 | 299 | if strip_prefix: |
|
300 | 300 | ref_name = ref[len(prefix):] |
|
301 | 301 | result.append((safe_unicode(ref_name), sha)) |
|
302 | 302 | |
|
303 | 303 | def get_name(entry): |
|
304 | 304 | return entry[0] |
|
305 | 305 | |
|
306 | 306 | return OrderedDict(sorted(result, key=get_name, reverse=reverse)) |
|
307 | 307 | |
|
308 | 308 | def _get_branches(self): |
|
309 | 309 | return self._get_refs_entries(prefix='refs/heads/', strip_prefix=True) |
|
310 | 310 | |
|
311 | 311 | @LazyProperty |
|
312 | 312 | def branches(self): |
|
313 | 313 | return self._get_branches() |
|
314 | 314 | |
|
315 | 315 | @LazyProperty |
|
316 | 316 | def branches_closed(self): |
|
317 | 317 | return {} |
|
318 | 318 | |
|
319 | 319 | @LazyProperty |
|
320 | 320 | def branches_all(self): |
|
321 | 321 | all_branches = {} |
|
322 | 322 | all_branches.update(self.branches) |
|
323 | 323 | all_branches.update(self.branches_closed) |
|
324 | 324 | return all_branches |
|
325 | 325 | |
|
326 | 326 | @LazyProperty |
|
327 | 327 | def tags(self): |
|
328 | 328 | return self._get_tags() |
|
329 | 329 | |
|
330 | 330 | def _get_tags(self): |
|
331 | 331 | return self._get_refs_entries( |
|
332 | 332 | prefix='refs/tags/', strip_prefix=True, reverse=True) |
|
333 | 333 | |
|
334 | 334 | def tag(self, name, user, commit_id=None, message=None, date=None, |
|
335 | 335 | **kwargs): |
|
336 | 336 | # TODO: fix this method to apply annotated tags correct with message |
|
337 | 337 | """ |
|
338 | 338 | Creates and returns a tag for the given ``commit_id``. |
|
339 | 339 | |
|
340 | 340 | :param name: name for new tag |
|
341 | 341 | :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>" |
|
342 | 342 | :param commit_id: commit id for which new tag would be created |
|
343 | 343 | :param message: message of the tag's commit |
|
344 | 344 | :param date: date of tag's commit |
|
345 | 345 | |
|
346 | 346 | :raises TagAlreadyExistError: if tag with same name already exists |
|
347 | 347 | """ |
|
348 | 348 | if name in self.tags: |
|
349 | 349 | raise TagAlreadyExistError("Tag %s already exists" % name) |
|
350 | 350 | commit = self.get_commit(commit_id=commit_id) |
|
351 | 351 | message = message or "Added tag %s for commit %s" % ( |
|
352 | 352 | name, commit.raw_id) |
|
353 | 353 | self._remote.set_refs('refs/tags/%s' % name, commit._commit['id']) |
|
354 | 354 | |
|
355 | 355 | self._refs = self._get_refs() |
|
356 | 356 | self.tags = self._get_tags() |
|
357 | 357 | return commit |
|
358 | 358 | |
|
359 | 359 | def remove_tag(self, name, user, message=None, date=None): |
|
360 | 360 | """ |
|
361 | 361 | Removes tag with the given ``name``. |
|
362 | 362 | |
|
363 | 363 | :param name: name of the tag to be removed |
|
364 | 364 | :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>" |
|
365 | 365 | :param message: message of the tag's removal commit |
|
366 | 366 | :param date: date of tag's removal commit |
|
367 | 367 | |
|
368 | 368 | :raises TagDoesNotExistError: if tag with given name does not exists |
|
369 | 369 | """ |
|
370 | 370 | if name not in self.tags: |
|
371 | 371 | raise TagDoesNotExistError("Tag %s does not exist" % name) |
|
372 | 372 | tagpath = vcspath.join( |
|
373 | 373 | self._remote.get_refs_path(), 'refs', 'tags', name) |
|
374 | 374 | try: |
|
375 | 375 | os.remove(tagpath) |
|
376 | 376 | self._refs = self._get_refs() |
|
377 | 377 | self.tags = self._get_tags() |
|
378 | 378 | except OSError as e: |
|
379 | 379 | raise RepositoryError(e.strerror) |
|
380 | 380 | |
|
381 | 381 | def _get_refs(self): |
|
382 | 382 | return self._remote.get_refs() |
|
383 | 383 | |
|
384 | 384 | @LazyProperty |
|
385 | 385 | def _refs(self): |
|
386 | 386 | return self._get_refs() |
|
387 | 387 | |
|
388 | 388 | @property |
|
389 | 389 | def _ref_tree(self): |
|
390 | 390 | node = tree = {} |
|
391 | 391 | for ref, sha in self._refs.iteritems(): |
|
392 | 392 | path = ref.split('/') |
|
393 | 393 | for bit in path[:-1]: |
|
394 | 394 | node = node.setdefault(bit, {}) |
|
395 | 395 | node[path[-1]] = sha |
|
396 | 396 | node = tree |
|
397 | 397 | return tree |
|
398 | 398 | |
|
399 | 399 | def get_commit(self, commit_id=None, commit_idx=None, pre_load=None): |
|
400 | 400 | """ |
|
401 | 401 | Returns `GitCommit` object representing commit from git repository |
|
402 | 402 | at the given `commit_id` or head (most recent commit) if None given. |
|
403 | 403 | """ |
|
404 | 404 | if commit_id is not None: |
|
405 | 405 | self._validate_commit_id(commit_id) |
|
406 | 406 | elif commit_idx is not None: |
|
407 | 407 | self._validate_commit_idx(commit_idx) |
|
408 | 408 | commit_id = commit_idx |
|
409 | 409 | commit_id = self._get_commit_id(commit_id) |
|
410 | 410 | try: |
|
411 | 411 | # Need to call remote to translate id for tagging scenario |
|
412 | 412 | commit_id = self._remote.get_object(commit_id)["commit_id"] |
|
413 | 413 | idx = self._commit_ids[commit_id] |
|
414 | 414 | except KeyError: |
|
415 | 415 | raise RepositoryError("Cannot get object with id %s" % commit_id) |
|
416 | 416 | |
|
417 | 417 | return GitCommit(self, commit_id, idx, pre_load=pre_load) |
|
418 | 418 | |
|
419 | 419 | def get_commits( |
|
420 | 420 | self, start_id=None, end_id=None, start_date=None, end_date=None, |
|
421 | 421 | branch_name=None, pre_load=None): |
|
422 | 422 | """ |
|
423 | 423 | Returns generator of `GitCommit` objects from start to end (both |
|
424 | 424 | are inclusive), in ascending date order. |
|
425 | 425 | |
|
426 | 426 | :param start_id: None, str(commit_id) |
|
427 | 427 | :param end_id: None, str(commit_id) |
|
428 | 428 | :param start_date: if specified, commits with commit date less than |
|
429 | 429 | ``start_date`` would be filtered out from returned set |
|
430 | 430 | :param end_date: if specified, commits with commit date greater than |
|
431 | 431 | ``end_date`` would be filtered out from returned set |
|
432 | 432 | :param branch_name: if specified, commits not reachable from given |
|
433 | 433 | branch would be filtered out from returned set |
|
434 | 434 | |
|
435 | 435 | :raise BranchDoesNotExistError: If given `branch_name` does not |
|
436 | 436 | exist. |
|
437 | 437 | :raise CommitDoesNotExistError: If commits for given `start` or |
|
438 | 438 | `end` could not be found. |
|
439 | 439 | |
|
440 | 440 | """ |
|
441 | 441 | if self.is_empty(): |
|
442 | 442 | raise EmptyRepositoryError("There are no commits yet") |
|
443 | 443 | self._validate_branch_name(branch_name) |
|
444 | 444 | |
|
445 | 445 | if start_id is not None: |
|
446 | 446 | self._validate_commit_id(start_id) |
|
447 | 447 | if end_id is not None: |
|
448 | 448 | self._validate_commit_id(end_id) |
|
449 | 449 | |
|
450 | 450 | start_raw_id = self._get_commit_id(start_id) |
|
451 | 451 | start_pos = self._commit_ids[start_raw_id] if start_id else None |
|
452 | 452 | end_raw_id = self._get_commit_id(end_id) |
|
453 | 453 | end_pos = max(0, self._commit_ids[end_raw_id]) if end_id else None |
|
454 | 454 | |
|
455 | 455 | if None not in [start_id, end_id] and start_pos > end_pos: |
|
456 | 456 | raise RepositoryError( |
|
457 | 457 | "Start commit '%s' cannot be after end commit '%s'" % |
|
458 | 458 | (start_id, end_id)) |
|
459 | 459 | |
|
460 | 460 | if end_pos is not None: |
|
461 | 461 | end_pos += 1 |
|
462 | 462 | |
|
463 | 463 | filter_ = [] |
|
464 | 464 | if branch_name: |
|
465 | 465 | filter_.append({'branch_name': branch_name}) |
|
466 | 466 | if start_date and not end_date: |
|
467 | 467 | filter_.append({'since': start_date}) |
|
468 | 468 | if end_date and not start_date: |
|
469 | 469 | filter_.append({'until': end_date}) |
|
470 | 470 | if start_date and end_date: |
|
471 | 471 | filter_.append({'since': start_date}) |
|
472 | 472 | filter_.append({'until': end_date}) |
|
473 | 473 | |
|
474 | 474 | # if start_pos or end_pos: |
|
475 | 475 | # filter_.append({'start': start_pos}) |
|
476 | 476 | # filter_.append({'end': end_pos}) |
|
477 | 477 | |
|
478 | 478 | if filter_: |
|
479 | 479 | revfilters = { |
|
480 | 480 | 'branch_name': branch_name, |
|
481 | 481 | 'since': start_date.strftime('%m/%d/%y %H:%M:%S') if start_date else None, |
|
482 | 482 | 'until': end_date.strftime('%m/%d/%y %H:%M:%S') if end_date else None, |
|
483 | 483 | 'start': start_pos, |
|
484 | 484 | 'end': end_pos, |
|
485 | 485 | } |
|
486 | 486 | commit_ids = self._get_all_commit_ids(filters=revfilters) |
|
487 | 487 | |
|
488 | 488 | # pure python stuff, it's slow due to walker walking whole repo |
|
489 | 489 | # def get_revs(walker): |
|
490 | 490 | # for walker_entry in walker: |
|
491 | 491 | # yield walker_entry.commit.id |
|
492 | 492 | # revfilters = {} |
|
493 | 493 | # commit_ids = list(reversed(list(get_revs(self._repo.get_walker(**revfilters))))) |
|
494 | 494 | else: |
|
495 | 495 | commit_ids = self.commit_ids |
|
496 | 496 | |
|
497 | 497 | if start_pos or end_pos: |
|
498 | 498 | commit_ids = commit_ids[start_pos: end_pos] |
|
499 | 499 | |
|
500 | 500 | return CollectionGenerator(self, commit_ids, pre_load=pre_load) |
|
501 | 501 | |
|
502 | 502 | def get_diff( |
|
503 | 503 | self, commit1, commit2, path='', ignore_whitespace=False, |
|
504 | 504 | context=3, path1=None): |
|
505 | 505 | """ |
|
506 | 506 | Returns (git like) *diff*, as plain text. Shows changes introduced by |
|
507 | 507 | ``commit2`` since ``commit1``. |
|
508 | 508 | |
|
509 | 509 | :param commit1: Entry point from which diff is shown. Can be |
|
510 | 510 | ``self.EMPTY_COMMIT`` - in this case, patch showing all |
|
511 | 511 | the changes since empty state of the repository until ``commit2`` |
|
512 | 512 | :param commit2: Until which commits changes should be shown. |
|
513 | 513 | :param ignore_whitespace: If set to ``True``, would not show whitespace |
|
514 | 514 | changes. Defaults to ``False``. |
|
515 | 515 | :param context: How many lines before/after changed lines should be |
|
516 | 516 | shown. Defaults to ``3``. |
|
517 | 517 | """ |
|
518 | 518 | self._validate_diff_commits(commit1, commit2) |
|
519 | 519 | if path1 is not None and path1 != path: |
|
520 | 520 | raise ValueError("Diff of two different paths not supported.") |
|
521 | 521 | |
|
522 | 522 | flags = [ |
|
523 | 523 | '-U%s' % context, '--full-index', '--binary', '-p', |
|
524 | 524 | '-M', '--abbrev=40'] |
|
525 | 525 | if ignore_whitespace: |
|
526 | 526 | flags.append('-w') |
|
527 | 527 | |
|
528 | 528 | if commit1 == self.EMPTY_COMMIT: |
|
529 | 529 | cmd = ['show'] + flags + [commit2.raw_id] |
|
530 | 530 | else: |
|
531 | 531 | cmd = ['diff'] + flags + [commit1.raw_id, commit2.raw_id] |
|
532 | 532 | |
|
533 | 533 | if path: |
|
534 | 534 | cmd.extend(['--', path]) |
|
535 | 535 | |
|
536 | 536 | stdout, __ = self.run_git_command(cmd) |
|
537 | 537 | # If we used 'show' command, strip first few lines (until actual diff |
|
538 | 538 | # starts) |
|
539 | 539 | if commit1 == self.EMPTY_COMMIT: |
|
540 | 540 | lines = stdout.splitlines() |
|
541 | 541 | x = 0 |
|
542 | 542 | for line in lines: |
|
543 | 543 | if line.startswith('diff'): |
|
544 | 544 | break |
|
545 | 545 | x += 1 |
|
546 | 546 | # Append new line just like 'diff' command do |
|
547 | 547 | stdout = '\n'.join(lines[x:]) + '\n' |
|
548 | 548 | return GitDiff(stdout) |
|
549 | 549 | |
|
550 | 550 | def strip(self, commit_id, branch_name): |
|
551 | 551 | commit = self.get_commit(commit_id=commit_id) |
|
552 | 552 | if commit.merge: |
|
553 | 553 | raise Exception('Cannot reset to merge commit') |
|
554 | 554 | |
|
555 | 555 | # parent is going to be the new head now |
|
556 | 556 | commit = commit.parents[0] |
|
557 | 557 | self._remote.set_refs('refs/heads/%s' % branch_name, commit.raw_id) |
|
558 | 558 | |
|
559 | 559 | self.commit_ids = self._get_all_commit_ids() |
|
560 | 560 | self._rebuild_cache(self.commit_ids) |
|
561 | 561 | |
|
562 | 562 | def get_common_ancestor(self, commit_id1, commit_id2, repo2): |
|
563 | 563 | if commit_id1 == commit_id2: |
|
564 | 564 | return commit_id1 |
|
565 | 565 | |
|
566 | 566 | if self != repo2: |
|
567 | 567 | commits = self._remote.get_missing_revs( |
|
568 | 568 | commit_id1, commit_id2, repo2.path) |
|
569 | 569 | if commits: |
|
570 | 570 | commit = repo2.get_commit(commits[-1]) |
|
571 | 571 | if commit.parents: |
|
572 | 572 | ancestor_id = commit.parents[0].raw_id |
|
573 | 573 | else: |
|
574 | 574 | ancestor_id = None |
|
575 | 575 | else: |
|
576 | 576 | # no commits from other repo, ancestor_id is the commit_id2 |
|
577 | 577 | ancestor_id = commit_id2 |
|
578 | 578 | else: |
|
579 | 579 | output, __ = self.run_git_command( |
|
580 | 580 | ['merge-base', commit_id1, commit_id2]) |
|
581 | 581 | ancestor_id = re.findall(r'[0-9a-fA-F]{40}', output)[0] |
|
582 | 582 | |
|
583 | 583 | return ancestor_id |
|
584 | 584 | |
|
585 | 585 | def compare(self, commit_id1, commit_id2, repo2, merge, pre_load=None): |
|
586 | 586 | repo1 = self |
|
587 | 587 | ancestor_id = None |
|
588 | 588 | |
|
589 | 589 | if commit_id1 == commit_id2: |
|
590 | 590 | commits = [] |
|
591 | 591 | elif repo1 != repo2: |
|
592 | 592 | missing_ids = self._remote.get_missing_revs(commit_id1, commit_id2, |
|
593 | 593 | repo2.path) |
|
594 | 594 | commits = [ |
|
595 | 595 | repo2.get_commit(commit_id=commit_id, pre_load=pre_load) |
|
596 | 596 | for commit_id in reversed(missing_ids)] |
|
597 | 597 | else: |
|
598 | 598 | output, __ = repo1.run_git_command( |
|
599 | 599 | ['log', '--reverse', '--pretty=format: %H', '-s', |
|
600 | 600 | '%s..%s' % (commit_id1, commit_id2)]) |
|
601 | 601 | commits = [ |
|
602 | 602 | repo1.get_commit(commit_id=commit_id, pre_load=pre_load) |
|
603 | 603 | for commit_id in re.findall(r'[0-9a-fA-F]{40}', output)] |
|
604 | 604 | |
|
605 | 605 | return commits |
|
606 | 606 | |
|
607 | 607 | @LazyProperty |
|
608 | 608 | def in_memory_commit(self): |
|
609 | 609 | """ |
|
610 | 610 | Returns ``GitInMemoryCommit`` object for this repository. |
|
611 | 611 | """ |
|
612 | 612 | return GitInMemoryCommit(self) |
|
613 | 613 | |
|
614 | 614 | def clone(self, url, update_after_clone=True, bare=False): |
|
615 | 615 | """ |
|
616 | 616 | Tries to clone commits from external location. |
|
617 | 617 | |
|
618 | 618 | :param update_after_clone: If set to ``False``, git won't checkout |
|
619 | 619 | working directory |
|
620 | 620 | :param bare: If set to ``True``, repository would be cloned into |
|
621 | 621 | *bare* git repository (no working directory at all). |
|
622 | 622 | """ |
|
623 | 623 | # init_bare and init expect empty dir created to proceed |
|
624 | 624 | if not os.path.exists(self.path): |
|
625 | 625 | os.mkdir(self.path) |
|
626 | 626 | |
|
627 | 627 | if bare: |
|
628 | 628 | self._remote.init_bare() |
|
629 | 629 | else: |
|
630 | 630 | self._remote.init() |
|
631 | 631 | |
|
632 | 632 | deferred = '^{}' |
|
633 | 633 | valid_refs = ('refs/heads', 'refs/tags', 'HEAD') |
|
634 | 634 | |
|
635 | 635 | return self._remote.clone( |
|
636 | 636 | url, deferred, valid_refs, update_after_clone) |
|
637 | 637 | |
|
638 | 638 | def pull(self, url, commit_ids=None): |
|
639 | 639 | """ |
|
640 | 640 | Tries to pull changes from external location. We use fetch here since |
|
641 | 641 | pull in get does merges and we want to be compatible with hg backend so |
|
642 | 642 | pull == fetch in this case |
|
643 | 643 | """ |
|
644 | 644 | self.fetch(url, commit_ids=commit_ids) |
|
645 | 645 | |
|
646 | 646 | def fetch(self, url, commit_ids=None): |
|
647 | 647 | """ |
|
648 | 648 | Tries to fetch changes from external location. |
|
649 | 649 | """ |
|
650 | 650 | refs = None |
|
651 | 651 | |
|
652 | 652 | if commit_ids is not None: |
|
653 | 653 | remote_refs = self._remote.get_remote_refs(url) |
|
654 | 654 | refs = [ |
|
655 | 655 | ref for ref in remote_refs if remote_refs[ref] in commit_ids] |
|
656 | 656 | self._remote.fetch(url, refs=refs) |
|
657 | 657 | |
|
658 | 658 | def set_refs(self, ref_name, commit_id): |
|
659 | 659 | self._remote.set_refs(ref_name, commit_id) |
|
660 | 660 | |
|
661 | 661 | def remove_ref(self, ref_name): |
|
662 | 662 | self._remote.remove_ref(ref_name) |
|
663 | 663 | |
|
664 | 664 | def _update_server_info(self): |
|
665 | 665 | """ |
|
666 | 666 | runs gits update-server-info command in this repo instance |
|
667 | 667 | """ |
|
668 | 668 | self._remote.update_server_info() |
|
669 | 669 | |
|
670 | 670 | def _current_branch(self): |
|
671 | 671 | """ |
|
672 | 672 | Return the name of the current branch. |
|
673 | 673 | |
|
674 | 674 | It only works for non bare repositories (i.e. repositories with a |
|
675 | 675 | working copy) |
|
676 | 676 | """ |
|
677 | 677 | if self.bare: |
|
678 | 678 | raise RepositoryError('Bare git repos do not have active branches') |
|
679 | 679 | |
|
680 | 680 | if self.is_empty(): |
|
681 | 681 | return None |
|
682 | 682 | |
|
683 | 683 | stdout, _ = self.run_git_command(['rev-parse', '--abbrev-ref', 'HEAD']) |
|
684 | 684 | return stdout.strip() |
|
685 | 685 | |
|
686 | 686 | def _checkout(self, branch_name, create=False): |
|
687 | 687 | """ |
|
688 | 688 | Checkout a branch in the working directory. |
|
689 | 689 | |
|
690 | 690 | It tries to create the branch if create is True, failing if the branch |
|
691 | 691 | already exists. |
|
692 | 692 | |
|
693 | 693 | It only works for non bare repositories (i.e. repositories with a |
|
694 | 694 | working copy) |
|
695 | 695 | """ |
|
696 | 696 | if self.bare: |
|
697 | 697 | raise RepositoryError('Cannot checkout branches in a bare git repo') |
|
698 | 698 | |
|
699 | 699 | cmd = ['checkout'] |
|
700 | 700 | if create: |
|
701 | 701 | cmd.append('-b') |
|
702 | 702 | cmd.append(branch_name) |
|
703 | 703 | self.run_git_command(cmd, fail_on_stderr=False) |
|
704 | 704 | |
|
705 | 705 | def _local_clone(self, clone_path, branch_name): |
|
706 | 706 | """ |
|
707 | 707 | Create a local clone of the current repo. |
|
708 | 708 | """ |
|
709 | 709 | # N.B.(skreft): the --branch option is required as otherwise the shallow |
|
710 | 710 | # clone will only fetch the active branch. |
|
711 | 711 | cmd = ['clone', '--branch', branch_name, '--single-branch', |
|
712 | 712 | self.path, os.path.abspath(clone_path)] |
|
713 | 713 | self.run_git_command(cmd, fail_on_stderr=False) |
|
714 | 714 | |
|
715 | 715 | def _local_fetch(self, repository_path, branch_name): |
|
716 | 716 | """ |
|
717 | 717 | Fetch a branch from a local repository. |
|
718 | 718 | """ |
|
719 | 719 | repository_path = os.path.abspath(repository_path) |
|
720 | 720 | if repository_path == self.path: |
|
721 | 721 | raise ValueError('Cannot fetch from the same repository') |
|
722 | 722 | |
|
723 | 723 | cmd = ['fetch', '--no-tags', repository_path, branch_name] |
|
724 | 724 | self.run_git_command(cmd, fail_on_stderr=False) |
|
725 | 725 | |
|
726 | 726 | def _last_fetch_heads(self): |
|
727 | 727 | """ |
|
728 | 728 | Return the last fetched heads that need merging. |
|
729 | 729 | |
|
730 | 730 | The algorithm is defined at |
|
731 | 731 | https://github.com/git/git/blob/v2.1.3/git-pull.sh#L283 |
|
732 | 732 | """ |
|
733 | 733 | if not self.bare: |
|
734 | 734 | fetch_heads_path = os.path.join(self.path, '.git', 'FETCH_HEAD') |
|
735 | 735 | else: |
|
736 | 736 | fetch_heads_path = os.path.join(self.path, 'FETCH_HEAD') |
|
737 | 737 | |
|
738 | 738 | heads = [] |
|
739 | 739 | with open(fetch_heads_path) as f: |
|
740 | 740 | for line in f: |
|
741 | 741 | if ' not-for-merge ' in line: |
|
742 | 742 | continue |
|
743 | 743 | line = re.sub('\t.*', '', line, flags=re.DOTALL) |
|
744 | 744 | heads.append(line) |
|
745 | 745 | |
|
746 | 746 | return heads |
|
747 | 747 | |
|
748 | 748 | def _local_pull(self, repository_path, branch_name): |
|
749 | 749 | """ |
|
750 | 750 | Pull a branch from a local repository. |
|
751 | 751 | """ |
|
752 | 752 | if self.bare: |
|
753 | 753 | raise RepositoryError('Cannot pull into a bare git repository') |
|
754 | 754 | # N.B.(skreft): The --ff-only option is to make sure this is a |
|
755 | 755 | # fast-forward (i.e., we are only pulling new changes and there are no |
|
756 | 756 | # conflicts with our current branch) |
|
757 | 757 | # Additionally, that option needs to go before --no-tags, otherwise git |
|
758 | 758 | # pull complains about it being an unknown flag. |
|
759 | 759 | cmd = ['pull', '--ff-only', '--no-tags', repository_path, branch_name] |
|
760 | 760 | self.run_git_command(cmd, fail_on_stderr=False) |
|
761 | 761 | |
|
762 | 762 | def _local_merge(self, merge_message, user_name, user_email, heads): |
|
763 | 763 | """ |
|
764 | 764 | Merge the given head into the checked out branch. |
|
765 | 765 | |
|
766 | 766 | It will force a merge commit. |
|
767 | 767 | |
|
768 | 768 | Currently it raises an error if the repo is empty, as it is not possible |
|
769 | 769 | to create a merge commit in an empty repo. |
|
770 | 770 | |
|
771 | 771 | :param merge_message: The message to use for the merge commit. |
|
772 | 772 | :param heads: the heads to merge. |
|
773 | 773 | """ |
|
774 | 774 | if self.bare: |
|
775 | 775 | raise RepositoryError('Cannot merge into a bare git repository') |
|
776 | 776 | |
|
777 | 777 | if not heads: |
|
778 | 778 | return |
|
779 | 779 | |
|
780 | 780 | if self.is_empty(): |
|
781 | 781 | # TODO(skreft): do somehting more robust in this case. |
|
782 | 782 | raise RepositoryError( |
|
783 | 783 | 'Do not know how to merge into empty repositories yet') |
|
784 | 784 | |
|
785 | 785 | # N.B.(skreft): the --no-ff option is used to enforce the creation of a |
|
786 | 786 | # commit message. We also specify the user who is doing the merge. |
|
787 | 787 | cmd = ['-c', 'user.name=%s' % safe_str(user_name), |
|
788 | 788 | '-c', 'user.email=%s' % safe_str(user_email), |
|
789 | 789 | 'merge', '--no-ff', '-m', safe_str(merge_message)] |
|
790 | 790 | cmd.extend(heads) |
|
791 | 791 | try: |
|
792 | 792 | self.run_git_command(cmd, fail_on_stderr=False) |
|
793 | 793 | except RepositoryError: |
|
794 | 794 | # Cleanup any merge leftovers |
|
795 | 795 | self.run_git_command(['merge', '--abort'], fail_on_stderr=False) |
|
796 | 796 | raise |
|
797 | 797 | |
|
798 | 798 | def _local_push( |
|
799 | 799 | self, source_branch, repository_path, target_branch, |
|
800 | 800 | enable_hooks=False, rc_scm_data=None): |
|
801 | 801 | """ |
|
802 | 802 | Push the source_branch to the given repository and target_branch. |
|
803 | 803 | |
|
804 | 804 | Currently it if the target_branch is not master and the target repo is |
|
805 | 805 | empty, the push will work, but then GitRepository won't be able to find |
|
806 | 806 | the pushed branch or the commits. As the HEAD will be corrupted (i.e., |
|
807 | 807 | pointing to master, which does not exist). |
|
808 | 808 | |
|
809 | 809 | It does not run the hooks in the target repo. |
|
810 | 810 | """ |
|
811 | 811 | # TODO(skreft): deal with the case in which the target repo is empty, |
|
812 | 812 | # and the target_branch is not master. |
|
813 | 813 | target_repo = GitRepository(repository_path) |
|
814 | 814 | if (not target_repo.bare and |
|
815 | 815 | target_repo._current_branch() == target_branch): |
|
816 | 816 | # Git prevents pushing to the checked out branch, so simulate it by |
|
817 | 817 | # pulling into the target repository. |
|
818 | 818 | target_repo._local_pull(self.path, source_branch) |
|
819 | 819 | else: |
|
820 | 820 | cmd = ['push', os.path.abspath(repository_path), |
|
821 | 821 | '%s:%s' % (source_branch, target_branch)] |
|
822 | 822 | gitenv = {} |
|
823 | 823 | if rc_scm_data: |
|
824 | 824 | gitenv.update({'RC_SCM_DATA': rc_scm_data}) |
|
825 | 825 | |
|
826 | 826 | if not enable_hooks: |
|
827 | 827 | gitenv['RC_SKIP_HOOKS'] = '1' |
|
828 | 828 | self.run_git_command(cmd, fail_on_stderr=False, extra_env=gitenv) |
|
829 | 829 | |
|
830 | 830 | def _get_new_pr_branch(self, source_branch, target_branch): |
|
831 | 831 | prefix = 'pr_%s-%s_' % (source_branch, target_branch) |
|
832 | 832 | pr_branches = [] |
|
833 | 833 | for branch in self.branches: |
|
834 | 834 | if branch.startswith(prefix): |
|
835 | 835 | pr_branches.append(int(branch[len(prefix):])) |
|
836 | 836 | |
|
837 | 837 | if not pr_branches: |
|
838 | 838 | branch_id = 0 |
|
839 | 839 | else: |
|
840 | 840 | branch_id = max(pr_branches) + 1 |
|
841 | 841 | |
|
842 | 842 | return '%s%d' % (prefix, branch_id) |
|
843 | 843 | |
|
844 | 844 | def _merge_repo(self, shadow_repository_path, target_ref, |
|
845 | 845 | source_repo, source_ref, merge_message, |
|
846 | 846 | merger_name, merger_email, dry_run=False, |
|
847 | 847 | use_rebase=False): |
|
848 | 848 | if target_ref.commit_id != self.branches[target_ref.name]: |
|
849 | 849 | return MergeResponse( |
|
850 | 850 | False, False, None, MergeFailureReason.TARGET_IS_NOT_HEAD) |
|
851 | 851 | |
|
852 | 852 | shadow_repo = GitRepository(shadow_repository_path) |
|
853 | 853 | shadow_repo._checkout(target_ref.name) |
|
854 | 854 | shadow_repo._local_pull(self.path, target_ref.name) |
|
855 | 855 | # Need to reload repo to invalidate the cache, or otherwise we cannot |
|
856 | 856 | # retrieve the last target commit. |
|
857 | 857 | shadow_repo = GitRepository(shadow_repository_path) |
|
858 | 858 | if target_ref.commit_id != shadow_repo.branches[target_ref.name]: |
|
859 | 859 | return MergeResponse( |
|
860 | 860 | False, False, None, MergeFailureReason.TARGET_IS_NOT_HEAD) |
|
861 | 861 | |
|
862 | 862 | pr_branch = shadow_repo._get_new_pr_branch( |
|
863 | 863 | source_ref.name, target_ref.name) |
|
864 | 864 | shadow_repo._checkout(pr_branch, create=True) |
|
865 | 865 | try: |
|
866 | 866 | shadow_repo._local_fetch(source_repo.path, source_ref.name) |
|
867 | 867 | except RepositoryError: |
|
868 | 868 | log.exception('Failure when doing local fetch on git shadow repo') |
|
869 | 869 | return MergeResponse( |
|
870 |
False, False, None, MergeFailureReason.MISSING_ |
|
|
870 | False, False, None, MergeFailureReason.MISSING_SOURCE_REF) | |
|
871 | 871 | |
|
872 | 872 | merge_ref = None |
|
873 | 873 | merge_failure_reason = MergeFailureReason.NONE |
|
874 | 874 | try: |
|
875 | 875 | shadow_repo._local_merge(merge_message, merger_name, merger_email, |
|
876 | 876 | [source_ref.commit_id]) |
|
877 | 877 | merge_possible = True |
|
878 | 878 | |
|
879 | 879 | # Need to reload repo to invalidate the cache, or otherwise we |
|
880 | 880 | # cannot retrieve the merge commit. |
|
881 | 881 | shadow_repo = GitRepository(shadow_repository_path) |
|
882 | 882 | merge_commit_id = shadow_repo.branches[pr_branch] |
|
883 | 883 | |
|
884 | 884 | # Set a reference pointing to the merge commit. This reference may |
|
885 | 885 | # be used to easily identify the last successful merge commit in |
|
886 | 886 | # the shadow repository. |
|
887 | 887 | shadow_repo.set_refs('refs/heads/pr-merge', merge_commit_id) |
|
888 | 888 | merge_ref = Reference('branch', 'pr-merge', merge_commit_id) |
|
889 | 889 | except RepositoryError: |
|
890 | 890 | log.exception('Failure when doing local merge on git shadow repo') |
|
891 | 891 | merge_possible = False |
|
892 | 892 | merge_failure_reason = MergeFailureReason.MERGE_FAILED |
|
893 | 893 | |
|
894 | 894 | if merge_possible and not dry_run: |
|
895 | 895 | try: |
|
896 | 896 | shadow_repo._local_push( |
|
897 | 897 | pr_branch, self.path, target_ref.name, enable_hooks=True, |
|
898 | 898 | rc_scm_data=self.config.get('rhodecode', 'RC_SCM_DATA')) |
|
899 | 899 | merge_succeeded = True |
|
900 | 900 | except RepositoryError: |
|
901 | 901 | log.exception( |
|
902 | 902 | 'Failure when doing local push on git shadow repo') |
|
903 | 903 | merge_succeeded = False |
|
904 | 904 | merge_failure_reason = MergeFailureReason.PUSH_FAILED |
|
905 | 905 | else: |
|
906 | 906 | merge_succeeded = False |
|
907 | 907 | |
|
908 | 908 | return MergeResponse( |
|
909 | 909 | merge_possible, merge_succeeded, merge_ref, |
|
910 | 910 | merge_failure_reason) |
|
911 | 911 | |
|
912 | 912 | def _get_shadow_repository_path(self, workspace_id): |
|
913 | 913 | # The name of the shadow repository must start with '.', so it is |
|
914 | 914 | # skipped by 'rhodecode.lib.utils.get_filesystem_repos'. |
|
915 | 915 | return os.path.join( |
|
916 | 916 | os.path.dirname(self.path), |
|
917 | 917 | '.__shadow_%s_%s' % (os.path.basename(self.path), workspace_id)) |
|
918 | 918 | |
|
919 | 919 | def _maybe_prepare_merge_workspace(self, workspace_id, target_ref): |
|
920 | 920 | shadow_repository_path = self._get_shadow_repository_path(workspace_id) |
|
921 | 921 | if not os.path.exists(shadow_repository_path): |
|
922 | 922 | self._local_clone(shadow_repository_path, target_ref.name) |
|
923 | 923 | |
|
924 | 924 | return shadow_repository_path |
|
925 | 925 | |
|
926 | 926 | def cleanup_merge_workspace(self, workspace_id): |
|
927 | 927 | shadow_repository_path = self._get_shadow_repository_path(workspace_id) |
|
928 | 928 | shutil.rmtree(shadow_repository_path, ignore_errors=True) |
@@ -1,798 +1,803 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2014-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | HG repository module |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | import logging |
|
26 | 26 | import binascii |
|
27 | 27 | import os |
|
28 | 28 | import shutil |
|
29 | 29 | import urllib |
|
30 | 30 | |
|
31 | 31 | from zope.cachedescriptors.property import Lazy as LazyProperty |
|
32 | 32 | |
|
33 | 33 | from rhodecode.lib.compat import OrderedDict |
|
34 | 34 | from rhodecode.lib.datelib import ( |
|
35 | 35 | date_to_timestamp_plus_offset, utcdate_fromtimestamp, makedate, |
|
36 | 36 | date_astimestamp) |
|
37 | 37 | from rhodecode.lib.utils import safe_unicode, safe_str |
|
38 | 38 | from rhodecode.lib.vcs import connection |
|
39 | 39 | from rhodecode.lib.vcs.backends.base import ( |
|
40 | 40 | BaseRepository, CollectionGenerator, Config, MergeResponse, |
|
41 | 41 | MergeFailureReason, Reference) |
|
42 | 42 | from rhodecode.lib.vcs.backends.hg.commit import MercurialCommit |
|
43 | 43 | from rhodecode.lib.vcs.backends.hg.diff import MercurialDiff |
|
44 | 44 | from rhodecode.lib.vcs.backends.hg.inmemory import MercurialInMemoryCommit |
|
45 | 45 | from rhodecode.lib.vcs.exceptions import ( |
|
46 | 46 | EmptyRepositoryError, RepositoryError, TagAlreadyExistError, |
|
47 | 47 | TagDoesNotExistError, CommitDoesNotExistError) |
|
48 | 48 | |
|
49 | 49 | hexlify = binascii.hexlify |
|
50 | 50 | nullid = "\0" * 20 |
|
51 | 51 | |
|
52 | 52 | log = logging.getLogger(__name__) |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | class MercurialRepository(BaseRepository): |
|
56 | 56 | """ |
|
57 | 57 | Mercurial repository backend |
|
58 | 58 | """ |
|
59 | 59 | DEFAULT_BRANCH_NAME = 'default' |
|
60 | 60 | |
|
61 | 61 | def __init__(self, repo_path, config=None, create=False, src_url=None, |
|
62 | 62 | update_after_clone=False, with_wire=None): |
|
63 | 63 | """ |
|
64 | 64 | Raises RepositoryError if repository could not be find at the given |
|
65 | 65 | ``repo_path``. |
|
66 | 66 | |
|
67 | 67 | :param repo_path: local path of the repository |
|
68 | 68 | :param config: config object containing the repo configuration |
|
69 | 69 | :param create=False: if set to True, would try to create repository if |
|
70 | 70 | it does not exist rather than raising exception |
|
71 | 71 | :param src_url=None: would try to clone repository from given location |
|
72 | 72 | :param update_after_clone=False: sets update of working copy after |
|
73 | 73 | making a clone |
|
74 | 74 | """ |
|
75 | 75 | self.path = safe_str(os.path.abspath(repo_path)) |
|
76 | 76 | self.config = config if config else Config() |
|
77 | 77 | self._remote = connection.Hg( |
|
78 | 78 | self.path, self.config, with_wire=with_wire) |
|
79 | 79 | |
|
80 | 80 | self._init_repo(create, src_url, update_after_clone) |
|
81 | 81 | |
|
82 | 82 | # caches |
|
83 | 83 | self._commit_ids = {} |
|
84 | 84 | |
|
85 | 85 | @LazyProperty |
|
86 | 86 | def commit_ids(self): |
|
87 | 87 | """ |
|
88 | 88 | Returns list of commit ids, in ascending order. Being lazy |
|
89 | 89 | attribute allows external tools to inject shas from cache. |
|
90 | 90 | """ |
|
91 | 91 | commit_ids = self._get_all_commit_ids() |
|
92 | 92 | self._rebuild_cache(commit_ids) |
|
93 | 93 | return commit_ids |
|
94 | 94 | |
|
95 | 95 | def _rebuild_cache(self, commit_ids): |
|
96 | 96 | self._commit_ids = dict((commit_id, index) |
|
97 | 97 | for index, commit_id in enumerate(commit_ids)) |
|
98 | 98 | |
|
99 | 99 | @LazyProperty |
|
100 | 100 | def branches(self): |
|
101 | 101 | return self._get_branches() |
|
102 | 102 | |
|
103 | 103 | @LazyProperty |
|
104 | 104 | def branches_closed(self): |
|
105 | 105 | return self._get_branches(active=False, closed=True) |
|
106 | 106 | |
|
107 | 107 | @LazyProperty |
|
108 | 108 | def branches_all(self): |
|
109 | 109 | all_branches = {} |
|
110 | 110 | all_branches.update(self.branches) |
|
111 | 111 | all_branches.update(self.branches_closed) |
|
112 | 112 | return all_branches |
|
113 | 113 | |
|
114 | 114 | def _get_branches(self, active=True, closed=False): |
|
115 | 115 | """ |
|
116 | 116 | Gets branches for this repository |
|
117 | 117 | Returns only not closed active branches by default |
|
118 | 118 | |
|
119 | 119 | :param active: return also active branches |
|
120 | 120 | :param closed: return also closed branches |
|
121 | 121 | |
|
122 | 122 | """ |
|
123 | 123 | if self.is_empty(): |
|
124 | 124 | return {} |
|
125 | 125 | |
|
126 | 126 | def get_name(ctx): |
|
127 | 127 | return ctx[0] |
|
128 | 128 | |
|
129 | 129 | _branches = [(safe_unicode(n), hexlify(h),) for n, h in |
|
130 | 130 | self._remote.branches(active, closed).items()] |
|
131 | 131 | |
|
132 | 132 | return OrderedDict(sorted(_branches, key=get_name, reverse=False)) |
|
133 | 133 | |
|
134 | 134 | @LazyProperty |
|
135 | 135 | def tags(self): |
|
136 | 136 | """ |
|
137 | 137 | Gets tags for this repository |
|
138 | 138 | """ |
|
139 | 139 | return self._get_tags() |
|
140 | 140 | |
|
141 | 141 | def _get_tags(self): |
|
142 | 142 | if self.is_empty(): |
|
143 | 143 | return {} |
|
144 | 144 | |
|
145 | 145 | def get_name(ctx): |
|
146 | 146 | return ctx[0] |
|
147 | 147 | |
|
148 | 148 | _tags = [(safe_unicode(n), hexlify(h),) for n, h in |
|
149 | 149 | self._remote.tags().items()] |
|
150 | 150 | |
|
151 | 151 | return OrderedDict(sorted(_tags, key=get_name, reverse=True)) |
|
152 | 152 | |
|
153 | 153 | def tag(self, name, user, commit_id=None, message=None, date=None, |
|
154 | 154 | **kwargs): |
|
155 | 155 | """ |
|
156 | 156 | Creates and returns a tag for the given ``commit_id``. |
|
157 | 157 | |
|
158 | 158 | :param name: name for new tag |
|
159 | 159 | :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>" |
|
160 | 160 | :param commit_id: commit id for which new tag would be created |
|
161 | 161 | :param message: message of the tag's commit |
|
162 | 162 | :param date: date of tag's commit |
|
163 | 163 | |
|
164 | 164 | :raises TagAlreadyExistError: if tag with same name already exists |
|
165 | 165 | """ |
|
166 | 166 | if name in self.tags: |
|
167 | 167 | raise TagAlreadyExistError("Tag %s already exists" % name) |
|
168 | 168 | commit = self.get_commit(commit_id=commit_id) |
|
169 | 169 | local = kwargs.setdefault('local', False) |
|
170 | 170 | |
|
171 | 171 | if message is None: |
|
172 | 172 | message = "Added tag %s for commit %s" % (name, commit.short_id) |
|
173 | 173 | |
|
174 | 174 | date, tz = date_to_timestamp_plus_offset(date) |
|
175 | 175 | |
|
176 | 176 | self._remote.tag( |
|
177 | 177 | name, commit.raw_id, message, local, user, date, tz) |
|
178 | 178 | self._remote.invalidate_vcs_cache() |
|
179 | 179 | |
|
180 | 180 | # Reinitialize tags |
|
181 | 181 | self.tags = self._get_tags() |
|
182 | 182 | tag_id = self.tags[name] |
|
183 | 183 | |
|
184 | 184 | return self.get_commit(commit_id=tag_id) |
|
185 | 185 | |
|
186 | 186 | def remove_tag(self, name, user, message=None, date=None): |
|
187 | 187 | """ |
|
188 | 188 | Removes tag with the given `name`. |
|
189 | 189 | |
|
190 | 190 | :param name: name of the tag to be removed |
|
191 | 191 | :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>" |
|
192 | 192 | :param message: message of the tag's removal commit |
|
193 | 193 | :param date: date of tag's removal commit |
|
194 | 194 | |
|
195 | 195 | :raises TagDoesNotExistError: if tag with given name does not exists |
|
196 | 196 | """ |
|
197 | 197 | if name not in self.tags: |
|
198 | 198 | raise TagDoesNotExistError("Tag %s does not exist" % name) |
|
199 | 199 | if message is None: |
|
200 | 200 | message = "Removed tag %s" % name |
|
201 | 201 | local = False |
|
202 | 202 | |
|
203 | 203 | date, tz = date_to_timestamp_plus_offset(date) |
|
204 | 204 | |
|
205 | 205 | self._remote.tag(name, nullid, message, local, user, date, tz) |
|
206 | 206 | self._remote.invalidate_vcs_cache() |
|
207 | 207 | self.tags = self._get_tags() |
|
208 | 208 | |
|
209 | 209 | @LazyProperty |
|
210 | 210 | def bookmarks(self): |
|
211 | 211 | """ |
|
212 | 212 | Gets bookmarks for this repository |
|
213 | 213 | """ |
|
214 | 214 | return self._get_bookmarks() |
|
215 | 215 | |
|
216 | 216 | def _get_bookmarks(self): |
|
217 | 217 | if self.is_empty(): |
|
218 | 218 | return {} |
|
219 | 219 | |
|
220 | 220 | def get_name(ctx): |
|
221 | 221 | return ctx[0] |
|
222 | 222 | |
|
223 | 223 | _bookmarks = [ |
|
224 | 224 | (safe_unicode(n), hexlify(h)) for n, h in |
|
225 | 225 | self._remote.bookmarks().items()] |
|
226 | 226 | |
|
227 | 227 | return OrderedDict(sorted(_bookmarks, key=get_name)) |
|
228 | 228 | |
|
229 | 229 | def _get_all_commit_ids(self): |
|
230 | 230 | return self._remote.get_all_commit_ids('visible') |
|
231 | 231 | |
|
232 | 232 | def get_diff( |
|
233 | 233 | self, commit1, commit2, path='', ignore_whitespace=False, |
|
234 | 234 | context=3, path1=None): |
|
235 | 235 | """ |
|
236 | 236 | Returns (git like) *diff*, as plain text. Shows changes introduced by |
|
237 | 237 | `commit2` since `commit1`. |
|
238 | 238 | |
|
239 | 239 | :param commit1: Entry point from which diff is shown. Can be |
|
240 | 240 | ``self.EMPTY_COMMIT`` - in this case, patch showing all |
|
241 | 241 | the changes since empty state of the repository until `commit2` |
|
242 | 242 | :param commit2: Until which commit changes should be shown. |
|
243 | 243 | :param ignore_whitespace: If set to ``True``, would not show whitespace |
|
244 | 244 | changes. Defaults to ``False``. |
|
245 | 245 | :param context: How many lines before/after changed lines should be |
|
246 | 246 | shown. Defaults to ``3``. |
|
247 | 247 | """ |
|
248 | 248 | self._validate_diff_commits(commit1, commit2) |
|
249 | 249 | if path1 is not None and path1 != path: |
|
250 | 250 | raise ValueError("Diff of two different paths not supported.") |
|
251 | 251 | |
|
252 | 252 | if path: |
|
253 | 253 | file_filter = [self.path, path] |
|
254 | 254 | else: |
|
255 | 255 | file_filter = None |
|
256 | 256 | |
|
257 | 257 | diff = self._remote.diff( |
|
258 | 258 | commit1.raw_id, commit2.raw_id, file_filter=file_filter, |
|
259 | 259 | opt_git=True, opt_ignorews=ignore_whitespace, |
|
260 | 260 | context=context) |
|
261 | 261 | return MercurialDiff(diff) |
|
262 | 262 | |
|
263 | 263 | def strip(self, commit_id, branch=None): |
|
264 | 264 | self._remote.strip(commit_id, update=False, backup="none") |
|
265 | 265 | |
|
266 | 266 | self._remote.invalidate_vcs_cache() |
|
267 | 267 | self.commit_ids = self._get_all_commit_ids() |
|
268 | 268 | self._rebuild_cache(self.commit_ids) |
|
269 | 269 | |
|
270 | 270 | def get_common_ancestor(self, commit_id1, commit_id2, repo2): |
|
271 | 271 | if commit_id1 == commit_id2: |
|
272 | 272 | return commit_id1 |
|
273 | 273 | |
|
274 | 274 | ancestors = self._remote.revs_from_revspec( |
|
275 | 275 | "ancestor(id(%s), id(%s))", commit_id1, commit_id2, |
|
276 | 276 | other_path=repo2.path) |
|
277 | 277 | return repo2[ancestors[0]].raw_id if ancestors else None |
|
278 | 278 | |
|
279 | 279 | def compare(self, commit_id1, commit_id2, repo2, merge, pre_load=None): |
|
280 | 280 | if commit_id1 == commit_id2: |
|
281 | 281 | commits = [] |
|
282 | 282 | else: |
|
283 | 283 | if merge: |
|
284 | 284 | indexes = self._remote.revs_from_revspec( |
|
285 | 285 | "ancestors(id(%s)) - ancestors(id(%s)) - id(%s)", |
|
286 | 286 | commit_id2, commit_id1, commit_id1, other_path=repo2.path) |
|
287 | 287 | else: |
|
288 | 288 | indexes = self._remote.revs_from_revspec( |
|
289 | 289 | "id(%s)..id(%s) - id(%s)", commit_id1, commit_id2, |
|
290 | 290 | commit_id1, other_path=repo2.path) |
|
291 | 291 | |
|
292 | 292 | commits = [repo2.get_commit(commit_idx=idx, pre_load=pre_load) |
|
293 | 293 | for idx in indexes] |
|
294 | 294 | |
|
295 | 295 | return commits |
|
296 | 296 | |
|
297 | 297 | @staticmethod |
|
298 | 298 | def check_url(url, config): |
|
299 | 299 | """ |
|
300 | 300 | Function will check given url and try to verify if it's a valid |
|
301 | 301 | link. Sometimes it may happened that mercurial will issue basic |
|
302 | 302 | auth request that can cause whole API to hang when used from python |
|
303 | 303 | or other external calls. |
|
304 | 304 | |
|
305 | 305 | On failures it'll raise urllib2.HTTPError, exception is also thrown |
|
306 | 306 | when the return code is non 200 |
|
307 | 307 | """ |
|
308 | 308 | # check first if it's not an local url |
|
309 | 309 | if os.path.isdir(url) or url.startswith('file:'): |
|
310 | 310 | return True |
|
311 | 311 | |
|
312 | 312 | # Request the _remote to verify the url |
|
313 | 313 | return connection.Hg.check_url(url, config.serialize()) |
|
314 | 314 | |
|
315 | 315 | @staticmethod |
|
316 | 316 | def is_valid_repository(path): |
|
317 | 317 | return os.path.isdir(os.path.join(path, '.hg')) |
|
318 | 318 | |
|
319 | 319 | def _init_repo(self, create, src_url=None, update_after_clone=False): |
|
320 | 320 | """ |
|
321 | 321 | Function will check for mercurial repository in given path. If there |
|
322 | 322 | is no repository in that path it will raise an exception unless |
|
323 | 323 | `create` parameter is set to True - in that case repository would |
|
324 | 324 | be created. |
|
325 | 325 | |
|
326 | 326 | If `src_url` is given, would try to clone repository from the |
|
327 | 327 | location at given clone_point. Additionally it'll make update to |
|
328 | 328 | working copy accordingly to `update_after_clone` flag. |
|
329 | 329 | """ |
|
330 | 330 | if create and os.path.exists(self.path): |
|
331 | 331 | raise RepositoryError( |
|
332 | 332 | "Cannot create repository at %s, location already exist" |
|
333 | 333 | % self.path) |
|
334 | 334 | |
|
335 | 335 | if src_url: |
|
336 | 336 | url = str(self._get_url(src_url)) |
|
337 | 337 | MercurialRepository.check_url(url, self.config) |
|
338 | 338 | |
|
339 | 339 | self._remote.clone(url, self.path, update_after_clone) |
|
340 | 340 | |
|
341 | 341 | # Don't try to create if we've already cloned repo |
|
342 | 342 | create = False |
|
343 | 343 | |
|
344 | 344 | if create: |
|
345 | 345 | os.makedirs(self.path, mode=0755) |
|
346 | 346 | |
|
347 | 347 | self._remote.localrepository(create) |
|
348 | 348 | |
|
349 | 349 | @LazyProperty |
|
350 | 350 | def in_memory_commit(self): |
|
351 | 351 | return MercurialInMemoryCommit(self) |
|
352 | 352 | |
|
353 | 353 | @LazyProperty |
|
354 | 354 | def description(self): |
|
355 | 355 | description = self._remote.get_config_value( |
|
356 | 356 | 'web', 'description', untrusted=True) |
|
357 | 357 | return safe_unicode(description or self.DEFAULT_DESCRIPTION) |
|
358 | 358 | |
|
359 | 359 | @LazyProperty |
|
360 | 360 | def contact(self): |
|
361 | 361 | contact = ( |
|
362 | 362 | self._remote.get_config_value("web", "contact") or |
|
363 | 363 | self._remote.get_config_value("ui", "username")) |
|
364 | 364 | return safe_unicode(contact or self.DEFAULT_CONTACT) |
|
365 | 365 | |
|
366 | 366 | @LazyProperty |
|
367 | 367 | def last_change(self): |
|
368 | 368 | """ |
|
369 | 369 | Returns last change made on this repository as |
|
370 | 370 | `datetime.datetime` object |
|
371 | 371 | """ |
|
372 | 372 | return utcdate_fromtimestamp(self._get_mtime(), makedate()[1]) |
|
373 | 373 | |
|
374 | 374 | def _get_mtime(self): |
|
375 | 375 | try: |
|
376 | 376 | return date_astimestamp(self.get_commit().date) |
|
377 | 377 | except RepositoryError: |
|
378 | 378 | # fallback to filesystem |
|
379 | 379 | cl_path = os.path.join(self.path, '.hg', "00changelog.i") |
|
380 | 380 | st_path = os.path.join(self.path, '.hg', "store") |
|
381 | 381 | if os.path.exists(cl_path): |
|
382 | 382 | return os.stat(cl_path).st_mtime |
|
383 | 383 | else: |
|
384 | 384 | return os.stat(st_path).st_mtime |
|
385 | 385 | |
|
386 | 386 | def _sanitize_commit_idx(self, idx): |
|
387 | 387 | # Note: Mercurial has ``int(-1)`` reserved as not existing id_or_idx |
|
388 | 388 | # number. A `long` is treated in the correct way though. So we convert |
|
389 | 389 | # `int` to `long` here to make sure it is handled correctly. |
|
390 | 390 | if isinstance(idx, int): |
|
391 | 391 | return long(idx) |
|
392 | 392 | return idx |
|
393 | 393 | |
|
394 | 394 | def _get_url(self, url): |
|
395 | 395 | """ |
|
396 | 396 | Returns normalized url. If schema is not given, would fall |
|
397 | 397 | to filesystem |
|
398 | 398 | (``file:///``) schema. |
|
399 | 399 | """ |
|
400 | 400 | url = url.encode('utf8') |
|
401 | 401 | if url != 'default' and '://' not in url: |
|
402 | 402 | url = "file:" + urllib.pathname2url(url) |
|
403 | 403 | return url |
|
404 | 404 | |
|
405 | 405 | def get_hook_location(self): |
|
406 | 406 | """ |
|
407 | 407 | returns absolute path to location where hooks are stored |
|
408 | 408 | """ |
|
409 | 409 | return os.path.join(self.path, '.hg', '.hgrc') |
|
410 | 410 | |
|
411 | 411 | def get_commit(self, commit_id=None, commit_idx=None, pre_load=None): |
|
412 | 412 | """ |
|
413 | 413 | Returns ``MercurialCommit`` object representing repository's |
|
414 | 414 | commit at the given `commit_id` or `commit_idx`. |
|
415 | 415 | """ |
|
416 | 416 | if self.is_empty(): |
|
417 | 417 | raise EmptyRepositoryError("There are no commits yet") |
|
418 | 418 | |
|
419 | 419 | if commit_id is not None: |
|
420 | 420 | self._validate_commit_id(commit_id) |
|
421 | 421 | try: |
|
422 | 422 | idx = self._commit_ids[commit_id] |
|
423 | 423 | return MercurialCommit(self, commit_id, idx, pre_load=pre_load) |
|
424 | 424 | except KeyError: |
|
425 | 425 | pass |
|
426 | 426 | elif commit_idx is not None: |
|
427 | 427 | self._validate_commit_idx(commit_idx) |
|
428 | 428 | commit_idx = self._sanitize_commit_idx(commit_idx) |
|
429 | 429 | try: |
|
430 | 430 | id_ = self.commit_ids[commit_idx] |
|
431 | 431 | if commit_idx < 0: |
|
432 | 432 | commit_idx += len(self.commit_ids) |
|
433 | 433 | return MercurialCommit( |
|
434 | 434 | self, id_, commit_idx, pre_load=pre_load) |
|
435 | 435 | except IndexError: |
|
436 | 436 | commit_id = commit_idx |
|
437 | 437 | else: |
|
438 | 438 | commit_id = "tip" |
|
439 | 439 | |
|
440 | 440 | # TODO Paris: Ugly hack to "serialize" long for msgpack |
|
441 | 441 | if isinstance(commit_id, long): |
|
442 | 442 | commit_id = float(commit_id) |
|
443 | 443 | |
|
444 | 444 | if isinstance(commit_id, unicode): |
|
445 | 445 | commit_id = safe_str(commit_id) |
|
446 | 446 | |
|
447 | 447 | raw_id, idx = self._remote.lookup(commit_id, both=True) |
|
448 | 448 | |
|
449 | 449 | return MercurialCommit(self, raw_id, idx, pre_load=pre_load) |
|
450 | 450 | |
|
451 | 451 | def get_commits( |
|
452 | 452 | self, start_id=None, end_id=None, start_date=None, end_date=None, |
|
453 | 453 | branch_name=None, pre_load=None): |
|
454 | 454 | """ |
|
455 | 455 | Returns generator of ``MercurialCommit`` objects from start to end |
|
456 | 456 | (both are inclusive) |
|
457 | 457 | |
|
458 | 458 | :param start_id: None, str(commit_id) |
|
459 | 459 | :param end_id: None, str(commit_id) |
|
460 | 460 | :param start_date: if specified, commits with commit date less than |
|
461 | 461 | ``start_date`` would be filtered out from returned set |
|
462 | 462 | :param end_date: if specified, commits with commit date greater than |
|
463 | 463 | ``end_date`` would be filtered out from returned set |
|
464 | 464 | :param branch_name: if specified, commits not reachable from given |
|
465 | 465 | branch would be filtered out from returned set |
|
466 | 466 | |
|
467 | 467 | :raise BranchDoesNotExistError: If given ``branch_name`` does not |
|
468 | 468 | exist. |
|
469 | 469 | :raise CommitDoesNotExistError: If commit for given ``start`` or |
|
470 | 470 | ``end`` could not be found. |
|
471 | 471 | """ |
|
472 | 472 | # actually we should check now if it's not an empty repo |
|
473 | 473 | branch_ancestors = False |
|
474 | 474 | if self.is_empty(): |
|
475 | 475 | raise EmptyRepositoryError("There are no commits yet") |
|
476 | 476 | self._validate_branch_name(branch_name) |
|
477 | 477 | |
|
478 | 478 | if start_id is not None: |
|
479 | 479 | self._validate_commit_id(start_id) |
|
480 | 480 | c_start = self.get_commit(commit_id=start_id) |
|
481 | 481 | start_pos = self._commit_ids[c_start.raw_id] |
|
482 | 482 | else: |
|
483 | 483 | start_pos = None |
|
484 | 484 | |
|
485 | 485 | if end_id is not None: |
|
486 | 486 | self._validate_commit_id(end_id) |
|
487 | 487 | c_end = self.get_commit(commit_id=end_id) |
|
488 | 488 | end_pos = max(0, self._commit_ids[c_end.raw_id]) |
|
489 | 489 | else: |
|
490 | 490 | end_pos = None |
|
491 | 491 | |
|
492 | 492 | if None not in [start_id, end_id] and start_pos > end_pos: |
|
493 | 493 | raise RepositoryError( |
|
494 | 494 | "Start commit '%s' cannot be after end commit '%s'" % |
|
495 | 495 | (start_id, end_id)) |
|
496 | 496 | |
|
497 | 497 | if end_pos is not None: |
|
498 | 498 | end_pos += 1 |
|
499 | 499 | |
|
500 | 500 | commit_filter = [] |
|
501 | 501 | if branch_name and not branch_ancestors: |
|
502 | 502 | commit_filter.append('branch("%s")' % branch_name) |
|
503 | 503 | elif branch_name and branch_ancestors: |
|
504 | 504 | commit_filter.append('ancestors(branch("%s"))' % branch_name) |
|
505 | 505 | if start_date and not end_date: |
|
506 | 506 | commit_filter.append('date(">%s")' % start_date) |
|
507 | 507 | if end_date and not start_date: |
|
508 | 508 | commit_filter.append('date("<%s")' % end_date) |
|
509 | 509 | if start_date and end_date: |
|
510 | 510 | commit_filter.append( |
|
511 | 511 | 'date(">%s") and date("<%s")' % (start_date, end_date)) |
|
512 | 512 | |
|
513 | 513 | # TODO: johbo: Figure out a simpler way for this solution |
|
514 | 514 | collection_generator = CollectionGenerator |
|
515 | 515 | if commit_filter: |
|
516 | 516 | commit_filter = map(safe_str, commit_filter) |
|
517 | 517 | revisions = self._remote.rev_range(commit_filter) |
|
518 | 518 | collection_generator = MercurialIndexBasedCollectionGenerator |
|
519 | 519 | else: |
|
520 | 520 | revisions = self.commit_ids |
|
521 | 521 | |
|
522 | 522 | if start_pos or end_pos: |
|
523 | 523 | revisions = revisions[start_pos:end_pos] |
|
524 | 524 | |
|
525 | 525 | return collection_generator(self, revisions, pre_load=pre_load) |
|
526 | 526 | |
|
527 | 527 | def pull(self, url, commit_ids=None): |
|
528 | 528 | """ |
|
529 | 529 | Tries to pull changes from external location. |
|
530 | 530 | |
|
531 | 531 | :param commit_ids: Optional. Can be set to a list of commit ids |
|
532 | 532 | which shall be pulled from the other repository. |
|
533 | 533 | """ |
|
534 | 534 | url = self._get_url(url) |
|
535 | 535 | self._remote.pull(url, commit_ids=commit_ids) |
|
536 | 536 | self._remote.invalidate_vcs_cache() |
|
537 | 537 | |
|
538 | 538 | def _local_clone(self, clone_path): |
|
539 | 539 | """ |
|
540 | 540 | Create a local clone of the current repo. |
|
541 | 541 | """ |
|
542 | 542 | self._remote.clone(self.path, clone_path, update_after_clone=True, |
|
543 | 543 | hooks=False) |
|
544 | 544 | |
|
545 | 545 | def _update(self, revision, clean=False): |
|
546 | 546 | """ |
|
547 | 547 | Update the working copty to the specified revision. |
|
548 | 548 | """ |
|
549 | 549 | self._remote.update(revision, clean=clean) |
|
550 | 550 | |
|
551 | 551 | def _identify(self): |
|
552 | 552 | """ |
|
553 | 553 | Return the current state of the working directory. |
|
554 | 554 | """ |
|
555 | 555 | return self._remote.identify().strip().rstrip('+') |
|
556 | 556 | |
|
557 | 557 | def _heads(self, branch=None): |
|
558 | 558 | """ |
|
559 | 559 | Return the commit ids of the repository heads. |
|
560 | 560 | """ |
|
561 | 561 | return self._remote.heads(branch=branch).strip().split(' ') |
|
562 | 562 | |
|
563 | 563 | def _ancestor(self, revision1, revision2): |
|
564 | 564 | """ |
|
565 | 565 | Return the common ancestor of the two revisions. |
|
566 | 566 | """ |
|
567 | 567 | return self._remote.ancestor( |
|
568 | 568 | revision1, revision2).strip().split(':')[-1] |
|
569 | 569 | |
|
570 | 570 | def _local_push( |
|
571 | 571 | self, revision, repository_path, push_branches=False, |
|
572 | 572 | enable_hooks=False): |
|
573 | 573 | """ |
|
574 | 574 | Push the given revision to the specified repository. |
|
575 | 575 | |
|
576 | 576 | :param push_branches: allow to create branches in the target repo. |
|
577 | 577 | """ |
|
578 | 578 | self._remote.push( |
|
579 | 579 | [revision], repository_path, hooks=enable_hooks, |
|
580 | 580 | push_branches=push_branches) |
|
581 | 581 | |
|
582 | 582 | def _local_merge(self, target_ref, merge_message, user_name, user_email, |
|
583 | 583 | source_ref, use_rebase=False): |
|
584 | 584 | """ |
|
585 | 585 | Merge the given source_revision into the checked out revision. |
|
586 | 586 | |
|
587 | 587 | Returns the commit id of the merge and a boolean indicating if the |
|
588 | 588 | commit needs to be pushed. |
|
589 | 589 | """ |
|
590 | 590 | self._update(target_ref.commit_id) |
|
591 | 591 | |
|
592 | 592 | ancestor = self._ancestor(target_ref.commit_id, source_ref.commit_id) |
|
593 | 593 | is_the_same_branch = self._is_the_same_branch(target_ref, source_ref) |
|
594 | 594 | |
|
595 | 595 | if ancestor == source_ref.commit_id: |
|
596 | 596 | # Nothing to do, the changes were already integrated |
|
597 | 597 | return target_ref.commit_id, False |
|
598 | 598 | |
|
599 | 599 | elif ancestor == target_ref.commit_id and is_the_same_branch: |
|
600 | 600 | # In this case we should force a commit message |
|
601 | 601 | return source_ref.commit_id, True |
|
602 | 602 | |
|
603 | 603 | if use_rebase: |
|
604 | 604 | try: |
|
605 | 605 | bookmark_name = 'rcbook%s%s' % (source_ref.commit_id, |
|
606 | 606 | target_ref.commit_id) |
|
607 | 607 | self.bookmark(bookmark_name, revision=source_ref.commit_id) |
|
608 | 608 | self._remote.rebase( |
|
609 | 609 | source=source_ref.commit_id, dest=target_ref.commit_id) |
|
610 | 610 | self._remote.invalidate_vcs_cache() |
|
611 | 611 | self._update(bookmark_name) |
|
612 | 612 | return self._identify(), True |
|
613 | 613 | except RepositoryError: |
|
614 | 614 | # The rebase-abort may raise another exception which 'hides' |
|
615 | 615 | # the original one, therefore we log it here. |
|
616 | 616 | log.exception('Error while rebasing shadow repo during merge.') |
|
617 | 617 | |
|
618 | 618 | # Cleanup any rebase leftovers |
|
619 | 619 | self._remote.invalidate_vcs_cache() |
|
620 | 620 | self._remote.rebase(abort=True) |
|
621 | 621 | self._remote.invalidate_vcs_cache() |
|
622 | 622 | self._remote.update(clean=True) |
|
623 | 623 | raise |
|
624 | 624 | else: |
|
625 | 625 | try: |
|
626 | 626 | self._remote.merge(source_ref.commit_id) |
|
627 | 627 | self._remote.invalidate_vcs_cache() |
|
628 | 628 | self._remote.commit( |
|
629 | 629 | message=safe_str(merge_message), |
|
630 | 630 | username=safe_str('%s <%s>' % (user_name, user_email))) |
|
631 | 631 | self._remote.invalidate_vcs_cache() |
|
632 | 632 | return self._identify(), True |
|
633 | 633 | except RepositoryError: |
|
634 | 634 | # Cleanup any merge leftovers |
|
635 | 635 | self._remote.update(clean=True) |
|
636 | 636 | raise |
|
637 | 637 | |
|
638 | 638 | def _is_the_same_branch(self, target_ref, source_ref): |
|
639 | 639 | return ( |
|
640 | 640 | self._get_branch_name(target_ref) == |
|
641 | 641 | self._get_branch_name(source_ref)) |
|
642 | 642 | |
|
643 | 643 | def _get_branch_name(self, ref): |
|
644 | 644 | if ref.type == 'branch': |
|
645 | 645 | return ref.name |
|
646 | 646 | return self._remote.ctx_branch(ref.commit_id) |
|
647 | 647 | |
|
648 | 648 | def _get_shadow_repository_path(self, workspace_id): |
|
649 | 649 | # The name of the shadow repository must start with '.', so it is |
|
650 | 650 | # skipped by 'rhodecode.lib.utils.get_filesystem_repos'. |
|
651 | 651 | return os.path.join( |
|
652 | 652 | os.path.dirname(self.path), |
|
653 | 653 | '.__shadow_%s_%s' % (os.path.basename(self.path), workspace_id)) |
|
654 | 654 | |
|
655 | 655 | def _maybe_prepare_merge_workspace(self, workspace_id, unused_target_ref): |
|
656 | 656 | shadow_repository_path = self._get_shadow_repository_path(workspace_id) |
|
657 | 657 | if not os.path.exists(shadow_repository_path): |
|
658 | 658 | self._local_clone(shadow_repository_path) |
|
659 | 659 | log.debug( |
|
660 | 660 | 'Prepared shadow repository in %s', shadow_repository_path) |
|
661 | 661 | |
|
662 | 662 | return shadow_repository_path |
|
663 | 663 | |
|
664 | 664 | def cleanup_merge_workspace(self, workspace_id): |
|
665 | 665 | shadow_repository_path = self._get_shadow_repository_path(workspace_id) |
|
666 | 666 | shutil.rmtree(shadow_repository_path, ignore_errors=True) |
|
667 | 667 | |
|
668 | 668 | def _merge_repo(self, shadow_repository_path, target_ref, |
|
669 | 669 | source_repo, source_ref, merge_message, |
|
670 | 670 | merger_name, merger_email, dry_run=False, |
|
671 | 671 | use_rebase=False): |
|
672 | 672 | if target_ref.commit_id not in self._heads(): |
|
673 | 673 | return MergeResponse( |
|
674 | 674 | False, False, None, MergeFailureReason.TARGET_IS_NOT_HEAD) |
|
675 | 675 | |
|
676 | if (target_ref.type == 'branch' and | |
|
677 | len(self._heads(target_ref.name)) != 1): | |
|
676 | try: | |
|
677 | if (target_ref.type == 'branch' and | |
|
678 | len(self._heads(target_ref.name)) != 1): | |
|
679 | return MergeResponse( | |
|
680 | False, False, None, | |
|
681 | MergeFailureReason.HG_TARGET_HAS_MULTIPLE_HEADS) | |
|
682 | except CommitDoesNotExistError as e: | |
|
683 | log.exception('Failure when looking up branch heads on hg target') | |
|
678 | 684 | return MergeResponse( |
|
679 | False, False, None, | |
|
680 | MergeFailureReason.HG_TARGET_HAS_MULTIPLE_HEADS) | |
|
685 | False, False, None, MergeFailureReason.MISSING_TARGET_REF) | |
|
681 | 686 | |
|
682 | 687 | shadow_repo = self._get_shadow_instance(shadow_repository_path) |
|
683 | 688 | |
|
684 | 689 | log.debug('Pulling in target reference %s', target_ref) |
|
685 | 690 | self._validate_pull_reference(target_ref) |
|
686 | 691 | shadow_repo._local_pull(self.path, target_ref) |
|
687 | 692 | try: |
|
688 | 693 | log.debug('Pulling in source reference %s', source_ref) |
|
689 | 694 | source_repo._validate_pull_reference(source_ref) |
|
690 | 695 | shadow_repo._local_pull(source_repo.path, source_ref) |
|
691 | 696 | except CommitDoesNotExistError: |
|
692 | 697 | log.exception('Failure when doing local pull on hg shadow repo') |
|
693 | 698 | return MergeResponse( |
|
694 |
False, False, None, MergeFailureReason.MISSING_ |
|
|
699 | False, False, None, MergeFailureReason.MISSING_SOURCE_REF) | |
|
695 | 700 | |
|
696 | 701 | merge_ref = None |
|
697 | 702 | merge_failure_reason = MergeFailureReason.NONE |
|
698 | 703 | |
|
699 | 704 | try: |
|
700 | 705 | merge_commit_id, needs_push = shadow_repo._local_merge( |
|
701 | 706 | target_ref, merge_message, merger_name, merger_email, |
|
702 | 707 | source_ref, use_rebase=use_rebase) |
|
703 | 708 | merge_possible = True |
|
704 | 709 | |
|
705 | 710 | # Set a bookmark pointing to the merge commit. This bookmark may be |
|
706 | 711 | # used to easily identify the last successful merge commit in the |
|
707 | 712 | # shadow repository. |
|
708 | 713 | shadow_repo.bookmark('pr-merge', revision=merge_commit_id) |
|
709 | 714 | merge_ref = Reference('book', 'pr-merge', merge_commit_id) |
|
710 | 715 | except RepositoryError: |
|
711 | 716 | log.exception('Failure when doing local merge on hg shadow repo') |
|
712 | 717 | merge_possible = False |
|
713 | 718 | merge_failure_reason = MergeFailureReason.MERGE_FAILED |
|
714 | 719 | |
|
715 | 720 | if merge_possible and not dry_run: |
|
716 | 721 | if needs_push: |
|
717 | 722 | # In case the target is a bookmark, update it, so after pushing |
|
718 | 723 | # the bookmarks is also updated in the target. |
|
719 | 724 | if target_ref.type == 'book': |
|
720 | 725 | shadow_repo.bookmark( |
|
721 | 726 | target_ref.name, revision=merge_commit_id) |
|
722 | 727 | |
|
723 | 728 | try: |
|
724 | 729 | shadow_repo_with_hooks = self._get_shadow_instance( |
|
725 | 730 | shadow_repository_path, |
|
726 | 731 | enable_hooks=True) |
|
727 | 732 | # Note: the push_branches option will push any new branch |
|
728 | 733 | # defined in the source repository to the target. This may |
|
729 | 734 | # be dangerous as branches are permanent in Mercurial. |
|
730 | 735 | # This feature was requested in issue #441. |
|
731 | 736 | shadow_repo_with_hooks._local_push( |
|
732 | 737 | merge_commit_id, self.path, push_branches=True, |
|
733 | 738 | enable_hooks=True) |
|
734 | 739 | merge_succeeded = True |
|
735 | 740 | except RepositoryError: |
|
736 | 741 | log.exception( |
|
737 | 742 | 'Failure when doing local push from the shadow ' |
|
738 | 743 | 'repository to the target repository.') |
|
739 | 744 | merge_succeeded = False |
|
740 | 745 | merge_failure_reason = MergeFailureReason.PUSH_FAILED |
|
741 | 746 | else: |
|
742 | 747 | merge_succeeded = True |
|
743 | 748 | else: |
|
744 | 749 | merge_succeeded = False |
|
745 | 750 | |
|
746 | 751 | return MergeResponse( |
|
747 | 752 | merge_possible, merge_succeeded, merge_ref, merge_failure_reason) |
|
748 | 753 | |
|
749 | 754 | def _get_shadow_instance( |
|
750 | 755 | self, shadow_repository_path, enable_hooks=False): |
|
751 | 756 | config = self.config.copy() |
|
752 | 757 | if not enable_hooks: |
|
753 | 758 | config.clear_section('hooks') |
|
754 | 759 | return MercurialRepository(shadow_repository_path, config) |
|
755 | 760 | |
|
756 | 761 | def _validate_pull_reference(self, reference): |
|
757 | 762 | if not (reference.name in self.bookmarks or |
|
758 | 763 | reference.name in self.branches or |
|
759 | 764 | self.get_commit(reference.commit_id)): |
|
760 | 765 | raise CommitDoesNotExistError( |
|
761 | 766 | 'Unknown branch, bookmark or commit id') |
|
762 | 767 | |
|
763 | 768 | def _local_pull(self, repository_path, reference): |
|
764 | 769 | """ |
|
765 | 770 | Fetch a branch, bookmark or commit from a local repository. |
|
766 | 771 | """ |
|
767 | 772 | repository_path = os.path.abspath(repository_path) |
|
768 | 773 | if repository_path == self.path: |
|
769 | 774 | raise ValueError('Cannot pull from the same repository') |
|
770 | 775 | |
|
771 | 776 | reference_type_to_option_name = { |
|
772 | 777 | 'book': 'bookmark', |
|
773 | 778 | 'branch': 'branch', |
|
774 | 779 | } |
|
775 | 780 | option_name = reference_type_to_option_name.get( |
|
776 | 781 | reference.type, 'revision') |
|
777 | 782 | |
|
778 | 783 | if option_name == 'revision': |
|
779 | 784 | ref = reference.commit_id |
|
780 | 785 | else: |
|
781 | 786 | ref = reference.name |
|
782 | 787 | |
|
783 | 788 | options = {option_name: [ref]} |
|
784 | 789 | self._remote.pull_cmd(repository_path, hooks=False, **options) |
|
785 | 790 | self._remote.invalidate_vcs_cache() |
|
786 | 791 | |
|
787 | 792 | def bookmark(self, bookmark, revision=None): |
|
788 | 793 | if isinstance(bookmark, unicode): |
|
789 | 794 | bookmark = safe_str(bookmark) |
|
790 | 795 | self._remote.bookmark(bookmark, revision=revision) |
|
791 | 796 | self._remote.invalidate_vcs_cache() |
|
792 | 797 | |
|
793 | 798 | |
|
794 | 799 | class MercurialIndexBasedCollectionGenerator(CollectionGenerator): |
|
795 | 800 | |
|
796 | 801 | def _commit_factory(self, commit_id): |
|
797 | 802 | return self.repo.get_commit( |
|
798 | 803 | commit_idx=commit_id, pre_load=self.pre_load) |
@@ -1,532 +1,532 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import datetime |
|
22 | 22 | from urllib2 import URLError |
|
23 | 23 | |
|
24 | 24 | import mock |
|
25 | 25 | import pytest |
|
26 | 26 | |
|
27 | 27 | from rhodecode.lib.vcs import backends |
|
28 | 28 | from rhodecode.lib.vcs.backends.base import ( |
|
29 | 29 | Config, BaseInMemoryCommit, Reference, MergeResponse, MergeFailureReason) |
|
30 | 30 | from rhodecode.lib.vcs.exceptions import VCSError, RepositoryError |
|
31 | 31 | from rhodecode.lib.vcs.nodes import FileNode |
|
32 | 32 | from rhodecode.tests.vcs.base import BackendTestMixin |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class TestRepositoryBase(BackendTestMixin): |
|
36 | 36 | recreate_repo_per_test = False |
|
37 | 37 | |
|
38 | 38 | def test_init_accepts_unicode_path(self, tmpdir): |
|
39 | 39 | path = unicode(tmpdir.join(u'unicode Γ€')) |
|
40 | 40 | self.Backend(path, create=True) |
|
41 | 41 | |
|
42 | 42 | def test_init_accepts_str_path(self, tmpdir): |
|
43 | 43 | path = str(tmpdir.join('str Γ€')) |
|
44 | 44 | self.Backend(path, create=True) |
|
45 | 45 | |
|
46 | 46 | def test_init_fails_if_path_does_not_exist(self, tmpdir): |
|
47 | 47 | path = unicode(tmpdir.join('i-do-not-exist')) |
|
48 | 48 | with pytest.raises(VCSError): |
|
49 | 49 | self.Backend(path) |
|
50 | 50 | |
|
51 | 51 | def test_init_fails_if_path_is_not_a_valid_repository(self, tmpdir): |
|
52 | 52 | path = unicode(tmpdir.mkdir(u'unicode Γ€')) |
|
53 | 53 | with pytest.raises(VCSError): |
|
54 | 54 | self.Backend(path) |
|
55 | 55 | |
|
56 | 56 | def test_has_commits_attribute(self): |
|
57 | 57 | self.repo.commit_ids |
|
58 | 58 | |
|
59 | 59 | def test_name(self): |
|
60 | 60 | assert self.repo.name.startswith('vcs-test') |
|
61 | 61 | |
|
62 | 62 | @pytest.mark.backends("hg", "git") |
|
63 | 63 | def test_has_default_branch_name(self): |
|
64 | 64 | assert self.repo.DEFAULT_BRANCH_NAME is not None |
|
65 | 65 | |
|
66 | 66 | @pytest.mark.backends("svn") |
|
67 | 67 | def test_has_no_default_branch_name(self): |
|
68 | 68 | assert self.repo.DEFAULT_BRANCH_NAME is None |
|
69 | 69 | |
|
70 | 70 | def test_has_empty_commit(self): |
|
71 | 71 | assert self.repo.EMPTY_COMMIT_ID is not None |
|
72 | 72 | assert self.repo.EMPTY_COMMIT is not None |
|
73 | 73 | |
|
74 | 74 | def test_empty_changeset_is_deprecated(self): |
|
75 | 75 | def get_empty_changeset(repo): |
|
76 | 76 | return repo.EMPTY_CHANGESET |
|
77 | 77 | pytest.deprecated_call(get_empty_changeset, self.repo) |
|
78 | 78 | |
|
79 | 79 | def test_bookmarks(self): |
|
80 | 80 | assert len(self.repo.bookmarks) == 0 |
|
81 | 81 | |
|
82 | 82 | # TODO: Cover two cases: Local repo path, remote URL |
|
83 | 83 | def test_check_url(self): |
|
84 | 84 | config = Config() |
|
85 | 85 | assert self.Backend.check_url(self.repo.path, config) |
|
86 | 86 | |
|
87 | 87 | def test_check_url_invalid(self): |
|
88 | 88 | config = Config() |
|
89 | 89 | with pytest.raises(URLError): |
|
90 | 90 | self.Backend.check_url(self.repo.path + "invalid", config) |
|
91 | 91 | |
|
92 | 92 | def test_get_contact(self): |
|
93 | 93 | self.repo.contact |
|
94 | 94 | |
|
95 | 95 | def test_get_description(self): |
|
96 | 96 | self.repo.description |
|
97 | 97 | |
|
98 | 98 | def test_get_hook_location(self): |
|
99 | 99 | assert len(self.repo.get_hook_location()) != 0 |
|
100 | 100 | |
|
101 | 101 | def test_last_change(self): |
|
102 | 102 | assert self.repo.last_change >= datetime.datetime(2010, 1, 1, 21, 0) |
|
103 | 103 | |
|
104 | 104 | def test_last_change_in_empty_repository(self, vcsbackend): |
|
105 | 105 | delta = datetime.timedelta(seconds=1) |
|
106 | 106 | start = datetime.datetime.now() |
|
107 | 107 | empty_repo = vcsbackend.create_repo() |
|
108 | 108 | now = datetime.datetime.now() |
|
109 | 109 | assert empty_repo.last_change >= start - delta |
|
110 | 110 | assert empty_repo.last_change <= now + delta |
|
111 | 111 | |
|
112 | 112 | def test_repo_equality(self): |
|
113 | 113 | assert self.repo == self.repo |
|
114 | 114 | |
|
115 | 115 | def test_repo_equality_broken_object(self): |
|
116 | 116 | import copy |
|
117 | 117 | _repo = copy.copy(self.repo) |
|
118 | 118 | delattr(_repo, 'path') |
|
119 | 119 | assert self.repo != _repo |
|
120 | 120 | |
|
121 | 121 | def test_repo_equality_other_object(self): |
|
122 | 122 | class dummy(object): |
|
123 | 123 | path = self.repo.path |
|
124 | 124 | assert self.repo != dummy() |
|
125 | 125 | |
|
126 | 126 | def test_get_commit_is_implemented(self): |
|
127 | 127 | self.repo.get_commit() |
|
128 | 128 | |
|
129 | 129 | def test_get_commits_is_implemented(self): |
|
130 | 130 | commit_iter = iter(self.repo.get_commits()) |
|
131 | 131 | commit = next(commit_iter) |
|
132 | 132 | assert commit.idx == 0 |
|
133 | 133 | |
|
134 | 134 | def test_supports_iteration(self): |
|
135 | 135 | repo_iter = iter(self.repo) |
|
136 | 136 | commit = next(repo_iter) |
|
137 | 137 | assert commit.idx == 0 |
|
138 | 138 | |
|
139 | 139 | def test_in_memory_commit(self): |
|
140 | 140 | imc = self.repo.in_memory_commit |
|
141 | 141 | assert isinstance(imc, BaseInMemoryCommit) |
|
142 | 142 | |
|
143 | 143 | @pytest.mark.backends("hg") |
|
144 | 144 | def test__get_url_unicode(self): |
|
145 | 145 | url = u'/home/repos/malmΓΆ' |
|
146 | 146 | assert self.repo._get_url(url) |
|
147 | 147 | |
|
148 | 148 | |
|
149 | 149 | class TestDeprecatedRepositoryAPI(BackendTestMixin): |
|
150 | 150 | recreate_repo_per_test = False |
|
151 | 151 | |
|
152 | 152 | def test_revisions_is_deprecated(self): |
|
153 | 153 | def get_revisions(repo): |
|
154 | 154 | return repo.revisions |
|
155 | 155 | pytest.deprecated_call(get_revisions, self.repo) |
|
156 | 156 | |
|
157 | 157 | def test_get_changeset_is_deprecated(self): |
|
158 | 158 | pytest.deprecated_call(self.repo.get_changeset) |
|
159 | 159 | |
|
160 | 160 | def test_get_changesets_is_deprecated(self): |
|
161 | 161 | pytest.deprecated_call(self.repo.get_changesets) |
|
162 | 162 | |
|
163 | 163 | def test_in_memory_changeset_is_deprecated(self): |
|
164 | 164 | def get_imc(repo): |
|
165 | 165 | return repo.in_memory_changeset |
|
166 | 166 | pytest.deprecated_call(get_imc, self.repo) |
|
167 | 167 | |
|
168 | 168 | |
|
169 | 169 | # TODO: these tests are incomplete, must check the resulting compare result for |
|
170 | 170 | # correcteness |
|
171 | 171 | class TestRepositoryCompare: |
|
172 | 172 | |
|
173 | 173 | @pytest.mark.parametrize('merge', [True, False]) |
|
174 | 174 | def test_compare_commits_of_same_repository(self, vcsbackend, merge): |
|
175 | 175 | target_repo = vcsbackend.create_repo(number_of_commits=5) |
|
176 | 176 | target_repo.compare( |
|
177 | 177 | target_repo[1].raw_id, target_repo[3].raw_id, target_repo, |
|
178 | 178 | merge=merge) |
|
179 | 179 | |
|
180 | 180 | @pytest.mark.xfail_backends('svn') |
|
181 | 181 | @pytest.mark.parametrize('merge', [True, False]) |
|
182 | 182 | def test_compare_cloned_repositories(self, vcsbackend, merge): |
|
183 | 183 | target_repo = vcsbackend.create_repo(number_of_commits=5) |
|
184 | 184 | source_repo = vcsbackend.clone_repo(target_repo) |
|
185 | 185 | assert target_repo != source_repo |
|
186 | 186 | |
|
187 | 187 | vcsbackend.add_file(source_repo, 'newfile', 'somecontent') |
|
188 | 188 | source_commit = source_repo.get_commit() |
|
189 | 189 | |
|
190 | 190 | target_repo.compare( |
|
191 | 191 | target_repo[1].raw_id, source_repo[3].raw_id, source_repo, |
|
192 | 192 | merge=merge) |
|
193 | 193 | |
|
194 | 194 | @pytest.mark.xfail_backends('svn') |
|
195 | 195 | @pytest.mark.parametrize('merge', [True, False]) |
|
196 | 196 | def test_compare_unrelated_repositories(self, vcsbackend, merge): |
|
197 | 197 | orig = vcsbackend.create_repo(number_of_commits=5) |
|
198 | 198 | unrelated = vcsbackend.create_repo(number_of_commits=5) |
|
199 | 199 | assert orig != unrelated |
|
200 | 200 | |
|
201 | 201 | orig.compare( |
|
202 | 202 | orig[1].raw_id, unrelated[3].raw_id, unrelated, merge=merge) |
|
203 | 203 | |
|
204 | 204 | |
|
205 | 205 | class TestRepositoryGetCommonAncestor: |
|
206 | 206 | |
|
207 | 207 | def test_get_common_ancestor_from_same_repo_existing(self, vcsbackend): |
|
208 | 208 | target_repo = vcsbackend.create_repo(number_of_commits=5) |
|
209 | 209 | |
|
210 | 210 | expected_ancestor = target_repo[2].raw_id |
|
211 | 211 | |
|
212 | 212 | assert target_repo.get_common_ancestor( |
|
213 | 213 | commit_id1=target_repo[2].raw_id, |
|
214 | 214 | commit_id2=target_repo[4].raw_id, |
|
215 | 215 | repo2=target_repo |
|
216 | 216 | ) == expected_ancestor |
|
217 | 217 | |
|
218 | 218 | assert target_repo.get_common_ancestor( |
|
219 | 219 | commit_id1=target_repo[4].raw_id, |
|
220 | 220 | commit_id2=target_repo[2].raw_id, |
|
221 | 221 | repo2=target_repo |
|
222 | 222 | ) == expected_ancestor |
|
223 | 223 | |
|
224 | 224 | @pytest.mark.xfail_backends("svn") |
|
225 | 225 | def test_get_common_ancestor_from_cloned_repo_existing(self, vcsbackend): |
|
226 | 226 | target_repo = vcsbackend.create_repo(number_of_commits=5) |
|
227 | 227 | source_repo = vcsbackend.clone_repo(target_repo) |
|
228 | 228 | assert target_repo != source_repo |
|
229 | 229 | |
|
230 | 230 | vcsbackend.add_file(source_repo, 'newfile', 'somecontent') |
|
231 | 231 | source_commit = source_repo.get_commit() |
|
232 | 232 | |
|
233 | 233 | expected_ancestor = target_repo[4].raw_id |
|
234 | 234 | |
|
235 | 235 | assert target_repo.get_common_ancestor( |
|
236 | 236 | commit_id1=target_repo[4].raw_id, |
|
237 | 237 | commit_id2=source_commit.raw_id, |
|
238 | 238 | repo2=source_repo |
|
239 | 239 | ) == expected_ancestor |
|
240 | 240 | |
|
241 | 241 | assert target_repo.get_common_ancestor( |
|
242 | 242 | commit_id1=source_commit.raw_id, |
|
243 | 243 | commit_id2=target_repo[4].raw_id, |
|
244 | 244 | repo2=target_repo |
|
245 | 245 | ) == expected_ancestor |
|
246 | 246 | |
|
247 | 247 | @pytest.mark.xfail_backends("svn") |
|
248 | 248 | def test_get_common_ancestor_from_unrelated_repo_missing(self, vcsbackend): |
|
249 | 249 | original = vcsbackend.create_repo(number_of_commits=5) |
|
250 | 250 | unrelated = vcsbackend.create_repo(number_of_commits=5) |
|
251 | 251 | assert original != unrelated |
|
252 | 252 | |
|
253 | 253 | assert original.get_common_ancestor( |
|
254 | 254 | commit_id1=original[0].raw_id, |
|
255 | 255 | commit_id2=unrelated[0].raw_id, |
|
256 | 256 | repo2=unrelated |
|
257 | 257 | ) == None |
|
258 | 258 | |
|
259 | 259 | assert original.get_common_ancestor( |
|
260 | 260 | commit_id1=original[-1].raw_id, |
|
261 | 261 | commit_id2=unrelated[-1].raw_id, |
|
262 | 262 | repo2=unrelated |
|
263 | 263 | ) == None |
|
264 | 264 | |
|
265 | 265 | |
|
266 | 266 | @pytest.mark.backends("git", "hg") |
|
267 | 267 | class TestRepositoryMerge: |
|
268 | 268 | def prepare_for_success(self, vcsbackend): |
|
269 | 269 | self.target_repo = vcsbackend.create_repo(number_of_commits=1) |
|
270 | 270 | self.source_repo = vcsbackend.clone_repo(self.target_repo) |
|
271 | 271 | vcsbackend.add_file(self.target_repo, 'README_MERGE1', 'Version 1') |
|
272 | 272 | vcsbackend.add_file(self.source_repo, 'README_MERGE2', 'Version 2') |
|
273 | 273 | imc = self.source_repo.in_memory_commit |
|
274 | 274 | imc.add(FileNode('file_x', content=self.source_repo.name)) |
|
275 | 275 | imc.commit( |
|
276 | 276 | message=u'Automatic commit from repo merge test', |
|
277 | 277 | author=u'Automatic') |
|
278 | 278 | self.target_commit = self.target_repo.get_commit() |
|
279 | 279 | self.source_commit = self.source_repo.get_commit() |
|
280 | 280 | # This only works for Git and Mercurial |
|
281 | 281 | default_branch = self.target_repo.DEFAULT_BRANCH_NAME |
|
282 | 282 | self.target_ref = Reference( |
|
283 | 283 | 'branch', default_branch, self.target_commit.raw_id) |
|
284 | 284 | self.source_ref = Reference( |
|
285 | 285 | 'branch', default_branch, self.source_commit.raw_id) |
|
286 | 286 | self.workspace = 'test-merge' |
|
287 | 287 | |
|
288 | 288 | def prepare_for_conflict(self, vcsbackend): |
|
289 | 289 | self.target_repo = vcsbackend.create_repo(number_of_commits=1) |
|
290 | 290 | self.source_repo = vcsbackend.clone_repo(self.target_repo) |
|
291 | 291 | vcsbackend.add_file(self.target_repo, 'README_MERGE', 'Version 1') |
|
292 | 292 | vcsbackend.add_file(self.source_repo, 'README_MERGE', 'Version 2') |
|
293 | 293 | self.target_commit = self.target_repo.get_commit() |
|
294 | 294 | self.source_commit = self.source_repo.get_commit() |
|
295 | 295 | # This only works for Git and Mercurial |
|
296 | 296 | default_branch = self.target_repo.DEFAULT_BRANCH_NAME |
|
297 | 297 | self.target_ref = Reference( |
|
298 | 298 | 'branch', default_branch, self.target_commit.raw_id) |
|
299 | 299 | self.source_ref = Reference( |
|
300 | 300 | 'branch', default_branch, self.source_commit.raw_id) |
|
301 | 301 | self.workspace = 'test-merge' |
|
302 | 302 | |
|
303 | 303 | def test_merge_success(self, vcsbackend): |
|
304 | 304 | self.prepare_for_success(vcsbackend) |
|
305 | 305 | |
|
306 | 306 | merge_response = self.target_repo.merge( |
|
307 | 307 | self.target_ref, self.source_repo, self.source_ref, self.workspace, |
|
308 | 308 | 'test user', 'test@rhodecode.com', 'merge message 1', |
|
309 | 309 | dry_run=False) |
|
310 | 310 | expected_merge_response = MergeResponse( |
|
311 | 311 | True, True, merge_response.merge_ref, |
|
312 | 312 | MergeFailureReason.NONE) |
|
313 | 313 | assert merge_response == expected_merge_response |
|
314 | 314 | |
|
315 | 315 | target_repo = backends.get_backend(vcsbackend.alias)( |
|
316 | 316 | self.target_repo.path) |
|
317 | 317 | target_commits = list(target_repo.get_commits()) |
|
318 | 318 | commit_ids = [c.raw_id for c in target_commits[:-1]] |
|
319 | 319 | assert self.source_ref.commit_id in commit_ids |
|
320 | 320 | assert self.target_ref.commit_id in commit_ids |
|
321 | 321 | |
|
322 | 322 | merge_commit = target_commits[-1] |
|
323 | 323 | assert merge_commit.raw_id == merge_response.merge_ref.commit_id |
|
324 | 324 | assert merge_commit.message.strip() == 'merge message 1' |
|
325 | 325 | assert merge_commit.author == 'test user <test@rhodecode.com>' |
|
326 | 326 | |
|
327 | 327 | # We call it twice so to make sure we can handle updates |
|
328 | 328 | target_ref = Reference( |
|
329 | 329 | self.target_ref.type, self.target_ref.name, |
|
330 | 330 | merge_response.merge_ref.commit_id) |
|
331 | 331 | |
|
332 | 332 | merge_response = target_repo.merge( |
|
333 | 333 | target_ref, self.source_repo, self.source_ref, self.workspace, |
|
334 | 334 | 'test user', 'test@rhodecode.com', 'merge message 2', |
|
335 | 335 | dry_run=False) |
|
336 | 336 | expected_merge_response = MergeResponse( |
|
337 | 337 | True, True, merge_response.merge_ref, |
|
338 | 338 | MergeFailureReason.NONE) |
|
339 | 339 | assert merge_response == expected_merge_response |
|
340 | 340 | |
|
341 | 341 | target_repo = backends.get_backend( |
|
342 | 342 | vcsbackend.alias)(self.target_repo.path) |
|
343 | 343 | merge_commit = target_repo.get_commit( |
|
344 | 344 | merge_response.merge_ref.commit_id) |
|
345 | 345 | assert merge_commit.message.strip() == 'merge message 1' |
|
346 | 346 | assert merge_commit.author == 'test user <test@rhodecode.com>' |
|
347 | 347 | |
|
348 | 348 | def test_merge_success_dry_run(self, vcsbackend): |
|
349 | 349 | self.prepare_for_success(vcsbackend) |
|
350 | 350 | |
|
351 | 351 | merge_response = self.target_repo.merge( |
|
352 | 352 | self.target_ref, self.source_repo, self.source_ref, self.workspace, |
|
353 | 353 | dry_run=True) |
|
354 | 354 | |
|
355 | 355 | # We call it twice so to make sure we can handle updates |
|
356 | 356 | merge_response_update = self.target_repo.merge( |
|
357 | 357 | self.target_ref, self.source_repo, self.source_ref, self.workspace, |
|
358 | 358 | dry_run=True) |
|
359 | 359 | |
|
360 | 360 | # Multiple merges may differ in their commit id. Therefore we set the |
|
361 | 361 | # commit id to `None` before comparing the merge responses. |
|
362 | 362 | merge_response = merge_response._replace( |
|
363 | 363 | merge_ref=merge_response.merge_ref._replace(commit_id=None)) |
|
364 | 364 | merge_response_update = merge_response_update._replace( |
|
365 | 365 | merge_ref=merge_response_update.merge_ref._replace(commit_id=None)) |
|
366 | 366 | |
|
367 | 367 | assert merge_response == merge_response_update |
|
368 | 368 | assert merge_response.possible is True |
|
369 | 369 | assert merge_response.executed is False |
|
370 | 370 | assert merge_response.merge_ref |
|
371 | 371 | assert merge_response.failure_reason is MergeFailureReason.NONE |
|
372 | 372 | |
|
373 | 373 | @pytest.mark.parametrize('dry_run', [True, False]) |
|
374 | 374 | def test_merge_conflict(self, vcsbackend, dry_run): |
|
375 | 375 | self.prepare_for_conflict(vcsbackend) |
|
376 | 376 | expected_merge_response = MergeResponse( |
|
377 | 377 | False, False, None, MergeFailureReason.MERGE_FAILED) |
|
378 | 378 | |
|
379 | 379 | merge_response = self.target_repo.merge( |
|
380 | 380 | self.target_ref, self.source_repo, self.source_ref, self.workspace, |
|
381 | 381 | 'test_user', 'test@rhodecode.com', 'test message', dry_run=dry_run) |
|
382 | 382 | assert merge_response == expected_merge_response |
|
383 | 383 | |
|
384 | 384 | # We call it twice so to make sure we can handle updates |
|
385 | 385 | merge_response = self.target_repo.merge( |
|
386 | 386 | self.target_ref, self.source_repo, self.source_ref, self.workspace, |
|
387 | 387 | 'test_user', 'test@rhodecode.com', 'test message', dry_run=dry_run) |
|
388 | 388 | assert merge_response == expected_merge_response |
|
389 | 389 | |
|
390 | 390 | def test_merge_target_is_not_head(self, vcsbackend): |
|
391 | 391 | self.prepare_for_success(vcsbackend) |
|
392 | 392 | expected_merge_response = MergeResponse( |
|
393 | 393 | False, False, None, MergeFailureReason.TARGET_IS_NOT_HEAD) |
|
394 | 394 | |
|
395 | 395 | target_ref = Reference( |
|
396 | 396 | self.target_ref.type, self.target_ref.name, '0' * 40) |
|
397 | 397 | |
|
398 | 398 | merge_response = self.target_repo.merge( |
|
399 | 399 | target_ref, self.source_repo, self.source_ref, self.workspace, |
|
400 | 400 | dry_run=True) |
|
401 | 401 | |
|
402 | 402 | assert merge_response == expected_merge_response |
|
403 | 403 | |
|
404 |
def test_merge_missing_ |
|
|
404 | def test_merge_missing_source_reference(self, vcsbackend): | |
|
405 | 405 | self.prepare_for_success(vcsbackend) |
|
406 | 406 | expected_merge_response = MergeResponse( |
|
407 |
False, False, None, MergeFailureReason.MISSING_ |
|
|
407 | False, False, None, MergeFailureReason.MISSING_SOURCE_REF) | |
|
408 | 408 | |
|
409 | 409 | source_ref = Reference( |
|
410 | 410 | self.source_ref.type, 'not_existing', self.source_ref.commit_id) |
|
411 | 411 | |
|
412 | 412 | merge_response = self.target_repo.merge( |
|
413 | 413 | self.target_ref, self.source_repo, source_ref, self.workspace, |
|
414 | 414 | dry_run=True) |
|
415 | 415 | |
|
416 | 416 | assert merge_response == expected_merge_response |
|
417 | 417 | |
|
418 | 418 | def test_merge_raises_exception(self, vcsbackend): |
|
419 | 419 | self.prepare_for_success(vcsbackend) |
|
420 | 420 | expected_merge_response = MergeResponse( |
|
421 | 421 | False, False, None, MergeFailureReason.UNKNOWN) |
|
422 | 422 | |
|
423 | 423 | with mock.patch.object(self.target_repo, '_merge_repo', |
|
424 | 424 | side_effect=RepositoryError()): |
|
425 | 425 | merge_response = self.target_repo.merge( |
|
426 | 426 | self.target_ref, self.source_repo, self.source_ref, |
|
427 | 427 | self.workspace, dry_run=True) |
|
428 | 428 | |
|
429 | 429 | assert merge_response == expected_merge_response |
|
430 | 430 | |
|
431 | 431 | def test_merge_invalid_user_name(self, vcsbackend): |
|
432 | 432 | repo = vcsbackend.create_repo(number_of_commits=1) |
|
433 | 433 | ref = Reference('branch', 'master', 'not_used') |
|
434 | 434 | with pytest.raises(ValueError): |
|
435 | 435 | repo.merge(ref, self, ref, 'workspace_id') |
|
436 | 436 | |
|
437 | 437 | def test_merge_invalid_user_email(self, vcsbackend): |
|
438 | 438 | repo = vcsbackend.create_repo(number_of_commits=1) |
|
439 | 439 | ref = Reference('branch', 'master', 'not_used') |
|
440 | 440 | with pytest.raises(ValueError): |
|
441 | 441 | repo.merge(ref, self, ref, 'workspace_id', 'user name') |
|
442 | 442 | |
|
443 | 443 | def test_merge_invalid_message(self, vcsbackend): |
|
444 | 444 | repo = vcsbackend.create_repo(number_of_commits=1) |
|
445 | 445 | ref = Reference('branch', 'master', 'not_used') |
|
446 | 446 | with pytest.raises(ValueError): |
|
447 | 447 | repo.merge( |
|
448 | 448 | ref, self, ref, 'workspace_id', 'user name', 'user@email.com') |
|
449 | 449 | |
|
450 | 450 | |
|
451 | 451 | class TestRepositoryStrip(BackendTestMixin): |
|
452 | 452 | recreate_repo_per_test = True |
|
453 | 453 | |
|
454 | 454 | @classmethod |
|
455 | 455 | def _get_commits(cls): |
|
456 | 456 | commits = [ |
|
457 | 457 | { |
|
458 | 458 | 'message': 'Initial commit', |
|
459 | 459 | 'author': 'Joe Doe <joe.doe@example.com>', |
|
460 | 460 | 'date': datetime.datetime(2010, 1, 1, 20), |
|
461 | 461 | 'branch': 'master', |
|
462 | 462 | 'added': [ |
|
463 | 463 | FileNode('foobar', content='foobar'), |
|
464 | 464 | FileNode('foobar2', content='foobar2'), |
|
465 | 465 | ], |
|
466 | 466 | }, |
|
467 | 467 | ] |
|
468 | 468 | for x in xrange(10): |
|
469 | 469 | commit_data = { |
|
470 | 470 | 'message': 'Changed foobar - commit%s' % x, |
|
471 | 471 | 'author': 'Jane Doe <jane.doe@example.com>', |
|
472 | 472 | 'date': datetime.datetime(2010, 1, 1, 21, x), |
|
473 | 473 | 'branch': 'master', |
|
474 | 474 | 'changed': [ |
|
475 | 475 | FileNode('foobar', 'FOOBAR - %s' % x), |
|
476 | 476 | ], |
|
477 | 477 | } |
|
478 | 478 | commits.append(commit_data) |
|
479 | 479 | return commits |
|
480 | 480 | |
|
481 | 481 | @pytest.mark.backends("git", "hg") |
|
482 | 482 | def test_strip_commit(self): |
|
483 | 483 | tip = self.repo.get_commit() |
|
484 | 484 | assert tip.idx == 10 |
|
485 | 485 | self.repo.strip(tip.raw_id, self.repo.DEFAULT_BRANCH_NAME) |
|
486 | 486 | |
|
487 | 487 | tip = self.repo.get_commit() |
|
488 | 488 | assert tip.idx == 9 |
|
489 | 489 | |
|
490 | 490 | @pytest.mark.backends("git", "hg") |
|
491 | 491 | def test_strip_multiple_commits(self): |
|
492 | 492 | tip = self.repo.get_commit() |
|
493 | 493 | assert tip.idx == 10 |
|
494 | 494 | |
|
495 | 495 | old = self.repo.get_commit(commit_idx=5) |
|
496 | 496 | self.repo.strip(old.raw_id, self.repo.DEFAULT_BRANCH_NAME) |
|
497 | 497 | |
|
498 | 498 | tip = self.repo.get_commit() |
|
499 | 499 | assert tip.idx == 4 |
|
500 | 500 | |
|
501 | 501 | |
|
502 | 502 | @pytest.mark.backends('hg', 'git') |
|
503 | 503 | class TestRepositoryPull: |
|
504 | 504 | |
|
505 | 505 | def test_pull(self, vcsbackend): |
|
506 | 506 | source_repo = vcsbackend.repo |
|
507 | 507 | target_repo = vcsbackend.create_repo() |
|
508 | 508 | assert len(source_repo.commit_ids) > len(target_repo.commit_ids) |
|
509 | 509 | |
|
510 | 510 | target_repo.pull(source_repo.path) |
|
511 | 511 | # Note: Get a fresh instance, avoids caching trouble |
|
512 | 512 | target_repo = vcsbackend.backend(target_repo.path) |
|
513 | 513 | assert len(source_repo.commit_ids) == len(target_repo.commit_ids) |
|
514 | 514 | |
|
515 | 515 | def test_pull_wrong_path(self, vcsbackend): |
|
516 | 516 | target_repo = vcsbackend.create_repo() |
|
517 | 517 | with pytest.raises(RepositoryError): |
|
518 | 518 | target_repo.pull(target_repo.path + "wrong") |
|
519 | 519 | |
|
520 | 520 | def test_pull_specific_commits(self, vcsbackend): |
|
521 | 521 | source_repo = vcsbackend.repo |
|
522 | 522 | target_repo = vcsbackend.create_repo() |
|
523 | 523 | |
|
524 | 524 | second_commit = source_repo[1].raw_id |
|
525 | 525 | if vcsbackend.alias == 'git': |
|
526 | 526 | second_commit_ref = 'refs/test-refs/a' |
|
527 | 527 | source_repo.set_refs(second_commit_ref, second_commit) |
|
528 | 528 | |
|
529 | 529 | target_repo.pull(source_repo.path, commit_ids=[second_commit]) |
|
530 | 530 | target_repo = vcsbackend.backend(target_repo.path) |
|
531 | 531 | assert 2 == len(target_repo.commit_ids) |
|
532 | 532 | assert second_commit == target_repo.get_commit().raw_id |
General Comments 0
You need to be logged in to leave comments.
Login now