def start(self, remote: str, branch: str, depth: int, tracking: bool) -> None: """Start new branch in repository and checkout :param str remote: Remote name :param str branch: Local branch name to create :param int depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer :param bool tracking: Whether to create a remote branch with tracking relationship """ if branch not in self.repo.heads: if not is_offline(): self.fetch(remote, ref=GitRef(branch=branch), depth=depth) try: self._create_branch_local(branch) self._checkout_branch_local(branch) except BaseException as err: LOG.debug('Failed to create and checkout branch', err) raise else: CONSOLE.stdout(f' - {fmt.ref(branch)} already exists') if self._is_branch_checked_out(branch): CONSOLE.stdout(' - On correct branch') else: self._checkout_branch_local(branch) if tracking and not is_offline(): self._create_branch_remote_tracking(branch, remote, depth)
def start(self, remote, branch, depth, tracking): """Start new branch in repository :param str remote: Remote name :param str branch: Local branch name to create :param int depth: Git clone depth. 0 indicates full clone, otherwise must be a positive integer :param bool tracking: Whether to create a remote branch with tracking relationship """ if branch not in self.repo.heads: if not is_offline(): self.fetch(remote, ref=branch, depth=depth) try: self._create_branch_local(branch) self._checkout_branch_local(branch) except ClowderGitError: self._exit() else: print(' - ' + fmt.ref_string(branch) + ' already exists') if self._is_branch_checked_out(branch): print(' - On correct branch') else: try: self._checkout_branch_local(branch) except ClowderGitError: self._exit() if tracking and not is_offline(): self._create_branch_remote_tracking(branch, remote, depth)
def start(self, remote, branch, depth, tracking): """Start new branch in repository""" if branch not in self.repo.heads: if not is_offline(): return_code = self.fetch(remote, ref=branch, depth=depth) if return_code != 0: sys.exit(1) return_code = self._create_branch_local(branch) if return_code != 0: self._exit('', return_code=return_code) return_code = self._checkout_branch_local(branch) if return_code != 0: self._exit('', return_code=return_code) else: branch_output = fmt.ref_string(branch) print(' - ' + branch_output + ' already exists') correct_branch = self._is_branch_checked_out(branch) if correct_branch: print(' - On correct branch') else: return_code = self._checkout_branch_local(branch) if return_code != 0: self._exit('', return_code=return_code) if tracking and not is_offline(): self._create_branch_remote_tracking(branch, remote, depth)
def print_status(self, fetch=False): """Print clowder repo status""" repo_path = os.path.join(self.root_directory, '.clowder') if not ProjectRepo.existing_git_repository(repo_path): output = colored('.clowder', 'green') print(output) return if not is_offline() and fetch: print(' - Fetch upstream changes for clowder repo') repo = ProjectRepo(self.clowder_path, self.remote, self.default_ref) repo.fetch(self.remote) project_output = ProjectRepo.format_project_string(repo_path, '.clowder') current_ref_output = ProjectRepo.format_project_ref_string(repo_path) clowder_symlink = os.path.join(self.root_directory, 'clowder.yaml') if not os.path.islink(clowder_symlink): print(project_output + ' ' + current_ref_output) return real_path = os.path.realpath(clowder_symlink) symlink_output = fmt.path('clowder.yaml') clowder_path = fmt.remove_prefix(real_path + '/', self.root_directory) path_output = fmt.path(clowder_path[1:-1]) print(project_output + ' ' + current_ref_output) print(symlink_output + ' -> ' + path_output + '\n')
def herd(self): """clowder herd command""" self._validate_clowder_yaml() if self.clowder_repo is None: exit_clowder_not_found() self.clowder_repo.print_status(fetch=True) if is_offline(): print(fmt.offline_error()) sys.exit(1) if self.clowder is None: sys.exit(1) branch = None if self.args.branch is None else self.args.branch[0] tag = None if self.args.tag is None else self.args.tag[0] depth = None if self.args.depth is None else self.args.depth[0] args = { 'group_names': self.args.groups, 'project_names': self.args.projects, 'skip': self.args.skip, 'branch': branch, 'tag': tag, 'depth': depth, 'rebase': self.args.rebase } if self.args.parallel: self.clowder.herd_parallel(**args) return self.clowder.herd(**args)
def prune(self): """clowder prune command""" self._validate_clowder_yaml() if self.clowder_repo is None: exit_clowder_not_found() self.clowder_repo.print_status() if self.clowder is None: sys.exit(1) if self.args.all: if is_offline(): print(fmt.offline_error()) sys.exit(1) self.clowder.prune(self.args.groups, self.args.branch, project_names=self.args.projects, skip=self.args.skip, force=self.args.force, local=True, remote=True) return if self.args.remote: if is_offline(): print(fmt.offline_error()) sys.exit(1) self.clowder.prune(self.args.groups, self.args.branch, project_names=self.args.projects, skip=self.args.skip, remote=True) return self.clowder.prune(self.args.groups, self.args.branch, project_names=self.args.projects, skip=self.args.skip, force=self.args.force, local=True)
def repo_push(self): """clowder repo push command""" if self.clowder_repo is None: exit_clowder_not_found() if is_offline(): print(fmt.offline_error()) sys.exit(1) self.clowder_repo.print_status(fetch=True) self.clowder_repo.push()
def branch(self, local=False, remote=False): """Print branches for project""" if not os.path.isdir(self.full_path()): print(colored(" - Project is missing\n", 'red')) return repo = ProjectRepo(self.full_path(), self._remote, self._ref) if not is_offline(): if remote: if self.fork is None: repo.fetch(self._remote, depth=self._depth) else: repo.fetch(self.fork.remote_name) repo.fetch(self._remote) repo.print_branches(local=local, remote=remote)
def branch(self, local=False, remote=False): """Print branches for project .. py:function:: branch(local=False, remote=False) :param Optional[bool] local: Print local branches :param Optional[bool] remote: Print remote branches """ repo = ProjectRepo(self.full_path(), self.remote, self.ref) if not is_offline() and remote: if self.fork is None: repo.fetch(self.remote, depth=self.depth) else: repo.fetch(self.fork.remote_name) repo.fetch(self.remote) repo.print_branches(local=local, remote=remote)
def init(self): """clowder init command""" if self.clowder_repo: cprint('Clowder already initialized in this directory\n', 'red') sys.exit(1) if is_offline(): print(fmt.offline_error()) sys.exit(1) url_output = colored(self.args.url, 'green') print('Create clowder repo from ' + url_output + '\n') clowder_repo = ClowderRepo(self.root_directory) if self.args.branch is None: branch = 'master' else: branch = str(self.args.branch[0]) clowder_repo.init(self.args.url, branch)
def print_status(self, fetch: bool = False) -> None: """Print clowder repo status :param bool fetch: Fetch before printing status """ if ENVIRONMENT.clowder_repo_dir is None: return if ENVIRONMENT.clowder_yaml is not None and not ENVIRONMENT.clowder_yaml.is_symlink( ): message = f"Found a {fmt.path(ENVIRONMENT.clowder_yaml.name)} file but it is not a symlink " \ f"to a file stored in the existing {fmt.path(Path('.clowder'))} repo" LOG.error(message) LOG.error() symlink_output: Optional[str] = None if ENVIRONMENT.clowder_yaml is not None and ENVIRONMENT.clowder_yaml.is_symlink( ): target_path = fmt.path(Path(ENVIRONMENT.clowder_yaml.name)) # FIXME: This can cause an error if symlink is pointing to existing file not relative to clowder dir source_path = fmt.path( ENVIRONMENT.clowder_yaml.resolve().relative_to( ENVIRONMENT.clowder_dir)) symlink_output = f"{target_path} -> {source_path}" if ENVIRONMENT.clowder_git_repo_dir is None: CONSOLE.stdout(fmt.green(ENVIRONMENT.clowder_repo_dir.name)) if symlink_output is not None: CONSOLE.stdout(symlink_output) CONSOLE.stdout() return if fetch and not is_offline(): CONSOLE.stdout(' - Fetch upstream changes for clowder repo') self.fetch(self.remote) clowder_git_repo_output = self.format_project_string( ENVIRONMENT.clowder_git_repo_dir.name) CONSOLE.stdout(f"{clowder_git_repo_output} {self.formatted_ref}") if symlink_output is not None: CONSOLE.stdout(symlink_output) CONSOLE.stdout()
def sync(self): """clowder sync command""" self._validate_clowder_yaml() if self.clowder_repo is None: exit_clowder_not_found() self.clowder_repo.print_status(fetch=True) if self.clowder is None: sys.exit(1) if is_offline(): print(fmt.offline_error()) sys.exit(1) all_fork_projects = self.clowder.get_all_fork_project_names() if all_fork_projects == '': cprint(' - No forks to sync\n', 'red') sys.exit() self.clowder.sync(all_fork_projects, rebase=self.args.rebase, parallel=self.args.parallel)
def status(self): """clowder status command""" self._validate_clowder_yaml() if self.clowder_repo is None: exit_clowder_not_found() self.clowder_repo.print_status(fetch=self.args.fetch) if self.clowder is None: sys.exit(1) if self.args.fetch: if is_offline(): print(fmt.offline_error()) sys.exit(1) print(' - Fetch upstream changes for projects\n') self.clowder.fetch(self.clowder.get_all_group_names()) all_project_paths = self.clowder.get_all_project_paths() padding = len(max(all_project_paths, key=len)) self.clowder.status(self.clowder.get_all_group_names(), padding)
def start(self): """clowder start command""" self._validate_clowder_yaml() if self.clowder_repo is None: exit_clowder_not_found() self.clowder_repo.print_status() if self.clowder is None: sys.exit(1) if self.args.tracking: if is_offline(): print(fmt.offline_error()) sys.exit(1) if self.args.projects is None: self.clowder.start_groups(self.args.groups, self.args.skip, self.args.branch, self.args.tracking) else: self.clowder.start_projects(self.args.projects, self.args.skip, self.args.branch, self.args.tracking)
def reset(self): """clowder reset command""" self._validate_clowder_yaml() if self.clowder_repo is None: exit_clowder_not_found() self.clowder_repo.print_status(fetch=True) if is_offline(): print(fmt.offline_error()) sys.exit(1) if self.clowder is None: sys.exit(1) timestamp_project = None if self.args.timestamp: timestamp_project = self.args.timestamp[0] self.clowder.reset(group_names=self.args.groups, project_names=self.args.projects, skip=self.args.skip, timestamp_project=timestamp_project, parallel=self.args.parallel)
def branch(self, local: bool = False, remote: bool = False) -> None: """Print branches for project :param bool local: Print local branches :param bool remote: Print remote branches """ if not is_offline() and remote: self.repo.fetch(self.remote, depth=self.git_settings.depth) if self.upstream: self.repo.fetch(self.upstream.remote) if local: self.repo.print_local_branches() if remote: if self.upstream: CONSOLE.stdout(fmt.upstream(self.name)) self.repo.print_remote_branches() if self.upstream: CONSOLE.stdout(fmt.upstream(self.upstream.name)) self.upstream.repo.print_remote_branches()