def pull(self, remote: Optional[str] = None, branch: Optional[str] = None, rebase: bool = False, prune: bool = False, tags: bool = False, jobs: Optional[int] = None, no_edit: bool = False, autostash: bool = False, depth: Optional[int] = None) -> None: # TODO: Check if detached message = f' - Pull' if rebase: message += ' with rebase' CONSOLE.stdout(message) GitOnline.pull(self.path, remote=remote, branch=branch, rebase=rebase, prune=prune, tags=tags, jobs=jobs, no_edit=no_edit, autostash=autostash, depth=depth)
def fetch(self, prune: bool = False, prune_tags: bool = False, tags: bool = False, depth: Optional[int] = None, remote: Optional[str] = None, branch: Optional[str] = None, unshallow: bool = False, jobs: Optional[int] = None, fetch_all: bool = False, check: bool = True, print_output: bool = True): # FIXME: Consolidate this implementation with the one for Remotes CONSOLE.stdout(f' - Fetch repo') try: GitOnline.fetch(self.path, remote=remote, prune=prune, prune_tags=prune_tags, tags=tags, depth=depth, branch=branch, unshallow=unshallow, jobs=jobs, fetch_all=fetch_all, print_output=print_output) except Exception: # noqa message = f'Failed to fetch repo' if check: GIT_LOG.error(message) raise CONSOLE.stdout(message)
def get_remote_tags(cls, path: Path, remote: str, url: Optional[str] = None) -> List[RemoteTag]: if url is None: tags = GitOnline.get_remote_tags_info(path, remote=remote) else: tags = GitOnline.get_remote_tags_info(remote=url) tags = [RemoteTag(path, tag, remote) for tag in tags] return sorted(tags)
def push(self, remote: Optional[str] = None, branch: Optional[str] = None, force: bool = False, set_upstream: bool = False) -> None: # TODO: Check if detached CONSOLE.stdout(' - Push local changes') GitOnline.push(self.path, local_branch=self.name, remote_branch=branch, remote=remote, force=force, set_upstream=set_upstream)
def get_remote_branches_online( cls, path: Path, remote: str, url: Optional[str] = None) -> List[RemoteBranch]: if url is None: branches = GitOnline.get_remote_branches_info(path, remote=remote) else: branches = GitOnline.get_remote_branches_info(remote=url) branches = [RemoteBranch(path, branch, remote) for branch in branches] return sorted(branches)
def delete(self) -> None: if not self.exists: CONSOLE.stdout( f" - Remote tag {Format.Git.ref(self.short_ref)} doesn't exist" ) return CONSOLE.stdout( f' - Delete remote tag {Format.Git.ref(self.short_ref)}') GitOnline.delete_remote_tag(self.path, tag=self.name, remote=self.remote.name, force=True)
def delete(self) -> None: from pygoodle.git.model.factory import GitFactory if not GitFactory.has_remote_branch_online(self.path, self.name, self.remote.name): CONSOLE.stdout( f" - Remote branch {Format.Git.ref(self.name)} doesn't exist") return CONSOLE.stdout(f' - Delete remote branch {Format.Git.ref(self.name)}') GitOnline.delete_remote_branch(self.path, branch=self.name, remote=self.remote.name, force=True)
def clone(self, path: Path, url: str, depth: Optional[int] = None, branch: Optional[str] = None, jobs: Optional[int] = None, origin: Optional[str] = None) -> 'Repo': CONSOLE.stdout(' - Clone repo') GitOnline.clone(path, url=url, depth=depth, branch=branch, jobs=jobs, origin=origin) return Repo(path)
def fetch(self, prune: bool = False, prune_tags: bool = False, tags: bool = False, depth: Optional[int] = None, branch: Optional[str] = None, unshallow: bool = False, jobs: Optional[int] = None, fetch_all: bool = False, check: bool = True, print_output: bool = True) -> None: output = self.name if branch is not None: branch = branch output = f'{output} {branch}' CONSOLE.stdout(f'Fetch from {output}') try: GitOnline.fetch(self.path, remote=self.name, prune=prune, prune_tags=prune_tags, tags=tags, depth=depth, branch=branch, unshallow=unshallow, jobs=jobs, fetch_all=fetch_all, print_output=print_output) except Exception: # noqa message = f'Failed to fetch from {output}' if check: GIT_LOG.error(message) raise CONSOLE.stdout(f' - {message}')
def update(self, init: bool = False, depth: Optional[int] = None, single_branch: bool = False, jobs: Optional[int] = None, recursive: bool = False, checkout: bool = False, rebase: bool = False, merge: bool = False) -> None: GitOnline.submodule_update(self.repo_path, init=init, depth=depth, single_branch=single_branch, jobs=jobs, recursive=recursive, checkout=checkout, merge=merge, rebase=rebase, paths=[self.submodule_path])
def default_branch(self, url: str) -> Optional[RemoteBranch]: if GitOffline.is_repo_cloned(self.path): default_branch = GitOffline.get_default_branch(self.path, self.name) if default_branch is not None: return RemoteBranch(self.path, default_branch, self.name) default_branch = GitOnline.get_default_branch(url) if default_branch is None: return None git_dir = GitOffline.git_dir(self.path) if git_dir is not None and git_dir.is_dir(): GitOffline.save_default_branch(git_dir, self.name, default_branch) return RemoteBranch(self.path, default_branch, self.name)
def create(self, branch: Optional[str] = None, remote: Optional[str] = None) -> None: from pygoodle.git.model.factory import GitFactory if GitFactory.has_remote_branch_online(self.path, self.name, self.remote.name): CONSOLE.stdout( f' - Remote branch {Format.Git.ref(self.name)} already exists') return CONSOLE.stdout(f' - Create remote branch {Format.Git.ref(self.name)}') branch = self.name if branch is None else branch from pygoodle.git.model.branch.local_branch import LocalBranch local_branch = LocalBranch(self.path, branch) GitOnline.fetch(self.path, prune=True) has_existing_branch = local_branch.exists if not has_existing_branch: local_branch.create(branch=branch, remote=remote) local_branch.push(remote=self.remote.name, branch=self.name) if not has_existing_branch: GitOffline.delete_local_branch(self.path, branch) GitOnline.fetch(self.path, prune=True)
def submodule_update(self, init: bool = False, depth: Optional[int] = None, single_branch: bool = False, jobs: Optional[int] = None, recursive: bool = False, remote: bool = False, checkout: bool = False, rebase: bool = False, merge: bool = False, paths: Optional[List[Path]] = None) -> None: CONSOLE.stdout(' - Update submodules') GitOnline.submodule_update(self.path, init=init, depth=depth, single_branch=single_branch, jobs=jobs, recursive=recursive, remote=remote, checkout=checkout, merge=merge, rebase=rebase, paths=paths)
def create(self) -> None: if GitOffline.has_tracking_branch(self.path, self.local_branch.name): CONSOLE.stdout(' - Tracking branch already exists') return # TODO: Add Format util to format tracking branch output: local_branch -> remote remote_branch CONSOLE.stdout( f' - Create tracking branch {Format.Git.ref(self.name)}') GitOnline.fetch(self.path, prune=True) # local and remote branches exist if self.local_branch.exists and self.upstream_branch.exists: self.set_upstream() return # only local branch exists if self.local_branch.exists: # GitOnline.push(self.path, # local_branch=self.name, # remote_branch=self.upstream_branch.name, # remote=self.upstream_branch.remote.name, # set_upstream=True) self.upstream_branch.create(branch=self.local_branch.name) self.set_upstream() GitOnline.fetch(self.path, prune=True) return # only remote branch exists if self.upstream_branch.exists: self.local_branch.create(branch=self.upstream_branch.name, remote=self.upstream_branch.remote.name) # GitOffline.create_local_branch(self.path, self.name, # branch=self.upstream_branch.name, # remote=self.upstream_branch.remote) return # local and remote branches DO NOT exist self.upstream_branch.create() GitOnline.fetch(self.path, prune=True) self.local_branch.create(branch=self.upstream_branch.name, remote=self.upstream_branch.remote.name)
def pull_lfs(self) -> None: CONSOLE.stdout(' - Pull git lfs files') GitOnline.pull_lfs(self.path)
def sha(self) -> str: """Commit sha""" return GitOnline.get_remote_tag_sha(self.path, self.name, self.remote.name)
def is_checked_out(self) -> bool: current_sha = GitOffline.current_head_commit_sha(self.path) tag_sha = GitOnline.get_remote_tag_sha(self.path, self.name) return current_sha == tag_sha