diff --git a/dvc/scm/git/backend/base.py b/dvc/scm/git/backend/base.py index d2bc24935..6fe9af08e 100644 --- a/dvc/scm/git/backend/base.py +++ b/dvc/scm/git/backend/base.py @@ -1,6 +1,6 @@ import os from abc import ABC, abstractmethod -from typing import Callable, Iterable, Mapping, Optional, Tuple +from typing import Callable, Iterable, Mapping, Optional, Tuple, Union from dvc.scm.base import SCMError @@ -52,7 +52,7 @@ class BaseGitBackend(ABC): pass @abstractmethod - def add(self, paths: Iterable[str], update=False): + def add(self, paths: Union[str, Iterable[str]], update=False): pass @abstractmethod diff --git a/dvc/scm/git/backend/dulwich.py b/dvc/scm/git/backend/dulwich.py index ea01d09ed..b628eac82 100644 --- a/dvc/scm/git/backend/dulwich.py +++ b/dvc/scm/git/backend/dulwich.py @@ -4,7 +4,16 @@ import logging import os import stat from io import BytesIO, StringIO -from typing import Callable, Dict, Iterable, Mapping, Optional, Tuple +from typing import ( + Callable, + Dict, + Iterable, + List, + Mapping, + Optional, + Tuple, + Union, +) from funcy import cached_property @@ -117,16 +126,19 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method def dir(self) -> str: return self.repo.commondir() - def add(self, paths: Iterable[str], update=False): + def add(self, paths: Union[str, Iterable[str]], update=False): from dvc.utils.fs import walk_files - if update: - raise NotImplementedError + assert paths or update if isinstance(paths, str): paths = [paths] - files = [] + if update and not paths: + self.repo.stage(list(self.repo.open_index())) + return + + files: List[bytes] = [] for path in paths: if not os.path.isabs(path) and self._submodules: # NOTE: If path is inside a submodule, Dulwich expects the @@ -142,13 +154,30 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method ) break if os.path.isdir(path): - files.extend(walk_files(path)) + files.extend( + os.fsencode(relpath(fpath, self.root_dir)) + for fpath in walk_files(path) + ) else: - files.append(path) + files.append(os.fsencode(relpath(path, self.root_dir))) - for fpath in files: - # NOTE: this doesn't check gitignore, same as GitPythonBackend.add - self.repo.stage(relpath(fpath, self.root_dir)) + # NOTE: this doesn't check gitignore, same as GitPythonBackend.add + if update: + index = self.repo.open_index() + if os.name == "nt": + # NOTE: we need git/unix separator to compare against index + # paths but repo.stage() expects to be called with OS paths + self.repo.stage( + [ + fname + for fname in files + if fname.replace(b"\\", b"/") in index + ] + ) + else: + self.repo.stage([fname for fname in files if fname in index]) + else: + self.repo.stage(files) def commit(self, msg: str, no_verify: bool = False): from dulwich.errors import CommitError @@ -561,9 +590,9 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method ) return ( { - status: [os.fsdecode(name)] + status: [os.fsdecode(name) for name in paths] for status, paths in staged.items() - for name in paths + if paths }, [os.fsdecode(name) for name in unstaged], [os.fsdecode(name) for name in untracked], diff --git a/dvc/scm/git/backend/gitpython.py b/dvc/scm/git/backend/gitpython.py index 08678ee30..59061f396 100644 --- a/dvc/scm/git/backend/gitpython.py +++ b/dvc/scm/git/backend/gitpython.py @@ -3,7 +3,7 @@ import locale import logging import os from functools import partial -from typing import Callable, Iterable, List, Mapping, Optional, Tuple +from typing import Callable, Iterable, List, Mapping, Optional, Tuple, Union from funcy import first, ignore @@ -188,11 +188,13 @@ class GitPythonBackend(BaseGitBackend): # pylint:disable=abstract-method def dir(self) -> str: return self.repo.git_dir - def add(self, paths: Iterable[str], update=False): + def add(self, paths: Union[str, Iterable[str]], update=False): # NOTE: GitPython is not currently able to handle index version >= 3. # See https://github.com/iterative/dvc/issues/610 for more details. try: if update: + if isinstance(paths, str): + paths = [paths] self.git.add(*paths, update=True) else: self.repo.index.add(paths) diff --git a/dvc/scm/git/backend/pygit2.py b/dvc/scm/git/backend/pygit2.py index f6c332538..45af40173 100644 --- a/dvc/scm/git/backend/pygit2.py +++ b/dvc/scm/git/backend/pygit2.py @@ -2,7 +2,7 @@ import locale import logging import os from io import BytesIO, StringIO -from typing import Callable, Iterable, List, Mapping, Optional, Tuple +from typing import Callable, Iterable, List, Mapping, Optional, Tuple, Union from dvc.scm.base import MergeConflictError, RevError, SCMError from dvc.utils import relpath @@ -84,7 +84,7 @@ class Pygit2Backend(BaseGitBackend): # pylint:disable=abstract-method def dir(self) -> str: raise NotImplementedError - def add(self, paths: Iterable[str], update=False): + def add(self, paths: Union[str, Iterable[str]], update=False): raise NotImplementedError def commit(self, msg: str, no_verify: bool = False): diff --git a/tests/unit/scm/test_git.py b/tests/unit/scm/test_git.py index 8c34b2342..b5bd78058 100644 --- a/tests/unit/scm/test_git.py +++ b/tests/unit/scm/test_git.py @@ -513,3 +513,30 @@ def test_remind_to_track(scm, caplog): scm.files_to_track = ["fname with spaces.txt", "тест", "foo"] scm.remind_to_track() assert "git add 'fname with spaces.txt' 'тест' foo" in caplog.text + + +def test_add(tmp_dir, scm, git): + if git.test_backend == "pygit2": + pytest.skip() + + tmp_dir.gen({"foo": "foo", "bar": "bar", "dir": {"baz": "baz"}}) + git.add(["foo", "dir"]) + staged, unstaged, untracked = scm.status() + assert set(staged["add"]) == {"foo", "dir/baz"} + assert len(unstaged) == 0 + assert len(untracked) == 1 + + scm.commit("commit") + tmp_dir.gen({"foo": "bar", "dir": {"baz": "bar"}}) + git.add([], update=True) + staged, unstaged, _ = scm.status() + assert set(staged["modify"]) == {"foo", "dir/baz"} + assert len(unstaged) == 0 + assert len(untracked) == 1 + + scm.reset() + git.add(["dir"], update=True) + staged, unstaged, _ = scm.status() + assert set(staged["modify"]) == {"dir/baz"} + assert len(unstaged) == 1 + assert len(untracked) == 1