mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-02 07:18:25 +01:00
Add argument commit_time to gc/brancher and iter_revs
1. Add argument `commit_time` to `gc`/`brancher` and `iter_revs` 2. Add unit test for iter_revs. Co-authored-by: Peter Rowlands (변기호) <[email protected]>
This commit is contained in:
committed by
David de la Iglesia Castro
co-authored by
Peter Rowlands
parent
20d1f7a87d
commit
217eb13495
@@ -386,6 +386,7 @@ class Repo:
|
||||
all_tags=False,
|
||||
all_commits=False,
|
||||
all_experiments=False,
|
||||
commit_time: Optional[str] = None,
|
||||
remote=None,
|
||||
force=False,
|
||||
jobs=None,
|
||||
@@ -416,6 +417,7 @@ class Repo:
|
||||
all_tags=all_tags,
|
||||
all_commits=all_commits,
|
||||
all_experiments=all_experiments,
|
||||
commit_time=commit_time,
|
||||
):
|
||||
for odb, objs in self.index.used_objs(
|
||||
targets,
|
||||
|
||||
+10
-1
@@ -1,3 +1,5 @@
|
||||
from typing import Optional
|
||||
|
||||
from dvc.scm import iter_revs
|
||||
|
||||
|
||||
@@ -8,6 +10,7 @@ def brancher( # noqa: E302
|
||||
all_tags=False,
|
||||
all_commits=False,
|
||||
all_experiments=False,
|
||||
commit_time: Optional[str] = None,
|
||||
sha_only=False,
|
||||
):
|
||||
"""Generator that iterates over specified revisions.
|
||||
@@ -17,6 +20,9 @@ def brancher( # noqa: E302
|
||||
all_branches (bool): iterate over all available branches.
|
||||
all_commits (bool): iterate over all commits.
|
||||
all_tags (bool): iterate over all available tags.
|
||||
commit_date (str): Keep experiments from the commits after(include)
|
||||
a certain date. Date must match the extended
|
||||
ISO 8601 format (YYYY-MM-DD).
|
||||
sha_only (bool): only return git SHA for a revision.
|
||||
|
||||
Yields:
|
||||
@@ -62,6 +68,7 @@ def brancher( # noqa: E302
|
||||
all_tags=all_tags,
|
||||
all_commits=all_commits,
|
||||
all_experiments=all_experiments,
|
||||
commit_time=commit_time,
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -72,7 +79,9 @@ def brancher( # noqa: E302
|
||||
self.root_dir = self.fs.path.join("/", *repo_root_parts)
|
||||
|
||||
if cwd_parts:
|
||||
cwd = self.fs.path.join("/", *cwd_parts)
|
||||
cwd = self.fs.path.join( # type: ignore[unreachable]
|
||||
"/", *cwd_parts
|
||||
)
|
||||
self.fs.path.chdir(cwd)
|
||||
|
||||
# ignore revs that don't contain repo root
|
||||
|
||||
@@ -67,6 +67,7 @@ def gc(
|
||||
all_tags=all_tags,
|
||||
all_commits=all_commits,
|
||||
all_experiments=all_experiments,
|
||||
commit_time=commit_time,
|
||||
remote=remote,
|
||||
force=force,
|
||||
jobs=jobs,
|
||||
|
||||
+49
-16
@@ -176,20 +176,7 @@ def resolve_rev(scm: "Git", rev: str) -> str:
|
||||
raise RevError(str(exc))
|
||||
|
||||
|
||||
def iter_revs(
|
||||
scm: "Git",
|
||||
revs: Optional[List[str]] = None,
|
||||
num: int = 1,
|
||||
all_branches: bool = False,
|
||||
all_tags: bool = False,
|
||||
all_commits: bool = False,
|
||||
all_experiments: bool = False,
|
||||
) -> Mapping[str, List[str]]:
|
||||
|
||||
if not any([revs, all_branches, all_tags, all_commits, all_experiments]):
|
||||
return {}
|
||||
|
||||
revs = revs or []
|
||||
def _get_n_commits(scm: "Git", revs: List[str], num: int) -> List[str]:
|
||||
results = []
|
||||
for rev in revs:
|
||||
if num == 0:
|
||||
@@ -205,6 +192,37 @@ def iter_revs(
|
||||
except RevError:
|
||||
break
|
||||
n += 1
|
||||
return results
|
||||
|
||||
|
||||
def iter_revs(
|
||||
scm: "Git",
|
||||
revs: Optional[List[str]] = None,
|
||||
num: int = 1,
|
||||
all_branches: bool = False,
|
||||
all_tags: bool = False,
|
||||
all_commits: bool = False,
|
||||
all_experiments: bool = False,
|
||||
commit_time: Optional[str] = None,
|
||||
) -> Mapping[str, List[str]]:
|
||||
from scmrepo.exceptions import SCMError as _SCMError
|
||||
|
||||
from dvc.repo.experiments.utils import exp_commits
|
||||
|
||||
if not any(
|
||||
[
|
||||
revs,
|
||||
all_branches,
|
||||
all_tags,
|
||||
all_commits,
|
||||
all_experiments,
|
||||
commit_time,
|
||||
]
|
||||
):
|
||||
return {}
|
||||
|
||||
revs = revs or []
|
||||
results: List[str] = _get_n_commits(scm, revs, num)
|
||||
|
||||
if all_commits:
|
||||
results.extend(scm.list_all_commits())
|
||||
@@ -215,9 +233,24 @@ def iter_revs(
|
||||
if all_tags:
|
||||
results.extend(scm.list_tags())
|
||||
|
||||
if all_experiments:
|
||||
from dvc.repo.experiments.utils import exp_commits
|
||||
if commit_time:
|
||||
from datetime import datetime
|
||||
|
||||
commit_timestamp = datetime.strptime(
|
||||
commit_time, "%Y-%m-%d"
|
||||
).timestamp()
|
||||
|
||||
def _time_filter(rev):
|
||||
try:
|
||||
if scm.resolve_commit(rev).commit_time >= commit_timestamp:
|
||||
return True
|
||||
return False
|
||||
except _SCMError:
|
||||
return True
|
||||
|
||||
results.extend(filter(_time_filter, scm.list_all_commits()))
|
||||
|
||||
if all_experiments:
|
||||
results.extend(exp_commits(scm))
|
||||
|
||||
rev_resolver = partial(resolve_rev, scm)
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
from datetime import datetime
|
||||
|
||||
from scmrepo.exceptions import SCMError
|
||||
|
||||
from dvc.repo.experiments import ExpRefInfo
|
||||
from dvc.scm import GitMergeError, iter_revs
|
||||
|
||||
@@ -5,6 +9,7 @@ from dvc.scm import GitMergeError, iter_revs
|
||||
def test_iter_revs(
|
||||
tmp_dir,
|
||||
scm,
|
||||
mocker,
|
||||
):
|
||||
"""
|
||||
new other
|
||||
@@ -62,6 +67,25 @@ def test_iter_revs(
|
||||
rev_root: [rev_root],
|
||||
}
|
||||
|
||||
def _func(rev):
|
||||
|
||||
if rev == rev_root:
|
||||
return mocker.Mock(commit_time=datetime(2022, 6, 28).timestamp())
|
||||
if rev == rev_old:
|
||||
raise SCMError
|
||||
return mocker.Mock(commit_time=datetime(2022, 6, 30).timestamp())
|
||||
|
||||
mocker.patch(
|
||||
"scmrepo.git.Git.resolve_commit", mocker.MagicMock(side_effect=_func)
|
||||
)
|
||||
|
||||
gen = iter_revs(scm, commit_time="2022-06-29")
|
||||
assert gen == {
|
||||
rev_new: [rev_new],
|
||||
rev_old: [rev_old],
|
||||
rev_other: [rev_other],
|
||||
}
|
||||
|
||||
|
||||
def test_merge_error(tmp_dir, scm):
|
||||
exc = GitMergeError("Merge failed")
|
||||
|
||||
Reference in New Issue
Block a user