Ejemplo n.º 1
0
    def _parse_git_repo_from_working_dir(self) -> bool:
        """
        Get the repo from the self.working_dir (clone self.git_url if it is not a git repo)
        """
        if self.working_dir and not self.git_repo:
            logger.debug("working_dir is set and git_repo is not: let's discover...")
            if is_git_repo(directory=self.working_dir):
                logger.debug("it's a git repo!")
                self.git_repo = git.Repo(path=self.working_dir)
                return True

            elif self.git_url and not self.offline:
                logger.debug(
                    "we just cloned git repo %s to %s", self.git_url, self.working_dir
                )
                self.git_repo = get_repo(url=self.git_url, directory=self.working_dir)
                return True

        return False
Ejemplo n.º 2
0
    def _parse_git_repo_from_working_dir(self) -> bool:
        """
        Get the repo from the self.working_dir (clone self.git_url if it is not a git repo)
        """
        if self.working_dir and not self.git_repo:
            logger.debug(
                "`working_dir` is set and `git_repo` is not: let's discover..."
            )
            if is_git_repo(directory=self.working_dir):
                logger.debug("It's a git repo!")
                self.git_repo = git.Repo(path=self.working_dir)
                return True

            elif self.git_url and not self.offline:
                self.git_repo = get_repo(url=self.git_url, directory=self.working_dir)
                logger.debug(
                    f"We just cloned git repo {self.git_url} to {self.working_dir}."
                )
                return True

        return False
Ejemplo n.º 3
0
    def __init__(
        self,
        git_repo: git.Repo = None,
        working_dir: str = None,
        ref: str = None,
        git_project: GitProject = None,
        git_service: GitService = None,
        git_url: str = None,
        full_name: str = None,
        namespace: str = None,
        repo_name: str = None,
        path_or_url: str = None,
    ) -> None:

        if path_or_url:
            if os.path.isdir(path_or_url):
                working_dir = working_dir or path_or_url
            else:
                try:
                    res = requests.head(path_or_url)
                    if res.ok:
                        git_url = git_url or path_or_url
                    else:
                        logger.warning("path_or_url is nor directory nor url")
                except requests.exceptions.BaseHTTPError as ex:
                    logger.warning("path_or_url is nor directory nor url")

        self.git_repo = git_repo
        self.working_dir = working_dir
        self._ref = ref
        self.git_project = git_project
        self.git_service = git_service
        self.git_url = git_url
        self.full_name = full_name
        self.repo_name = repo_name
        self.namespace = namespace
        self.working_dir_temporary = False

        change = True
        while change:
            change = False

            if self.repo_name and self.namespace and not self.full_name:
                self.full_name = f"{self.namespace}/{self.repo_name}"
                change = True

            if self.full_name and not self.namespace:
                self.namespace = self.full_name.split("/")[0]
                change = True

            if self.full_name and not self.repo_name:
                self.repo_name = self.full_name.split("/")[1]
                change = True

            if (self.repo_name and self.namespace and self.git_service
                    and not self.git_project):
                self.git_project = self.git_service.get_project(
                    repo=self.repo_name, namespace=self.namespace)
                change = True

            if self.git_project and not self.git_service:
                self.git_service = self.git_project.service
                change = True

            if self.git_repo and not self._ref:
                if self.git_repo.head.is_detached:
                    self._ref = self.git_repo.head.commit.hexsha
                else:
                    self._ref = self.git_repo.active_branch
                change = True

            if self.git_repo and not self.working_dir:
                self.working_dir = self.git_repo.working_dir
                change = True

            if self.working_dir and not self.git_repo:
                logger.debug(
                    "working_dir is set and git_repo is not: let's discover..."
                )
                if is_git_repo(directory=self.working_dir):
                    self.git_repo = git.Repo(path=self.working_dir)
                    logger.debug("it's a git repo!")
                    change = True
                elif self.git_url:
                    self.git_repo = get_repo(url=self.git_url,
                                             directory=self.working_dir)
                    logger.debug(
                        "we just cloned git repo %s to %s",
                        self.git_url,
                        self.working_dir,
                    )
                    change = True

            if self.git_url and not self.working_dir and not self.git_repo:
                self.git_repo = get_repo(url=self.git_url)
                self.working_dir_temporary = True
                change = True

            if self.git_project and not self.git_url:
                self.git_url = self.git_project.get_git_urls()["git"]
                change = True

            if self.git_project and not self.repo_name:
                self.repo_name = self.git_project.repo
                change = True

            if self.git_project and not self.namespace:
                self.namespace = self.git_project.namespace
                change = True

            if self.git_repo and not self.git_url:
                # this is prone to errors
                # also if we want url to upstream, we may want to ask for it explicitly
                # since this can point to a fork
                # .urls returns generator
                self.git_url = list(self.git_repo.remote().urls)[0]
                logger.debug("remote url of the repo is %s", self.git_url)
                change = True

        if ref:

            if ref not in self.git_repo.branches:
                self.git_repo.create_head(self._ref)

            self.git_repo.branches[self._ref].checkout()
Ejemplo n.º 4
0
    def __init__(
        self,
        git_repo: git.Repo = None,
        working_dir: str = None,
        branch: str = None,
        git_project: GitProject = None,
        git_service: GitService = None,
        git_url: str = None,
        full_name: str = None,
        namespace: str = None,
        repo_name: str = None,
    ) -> None:

        self.git_repo = git_repo
        self.working_dir = working_dir
        self._branch = branch
        self.git_project = git_project
        self.git_service = git_service
        self.git_url = git_url
        self.full_name = full_name
        self.repo_name = repo_name
        self.namespace = namespace
        self.working_dir_temporary = False

        change = True
        while change:
            change = False

            if self.repo_name and self.namespace and not self.full_name:
                self.full_name = f"{self.namespace}/{self.repo_name}"
                change = True

            if self.full_name and not self.namespace:
                self.namespace = self.full_name.split("/")[0]
                change = True

            if self.full_name and not self.repo_name:
                self.repo_name = self.full_name.split("/")[1]
                change = True

            if (self.repo_name and self.namespace and self.git_service
                    and not self.git_project):
                self.git_project = self.git_service.get_project(
                    repo=self.repo_name, namespace=self.namespace)
                change = True

            if self.git_project and not self.git_service:
                self.git_service = self.git_project.service
                change = True

            if self.git_repo and not self._branch:
                self._branch = self.git_repo.active_branch
                change = True

            if self.git_repo and not self.working_dir:
                self.working_dir = self.git_repo.working_dir
                change = True

            if self.working_dir and not self.git_repo:
                if is_git_repo(directory=self.working_dir):
                    self.git_repo = git.Repo(path=self.working_dir)
                    change = True
                elif self.git_url:
                    self.git_repo = get_repo(url=self.git_url,
                                             directory=self.working_dir)
                    change = True

            if self.git_url and not self.working_dir and not self.git_repo:
                self.git_repo = get_repo(url=self.git_url)
                self.working_dir_temporary = True
                change = True

            if self.git_project and not self.git_url:
                self.git_url = self.git_project.get_git_urls()["git"]
                change = True

            if self.git_project and not self.repo_name:
                self.repo_name = self.git_project.repo
                change = True

            if self.git_project and not self.namespace:
                self.namespace = self.git_project.namespace
                change = True

            if self.git_repo and not self.git_url:
                self.git_url = self.git_repo.remote().urls[0]
                change = True

        if branch:

            if branch not in self.git_repo.branches:
                self.git_repo.create_head(self._branch)

            self.git_repo.branches[self._branch].checkout()