home readme diff tree note docs

0commit 4eb94607793bb5701f1de6fa87d3367bc009554c
1Author: lakefox <mason@lakefox.net>
2Date:   Sat May 23 11:35:27 2026 -0600
3
4    Improved
5
6diff --git a/cgi-bin/cgi.awk b/cgi-bin/cgi.awk
7index 0dc95e4..d16c0df 100755
8--- a/cgi-bin/cgi.awk
9+++ b/cgi-bin/cgi.awk
10@@ -7,152 +7,34 @@ BEGIN {
11         for (i in pairs) {
12                 split(pairs[i], kv, "=")
13 
14-                if (kv[1] == "action") {
15-                        action = sanitize(kv[2])
16-                } else if (kv[1] == "name") {
17+                if (kv[1] == "name") {
18                         name = sanitize(kv[2])
19                 } else if (kv[1] == "hash") {
20                         hash = sanitize(kv[2])
21                 } else if (kv[1] == "path") {
22                         path = sanitize(kv[2])
23-                }
24-        }
25-
26-        print "Content-Type: text/html\r\n"
27-        print "<!DOCTYPE html>"
28-        print "<html>"
29-        print "<head>"
30-
31-        if (name == "") name = "all"
32-        if (hash == "") hash = "HEAD"
33-
34-        print "<title>Git: " name "</title>"
35-        print "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>"
36-        print "<meta name='viewport' content='width=device-width, initial-scale=1'>"
37-        print "</head>"
38-        print "<body>"
39-
40-        if (name != "all") {
41-                print "<a href='?name=" name "'>home</a>"
42-                print "<a href='?name=" name "&hash=" hash "&action=readme'>readme</a>"
43-                print "<a href='?name=" name "&hash=" hash "&action=diff'>diff</a>"
44-                print "<a href='?name=" name "&hash=" hash "&action=tree'>tree</a>"
45-                print "<a href='?name=" name "&hash=" hash "&action=notes'>note</a>"
46-                print "<a href='?name=" name "&hash=" hash "&action=docs'>docs</a>"
47-        }
48-
49-        if (action == "") {
50-                if (name == "all") {
51-                        cmd = "ls /var/git/"
52-                        print "<h1>Git</h1>"
53-                        print "<p>Select a repo to view.</p>"
54-                
55-                        print "<ul>"
56-                        while (cmd | getline file) {
57-                                split(file, k, ".")
58-                                print "<li><a href='/cgi-bin/git.awk?name=" k[1] "'>/" k[1] "</a></li>"
59-                        }
60-                        close(cmd)
61-                        print "</ul>"
62                 } else {
63-                        cmd = "git --git-dir=/var/git/" name ".git/ log"
64-                        print "<pre><code>"     
65-                        while (cmd | getline line) {
66-
67-                                if (line ~ /^commit/) {
68-                                        split(line, commit, " ")
69-                                        print "<a href='/cgi-bin/git.awk?name=" name "&hash=" commit[2] "&action=diff'>commit " commit[2] "</a>"
70-                                } else {
71-                                        print line
72-                                }
73-                        }
74-                        close(cmd)
75-                        print "</code></pre>"
76-                }
77-        } else if (action == "tree") {
78-                cmd = "git --git-dir=/var/git/" name ".git/ show " hash ":" path
79-                print "<br/>"
80-                print "<b>" hash ":" path "</b>"
81-                print "<pre><code>"     
82-                if (substr(path, length(path), 1) == "/" || path == "") {
83-                        while (cmd | getline line) {
84-                                if (line !~ /^tree/) {
85-                                        print "<a href='/cgi-bin/git.awk?name=" name "&hash=" hash "&action=tree&path=" path line "'>" line "</a>"
86-                                }
87-                        }
88-                } else {
89-                        counter = 0
90-                        while (cmd | getline line) {
91-                                print "<span id='" counter "'><a href='#" counter "' style='user-select:none;'>" counter "</a></span>" escape_html(line)
92-                                counter += 1
93-                        }
94-                }
95-                close(cmd)
96-                print "</code></pre>"
97-        } if (action == "diff") {
98-                cmd = "git --git-dir=/var/git/" name ".git/ show " hash
99-                print "<pre><code>"     
100-                counter = 0
101-		while (cmd | getline line) {
102-			print "<span id='" counter "'><a href='#" counter "' style='user-select:none;'>" counter "</a></span>" escape_html(line)
103-			counter += 1
104+			 params[kv[1]] = sanitize(kv[2])
105 		}
106-                close(cmd)
107-                print "</code></pre>"
108-
109-        } if (action == "readme") {
110-                # 1. discover the README file name (single match assumed)
111-                cmd = "git --git-dir=/var/git/" name ".git ls-tree -r " hash " --name-only | grep '^README\\.'"
112-                fn = ""
113-                while ((cmd | getline fn) > 0) break      # take first match
114-                close(cmd)
115-                if (fn == "") { print "<br/>README not found for: " hash; exit 1 }
116+        }
117 
118-                cmd = "git --git-dir=/var/git/" name ".git show " hash ":" fn
119-                print "<pre><code>"
120-                while ((cmd | getline line) > 0) {
121-                        print escape_html(line)
122-                }
123-                close(cmd)
124-                print "</code></pre>"   
125-        } else if (action == "notes") {
126-                cmd = "git --git-dir=/var/git/" name ".git/ notes show " hash
127-                
128-                print "<pre><code>"     
129-                while (cmd | getline line) {
130-                        print escape_html(line)
131-                }
132-                close(cmd)
133-                print "</code></pre>"
134-        } else if (action == "docs") {
135-                if (path == "") {
136-                        cmd = "git --git-dir=/var/git/" name ".git/ ls-tree -r --name-only " hash ":docs/";
137-                   
138-                        print "<h1>Index</h1>"
139-                        print "<ul>"    
140-                        while ((cmd | getline) > 0) {
141-                                print "<li><a href=\"/cgi-bin/git.awk?name=" name "&hash=" hash "&action=docs&path=" $0 "\">" escape_html($0) "</a></li>";
142-                        }
143-                        print "</ul>"
144+        if (name == "") { print "Status: 400 Bad Request\r\n\r\nMissing repository."; exit }
145+	if (hash == "") hash = "HEAD"
146 
147-                        close(cmd);
148-                } else {
149-                        cmd = "git --git-dir=/var/git/" name ".git/ show " hash ":docs/" path " | mandoc -O width=120";
150-                    
151-                        print "<pre><code>"
152-                        while ((cmd | getline) > 0) {
153-				gsub(/.\010/, "", $0);
154-                                print escape_html($0);
155-                        }
156-                        print "</code></pre>"   
157+	query_args = ""
158+	for (p in params) query_args = query_args p "=" params[p] " "
159 
160-                        close(cmd);
161+	file = "/tmp/" hash "/" path
162 
163-                }
164-        }
165+	if (system("test -x " file) != 0) {
166+		system("mkdir -p /tmp/" hash " && git --git-dir=/var/git/" name ".git/ show " hash ":" path " > " file " && chmod +x " file)
167+	}
168 
169-        print "</body>"
170-        print "</html>"
171+	cmd = query_args " " file
172+	while ((cmd | getline line) > 0) {
173+		print line
174+	}
175+	close(cmd)
176 }
177 
178