From 6e02dddabdd40dd42e6c7c9069ebd4e8110a87b7 Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Thu, 17 Nov 2022 17:20:12 +0900 Subject: [PATCH] output: check push in get_used_objs --- dvc/output.py | 6 ++++++ dvc/repo/__init__.py | 2 ++ dvc/repo/index.py | 2 ++ dvc/repo/push.py | 1 + tests/func/test_used_objs.py | 13 +++++++++++++ 5 files changed, 24 insertions(+) diff --git a/dvc/output.py b/dvc/output.py index fb0b37bad..031edc31b 100644 --- a/dvc/output.py +++ b/dvc/output.py @@ -1043,9 +1043,15 @@ class Output: if not self.use_cache: return {} + push: bool = kwargs.pop("push", False) if self.stage.is_repo_import: + if push: + return {} return self.get_used_external(**kwargs) + if push and not self.can_push: + return {} + if not self.hash_info: msg = ( "Output '{}'({}) is missing version info. " diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index 8e5a20d57..d590045fb 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -401,6 +401,7 @@ class Repo: used_run_cache=None, revs=None, num=1, + push: bool = False, ): """Get the stages related to the given target and collect the `info` of its outputs. @@ -435,6 +436,7 @@ class Repo: jobs=jobs, recursive=recursive, with_deps=with_deps, + push=push, ).items(): used[odb].update(objs) diff --git a/dvc/repo/index.py b/dvc/repo/index.py index a4273465f..4c6be0cf5 100644 --- a/dvc/repo/index.py +++ b/dvc/repo/index.py @@ -268,6 +268,7 @@ class Index: force: bool = False, recursive: bool = False, jobs: int = None, + push: bool = False, ) -> "ObjectContainer": from collections import defaultdict @@ -281,6 +282,7 @@ class Index: force=force, jobs=jobs, filter_info=filter_info, + push=push, ).items(): used[odb].update(objs) return used diff --git a/dvc/repo/push.py b/dvc/repo/push.py index 84b486fda..bb4369107 100644 --- a/dvc/repo/push.py +++ b/dvc/repo/push.py @@ -93,6 +93,7 @@ def push( recursive=recursive, used_run_cache=used_run_cache, revs=revs, + push=True, ) pushed = len(used_run_cache) diff --git a/tests/func/test_used_objs.py b/tests/func/test_used_objs.py index 4d6c8a61d..29d16a44e 100644 --- a/tests/func/test_used_objs.py +++ b/tests/func/test_used_objs.py @@ -51,3 +51,16 @@ def test_from_gitfs_when_pwd_not_in_root( with wdir.chdir(): assert dvc.used_objs([target], revs=[scm.get_rev()]) + + +def test_used_objs_push(tmp_dir, scm, dvc): + stage = tmp_dir.dvc_gen("foo", "foo")[0] + hash_info = stage.outs[0].hash_info + + stage.outs[0].can_push = True + assert stage.get_used_objs(push=False) == {None: {hash_info}} + assert stage.get_used_objs(push=True) == {None: {hash_info}} + + stage.outs[0].can_push = False + assert stage.get_used_objs(push=False) == {None: {hash_info}} + assert stage.get_used_objs(push=True) == {}