about summary refs log tree commit diff stats
path: root/bonus/git.cgi
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-24 16:09:43 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-24 16:26:28 +0100
commit5d452786d9e012c77b2ab002da9d15bab2273f42 (patch)
tree52d755b06d183fff696c0cabdce3053ec5fc36c6 /bonus/git.cgi
parentd01670fd68c89a5ad39396e47155f2009d697578 (diff)
downloadchawan-5d452786d9e012c77b2ab002da9d15bab2273f42.tar.gz
git.cgi: add custom git switch
just a clickable UI for switching branches
Diffstat (limited to 'bonus/git.cgi')
-rwxr-xr-xbonus/git.cgi51
1 files changed, 35 insertions, 16 deletions
diff --git a/bonus/git.cgi b/bonus/git.cgi
index 5bbf12a3..3f2ed65e 100755
--- a/bonus/git.cgi
+++ b/bonus/git.cgi
@@ -1,11 +1,11 @@
 #!/usr/bin/env -S qjs --std
-/* adds clickable links to commit hashes
+/* adds clickable links to commit hashes + a clickable UI to git switch
  * usage:
  * 0. install QuickJS (https://bellard.org/quickjs)
  * 1. put this script in your CGI directory
  * 2. chmod +x /your/cgi-bin/directory/git.cgi
  * 3. ln -s /your/cgi-bin/directory/git.cgi /usr/local/bin/gitcha
- * 4. gitcha log
+ * 4. run `gitcha log' or `gitcha switch'
  * other params work too, but for those it's more convenient to use git.
  * (if you have ansi2html, it also works with w3m. just set GITCHA_CHA=w3m) */
 
@@ -26,14 +26,14 @@ for (const p of std.getenv("QUERY_STRING").split('&')) {
 	query[decodeURIComponent(sp[0])] = decodeURIComponent(sp[1] ?? '');
 }
 
-os.chdir(query.path);
-
-const config = ["-c", "color.ui=always", "-c", "log.decorate=short"];
-const params = query.params ? decodeURIComponent(query.params).split(' ')
-	.map(x => decodeURIComponent(x)) : [];
+function startGitCmd(config, params) {
+	const titleParams = params.join(' ').replace(/[&<>]/g,
+		x => ({'&': '&amp', '<': '&lt', '>': '&gt'}[x]));
+	std.out.puts(`Content-Type: text/html
 
-if (params[0] == "log") {
-	std.out.puts('Content-Type: text/html\n\n');
+<!DOCTYPE html>
+<title>git ${titleParams}</title>`);
+	std.out.flush();
 	const [read_fd, write_fd] = os.pipe();
 	const [read_fd2, write_fd2] = os.pipe();
 	os.exec(["git", ...config, ...params], {
@@ -50,18 +50,37 @@ if (params[0] == "log") {
 	});
 	os.close(read_fd);
 	os.close(write_fd2);
-	const f = std.fdopen(read_fd2, "r");
+	return std.fdopen(read_fd2, "r");
+}
+
+os.chdir(query.path);
+
+const config = ["-c", "color.ui=always", "-c", "log.decorate=short"];
+const params = query.params ? decodeURIComponent(query.params).split(' ')
+	.map(x => decodeURIComponent(x)) : [];
+
+if (params[0] == "log") {
+	const f = startGitCmd(config, params);
 	const cgi = `${query.prefix}git.cgi?prefix=${query.prefix}&path=${query.path}&params=show`;
-	const titleParams = params.join(' ').replace(/[&<>]/g,
-		x => ({'&': '&amp', '<': '&lt', '>': '&gt'}[x]));
-	console.log(`<!DOCTYPE html>\n<title>git ${titleParams}</title>`);
 	while ((l = f.getline()) !== null) {
 		console.log(l.replace(/[a-f0-9]{40}/g,
 			x => `<a href='${cgi}%20${x}'>${x}</a>`));
 	}
 	f.close();
+} else if (params[0] == "switch" && params.length == 1) {
+	const f = startGitCmd(config, ["branch"]);
+	const cgi = `${query.prefix}git.cgi?prefix=${query.prefix}&path=${query.path}&params=switch`;
+	while ((l = f.getline()) !== null) {
+		console.log(l.replace(/^(\s+)([\w.-]+)$/g,
+			(_, ws, name) => `${ws}<a href='${cgi}%20${name}'>${name}</a>`));
+	}
+	f.close();
 } else {
-	std.out.puts('Content-Type: text/x-ansi\n\n');
-	const pid = os.exec(["git", ...config, ...params], {block: false});
-	waitpid(pid, 0);
+	std.out.puts("Content-Type: text/x-ansi\n\n");
+	std.out.flush();
+	const pid = os.exec(["git", ...config, ...params], {
+		block: false,
+		stderr: 1
+	});
+	os.waitpid(pid, 0);
 }