summaryrefslogtreecommitdiff
path: root/git-gmi/git.py
diff options
context:
space:
mode:
Diffstat (limited to 'git-gmi/git.py')
-rw-r--r--git-gmi/git.py55
1 files changed, 41 insertions, 14 deletions
diff --git a/git-gmi/git.py b/git-gmi/git.py
index 9a0ab0d..990ca0c 100644
--- a/git-gmi/git.py
+++ b/git-gmi/git.py
@@ -2,6 +2,9 @@ from pygit2 import *
import mimetypes
from const import *
+mimetypes.add_type("text/gemini", ".gmi")
+mimetypes.add_type("text/gemini", ".gemini")
+
class GitGmiRepo:
def __init__(self, name: str, path: str):
@@ -9,25 +12,37 @@ class GitGmiRepo:
self.path = path
try:
self.repo = Repository(path)
- except FileNotFoundError:
- print(f"Error: repository {path} not found")
+ except GitError:
+ raise FileNotFoundError(f"Error: no such repo: {name}")
+
+ def generate_header(self):
+ header = (
+ f"# {self.name}\n"
+ f"=> {CGI_PATH} {GIT_GMI_SITE_TITLE}\n"
+ f"=> {CGI_PATH}{self.name}/summary summary\n"
+ f"=> {CGI_PATH}{self.name}/tree/master/ tree\n"
+ f"=> {CGI_PATH}{self.name}/log log\n\n"
+ )
+ return header
def view_summary(self) -> str:
+ response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header()
tree = self.get_tree("master")
trls = self.list_tree(tree)
+ found_readme = False
for item in trls:
- if item["type"] == "file" and item["name"].lower().split(".")[0] == (
- "readme"
+ if (
+ item["type"] == "file"
+ and item["name"].lower().split(".")[0] == ("readme")
+ and not found_readme
):
- # mimetypes.guess_type() returns tuple (type, encoding)
- # only the first one of which we care about
- response = (
- f"{STATUS_SUCCESS} {mimetypes.guess_type(item['name'])[0]}\n"
- "=> tree/master/ tree\n"
- "=> log/ log\n\n"
+ found_readme = True
+ response += (
+ f"## {item['name']} | {item['size']} bytes\n"
+ f"{item['blob'].data.decode('utf-8')}"
)
- response += item["blob"].data.decode("utf-8")
- break
+ if not found_readme:
+ response += "## No readme found."
return response
def get_commit_log(self) -> list:
@@ -48,7 +63,7 @@ class GitGmiRepo:
return log # reverse chronical order
def view_log(self) -> str:
- response = f"{STATUS_SUCCESS} {META_GEMINI}\n"
+ response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header()
log = self.get_commit_log()
for cmt in log:
response += f"## {cmt['short_id']} - {cmt['author']}\n{cmt['msg']}\n\n"
@@ -123,7 +138,7 @@ class GitGmiRepo:
def view_tree(self, branch: str, location=[]) -> str:
# actual Gemini response
# consists of a header and a body
- response = f"{STATUS_SUCCESS} {META_GEMINI}\n"
+ response = f"{STATUS_SUCCESS} {META_GEMINI}\n" + self.generate_header()
tree = self.get_tree(branch)
contents = self.list_tree(tree, location)
for item in contents:
@@ -151,6 +166,18 @@ class GitGmiRepo:
def view_blob(self, branch: str, location=[]) -> str:
blob = self.get_blob(branch, location)
+ response = (
+ f"{STATUS_SUCCESS} {META_GEMINI}\n"
+ + self.generate_header()
+ + f"## {self.name}/{'/'.join(location)} | {blob.size} bytes\n\n"
+ f"=> {blob.name}?raw view raw\n\n"
+ f"```\n"
+ )
+ response += blob.data.decode("utf-8") + "\n```"
+ return response
+
+ def view_raw_blob(self, branch: str, location=[]) -> str:
+ blob = self.get_blob(branch, location)
guessed_mimetype = mimetypes.guess_type(blob.name)[0] or "text/plain"
response = f"{STATUS_SUCCESS} {guessed_mimetype}\n"
response += blob.data.decode("utf-8")