'Handle a request from a local Galaxy instance to retrieve the changeset revision hash to which an installed repository can be updated.'

@web.expose
def get_changeset_revision_and_ctx_rev(self, trans, **kwd):

    def has_galaxy_utilities(repository_metadata):
        has_galaxy_utilities_dict = dict(includes_data_managers=False, includes_datatypes=False, includes_tools=False, includes_tools_for_display_in_tool_panel=False, has_repository_dependencies=False, has_repository_dependencies_only_if_compiling_contained_td=False, includes_tool_dependencies=False, includes_workflows=False)
        if repository_metadata:
            metadata = repository_metadata.metadata
            if metadata:
                if ('data_manager' in metadata):
                    has_galaxy_utilities_dict['includes_data_managers'] = True
                if ('datatypes' in metadata):
                    has_galaxy_utilities_dict['includes_datatypes'] = True
                if ('tools' in metadata):
                    has_galaxy_utilities_dict['includes_tools'] = True
                if ('tool_dependencies' in metadata):
                    has_galaxy_utilities_dict['includes_tool_dependencies'] = True
                repository_dependencies_dict = metadata.get('repository_dependencies', {})
                repository_dependencies = repository_dependencies_dict.get('repository_dependencies', [])
                (has_repository_dependencies, has_repository_dependencies_only_if_compiling_contained_td) = repository_util.get_repository_dependency_types(repository_dependencies)
                has_galaxy_utilities_dict['has_repository_dependencies'] = has_repository_dependencies
                has_galaxy_utilities_dict['has_repository_dependencies_only_if_compiling_contained_td'] = has_repository_dependencies_only_if_compiling_contained_td
                if ('workflows' in metadata):
                    has_galaxy_utilities_dict['includes_workflows'] = True
        return has_galaxy_utilities_dict
    name = kwd.get('name', None)
    owner = kwd.get('owner', None)
    changeset_revision = kwd.get('changeset_revision', None)
    repository = repository_util.get_repository_by_name_and_owner(trans.app, name, owner)
    repository_metadata = metadata_util.get_repository_metadata_by_changeset_revision(trans.app, trans.security.encode_id(repository.id), changeset_revision)
    has_galaxy_utilities_dict = has_galaxy_utilities(repository_metadata)
    includes_data_managers = has_galaxy_utilities_dict['includes_data_managers']
    includes_datatypes = has_galaxy_utilities_dict['includes_datatypes']
    includes_tools = has_galaxy_utilities_dict['includes_tools']
    includes_tools_for_display_in_tool_panel = has_galaxy_utilities_dict['includes_tools_for_display_in_tool_panel']
    includes_tool_dependencies = has_galaxy_utilities_dict['includes_tool_dependencies']
    has_repository_dependencies = has_galaxy_utilities_dict['has_repository_dependencies']
    has_repository_dependencies_only_if_compiling_contained_td = has_galaxy_utilities_dict['has_repository_dependencies_only_if_compiling_contained_td']
    includes_workflows = has_galaxy_utilities_dict['includes_workflows']
    repo = hg_util.get_repo_for_repository(trans.app, repository=repository, repo_path=None, create=False)
    update_to_ctx = hg_util.get_changectx_for_changeset(repo, changeset_revision)
    ctx_rev = str(update_to_ctx.rev())
    latest_changeset_revision = changeset_revision
    update_dict = dict(changeset_revision=changeset_revision, ctx_rev=ctx_rev, includes_data_managers=includes_data_managers, includes_datatypes=includes_datatypes, includes_tools=includes_tools, includes_tools_for_display_in_tool_panel=includes_tools_for_display_in_tool_panel, includes_tool_dependencies=includes_tool_dependencies, has_repository_dependencies=has_repository_dependencies, has_repository_dependencies_only_if_compiling_contained_td=has_repository_dependencies_only_if_compiling_contained_td, includes_workflows=includes_workflows)
    if (changeset_revision == repository.tip(trans.app)):
        return encoding_util.tool_shed_encode(update_dict)
    elif repository_metadata:
        return encoding_util.tool_shed_encode(update_dict)
    else:
        update_to_changeset_hash = None
        for changeset in repo.changelog:
            includes_tools = False
            has_repository_dependencies = False
            has_repository_dependencies_only_if_compiling_contained_td = False
            changeset_hash = str(repo.changectx(changeset))
            hg_util.get_changectx_for_changeset(repo, changeset_hash)
            if update_to_changeset_hash:
                update_to_repository_metadata = metadata_util.get_repository_metadata_by_changeset_revision(trans.app, trans.security.encode_id(repository.id), changeset_hash)
                if update_to_repository_metadata:
                    has_galaxy_utilities_dict = has_galaxy_utilities(repository_metadata)
                    includes_data_managers = has_galaxy_utilities_dict['includes_data_managers']
                    includes_datatypes = has_galaxy_utilities_dict['includes_datatypes']
                    includes_tools = has_galaxy_utilities_dict['includes_tools']
                    includes_tools_for_display_in_tool_panel = has_galaxy_utilities_dict['includes_tools_for_display_in_tool_panel']
                    includes_tool_dependencies = has_galaxy_utilities_dict['includes_tool_dependencies']
                    has_repository_dependencies = has_galaxy_utilities_dict['has_repository_dependencies']
                    has_repository_dependencies_only_if_compiling_contained_td = has_galaxy_utilities_dict['has_repository_dependencies_only_if_compiling_contained_td']
                    includes_workflows = has_galaxy_utilities_dict['includes_workflows']
                    if (changeset_hash == repository.tip(trans.app)):
                        update_to_ctx = hg_util.get_changectx_for_changeset(repo, changeset_hash)
                        latest_changeset_revision = changeset_hash
                    else:
                        update_to_ctx = hg_util.get_changectx_for_changeset(repo, update_to_changeset_hash)
                        latest_changeset_revision = update_to_changeset_hash
                    break
            elif ((not update_to_changeset_hash) and (changeset_hash == changeset_revision)):
                update_to_changeset_hash = changeset_hash
        update_dict['includes_data_managers'] = includes_data_managers
        update_dict['includes_datatypes'] = includes_datatypes
        update_dict['includes_tools'] = includes_tools
        update_dict['includes_tools_for_display_in_tool_panel'] = includes_tools_for_display_in_tool_panel
        update_dict['includes_tool_dependencies'] = includes_tool_dependencies
        update_dict['includes_workflows'] = includes_workflows
        update_dict['has_repository_dependencies'] = has_repository_dependencies
        update_dict['has_repository_dependencies_only_if_compiling_contained_td'] = has_repository_dependencies_only_if_compiling_contained_td
        update_dict['changeset_revision'] = str(latest_changeset_revision)
    update_dict['ctx_rev'] = str(update_to_ctx.rev())
    return encoding_util.tool_shed_encode(update_dict)
