def stream_data(self, ostream): """Writes our data directly to the given output stream :param ostream: File object compatible stream object. :return: self""" istream = self.repo.odb.stream(self.binsha) stream_copy(istream, ostream) return self
def tree_to_github( tree: git.Tree, target_subdir: str, gh_repo: GithubRepo ) -> Dict[str, InputGitTreeElement]: """Extrae los contenidos de un commit de Git en formato Tree de Github. Returns: un diccionario donde las claves son rutas en el repo, y los valores el InputGitTreeElement que los modifica. """ odb = tree.repo.odb target_subdir = target_subdir.rstrip("/") + "/" entries = traverse_tree_recursive(odb, tree.binsha, target_subdir) contents = {} for sha, mode, path in entries: # TODO: get exclusion list from repos.yml if path.endswith("README.md"): continue fileobj = io.BytesIO() stream_copy(odb.stream(sha), fileobj) fileobj.seek(0) try: text = fileobj.read().decode("utf-8") input_elem = InputGitTreeElement(path, f"{mode:o}", "blob", text) except UnicodeDecodeError: # POST /trees solo permite texto, hay que crear un blob para binario. fileobj.seek(0) data = base64.b64encode(fileobj.read()) blob = gh_repo.create_git_blob(data.decode("ascii"), "base64") input_elem = InputGitTreeElement(path, f"{mode:o}", "blob", sha=blob.sha) finally: contents[path] = input_elem return contents