mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-27 07:18:28 +01:00
experiments: add dvc exp push/pull/list (#5042)
* git: add support for on_diverged in push_refspec * experiments: update executor for push-refspec changes * experiments: implement push() * tests: add tests for experiments.push * git: add iter_remote_refs * roughly equivalent to `git ls-remote --refs` * experiments: add experiments.list for listing local and remote experiments * refactor git upstream/downstream in tests * experiments: add experiments.pull * experiments: add CLI commands for push/pull/list * exp list: drop short options * experiments: s/list/ls/ * dulwich: catch not repo errors when creating remote client * fix remote rev resolution for list * fix command test * add tests for push/pull with ambiguous experiment names
This commit is contained in:
@@ -321,6 +321,7 @@ class Git(Base):
|
||||
get_ref = partialmethod(_backend_func, "get_ref")
|
||||
remove_ref = partialmethod(_backend_func, "remove_ref")
|
||||
iter_refs = partialmethod(_backend_func, "iter_refs")
|
||||
iter_remote_refs = partialmethod(_backend_func, "iter_remote_refs")
|
||||
get_refs_containing = partialmethod(_backend_func, "get_refs_containing")
|
||||
push_refspec = partialmethod(_backend_func, "push_refspec")
|
||||
fetch_refspecs = partialmethod(_backend_func, "fetch_refspecs")
|
||||
|
||||
@@ -174,22 +174,42 @@ class BaseGitBackend(ABC):
|
||||
If base is specified, only refs which begin with base will be yielded.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def iter_remote_refs(self, url: str, base: Optional[str] = None):
|
||||
"""Iterate over all refs in the specified remote Git repo.
|
||||
|
||||
If base is specified, only refs which begin with base will be yielded.
|
||||
URL can be a named Git remote or URL.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_refs_containing(self, rev: str, pattern: Optional[str] = None):
|
||||
"""Iterate over all git refs containing the specfied revision."""
|
||||
|
||||
@abstractmethod
|
||||
def push_refspec(self, url: str, src: Optional[str], dest: str):
|
||||
def push_refspec(
|
||||
self,
|
||||
url: str,
|
||||
src: Optional[str],
|
||||
dest: str,
|
||||
force: bool = False,
|
||||
on_diverged: Optional[Callable[[str, str], bool]] = None,
|
||||
):
|
||||
"""Push refspec to a remote Git repo.
|
||||
|
||||
Args:
|
||||
url: Remote repo Git URL (Note this must be a Git URL and not
|
||||
a remote name).
|
||||
url: Git remote name or absolute Git URL.
|
||||
src: Local refspec. If src ends with "/" it will be treated as a
|
||||
prefix, and all refs inside src will be pushed using dest
|
||||
as destination refspec prefix. If src is None, dest will be
|
||||
deleted from the remote.
|
||||
dest: Remote refspec.
|
||||
force: If True, remote refs will be overwritten.
|
||||
on_diverged: Callback function which will be called if local ref
|
||||
and remote have diverged and force is False. If the callback
|
||||
returns True the remote ref will be overwritten.
|
||||
Callback will be of the form:
|
||||
on_diverged(local_refname, remote_sha)
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -198,7 +218,7 @@ class BaseGitBackend(ABC):
|
||||
url: str,
|
||||
refspecs: Iterable[str],
|
||||
force: Optional[bool] = False,
|
||||
on_diverged: Optional[Callable[[bytes, bytes], bool]] = None,
|
||||
on_diverged: Optional[Callable[[str, str], bool]] = None,
|
||||
):
|
||||
"""Fetch refspecs from a remote Git repo.
|
||||
|
||||
@@ -211,6 +231,8 @@ class BaseGitBackend(ABC):
|
||||
on_diverged: Callback function which will be called if local ref
|
||||
and remote have diverged and force is False. If the callback
|
||||
returns True the local ref will be overwritten.
|
||||
Callback will be of the form:
|
||||
on_diverged(local_refname, remote_sha)
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@@ -185,11 +185,92 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
else:
|
||||
yield os.fsdecode(key)
|
||||
|
||||
def iter_remote_refs(self, url: str, base: Optional[str] = None):
|
||||
from dulwich.client import get_transport_and_path
|
||||
from dulwich.porcelain import get_remote_repo
|
||||
|
||||
try:
|
||||
_remote, location = get_remote_repo(self.repo, url)
|
||||
client, path = get_transport_and_path(location)
|
||||
except Exception as exc:
|
||||
raise SCMError(
|
||||
f"'{url}' is not a valid Git remote or URL"
|
||||
) from exc
|
||||
|
||||
if base:
|
||||
yield from (
|
||||
os.fsdecode(ref)
|
||||
for ref in client.get_refs(path)
|
||||
if ref.startswith(os.fsencode(base))
|
||||
)
|
||||
else:
|
||||
yield from (os.fsdecode(ref) for ref in client.get_refs(path))
|
||||
|
||||
def get_refs_containing(self, rev: str, pattern: Optional[str] = None):
|
||||
raise NotImplementedError
|
||||
|
||||
def push_refspec(self, url: str, src: Optional[str], dest: str):
|
||||
def push_refspec(
|
||||
self,
|
||||
url: str,
|
||||
src: Optional[str],
|
||||
dest: str,
|
||||
force: bool = False,
|
||||
on_diverged: Optional[Callable[[str, str], bool]] = None,
|
||||
):
|
||||
from dulwich.client import get_transport_and_path
|
||||
from dulwich.errors import NotGitRepository, SendPackError
|
||||
from dulwich.porcelain import (
|
||||
DivergedBranches,
|
||||
check_diverged,
|
||||
get_remote_repo,
|
||||
)
|
||||
|
||||
dest_refs, values = self._push_dest_refs(src, dest)
|
||||
|
||||
try:
|
||||
_remote, location = get_remote_repo(self.repo, url)
|
||||
client, path = get_transport_and_path(location)
|
||||
except Exception as exc:
|
||||
raise SCMError(
|
||||
f"'{url}' is not a valid Git remote or URL"
|
||||
) from exc
|
||||
|
||||
def update_refs(refs):
|
||||
new_refs = {}
|
||||
for ref, value in zip(dest_refs, values):
|
||||
if ref in refs:
|
||||
local_sha = self.repo.refs[ref]
|
||||
remote_sha = refs[ref]
|
||||
try:
|
||||
check_diverged(self.repo, remote_sha, local_sha)
|
||||
except DivergedBranches:
|
||||
if not force:
|
||||
overwrite = False
|
||||
if on_diverged:
|
||||
overwrite = on_diverged(
|
||||
os.fsdecode(ref), os.fsdecode(remote_sha),
|
||||
)
|
||||
if not overwrite:
|
||||
continue
|
||||
new_refs[ref] = value
|
||||
return new_refs
|
||||
|
||||
def progress(msg):
|
||||
logger.trace("git send_pack: %s", msg)
|
||||
|
||||
try:
|
||||
client.send_pack(
|
||||
path,
|
||||
update_refs,
|
||||
self.repo.object_store.generate_pack_data,
|
||||
progress=progress,
|
||||
)
|
||||
except (NotGitRepository, SendPackError) as exc:
|
||||
raise SCMError("Git failed to push '{src}' to '{url}'") from exc
|
||||
|
||||
def _push_dest_refs(
|
||||
self, src: str, dest: str
|
||||
) -> Tuple[Iterable[bytes], Iterable[bytes]]:
|
||||
from dulwich.objects import ZERO_SHA
|
||||
|
||||
if src is not None and src.endswith("/"):
|
||||
@@ -203,37 +284,22 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
else:
|
||||
values = [self.repo.refs[os.fsencode(src)]]
|
||||
dest_refs = [os.fsencode(dest)]
|
||||
|
||||
def update_refs(refs):
|
||||
for ref, value in zip(dest_refs, values):
|
||||
refs[ref] = value
|
||||
return refs
|
||||
|
||||
try:
|
||||
client, path = get_transport_and_path(url)
|
||||
except Exception as exc:
|
||||
raise SCMError("Could not get remote client") from exc
|
||||
|
||||
def progress(msg):
|
||||
logger.trace("git send_pack: %s", msg)
|
||||
|
||||
client.send_pack(
|
||||
path,
|
||||
update_refs,
|
||||
self.repo.object_store.generate_pack_data,
|
||||
progress=progress,
|
||||
)
|
||||
return dest_refs, values
|
||||
|
||||
def fetch_refspecs(
|
||||
self,
|
||||
url: str,
|
||||
refspecs: Iterable[str],
|
||||
force: Optional[bool] = False,
|
||||
on_diverged: Optional[Callable[[bytes, bytes], bool]] = None,
|
||||
on_diverged: Optional[Callable[[str, str], bool]] = None,
|
||||
):
|
||||
from dulwich.client import get_transport_and_path
|
||||
from dulwich.objectspec import parse_reftuples
|
||||
from dulwich.porcelain import DivergedBranches, check_diverged
|
||||
from dulwich.porcelain import (
|
||||
DivergedBranches,
|
||||
check_diverged,
|
||||
get_remote_repo,
|
||||
)
|
||||
|
||||
fetch_refs = []
|
||||
|
||||
@@ -253,10 +319,12 @@ class DulwichBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
]
|
||||
|
||||
try:
|
||||
client, path = get_transport_and_path(url)
|
||||
_remote, location = get_remote_repo(self.repo, url)
|
||||
client, path = get_transport_and_path(location)
|
||||
except Exception as exc:
|
||||
|
||||
raise SCMError("Could not get remote client") from exc
|
||||
raise SCMError(
|
||||
f"'{url}' is not a valid Git remote or URL"
|
||||
) from exc
|
||||
|
||||
def progress(msg):
|
||||
logger.trace("git fetch: %s", msg)
|
||||
|
||||
@@ -362,6 +362,9 @@ class GitPythonBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
base = base[:-1]
|
||||
yield "/".join([base, ref.name])
|
||||
|
||||
def iter_remote_refs(self, url: str, base: Optional[str] = None):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_refs_containing(self, rev: str, pattern: Optional[str] = None):
|
||||
from git.exc import GitCommandError
|
||||
|
||||
@@ -379,7 +382,14 @@ class GitPythonBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
except GitCommandError:
|
||||
pass
|
||||
|
||||
def push_refspec(self, url: str, src: Optional[str], dest: str):
|
||||
def push_refspec(
|
||||
self,
|
||||
url: str,
|
||||
src: Optional[str],
|
||||
dest: str,
|
||||
force: bool = False,
|
||||
on_diverged: Optional[Callable[[str, str], bool]] = None,
|
||||
):
|
||||
raise NotImplementedError
|
||||
|
||||
def fetch_refspecs(
|
||||
@@ -387,7 +397,7 @@ class GitPythonBackend(BaseGitBackend): # pylint:disable=abstract-method
|
||||
url: str,
|
||||
refspecs: Iterable[str],
|
||||
force: Optional[bool] = False,
|
||||
on_diverged: Optional[Callable[[bytes, bytes], bool]] = None,
|
||||
on_diverged: Optional[Callable[[str, str], bool]] = None,
|
||||
):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
Reference in New Issue
Block a user