From 20d1f7a87deb5c44876f37b16bf731b36e63ebaf Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Fri, 1 Jul 2022 17:16:09 +0800 Subject: [PATCH] gc: add a new flag `--time` (#678) fix #678 1. Add a new flag `--time` to `dvc gc`. 2. Modify the unit test of `dvc gc` 3. Fix an argument checking error and update the error msg. Co-authored-by: Dave Berenbaum --- dvc/commands/gc.py | 15 +++++++++++++++ dvc/repo/gc.py | 5 ++++- tests/unit/command/test_gc.py | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/dvc/commands/gc.py b/dvc/commands/gc.py index c51557f3c..ee4845d66 100644 --- a/dvc/commands/gc.py +++ b/dvc/commands/gc.py @@ -17,6 +17,8 @@ class CmdGC(CmdBase): all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, + all_experiments=self.args.all_experiments, + commit_time=self.args.commit_time, workspace=self.args.workspace, ) @@ -53,6 +55,7 @@ class CmdGC(CmdBase): all_tags=self.args.all_tags, all_commits=self.args.all_commits, all_experiments=self.args.all_experiments, + commit_time=self.args.commit_time, cloud=self.args.cloud, remote=self.args.remote, force=self.args.force, @@ -104,6 +107,18 @@ def add_parser(subparsers, parent_parser): default=False, help="Keep data files for all Git commits.", ) + gc_parser.add_argument( + "--time", + type=str, + dest="commit_time", + metavar="", + default=None, + help=( + "Keep cached data referenced in the commits after ( inclusive )" + " a certain time. Date must match the extended ISO 8601 format " + "(YYYY-MM-DD)." + ), + ) gc_parser.add_argument( "--all-experiments", action="store_true", diff --git a/dvc/repo/gc.py b/dvc/repo/gc.py index d90ac8233..ca024e977 100644 --- a/dvc/repo/gc.py +++ b/dvc/repo/gc.py @@ -1,4 +1,5 @@ import logging +from typing import Optional from dvc.exceptions import InvalidArgumentError @@ -11,7 +12,7 @@ def _raise_error_if_all_disabled(**kwargs): if not any(kwargs.values()): raise InvalidArgumentError( "Either of `-w|--workspace`, `-a|--all-branches`, `-T|--all-tags` " - "`--all-experiments` or `--all-commits` needs to be set." + "`--all-experiments`, `--all-commits` or `--time` needs to be set." ) @@ -25,6 +26,7 @@ def gc( all_tags=False, all_commits=False, all_experiments=False, + commit_time: Optional[str] = None, force=False, jobs=None, repos=None, @@ -40,6 +42,7 @@ def gc( all_commits=all_commits, all_branches=all_branches, all_experiments=all_experiments, + commit_time=commit_time, ) from contextlib import ExitStack diff --git a/tests/unit/command/test_gc.py b/tests/unit/command/test_gc.py index bddff3e32..dd690fe60 100644 --- a/tests/unit/command/test_gc.py +++ b/tests/unit/command/test_gc.py @@ -14,6 +14,8 @@ def test_(dvc, scm, mocker): "--all-branches", "--all-commits", "--all-experiments", + "--time", + "2022-06-30", "--cloud", "--remote", "origin", @@ -38,6 +40,7 @@ def test_(dvc, scm, mocker): all_branches=True, all_commits=True, all_experiments=True, + commit_time="2022-06-30", cloud=True, remote="origin", force=True,