code: def test_branch(tmp_dir, scm, dvc, exp_stage):
      from dvc.exceptions import InvalidArgumentError
  
      with pytest.raises(InvalidArgumentError):
          dvc.experiments.branch("foo", "branch")
  
      scm.branch("branch-exists")
  
      results = dvc.experiments.run(
          exp_stage.addressing, params=["foo=2"], name="foo"
      )
      exp_a = first(results)
      ref_a = dvc.experiments.get_branch_by_rev(exp_a)
  
      with pytest.raises(InvalidArgumentError):
          dvc.experiments.branch("foo", "branch-exists")
      dvc.experiments.branch("foo")
      dvc.experiments.branch("foo", "branch-name")
      dvc.experiments.branch(exp_a, "branch-rev")
      dvc.experiments.branch(ref_a, "branch-ref")
  
      for name in [
          "foo-branch",
          "branch-name",
          "branch-rev",
          "branch-ref",
      ]:
          assert name in scm.list_branches()
          assert scm.resolve_rev(name) == exp_a
  
      tmp_dir.scm_gen({"new_file": "new_file"}, commit="new baseline")
      results = dvc.experiments.run(
          exp_stage.addressing, params=["foo=2"], name="foo"
      )
      exp_b = first(results)
      ref_b = dvc.experiments.get_branch_by_rev(exp_b)
  
      with pytest.raises(InvalidArgumentError):
          dvc.experiments.branch("foo", "branch-name")
      dvc.experiments.branch(ref_b, "branch-ref-b")
  
      assert "branch-ref-b" in scm.list_branches()
      assert scm.resolve_rev("branch-ref-b") == exp_b
  
imports: import itertools
  import logging
  import os
  import stat
  from textwrap import dedent
  import pytest
  from configobj import ConfigObj
  from funcy import first
  from dvc.dvcfile import PROJECT_FILE
  from dvc.env import DVC_EXP_BASELINE_REV, DVC_EXP_NAME, DVC_ROOT, DVC_STUDIO_OFFLINE, DVC_STUDIO_REPO_URL, DVC_STUDIO_TOKEN, DVC_STUDIO_URL
  from dvc.exceptions import DvcException, ReproductionError
  from dvc.repo.experiments.exceptions import ExperimentExistsError
  from dvc.repo.experiments.queue.base import BaseStashQueue
  from dvc.repo.experiments.refs import CELERY_STASH
  from dvc.repo.experiments.utils import exp_refs_by_rev
  from dvc.scm import SCMError, resolve_rev
  from dvc.stage.exceptions import StageFileDoesNotExistError
  from dvc.testing.scripts import COPY_SCRIPT
  from dvc.utils.serialize import PythonFileCorruptedError
label_desc: Use image node_id 8 for calling a global function with example usage: test_branch(tmp_dir, scm, dvc, exp_stage) without return types