scm: add config option to stage files automatically (#4543)

* scm: add "autostage" config option to stage files automatically

Fixes #4330

* Update tests/unit/scm/test_scm.py

* Update tests/unit/scm/test_scm.py

* scm_context, git: call reset_tracked_files from scm_context, remove output message

* Update dvc/repo/scm_context.py

Co-authored-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Robert Lowry
2020-09-21 11:59:25 +03:00
committed by GitHub
co-authored by Ruslan Kuprieiev
parent 6a3c516b71
commit 15488ebefd
5 changed files with 32 additions and 1 deletions
+1
View File
@@ -139,6 +139,7 @@ SCHEMA = {
Optional("analytics", default=True): Bool,
Optional("hardlink_lock", default=False): Bool,
Optional("no_scm", default=False): Bool,
Optional("autostage", default=False): Bool,
Optional("experiments", default=False): Bool,
Optional("check_update", default=True): Bool,
},
+5 -1
View File
@@ -3,7 +3,11 @@ def scm_context(method):
try:
result = method(repo, *args, **kw)
repo.scm.reset_ignores()
repo.scm.remind_to_track()
autostage = repo.config.get("core", {}).get("autostage", False)
if autostage:
repo.scm.track_changed_files()
else:
repo.scm.remind_to_track()
repo.scm.reset_tracked_files()
return result
except Exception:
+5
View File
@@ -154,6 +154,11 @@ class Base:
Method to remind user to track newly created files handled by scm
"""
def track_changed_files(self):
"""
Method to stage files that have changed
"""
def track_file(self, path):
"""
Method to add file to mechanism that will remind user
+6
View File
@@ -381,6 +381,12 @@ class Git(Base):
"\tgit add {files}".format(files=files)
)
def track_changed_files(self):
if not self.files_to_track:
return
self.add(self.files_to_track)
def track_file(self, path):
self.files_to_track.add(path)
+15
View File
@@ -17,6 +17,7 @@ class TestScmContext(TestCase):
method = mock.Mock()
wrapped = scm_context(method)
self.repo_mock.configure_mock(config={})
wrapped(self.repo_mock)
self.assertEqual(1, method.call_count)
@@ -25,6 +26,20 @@ class TestScmContext(TestCase):
self.assertEqual(0, self.scm_mock.cleanup_ignores.call_count)
def test_should_check_autostage(self):
method = mock.Mock()
wrapped = scm_context(method)
config_autostage_attrs = {"config": {"core": {"autostage": True}}}
self.repo_mock.configure_mock(**config_autostage_attrs)
wrapped(self.repo_mock)
self.assertEqual(1, method.call_count)
self.assertEqual(1, self.scm_mock.reset_ignores.call_count)
self.assertEqual(1, self.scm_mock.track_changed_files.call_count)
self.assertEqual(0, self.scm_mock.cleanup_ignores.call_count)
def test_should_throw_and_cleanup(self):
method = mock.Mock(side_effect=Exception("some problem"))
wrapped = scm_context(method)