##// END OF EJS Templates
fix(LFS): added git lfs push --all <GIT-URL> option's related changes.
ilin.s -
r5258:5a32a6f1 default
parent child Browse files
Show More
@@ -1,1053 +1,1053 b''
1 1 # Copyright (C) 2014-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 """
20 20 GIT repository module
21 21 """
22 22
23 23 import logging
24 24 import os
25 25 import re
26 26
27 27 from zope.cachedescriptors.property import Lazy as LazyProperty
28 28
29 29 from collections import OrderedDict
30 30 from rhodecode.lib.datelib import (
31 31 utcdate_fromtimestamp, makedate, date_astimestamp)
32 32 from rhodecode.lib.hash_utils import safe_str
33 33 from rhodecode.lib.utils2 import CachedProperty
34 34 from rhodecode.lib.vcs import connection, path as vcspath
35 35 from rhodecode.lib.vcs.backends.base import (
36 36 BaseRepository, CollectionGenerator, Config, MergeResponse,
37 37 MergeFailureReason, Reference)
38 38 from rhodecode.lib.vcs.backends.git.commit import GitCommit
39 39 from rhodecode.lib.vcs.backends.git.diff import GitDiff
40 40 from rhodecode.lib.vcs.backends.git.inmemory import GitInMemoryCommit
41 41 from rhodecode.lib.vcs.exceptions import (
42 42 CommitDoesNotExistError, EmptyRepositoryError,
43 43 RepositoryError, TagAlreadyExistError, TagDoesNotExistError, VCSError, UnresolvedFilesInRepo)
44 44
45 45
46 46 SHA_PATTERN = re.compile(r'^([0-9a-fA-F]{12}|[0-9a-fA-F]{40})$')
47 47
48 48 log = logging.getLogger(__name__)
49 49
50 50
51 51 class GitRepository(BaseRepository):
52 52 """
53 53 Git repository backend.
54 54 """
55 55 DEFAULT_BRANCH_NAME = os.environ.get('GIT_DEFAULT_BRANCH_NAME') or 'master'
56 56 DEFAULT_REF = f'branch:{DEFAULT_BRANCH_NAME}'
57 57
58 58 contact = BaseRepository.DEFAULT_CONTACT
59 59
60 60 def __init__(self, repo_path, config=None, create=False, src_url=None,
61 61 do_workspace_checkout=False, with_wire=None, bare=False):
62 62
63 63 self.path = safe_str(os.path.abspath(repo_path))
64 64 self.config = config if config else self.get_default_config()
65 65 self.with_wire = with_wire or {"cache": False} # default should not use cache
66 66
67 67 self._init_repo(create, src_url, do_workspace_checkout, bare)
68 68
69 69 # caches
70 70 self._commit_ids = {}
71 71
72 72 @LazyProperty
73 73 def _remote(self):
74 74 repo_id = self.path
75 75 return connection.Git(self.path, repo_id, self.config, with_wire=self.with_wire)
76 76
77 77 @LazyProperty
78 78 def bare(self):
79 79 return self._remote.bare()
80 80
81 81 @LazyProperty
82 82 def head(self):
83 83 return self._remote.head()
84 84
85 85 @CachedProperty
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 commit ids 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 = {commit_id: index
97 97 for index, commit_id in enumerate(commit_ids)}
98 98
99 99 def run_git_command(self, cmd, **opts):
100 100 """
101 101 Runs given ``cmd`` as git command and returns tuple
102 102 (stdout, stderr).
103 103
104 104 :param cmd: git command to be executed
105 105 :param opts: env options to pass into Subprocess command
106 106 """
107 107 if not isinstance(cmd, list):
108 108 raise ValueError(f'cmd must be a list, got {type(cmd)} instead')
109 109
110 110 skip_stderr_log = opts.pop('skip_stderr_log', False)
111 111 out, err = self._remote.run_git_command(cmd, **opts)
112 112 if err and not skip_stderr_log:
113 113 log.debug('Stderr output of git command "%s":\n%s', cmd, err)
114 114 return out, err
115 115
116 116 @staticmethod
117 117 def check_url(url, config):
118 118 """
119 119 Function will check given url and try to verify if it's a valid
120 120 link. Sometimes it may happened that git will issue basic
121 121 auth request that can cause whole API to hang when used from python
122 122 or other external calls.
123 123
124 124 On failures it'll raise urllib2.HTTPError, exception is also thrown
125 125 when the return code is non 200
126 126 """
127 127 # check first if it's not an url
128 128 if os.path.isdir(url) or url.startswith('file:'):
129 129 return True
130 130
131 131 if '+' in url.split('://', 1)[0]:
132 132 url = url.split('+', 1)[1]
133 133
134 134 # Request the _remote to verify the url
135 135 return connection.Git.check_url(url, config.serialize())
136 136
137 137 @staticmethod
138 138 def is_valid_repository(path):
139 139 if os.path.isdir(os.path.join(path, '.git')):
140 140 return True
141 141 # check case of bare repository
142 142 try:
143 143 GitRepository(path)
144 144 return True
145 145 except VCSError:
146 146 pass
147 147 return False
148 148
149 149 def _init_repo(self, create, src_url=None, do_workspace_checkout=False,
150 150 bare=False):
151 151 if create and os.path.exists(self.path):
152 152 raise RepositoryError(
153 153 f"Cannot create repository at {self.path}, location already exist")
154 154
155 155 if bare and do_workspace_checkout:
156 156 raise RepositoryError("Cannot update a bare repository")
157 157 try:
158 158
159 159 if src_url:
160 160 # check URL before any actions
161 161 GitRepository.check_url(src_url, self.config)
162 162
163 163 if create:
164 164 if bare:
165 165 self._remote.init_bare()
166 166 else:
167 167 self._remote.init()
168 168
169 169 if src_url and bare:
170 170 # bare repository only allows a fetch and checkout is not allowed
171 171 self.fetch(src_url, commit_ids=None)
172 172 elif src_url:
173 173 self.pull(src_url, commit_ids=None,
174 174 update_after=do_workspace_checkout)
175 175
176 176 else:
177 177 if not self._remote.assert_correct_path():
178 178 raise RepositoryError(
179 179 f'Path "{self.path}" does not contain a Git repository')
180 180
181 181 # TODO: johbo: check if we have to translate the OSError here
182 182 except OSError as err:
183 183 raise RepositoryError(err)
184 184
185 185 def _get_all_commit_ids(self):
186 186 return self._remote.get_all_commit_ids()
187 187
188 188 def _get_commit_ids(self, filters=None):
189 189 # we must check if this repo is not empty, since later command
190 190 # fails if it is. And it's cheaper to ask than throw the subprocess
191 191 # errors
192 192
193 193 head = self._remote.head(show_exc=False)
194 194
195 195 if not head:
196 196 return []
197 197
198 198 rev_filter = ['--branches', '--tags']
199 199 extra_filter = []
200 200
201 201 if filters:
202 202 if filters.get('since'):
203 203 extra_filter.append('--since=%s' % (filters['since']))
204 204 if filters.get('until'):
205 205 extra_filter.append('--until=%s' % (filters['until']))
206 206 if filters.get('branch_name'):
207 207 rev_filter = []
208 208 extra_filter.append(filters['branch_name'])
209 209 rev_filter.extend(extra_filter)
210 210
211 211 # if filters.get('start') or filters.get('end'):
212 212 # # skip is offset, max-count is limit
213 213 # if filters.get('start'):
214 214 # extra_filter += ' --skip=%s' % filters['start']
215 215 # if filters.get('end'):
216 216 # extra_filter += ' --max-count=%s' % (filters['end'] - (filters['start'] or 0))
217 217
218 218 cmd = ['rev-list', '--reverse', '--date-order'] + rev_filter
219 219 try:
220 220 output, __ = self.run_git_command(cmd)
221 221 except RepositoryError:
222 222 # Can be raised for empty repositories
223 223 return []
224 224 return output.splitlines()
225 225
226 226 def _lookup_commit(self, commit_id_or_idx, translate_tag=True, maybe_unreachable=False, reference_obj=None):
227 227
228 228 def is_null(value):
229 229 return len(value) == commit_id_or_idx.count('0')
230 230
231 231 if commit_id_or_idx in (None, '', 'tip', 'HEAD', 'head', -1):
232 232 return self.commit_ids[-1]
233 233
234 234 commit_missing_err = "Commit {} does not exist for `{}`".format(
235 235 *map(safe_str, [commit_id_or_idx, self.name]))
236 236
237 237 is_bstr = isinstance(commit_id_or_idx, str)
238 238 is_branch = reference_obj and reference_obj.branch
239 239
240 240 lookup_ok = False
241 241 if is_bstr:
242 242 # Need to call remote to translate id for tagging scenarios,
243 243 # or branch that are numeric
244 244 try:
245 245 remote_data = self._remote.get_object(commit_id_or_idx,
246 246 maybe_unreachable=maybe_unreachable)
247 247 commit_id_or_idx = remote_data["commit_id"]
248 248 lookup_ok = True
249 249 except (CommitDoesNotExistError,):
250 250 lookup_ok = False
251 251
252 252 if lookup_ok is False:
253 253 is_numeric_idx = \
254 254 (is_bstr and commit_id_or_idx.isdigit() and len(commit_id_or_idx) < 12) \
255 255 or isinstance(commit_id_or_idx, int)
256 256 if not is_branch and (is_numeric_idx or is_null(commit_id_or_idx)):
257 257 try:
258 258 commit_id_or_idx = self.commit_ids[int(commit_id_or_idx)]
259 259 lookup_ok = True
260 260 except Exception:
261 261 raise CommitDoesNotExistError(commit_missing_err)
262 262
263 263 # we failed regular lookup, and by integer number lookup
264 264 if lookup_ok is False:
265 265 raise CommitDoesNotExistError(commit_missing_err)
266 266
267 267 # Ensure we return full id
268 268 if not SHA_PATTERN.match(str(commit_id_or_idx)):
269 269 raise CommitDoesNotExistError(
270 270 "Given commit id %s not recognized" % commit_id_or_idx)
271 271 return commit_id_or_idx
272 272
273 273 def get_hook_location(self):
274 274 """
275 275 returns absolute path to location where hooks are stored
276 276 """
277 277 loc = os.path.join(self.path, 'hooks')
278 278 if not self.bare:
279 279 loc = os.path.join(self.path, '.git', 'hooks')
280 280 return loc
281 281
282 282 @LazyProperty
283 283 def last_change(self):
284 284 """
285 285 Returns last change made on this repository as
286 286 `datetime.datetime` object.
287 287 """
288 288 try:
289 289 return self.get_commit().date
290 290 except RepositoryError:
291 291 tzoffset = makedate()[1]
292 292 return utcdate_fromtimestamp(self._get_fs_mtime(), tzoffset)
293 293
294 294 def _get_fs_mtime(self):
295 295 idx_loc = '' if self.bare else '.git'
296 296 # fallback to filesystem
297 297 in_path = os.path.join(self.path, idx_loc, "index")
298 298 he_path = os.path.join(self.path, idx_loc, "HEAD")
299 299 if os.path.exists(in_path):
300 300 return os.stat(in_path).st_mtime
301 301 else:
302 302 return os.stat(he_path).st_mtime
303 303
304 304 @LazyProperty
305 305 def description(self):
306 306 description = self._remote.get_description()
307 307 return safe_str(description or self.DEFAULT_DESCRIPTION)
308 308
309 309 def _get_refs_entries(self, prefix='', reverse=False, strip_prefix=True):
310 310 if self.is_empty():
311 311 return OrderedDict()
312 312
313 313 result = []
314 314 for ref, sha in self._refs.items():
315 315 if ref.startswith(prefix):
316 316 ref_name = ref
317 317 if strip_prefix:
318 318 ref_name = ref[len(prefix):]
319 319 result.append((safe_str(ref_name), sha))
320 320
321 321 def get_name(entry):
322 322 return entry[0]
323 323
324 324 return OrderedDict(sorted(result, key=get_name, reverse=reverse))
325 325
326 326 def _get_branches(self):
327 327 return self._get_refs_entries(prefix='refs/heads/', strip_prefix=True)
328 328
329 329 @CachedProperty
330 330 def branches(self):
331 331 return self._get_branches()
332 332
333 333 @CachedProperty
334 334 def branches_closed(self):
335 335 return {}
336 336
337 337 @CachedProperty
338 338 def bookmarks(self):
339 339 return {}
340 340
341 341 @CachedProperty
342 342 def branches_all(self):
343 343 all_branches = {}
344 344 all_branches.update(self.branches)
345 345 all_branches.update(self.branches_closed)
346 346 return all_branches
347 347
348 348 @CachedProperty
349 349 def tags(self):
350 350 return self._get_tags()
351 351
352 352 def _get_tags(self):
353 353 return self._get_refs_entries(prefix='refs/tags/', strip_prefix=True, reverse=True)
354 354
355 355 def tag(self, name, user, commit_id=None, message=None, date=None,
356 356 **kwargs):
357 357 # TODO: fix this method to apply annotated tags correct with message
358 358 """
359 359 Creates and returns a tag for the given ``commit_id``.
360 360
361 361 :param name: name for new tag
362 362 :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>"
363 363 :param commit_id: commit id for which new tag would be created
364 364 :param message: message of the tag's commit
365 365 :param date: date of tag's commit
366 366
367 367 :raises TagAlreadyExistError: if tag with same name already exists
368 368 """
369 369 if name in self.tags:
370 370 raise TagAlreadyExistError("Tag %s already exists" % name)
371 371 commit = self.get_commit(commit_id=commit_id)
372 372 message = message or f"Added tag {name} for commit {commit.raw_id}"
373 373
374 374 self._remote.set_refs('refs/tags/%s' % name, commit.raw_id)
375 375
376 376 self._invalidate_prop_cache('tags')
377 377 self._invalidate_prop_cache('_refs')
378 378
379 379 return commit
380 380
381 381 def remove_tag(self, name, user, message=None, date=None):
382 382 """
383 383 Removes tag with the given ``name``.
384 384
385 385 :param name: name of the tag to be removed
386 386 :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>"
387 387 :param message: message of the tag's removal commit
388 388 :param date: date of tag's removal commit
389 389
390 390 :raises TagDoesNotExistError: if tag with given name does not exists
391 391 """
392 392 if name not in self.tags:
393 393 raise TagDoesNotExistError("Tag %s does not exist" % name)
394 394
395 395 self._remote.tag_remove(name)
396 396 self._invalidate_prop_cache('tags')
397 397 self._invalidate_prop_cache('_refs')
398 398
399 399 def _get_refs(self):
400 400 return self._remote.get_refs()
401 401
402 402 @CachedProperty
403 403 def _refs(self):
404 404 return self._get_refs()
405 405
406 406 @property
407 407 def _ref_tree(self):
408 408 node = tree = {}
409 409 for ref, sha in self._refs.items():
410 410 path = ref.split('/')
411 411 for bit in path[:-1]:
412 412 node = node.setdefault(bit, {})
413 413 node[path[-1]] = sha
414 414 node = tree
415 415 return tree
416 416
417 417 def get_remote_ref(self, ref_name):
418 418 ref_key = f'refs/remotes/origin/{safe_str(ref_name)}'
419 419 try:
420 420 return self._refs[ref_key]
421 421 except Exception:
422 422 return
423 423
424 424 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
425 425 translate_tag=True, maybe_unreachable=False, reference_obj=None):
426 426 """
427 427 Returns `GitCommit` object representing commit from git repository
428 428 at the given `commit_id` or head (most recent commit) if None given.
429 429 """
430 430
431 431 if self.is_empty():
432 432 raise EmptyRepositoryError("There are no commits yet")
433 433
434 434 if commit_id is not None:
435 435 self._validate_commit_id(commit_id)
436 436 try:
437 437 # we have cached idx, use it without contacting the remote
438 438 idx = self._commit_ids[commit_id]
439 439 return GitCommit(self, commit_id, idx, pre_load=pre_load)
440 440 except KeyError:
441 441 pass
442 442
443 443 elif commit_idx is not None:
444 444 self._validate_commit_idx(commit_idx)
445 445 try:
446 446 _commit_id = self.commit_ids[commit_idx]
447 447 if commit_idx < 0:
448 448 commit_idx = self.commit_ids.index(_commit_id)
449 449 return GitCommit(self, _commit_id, commit_idx, pre_load=pre_load)
450 450 except IndexError:
451 451 commit_id = commit_idx
452 452 else:
453 453 commit_id = "tip"
454 454
455 455 if translate_tag:
456 456 commit_id = self._lookup_commit(
457 457 commit_id, maybe_unreachable=maybe_unreachable,
458 458 reference_obj=reference_obj)
459 459
460 460 try:
461 461 idx = self._commit_ids[commit_id]
462 462 except KeyError:
463 463 idx = -1
464 464
465 465 return GitCommit(self, commit_id, idx, pre_load=pre_load)
466 466
467 467 def get_commits(
468 468 self, start_id=None, end_id=None, start_date=None, end_date=None,
469 469 branch_name=None, show_hidden=False, pre_load=None, translate_tags=True):
470 470 """
471 471 Returns generator of `GitCommit` objects from start to end (both
472 472 are inclusive), in ascending date order.
473 473
474 474 :param start_id: None, str(commit_id)
475 475 :param end_id: None, str(commit_id)
476 476 :param start_date: if specified, commits with commit date less than
477 477 ``start_date`` would be filtered out from returned set
478 478 :param end_date: if specified, commits with commit date greater than
479 479 ``end_date`` would be filtered out from returned set
480 480 :param branch_name: if specified, commits not reachable from given
481 481 branch would be filtered out from returned set
482 482 :param show_hidden: Show hidden commits such as obsolete or hidden from
483 483 Mercurial evolve
484 484 :raise BranchDoesNotExistError: If given `branch_name` does not
485 485 exist.
486 486 :raise CommitDoesNotExistError: If commits for given `start` or
487 487 `end` could not be found.
488 488
489 489 """
490 490 if self.is_empty():
491 491 raise EmptyRepositoryError("There are no commits yet")
492 492
493 493 self._validate_branch_name(branch_name)
494 494
495 495 if start_id is not None:
496 496 self._validate_commit_id(start_id)
497 497 if end_id is not None:
498 498 self._validate_commit_id(end_id)
499 499
500 500 start_raw_id = self._lookup_commit(start_id)
501 501 start_pos = self._commit_ids[start_raw_id] if start_id else None
502 502 end_raw_id = self._lookup_commit(end_id)
503 503 end_pos = max(0, self._commit_ids[end_raw_id]) if end_id else None
504 504
505 505 if None not in [start_id, end_id] and start_pos > end_pos:
506 506 raise RepositoryError(
507 507 "Start commit '%s' cannot be after end commit '%s'" %
508 508 (start_id, end_id))
509 509
510 510 if end_pos is not None:
511 511 end_pos += 1
512 512
513 513 filter_ = []
514 514 if branch_name:
515 515 filter_.append({'branch_name': branch_name})
516 516 if start_date and not end_date:
517 517 filter_.append({'since': start_date})
518 518 if end_date and not start_date:
519 519 filter_.append({'until': end_date})
520 520 if start_date and end_date:
521 521 filter_.append({'since': start_date})
522 522 filter_.append({'until': end_date})
523 523
524 524 # if start_pos or end_pos:
525 525 # filter_.append({'start': start_pos})
526 526 # filter_.append({'end': end_pos})
527 527
528 528 if filter_:
529 529 revfilters = {
530 530 'branch_name': branch_name,
531 531 'since': start_date.strftime('%m/%d/%y %H:%M:%S') if start_date else None,
532 532 'until': end_date.strftime('%m/%d/%y %H:%M:%S') if end_date else None,
533 533 'start': start_pos,
534 534 'end': end_pos,
535 535 }
536 536 commit_ids = self._get_commit_ids(filters=revfilters)
537 537
538 538 else:
539 539 commit_ids = self.commit_ids
540 540
541 541 if start_pos or end_pos:
542 542 commit_ids = commit_ids[start_pos: end_pos]
543 543
544 544 return CollectionGenerator(self, commit_ids, pre_load=pre_load,
545 545 translate_tag=translate_tags)
546 546
547 547 def get_diff(
548 548 self, commit1, commit2, path='', ignore_whitespace=False,
549 549 context=3, path1=None):
550 550 """
551 551 Returns (git like) *diff*, as plain text. Shows changes introduced by
552 552 ``commit2`` since ``commit1``.
553 553
554 554 :param commit1: Entry point from which diff is shown. Can be
555 555 ``self.EMPTY_COMMIT`` - in this case, patch showing all
556 556 the changes since empty state of the repository until ``commit2``
557 557 :param commit2: Until which commits changes should be shown.
558 558 :param path:
559 559 :param ignore_whitespace: If set to ``True``, would not show whitespace
560 560 changes. Defaults to ``False``.
561 561 :param context: How many lines before/after changed lines should be
562 562 shown. Defaults to ``3``.
563 563 :param path1:
564 564 """
565 565 self._validate_diff_commits(commit1, commit2)
566 566 if path1 is not None and path1 != path:
567 567 raise ValueError("Diff of two different paths not supported.")
568 568
569 569 if path:
570 570 file_filter = path
571 571 else:
572 572 file_filter = None
573 573
574 574 diff = self._remote.diff(
575 575 commit1.raw_id, commit2.raw_id, file_filter=file_filter,
576 576 opt_ignorews=ignore_whitespace,
577 577 context=context)
578 578
579 579 return GitDiff(diff)
580 580
581 581 def strip(self, commit_id, branch_name):
582 582 commit = self.get_commit(commit_id=commit_id)
583 583 if commit.merge:
584 584 raise Exception('Cannot reset to merge commit')
585 585
586 586 if not branch_name:
587 587 raise ValueError(f'git strip requires a valid branch name, got {branch_name} instead')
588 588
589 589 # parent is going to be the new head now
590 590 commit = commit.parents[0]
591 591 self._remote.update_refs(f'refs/heads/{branch_name}', commit.raw_id)
592 592
593 593 # clear cached properties
594 594 self._invalidate_prop_cache('commit_ids')
595 595 self._invalidate_prop_cache('_refs')
596 596 self._invalidate_prop_cache('branches')
597 597
598 598 return len(self.commit_ids)
599 599
600 600 def get_common_ancestor(self, commit_id1, commit_id2, repo2):
601 601 log.debug('Calculating common ancestor between %sc1:%s and %sc2:%s',
602 602 self, commit_id1, repo2, commit_id2)
603 603
604 604 if commit_id1 == commit_id2:
605 605 return commit_id1
606 606
607 607 if self != repo2:
608 608 commits = self._remote.get_missing_revs(
609 609 commit_id1, commit_id2, repo2.path)
610 610 if commits:
611 611 commit = repo2.get_commit(commits[-1])
612 612 if commit.parents:
613 613 ancestor_id = commit.parents[0].raw_id
614 614 else:
615 615 ancestor_id = None
616 616 else:
617 617 # no commits from other repo, ancestor_id is the commit_id2
618 618 ancestor_id = commit_id2
619 619 else:
620 620 output, __ = self.run_git_command(
621 621 ['merge-base', commit_id1, commit_id2])
622 622 ancestor_id = self.COMMIT_ID_PAT.findall(output)[0]
623 623
624 624 log.debug('Found common ancestor with sha: %s', ancestor_id)
625 625
626 626 return ancestor_id
627 627
628 628 def compare(self, commit_id1, commit_id2, repo2, merge, pre_load=None):
629 629 repo1 = self
630 630 ancestor_id = None
631 631
632 632 if commit_id1 == commit_id2:
633 633 commits = []
634 634 elif repo1 != repo2:
635 635 missing_ids = self._remote.get_missing_revs(commit_id1, commit_id2,
636 636 repo2.path)
637 637 commits = [
638 638 repo2.get_commit(commit_id=commit_id, pre_load=pre_load)
639 639 for commit_id in reversed(missing_ids)]
640 640 else:
641 641 output, __ = repo1.run_git_command(
642 642 ['log', '--reverse', '--pretty=format: %H', '-s',
643 643 f'{commit_id1}..{commit_id2}'])
644 644 commits = [
645 645 repo1.get_commit(commit_id=commit_id, pre_load=pre_load)
646 646 for commit_id in self.COMMIT_ID_PAT.findall(output)]
647 647
648 648 return commits
649 649
650 650 @LazyProperty
651 651 def in_memory_commit(self):
652 652 """
653 653 Returns ``GitInMemoryCommit`` object for this repository.
654 654 """
655 655 return GitInMemoryCommit(self)
656 656
657 657 def pull(self, url, commit_ids=None, update_after=False):
658 658 """
659 659 Pull changes from external location. Pull is different in GIT
660 660 that fetch since it's doing a checkout
661 661
662 662 :param commit_ids: Optional. Can be set to a list of commit ids
663 663 which shall be pulled from the other repository.
664 664 """
665 665 refs = None
666 666 if commit_ids is not None:
667 667 remote_refs = self._remote.get_remote_refs(url)
668 668 refs = [ref for ref in remote_refs if remote_refs[ref] in commit_ids]
669 669 self._remote.pull(url, refs=refs, update_after=update_after)
670 670 self._remote.invalidate_vcs_cache()
671 671
672 672 def fetch(self, url, commit_ids=None, **kwargs):
673 673 """
674 674 Fetch all git objects from external location.
675 675 """
676 676 self._remote.sync_fetch(url, refs=commit_ids, **kwargs)
677 677 self._remote.invalidate_vcs_cache()
678 678
679 def push(self, url):
679 def push(self, url, **kwargs):
680 680 refs = None
681 self._remote.sync_push(url, refs=refs)
681 self._remote.sync_push(url, refs=refs, **kwargs)
682 682
683 683 def set_refs(self, ref_name, commit_id):
684 684 self._remote.set_refs(ref_name, commit_id)
685 685 self._invalidate_prop_cache('_refs')
686 686
687 687 def remove_ref(self, ref_name):
688 688 self._remote.remove_ref(ref_name)
689 689 self._invalidate_prop_cache('_refs')
690 690
691 691 def run_gc(self, prune=True):
692 692 cmd = ['gc', '--aggressive']
693 693 if prune:
694 694 cmd += ['--prune=now']
695 695 _stdout, stderr = self.run_git_command(cmd, fail_on_stderr=False)
696 696 return stderr
697 697
698 698 def _update_server_info(self):
699 699 """
700 700 runs gits update-server-info command in this repo instance
701 701 """
702 702 self._remote.update_server_info()
703 703
704 704 def _current_branch(self):
705 705 """
706 706 Return the name of the current branch.
707 707
708 708 It only works for non bare repositories (i.e. repositories with a
709 709 working copy)
710 710 """
711 711 if self.bare:
712 712 raise RepositoryError('Bare git repos do not have active branches')
713 713
714 714 if self.is_empty():
715 715 return None
716 716
717 717 stdout, _ = self.run_git_command(['rev-parse', '--abbrev-ref', 'HEAD'])
718 718 return stdout.strip()
719 719
720 720 def _checkout(self, branch_name, create=False, force=False):
721 721 """
722 722 Checkout a branch in the working directory.
723 723
724 724 It tries to create the branch if create is True, failing if the branch
725 725 already exists.
726 726
727 727 It only works for non bare repositories (i.e. repositories with a
728 728 working copy)
729 729 """
730 730 if self.bare:
731 731 raise RepositoryError('Cannot checkout branches in a bare git repo')
732 732
733 733 cmd = ['checkout']
734 734 if force:
735 735 cmd.append('-f')
736 736 if create:
737 737 cmd.append('-b')
738 738 cmd.append(branch_name)
739 739 self.run_git_command(cmd, fail_on_stderr=False)
740 740
741 741 def _create_branch(self, branch_name, commit_id):
742 742 """
743 743 creates a branch in a GIT repo
744 744 """
745 745 self._remote.create_branch(branch_name, commit_id)
746 746
747 747 def _identify(self):
748 748 """
749 749 Return the current state of the working directory.
750 750 """
751 751 if self.bare:
752 752 raise RepositoryError('Bare git repos do not have active branches')
753 753
754 754 if self.is_empty():
755 755 return None
756 756
757 757 stdout, _ = self.run_git_command(['rev-parse', 'HEAD'])
758 758 return stdout.strip()
759 759
760 760 def _local_clone(self, clone_path, branch_name, source_branch=None):
761 761 """
762 762 Create a local clone of the current repo.
763 763 """
764 764 # N.B.(skreft): the --branch option is required as otherwise the shallow
765 765 # clone will only fetch the active branch.
766 766 cmd = ['clone', '--branch', branch_name,
767 767 self.path, os.path.abspath(clone_path)]
768 768
769 769 self.run_git_command(cmd, fail_on_stderr=False)
770 770
771 771 # if we get the different source branch, make sure we also fetch it for
772 772 # merge conditions
773 773 if source_branch and source_branch != branch_name:
774 774 # check if the ref exists.
775 775 shadow_repo = GitRepository(os.path.abspath(clone_path))
776 776 if shadow_repo.get_remote_ref(source_branch):
777 777 cmd = ['fetch', self.path, source_branch]
778 778 self.run_git_command(cmd, fail_on_stderr=False)
779 779
780 780 def _local_fetch(self, repository_path, branch_name, use_origin=False):
781 781 """
782 782 Fetch a branch from a local repository.
783 783 """
784 784 repository_path = os.path.abspath(repository_path)
785 785 if repository_path == self.path:
786 786 raise ValueError('Cannot fetch from the same repository')
787 787
788 788 if use_origin:
789 789 branch_name = '+{branch}:refs/heads/{branch}'.format(
790 790 branch=branch_name)
791 791
792 792 cmd = ['fetch', '--no-tags', '--update-head-ok',
793 793 repository_path, branch_name]
794 794 self.run_git_command(cmd, fail_on_stderr=False)
795 795
796 796 def _local_reset(self, branch_name):
797 797 branch_name = f'{branch_name}'
798 798 cmd = ['reset', '--hard', branch_name, '--']
799 799 self.run_git_command(cmd, fail_on_stderr=False)
800 800
801 801 def _last_fetch_heads(self):
802 802 """
803 803 Return the last fetched heads that need merging.
804 804
805 805 The algorithm is defined at
806 806 https://github.com/git/git/blob/v2.1.3/git-pull.sh#L283
807 807 """
808 808 if not self.bare:
809 809 fetch_heads_path = os.path.join(self.path, '.git', 'FETCH_HEAD')
810 810 else:
811 811 fetch_heads_path = os.path.join(self.path, 'FETCH_HEAD')
812 812
813 813 heads = []
814 814 with open(fetch_heads_path) as f:
815 815 for line in f:
816 816 if ' not-for-merge ' in line:
817 817 continue
818 818 line = re.sub('\t.*', '', line, flags=re.DOTALL)
819 819 heads.append(line)
820 820
821 821 return heads
822 822
823 823 def get_shadow_instance(self, shadow_repository_path, enable_hooks=False, cache=False):
824 824 return GitRepository(shadow_repository_path, with_wire={"cache": cache})
825 825
826 826 def _local_pull(self, repository_path, branch_name, ff_only=True):
827 827 """
828 828 Pull a branch from a local repository.
829 829 """
830 830 if self.bare:
831 831 raise RepositoryError('Cannot pull into a bare git repository')
832 832 # N.B.(skreft): The --ff-only option is to make sure this is a
833 833 # fast-forward (i.e., we are only pulling new changes and there are no
834 834 # conflicts with our current branch)
835 835 # Additionally, that option needs to go before --no-tags, otherwise git
836 836 # pull complains about it being an unknown flag.
837 837 cmd = ['pull']
838 838 if ff_only:
839 839 cmd.append('--ff-only')
840 840 cmd.extend(['--no-tags', repository_path, branch_name])
841 841 self.run_git_command(cmd, fail_on_stderr=False)
842 842
843 843 def _local_merge(self, merge_message, user_name, user_email, heads):
844 844 """
845 845 Merge the given head into the checked out branch.
846 846
847 847 It will force a merge commit.
848 848
849 849 Currently it raises an error if the repo is empty, as it is not possible
850 850 to create a merge commit in an empty repo.
851 851
852 852 :param merge_message: The message to use for the merge commit.
853 853 :param heads: the heads to merge.
854 854 """
855 855 if self.bare:
856 856 raise RepositoryError('Cannot merge into a bare git repository')
857 857
858 858 if not heads:
859 859 return
860 860
861 861 if self.is_empty():
862 862 # TODO(skreft): do something more robust in this case.
863 863 raise RepositoryError('Do not know how to merge into empty repositories yet')
864 864 unresolved = None
865 865
866 866 # N.B.(skreft): the --no-ff option is used to enforce the creation of a
867 867 # commit message. We also specify the user who is doing the merge.
868 868 cmd = ['-c', f'user.name="{user_name}"',
869 869 '-c', f'user.email={user_email}',
870 870 'merge', '--no-ff', '-m', safe_str(merge_message)]
871 871
872 872 merge_cmd = cmd + heads
873 873
874 874 try:
875 875 self.run_git_command(merge_cmd, fail_on_stderr=False)
876 876 except RepositoryError:
877 877 files = self.run_git_command(['diff', '--name-only', '--diff-filter', 'U'],
878 878 fail_on_stderr=False)[0].splitlines()
879 879 # NOTE(marcink): we add U notation for consistent with HG backend output
880 880 unresolved = [f'U {f}' for f in files]
881 881
882 882 # Cleanup any merge leftovers
883 883 self._remote.invalidate_vcs_cache()
884 884 self.run_git_command(['merge', '--abort'], fail_on_stderr=False)
885 885
886 886 if unresolved:
887 887 raise UnresolvedFilesInRepo(unresolved)
888 888 else:
889 889 raise
890 890
891 891 def _local_push(
892 892 self, source_branch, repository_path, target_branch,
893 893 enable_hooks=False, rc_scm_data=None):
894 894 """
895 895 Push the source_branch to the given repository and target_branch.
896 896
897 897 Currently it if the target_branch is not master and the target repo is
898 898 empty, the push will work, but then GitRepository won't be able to find
899 899 the pushed branch or the commits. As the HEAD will be corrupted (i.e.,
900 900 pointing to master, which does not exist).
901 901
902 902 It does not run the hooks in the target repo.
903 903 """
904 904 # TODO(skreft): deal with the case in which the target repo is empty,
905 905 # and the target_branch is not master.
906 906 target_repo = GitRepository(repository_path)
907 907 if (not target_repo.bare and
908 908 target_repo._current_branch() == target_branch):
909 909 # Git prevents pushing to the checked out branch, so simulate it by
910 910 # pulling into the target repository.
911 911 target_repo._local_pull(self.path, source_branch)
912 912 else:
913 913 cmd = ['push', os.path.abspath(repository_path),
914 914 f'{source_branch}:{target_branch}']
915 915 gitenv = {}
916 916 if rc_scm_data:
917 917 gitenv.update({'RC_SCM_DATA': rc_scm_data})
918 918
919 919 if not enable_hooks:
920 920 gitenv['RC_SKIP_HOOKS'] = '1'
921 921 self.run_git_command(cmd, fail_on_stderr=False, extra_env=gitenv)
922 922
923 923 def _get_new_pr_branch(self, source_branch, target_branch):
924 924 prefix = f'pr_{source_branch}-{target_branch}_'
925 925 pr_branches = []
926 926 for branch in self.branches:
927 927 if branch.startswith(prefix):
928 928 pr_branches.append(int(branch[len(prefix):]))
929 929
930 930 if not pr_branches:
931 931 branch_id = 0
932 932 else:
933 933 branch_id = max(pr_branches) + 1
934 934
935 935 return '%s%d' % (prefix, branch_id)
936 936
937 937 def _maybe_prepare_merge_workspace(
938 938 self, repo_id, workspace_id, target_ref, source_ref):
939 939 shadow_repository_path = self._get_shadow_repository_path(
940 940 self.path, repo_id, workspace_id)
941 941 if not os.path.exists(shadow_repository_path):
942 942 self._local_clone(
943 943 shadow_repository_path, target_ref.name, source_ref.name)
944 944 log.debug('Prepared %s shadow repository in %s',
945 945 self.alias, shadow_repository_path)
946 946
947 947 return shadow_repository_path
948 948
949 949 def _merge_repo(self, repo_id, workspace_id, target_ref,
950 950 source_repo, source_ref, merge_message,
951 951 merger_name, merger_email, dry_run=False,
952 952 use_rebase=False, close_branch=False):
953 953
954 954 log.debug('Executing merge_repo with %s strategy, dry_run mode:%s',
955 955 'rebase' if use_rebase else 'merge', dry_run)
956 956
957 957 if target_ref.commit_id != self.branches[target_ref.name]:
958 958 log.warning('Target ref %s commit mismatch %s vs %s', target_ref,
959 959 target_ref.commit_id, self.branches[target_ref.name])
960 960 return MergeResponse(
961 961 False, False, None, MergeFailureReason.TARGET_IS_NOT_HEAD,
962 962 metadata={'target_ref': target_ref})
963 963
964 964 shadow_repository_path = self._maybe_prepare_merge_workspace(
965 965 repo_id, workspace_id, target_ref, source_ref)
966 966 shadow_repo = self.get_shadow_instance(shadow_repository_path)
967 967
968 968 # checkout source, if it's different. Otherwise we could not
969 969 # fetch proper commits for merge testing
970 970 if source_ref.name != target_ref.name:
971 971 if shadow_repo.get_remote_ref(source_ref.name):
972 972 shadow_repo._checkout(source_ref.name, force=True)
973 973
974 974 # checkout target, and fetch changes
975 975 shadow_repo._checkout(target_ref.name, force=True)
976 976
977 977 # fetch/reset pull the target, in case it is changed
978 978 # this handles even force changes
979 979 shadow_repo._local_fetch(self.path, target_ref.name, use_origin=True)
980 980 shadow_repo._local_reset(target_ref.name)
981 981
982 982 # Need to reload repo to invalidate the cache, or otherwise we cannot
983 983 # retrieve the last target commit.
984 984 shadow_repo = self.get_shadow_instance(shadow_repository_path)
985 985 if target_ref.commit_id != shadow_repo.branches[target_ref.name]:
986 986 log.warning('Shadow Target ref %s commit mismatch %s vs %s',
987 987 target_ref, target_ref.commit_id,
988 988 shadow_repo.branches[target_ref.name])
989 989 return MergeResponse(
990 990 False, False, None, MergeFailureReason.TARGET_IS_NOT_HEAD,
991 991 metadata={'target_ref': target_ref})
992 992
993 993 # calculate new branch
994 994 pr_branch = shadow_repo._get_new_pr_branch(
995 995 source_ref.name, target_ref.name)
996 996 log.debug('using pull-request merge branch: `%s`', pr_branch)
997 997 # checkout to temp branch, and fetch changes
998 998 shadow_repo._checkout(pr_branch, create=True)
999 999 try:
1000 1000 shadow_repo._local_fetch(source_repo.path, source_ref.name)
1001 1001 except RepositoryError:
1002 1002 log.exception('Failure when doing local fetch on '
1003 1003 'shadow repo: %s', shadow_repo)
1004 1004 return MergeResponse(
1005 1005 False, False, None, MergeFailureReason.MISSING_SOURCE_REF,
1006 1006 metadata={'source_ref': source_ref})
1007 1007
1008 1008 merge_ref = None
1009 1009 merge_failure_reason = MergeFailureReason.NONE
1010 1010 metadata = {}
1011 1011 try:
1012 1012 shadow_repo._local_merge(merge_message, merger_name, merger_email,
1013 1013 [source_ref.commit_id])
1014 1014 merge_possible = True
1015 1015
1016 1016 # Need to invalidate the cache, or otherwise we
1017 1017 # cannot retrieve the merge commit.
1018 1018 shadow_repo = shadow_repo.get_shadow_instance(shadow_repository_path)
1019 1019 merge_commit_id = shadow_repo.branches[pr_branch]
1020 1020
1021 1021 # Set a reference pointing to the merge commit. This reference may
1022 1022 # be used to easily identify the last successful merge commit in
1023 1023 # the shadow repository.
1024 1024 shadow_repo.set_refs('refs/heads/pr-merge', merge_commit_id)
1025 1025 merge_ref = Reference('branch', 'pr-merge', merge_commit_id)
1026 1026 except RepositoryError as e:
1027 1027 log.exception('Failure when doing local merge on git shadow repo')
1028 1028 if isinstance(e, UnresolvedFilesInRepo):
1029 1029 metadata['unresolved_files'] = '\n* conflict: ' + ('\n * conflict: '.join(e.args[0]))
1030 1030
1031 1031 merge_possible = False
1032 1032 merge_failure_reason = MergeFailureReason.MERGE_FAILED
1033 1033
1034 1034 if merge_possible and not dry_run:
1035 1035 try:
1036 1036 shadow_repo._local_push(
1037 1037 pr_branch, self.path, target_ref.name, enable_hooks=True,
1038 1038 rc_scm_data=self.config.get('rhodecode', 'RC_SCM_DATA'))
1039 1039 merge_succeeded = True
1040 1040 except RepositoryError:
1041 1041 log.exception(
1042 1042 'Failure when doing local push from the shadow '
1043 1043 'repository to the target repository at %s.', self.path)
1044 1044 merge_succeeded = False
1045 1045 merge_failure_reason = MergeFailureReason.PUSH_FAILED
1046 1046 metadata['target'] = 'git shadow repo'
1047 1047 metadata['merge_commit'] = pr_branch
1048 1048 else:
1049 1049 merge_succeeded = False
1050 1050
1051 1051 return MergeResponse(
1052 1052 merge_possible, merge_succeeded, merge_ref, merge_failure_reason,
1053 1053 metadata=metadata)
@@ -1,1024 +1,1024 b''
1 1 # Copyright (C) 2014-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 """
20 20 HG repository module
21 21 """
22 22 import os
23 23 import logging
24 24 import binascii
25 25 import configparser
26 26 import urllib.request
27 27 import urllib.parse
28 28 import urllib.error
29 29
30 30 from zope.cachedescriptors.property import Lazy as LazyProperty
31 31
32 32 from collections import OrderedDict
33 33 from rhodecode.lib.datelib import (
34 34 date_to_timestamp_plus_offset, utcdate_fromtimestamp, makedate)
35 35 from rhodecode.lib.str_utils import safe_str
36 36 from rhodecode.lib.utils2 import CachedProperty
37 37 from rhodecode.lib.vcs import connection, exceptions
38 38 from rhodecode.lib.vcs.backends.base import (
39 39 BaseRepository, CollectionGenerator, Config, MergeResponse,
40 40 MergeFailureReason, Reference, BasePathPermissionChecker)
41 41 from rhodecode.lib.vcs.backends.hg.commit import MercurialCommit
42 42 from rhodecode.lib.vcs.backends.hg.diff import MercurialDiff
43 43 from rhodecode.lib.vcs.backends.hg.inmemory import MercurialInMemoryCommit
44 44 from rhodecode.lib.vcs.exceptions import (
45 45 EmptyRepositoryError, RepositoryError, TagAlreadyExistError,
46 46 TagDoesNotExistError, CommitDoesNotExistError, SubrepoMergeError, UnresolvedFilesInRepo)
47 47
48 48 hexlify = binascii.hexlify
49 49 nullid = "\0" * 20
50 50
51 51 log = logging.getLogger(__name__)
52 52
53 53
54 54 class MercurialRepository(BaseRepository):
55 55 """
56 56 Mercurial repository backend
57 57 """
58 58 DEFAULT_BRANCH_NAME = 'default'
59 59
60 60 def __init__(self, repo_path, config=None, create=False, src_url=None,
61 61 do_workspace_checkout=False, with_wire=None, bare=False):
62 62 """
63 63 Raises RepositoryError if repository could not be find at the given
64 64 ``repo_path``.
65 65
66 66 :param repo_path: local path of the repository
67 67 :param config: config object containing the repo configuration
68 68 :param create=False: if set to True, would try to create repository if
69 69 it does not exist rather than raising exception
70 70 :param src_url=None: would try to clone repository from given location
71 71 :param do_workspace_checkout=False: sets update of working copy after
72 72 making a clone
73 73 :param bare: not used, compatible with other VCS
74 74 """
75 75
76 76 self.path = safe_str(os.path.abspath(repo_path))
77 77 # mercurial since 4.4.X requires certain configuration to be present
78 78 # because sometimes we init the repos with config we need to meet
79 79 # special requirements
80 80 self.config = config if config else self.get_default_config(
81 81 default=[('extensions', 'largefiles', '')])
82 82
83 83 # NOTE(marcink): since python3 hgsubversion is deprecated.
84 84 # From old installations we might still have this set enabled
85 85 # we explicitly remove this now here to make sure it wont propagate further
86 86 if config and config.get('extensions', 'hgsubversion') is not None:
87 87 config.drop_option('extensions', 'hgsubversion')
88 88
89 89 self.with_wire = with_wire or {"cache": False} # default should not use cache
90 90
91 91 self._init_repo(create, src_url, do_workspace_checkout)
92 92
93 93 # caches
94 94 self._commit_ids = {}
95 95
96 96 @LazyProperty
97 97 def _remote(self):
98 98 repo_id = self.path
99 99 return connection.Hg(self.path, repo_id, self.config, with_wire=self.with_wire)
100 100
101 101 @CachedProperty
102 102 def commit_ids(self):
103 103 """
104 104 Returns list of commit ids, in ascending order. Being lazy
105 105 attribute allows external tools to inject shas from cache.
106 106 """
107 107 commit_ids = self._get_all_commit_ids()
108 108 self._rebuild_cache(commit_ids)
109 109 return commit_ids
110 110
111 111 def _rebuild_cache(self, commit_ids):
112 112 self._commit_ids = {commit_id: index
113 113 for index, commit_id in enumerate(commit_ids)}
114 114
115 115 @CachedProperty
116 116 def branches(self):
117 117 return self._get_branches()
118 118
119 119 @CachedProperty
120 120 def branches_closed(self):
121 121 return self._get_branches(active=False, closed=True)
122 122
123 123 @CachedProperty
124 124 def branches_all(self):
125 125 all_branches = {}
126 126 all_branches.update(self.branches)
127 127 all_branches.update(self.branches_closed)
128 128 return all_branches
129 129
130 130 def _get_branches(self, active=True, closed=False):
131 131 """
132 132 Gets branches for this repository
133 133 Returns only not closed active branches by default
134 134
135 135 :param active: return also active branches
136 136 :param closed: return also closed branches
137 137
138 138 """
139 139 if self.is_empty():
140 140 return {}
141 141
142 142 def get_name(ctx):
143 143 return ctx[0]
144 144
145 145 _branches = [(n, h,) for n, h in
146 146 self._remote.branches(active, closed).items()]
147 147
148 148 return OrderedDict(sorted(_branches, key=get_name, reverse=False))
149 149
150 150 @CachedProperty
151 151 def tags(self):
152 152 """
153 153 Gets tags for this repository
154 154 """
155 155 return self._get_tags()
156 156
157 157 def _get_tags(self):
158 158 if self.is_empty():
159 159 return {}
160 160
161 161 def get_name(ctx):
162 162 return ctx[0]
163 163
164 164 _tags = [(n, h,) for n, h in
165 165 self._remote.tags().items()]
166 166
167 167 return OrderedDict(sorted(_tags, key=get_name, reverse=True))
168 168
169 169 def tag(self, name, user, commit_id=None, message=None, date=None, **kwargs):
170 170 """
171 171 Creates and returns a tag for the given ``commit_id``.
172 172
173 173 :param name: name for new tag
174 174 :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>"
175 175 :param commit_id: commit id for which new tag would be created
176 176 :param message: message of the tag's commit
177 177 :param date: date of tag's commit
178 178
179 179 :raises TagAlreadyExistError: if tag with same name already exists
180 180 """
181 181 if name in self.tags:
182 182 raise TagAlreadyExistError("Tag %s already exists" % name)
183 183
184 184 commit = self.get_commit(commit_id=commit_id)
185 185 local = kwargs.setdefault('local', False)
186 186
187 187 if message is None:
188 188 message = f"Added tag {name} for commit {commit.short_id}"
189 189
190 190 date, tz = date_to_timestamp_plus_offset(date)
191 191
192 192 self._remote.tag(name, commit.raw_id, message, local, user, date, tz)
193 193 self._remote.invalidate_vcs_cache()
194 194
195 195 # Reinitialize tags
196 196 self._invalidate_prop_cache('tags')
197 197 tag_id = self.tags[name]
198 198
199 199 return self.get_commit(commit_id=tag_id)
200 200
201 201 def remove_tag(self, name, user, message=None, date=None):
202 202 """
203 203 Removes tag with the given `name`.
204 204
205 205 :param name: name of the tag to be removed
206 206 :param user: full username, i.e.: "Joe Doe <joe.doe@example.com>"
207 207 :param message: message of the tag's removal commit
208 208 :param date: date of tag's removal commit
209 209
210 210 :raises TagDoesNotExistError: if tag with given name does not exists
211 211 """
212 212 if name not in self.tags:
213 213 raise TagDoesNotExistError("Tag %s does not exist" % name)
214 214
215 215 if message is None:
216 216 message = "Removed tag %s" % name
217 217 local = False
218 218
219 219 date, tz = date_to_timestamp_plus_offset(date)
220 220
221 221 self._remote.tag(name, nullid, message, local, user, date, tz)
222 222 self._remote.invalidate_vcs_cache()
223 223 self._invalidate_prop_cache('tags')
224 224
225 225 @LazyProperty
226 226 def bookmarks(self):
227 227 """
228 228 Gets bookmarks for this repository
229 229 """
230 230 return self._get_bookmarks()
231 231
232 232 def _get_bookmarks(self):
233 233 if self.is_empty():
234 234 return {}
235 235
236 236 def get_name(ctx):
237 237 return ctx[0]
238 238
239 239 _bookmarks = [
240 240 (n, h) for n, h in
241 241 self._remote.bookmarks().items()]
242 242
243 243 return OrderedDict(sorted(_bookmarks, key=get_name))
244 244
245 245 def _get_all_commit_ids(self):
246 246 return self._remote.get_all_commit_ids('visible')
247 247
248 248 def get_diff(
249 249 self, commit1, commit2, path='', ignore_whitespace=False,
250 250 context=3, path1=None):
251 251 """
252 252 Returns (git like) *diff*, as plain text. Shows changes introduced by
253 253 `commit2` since `commit1`.
254 254
255 255 :param commit1: Entry point from which diff is shown. Can be
256 256 ``self.EMPTY_COMMIT`` - in this case, patch showing all
257 257 the changes since empty state of the repository until `commit2`
258 258 :param commit2: Until which commit changes should be shown.
259 259 :param ignore_whitespace: If set to ``True``, would not show whitespace
260 260 changes. Defaults to ``False``.
261 261 :param context: How many lines before/after changed lines should be
262 262 shown. Defaults to ``3``.
263 263 """
264 264 self._validate_diff_commits(commit1, commit2)
265 265 if path1 is not None and path1 != path:
266 266 raise ValueError("Diff of two different paths not supported.")
267 267
268 268 if path:
269 269 file_filter = [self.path, path]
270 270 else:
271 271 file_filter = None
272 272
273 273 diff = self._remote.diff(
274 274 commit1.raw_id, commit2.raw_id, file_filter=file_filter,
275 275 opt_git=True, opt_ignorews=ignore_whitespace,
276 276 context=context)
277 277 return MercurialDiff(diff)
278 278
279 279 def strip(self, commit_id, branch=None):
280 280 self._remote.strip(commit_id, update=False, backup=False)
281 281
282 282 self._remote.invalidate_vcs_cache()
283 283 # clear cache
284 284 self._invalidate_prop_cache('commit_ids')
285 285
286 286 return len(self.commit_ids)
287 287
288 288 def verify(self):
289 289 verify = self._remote.verify()
290 290
291 291 self._remote.invalidate_vcs_cache()
292 292 return verify
293 293
294 294 def hg_update_cache(self):
295 295 update_cache = self._remote.hg_update_cache()
296 296
297 297 self._remote.invalidate_vcs_cache()
298 298 return update_cache
299 299
300 300 def hg_rebuild_fn_cache(self):
301 301 update_cache = self._remote.hg_rebuild_fn_cache()
302 302
303 303 self._remote.invalidate_vcs_cache()
304 304 return update_cache
305 305
306 306 def get_common_ancestor(self, commit_id1, commit_id2, repo2):
307 307 log.debug('Calculating common ancestor between %sc1:%s and %sc2:%s',
308 308 self, commit_id1, repo2, commit_id2)
309 309
310 310 if commit_id1 == commit_id2:
311 311 return commit_id1
312 312
313 313 ancestors = self._remote.revs_from_revspec(
314 314 "ancestor(id(%s), id(%s))", commit_id1, commit_id2,
315 315 other_path=repo2.path)
316 316
317 317 ancestor_id = repo2[ancestors[0]].raw_id if ancestors else None
318 318
319 319 log.debug('Found common ancestor with sha: %s', ancestor_id)
320 320 return ancestor_id
321 321
322 322 def compare(self, commit_id1, commit_id2, repo2, merge, pre_load=None):
323 323 if commit_id1 == commit_id2:
324 324 commits = []
325 325 else:
326 326 if merge:
327 327 indexes = self._remote.revs_from_revspec(
328 328 "ancestors(id(%s)) - ancestors(id(%s)) - id(%s)",
329 329 commit_id2, commit_id1, commit_id1, other_path=repo2.path)
330 330 else:
331 331 indexes = self._remote.revs_from_revspec(
332 332 "id(%s)..id(%s) - id(%s)", commit_id1, commit_id2,
333 333 commit_id1, other_path=repo2.path)
334 334
335 335 commits = [repo2.get_commit(commit_idx=idx, pre_load=pre_load)
336 336 for idx in indexes]
337 337
338 338 return commits
339 339
340 340 @staticmethod
341 341 def check_url(url, config):
342 342 """
343 343 Function will check given url and try to verify if it's a valid
344 344 link. Sometimes it may happened that mercurial will issue basic
345 345 auth request that can cause whole API to hang when used from python
346 346 or other external calls.
347 347
348 348 On failures it'll raise urllib2.HTTPError, exception is also thrown
349 349 when the return code is non 200
350 350 """
351 351 # check first if it's not an local url
352 352 if os.path.isdir(url) or url.startswith('file:'):
353 353 return True
354 354
355 355 # Request the _remote to verify the url
356 356 return connection.Hg.check_url(url, config.serialize())
357 357
358 358 @staticmethod
359 359 def is_valid_repository(path):
360 360 return os.path.isdir(os.path.join(path, '.hg'))
361 361
362 362 def _init_repo(self, create, src_url=None, do_workspace_checkout=False):
363 363 """
364 364 Function will check for mercurial repository in given path. If there
365 365 is no repository in that path it will raise an exception unless
366 366 `create` parameter is set to True - in that case repository would
367 367 be created.
368 368
369 369 If `src_url` is given, would try to clone repository from the
370 370 location at given clone_point. Additionally it'll make update to
371 371 working copy accordingly to `do_workspace_checkout` flag.
372 372 """
373 373 if create and os.path.exists(self.path):
374 374 raise RepositoryError(
375 375 f"Cannot create repository at {self.path}, location already exist")
376 376
377 377 if src_url:
378 378 url = str(self._get_url(src_url))
379 379 MercurialRepository.check_url(url, self.config)
380 380
381 381 self._remote.clone(url, self.path, do_workspace_checkout)
382 382
383 383 # Don't try to create if we've already cloned repo
384 384 create = False
385 385
386 386 if create:
387 387 os.makedirs(self.path, mode=0o755)
388 388
389 389 self._remote.localrepository(create)
390 390
391 391 @LazyProperty
392 392 def in_memory_commit(self):
393 393 return MercurialInMemoryCommit(self)
394 394
395 395 @LazyProperty
396 396 def description(self):
397 397 description = self._remote.get_config_value(
398 398 'web', 'description', untrusted=True)
399 399 return safe_str(description or self.DEFAULT_DESCRIPTION)
400 400
401 401 @LazyProperty
402 402 def contact(self):
403 403 contact = (
404 404 self._remote.get_config_value("web", "contact") or
405 405 self._remote.get_config_value("ui", "username"))
406 406 return safe_str(contact or self.DEFAULT_CONTACT)
407 407
408 408 @LazyProperty
409 409 def last_change(self):
410 410 """
411 411 Returns last change made on this repository as
412 412 `datetime.datetime` object.
413 413 """
414 414 try:
415 415 return self.get_commit().date
416 416 except RepositoryError:
417 417 tzoffset = makedate()[1]
418 418 return utcdate_fromtimestamp(self._get_fs_mtime(), tzoffset)
419 419
420 420 def _get_fs_mtime(self):
421 421 # fallback to filesystem
422 422 cl_path = os.path.join(self.path, '.hg', "00changelog.i")
423 423 st_path = os.path.join(self.path, '.hg', "store")
424 424 if os.path.exists(cl_path):
425 425 return os.stat(cl_path).st_mtime
426 426 else:
427 427 return os.stat(st_path).st_mtime
428 428
429 429 def _get_url(self, url):
430 430 """
431 431 Returns normalized url. If schema is not given, would fall
432 432 to filesystem
433 433 (``file:///``) schema.
434 434 """
435 435 if url != 'default' and '://' not in url:
436 436 url = "file:" + urllib.request.pathname2url(url)
437 437 return url
438 438
439 439 def get_hook_location(self):
440 440 """
441 441 returns absolute path to location where hooks are stored
442 442 """
443 443 return os.path.join(self.path, '.hg', '.hgrc')
444 444
445 445 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None,
446 446 translate_tag=None, maybe_unreachable=False, reference_obj=None):
447 447 """
448 448 Returns ``MercurialCommit`` object representing repository's
449 449 commit at the given `commit_id` or `commit_idx`.
450 450 """
451 451 if self.is_empty():
452 452 raise EmptyRepositoryError("There are no commits yet")
453 453
454 454 if commit_id is not None:
455 455 self._validate_commit_id(commit_id)
456 456 try:
457 457 # we have cached idx, use it without contacting the remote
458 458 idx = self._commit_ids[commit_id]
459 459 return MercurialCommit(self, commit_id, idx, pre_load=pre_load)
460 460 except KeyError:
461 461 pass
462 462
463 463 elif commit_idx is not None:
464 464 self._validate_commit_idx(commit_idx)
465 465 try:
466 466 _commit_id = self.commit_ids[commit_idx]
467 467 if commit_idx < 0:
468 468 commit_idx = self.commit_ids.index(_commit_id)
469 469
470 470 return MercurialCommit(self, _commit_id, commit_idx, pre_load=pre_load)
471 471 except IndexError:
472 472 commit_id = commit_idx
473 473 else:
474 474 commit_id = "tip"
475 475
476 476 # case here is no cached version, do an actual lookup instead
477 477 try:
478 478 raw_id, idx = self._remote.lookup(commit_id, both=True)
479 479 except CommitDoesNotExistError:
480 480 msg = "Commit {} does not exist for `{}`".format(
481 481 *map(safe_str, [commit_id, self.name]))
482 482 raise CommitDoesNotExistError(msg)
483 483
484 484 return MercurialCommit(self, raw_id, idx, pre_load=pre_load)
485 485
486 486 def get_commits(
487 487 self, start_id=None, end_id=None, start_date=None, end_date=None,
488 488 branch_name=None, show_hidden=False, pre_load=None, translate_tags=None):
489 489 """
490 490 Returns generator of ``MercurialCommit`` objects from start to end
491 491 (both are inclusive)
492 492
493 493 :param start_id: None, str(commit_id)
494 494 :param end_id: None, str(commit_id)
495 495 :param start_date: if specified, commits with commit date less than
496 496 ``start_date`` would be filtered out from returned set
497 497 :param end_date: if specified, commits with commit date greater than
498 498 ``end_date`` would be filtered out from returned set
499 499 :param branch_name: if specified, commits not reachable from given
500 500 branch would be filtered out from returned set
501 501 :param show_hidden: Show hidden commits such as obsolete or hidden from
502 502 Mercurial evolve
503 503 :raise BranchDoesNotExistError: If given ``branch_name`` does not
504 504 exist.
505 505 :raise CommitDoesNotExistError: If commit for given ``start`` or
506 506 ``end`` could not be found.
507 507 """
508 508 # actually we should check now if it's not an empty repo
509 509 if self.is_empty():
510 510 raise EmptyRepositoryError("There are no commits yet")
511 511 self._validate_branch_name(branch_name)
512 512
513 513 branch_ancestors = False
514 514 if start_id is not None:
515 515 self._validate_commit_id(start_id)
516 516 c_start = self.get_commit(commit_id=start_id)
517 517 start_pos = self._commit_ids[c_start.raw_id]
518 518 else:
519 519 start_pos = None
520 520
521 521 if end_id is not None:
522 522 self._validate_commit_id(end_id)
523 523 c_end = self.get_commit(commit_id=end_id)
524 524 end_pos = max(0, self._commit_ids[c_end.raw_id])
525 525 else:
526 526 end_pos = None
527 527
528 528 if None not in [start_id, end_id] and start_pos > end_pos:
529 529 raise RepositoryError(
530 530 "Start commit '%s' cannot be after end commit '%s'" %
531 531 (start_id, end_id))
532 532
533 533 if end_pos is not None:
534 534 end_pos += 1
535 535
536 536 commit_filter = []
537 537
538 538 if branch_name and not branch_ancestors:
539 539 commit_filter.append(f'branch("{branch_name}")')
540 540 elif branch_name and branch_ancestors:
541 541 commit_filter.append(f'ancestors(branch("{branch_name}"))')
542 542
543 543 if start_date and not end_date:
544 544 commit_filter.append(f'date(">{start_date}")')
545 545 if end_date and not start_date:
546 546 commit_filter.append(f'date("<{end_date}")')
547 547 if start_date and end_date:
548 548 commit_filter.append(
549 549 f'date(">{start_date}") and date("<{end_date}")')
550 550
551 551 if not show_hidden:
552 552 commit_filter.append('not obsolete()')
553 553 commit_filter.append('not hidden()')
554 554
555 555 # TODO: johbo: Figure out a simpler way for this solution
556 556 collection_generator = CollectionGenerator
557 557 if commit_filter:
558 558 commit_filter = ' and '.join(map(safe_str, commit_filter))
559 559 revisions = self._remote.rev_range([commit_filter])
560 560 collection_generator = MercurialIndexBasedCollectionGenerator
561 561 else:
562 562 revisions = self.commit_ids
563 563
564 564 if start_pos or end_pos:
565 565 revisions = revisions[start_pos:end_pos]
566 566
567 567 return collection_generator(self, revisions, pre_load=pre_load)
568 568
569 569 def pull(self, url, commit_ids=None):
570 570 """
571 571 Pull changes from external location.
572 572
573 573 :param commit_ids: Optional. Can be set to a list of commit ids
574 574 which shall be pulled from the other repository.
575 575 """
576 576 url = self._get_url(url)
577 577 self._remote.pull(url, commit_ids=commit_ids)
578 578 self._remote.invalidate_vcs_cache()
579 579
580 580 def fetch(self, url, commit_ids=None, **kwargs):
581 581 """
582 582 Backward compatibility with GIT fetch==pull
583 583 """
584 584 return self.pull(url, commit_ids=commit_ids)
585 585
586 def push(self, url):
586 def push(self, url, **kwargs):
587 587 url = self._get_url(url)
588 588 self._remote.sync_push(url)
589 589
590 590 def _local_clone(self, clone_path):
591 591 """
592 592 Create a local clone of the current repo.
593 593 """
594 594 self._remote.clone(self.path, clone_path, update_after_clone=True,
595 595 hooks=False)
596 596
597 597 def _update(self, revision, clean=False):
598 598 """
599 599 Update the working copy to the specified revision.
600 600 """
601 601 log.debug('Doing checkout to commit: `%s` for %s', revision, self)
602 602 self._remote.update(revision, clean=clean)
603 603
604 604 def _identify(self):
605 605 """
606 606 Return the current state of the working directory.
607 607 """
608 608 return self._remote.identify().strip().rstrip('+')
609 609
610 610 def _heads(self, branch=None):
611 611 """
612 612 Return the commit ids of the repository heads.
613 613 """
614 614 return self._remote.heads(branch=branch).strip().split(' ')
615 615
616 616 def _ancestor(self, revision1, revision2):
617 617 """
618 618 Return the common ancestor of the two revisions.
619 619 """
620 620 return self._remote.ancestor(revision1, revision2)
621 621
622 622 def _local_push(
623 623 self, revision, repository_path, push_branches=False,
624 624 enable_hooks=False):
625 625 """
626 626 Push the given revision to the specified repository.
627 627
628 628 :param push_branches: allow to create branches in the target repo.
629 629 """
630 630 self._remote.push(
631 631 [revision], repository_path, hooks=enable_hooks,
632 632 push_branches=push_branches)
633 633
634 634 def _local_merge(self, target_ref, merge_message, user_name, user_email,
635 635 source_ref, use_rebase=False, close_commit_id=None, dry_run=False):
636 636 """
637 637 Merge the given source_revision into the checked out revision.
638 638
639 639 Returns the commit id of the merge and a boolean indicating if the
640 640 commit needs to be pushed.
641 641 """
642 642
643 643 source_ref_commit_id = source_ref.commit_id
644 644 target_ref_commit_id = target_ref.commit_id
645 645
646 646 # update our workdir to target ref, for proper merge
647 647 self._update(target_ref_commit_id, clean=True)
648 648
649 649 ancestor = self._ancestor(target_ref_commit_id, source_ref_commit_id)
650 650 is_the_same_branch = self._is_the_same_branch(target_ref, source_ref)
651 651
652 652 if close_commit_id:
653 653 # NOTE(marcink): if we get the close commit, this is our new source
654 654 # which will include the close commit itself.
655 655 source_ref_commit_id = close_commit_id
656 656
657 657 if ancestor == source_ref_commit_id:
658 658 # Nothing to do, the changes were already integrated
659 659 return target_ref_commit_id, False
660 660
661 661 elif ancestor == target_ref_commit_id and is_the_same_branch:
662 662 # In this case we should force a commit message
663 663 return source_ref_commit_id, True
664 664
665 665 unresolved = None
666 666 if use_rebase:
667 667 try:
668 668 bookmark_name = f'rcbook{source_ref_commit_id}{target_ref_commit_id}'
669 669 self.bookmark(bookmark_name, revision=source_ref.commit_id)
670 670 self._remote.rebase(
671 671 source=source_ref_commit_id, dest=target_ref_commit_id)
672 672 self._remote.invalidate_vcs_cache()
673 673 self._update(bookmark_name, clean=True)
674 674 return self._identify(), True
675 675 except RepositoryError as e:
676 676 # The rebase-abort may raise another exception which 'hides'
677 677 # the original one, therefore we log it here.
678 678 log.exception('Error while rebasing shadow repo during merge.')
679 679 if 'unresolved conflicts' in safe_str(e):
680 680 unresolved = self._remote.get_unresolved_files()
681 681 log.debug('unresolved files: %s', unresolved)
682 682
683 683 # Cleanup any rebase leftovers
684 684 self._remote.invalidate_vcs_cache()
685 685 self._remote.rebase(abort=True)
686 686 self._remote.invalidate_vcs_cache()
687 687 self._remote.update(clean=True)
688 688 if unresolved:
689 689 raise UnresolvedFilesInRepo(unresolved)
690 690 else:
691 691 raise
692 692 else:
693 693 try:
694 694 self._remote.merge(source_ref_commit_id)
695 695 self._remote.invalidate_vcs_cache()
696 696 self._remote.commit(
697 697 message=safe_str(merge_message),
698 698 username=safe_str(f'{user_name} <{user_email}>'))
699 699 self._remote.invalidate_vcs_cache()
700 700 return self._identify(), True
701 701 except RepositoryError as e:
702 702 # The merge-abort may raise another exception which 'hides'
703 703 # the original one, therefore we log it here.
704 704 log.exception('Error while merging shadow repo during merge.')
705 705 if 'unresolved merge conflicts' in safe_str(e):
706 706 unresolved = self._remote.get_unresolved_files()
707 707 log.debug('unresolved files: %s', unresolved)
708 708
709 709 # Cleanup any merge leftovers
710 710 self._remote.update(clean=True)
711 711 if unresolved:
712 712 raise UnresolvedFilesInRepo(unresolved)
713 713 else:
714 714 raise
715 715
716 716 def _local_close(self, target_ref, user_name, user_email,
717 717 source_ref, close_message=''):
718 718 """
719 719 Close the branch of the given source_revision
720 720
721 721 Returns the commit id of the close and a boolean indicating if the
722 722 commit needs to be pushed.
723 723 """
724 724 self._update(source_ref.commit_id)
725 725 message = close_message or f"Closing branch: `{source_ref.name}`"
726 726 try:
727 727 self._remote.commit(
728 728 message=safe_str(message),
729 729 username=safe_str(f'{user_name} <{user_email}>'),
730 730 close_branch=True)
731 731 self._remote.invalidate_vcs_cache()
732 732 return self._identify(), True
733 733 except RepositoryError:
734 734 # Cleanup any commit leftovers
735 735 self._remote.update(clean=True)
736 736 raise
737 737
738 738 def _is_the_same_branch(self, target_ref, source_ref):
739 739 return (
740 740 self._get_branch_name(target_ref) ==
741 741 self._get_branch_name(source_ref))
742 742
743 743 def _get_branch_name(self, ref):
744 744 if ref.type == 'branch':
745 745 return ref.name
746 746 return self._remote.ctx_branch(ref.commit_id)
747 747
748 748 def _maybe_prepare_merge_workspace(
749 749 self, repo_id, workspace_id, unused_target_ref, unused_source_ref):
750 750 shadow_repository_path = self._get_shadow_repository_path(
751 751 self.path, repo_id, workspace_id)
752 752 if not os.path.exists(shadow_repository_path):
753 753 self._local_clone(shadow_repository_path)
754 754 log.debug(
755 755 'Prepared shadow repository in %s', shadow_repository_path)
756 756
757 757 return shadow_repository_path
758 758
759 759 def _merge_repo(self, repo_id, workspace_id, target_ref,
760 760 source_repo, source_ref, merge_message,
761 761 merger_name, merger_email, dry_run=False,
762 762 use_rebase=False, close_branch=False):
763 763
764 764 log.debug('Executing merge_repo with %s strategy, dry_run mode:%s',
765 765 'rebase' if use_rebase else 'merge', dry_run)
766 766
767 767 if target_ref.commit_id not in self._heads():
768 768 return MergeResponse(
769 769 False, False, None, MergeFailureReason.TARGET_IS_NOT_HEAD,
770 770 metadata={'target_ref': target_ref})
771 771
772 772 try:
773 773 if target_ref.type == 'branch' and len(self._heads(target_ref.name)) != 1:
774 774 heads_all = self._heads(target_ref.name)
775 775 max_heads = 10
776 776 if len(heads_all) > max_heads:
777 777 heads = '\n,'.join(
778 778 heads_all[:max_heads] +
779 779 [f'and {len(heads_all)-max_heads} more.'])
780 780 else:
781 781 heads = '\n,'.join(heads_all)
782 782 metadata = {
783 783 'target_ref': target_ref,
784 784 'source_ref': source_ref,
785 785 'heads': heads
786 786 }
787 787 return MergeResponse(
788 788 False, False, None,
789 789 MergeFailureReason.HG_TARGET_HAS_MULTIPLE_HEADS,
790 790 metadata=metadata)
791 791 except CommitDoesNotExistError:
792 792 log.exception('Failure when looking up branch heads on hg target')
793 793 return MergeResponse(
794 794 False, False, None, MergeFailureReason.MISSING_TARGET_REF,
795 795 metadata={'target_ref': target_ref})
796 796
797 797 shadow_repository_path = self._maybe_prepare_merge_workspace(
798 798 repo_id, workspace_id, target_ref, source_ref)
799 799 shadow_repo = self.get_shadow_instance(shadow_repository_path)
800 800
801 801 log.debug('Pulling in target reference %s', target_ref)
802 802 self._validate_pull_reference(target_ref)
803 803 shadow_repo._local_pull(self.path, target_ref)
804 804
805 805 try:
806 806 log.debug('Pulling in source reference %s', source_ref)
807 807 source_repo._validate_pull_reference(source_ref)
808 808 shadow_repo._local_pull(source_repo.path, source_ref)
809 809 except CommitDoesNotExistError:
810 810 log.exception('Failure when doing local pull on hg shadow repo')
811 811 return MergeResponse(
812 812 False, False, None, MergeFailureReason.MISSING_SOURCE_REF,
813 813 metadata={'source_ref': source_ref})
814 814
815 815 merge_ref = None
816 816 merge_commit_id = None
817 817 close_commit_id = None
818 818 merge_failure_reason = MergeFailureReason.NONE
819 819 metadata = {}
820 820
821 821 # enforce that close branch should be used only in case we source from
822 822 # an actual Branch
823 823 close_branch = close_branch and source_ref.type == 'branch'
824 824
825 825 # don't allow to close branch if source and target are the same
826 826 close_branch = close_branch and source_ref.name != target_ref.name
827 827
828 828 needs_push_on_close = False
829 829 if close_branch and not use_rebase and not dry_run:
830 830 try:
831 831 close_commit_id, needs_push_on_close = shadow_repo._local_close(
832 832 target_ref, merger_name, merger_email, source_ref)
833 833 merge_possible = True
834 834 except RepositoryError:
835 835 log.exception('Failure when doing close branch on '
836 836 'shadow repo: %s', shadow_repo)
837 837 merge_possible = False
838 838 merge_failure_reason = MergeFailureReason.MERGE_FAILED
839 839 else:
840 840 merge_possible = True
841 841
842 842 needs_push = False
843 843 if merge_possible:
844 844
845 845 try:
846 846 merge_commit_id, needs_push = shadow_repo._local_merge(
847 847 target_ref, merge_message, merger_name, merger_email,
848 848 source_ref, use_rebase=use_rebase,
849 849 close_commit_id=close_commit_id, dry_run=dry_run)
850 850 merge_possible = True
851 851
852 852 # read the state of the close action, if it
853 853 # maybe required a push
854 854 needs_push = needs_push or needs_push_on_close
855 855
856 856 # Set a bookmark pointing to the merge commit. This bookmark
857 857 # may be used to easily identify the last successful merge
858 858 # commit in the shadow repository.
859 859 shadow_repo.bookmark('pr-merge', revision=merge_commit_id)
860 860 merge_ref = Reference('book', 'pr-merge', merge_commit_id)
861 861 except SubrepoMergeError:
862 862 log.exception(
863 863 'Subrepo merge error during local merge on hg shadow repo.')
864 864 merge_possible = False
865 865 merge_failure_reason = MergeFailureReason.SUBREPO_MERGE_FAILED
866 866 needs_push = False
867 867 except RepositoryError as e:
868 868 log.exception('Failure when doing local merge on hg shadow repo')
869 869 metadata['unresolved_files'] = 'no unresolved files found'
870 870
871 871 if isinstance(e, UnresolvedFilesInRepo):
872 872 all_conflicts = list(e.args[0])
873 873 max_conflicts = 20
874 874 if len(all_conflicts) > max_conflicts:
875 875 conflicts = all_conflicts[:max_conflicts] \
876 876 + [f'and {len(all_conflicts)-max_conflicts} more.']
877 877 else:
878 878 conflicts = all_conflicts
879 879 metadata['unresolved_files'] = \
880 880 '\n* conflict: ' + \
881 881 ('\n * conflict: '.join(conflicts))
882 882
883 883 merge_possible = False
884 884 merge_failure_reason = MergeFailureReason.MERGE_FAILED
885 885 needs_push = False
886 886
887 887 if merge_possible and not dry_run:
888 888 if needs_push:
889 889 # In case the target is a bookmark, update it, so after pushing
890 890 # the bookmarks is also updated in the target.
891 891 if target_ref.type == 'book':
892 892 shadow_repo.bookmark(
893 893 target_ref.name, revision=merge_commit_id)
894 894 try:
895 895 shadow_repo_with_hooks = self.get_shadow_instance(
896 896 shadow_repository_path,
897 897 enable_hooks=True)
898 898 # This is the actual merge action, we push from shadow
899 899 # into origin.
900 900 # Note: the push_branches option will push any new branch
901 901 # defined in the source repository to the target. This may
902 902 # be dangerous as branches are permanent in Mercurial.
903 903 # This feature was requested in issue #441.
904 904 shadow_repo_with_hooks._local_push(
905 905 merge_commit_id, self.path, push_branches=True,
906 906 enable_hooks=True)
907 907
908 908 # maybe we also need to push the close_commit_id
909 909 if close_commit_id:
910 910 shadow_repo_with_hooks._local_push(
911 911 close_commit_id, self.path, push_branches=True,
912 912 enable_hooks=True)
913 913 merge_succeeded = True
914 914 except RepositoryError:
915 915 log.exception(
916 916 'Failure when doing local push from the shadow '
917 917 'repository to the target repository at %s.', self.path)
918 918 merge_succeeded = False
919 919 merge_failure_reason = MergeFailureReason.PUSH_FAILED
920 920 metadata['target'] = 'hg shadow repo'
921 921 metadata['merge_commit'] = merge_commit_id
922 922 else:
923 923 merge_succeeded = True
924 924 else:
925 925 merge_succeeded = False
926 926
927 927 return MergeResponse(
928 928 merge_possible, merge_succeeded, merge_ref, merge_failure_reason,
929 929 metadata=metadata)
930 930
931 931 def get_shadow_instance(self, shadow_repository_path, enable_hooks=False, cache=False):
932 932 config = self.config.copy()
933 933 if not enable_hooks:
934 934 config.clear_section('hooks')
935 935 return MercurialRepository(shadow_repository_path, config, with_wire={"cache": cache})
936 936
937 937 def _validate_pull_reference(self, reference):
938 938 if not (reference.name in self.bookmarks or
939 939 reference.name in self.branches or
940 940 self.get_commit(reference.commit_id)):
941 941 raise CommitDoesNotExistError(
942 942 'Unknown branch, bookmark or commit id')
943 943
944 944 def _local_pull(self, repository_path, reference):
945 945 """
946 946 Fetch a branch, bookmark or commit from a local repository.
947 947 """
948 948 repository_path = os.path.abspath(repository_path)
949 949 if repository_path == self.path:
950 950 raise ValueError('Cannot pull from the same repository')
951 951
952 952 reference_type_to_option_name = {
953 953 'book': 'bookmark',
954 954 'branch': 'branch',
955 955 }
956 956 option_name = reference_type_to_option_name.get(
957 957 reference.type, 'revision')
958 958
959 959 if option_name == 'revision':
960 960 ref = reference.commit_id
961 961 else:
962 962 ref = reference.name
963 963
964 964 options = {option_name: [ref]}
965 965 self._remote.pull_cmd(repository_path, hooks=False, **options)
966 966 self._remote.invalidate_vcs_cache()
967 967
968 968 def bookmark(self, bookmark, revision=None):
969 969 if isinstance(bookmark, str):
970 970 bookmark = safe_str(bookmark)
971 971 self._remote.bookmark(bookmark, revision=revision)
972 972 self._remote.invalidate_vcs_cache()
973 973
974 974 def get_path_permissions(self, username):
975 975 hgacl_file = os.path.join(self.path, '.hg/hgacl')
976 976
977 977 def read_patterns(suffix):
978 978 svalue = None
979 979 for section, option in [
980 980 ('narrowacl', username + suffix),
981 981 ('narrowacl', 'default' + suffix),
982 982 ('narrowhgacl', username + suffix),
983 983 ('narrowhgacl', 'default' + suffix)
984 984 ]:
985 985 try:
986 986 svalue = hgacl.get(section, option)
987 987 break # stop at the first value we find
988 988 except configparser.NoOptionError:
989 989 pass
990 990 if not svalue:
991 991 return None
992 992 result = ['/']
993 993 for pattern in svalue.split():
994 994 result.append(pattern)
995 995 if '*' not in pattern and '?' not in pattern:
996 996 result.append(pattern + '/*')
997 997 return result
998 998
999 999 if os.path.exists(hgacl_file):
1000 1000 try:
1001 1001 hgacl = configparser.RawConfigParser()
1002 1002 hgacl.read(hgacl_file)
1003 1003
1004 1004 includes = read_patterns('.includes')
1005 1005 excludes = read_patterns('.excludes')
1006 1006 return BasePathPermissionChecker.create_from_patterns(
1007 1007 includes, excludes)
1008 1008 except BaseException as e:
1009 1009 msg = 'Cannot read ACL settings from {} on {}: {}'.format(
1010 1010 hgacl_file, self.name, e)
1011 1011 raise exceptions.RepositoryRequirementError(msg)
1012 1012 else:
1013 1013 return None
1014 1014
1015 1015
1016 1016 class MercurialIndexBasedCollectionGenerator(CollectionGenerator):
1017 1017
1018 1018 def _commit_factory(self, commit_id):
1019 1019 if isinstance(commit_id, int):
1020 1020 return self.repo.get_commit(
1021 1021 commit_idx=commit_id, pre_load=self.pre_load)
1022 1022 else:
1023 1023 return self.repo.get_commit(
1024 1024 commit_id=commit_id, pre_load=self.pre_load)
@@ -1,1044 +1,1044 b''
1 1 # Copyright (C) 2010-2023 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 """
20 20 Scm model for RhodeCode
21 21 """
22 22
23 23 import os.path
24 24 import traceback
25 25 import logging
26 26 import io
27 27
28 28 from sqlalchemy import func
29 29 from zope.cachedescriptors.property import Lazy as LazyProperty
30 30
31 31 import rhodecode
32 32 from rhodecode.lib.str_utils import safe_bytes
33 33 from rhodecode.lib.vcs import get_backend
34 34 from rhodecode.lib.vcs.exceptions import RepositoryError, NodeNotChangedError
35 35 from rhodecode.lib.vcs.nodes import FileNode
36 36 from rhodecode.lib.vcs.backends.base import EmptyCommit
37 37 from rhodecode.lib import helpers as h, rc_cache
38 38 from rhodecode.lib.auth import (
39 39 HasRepoPermissionAny, HasRepoGroupPermissionAny,
40 40 HasUserGroupPermissionAny)
41 41 from rhodecode.lib.exceptions import NonRelativePathError, IMCCommitError
42 42 from rhodecode.lib import hooks_utils
43 43 from rhodecode.lib.utils import (
44 44 get_filesystem_repos, make_db_config)
45 45 from rhodecode.lib.str_utils import safe_str
46 46 from rhodecode.lib.system_info import get_system_info
47 47 from rhodecode.model import BaseModel
48 48 from rhodecode.model.db import (
49 49 or_, false, null,
50 50 Repository, CacheKey, UserFollowing, UserLog, User, RepoGroup,
51 51 PullRequest, FileStore)
52 52 from rhodecode.model.settings import VcsSettingsModel
53 53 from rhodecode.model.validation_schema.validators import url_validator, InvalidCloneUrl
54 54
55 55 log = logging.getLogger(__name__)
56 56
57 57
58 58 class UserTemp(object):
59 59 def __init__(self, user_id):
60 60 self.user_id = user_id
61 61
62 62 def __repr__(self):
63 63 return "<{}('id:{}')>".format(self.__class__.__name__, self.user_id)
64 64
65 65
66 66 class RepoTemp(object):
67 67 def __init__(self, repo_id):
68 68 self.repo_id = repo_id
69 69
70 70 def __repr__(self):
71 71 return "<{}('id:{}')>".format(self.__class__.__name__, self.repo_id)
72 72
73 73
74 74 class SimpleCachedRepoList(object):
75 75 """
76 76 Lighter version of of iteration of repos without the scm initialisation,
77 77 and with cache usage
78 78 """
79 79 def __init__(self, db_repo_list, repos_path, order_by=None, perm_set=None):
80 80 self.db_repo_list = db_repo_list
81 81 self.repos_path = repos_path
82 82 self.order_by = order_by
83 83 self.reversed = (order_by or '').startswith('-')
84 84 if not perm_set:
85 85 perm_set = ['repository.read', 'repository.write',
86 86 'repository.admin']
87 87 self.perm_set = perm_set
88 88
89 89 def __len__(self):
90 90 return len(self.db_repo_list)
91 91
92 92 def __repr__(self):
93 93 return '<{} ({})>'.format(self.__class__.__name__, self.__len__())
94 94
95 95 def __iter__(self):
96 96 for dbr in self.db_repo_list:
97 97 # check permission at this level
98 98 has_perm = HasRepoPermissionAny(*self.perm_set)(
99 99 dbr.repo_name, 'SimpleCachedRepoList check')
100 100 if not has_perm:
101 101 continue
102 102
103 103 tmp_d = {
104 104 'name': dbr.repo_name,
105 105 'dbrepo': dbr.get_dict(),
106 106 'dbrepo_fork': dbr.fork.get_dict() if dbr.fork else {}
107 107 }
108 108 yield tmp_d
109 109
110 110
111 111 class _PermCheckIterator(object):
112 112
113 113 def __init__(
114 114 self, obj_list, obj_attr, perm_set, perm_checker,
115 115 extra_kwargs=None):
116 116 """
117 117 Creates iterator from given list of objects, additionally
118 118 checking permission for them from perm_set var
119 119
120 120 :param obj_list: list of db objects
121 121 :param obj_attr: attribute of object to pass into perm_checker
122 122 :param perm_set: list of permissions to check
123 123 :param perm_checker: callable to check permissions against
124 124 """
125 125 self.obj_list = obj_list
126 126 self.obj_attr = obj_attr
127 127 self.perm_set = perm_set
128 128 self.perm_checker = perm_checker(*self.perm_set)
129 129 self.extra_kwargs = extra_kwargs or {}
130 130
131 131 def __len__(self):
132 132 return len(self.obj_list)
133 133
134 134 def __repr__(self):
135 135 return '<{} ({})>'.format(self.__class__.__name__, self.__len__())
136 136
137 137 def __iter__(self):
138 138 for db_obj in self.obj_list:
139 139 # check permission at this level
140 140 # NOTE(marcink): the __dict__.get() is ~4x faster then getattr()
141 141 name = db_obj.__dict__.get(self.obj_attr, None)
142 142 if not self.perm_checker(name, self.__class__.__name__, **self.extra_kwargs):
143 143 continue
144 144
145 145 yield db_obj
146 146
147 147
148 148 class RepoList(_PermCheckIterator):
149 149
150 150 def __init__(self, db_repo_list, perm_set=None, extra_kwargs=None):
151 151 if not perm_set:
152 152 perm_set = ['repository.read', 'repository.write', 'repository.admin']
153 153
154 154 super().__init__(
155 155 obj_list=db_repo_list,
156 156 obj_attr='_repo_name', perm_set=perm_set,
157 157 perm_checker=HasRepoPermissionAny,
158 158 extra_kwargs=extra_kwargs)
159 159
160 160
161 161 class RepoGroupList(_PermCheckIterator):
162 162
163 163 def __init__(self, db_repo_group_list, perm_set=None, extra_kwargs=None):
164 164 if not perm_set:
165 165 perm_set = ['group.read', 'group.write', 'group.admin']
166 166
167 167 super().__init__(
168 168 obj_list=db_repo_group_list,
169 169 obj_attr='_group_name', perm_set=perm_set,
170 170 perm_checker=HasRepoGroupPermissionAny,
171 171 extra_kwargs=extra_kwargs)
172 172
173 173
174 174 class UserGroupList(_PermCheckIterator):
175 175
176 176 def __init__(self, db_user_group_list, perm_set=None, extra_kwargs=None):
177 177 if not perm_set:
178 178 perm_set = ['usergroup.read', 'usergroup.write', 'usergroup.admin']
179 179
180 180 super().__init__(
181 181 obj_list=db_user_group_list,
182 182 obj_attr='users_group_name', perm_set=perm_set,
183 183 perm_checker=HasUserGroupPermissionAny,
184 184 extra_kwargs=extra_kwargs)
185 185
186 186
187 187 class ScmModel(BaseModel):
188 188 """
189 189 Generic Scm Model
190 190 """
191 191
192 192 @LazyProperty
193 193 def repos_path(self):
194 194 """
195 195 Gets the repositories root path from database
196 196 """
197 197
198 198 settings_model = VcsSettingsModel(sa=self.sa)
199 199 return settings_model.get_repos_location()
200 200
201 201 def repo_scan(self, repos_path=None):
202 202 """
203 203 Listing of repositories in given path. This path should not be a
204 204 repository itself. Return a dictionary of repository objects
205 205
206 206 :param repos_path: path to directory containing repositories
207 207 """
208 208
209 209 if repos_path is None:
210 210 repos_path = self.repos_path
211 211
212 212 log.info('scanning for repositories in %s', repos_path)
213 213
214 214 config = make_db_config()
215 215 config.set('extensions', 'largefiles', '')
216 216 repos = {}
217 217
218 218 for name, path in get_filesystem_repos(repos_path, recursive=True):
219 219 # name need to be decomposed and put back together using the /
220 220 # since this is internal storage separator for rhodecode
221 221 name = Repository.normalize_repo_name(name)
222 222
223 223 try:
224 224 if name in repos:
225 225 raise RepositoryError('Duplicate repository name %s '
226 226 'found in %s' % (name, path))
227 227 elif path[0] in rhodecode.BACKENDS:
228 228 backend = get_backend(path[0])
229 229 repos[name] = backend(path[1], config=config,
230 230 with_wire={"cache": False})
231 231 except OSError:
232 232 continue
233 233 except RepositoryError:
234 234 log.exception('Failed to create a repo')
235 235 continue
236 236
237 237 log.debug('found %s paths with repositories', len(repos))
238 238 return repos
239 239
240 240 def get_repos(self, all_repos=None, sort_key=None):
241 241 """
242 242 Get all repositories from db and for each repo create it's
243 243 backend instance and fill that backed with information from database
244 244
245 245 :param all_repos: list of repository names as strings
246 246 give specific repositories list, good for filtering
247 247
248 248 :param sort_key: initial sorting of repositories
249 249 """
250 250 if all_repos is None:
251 251 all_repos = self.sa.query(Repository)\
252 252 .filter(Repository.group_id == null())\
253 253 .order_by(func.lower(Repository.repo_name)).all()
254 254 repo_iter = SimpleCachedRepoList(
255 255 all_repos, repos_path=self.repos_path, order_by=sort_key)
256 256 return repo_iter
257 257
258 258 def get_repo_groups(self, all_groups=None):
259 259 if all_groups is None:
260 260 all_groups = RepoGroup.query()\
261 261 .filter(RepoGroup.group_parent_id == null()).all()
262 262 return [x for x in RepoGroupList(all_groups)]
263 263
264 264 def mark_for_invalidation(self, repo_name, delete=False):
265 265 """
266 266 Mark caches of this repo invalid in the database. `delete` flag
267 267 removes the cache entries
268 268
269 269 :param repo_name: the repo_name for which caches should be marked
270 270 invalid, or deleted
271 271 :param delete: delete the entry keys instead of setting bool
272 272 flag on them, and also purge caches used by the dogpile
273 273 """
274 274 repo = Repository.get_by_repo_name(repo_name)
275 275
276 276 if repo:
277 277 invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format(
278 278 repo_id=repo.repo_id)
279 279 CacheKey.set_invalidate(invalidation_namespace, delete=delete)
280 280
281 281 repo_id = repo.repo_id
282 282 config = repo._config
283 283 config.set('extensions', 'largefiles', '')
284 284 repo.update_commit_cache(config=config, cs_cache=None)
285 285 if delete:
286 286 cache_namespace_uid = f'cache_repo.{repo_id}'
287 287 rc_cache.clear_cache_namespace('cache_repo', cache_namespace_uid, method=rc_cache.CLEAR_INVALIDATE)
288 288
289 289 def toggle_following_repo(self, follow_repo_id, user_id):
290 290
291 291 f = self.sa.query(UserFollowing)\
292 292 .filter(UserFollowing.follows_repo_id == follow_repo_id)\
293 293 .filter(UserFollowing.user_id == user_id).scalar()
294 294
295 295 if f is not None:
296 296 try:
297 297 self.sa.delete(f)
298 298 return
299 299 except Exception:
300 300 log.error(traceback.format_exc())
301 301 raise
302 302
303 303 try:
304 304 f = UserFollowing()
305 305 f.user_id = user_id
306 306 f.follows_repo_id = follow_repo_id
307 307 self.sa.add(f)
308 308 except Exception:
309 309 log.error(traceback.format_exc())
310 310 raise
311 311
312 312 def toggle_following_user(self, follow_user_id, user_id):
313 313 f = self.sa.query(UserFollowing)\
314 314 .filter(UserFollowing.follows_user_id == follow_user_id)\
315 315 .filter(UserFollowing.user_id == user_id).scalar()
316 316
317 317 if f is not None:
318 318 try:
319 319 self.sa.delete(f)
320 320 return
321 321 except Exception:
322 322 log.error(traceback.format_exc())
323 323 raise
324 324
325 325 try:
326 326 f = UserFollowing()
327 327 f.user_id = user_id
328 328 f.follows_user_id = follow_user_id
329 329 self.sa.add(f)
330 330 except Exception:
331 331 log.error(traceback.format_exc())
332 332 raise
333 333
334 334 def is_following_repo(self, repo_name, user_id, cache=False):
335 335 r = self.sa.query(Repository)\
336 336 .filter(Repository.repo_name == repo_name).scalar()
337 337
338 338 f = self.sa.query(UserFollowing)\
339 339 .filter(UserFollowing.follows_repository == r)\
340 340 .filter(UserFollowing.user_id == user_id).scalar()
341 341
342 342 return f is not None
343 343
344 344 def is_following_user(self, username, user_id, cache=False):
345 345 u = User.get_by_username(username)
346 346
347 347 f = self.sa.query(UserFollowing)\
348 348 .filter(UserFollowing.follows_user == u)\
349 349 .filter(UserFollowing.user_id == user_id).scalar()
350 350
351 351 return f is not None
352 352
353 353 def get_followers(self, repo):
354 354 repo = self._get_repo(repo)
355 355
356 356 return self.sa.query(UserFollowing)\
357 357 .filter(UserFollowing.follows_repository == repo).count()
358 358
359 359 def get_forks(self, repo):
360 360 repo = self._get_repo(repo)
361 361 return self.sa.query(Repository)\
362 362 .filter(Repository.fork == repo).count()
363 363
364 364 def get_pull_requests(self, repo):
365 365 repo = self._get_repo(repo)
366 366 return self.sa.query(PullRequest)\
367 367 .filter(PullRequest.target_repo == repo)\
368 368 .filter(PullRequest.status != PullRequest.STATUS_CLOSED).count()
369 369
370 370 def get_artifacts(self, repo):
371 371 repo = self._get_repo(repo)
372 372 return self.sa.query(FileStore)\
373 373 .filter(FileStore.repo == repo)\
374 374 .filter(or_(FileStore.hidden == null(), FileStore.hidden == false())).count()
375 375
376 376 def mark_as_fork(self, repo, fork, user):
377 377 repo = self._get_repo(repo)
378 378 fork = self._get_repo(fork)
379 379 if fork and repo.repo_id == fork.repo_id:
380 380 raise Exception("Cannot set repository as fork of itself")
381 381
382 382 if fork and repo.repo_type != fork.repo_type:
383 383 raise RepositoryError(
384 384 "Cannot set repository as fork of repository with other type")
385 385
386 386 repo.fork = fork
387 387 self.sa.add(repo)
388 388 return repo
389 389
390 390 def pull_changes(self, repo, username, remote_uri=None, validate_uri=True, **kwargs):
391 391 dbrepo = self._get_repo(repo)
392 392 remote_uri = remote_uri or dbrepo.clone_uri
393 393 if not remote_uri:
394 394 raise Exception("This repository doesn't have a clone uri")
395 395
396 396 repo = dbrepo.scm_instance(cache=False)
397 397 repo.config.clear_section('hooks')
398 398
399 399 try:
400 400 # NOTE(marcink): add extra validation so we skip invalid urls
401 401 # this is due this tasks can be executed via scheduler without
402 402 # proper validation of remote_uri
403 403 if validate_uri:
404 404 config = make_db_config(clear_session=False)
405 405 url_validator(remote_uri, dbrepo.repo_type, config)
406 406 except InvalidCloneUrl:
407 407 raise
408 408
409 409 repo_name = dbrepo.repo_name
410 410 try:
411 411 # TODO: we need to make sure those operations call proper hooks !
412 412 repo.fetch(remote_uri, **kwargs)
413 413
414 414 self.mark_for_invalidation(repo_name)
415 415 except Exception:
416 416 log.error(traceback.format_exc())
417 417 raise
418 418
419 def push_changes(self, repo, username, remote_uri=None, validate_uri=True):
419 def push_changes(self, repo, username, remote_uri=None, validate_uri=True, **kwargs):
420 420 dbrepo = self._get_repo(repo)
421 421 remote_uri = remote_uri or dbrepo.push_uri
422 422 if not remote_uri:
423 423 raise Exception("This repository doesn't have a clone uri")
424 424
425 425 repo = dbrepo.scm_instance(cache=False)
426 426 repo.config.clear_section('hooks')
427 427
428 428 try:
429 429 # NOTE(marcink): add extra validation so we skip invalid urls
430 430 # this is due this tasks can be executed via scheduler without
431 431 # proper validation of remote_uri
432 432 if validate_uri:
433 433 config = make_db_config(clear_session=False)
434 434 url_validator(remote_uri, dbrepo.repo_type, config)
435 435 except InvalidCloneUrl:
436 436 raise
437 437
438 438 try:
439 repo.push(remote_uri)
439 repo.push(remote_uri, **kwargs)
440 440 except Exception:
441 441 log.error(traceback.format_exc())
442 442 raise
443 443
444 444 def commit_change(self, repo, repo_name, commit, user, author, message,
445 445 content: bytes, f_path: bytes, branch: str = None):
446 446 """
447 447 Commits changes
448 448 """
449 449 user = self._get_user(user)
450 450
451 451 # message and author needs to be unicode
452 452 # proper backend should then translate that into required type
453 453 message = safe_str(message)
454 454 author = safe_str(author)
455 455 imc = repo.in_memory_commit
456 456 imc.change(FileNode(f_path, content, mode=commit.get_file_mode(f_path)))
457 457 try:
458 458 # TODO: handle pre-push action !
459 459 tip = imc.commit(
460 460 message=message, author=author, parents=[commit],
461 461 branch=branch or commit.branch)
462 462 except Exception as e:
463 463 log.error(traceback.format_exc())
464 464 raise IMCCommitError(str(e))
465 465 finally:
466 466 # always clear caches, if commit fails we want fresh object also
467 467 self.mark_for_invalidation(repo_name)
468 468
469 469 # We trigger the post-push action
470 470 hooks_utils.trigger_post_push_hook(
471 471 username=user.username, action='push_local', hook_type='post_push',
472 472 repo_name=repo_name, repo_type=repo.alias, commit_ids=[tip.raw_id])
473 473 return tip
474 474
475 475 def _sanitize_path(self, f_path: bytes):
476 476 if f_path.startswith(b'/') or f_path.startswith(b'./') or b'../' in f_path:
477 477 raise NonRelativePathError(b'%b is not an relative path' % f_path)
478 478 if f_path:
479 479 f_path = os.path.normpath(f_path)
480 480 return f_path
481 481
482 482 def get_dirnode_metadata(self, request, commit, dir_node):
483 483 if not dir_node.is_dir():
484 484 return []
485 485
486 486 data = []
487 487 for node in dir_node:
488 488 if not node.is_file():
489 489 # we skip file-nodes
490 490 continue
491 491
492 492 last_commit = node.last_commit
493 493 last_commit_date = last_commit.date
494 494 data.append({
495 495 'name': node.name,
496 496 'size': h.format_byte_size_binary(node.size),
497 497 'modified_at': h.format_date(last_commit_date),
498 498 'modified_ts': last_commit_date.isoformat(),
499 499 'revision': last_commit.revision,
500 500 'short_id': last_commit.short_id,
501 501 'message': h.escape(last_commit.message),
502 502 'author': h.escape(last_commit.author),
503 503 'user_profile': h.gravatar_with_user(
504 504 request, last_commit.author),
505 505 })
506 506
507 507 return data
508 508
509 509 def get_nodes(self, repo_name, commit_id, root_path='/', flat=True,
510 510 extended_info=False, content=False, max_file_bytes=None):
511 511 """
512 512 recursive walk in root dir and return a set of all path in that dir
513 513 based on repository walk function
514 514
515 515 :param repo_name: name of repository
516 516 :param commit_id: commit id for which to list nodes
517 517 :param root_path: root path to list
518 518 :param flat: return as a list, if False returns a dict with description
519 519 :param extended_info: show additional info such as md5, binary, size etc
520 520 :param content: add nodes content to the return data
521 521 :param max_file_bytes: will not return file contents over this limit
522 522
523 523 """
524 524 _files = list()
525 525 _dirs = list()
526 526
527 527 try:
528 528 _repo = self._get_repo(repo_name)
529 529 commit = _repo.scm_instance().get_commit(commit_id=commit_id)
530 530 root_path = root_path.lstrip('/')
531 531
532 532 # get RootNode, inject pre-load options before walking
533 533 top_node = commit.get_node(root_path)
534 534 extended_info_pre_load = []
535 535 if extended_info:
536 536 extended_info_pre_load += ['md5']
537 537 top_node.default_pre_load = ['is_binary', 'size'] + extended_info_pre_load
538 538
539 539 for __, dirs, files in commit.walk(top_node):
540 540
541 541 for f in files:
542 542 _content = None
543 543 _data = f_name = f.str_path
544 544
545 545 if not flat:
546 546 _data = {
547 547 "name": h.escape(f_name),
548 548 "type": "file",
549 549 }
550 550 if extended_info:
551 551 _data.update({
552 552 "md5": f.md5,
553 553 "binary": f.is_binary,
554 554 "size": f.size,
555 555 "extension": f.extension,
556 556 "mimetype": f.mimetype,
557 557 "lines": f.lines()[0]
558 558 })
559 559
560 560 if content:
561 561 over_size_limit = (max_file_bytes is not None
562 562 and f.size > max_file_bytes)
563 563 full_content = None
564 564 if not f.is_binary and not over_size_limit:
565 565 full_content = f.str_content
566 566
567 567 _data.update({
568 568 "content": full_content,
569 569 })
570 570 _files.append(_data)
571 571
572 572 for d in dirs:
573 573 _data = d_name = d.str_path
574 574 if not flat:
575 575 _data = {
576 576 "name": h.escape(d_name),
577 577 "type": "dir",
578 578 }
579 579 if extended_info:
580 580 _data.update({
581 581 "md5": "",
582 582 "binary": False,
583 583 "size": 0,
584 584 "extension": "",
585 585 })
586 586 if content:
587 587 _data.update({
588 588 "content": None
589 589 })
590 590 _dirs.append(_data)
591 591 except RepositoryError:
592 592 log.exception("Exception in get_nodes")
593 593 raise
594 594
595 595 return _dirs, _files
596 596
597 597 def get_quick_filter_nodes(self, repo_name, commit_id, root_path='/'):
598 598 """
599 599 Generate files for quick filter in files view
600 600 """
601 601
602 602 _files = list()
603 603 _dirs = list()
604 604 try:
605 605 _repo = self._get_repo(repo_name)
606 606 commit = _repo.scm_instance().get_commit(commit_id=commit_id)
607 607 root_path = root_path.lstrip('/')
608 608
609 609 top_node = commit.get_node(root_path)
610 610 top_node.default_pre_load = []
611 611
612 612 for __, dirs, files in commit.walk(top_node):
613 613 for f in files:
614 614
615 615 _data = {
616 616 "name": h.escape(f.str_path),
617 617 "type": "file",
618 618 }
619 619
620 620 _files.append(_data)
621 621
622 622 for d in dirs:
623 623
624 624 _data = {
625 625 "name": h.escape(d.str_path),
626 626 "type": "dir",
627 627 }
628 628
629 629 _dirs.append(_data)
630 630 except RepositoryError:
631 631 log.exception("Exception in get_quick_filter_nodes")
632 632 raise
633 633
634 634 return _dirs, _files
635 635
636 636 def get_node(self, repo_name, commit_id, file_path,
637 637 extended_info=False, content=False, max_file_bytes=None, cache=True):
638 638 """
639 639 retrieve single node from commit
640 640 """
641 641
642 642 try:
643 643
644 644 _repo = self._get_repo(repo_name)
645 645 commit = _repo.scm_instance().get_commit(commit_id=commit_id)
646 646
647 647 file_node = commit.get_node(file_path)
648 648 if file_node.is_dir():
649 649 raise RepositoryError('The given path is a directory')
650 650
651 651 _content = None
652 652 f_name = file_node.str_path
653 653
654 654 file_data = {
655 655 "name": h.escape(f_name),
656 656 "type": "file",
657 657 }
658 658
659 659 if extended_info:
660 660 file_data.update({
661 661 "extension": file_node.extension,
662 662 "mimetype": file_node.mimetype,
663 663 })
664 664
665 665 if cache:
666 666 md5 = file_node.md5
667 667 is_binary = file_node.is_binary
668 668 size = file_node.size
669 669 else:
670 670 is_binary, md5, size, _content = file_node.metadata_uncached()
671 671
672 672 file_data.update({
673 673 "md5": md5,
674 674 "binary": is_binary,
675 675 "size": size,
676 676 })
677 677
678 678 if content and cache:
679 679 # get content + cache
680 680 size = file_node.size
681 681 over_size_limit = (max_file_bytes is not None and size > max_file_bytes)
682 682 full_content = None
683 683 all_lines = 0
684 684 if not file_node.is_binary and not over_size_limit:
685 685 full_content = safe_str(file_node.content)
686 686 all_lines, empty_lines = file_node.count_lines(full_content)
687 687
688 688 file_data.update({
689 689 "content": full_content,
690 690 "lines": all_lines
691 691 })
692 692 elif content:
693 693 # get content *without* cache
694 694 if _content is None:
695 695 is_binary, md5, size, _content = file_node.metadata_uncached()
696 696
697 697 over_size_limit = (max_file_bytes is not None and size > max_file_bytes)
698 698 full_content = None
699 699 all_lines = 0
700 700 if not is_binary and not over_size_limit:
701 701 full_content = safe_str(_content)
702 702 all_lines, empty_lines = file_node.count_lines(full_content)
703 703
704 704 file_data.update({
705 705 "content": full_content,
706 706 "lines": all_lines
707 707 })
708 708
709 709 except RepositoryError:
710 710 log.exception("Exception in get_node")
711 711 raise
712 712
713 713 return file_data
714 714
715 715 def get_fts_data(self, repo_name, commit_id, root_path='/'):
716 716 """
717 717 Fetch node tree for usage in full text search
718 718 """
719 719
720 720 tree_info = list()
721 721
722 722 try:
723 723 _repo = self._get_repo(repo_name)
724 724 commit = _repo.scm_instance().get_commit(commit_id=commit_id)
725 725 root_path = root_path.lstrip('/')
726 726 top_node = commit.get_node(root_path)
727 727 top_node.default_pre_load = []
728 728
729 729 for __, dirs, files in commit.walk(top_node):
730 730
731 731 for f in files:
732 732 is_binary, md5, size, _content = f.metadata_uncached()
733 733 _data = {
734 734 "name": f.str_path,
735 735 "md5": md5,
736 736 "extension": f.extension,
737 737 "binary": is_binary,
738 738 "size": size
739 739 }
740 740
741 741 tree_info.append(_data)
742 742
743 743 except RepositoryError:
744 744 log.exception("Exception in get_nodes")
745 745 raise
746 746
747 747 return tree_info
748 748
749 749 def create_nodes(self, user, repo, message, nodes, parent_commit=None,
750 750 author=None, trigger_push_hook=True):
751 751 """
752 752 Commits given multiple nodes into repo
753 753
754 754 :param user: RhodeCode User object or user_id, the commiter
755 755 :param repo: RhodeCode Repository object
756 756 :param message: commit message
757 757 :param nodes: mapping {filename:{'content':content},...}
758 758 :param parent_commit: parent commit, can be empty than it's
759 759 initial commit
760 760 :param author: author of commit, cna be different that commiter
761 761 only for git
762 762 :param trigger_push_hook: trigger push hooks
763 763
764 764 :returns: new committed commit
765 765 """
766 766
767 767 user = self._get_user(user)
768 768 scm_instance = repo.scm_instance(cache=False)
769 769
770 770 message = safe_str(message)
771 771 commiter = user.full_contact
772 772 author = safe_str(author) if author else commiter
773 773
774 774 imc = scm_instance.in_memory_commit
775 775
776 776 if not parent_commit:
777 777 parent_commit = EmptyCommit(alias=scm_instance.alias)
778 778
779 779 if isinstance(parent_commit, EmptyCommit):
780 780 # EmptyCommit means we're editing empty repository
781 781 parents = None
782 782 else:
783 783 parents = [parent_commit]
784 784
785 785 upload_file_types = (io.BytesIO, io.BufferedRandom)
786 786 processed_nodes = []
787 787 for filename, content_dict in nodes.items():
788 788 if not isinstance(filename, bytes):
789 789 raise ValueError(f'filename key in nodes needs to be bytes , or {upload_file_types}')
790 790 content = content_dict['content']
791 791 if not isinstance(content, upload_file_types + (bytes,)):
792 792 raise ValueError('content key value in nodes needs to be bytes')
793 793
794 794 for f_path in nodes:
795 795 f_path = self._sanitize_path(f_path)
796 796 content = nodes[f_path]['content']
797 797
798 798 # decoding here will force that we have proper encoded values
799 799 # in any other case this will throw exceptions and deny commit
800 800
801 801 if isinstance(content, bytes):
802 802 pass
803 803 elif isinstance(content, upload_file_types):
804 804 content = content.read()
805 805 else:
806 806 raise Exception(f'Content is of unrecognized type {type(content)}, expected {upload_file_types}')
807 807 processed_nodes.append((f_path, content))
808 808
809 809 # add multiple nodes
810 810 for path, content in processed_nodes:
811 811 imc.add(FileNode(path, content=content))
812 812
813 813 # TODO: handle pre push scenario
814 814 tip = imc.commit(message=message,
815 815 author=author,
816 816 parents=parents,
817 817 branch=parent_commit.branch)
818 818
819 819 self.mark_for_invalidation(repo.repo_name)
820 820 if trigger_push_hook:
821 821 hooks_utils.trigger_post_push_hook(
822 822 username=user.username, action='push_local',
823 823 repo_name=repo.repo_name, repo_type=scm_instance.alias,
824 824 hook_type='post_push',
825 825 commit_ids=[tip.raw_id])
826 826 return tip
827 827
828 828 def update_nodes(self, user, repo, message, nodes, parent_commit=None,
829 829 author=None, trigger_push_hook=True):
830 830 user = self._get_user(user)
831 831 scm_instance = repo.scm_instance(cache=False)
832 832
833 833 message = safe_str(message)
834 834 commiter = user.full_contact
835 835 author = safe_str(author) if author else commiter
836 836
837 837 imc = scm_instance.in_memory_commit
838 838
839 839 if not parent_commit:
840 840 parent_commit = EmptyCommit(alias=scm_instance.alias)
841 841
842 842 if isinstance(parent_commit, EmptyCommit):
843 843 # EmptyCommit means we we're editing empty repository
844 844 parents = None
845 845 else:
846 846 parents = [parent_commit]
847 847
848 848 # add multiple nodes
849 849 for _filename, data in nodes.items():
850 850 # new filename, can be renamed from the old one, also sanitaze
851 851 # the path for any hack around relative paths like ../../ etc.
852 852 filename = self._sanitize_path(data['filename'])
853 853 old_filename = self._sanitize_path(_filename)
854 854 content = data['content']
855 855 file_mode = data.get('mode')
856 856 filenode = FileNode(old_filename, content=content, mode=file_mode)
857 857 op = data['op']
858 858 if op == 'add':
859 859 imc.add(filenode)
860 860 elif op == 'del':
861 861 imc.remove(filenode)
862 862 elif op == 'mod':
863 863 if filename != old_filename:
864 864 # TODO: handle renames more efficient, needs vcs lib changes
865 865 imc.remove(filenode)
866 866 imc.add(FileNode(filename, content=content, mode=file_mode))
867 867 else:
868 868 imc.change(filenode)
869 869
870 870 try:
871 871 # TODO: handle pre push scenario commit changes
872 872 tip = imc.commit(message=message,
873 873 author=author,
874 874 parents=parents,
875 875 branch=parent_commit.branch)
876 876 except NodeNotChangedError:
877 877 raise
878 878 except Exception as e:
879 879 log.exception("Unexpected exception during call to imc.commit")
880 880 raise IMCCommitError(str(e))
881 881 finally:
882 882 # always clear caches, if commit fails we want fresh object also
883 883 self.mark_for_invalidation(repo.repo_name)
884 884
885 885 if trigger_push_hook:
886 886 hooks_utils.trigger_post_push_hook(
887 887 username=user.username, action='push_local', hook_type='post_push',
888 888 repo_name=repo.repo_name, repo_type=scm_instance.alias,
889 889 commit_ids=[tip.raw_id])
890 890
891 891 return tip
892 892
893 893 def delete_nodes(self, user, repo, message, nodes, parent_commit=None,
894 894 author=None, trigger_push_hook=True):
895 895 """
896 896 Deletes given multiple nodes into `repo`
897 897
898 898 :param user: RhodeCode User object or user_id, the committer
899 899 :param repo: RhodeCode Repository object
900 900 :param message: commit message
901 901 :param nodes: mapping {filename:{'content':content},...}
902 902 :param parent_commit: parent commit, can be empty than it's initial
903 903 commit
904 904 :param author: author of commit, cna be different that commiter only
905 905 for git
906 906 :param trigger_push_hook: trigger push hooks
907 907
908 908 :returns: new commit after deletion
909 909 """
910 910
911 911 user = self._get_user(user)
912 912 scm_instance = repo.scm_instance(cache=False)
913 913
914 914 processed_nodes = []
915 915 for f_path in nodes:
916 916 f_path = self._sanitize_path(f_path)
917 917 # content can be empty but for compatibility it allows same dicts
918 918 # structure as add_nodes
919 919 content = nodes[f_path].get('content')
920 920 processed_nodes.append((safe_bytes(f_path), content))
921 921
922 922 message = safe_str(message)
923 923 commiter = user.full_contact
924 924 author = safe_str(author) if author else commiter
925 925
926 926 imc = scm_instance.in_memory_commit
927 927
928 928 if not parent_commit:
929 929 parent_commit = EmptyCommit(alias=scm_instance.alias)
930 930
931 931 if isinstance(parent_commit, EmptyCommit):
932 932 # EmptyCommit means we we're editing empty repository
933 933 parents = None
934 934 else:
935 935 parents = [parent_commit]
936 936 # add multiple nodes
937 937 for path, content in processed_nodes:
938 938 imc.remove(FileNode(path, content=content))
939 939
940 940 # TODO: handle pre push scenario
941 941 tip = imc.commit(message=message,
942 942 author=author,
943 943 parents=parents,
944 944 branch=parent_commit.branch)
945 945
946 946 self.mark_for_invalidation(repo.repo_name)
947 947 if trigger_push_hook:
948 948 hooks_utils.trigger_post_push_hook(
949 949 username=user.username, action='push_local', hook_type='post_push',
950 950 repo_name=repo.repo_name, repo_type=scm_instance.alias,
951 951 commit_ids=[tip.raw_id])
952 952 return tip
953 953
954 954 def strip(self, repo, commit_id, branch):
955 955 scm_instance = repo.scm_instance(cache=False)
956 956 scm_instance.config.clear_section('hooks')
957 957 scm_instance.strip(commit_id, branch)
958 958 self.mark_for_invalidation(repo.repo_name)
959 959
960 960 def get_unread_journal(self):
961 961 return self.sa.query(UserLog).count()
962 962
963 963 @classmethod
964 964 def backend_landing_ref(cls, repo_type):
965 965 """
966 966 Return a default landing ref based on a repository type.
967 967 """
968 968
969 969 landing_ref = {
970 970 'hg': ('branch:default', 'default'),
971 971 'git': ('branch:master', 'master'),
972 972 'svn': ('rev:tip', 'latest tip'),
973 973 'default': ('rev:tip', 'latest tip'),
974 974 }
975 975
976 976 return landing_ref.get(repo_type) or landing_ref['default']
977 977
978 978 def get_repo_landing_revs(self, translator, repo=None):
979 979 """
980 980 Generates select option with tags branches and bookmarks (for hg only)
981 981 grouped by type
982 982
983 983 :param repo:
984 984 """
985 985 from rhodecode.lib.vcs.backends.git import GitRepository
986 986
987 987 _ = translator
988 988 repo = self._get_repo(repo)
989 989
990 990 if repo:
991 991 repo_type = repo.repo_type
992 992 else:
993 993 repo_type = 'default'
994 994
995 995 default_landing_ref, landing_ref_lbl = self.backend_landing_ref(repo_type)
996 996
997 997 default_ref_options = [
998 998 [default_landing_ref, landing_ref_lbl]
999 999 ]
1000 1000 default_choices = [
1001 1001 default_landing_ref
1002 1002 ]
1003 1003
1004 1004 if not repo:
1005 1005 # presented at NEW repo creation
1006 1006 return default_choices, default_ref_options
1007 1007
1008 1008 repo = repo.scm_instance()
1009 1009
1010 1010 ref_options = [(default_landing_ref, landing_ref_lbl)]
1011 1011 choices = [default_landing_ref]
1012 1012
1013 1013 # branches
1014 1014 branch_group = [(f'branch:{safe_str(b)}', safe_str(b)) for b in repo.branches]
1015 1015 if not branch_group:
1016 1016 # new repo, or without maybe a branch?
1017 1017 branch_group = default_ref_options
1018 1018
1019 1019 branches_group = (branch_group, _("Branches"))
1020 1020 ref_options.append(branches_group)
1021 1021 choices.extend([x[0] for x in branches_group[0]])
1022 1022
1023 1023 # bookmarks for HG
1024 1024 if repo.alias == 'hg':
1025 1025 bookmarks_group = (
1026 1026 [(f'book:{safe_str(b)}', safe_str(b))
1027 1027 for b in repo.bookmarks],
1028 1028 _("Bookmarks"))
1029 1029 ref_options.append(bookmarks_group)
1030 1030 choices.extend([x[0] for x in bookmarks_group[0]])
1031 1031
1032 1032 # tags
1033 1033 tags_group = (
1034 1034 [(f'tag:{safe_str(t)}', safe_str(t))
1035 1035 for t in repo.tags],
1036 1036 _("Tags"))
1037 1037 ref_options.append(tags_group)
1038 1038 choices.extend([x[0] for x in tags_group[0]])
1039 1039
1040 1040 return choices, ref_options
1041 1041
1042 1042 def get_server_info(self, environ=None):
1043 1043 server_info = get_system_info(environ)
1044 1044 return server_info
General Comments 0
You need to be logged in to leave comments. Login now