# HG changeset patch # User RhodeCode Admin # Date 2023-01-18 21:10:40 # Node ID 07a18b1189681c33367c9d753efb43dd775025a0 # Parent 98843d15a82ffba989b29d7a7971c8e027e49d6c repos: cleannup and fix landing-ref code. with latest git update this exposed a bug when we allowed to set references like tip for git repos now the defaults are loaded from a single place controlled by default branch for git. Repos without master are now working just fine with new git and new logic. diff --git a/rhodecode/api/tests/test_update_repo.py b/rhodecode/api/tests/test_update_repo.py --- a/rhodecode/api/tests/test_update_repo.py +++ b/rhodecode/api/tests/test_update_repo.py @@ -22,6 +22,7 @@ import mock import pytest from rhodecode.model.repo import RepoModel +from rhodecode.model.scm import ScmModel from rhodecode.tests import TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN from rhodecode.api.tests.utils import ( build_data, api_call, assert_error, assert_ok, crash, jsonify) @@ -65,8 +66,8 @@ class TestApiUpdateRepo(object): ({'push_uri': 'http://example.com/repo_push'}, {'push_uri': 'http://example.com/repo_push'}), - ({'landing_rev': 'rev:tip'}, - {'landing_rev': ['rev', 'tip']}), + ({'landing_rev': None}, # auto-updated based on type of repo + {'landing_rev': [None, None]}), ({'enable_statistics': True}, SAME_AS_UPDATES), @@ -97,6 +98,12 @@ class TestApiUpdateRepo(object): if updates.get('_group'): fixture.create_repo_group(updates['_group']) + if 'landing_rev' in updates: + default_landing_ref, _lbl = ScmModel.backend_landing_ref(backend.alias) + _type, _name = default_landing_ref.split(':') + updates['landing_rev'] = default_landing_ref + expected['landing_rev'] = [_type, _name] + expected_api_data = repo.get_api_data(include_secrets=True) if expected is SAME_AS_UPDATES: expected_api_data.update(updates) diff --git a/rhodecode/lib/vcs/backends/git/repository.py b/rhodecode/lib/vcs/backends/git/repository.py --- a/rhodecode/lib/vcs/backends/git/repository.py +++ b/rhodecode/lib/vcs/backends/git/repository.py @@ -55,6 +55,7 @@ class GitRepository(BaseRepository): Git repository backend. """ DEFAULT_BRANCH_NAME = os.environ.get('GIT_DEFAULT_BRANCH_NAME') or 'master' + DEFAULT_REF = 'branch:{}'.format(DEFAULT_BRANCH_NAME) contact = BaseRepository.DEFAULT_CONTACT diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -497,7 +497,7 @@ class RepoModel(BaseModel): def _create_repo(self, repo_name, repo_type, description, owner, private=False, clone_uri=None, repo_group=None, - landing_rev='rev:tip', fork_of=None, + landing_rev=None, fork_of=None, copy_fork_permissions=False, enable_statistics=False, enable_locking=False, enable_downloads=False, copy_group_permissions=False, @@ -512,6 +512,8 @@ class RepoModel(BaseModel): owner = self._get_user(owner) fork_of = self._get_repo(fork_of) repo_group = self._get_repo_group(safe_int(repo_group)) + default_landing_ref, _lbl = ScmModel.backend_landing_ref(repo_type) + landing_rev = landing_rev or default_landing_ref try: repo_name = safe_unicode(repo_name) diff --git a/rhodecode/model/scm.py b/rhodecode/model/scm.py --- a/rhodecode/model/scm.py +++ b/rhodecode/model/scm.py @@ -966,6 +966,8 @@ class ScmModel(BaseModel): :param repo: """ + from rhodecode.lib.vcs.backends.git import GitRepository + _ = translator repo = self._get_repo(repo) @@ -984,12 +986,13 @@ class ScmModel(BaseModel): ] if not repo: + # presented at NEW repo creation return default_choices, default_ref_options repo = repo.scm_instance() - ref_options = [('rev:tip', 'latest tip')] - choices = ['rev:tip'] + ref_options = [(default_landing_ref, landing_ref_lbl)] + choices = [default_landing_ref] # branches branch_group = [(u'branch:%s' % safe_unicode(b), safe_unicode(b)) for b in repo.branches] diff --git a/rhodecode/model/validation_schema/schemas/repo_schema.py b/rhodecode/model/validation_schema/schemas/repo_schema.py --- a/rhodecode/model/validation_schema/schemas/repo_schema.py +++ b/rhodecode/model/validation_schema/schemas/repo_schema.py @@ -26,11 +26,6 @@ from rhodecode.model.validation_schema.u from rhodecode.model.validation_schema import validators, preparers, types DEFAULT_LANDING_REF = 'rev:tip' -DEFAULT_BACKEND_LANDING_REF = { - 'hg': 'branch:default', - 'git': 'branch:master', - 'svn': 'rev:tip', -} def get_group_and_repo(repo_name): @@ -80,12 +75,13 @@ def deferred_sync_uri_validator(node, kw @colander.deferred def deferred_landing_ref_widget(node, kw): + from rhodecode.model.scm import ScmModel + repo_type = kw.get('repo_type') default_opts = [] if repo_type: - default_opts.append( - (DEFAULT_BACKEND_LANDING_REF[repo_type], - DEFAULT_BACKEND_LANDING_REF[repo_type])) + default_landing_ref, _lbl = ScmModel.backend_landing_ref(repo_type) + default_opts.append((default_landing_ref, default_landing_ref)) items = kw.get('repo_ref_items', default_opts) items = convert_to_optgroup(items) diff --git a/rhodecode/tests/fixture.py b/rhodecode/tests/fixture.py --- a/rhodecode/tests/fixture.py +++ b/rhodecode/tests/fixture.py @@ -39,6 +39,7 @@ from rhodecode.model.repo_group import R from rhodecode.model.user_group import UserGroupModel from rhodecode.model.gist import GistModel from rhodecode.model.auth_token import AuthTokenModel +from rhodecode.model.scm import ScmModel from rhodecode.authentication.plugins.auth_rhodecode import \ RhodeCodeAuthPlugin @@ -190,15 +191,19 @@ class Fixture(object): return context() def _get_repo_create_params(self, **custom): + repo_type = custom.get('repo_type') or 'hg' + + default_landing_ref, landing_ref_lbl = ScmModel.backend_landing_ref(repo_type) + defs = { 'repo_name': None, - 'repo_type': 'hg', + 'repo_type': repo_type, 'clone_uri': '', 'push_uri': '', 'repo_group': '-1', 'repo_description': 'DESC', 'repo_private': False, - 'repo_landing_rev': 'rev:tip', + 'repo_landing_commit_ref': default_landing_ref, 'repo_copy_permissions': False, 'repo_state': Repository.STATE_CREATED, } @@ -279,14 +284,16 @@ class Fixture(object): def create_fork(self, repo_to_fork, fork_name, **kwargs): repo_to_fork = Repository.get_by_repo_name(repo_to_fork) - form_data = self._get_repo_create_params(repo_name=fork_name, - fork_parent_id=repo_to_fork.repo_id, - repo_type=repo_to_fork.repo_type, - **kwargs) + form_data = self._get_repo_create_params( + repo_name=fork_name, + fork_parent_id=repo_to_fork.repo_id, + repo_type=repo_to_fork.repo_type, + **kwargs) + #TODO: fix it !! form_data['description'] = form_data['repo_description'] form_data['private'] = form_data['repo_private'] - form_data['landing_rev'] = form_data['repo_landing_rev'] + form_data['landing_rev'] = form_data['repo_landing_commit_ref'] owner = kwargs.get('cur_user', TEST_USER_ADMIN_LOGIN) RepoModel().create_fork(form_data, cur_user=owner)