From 2cbe25ac90c455e30348b9c2a2bf023808bc323b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Thu, 25 Nov 2021 13:47:23 +0545 Subject: [PATCH] scm: move warning on unsupported index to scmcontext Also makes `Repo.init` avoid using `scm.add` directly. --- dvc/repo/init.py | 18 +++++++----- dvc/repo/scm_context.py | 47 ++++++++++++++++++++++++++------ dvc/scm/exceptions.py | 4 +++ dvc/scm/git/backend/gitpython.py | 12 ++------ 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/dvc/repo/init.py b/dvc/repo/init.py index aca63b478..1bed86a46 100644 --- a/dvc/repo/init.py +++ b/dvc/repo/init.py @@ -77,14 +77,18 @@ def init(root_dir=os.curdir, no_scm=False, force=False, subdir=False): proj.plots.templates.init() - scm.add( - [config.files["repo"], dvcignore, proj.plots.templates.templates_dir] - ) + with proj.scm_context(autostage=True) as context: + files = [ + config.files["repo"], + dvcignore, + proj.plots.templates.templates_dir, + ] + ignore_file = context.scm.ignore_file + if ignore_file: + files.extend([os.path.join(dvc_dir, ignore_file)]) + proj.scm_context.track_file(files) logger.info("Initialized DVC repository.\n") - - if scm.ignore_file: - scm.add([os.path.join(dvc_dir, scm.ignore_file)]) + if not no_scm: logger.info("You can now commit the changes to git.\n") - return proj diff --git a/dvc/repo/scm_context.py b/dvc/repo/scm_context.py index 436bbb4a3..c14ef57bf 100644 --- a/dvc/repo/scm_context.py +++ b/dvc/repo/scm_context.py @@ -2,9 +2,19 @@ import logging import shlex from contextlib import contextmanager from functools import wraps -from typing import TYPE_CHECKING, Any, Dict, Iterator, List, Set +from typing import ( + TYPE_CHECKING, + Any, + Dict, + Iterable, + Iterator, + List, + Set, + Union, +) from dvc.utils import relpath +from dvc.utils.collections import ensure_list if TYPE_CHECKING: from dvc.repo import Repo @@ -26,17 +36,38 @@ class SCMContext: self.files_to_track: Set[str] = set() self.quiet: bool = False - def track_file(self, path: str) -> None: + def track_file( + self, paths: Union[str, Iterable[str], None] = None + ) -> None: """Track file to remind user to track new files or autostage later.""" - return self.files_to_track.add(path) + return self.files_to_track.update(ensure_list(paths)) + + @staticmethod + def _make_git_add_cmd(paths: Union[str, Iterable[str]]) -> str: + files = " ".join(map(shlex.quote, ensure_list(paths))) + return f"\tgit add {files}".expandtabs(4) + + def add(self, paths: Union[str, Iterable[str]]) -> None: + from dvc.scm.exceptions import UnsupportedIndexFormat + + try: + return self.scm.add(paths) + except UnsupportedIndexFormat: + link = "https://github.com/iterative/dvc/issues/610" + add_cmd = self._make_git_add_cmd([relpath(path) for path in paths]) + logger.info("") + msg = ( + f"failed to add, add manually using:\n\n{add_cmd}\n" + f"\nSee {link} for more details.\n" + ) + logger.warning(msg) def track_changed_files(self) -> None: """Stage files that have changed.""" if not self.files_to_track: return - logger.debug("Staging files: %s", self.files_to_track) - return self.scm.add(self.files_to_track) + return self.add(self.files_to_track) def ignore(self, path: str) -> None: from dvc.scm import SCMError @@ -91,11 +122,9 @@ class SCMContext: if autostage: self.track_changed_files() elif not quiet and not isinstance(self.scm, NoSCM): - files = " ".join(map(shlex.quote, self.files_to_track)) - + add_cmd = self._make_git_add_cmd(self.files_to_track) logger.info( - "\nTo track the changes with git, run:\n\n" - f"\tgit add {files}" + f"\nTo track the changes with git, run:\n" f"\n{add_cmd}" ) logger.info( "\nTo enable auto staging, run:\n\n" diff --git a/dvc/scm/exceptions.py b/dvc/scm/exceptions.py index 622c7f6c7..d597fdb9e 100644 --- a/dvc/scm/exceptions.py +++ b/dvc/scm/exceptions.py @@ -39,3 +39,7 @@ class CloneError(SCMError): class RevError(SCMError): pass + + +class UnsupportedIndexFormat(SCMError): + pass diff --git a/dvc/scm/git/backend/gitpython.py b/dvc/scm/git/backend/gitpython.py index 3fa037e26..549479d94 100644 --- a/dvc/scm/git/backend/gitpython.py +++ b/dvc/scm/git/backend/gitpython.py @@ -23,6 +23,7 @@ from dvc.scm.exceptions import ( MergeConflictError, RevError, SCMError, + UnsupportedIndexFormat, ) from dvc.scm.utils import relpath @@ -221,15 +222,8 @@ class GitPythonBackend(BaseGitBackend): # pylint:disable=abstract-method self.git.add(*paths, update=True) else: self.repo.index.add(paths) - except AssertionError: - msg = ( - "failed to add '{}' to git. You can add those files " - "manually using `git add`. See " - "https://github.com/iterative/dvc/issues/610 for more " - "details.".format(str(paths)) - ) - - logger.exception(msg) + except AssertionError as exc: + raise UnsupportedIndexFormat from exc def commit(self, msg: str, no_verify: bool = False): from git.exc import HookExecutionError