code: def test_rerun_branch(path=None):
      ds = Dataset(path).create()
      if ds.repo.is_managed_branch():
          assert_status(
              "impossible",
              ds.rerun(branch="triggers-abort", on_failure="ignore"),
          )
          raise SkipTest("Test incompatible with adjusted branch")
  
      ds.repo.tag("prerun")
  
      outfile = op.join(path, "run-file")
  
      with swallow_outputs():
          ds.run(f"echo x$({cat_command} run-file) > run-file")
      ds.rerun()
      eq_("xx\n", open(outfile).read())
  
      with open(op.join(path, "nonrun-file"), "w") as f:
          f.write("foo")
      ds.save("nonrun-file")
  
      # Rerun the commands on a new branch that starts at the parent
      # commit of the first run.
      with swallow_outputs():
          ds.rerun(since="prerun", onto="prerun", branch="rerun")
  
      eq_(ds.repo.get_active_branch(), "rerun")
      eq_("xx\n", open(outfile).read())
  
      # NOTE: This test depends on the non-run commit above following a run
      # commit.  Otherwise, all the metadata (e.g., author date) aside from the
      # parent commit that is used to generate the commit ID may be set when
      # running the tests, which would result in two commits rather than three.
      for revrange in [
          "rerun.." + DEFAULT_BRANCH,
          DEFAULT_BRANCH + "..rerun",
      ]:
          eq_(len(ds.repo.get_revisions(revrange)), 3)
      eq_(
          ds.repo.get_merge_base([DEFAULT_BRANCH, "rerun"]),
          ds.repo.get_hexsha("prerun"),
      )
  
      # Start rerun branch at tip of current branch.
      ds.repo.checkout(DEFAULT_BRANCH)
      ds.rerun(since="prerun", branch="rerun2")
      eq_(ds.repo.get_active_branch(), "rerun2")
      eq_("xxxx\n", open(outfile).read())
  
      eq_(len(ds.repo.get_revisions(DEFAULT_BRANCH + "..rerun2")), 2)
      eq_(len(ds.repo.get_revisions("rerun2.." + DEFAULT_BRANCH)), 0)
  
      # Using an existing branch name fails.
      ds.repo.checkout(DEFAULT_BRANCH)
      assert_raises(
          IncompleteResultsError,
          ds.rerun,
          since="prerun",
          branch="rerun2",
      )
  
imports: import logging
  import os.path
  import sys
  from io import StringIO
  from os import remove
  import pytest
  from unittest.mock import patch
  from datalad.api import install, run
  from datalad.core.local.run import run_command
  from datalad.core.local.tests.test_run import last_commit_msg
  from datalad.distribution.dataset import Dataset
  from datalad.local.rerun import diff_revision, get_run_info, new_or_modified
  from datalad.support.exceptions import CommandError, IncompleteResultsError
  from datalad.support.gitrepo import GitRepo
  from datalad.tests.utils_pytest import DEFAULT_BRANCH, DEFAULT_REMOTE, SkipTest, assert_dict_equal, assert_false, assert_in, assert_in_results, assert_not_in, assert_not_in_results, assert_raises, assert_repo_status, assert_result_count, assert_status, create_tree, eq_, known_failure_windows, neq_, ok_, ok_exists, ok_file_has_content, ok_file_under_git, skip_if_adjusted_branch, slow, swallow_logs, swallow_outputs, with_tempfile, with_tree
  from datalad.utils import Path, chpwd, on_windows
label_desc: Use image node_id 7 for calling a global function with example usage: test_rerun_branch(path) without return types