code: def _append_branch_to_refspec_if_needed(ds, refspecs, branch):
      # try to anticipate any flavor of an idea of a branch ending up in a refspec
      looks_like_that_branch = re.compile(
          r"((^|.*:)refs/heads/|.*:|^){}$".format(branch)
      )
      if all(not looks_like_that_branch.match(r) for r in refspecs):
          refspecs.append(
              branch
              if ds.config.get("branch.{}.merge".format(branch), None)
              else "{branch}:{branch}".format(branch=branch)
          )
  
imports: from itertools import chain
  import logging
  import re
  from datalad.interface.base import Interface, build_doc, eval_results
  from datalad.interface.common_opts import jobs_opt, recursion_limit, recursion_flag
  from datalad.interface.utils import render_action_summary
  from datalad.interface.results import annexjson2result
  from datalad.log import log_progress
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.gitrepo import GitRepo
  from datalad.support.param import Parameter
  from datalad.support.constraints import EnsureStr, EnsureNone, EnsureChoice
  from datalad.support.exceptions import CommandError
  from datalad.utils import Path, ensure_list, todo_interface_for_extensions
  from datalad.distribution.dataset import Dataset, EnsureDataset, datasetmethod, require_dataset, resolve_path
  from datalad.core.local.diff import diff_dataset
label_desc: Use image node_id 4 for calling a global function with example usage: _append_branch_to_refspec_if_needed(ds, refspecs, branch) without return types