From 5a68e821f27c44e54ba0815f79ae29cca696b1bb Mon Sep 17 00:00:00 2001 From: skshetry <18718008+skshetry@users.noreply.github.com> Date: Mon, 21 Jul 2025 11:59:27 +0545 Subject: [PATCH] stop using gitpython in tests (#10808) --- tests/dir_helpers.py | 5 +++-- tests/func/experiments/test_experiments.py | 4 ++-- tests/func/experiments/test_queue.py | 2 +- tests/func/experiments/test_remote.py | 7 +++---- tests/func/experiments/test_save.py | 2 +- tests/func/experiments/test_show.py | 6 +++--- tests/func/test_checkout.py | 3 ++- tests/func/test_import.py | 10 ++++++---- tests/func/test_install.py | 5 +++-- tests/func/test_repo.py | 7 +++++-- tests/func/test_scm.py | 5 ++--- tests/func/test_status.py | 4 ++-- tests/func/test_update.py | 6 +++--- tests/unit/repo/experiments/test_utils.py | 2 +- tests/unit/utils/test_studio.py | 4 +++- 15 files changed, 40 insertions(+), 32 deletions(-) diff --git a/tests/dir_helpers.py b/tests/dir_helpers.py index ac6e92d91..b63d9edd4 100644 --- a/tests/dir_helpers.py +++ b/tests/dir_helpers.py @@ -46,6 +46,7 @@ from global repo template to creating everything inplace, which: import os import pytest +from dulwich.porcelain import remote_add as git_remote_add __all__ = [ "erepo_dir", @@ -96,7 +97,7 @@ class GitRemote: def git_upstream(tmp_dir, erepo_dir, git_dir, request): remote = erepo_dir if "dvc" in request.fixturenames else git_dir url = f"file://{remote.resolve().as_posix()}" - tmp_dir.scm.gitpython.repo.create_remote("upstream", url) + git_remote_add(tmp_dir, "upstream", url) return GitRemote(remote, "upstream", url) @@ -104,5 +105,5 @@ def git_upstream(tmp_dir, erepo_dir, git_dir, request): def git_downstream(tmp_dir, erepo_dir, git_dir, request): remote = erepo_dir if "dvc" in request.fixturenames else git_dir url = f"file://{tmp_dir.resolve().as_posix()}" - remote.scm.gitpython.repo.create_remote("upstream", url) + git_remote_add(remote, "upstream", url) return GitRemote(remote, "upstream", url) diff --git a/tests/func/experiments/test_experiments.py b/tests/func/experiments/test_experiments.py index df7bd5215..69ab6ba1b 100644 --- a/tests/func/experiments/test_experiments.py +++ b/tests/func/experiments/test_experiments.py @@ -227,7 +227,7 @@ def test_detached_parent(tmp_dir, scm, dvc, exp_stage, mocker): scm.commit("v2") scm.checkout(detached_rev) - assert scm.gitpython.repo.head.is_detached + assert scm.pygit2.repo.head_is_detached results = dvc.experiments.run(exp_stage.addressing, params=["foo=3"]) exp_rev = first(results) @@ -792,4 +792,4 @@ def test_custom_commit_message(tmp_dir, scm, dvc, tmp): stage.addressing, tmp_dir=tmp, message="custom commit message" ) ) - assert scm.gitpython.repo.commit(exp).message == "custom commit message" + assert scm.resolve_commit(exp).message == "custom commit message" diff --git a/tests/func/experiments/test_queue.py b/tests/func/experiments/test_queue.py index 889dd92cf..5a32668e2 100644 --- a/tests/func/experiments/test_queue.py +++ b/tests/func/experiments/test_queue.py @@ -61,4 +61,4 @@ def test_custom_commit_message_queue(tmp_dir, scm, dvc): dvc.experiments.run(stage.addressing, queue=True, message="custom commit message") exp = first(dvc.experiments.run(run_all=True)) - assert scm.gitpython.repo.commit(exp).message == "custom commit message" + assert scm.resolve_commit(exp).message == "custom commit message" diff --git a/tests/func/experiments/test_remote.py b/tests/func/experiments/test_remote.py index a1fbb9047..0412f3156 100644 --- a/tests/func/experiments/test_remote.py +++ b/tests/func/experiments/test_remote.py @@ -1,6 +1,7 @@ import logging import pytest +from dulwich.porcelain import pull as git_pull from funcy import first from dvc.repo.experiments.utils import exp_refs_by_rev @@ -180,10 +181,8 @@ def test_list_remote(tmp_dir, scm, dvc, git_downstream, exp_stage, use_url): def test_pull(tmp_dir, scm, dvc, git_downstream, exp_stage, use_url): from dvc.exceptions import InvalidArgumentError - # fetch and checkout to downstream so both repos start from same commit - downstream_repo = git_downstream.tmp_dir.scm.gitpython.repo - fetched = downstream_repo.remote(git_downstream.remote).fetch() - downstream_repo.git.checkout(fetched) + # pull to downstream so both repos start from same commit + git_pull(git_downstream.tmp_dir, "upstream") remote = git_downstream.url if use_url else git_downstream.remote downstream_exp = git_downstream.tmp_dir.dvc.experiments diff --git a/tests/func/experiments/test_save.py b/tests/func/experiments/test_save.py index bc8cc2d59..9fe7715c0 100644 --- a/tests/func/experiments/test_save.py +++ b/tests/func/experiments/test_save.py @@ -165,7 +165,7 @@ def test_exp_save_custom_message(tmp_dir, dvc, scm): setup_stage(tmp_dir, dvc, scm) exp = dvc.experiments.save(message="custom commit message") - assert scm.gitpython.repo.commit(exp).message == "custom commit message" + assert scm.resolve_commit(exp).message == "custom commit message" def test_exp_save_target(tmp_dir, dvc, scm): diff --git a/tests/func/experiments/test_show.py b/tests/func/experiments/test_show.py index 54ef4ba6d..eeb31955f 100644 --- a/tests/func/experiments/test_show.py +++ b/tests/func/experiments/test_show.py @@ -102,7 +102,7 @@ def test_show_simple(tmp_dir, scm, dvc, exp_stage): def test_show_experiment(tmp_dir, scm, dvc, exp_stage, workspace): baseline_rev = scm.get_rev() timestamp = datetime.fromtimestamp( # noqa: DTZ006 - scm.gitpython.repo.rev_parse(baseline_rev).committed_date + scm.resolve_commit(baseline_rev).commit_time ) exp_rev = first( @@ -424,7 +424,7 @@ def test_show_csv(tmp_dir, scm, dvc, exp_stage, capsys): def _get_rev_isotimestamp(rev): return datetime.fromtimestamp( # noqa: DTZ006 - scm.gitpython.repo.rev_parse(rev).committed_date + scm.resolve_commit(rev).commit_time ).isoformat() result1 = dvc.experiments.run(exp_stage.addressing, params=["foo=2"]) @@ -653,7 +653,7 @@ def test_metrics_renaming(tmp_dir, dvc, scm, capsys, copy_script): def _get_rev_isotimestamp(rev): return datetime.fromtimestamp( # noqa: DTZ006 - scm.gitpython.repo.rev_parse(rev).committed_date + scm.resolve_commit(rev).commit_time ).isoformat() assert f",master,baseline,{_get_rev_isotimestamp(scores_rev)},,1,,1" in cap.out diff --git a/tests/func/test_checkout.py b/tests/func/test_checkout.py index 3608d362f..bf59f557b 100644 --- a/tests/func/test_checkout.py +++ b/tests/func/test_checkout.py @@ -5,6 +5,7 @@ import stat import textwrap import pytest +from dulwich.porcelain import remove as git_rm from dvc.cli import main from dvc.dvcfile import PROJECT_FILE, load_file @@ -379,7 +380,7 @@ def test_stats_on_checkout(tmp_dir, dvc, scm): tmp_dir.gen({"lorem": "lorem", "bar": "new bar", "dir2": {"file": "file"}}) (tmp_dir / "foo").unlink() - scm.gitpython.repo.git.rm("foo.dvc") + git_rm(tmp_dir, ["foo.dvc"]) tmp_dir.dvc_add(["bar", "lorem", "dir2"], commit="second") scm.checkout("HEAD~") diff --git a/tests/func/test_import.py b/tests/func/test_import.py index cd4189592..68b97d64c 100644 --- a/tests/func/test_import.py +++ b/tests/func/test_import.py @@ -2,6 +2,8 @@ import filecmp import os import pytest +from dulwich.porcelain import push as git_push +from dulwich.porcelain import remote_add as git_remote_add from funcy import first from dvc.cachemgr import CacheManager @@ -352,8 +354,8 @@ def test_push_wildcard_from_bare_git_repo( ) 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") + git_remote_add(erepo_dir, "origin", os.fspath(tmp_dir)) + git_push(erepo_dir, "origin") dvc_repo = make_tmp_dir("dvc-repo", scm=True, dvc=True) with dvc_repo.chdir(): @@ -435,8 +437,8 @@ def test_import_from_bare_git_repo(tmp_dir, make_tmp_dir, erepo_dir, local_cloud erepo_dir.dvc_gen({"foo": "foo"}, commit="initial") erepo_dir.dvc.push() - erepo_dir.scm.gitpython.repo.create_remote("origin", os.fspath(tmp_dir)) - erepo_dir.scm.gitpython.repo.remote("origin").push("master") + git_remote_add(erepo_dir, "origin", os.fspath(tmp_dir)) + git_push(erepo_dir, "origin", "master") dvc_repo = make_tmp_dir("dvc-repo", scm=True, dvc=True) with dvc_repo.chdir(): diff --git a/tests/func/test_install.py b/tests/func/test_install.py index 00f902c6f..fa830e5c5 100644 --- a/tests/func/test_install.py +++ b/tests/func/test_install.py @@ -3,6 +3,7 @@ import pathlib import sys import pytest +from dulwich.porcelain import remote_add as git_remote_add from git import GitCommandError from dvc.exceptions import DvcException @@ -81,8 +82,8 @@ class TestInstall: storage_path / "files" / "md5" / file_checksum[:2] / file_checksum[2:] ) - scm.gitpython.repo.clone(os.fspath(git_remote)) - scm.gitpython.repo.create_remote("origin", os.fspath(git_remote)) + scm.clone(os.fspath(tmp_dir), os.fspath(git_remote)) + git_remote_add(tmp_dir, "origin", os.fspath(git_remote)) dvc.install() diff --git a/tests/func/test_repo.py b/tests/func/test_repo.py index e37dbc977..8d804b026 100644 --- a/tests/func/test_repo.py +++ b/tests/func/test_repo.py @@ -1,5 +1,8 @@ import os +from dulwich.porcelain import push as git_push +from dulwich.porcelain import remote_add as git_remote_add + from dvc.cachemgr import CacheManager from dvc.dvcfile import LOCK_FILE, PROJECT_FILE from dvc.fs import system @@ -19,8 +22,8 @@ def test_open_bare(tmp_dir, scm, dvc, tmp_path_factory): url = os.fspath(tmp_path_factory.mktemp("bare")) Git.init(url, bare=True).close() - scm.gitpython.repo.create_remote("origin", url) - scm.gitpython.repo.remote("origin").push("master") + git_remote_add(tmp_dir, "origin", url) + git_push(tmp_dir, "origin") with Repo.open(url) as repo: assert repo.scm.root_dir != url diff --git a/tests/func/test_scm.py b/tests/func/test_scm.py index b5cab8aea..24742fe27 100644 --- a/tests/func/test_scm.py +++ b/tests/func/test_scm.py @@ -1,7 +1,6 @@ import os import pytest -from git import Repo from dvc.scm import SCM, Git, NoSCM, SCMError, lfs_prefetch @@ -11,7 +10,7 @@ def test_init_none(tmp_dir): def test_init_git(tmp_dir): - Repo.init(os.fspath(tmp_dir)) + Git.init(os.fspath(tmp_dir)) assert isinstance(SCM(os.fspath(tmp_dir)), Git) @@ -21,7 +20,7 @@ def test_init_no_git(tmp_dir): def test_init_sub_dir(tmp_dir): - Repo.init(os.fspath(tmp_dir)) + Git.init(os.fspath(tmp_dir)) subdir = tmp_dir / "dir" subdir.mkdir() diff --git a/tests/func/test_status.py b/tests/func/test_status.py index 84964d6bf..6bb61f66f 100644 --- a/tests/func/test_status.py +++ b/tests/func/test_status.py @@ -1,6 +1,7 @@ import os import pytest +from dulwich.porcelain import remove as git_rm from dvc.cli import main from dvc.fs import localfs @@ -63,8 +64,7 @@ def test_status_before_and_after_dvc_init(tmp_dir, dvc, git_dir): with git_dir.chdir(): git_dir.init(dvc=True) - git_dir.scm.gitpython.repo.index.remove(["file"]) - os.remove("file") + git_rm(git_dir, ["file"]) git_dir.dvc_gen("file", "second version", commit="with dvc") new_rev = git_dir.scm.get_rev() diff --git a/tests/func/test_update.py b/tests/func/test_update.py index a5b7735e0..4e6d66877 100644 --- a/tests/func/test_update.py +++ b/tests/func/test_update.py @@ -1,6 +1,7 @@ import os import pytest +from dulwich.porcelain import remove as git_rm from dvc.dependency import base from dvc.dvcfile import load_file @@ -89,7 +90,7 @@ def test_update_import_after_remote_updates_to_dvc(tmp_dir, dvc, erepo_dir): new_rev = None with erepo_dir.branch("branch", new=False), erepo_dir.chdir(): - erepo_dir.scm.gitpython.repo.index.remove(["version"]) + git_rm(erepo_dir, ["version"], cached=True) erepo_dir.dvc_gen("version", "updated", commit="upgrade to DVC tracking") new_rev = erepo_dir.scm.get_rev() @@ -124,8 +125,7 @@ def test_update_before_and_after_dvc_init(tmp_dir, dvc, git_dir): with git_dir.chdir(): git_dir.init(dvc=True) - git_dir.scm.gitpython.repo.index.remove(["file"]) - os.remove("file") + git_rm(git_dir, ["file"]) git_dir.dvc_gen("file", "second version", commit="with dvc") new_rev = git_dir.scm.get_rev() diff --git a/tests/unit/repo/experiments/test_utils.py b/tests/unit/repo/experiments/test_utils.py index 06659de38..1c36247c6 100644 --- a/tests/unit/repo/experiments/test_utils.py +++ b/tests/unit/repo/experiments/test_utils.py @@ -16,7 +16,7 @@ def commit_exp_ref(tmp_dir, scm, file="foo", contents="foo", name="foo"): tmp_dir.scm_gen(file, contents, commit="init") rev = scm.get_rev() ref = f"{EXPS_NAMESPACE}/ab/c123/{name}" - scm.gitpython.set_ref(ref, rev) + scm.dulwich.repo.refs[ref.encode("utf-8")] = rev.encode("utf-8") return ref, rev diff --git a/tests/unit/utils/test_studio.py b/tests/unit/utils/test_studio.py index cc9351ce8..bbf533227 100644 --- a/tests/unit/utils/test_studio.py +++ b/tests/unit/utils/test_studio.py @@ -1,6 +1,7 @@ from urllib.parse import urljoin import pytest +from dulwich.porcelain import remote_add as git_remote_add from requests import Response from dvc.env import ( @@ -85,7 +86,8 @@ def test_env_to_config(): ], ) def test_get_repo_url(dvc, scm, monkeypatch, exp_git_remote, repo_url): - scm.gitpython.repo.create_remote("origin", "git@url") + git_remote_add(scm.root_dir, "origin", "git@url") + if exp_git_remote: monkeypatch.setenv(DVC_EXP_GIT_REMOTE, exp_git_remote) assert get_repo_url(dvc) == repo_url