about summary refs log tree commit diff stats
path: root/bonus/git.cgi
blob: 29390d7417e6b07594a5945ffa70e30bcdda4a6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env -S qjs --std
/* adds clickable links to git log, git branch and git stash list
 * 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. run `gitcha log', `gitcha switch' or `gitcha stash list'
 * other params work too, but without any special processing. it's still useful
 * for ones that open the pager, like git show; this way you can reload the view
 * with `U'.
 * it's less convenient for e.g. git checkout and friends, so it may be best to
 * just alias the pager-opening commands.
 * (if you have ansi2html, it also works with w3m. just set GITCHA_CHA=w3m) */

const gitcha = std.getenv("GITCHA_GITCHA") ?? "gitcha";
if (scriptArgs[0].split('/').pop() == gitcha) {
	const cha = std.getenv("GITCHA_CHA") ?? 'cha';
	const params = encodeURIComponent(scriptArgs.slice(1)
		.map(x => encodeURIComponent(x)).join(' '));
	const [path, _] = os.getcwd();
	const prefix = cha == "w3m" ? '/cgi-bin/' : "cgi-bin:";
	os.exec([cha, `${prefix}git.cgi?params=${params}&path=${path}&prefix=${prefix}`]);
	std.exit(0);
}

const query = {};
for (const p of std.getenv("QUERY_STRING").split('&')) {
	const sp = p.split('=');
	query[decodeURIComponent(sp[0])] = decodeURIComponent(sp[1] ?? '');
}

function startGitCmd(config, params) {
	std.out.puts("Content-Type: text/html\n\n");
	std.out.flush();
	const [read_fd, write_fd] = os.pipe();
	const [read_fd2, write_fd2] = os.pipe();
	os.exec(["git", ...config, ...params], {
		stdout: write_fd,
		block: false
	});
	os.close(write_fd);
	const libexecDir = std.getenv("CHA_LIBEXEC_DIR") ??
		'/usr/local/libexec/chawan';
	const title = encodeURIComponent('git ' + params.join(' '));
	os.exec([libexecDir + "/ansi2html", "-st", title], {
		stdin: read_fd,
		stdout: write_fd2,
		block: false
	});
	os.close(read_fd);
	os.close(write_fd2);
	return std.fdopen(read_fd2, "r");
}

function runGitCmd(config, params, regex, subfun) {
	const f = startGitCmd(config, params);
	while ((l = f.getline()) !== null) {
		console.log(l.replace(regex, subfun));
	}
	f.close();
}

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)) : [];

const cgi0 = `${query.prefix}git.cgi?prefix=${query.prefix}&path=${query.path}`;
const cgi1 = `${cgi0}&params=show`;
const cgi2 = `${cgi0}&params=log`;
const cgi3 = `${cgi0}&params=switch`;
const cgi4 = `${cgi0}&params=stash%20apply`;
if (params[0] == "log") {
	runGitCmd(config, params, /[a-f0-9]{40}/g,
		x => `<a href='${cgi1}%20${x}'>${x}</a>`)
} else if (params[0] == "branch" && params.length == 1) {
	runGitCmd(config, params, /^(\s+)([\w.-]+)$/g,
		(_, ws, name) => `${ws}<a href='${cgi2}%20${name}'>${name}</a>\
 (<a href='${cgi3}%20${name}'>switch</a>)`);
} else if (params[0] == "stash" && params[1] == "list") {
	runGitCmd(config, params, /^stash@\{([0-9]+)\}/g,
		(s, n) => `stash@{<a href='${cgi1}%20${s}'>${n}</a>}\
 (<a href='${cgi4}%20${s}'>apply</a>)`);
} else {
	const title = encodeURIComponent('git ' + params.join(' '));
	std.out.puts(`Content-Type: text/x-ansi;title=${title}\n\n`);
	std.out.flush();
	const pid = os.exec(["git", ...config, ...params], {
		block: false,
		stderr: 1
	});
	os.waitpid(pid, 0);
}