From fa4bf40eb472c840db12f7e4ebcdb2716a0778f3 Mon Sep 17 00:00:00 2001 From: Ioana Grigoropol <14314993+ju0gri@users.noreply.github.com> Date: Fri, 25 Dec 2020 21:50:36 +0000 Subject: [PATCH] api: add globbing option for pushing (#5033) * api: add globbing option for pushing Related: #4816 Signed-off-by: Ioana Grigoropol * api: use utility function for push command Signed-off-by: Ioana Grigoropol --- dvc/command/data_sync.py | 7 ++++++ dvc/repo/push.py | 6 +++++- tests/func/test_import.py | 32 +++++++++++++++++++++++++++- tests/unit/command/test_data_sync.py | 2 ++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/dvc/command/data_sync.py b/dvc/command/data_sync.py index cf4ef74e15..8ce2e65cdd 100644 --- a/dvc/command/data_sync.py +++ b/dvc/command/data_sync.py @@ -57,6 +57,7 @@ def run(self): with_deps=self.args.with_deps, recursive=self.args.recursive, run_cache=self.args.run_cache, + glob=self.args.glob, ) self.log_summary({"pushed": processed_files_count}) except DvcException: @@ -240,6 +241,12 @@ def add_parser(subparsers, _parent_parser): default=False, help="Push run history for all stages.", ) + push_parser.add_argument( + "--glob", + action="store_true", + default=False, + help="Allows targets containing shell-style wildcards.", + ) push_parser.set_defaults(func=CmdDataPush) # Fetch diff --git a/dvc/repo/push.py b/dvc/repo/push.py index 7800557870..23bb5244de 100644 --- a/dvc/repo/push.py +++ b/dvc/repo/push.py @@ -1,3 +1,4 @@ +from ..utils import glob_targets from . import locked @@ -14,14 +15,17 @@ def push( all_commits=False, run_cache=False, revs=None, + glob=False, ): used_run_cache = self.stage_cache.push(remote) if run_cache else [] if isinstance(targets, str): targets = [targets] + expanded_targets = glob_targets(targets, glob=glob) + used = self.used_cache( - targets, + expanded_targets, all_branches=all_branches, all_tags=all_tags, all_commits=all_commits, diff --git a/tests/func/test_import.py b/tests/func/test_import.py index 9d945053f2..9fe6d4fcea 100644 --- a/tests/func/test_import.py +++ b/tests/func/test_import.py @@ -9,7 +9,7 @@ from dvc.cache import Cache from dvc.config import NoRemoteError from dvc.dvcfile import Dvcfile -from dvc.exceptions import DownloadError, PathMissingError +from dvc.exceptions import CollectCacheError, DownloadError, PathMissingError from dvc.external_repo import IsADVCRepoError from dvc.stage.exceptions import StagePathNotFoundError from dvc.system import System @@ -264,6 +264,36 @@ def test_pull_wildcard_imported_directory_stage(tmp_dir, dvc, erepo_dir): assert (tmp_dir / "dir_imported123").read_text() == {"foo": "foo content"} +def test_push_wildcard_from_bare_git_repo( + tmp_dir, make_tmp_dir, erepo_dir, local_cloud +): + import git + + git.Repo.init(os.fspath(tmp_dir), bare=True) + + erepo_dir.add_remote(config=local_cloud.config) + with erepo_dir.chdir(): + erepo_dir.dvc_gen( + { + "dir123": {"foo": "foo content"}, + "dirextra": {"extrafoo": "extra foo content"}, + }, + commit="initial", + ) + erepo_dir.dvc.push( + [os.path.join(os.fspath(erepo_dir), "dire*")], glob=True + ) + + erepo_dir.scm.gitpython.repo.create_remote("origin", os.fspath(tmp_dir)) + erepo_dir.scm.gitpython.repo.remote("origin").push("master") + + dvc_repo = make_tmp_dir("dvc-repo", scm=True, dvc=True) + with dvc_repo.chdir(): + dvc_repo.dvc.imp(os.fspath(tmp_dir), "dirextra") + with pytest.raises(CollectCacheError): + dvc_repo.dvc.imp(os.fspath(tmp_dir), "dir123") + + def test_download_error_pulling_imported_stage(tmp_dir, dvc, erepo_dir): with erepo_dir.chdir(): erepo_dir.dvc_gen("foo", "foo content", commit="create foo") diff --git a/tests/unit/command/test_data_sync.py b/tests/unit/command/test_data_sync.py index bb4433a11e..cd5c8253b3 100644 --- a/tests/unit/command/test_data_sync.py +++ b/tests/unit/command/test_data_sync.py @@ -96,6 +96,7 @@ def test_push(mocker): "--with-deps", "--recursive", "--run-cache", + "--glob", ] ) assert cli_args.func == CmdDataPush @@ -115,4 +116,5 @@ def test_push(mocker): with_deps=True, recursive=True, run_cache=True, + glob=True, )