code: def _rerun(
      dset, results, assume_ready=None, explicit=False, jobs=None
  ):
      ds_repo = dset.repo
      # Keep a map from an original hexsha to a new hexsha created by the rerun
      # (i.e. a reran, cherry-picked, or merged commit).
      new_bases = {}  # original hexsha => reran hexsha
      branch_to_restore = ds_repo.get_active_branch()
      head = onto = ds_repo.get_hexsha()
      for res in results:
          lgr.info(_get_rerun_log_msg(res))
          rerun_action = res.get("rerun_action")
          if not rerun_action:
              yield res
              continue
  
          res_hexsha = res["commit"]
          if rerun_action == "checkout":
              if res.get("branch"):
                  branch = res["branch"]
                  checkout_options = ["-b", branch]
                  branch_to_restore = branch
              else:
                  checkout_options = ["--detach"]
                  branch_to_restore = None
              ds_repo.checkout(res_hexsha, options=checkout_options)
              head = onto = res_hexsha
              continue
  
          # First handle the two cases that don't require additional steps to
          # identify the base, a root commit or a merge commit.
  
          if not res["parents"]:
              _mark_nonrun_result(res, "skip")
              yield res
              continue
  
          if rerun_action == "merge":
              old_parents = res["parents"]
              new_parents = [new_bases.get(p, p) for p in old_parents]
              if old_parents == new_parents:
                  if not ds_repo.is_ancestor(res_hexsha, head):
                      ds_repo.checkout(res_hexsha)
              elif res_hexsha != head:
                  if ds_repo.is_ancestor(res_hexsha, onto):
                      new_parents = [
                          p
                          for p in new_parents
                          if not ds_repo.is_ancestor(p, onto)
                      ]
                  if new_parents:
                      if new_parents[0] != head:
                          # Keep the direction of the original merge.
                          ds_repo.checkout(new_parents[0])
                      if len(new_parents) > 1:
                          msg = ds_repo.format_commit("%B", res_hexsha)
                          ds_repo.call_git(
                              [
                                  "merge",
                                  "-m",
                                  msg,
                                  "--no-ff",
                                  "--allow-unrelated-histories",
                              ]
                              + new_parents[1:]
                          )
                      head = ds_repo.get_hexsha()
                      new_bases[res_hexsha] = head
              yield res
              continue
  
          # For all the remaining actions, first make sure we're on the
          # appropriate base.
  
          parent = res["parents"][0]
          new_base = new_bases.get(parent)
          head_to_restore = None  # ... to find our way back if we skip.
  
          if new_base:
              if new_base != head:
                  ds_repo.checkout(new_base)
                  head_to_restore, head = head, new_base
          elif parent != head and ds_repo.is_ancestor(onto, parent):
              if rerun_action == "run":
                  ds_repo.checkout(parent)
                  head = parent
              else:
                  _mark_nonrun_result(res, "skip")
                  yield res
                  continue
          else:
              if parent != head:
                  new_bases[parent] = head
  
          # We've adjusted base. Now skip, pick, or run the commit.
  
          if rerun_action == "skip-or-pick":
              if ds_repo.is_ancestor(res_hexsha, head):
                  _mark_nonrun_result(res, "skip")
                  if head_to_restore:
                      ds_repo.checkout(head_to_restore)
                      head, head_to_restore = head_to_restore, None
                  yield res
                  continue
              else:
                  ds_repo.cherry_pick(res_hexsha)
                  _mark_nonrun_result(res, "pick")
                  yield res
          elif rerun_action == "run":
              run_info = res["run_info"]
              # Keep a "rerun" trail.
              if "chain" in run_info:
                  run_info["chain"].append(res_hexsha)
              else:
                  run_info["chain"] = [res_hexsha]
  
              # now we have to find out what was modified during the last run,
              # and enable re-modification ideally, we would bring back the
              # entire state of the tree with #1424, but we limit ourself to file
              # addition/not-in-place-modification for now
              auto_outputs = (
                  ap["path"] for ap in new_or_modified(res["diff"])
              )
              outputs = run_info.get("outputs", [])
              outputs_dir = op.join(dset.path, run_info["pwd"])
              auto_outputs = [
                  p
                  for p in auto_outputs
                  # run records outputs relative to the "pwd" field.
                  if op.relpath(p, outputs_dir) not in outputs
              ]
  
              message = res["rerun_message"] or res["run_message"]
              for r in run_command(
                  run_info["cmd"],
                  dataset=dset,
                  inputs=run_info.get("inputs", []),
                  extra_inputs=run_info.get("extra_inputs", []),
                  outputs=outputs,
                  assume_ready=assume_ready,
                  explicit=explicit,
                  rerun_outputs=auto_outputs,
                  message=message,
                  jobs=jobs,
                  rerun_info=run_info,
              ):
                  yield r
          new_head = ds_repo.get_hexsha()
          if new_head not in [head, res_hexsha]:
              new_bases[res_hexsha] = new_head
          head = new_head
  
      if branch_to_restore:
          # The user asked us to replay the sequence onto a branch, but the
          # history had merges, so we're in a detached state.
          ds_repo.update_ref("refs/heads/" + branch_to_restore, "HEAD")
          ds_repo.checkout(branch_to_restore)
  
imports: import json
  import logging
  import os.path
  import re
  import sys
  from copy import copy
  from functools import partial
  from itertools import dropwhile
  from datalad.consts import PRE_INIT_COMMIT_SHA
  from datalad.core.local.run import _format_cmd_shorty, assume_ready_opt, format_command, run_command
  from datalad.distribution.dataset import EnsureDataset, datasetmethod, require_dataset
  from datalad.interface.base import Interface, build_doc, eval_results
  from datalad.interface.results import get_status_dict
  from datalad.interface.common_opts import jobs_opt
  from datalad.support.constraints import EnsureNone, EnsureStr
  from datalad.support.exceptions import CapturedException
  from datalad.support.json_py import load_stream
  from datalad.support.param import Parameter
label_desc: Use image node_id 4 for calling a global function with example usage: _rerun(dset, results, assume_ready, explicit, jobs) without return types