~fkfd/git.gmi

036159de2770b5ca9698474dd15fb63e04a51190 — Frederick Yin 1 year, 2 months ago 121921c
Enhance comments
2 files changed, 10 insertions(+), 2 deletions(-)

M git-gmi/gateway.py
M git-gmi/git.py
M git-gmi/gateway.py => git-gmi/gateway.py +5 -1
@@ 9,7 9,8 @@ from os import environ, listdir

def handle_cgi_request(path: str, query: str):
    # intended to work with Jetforce.
    # url: gemini://git.gemini.site/cgi-bin/cgi.py/repo/src/static/css/[index.css]
    # hypothetical example:
    # url: gemini://git.gemini.site/git/cgi/repo/src/static/css/[index.css]
    # path: /repo/src/static/css/[index.css]
    # path_trace = ['repo', 'src', 'static', 'css', 'index.css']
    path_trace = path[1:].split("/")


@@ 29,6 30,7 @@ def handle_cgi_request(path: str, query: str):
    if len(path_trace) > 1:
        view = path_trace[1]  # e.g. summary, tree, log
    else:
        # gemini://git.gemini.site/git/cgi/<repo>/
        print("31 summary")
        return



@@ 42,10 44,12 @@ def handle_cgi_request(path: str, query: str):

    elif view == "tree":
        if len(path_trace) == 2:
            # gemini://git.gemini.site/git/cgi/<repo>/tree/
            print(f"31 {MAIN_BRANCH}/")
            return

        if len(path_trace) > 2:
            # gemini://git.gemini.site/git/cgi/<repo>/tree/<branch>/
            branch = path_trace[2]

        location = path_trace[3:]

M git-gmi/git.py => git-gmi/git.py +5 -1
@@ 10,6 10,7 @@ mimetypes.add_type("text/gemini", ".gemini")


def convert_filesize(bytes: int) -> str:
    # convert filesize in bytes to a human-friendly format
    return size(bytes, system=alternative)




@@ 23,6 24,7 @@ class GitGmiRepo:
            raise FileNotFoundError(f"Error: no such repo: {name}")

    def generate_header(self):
        # global "header" to display above all views (except raw files)
        header = (
            f"# {self.name}\n"
            f"=> {CGI_PATH} {GIT_GMI_SITE_TITLE}\n"


@@ 62,7 64,7 @@ class GitGmiRepo:
        return response

    def get_commit_log(self) -> list:
        # returns the commit log in a human-readable way.
        # returns useful info from commit log.
        repo = self.repo
        commits = list(repo.walk(repo[repo.head.target].id, GIT_SORT_TIME))
        log = [


@@ 82,6 84,7 @@ class GitGmiRepo:
        response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header()
        log = self.get_commit_log()
        for cmt in log:
            # looks like "2020-06-06 04:51:21 UTC"
            time = str(datetime.utcfromtimestamp(cmt["time"])) + " UTC"
            response += (
                f"## {cmt['short_id']} - {cmt['author']} - {time}\n"


@@ 199,6 202,7 @@ class GitGmiRepo:

    def view_raw_blob(self, branch: str, location=[]) -> str:
        blob = self.get_blob(branch, location)
        # if mimetypes can't make out the type, set it to plaintext
        guessed_mimetype = mimetypes.guess_type(blob.name)[0] or "text/plain"
        response = f"{STATUS_SUCCESS} {guessed_mimetype}\n"
        response += blob.data.decode("utf-8")