code: def test_surprise_subds(path=None):
      # https://github.com/datalad/datalad/issues/3139
      ds = create(path, force=True)
      # a lonely repo without any commit
      somerepo = AnnexRepo(
          path=op.join(path, "d1", "subrepo"), create=True
      )
      # a proper subdataset
      subds = create(op.join(path, "d2", "subds"), force=True)
  
      # If subrepo is an adjusted branch, it would have a commit, making most of
      # this test irrelevant because it is about the unborn branch edge case.
      adjusted = somerepo.is_managed_branch()
      # This edge case goes away with Git v2.22.0.
      fixed_git = somerepo.git_version >= "2.22.0"
  
      # save non-recursive
      res = ds.save(recursive=False, on_failure="ignore")
      if not adjusted and fixed_git:
          # We get an appropriate error about no commit being checked out.
          assert_in_results(res, action="add_submodule", status="error")
  
      # the content of both subds and subrepo are not added to their
      # respective parent as no --recursive was given
      assert_repo_status(subds.path, untracked=["subfile"])
      assert_repo_status(somerepo.path, untracked=["subfile"])
  
      if adjusted or fixed_git:
          if adjusted:
              # adjusted branch: #datalad/3178 (that would have a commit)
              modified = [subds.repo.pathobj, somerepo.pathobj]
              untracked = []
          else:
              # Newer Git versions refuse to add a sub-repository with no commits
              # checked out.
              modified = [subds.repo.pathobj]
              untracked = ["d1"]
          assert_repo_status(
              ds.path, modified=modified, untracked=untracked
          )
          assert_not_in(
              ds.repo.pathobj / "d1" / "subrepo" / "subfile",
              ds.repo.get_content_info(),
          )
      else:
          # however, while the subdataset is added (and reported as modified
          # because it content is still untracked) the subrepo
          # cannot be added (it has no commit)
          # worse: its untracked file add been added to the superdataset
          assert_repo_status(ds.path, modified=["d2/subds"])
          assert_in(
              ds.repo.pathobj / "d1" / "subrepo" / "subfile",
              ds.repo.get_content_info(),
          )
      # with proper subdatasets, all evil is gone
      assert_not_in(
          ds.repo.pathobj / "d2" / "subds" / "subfile",
          ds.repo.get_content_info(),
      )
  
imports: import itertools
  import logging
  import os
  import os.path
  import pytest
  import datalad.utils
  from datalad.api import create, install, save
  from datalad.distribution.dataset import Dataset
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.exceptions import CommandError
  from datalad.tests.utils_pytest import DEFAULT_BRANCH, OBSCURE_FILENAME, SkipTest, assert_in, assert_in_results, assert_not_in, assert_raises, assert_repo_status, assert_result_count, assert_status, chpwd, create_tree, eq_, known_failure, known_failure_windows, maybe_adjust_repo, neq_, ok_, patch, skip_if_adjusted_branch, skip_wo_symlink_capability, swallow_logs, swallow_outputs, with_tempfile, with_tree
  from datalad.utils import Path, ensure_list, rmtree
label_desc: Use image node_id 26 for calling a global function with example usage: test_surprise_subds(path) without return types