~fkfd/git.gmi

c6d86838dcd22296278b249d995b0b38632a82bf — Frederick Yin 1 year, 2 months ago 036159d v0.3.0
Refs view
2 files changed, 40 insertions(+), 6 deletions(-)

M git-gmi/gateway.py
M git-gmi/git.py
M git-gmi/gateway.py => git-gmi/gateway.py +8 -0
@@ 73,5 73,13 @@ def handle_cgi_request(path: str, query: str):
            print(STATUS_TEMPORARY_FAILURE)
            return

    elif view == "refs":
        try:
            print(repo.view_refs())
            return
        except:
            print(STATUS_TEMPORARY_FAILURE)
            return


handle_cgi_request(environ.get("PATH_INFO"), environ.get("QUERY_STRING"))

M git-gmi/git.py => git-gmi/git.py +32 -6
@@ 30,7 30,8 @@ class GitGmiRepo:
            f"=> {CGI_PATH} {GIT_GMI_SITE_TITLE}\n"
            f"=> {CGI_PATH}{self.name}/summary summary\n"
            f"=> {CGI_PATH}{self.name}/tree/{MAIN_BRANCH}/ tree\n"
            f"=> {CGI_PATH}{self.name}/log log\n\n"
            f"=> {CGI_PATH}{self.name}/log log\n"
            f"=> {CGI_PATH}{self.name}/refs refs\n\n"
        )
        return header



@@ 93,6 94,28 @@ class GitGmiRepo:
            )
        return response

    def get_refs(self) -> list:
        refs = self.repo.listall_reference_objects()
        return [
            {
                "name": ref.name,
                "shorthand": ref.shorthand,
                "target": ref.target,
                "type": ref.type,
            }
            for ref in refs
        ]

    def view_refs(self) -> str:
        response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header()
        refs = self.get_refs()
        for ref in refs:
            response += (
                f"## {ref['shorthand']}\n"
                f"=> tree/{ref['shorthand']}/ view tree\n\n"
            )
        return response

    @classmethod
    def parse_recursive_tree(cls, tree: Tree) -> list:
        # recursively replace all Trees with a list of Blobs inside it,


@@ 105,14 128,17 @@ class GitGmiRepo:

        return tree_list

    def get_tree(self, commit_str: str) -> list:
    def get_tree(self, revision_str: str) -> list:
        # returns a recursive list of Blob objects
        try:
            commit = self.repo.revparse_single(commit_str)
            # top level tree; may contain sub-trees
            return self.parse_recursive_tree(commit.tree)
            revision = self.repo.revparse_single(revision_str)
            if isinstance(revision, Commit):
                # top level tree; may contain sub-trees
                return self.parse_recursive_tree(revision.tree)
            elif isinstance(revision, Tag):
                return self.parse_recursive_tree(revision.get_object().tree)
        except ValueError:
            raise FileNotFoundError(f"Error: no such tree: {commit_str}")
            raise FileNotFoundError(f"Error: no such tree: {revision_str}")
            return None

    @staticmethod