code: def _diff_ds(
      ds,
      fr,
      to,
      constant_refs,
      recursion_level,
      origpaths,
      untracked,
      annexinfo,
      cache,
      order="depth-first",
      datasets_only=False,
  ):
      if not ds.is_installed():
          # asked to query a subdataset that is not available
          lgr.debug("Skip diff of unavailable subdataset: %s", ds)
          return
  
      repo = ds.repo
      repo_path = repo.pathobj
      if datasets_only:
          assert (
              not origpaths
          )  # protected above with NotImplementedError
          paths = dict(
              (sds.pathobj.relative_to(ds.pathobj), False)
              for sds in ds.subdatasets(
                  recursive=False,
                  state="present",
                  result_renderer="disabled",
                  result_xfm="datasets",
              )
          )
          if not paths:
              # no subdatasets, nothing todo???
              return
      else:
          # filter and normalize paths that match this dataset before passing them
          # onto the low-level query method
          paths = (
              None
              if origpaths is None
              else dict(
                  (repo_path / p.relative_to(ds.pathobj), goinside)
                  for p, goinside in origpaths.items()
                  if ds.pathobj in p.parents
                  or (p == ds.pathobj and goinside)
              )
          )
      paths_arg = list(paths) if paths else None
      try:
          lgr.debug("Diff %s from '%s' to '%s'", ds, fr, to)
          diff_state = repo.diffstatus(
              fr,
              to,
              paths=paths_arg,
              untracked=untracked,
              eval_submodule_state="full" if to is None else "commit",
              _cache=cache,
          )
      except InvalidGitReferenceError as e:
          yield dict(
              path=ds.path,
              status="impossible",
              message=str(e),
          )
          return
  
      if annexinfo and hasattr(repo, "get_content_annexinfo"):
          # this will amend `diff_state`
          repo.get_content_annexinfo(
              paths=paths_arg,
              init=diff_state,
              eval_availability=annexinfo in ("availability", "all"),
              ref=to,
          )
          # if `fr` is None, we compare against a preinit state, and
          # a get_content_annexinfo on that state doesn't get us anything new
          if fr and fr != to:
              repo.get_content_annexinfo(
                  paths=paths_arg,
                  init=diff_state,
                  eval_availability=annexinfo
                  in ("availability", "all"),
                  ref=fr,
                  key_prefix="prev_",
              )
  
      # potentially collect subdataset diff call specs for the end
      # (if order == 'breadth-first')
      ds_diffs = []
      subds_diffcalls = []
      for path, props in diff_state.items():
          pathinds = str(ds.pathobj / path.relative_to(repo_path))
          path_rec = dict(
              props,
              path=pathinds,
              # report the dataset path rather than the repo path to avoid
              # realpath/symlink issues
              parentds=ds.path,
              status="ok",
          )
          if order in ("breadth-first", "depth-first"):
              yield path_rec
          elif order == "bottom-up":
              ds_diffs.append(path_rec)
          else:
              raise ValueError(order)
          # for a dataset we need to decide whether to dive in, or not
          if props.get("type", None) == "dataset" and (
              # subdataset path was given in rsync-style 'ds/'
              (paths and paths.get(path, False))
              # there is still sufficient recursion level left
              or recursion_level != 0
              # no recursion possible anymore, but one of the given
              # path arguments is in this subdataset
              or (
                  recursion_level == 0
                  and paths
                  and any(path in p.parents for p in paths)
              )
          ):
              subds_state = props.get("state", None)
              if subds_state in ("clean", "deleted"):
                  # no need to look into the subdataset
                  continue
              elif subds_state in ("added", "modified"):
                  # dive
                  subds = Dataset(pathinds)
                  call_args = (
                      subds,
                      # from before time or from the reported state
                      fr
                      if constant_refs
                      else None
                      if subds_state == "added"
                      else props["prev_gitshasum"],
                      # to the last recorded state, or the worktree
                      None
                      if to is None
                      else to
                      if constant_refs
                      else props["gitshasum"],
                      constant_refs,
                  )
                  call_kwargs = dict(
                      # subtract on level on the way down, unless the path
                      # args instructed to go inside this subdataset
                      recursion_level=recursion_level
                      # protect against dropping below zero (would mean unconditional
                      # recursion)
                      if not recursion_level
                      or (paths and paths.get(path, False))
                      else recursion_level - 1,
                      origpaths=origpaths,
                      untracked=untracked,
                      annexinfo=annexinfo,
                      cache=cache,
                      order=order,
                      datasets_only=datasets_only,
                  )
                  if order in ("depth-first", "bottom-up"):
                      yield from _diff_ds(*call_args, **call_kwargs)
                  elif order == "breadth-first":
                      subds_diffcalls.append((call_args, call_kwargs))
                  else:
                      raise ValueError(order)
              else:
                  raise RuntimeError(
                      "Unexpected subdataset state '{}'. That sucks!".format(
                          subds_state
                      )
                  )
      # deal with staged ds diffs (for bottom-up)
      for rec in ds_diffs:
          yield rec
      # deal with staged subdataset diffs (for breadth-first)
      for call_args, call_kwargs in subds_diffcalls:
          yield from _diff_ds(*call_args, **call_kwargs)
  
file_path: datalad/datalad/core/local/diff.py
imports: import logging
  import os.path
  from datalad.utils import ensure_list, ensure_unicode, get_dataset_root
  from datalad.interface.base import Interface, build_doc, eval_results
  from datalad.distribution.dataset import Dataset, datasetmethod, require_dataset, resolve_path, path_under_rev_dataset
  from datalad.support.constraints import EnsureNone, EnsureStr
  from datalad.support.param import Parameter
  from datalad.core.local.status import Status, _common_diffstatus_params
  from datalad.support.exceptions import InvalidGitReferenceError
label_desc: Use image node_id 2 for calling a global function with example usage: _diff_ds(ds, fr, to, constant_refs, recursion_level, origpaths, untracked, annexinfo, cache, order, datasets_only) without return types