Skip to content

Commit

Permalink
fix(registry): push to remote automatically only on cloned repos
Browse files Browse the repository at this point in the history
  • Loading branch information
shcheklein committed Sep 19, 2023
1 parent 53bb03f commit 92c7b89
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
11 changes: 5 additions & 6 deletions gto/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
parse_shortcut,
)
from gto.exceptions import NoRepo, NotImplementedInGTO, RefNotFound, WrongArgs
from gto.git_utils import has_remote
from gto.index import Artifact, RepoIndexManager
from gto.registry import GitRegistry
from gto.tag import parse_name as parse_tag_name
Expand Down Expand Up @@ -97,7 +96,7 @@ def register(
bump_major=bump_major,
bump_minor=bump_minor,
bump_patch=bump_patch,
push=push or has_remote(reg.scm),
push=push,
stdout=stdout,
author=author,
author_email=author_email,
Expand Down Expand Up @@ -131,7 +130,7 @@ def assign(
message=message,
simple=simple,
force=force,
push=push or has_remote(reg.scm),
push=push,
skip_registration=skip_registration,
stdout=stdout,
author=author,
Expand Down Expand Up @@ -165,7 +164,7 @@ def unassign(
simple=simple if simple is not None else False,
force=force,
delete=delete,
push=push or has_remote(reg.scm),
push=push,
author=author,
author_email=author_email,
)
Expand Down Expand Up @@ -195,7 +194,7 @@ def deregister(
simple=simple if simple is not None else True,
force=force,
delete=delete,
push=push or has_remote(reg.scm),
push=push,
author=author,
author_email=author_email,
)
Expand Down Expand Up @@ -223,7 +222,7 @@ def deprecate(
simple=simple,
force=force,
delete=delete,
push=push or has_remote(reg.scm),
push=push,
author=author,
author_email=author_email,
)
Expand Down
25 changes: 13 additions & 12 deletions gto/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tempfile import TemporaryDirectory
from typing import Optional, Union

from scmrepo.exceptions import InvalidRemote, SCMError
from scmrepo.exceptions import SCMError
from scmrepo.git import Git, SyncStatus

from gto.config import RegistryConfig
Expand All @@ -17,7 +17,17 @@
class RemoteRepoMixin:
@classmethod
@contextmanager
def from_scm(cls, scm: Git, config: Optional[RegistryConfig] = None):
def from_scm(
cls,
scm: Git,
cloned: bool = False,
config: Optional[RegistryConfig] = None,
):
"""
`cloned` - scm is a remote repo that was cloned locally into a tmp
directory to be used for the duration of the context manager.
Means that we push tags and changes back to the remote repo.
"""
raise NotImplementedError()

@classmethod
Expand Down Expand Up @@ -51,7 +61,7 @@ def from_url(
with cloned_git_repo(url_or_scm) as scm:
if branch:
scm.checkout(branch)
with cls.from_scm(scm=scm, config=config) as obj:
with cls.from_scm(scm=scm, config=config, cloned=True) as obj:
yield obj

def _call_commit_push(
Expand Down Expand Up @@ -152,12 +162,3 @@ def git_add_and_commit_all_changes(scm: Git, message: str) -> None:
def _reset_repo_to_head(scm: Git) -> None:
if scm.stash.push(include_untracked=True):
scm.stash.drop()


def has_remote(scm: Git, remote: str = "origin") -> bool:
try:
scm.validate_git_remote(remote)
return True
except InvalidRemote:
pass
return False
23 changes: 17 additions & 6 deletions gto/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,22 @@ def artifact_centric_representation(self):

class RepoIndexManager(FileIndexManager, RemoteRepoMixin):
scm: Git
cloned: bool

def __init__(self, scm: Git, config):
super().__init__(scm=scm, config=config) # type: ignore[call-arg]
def __init__(self, scm: Git, cloned: bool, config):
super().__init__(scm=scm, cloned=cloned, config=config) # type: ignore[call-arg]

@classmethod
@contextmanager
def from_scm(cls, scm: Git, config: Optional[RegistryConfig] = None):
def from_scm(
cls,
scm: Git,
cloned: bool = False,
config: Optional[RegistryConfig] = None,
):
if config is None:
config = read_registry_config(os.path.join(scm.root_dir, CONFIG_FILE_NAME))
yield cls(scm=scm, config=config)
yield cls(scm=scm, cloned=cloned, config=config)

def add(
self,
Expand All @@ -351,7 +357,7 @@ def add(
commit=commit,
commit_message=commit_message
or generate_annotate_commit_message(name=name, type=type, path=path),
push=push,
push=push or self.cloned,
stdout=stdout,
name=name,
type=type,
Expand Down Expand Up @@ -458,7 +464,12 @@ class EnrichmentManager(BaseManager, RemoteRepoMixin):

@classmethod
@contextmanager
def from_scm(cls, scm: Git, config: Optional[RegistryConfig] = None):
def from_scm(
cls,
scm: Git,
cloned: Optional[bool] = False,
config: Optional[RegistryConfig] = None,
):
if config is None:
config = read_registry_config(os.path.join(scm.root_dir, CONFIG_FILE_NAME))
yield cls(scm=scm, config=config)
Expand Down
11 changes: 9 additions & 2 deletions gto/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

class GitRegistry(BaseModel, RemoteRepoMixin):
scm: Git
cloned: bool
artifact_manager: TagArtifactManager
version_manager: TagVersionManager
stage_manager: TagStageManager
Expand All @@ -54,12 +55,18 @@ class Config:

@classmethod
@contextmanager
def from_scm(cls, scm: Git, config: Optional[RegistryConfig] = None):
def from_scm(
cls,
scm: Git,
cloned: bool = False,
config: Optional[RegistryConfig] = None,
):
if config is None:
config = read_registry_config(os.path.join(scm.root_dir, CONFIG_FILE_NAME))

yield cls(
scm=scm,
cloned=cloned,
config=config,
artifact_manager=TagArtifactManager(scm=scm, config=config),
version_manager=TagVersionManager(scm=scm, config=config),
Expand Down Expand Up @@ -572,7 +579,7 @@ def get_stages(self, allowed: bool = False, used: bool = False):
def _push_tag_or_echo_reminder(
self, tag_name: str, push: bool, stdout: bool, delete: bool = False
) -> None:
if push:
if push or self.cloned:
if stdout:
echo(
f"Running `git push{' --delete ' if delete else ' '}origin {tag_name}`"
Expand Down

0 comments on commit 92c7b89

Please sign in to comment.