Beispiel #1
0
def get_scms_for_path(path):
    """
    Returns all scm's found at the given path. If no scm is recognized
    - empty list is returned.

    :param path: path to directory which should be checked. May be callable.

    :raises VCSError: if given ``path`` is not a directory
    """
    from rhodecode.lib.vcs.backends import get_backend
    if hasattr(path, '__call__'):
        path = path()
    if not os.path.isdir(path):
        raise VCSError("Given path %r is not a directory" % path)

    result = []
    for key in ALIASES:
        dirname = os.path.join(path, '.' + key)
        if os.path.isdir(dirname):
            result.append(key)
            continue
        # We still need to check if it's not bare repository as
        # bare repos don't have working directories
        try:
            get_backend(key)(path)
            result.append(key)
            continue
        except RepositoryError:
            # Wrong backend
            pass
        except VCSError:
            # No backend at all
            pass
    return result
    def test_merge_success(self, vcsbackend):
        self.prepare_for_success(vcsbackend)

        merge_response = self.target_repo.merge(self.target_ref,
                                                self.source_repo,
                                                self.source_ref,
                                                self.workspace,
                                                'test user',
                                                '*****@*****.**',
                                                'merge message 1',
                                                dry_run=False)
        expected_merge_response = MergeResponse(True, True,
                                                merge_response.merge_commit_id,
                                                MergeFailureReason.NONE)
        assert merge_response == expected_merge_response

        target_repo = backends.get_backend(vcsbackend.alias)(
            self.target_repo.path)
        target_commits = list(target_repo.get_commits())
        commit_ids = [c.raw_id for c in target_commits[:-1]]
        assert self.source_ref.commit_id in commit_ids
        assert self.target_ref.commit_id in commit_ids

        merge_commit = target_commits[-1]
        assert merge_commit.raw_id == merge_response.merge_commit_id
        assert merge_commit.message.strip() == 'merge message 1'
        assert merge_commit.author == 'test user <*****@*****.**>'

        # We call it twice so to make sure we can handle updates
        target_ref = Reference(self.target_ref.type, self.target_ref.name,
                               merge_response.merge_commit_id)

        merge_response = target_repo.merge(target_ref,
                                           self.source_repo,
                                           self.source_ref,
                                           self.workspace,
                                           'test user',
                                           '*****@*****.**',
                                           'merge message 2',
                                           dry_run=False)
        expected_merge_response = MergeResponse(True, True,
                                                merge_response.merge_commit_id,
                                                MergeFailureReason.NONE)
        assert merge_response == expected_merge_response

        target_repo = backends.get_backend(vcsbackend.alias)(
            self.target_repo.path)
        merge_commit = target_repo.get_commit(merge_response.merge_commit_id)
        assert merge_commit.message.strip() == 'merge message 1'
        assert merge_commit.author == 'test user <*****@*****.**>'
Beispiel #3
0
    def __create_repo(self, repo_name, alias, new_parent_id, clone_uri=False):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent_id:
        :param clone_uri:
        """
        from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group

        if new_parent_id:
            paths = RepoGroup.get(new_parent_id).full_path.split(RepoGroup.url_sep())
            new_parent_path = os.sep.join(paths)
        else:
            new_parent_path = ""

        # we need to make it str for mercurial
        repo_path = os.path.join(*map(lambda x: safe_str(x), [self.repos_path, new_parent_path, repo_name]))

        # check if this path is not a repository
        if is_valid_repo(repo_path, self.repos_path):
            raise Exception("This path %s is a valid repository" % repo_path)

        # check if this path is a group
        if is_valid_repos_group(repo_path, self.repos_path):
            raise Exception("This path %s is a valid group" % repo_path)

        log.info("creating repo %s in %s @ %s" % (repo_name, safe_unicode(repo_path), clone_uri))
        backend = get_backend(alias)

        backend(repo_path, create=True, src_url=clone_uri)
def get_scms_for_path(path):
    """
    Returns all scm's found at the given path. If no scm is recognized
    - empty list is returned.

    :param path: path to directory which should be checked. May be callable.

    :raises VCSError: if given ``path`` is not a directory
    """
    from rhodecode.lib.vcs.backends import get_backend
    if hasattr(path, '__call__'):
        path = path()
    if not os.path.isdir(path):
        raise VCSError("Given path %r is not a directory" % path)

    result = []
    for key in settings.available_aliases():
        try:
            backend = get_backend(key)
        except VCSBackendNotSupportedError:
            log.warning('VCSBackendNotSupportedError: %s not supported', key)
            continue
        if backend.is_valid_repository(path):
            result.append(key)
    return result
Beispiel #5
0
def _create_empty_repository(cls, backend_alias=None):
    Backend = get_backend(backend_alias or cls.backend_alias)
    repo_path = get_new_dir(str(time.time()))
    repo = Backend(repo_path, create=True)
    if hasattr(cls, '_get_commits'):
        cls.tip = _add_commits_to_repo(repo, cls._get_commits())

    return repo
Beispiel #6
0
def vcs_repo(request, backend_alias):
    Backend = get_backend(backend_alias)
    repo_path = get_new_dir(str(time.time()))
    repo = Backend(repo_path, create=True)

    @request.addfinalizer
    def cleanup():
        shutil.rmtree(repo_path)

    return repo
Beispiel #7
0
    def __create_repo(self,
                      repo_name,
                      alias,
                      parent,
                      clone_uri=False,
                      repo_store_location=None):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent_id:
        :param clone_uri:
        :param repo_path:
        """
        from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group
        from rhodecode.model.scm import ScmModel

        if parent:
            new_parent_path = os.sep.join(parent.full_path_splitted)
        else:
            new_parent_path = ''
        if repo_store_location:
            _paths = [repo_store_location]
        else:
            _paths = [self.repos_path, new_parent_path, repo_name]
        # we need to make it str for mercurial
        repo_path = os.path.join(*map(lambda x: safe_str(x), _paths))

        # check if this path is not a repository
        if is_valid_repo(repo_path, self.repos_path):
            raise Exception('This path %s is a valid repository' % repo_path)

        # check if this path is a group
        if is_valid_repos_group(repo_path, self.repos_path):
            raise Exception('This path %s is a valid group' % repo_path)

        log.info(
            'creating repo %s in %s @ %s' %
            (repo_name, safe_unicode(repo_path), obfuscate_url_pw(clone_uri)))
        backend = get_backend(alias)
        if alias == 'hg':
            repo = backend(repo_path, create=True, src_url=clone_uri)
        elif alias == 'git':
            repo = backend(repo_path,
                           create=True,
                           src_url=clone_uri,
                           bare=True)
            # add rhodecode hook into this repo
            ScmModel().install_git_hook(repo=repo)
        else:
            raise Exception('Undefined alias %s' % alias)
        return repo
    def test_merge_rebase_source_is_updated_bookmark(self, vcsbackend_hg):
        target_repo = vcsbackend_hg.create_repo(number_of_commits=1)
        source_repo = vcsbackend_hg.clone_repo(target_repo)
        vcsbackend_hg.add_file(target_repo, 'README_MERGE1', 'Version 1')
        vcsbackend_hg.add_file(source_repo, 'README_MERGE2', 'Version 2')
        imc = source_repo.in_memory_commit
        imc.add(FileNode('file_x', content=source_repo.name))
        imc.commit(message=u'Automatic commit from repo merge test',
                   author=u'Automatic')
        target_commit = target_repo.get_commit()
        source_commit = source_repo.get_commit()

        vcsbackend_hg.add_file(source_repo, 'LICENSE', 'LICENSE Info')

        default_branch = target_repo.DEFAULT_BRANCH_NAME
        bookmark_name = 'bookmark'
        source_repo._update(default_branch)
        source_repo.bookmark(bookmark_name)

        target_ref = Reference('branch', default_branch, target_commit.raw_id)
        source_ref = Reference('book', bookmark_name, source_commit.raw_id)
        workspace = 'test-merge'

        with mock.patch.object(rhodecode.lib.vcs.conf.settings,
                               'HG_USE_REBASE_FOR_MERGING',
                               return_value=True):
            merge_response = target_repo.merge(target_ref,
                                               source_repo,
                                               source_ref,
                                               workspace,
                                               'test user',
                                               '*****@*****.**',
                                               'merge message 1',
                                               dry_run=False)

        expected_merge_response = MergeResponse(True, True,
                                                merge_response.merge_commit_id,
                                                MergeFailureReason.NONE)
        assert merge_response == expected_merge_response

        target_repo = backends.get_backend(vcsbackend_hg.alias)(
            target_repo.path)
        last_commit = target_repo.get_commit()
        assert last_commit.message == source_commit.message
        assert last_commit.author == source_commit.author
        # This checks that we effectively did a rebase
        assert last_commit.raw_id != source_commit.raw_id

        # Check the target has only 4 commits: 2 were already in target and
        # only two should have been added
        assert len(target_repo.commit_ids) == 2 + 2
    def pull_request(self, request, backend, pr_util):
        """
        A pull request combined with multiples patches.
        """
        BackendClass = get_backend(backend.alias)
        self.merge_patcher = mock.patch.object(BackendClass, 'merge')
        self.workspace_remove_patcher = mock.patch.object(
            BackendClass, 'cleanup_merge_workspace')

        self.workspace_remove_mock = self.workspace_remove_patcher.start()
        self.merge_mock = self.merge_patcher.start()
        self.comment_patcher = mock.patch(
            'rhodecode.model.changeset_status.ChangesetStatusModel.set_status')
        self.comment_patcher.start()
        self.notification_patcher = mock.patch(
            'rhodecode.model.notification.NotificationModel.create')
        self.notification_patcher.start()
        self.helper_patcher = mock.patch('rhodecode.lib.helpers.url')
        self.helper_patcher.start()

        self.hook_patcher = mock.patch.object(PullRequestModel,
                                              '_trigger_pull_request_hook')
        self.hook_mock = self.hook_patcher.start()

        self.invalidation_patcher = mock.patch(
            'rhodecode.model.pull_request.ScmModel.mark_for_invalidation')
        self.invalidation_mock = self.invalidation_patcher.start()

        self.pull_request = pr_util.create_pull_request(mergeable=True,
                                                        name_suffix=u'ąć')
        self.source_commit = self.pull_request.source_ref_parts.commit_id
        self.target_commit = self.pull_request.target_ref_parts.commit_id
        self.workspace_id = 'pr-%s' % self.pull_request.pull_request_id

        @request.addfinalizer
        def cleanup_pull_request():
            calls = [
                mock.call(self.pull_request, self.pull_request.author,
                          'create')
            ]
            self.hook_mock.assert_has_calls(calls)

            self.workspace_remove_patcher.stop()
            self.merge_patcher.stop()
            self.comment_patcher.stop()
            self.notification_patcher.stop()
            self.helper_patcher.stop()
            self.hook_patcher.stop()
            self.invalidation_patcher.stop()

        return self.pull_request
Beispiel #10
0
    def __create_repo(self, repo_name, alias, parent, clone_uri=False,
                      repo_store_location=None):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent_id:
        :param clone_uri:
        :param repo_path:
        """
        from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group
        from rhodecode.model.scm import ScmModel

        if parent:
            new_parent_path = os.sep.join(parent.full_path_splitted)
        else:
            new_parent_path = ''
        if repo_store_location:
            _paths = [repo_store_location]
        else:
            _paths = [self.repos_path, new_parent_path, repo_name]
        # we need to make it str for mercurial
        repo_path = os.path.join(*map(lambda x: safe_str(x), _paths))

        # check if this path is not a repository
        if is_valid_repo(repo_path, self.repos_path):
            raise Exception('This path %s is a valid repository' % repo_path)

        # check if this path is a group
        if is_valid_repos_group(repo_path, self.repos_path):
            raise Exception('This path %s is a valid group' % repo_path)

        log.info('creating repo %s in %s @ %s' % (
                     repo_name, safe_unicode(repo_path),
                     obfuscate_url_pw(clone_uri)
                )
        )
        backend = get_backend(alias)
        if alias == 'hg':
            repo = backend(repo_path, create=True, src_url=clone_uri)
        elif alias == 'git':
            repo = backend(repo_path, create=True, src_url=clone_uri, bare=True)
            # add rhodecode hook into this repo
            ScmModel().install_git_hook(repo=repo)
        else:
            raise Exception('Undefined alias %s' % alias)
        return repo
    def test_merge_target_is_bookmark(self, vcsbackend_hg):
        target_repo = vcsbackend_hg.create_repo(number_of_commits=1)
        source_repo = vcsbackend_hg.clone_repo(target_repo)
        vcsbackend_hg.add_file(target_repo, 'README_MERGE1', 'Version 1')
        vcsbackend_hg.add_file(source_repo, 'README_MERGE2', 'Version 2')
        imc = source_repo.in_memory_commit
        imc.add(FileNode('file_x', content=source_repo.name))
        imc.commit(message=u'Automatic commit from repo merge test',
                   author=u'Automatic')
        target_commit = target_repo.get_commit()
        source_commit = source_repo.get_commit()
        default_branch = target_repo.DEFAULT_BRANCH_NAME
        bookmark_name = 'bookmark'
        target_repo._update(default_branch)
        target_repo.bookmark(bookmark_name)
        target_ref = Reference('book', bookmark_name, target_commit.raw_id)
        source_ref = Reference('branch', default_branch, source_commit.raw_id)
        workspace = 'test-merge'

        merge_response = target_repo.merge(target_ref,
                                           source_repo,
                                           source_ref,
                                           workspace,
                                           'test user',
                                           '*****@*****.**',
                                           'merge message 1',
                                           dry_run=False)
        expected_merge_response = MergeResponse(True, True,
                                                merge_response.merge_commit_id,
                                                MergeFailureReason.NONE)
        assert merge_response == expected_merge_response

        target_repo = backends.get_backend(vcsbackend_hg.alias)(
            target_repo.path)
        target_commits = list(target_repo.get_commits())
        commit_ids = [c.raw_id for c in target_commits[:-1]]
        assert source_ref.commit_id in commit_ids
        assert target_ref.commit_id in commit_ids

        merge_commit = target_commits[-1]
        assert merge_commit.raw_id == merge_response.merge_commit_id
        assert merge_commit.message.strip() == 'merge message 1'
        assert merge_commit.author == 'test user <*****@*****.**>'

        # Check the bookmark was updated in the target repo
        assert (target_repo.bookmarks[bookmark_name] ==
                merge_response.merge_commit_id)
Beispiel #12
0
    def __create_repo(self, repo_name, alias, new_parent_id, clone_uri=False):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent_id:
        :param clone_uri:
        """
        from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group

        if new_parent_id:
            paths = RepoGroup.get(new_parent_id)\
                .full_path.split(RepoGroup.url_sep())
            new_parent_path = os.sep.join(paths)
        else:
            new_parent_path = ''

        # we need to make it str for mercurial
        repo_path = os.path.join(*map(lambda x: safe_str(
            x), [self.repos_path, new_parent_path, repo_name]))

        # check if this path is not a repository
        if is_valid_repo(repo_path, self.repos_path):
            raise Exception('This path %s is a valid repository' % repo_path)

        # check if this path is a group
        if is_valid_repos_group(repo_path, self.repos_path):
            raise Exception('This path %s is a valid group' % repo_path)

        log.info('creating repo %s in %s @ %s' %
                 (repo_name, safe_unicode(repo_path), clone_uri))
        backend = get_backend(alias)

        backend(repo_path, create=True, src_url=clone_uri)
    def test_merge_source_is_bookmark(self, vcsbackend_hg):
        target_repo = vcsbackend_hg.create_repo(number_of_commits=1)
        source_repo = vcsbackend_hg.clone_repo(target_repo)
        imc = source_repo.in_memory_commit
        imc.add(FileNode('file_x', content=source_repo.name))
        imc.commit(message=u'Automatic commit from repo merge test',
                   author=u'Automatic')
        target_commit = target_repo.get_commit()
        source_commit = source_repo.get_commit()
        default_branch = target_repo.DEFAULT_BRANCH_NAME
        bookmark_name = 'bookmark'
        target_ref = Reference('branch', default_branch, target_commit.raw_id)
        source_repo._update(default_branch)
        source_repo.bookmark(bookmark_name)
        source_ref = Reference('book', bookmark_name, source_commit.raw_id)
        workspace = 'test-merge'

        merge_response = target_repo.merge(target_ref,
                                           source_repo,
                                           source_ref,
                                           workspace,
                                           'test user',
                                           '*****@*****.**',
                                           'merge message 1',
                                           dry_run=False)
        expected_merge_response = MergeResponse(True, True,
                                                merge_response.merge_commit_id,
                                                MergeFailureReason.NONE)
        assert merge_response == expected_merge_response

        target_repo = backends.get_backend(vcsbackend_hg.alias)(
            target_repo.path)
        target_commits = list(target_repo.get_commits())
        commit_ids = [c.raw_id for c in target_commits]
        assert source_ref.commit_id == commit_ids[-1]
        assert target_ref.commit_id == commit_ids[-2]
Beispiel #14
0
 def branch(self):
     from rhodecode.lib.vcs.backends import get_backend
     return get_backend(self.alias).DEFAULT_BRANCH_NAME
    def _create_filesystem_repo(self,
                                repo_name,
                                repo_type,
                                repo_group,
                                clone_uri=None,
                                repo_store_location=None,
                                use_global_config=False):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent:
        :param clone_uri:
        :param repo_store_location:
        """
        from rhodecode.lib.utils import is_valid_repo, is_valid_repo_group
        from rhodecode.model.scm import ScmModel

        if Repository.NAME_SEP in repo_name:
            raise ValueError('repo_name must not contain groups got `%s`' %
                             repo_name)

        if isinstance(repo_group, RepoGroup):
            new_parent_path = os.sep.join(repo_group.full_path_splitted)
        else:
            new_parent_path = repo_group or ''

        if repo_store_location:
            _paths = [repo_store_location]
        else:
            _paths = [self.repos_path, new_parent_path, repo_name]
            # we need to make it str for mercurial
        repo_path = os.path.join(*map(lambda x: safe_str(x), _paths))

        # check if this path is not a repository
        if is_valid_repo(repo_path, self.repos_path):
            raise Exception('This path %s is a valid repository' % repo_path)

        # check if this path is a group
        if is_valid_repo_group(repo_path, self.repos_path):
            raise Exception('This path %s is a valid group' % repo_path)

        log.info('creating repo %s in %s from url: `%s`', repo_name,
                 safe_unicode(repo_path), obfuscate_url_pw(clone_uri))

        backend = get_backend(repo_type)

        config_repo = None if use_global_config else repo_name
        if config_repo and new_parent_path:
            config_repo = Repository.NAME_SEP.join(
                (new_parent_path, config_repo))
        config = make_db_config(clear_session=False, repo=config_repo)
        config.set('extensions', 'largefiles', '')

        # patch and reset hooks section of UI config to not run any
        # hooks on creating remote repo
        config.clear_section('hooks')

        # TODO: johbo: Unify this, hardcoded "bare=True" does not look nice
        if repo_type == 'git':
            repo = backend(repo_path,
                           config=config,
                           create=True,
                           src_url=clone_uri,
                           bare=True)
        else:
            repo = backend(repo_path,
                           config=config,
                           create=True,
                           src_url=clone_uri)

        ScmModel().install_hooks(repo, repo_type=repo_type)

        log.debug('Created repo %s with %s backend', safe_unicode(repo_name),
                  safe_unicode(repo_type))
        return repo
def get_scm_backend(backend_type):
    from rhodecode.lib.vcs.backends import get_backend
    return get_backend(backend_type)
Beispiel #17
0
 def branch(self):
     from rhodecode.lib.vcs.backends import get_backend
     return get_backend(self.alias).DEFAULT_BRANCH_NAME
Beispiel #18
0
def test_create_repo_object(repo, repeat):
    backend = get_backend(repo.alias)
    for x in xrange(repeat):
        repo = backend(repo.path)