code: def _create_dataset_sibling(
      name,
      ds,
      hierarchy_basepath,
      shell,
      replicate_local_structure,
      ri,
      target_dir,
      target_url,
      target_pushurl,
      existing,
      shared,
      group,
      publish_depends,
      publish_by_default,
      install_postupdate_hook,
      as_common_datasrc,
      annex_wanted,
      annex_group,
      annex_groupwanted,
      inherit,
  ):
      """Everyone is very smart here and could figure out the combinatorial
      affluence among provided tiny (just slightly over a dozen) number of options
      and only a few pages of code
      """
      localds_path = ds.path
      ds_name = relpath(localds_path, start=hierarchy_basepath)
      if not replicate_local_structure:
          ds_name = (
              ""
              if ds_name == curdir
              else "-{}".format(ds_name.replace("/", "-"))
          )
          remoteds_path = target_dir.replace("%RELNAME", ds_name)
      else:
          # TODO: opj depends on local platform, not the remote one.
          # check how to deal with it. Does windows ssh server accept
          # posix paths? vice versa? Should planned SSH class provide
          # tools for this issue?
          # see gh-1188
          remoteds_path = normpath(opj(target_dir, ds_name))
  
      ds_repo = ds.repo
      # construct a would-be ssh url based on the current dataset's path
      ri.path = remoteds_path
      ds_url = ri.as_str()
      # configure dataset's git-access urls
      ds_target_url = (
          target_url.replace("%RELNAME", ds_name)
          if target_url
          else ds_url
      )
      # push, configure only if needed
      ds_target_pushurl = None
      if ds_target_url != ds_url:
          # not guaranteed that we can push via the primary URL
          ds_target_pushurl = (
              target_pushurl.replace("%RELNAME", ds_name)
              if target_pushurl
              else ds_url
          )
  
      lgr.info(
          "Considering to create a target dataset %s at %s of %s",
          localds_path,
          remoteds_path,
          "localhost" if isinstance(ri, PathRI) else ri.hostname,
      )
      # Must be set to True only if exists and existing='reconfigure'
      # otherwise we might skip actions if we say existing='reconfigure'
      # but it did not even exist before
      only_reconfigure = False
      if remoteds_path != ".":
          # check if target exists
          # TODO: Is this condition valid for != '.' only?
          path_children = _ls_remote_path(shell, remoteds_path)
          path_exists = path_children is not None
  
          if path_exists:
              _msg = "Target path %s already exists." % remoteds_path
          if path_exists and not path_children:
              # path should be an empty directory, which should be ok to remove
              try:
                  lgr.debug(
                      "Trying to rmdir %s on remote since seems to be an empty dir",
                      remoteds_path,
                  )
                  # should be safe since should not remove anything unless an empty dir
                  shell("rmdir {}".format(sh_quote(remoteds_path)))
                  path_exists = False
              except CommandError as e:
                  # If fails to rmdir -- either contains stuff no permissions
                  # TODO: fixup encode/decode dance again :-/ we should have got
                  # unicode/str here by now.  I guess it is the same as
                  # https://github.com/ReproNim/niceman/issues/83
                  # where I have reused this Runner thing
                  try:
                      # ds_name is unicode which makes _msg unicode so we must be
                      # unicode-ready
                      err_str = str(e.stderr)
                  except UnicodeDecodeError:
                      err_str = e.stderr.decode(errors="replace")
                  _msg += " And it fails to rmdir (%s)." % (
                      err_str.strip(),
                  )
  
          if path_exists:
              if existing == "error":
                  raise RuntimeError(_msg)
              elif existing == "skip":
                  lgr.info(_msg + " Skipping")
                  return
              elif existing == "replace":
                  remove = False
                  if path_children:
                      has_git = ".git" in path_children
                      _msg_stats = (
                          _msg
                          + " It is %sa git repository and has %d files/dirs."
                          % (
                              "" if has_git else "not ",
                              len(path_children),
                          )
                      )
                      if ui.is_interactive:
                          remove = ui.yesno(
                              "Do you really want to remove it?",
                              title=_msg_stats,
                              default=False,
                          )
                      else:
                          raise RuntimeError(
                              _msg_stats
                              + " Remove it manually first or rerun datalad in "
                              "interactive shell to confirm this action."
                          )
                  if not remove:
                      raise RuntimeError(_msg)
                  # Remote location might already contain a git repository or be
                  # just a directory.
                  lgr.info(_msg + " Replacing")
                  # enable write permissions to allow removing dir
                  shell(
                      "chmod -R +r+w {}".format(sh_quote(remoteds_path))
                  )
                  # remove target at path
                  shell("rm -rf {}".format(sh_quote(remoteds_path)))
                  # if we succeeded in removing it
                  path_exists = False
                  # Since it is gone now, git-annex also should forget about it
                  remotes = ds_repo.get_remotes()
                  if name in remotes:
                      # so we had this remote already, we should announce it dead
                      # XXX what if there was some kind of mismatch and this name
                      # isn't matching the actual remote UUID?  should have we
                      # checked more carefully?
                      lgr.info(
                          "Announcing existing remote %s dead to annex and removing",
                          name,
                      )
                      if isinstance(ds_repo, AnnexRepo):
                          ds_repo.set_remote_dead(name)
                      ds_repo.remove_remote(name)
              elif existing == "reconfigure":
                  lgr.info(_msg + " Will only reconfigure")
                  only_reconfigure = True
              else:
                  raise ValueError(
                      "Do not know how to handle existing={}".format(
                          repr(existing)
                      )
                  )
          if not path_exists:
              shell("{} {}".format(mkdir_cmd, sh_quote(remoteds_path)))
  
      delayed_super = _DelayedSuper(ds)
      if inherit and delayed_super.super:
          if shared is None:
              # here we must analyze current_ds's super, not the super_ds
              # inherit from the setting on remote end
              shared = CreateSibling._get_ds_remote_shared_setting(
                  delayed_super, name, shell
              )
  
          if not install_postupdate_hook:
              # Even though directive from above was False due to no UI explicitly
              # requested, we were asked to inherit the setup, so we might need
              # to install the hook, if super has it on remote
              install_postupdate_hook = (
                  CreateSibling._has_active_postupdate(
                      delayed_super, name, shell
                  )
              )
  
      if group:
          # Either repository existed before or a new directory was created for it,
          # set its group to a desired one if was provided with the same chgrp
          shell(
              "chgrp -R {} {}".format(
                  sh_quote(str(group)), sh_quote(remoteds_path)
              )
          )
      # don't (re-)initialize dataset if existing == reconfigure
      if not only_reconfigure:
          # init git and possibly annex repo
          if not CreateSibling.init_remote_repo(
              remoteds_path, shell, shared, ds, description=target_url
          ):
              return
  
          if target_url and not is_ssh(target_url):
              # we are not coming in via SSH, hence cannot assume proper
              # setup for webserver access -> fix
              shell(
                  "git -C {} update-server-info".format(
                      sh_quote(remoteds_path)
                  )
              )
      else:
          # TODO -- we might still want to reconfigure 'shared' setting!
          pass
  
      # at this point we have a remote sibling in some shape or form
      # -> add as remote
      lgr.debug("Adding the siblings")
      # TODO generator, yield the now swallowed results
      Siblings.__call__(
          "configure",
          dataset=ds,
          name=name,
          url=ds_target_url,
          pushurl=ds_target_pushurl,
          recursive=False,
          fetch=True,
          as_common_datasrc=as_common_datasrc,
          publish_by_default=publish_by_default,
          publish_depends=publish_depends,
          annex_wanted=annex_wanted,
          annex_group=annex_group,
          annex_groupwanted=annex_groupwanted,
          inherit=inherit,
          result_renderer="disabled",
      )
  
      # check git version on remote end
      lgr.info("Adjusting remote git configuration")
      if (
          shell.get_git_version()
          and shell.get_git_version() >= LooseVersion("2.4")
      ):
          # allow for pushing to checked out branch
          try:
              shell(
                  "git -C {} config receive.denyCurrentBranch updateInstead".format(
                      sh_quote(remoteds_path)
                  )
              )
          except CommandError as e:
              ce = CapturedException(e)
              lgr.error(
                  "git config failed at remote location %s.\n"
                  "You will not be able to push to checked out "
                  "branch. Error: %s",
                  remoteds_path,
                  ce,
              )
      else:
          lgr.error(
              "Git version >= 2.4 needed to configure remote."
              " Version detected on server: %s\nSkipping configuration"
              " of receive.denyCurrentBranch - you will not be able to"
              " publish updates to this repository. Upgrade your git"
              " and run with --existing=reconfigure",
              shell.get_git_version(),
          )
  
      branch = ds_repo.get_active_branch()
      if branch is not None:
          branch = ds_repo.get_corresponding_branch(branch) or branch
          # Setting the HEAD for the created sibling to the original repo's
          # current branch should be unsurprising, and it helps with consumers
          # that don't properly handle the default branch with no commits. See
          # gh-4349.
          shell(
              "git -C {} symbolic-ref HEAD refs/heads/{}".format(
                  sh_quote(remoteds_path), branch
              )
          )
  
      if install_postupdate_hook:
          # enable metadata refresh on dataset updates to publication server
          lgr.info("Enabling git post-update hook ...")
          try:
              CreateSibling.create_postupdate_hook(
                  remoteds_path, shell, ds
              )
          except CommandError as e:
              ce = CapturedException(e)
              lgr.error(
                  "Failed to add json creation command to post update "
                  "hook.\nError: %s",
                  ce,
              )
  
      return remoteds_path
  
imports: import logging
  import os
  from os.path import curdir
  import shlex
  from os.path import join
  from os.path import normpath, relpath
  from looseversion import LooseVersion
  from datalad import ssh_manager
  from datalad.cmd import CommandError, StdOutErrCapture, WitlessRunner
  from datalad.consts import TIMESTAMP_FMT, WEB_META_LOG
  from datalad.core.local.diff import diff_dataset
  from datalad.distribution.dataset import Dataset, EnsureDataset, datasetmethod, require_dataset, resolve_path
  from datalad.distribution.siblings import Siblings, _DelayedSuper
  from datalad.interface.base import Interface, build_doc, eval_results
  from datalad.interface.common_opts import annex_group_opt, annex_groupwanted_opt, annex_wanted_opt, as_common_datasrc, inherit_opt, publish_by_default, publish_depends, recursion_flag, recursion_limit
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.constraints import EnsureBool, EnsureChoice, EnsureNone, EnsureStr
  from datalad.support.exceptions import CapturedException, InsufficientArgumentsError, MissingExternalDependency
  from datalad.support.external_versions import external_versions
  from datalad.support.network import RI, PathRI, is_ssh
  from datalad.support.param import Parameter
  from datalad.ui import ui
  from datalad.utils import _path_, ensure_list, make_tempfile, on_windows
  from datalad.utils import quote_cmdlinearg
  from datalad.utils import slash_join
label_desc: Use image node_id 1 for calling a global function with example usage: _create_dataset_sibling(name, ds, hierarchy_basepath, shell, replicate_local_structure, ri, target_dir, target_url, target_pushurl, existing, shared, group, publish_depends, publish_by_default, install_postupdate_hook, as_common_datasrc, annex_wanted, annex_group, annex_groupwanted, inherit) and returns: remoteds_path