Creates an empty repo on the given directory -- basically a `.dvc` directory with subdirectories for configuration and cache. It should be tracked by a SCM or use the `--no-scm` flag. If the given directory is not empty, you must use the `--force` flag to override it. Arg
(root_dir=os.curdir, no_scm=False, force=False, subdir=False)
| 13 | |
| 14 | |
| 15 | def init(root_dir=os.curdir, no_scm=False, force=False, subdir=False): # noqa: C901 |
| 16 | """ |
| 17 | Creates an empty repo on the given directory -- basically a |
| 18 | `.dvc` directory with subdirectories for configuration and cache. |
| 19 | |
| 20 | It should be tracked by a SCM or use the `--no-scm` flag. |
| 21 | |
| 22 | If the given directory is not empty, you must use the `--force` |
| 23 | flag to override it. |
| 24 | |
| 25 | Args: |
| 26 | root_dir: Path to repo's root directory. |
| 27 | |
| 28 | Returns: |
| 29 | Repo instance. |
| 30 | |
| 31 | Raises: |
| 32 | KeyError: Raises an exception. |
| 33 | """ |
| 34 | |
| 35 | if no_scm and subdir: |
| 36 | raise InvalidArgumentError( |
| 37 | "Cannot initialize repo with `--no-scm` and `--subdir`" |
| 38 | ) |
| 39 | |
| 40 | root_dir = os.path.abspath(root_dir) |
| 41 | dvc_dir = os.path.join(root_dir, Repo.DVC_DIR) |
| 42 | |
| 43 | try: |
| 44 | scm = SCM(root_dir, search_parent_directories=subdir, no_scm=no_scm) |
| 45 | except SCMError: |
| 46 | raise InitError( # noqa: B904 |
| 47 | f"{root_dir} is not tracked by any supported SCM tool (e.g. Git). " |
| 48 | "Use `--no-scm` if you don't want to use any SCM or " |
| 49 | "`--subdir` if initializing inside a subdirectory of a parent SCM " |
| 50 | "repository." |
| 51 | ) |
| 52 | |
| 53 | if scm.is_ignored(dvc_dir): |
| 54 | raise InitError( |
| 55 | f"{dvc_dir} is ignored by your SCM tool. \n" |
| 56 | "Make sure that it's tracked, " |
| 57 | "for example, by adding '!.dvc' to .gitignore." |
| 58 | ) |
| 59 | |
| 60 | if os.path.isdir(dvc_dir): |
| 61 | if not force: |
| 62 | raise InitError(f"'{relpath(dvc_dir)}' exists. Use `-f` to force.") |
| 63 | |
| 64 | remove(dvc_dir) |
| 65 | |
| 66 | os.makedirs(dvc_dir, exist_ok=True) |
| 67 | |
| 68 | config = Config.init(dvc_dir) |
| 69 | |
| 70 | if no_scm: |
| 71 | with config.edit() as conf: |
| 72 | conf["core"]["no_scm"] = True |
no test coverage detected