From d294945499e76614ae5ffdaea43c2ebe6eb058fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Trifir=C3=B2?= Date: Wed, 25 Jan 2023 16:32:45 +0100 Subject: [PATCH] gc: fix usage with --date --- dvc/repo/brancher.py | 11 ++++++++++- dvc/scm.py | 6 +++--- tests/func/test_gc.py | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/dvc/repo/brancher.py b/dvc/repo/brancher.py index 9a9a71871..6182309bb 100644 --- a/dvc/repo/brancher.py +++ b/dvc/repo/brancher.py @@ -32,7 +32,16 @@ def brancher( # noqa: E302 - empty string it there is no branches to iterate over - "workspace" if there are uncommitted changes in the SCM repo """ - if not any([revs, all_branches, all_tags, all_commits, all_experiments]): + if not any( + [ + revs, + all_branches, + all_tags, + all_commits, + all_experiments, + commit_date, + ] + ): yield "" return diff --git a/dvc/scm.py b/dvc/scm.py index 8d0ad8b3b..af24280b2 100644 --- a/dvc/scm.py +++ b/dvc/scm.py @@ -242,9 +242,9 @@ def iter_revs( # noqa: C901 def _time_filter(rev): try: - if scm.resolve_commit(rev).commit_date >= commit_datestamp: - return True - return False + return ( + scm.resolve_commit(rev).commit_time >= commit_datestamp + ) except _SCMError: return True diff --git a/tests/func/test_gc.py b/tests/func/test_gc.py index 03e4d458d..e63e144d7 100644 --- a/tests/func/test_gc.py +++ b/tests/func/test_gc.py @@ -1,3 +1,4 @@ +import datetime import logging import os import shutil @@ -327,3 +328,29 @@ def test_gc_rev_num(tmp_dir, scm, dvc): assert not cache.exists() else: assert cache.read_text() == str(i) + + +def test_date(tmp_dir, scm, dvc): + tmp_dir.dvc_gen("testfile", "content", commit="add testfile") + + now = datetime.datetime.now() + datestamp = now.date().replace(day=now.day + 1).isoformat() + + tmp_dir.dvc_gen("testfile", "modified", commit="modified") + + dvc.gc(commit_date=datestamp) + + assert _count_files(dvc.odb.local.path) == 1 + assert dvc.odb.local.exists( + "9ae73c65f418e6f79ceb4f0e4a4b98d5" # "modified" + ) + + tmp_dir.dvc_gen("testfile", "modified, again", commit="modify") + + datestamp = now.date().replace(day=now.day - 1).isoformat() + dvc.gc(commit_date=datestamp) + assert _count_files(dvc.odb.local.path) == 2 + assert dvc.odb.local.exists("9ae73c65f418e6f79ceb4f0e4a4b98d5") + assert dvc.odb.local.exists( + "3bcf3b1be3e794a97a5a6b93a005784c" # "modified, again" + )