def destroy_and_reclone(self): """Deletes the current directory and reclone the repository.""" # Get local path for the repo local_path = self.repo.working_dir self.logger.info("Preparing to delete and reclone repo %s", local_path) # Get all of the remotes info remotes = {} for r in self.repo.remotes: remotes[r.name] = r.url self.logger.debug("Remotes for %s: %s", local_path, ' '.join(list(remotes))) if len(remotes) == 0: msg = "No remotes found for repo {repo}, cannot reclone. Aborting deletion.".format( repo=local_path) raise exceptions.RepoCreationException(msg) # Select a remote for the clone, 'origin' by default if "origin" in remotes.keys(): default_remote = "origin" else: default_remote = list(remotes)[0] self.logger.debug("Default remote for cloning set to '%s'", default_remote) # Delete the local repo self.logger.info("Deleting local repo at %s", local_path) shutil.rmtree(local_path, ignore_errors=True) # Clone it again repo = self.clone(remotes[default_remote], local_path) self.__setup(repo=repo.repo, path=local_path) # Recreate the remotes for name, url in remotes.items(): if name != default_remote: try: self.logger.debug("Adding remote %s", name) r = self.repo.create_remote(name, url) except git.GitCommandError as ex: msg = "Issue with recreating remote {remote}. Error: {error}".format( remote=name, error=ex) raise_from(exceptions.RemoteException(msg), ex)
def destroy_and_reclone(self): """Deletes the current directory and reclone the repository.""" # Get local path for the repo local_path = self.repo.working_dir self.logger.info(f"Preparing to delete and reclone repo {local_path}") # Get all of the remotes info remotes = {} for r in self.repo.remotes: remotes[r.name] = r.url remote_list = ' '.join(list(remotes)) self.logger.debug(f"Remotes for {local_path}: {remote_list}") if len(remotes) == 0: msg = (f"No remotes found for repo {local_path}, cannot reclone. " "Aborting deletion.") raise exceptions.RepoCreationException(msg) # Select a remote for the clone, 'origin' by default if "origin" in remotes.keys(): default_remote = "origin" else: default_remote = list(remotes)[0] msg = f"Default remote for cloning set to '{default_remote}'" self.logger.debug(msg) # Delete the local repo self.logger.info(f"Deleting local repo at {local_path}") shutil.rmtree(local_path, ignore_errors=True) # Clone it again repo = self.clone(remotes[default_remote], local_path) self.__setup(repo=repo.repo, path=local_path) # Recreate the remotes for name, url in remotes.items(): if name != default_remote: try: self.logger.debug(f"Adding remote {name}") r = self.repo.create_remote(name, url) except git.GitCommandError as ex: msg = f"Issue with recreating remote {name}. Error: {ex}" raise exceptions.RemoteException(msg) from ex
def fetch(self, remote="origin"): """Refresh the specified remote :param str remote: Remote to fetch """ try: remote = self.git_repo.repo.remote(remote) except ValueError: repo = self.git_repo.repo.working_dir msg = f"Remote {remote} does not exist on repo {repo}" raise exceptions.ReferenceNotFoundException(msg) try: remote.fetch() except git.GitCommandError as ex: msg = (f"Could not fetch remote {remote.name} ({remote.url}). " f"Error: {ex}") raise exceptions.RemoteException(msg) from ex
def fetch_all(self): """Refresh all the repo's remotes. All the remotes will be fetched even if one fails ; in this case a single exception containing the list of failed remotes is returned. """ remotes = self.names() errors = [] for remote in remotes: try: self.fetch(remote) except exceptions.RemoteException: self.logger.exception("Error fetching remote %s", remote) errors.append(remote) if errors: msg = "Error fetching these remotes: {0}".format(", ".join(errors)) raise exceptions.RemoteException(msg)
def fetch(self, remote="origin"): """Refresh the specified remote :param str remote: Remote to fetch """ try: remote = self.git_repo.repo.remote(remote) except ValueError: msg = "Remote {remote} does not exist on repo {repo}".format( remote=remote, repo=self.git_repo.repo.working_dir) raise exceptions.ReferenceNotFoundException(msg) try: remote.fetch() except git.GitCommandError as ex: msg = ("Could not fetch remote {remote} ({url}). " "Error: {error}".format(remote=remote.name, url=remote.url, error=ex)) raise_from(exceptions.RemoteException(msg), ex)
def exists(self, name, remote=None): """Checks if a branch exists locally or on the specified remote. :param str name: Name of the branch to find :param str remote: Remote name to check for the branch, or None if local """ if not remote: if name in self.git_repo.repo.branches: return True else: return False else: if remote not in self.git_repo.remote.names(): raise exceptions.RemoteException( f"Remote {remote} does not exist.") if name in self.git_repo.repo.remotes[remote].refs: return True else: return False