aboutsummaryrefslogtreecommitdiff
path: root/api/git.js
blob: b8d89c3dd58a08a6d4128a68cfd855828dc5eb79 (plain)
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
const { exec } = require("child_process");

const log_format='{"hash": "%H", "author": "%an", "author_email": "%ae", "date": "%at", "subject": "%s"}'

function getLog(path)
{
	return new Promise((resolve) =>
	{
		exec(`git -C ${path} log --format=format:'${log_format}'`, (error, stdout, stderr) =>
		{
			if(error) {
				const no_such_fileor_dir = new RegExp(`cannot change to '${path.replace('/', "\/")}': No such file or directory\\n$`);

				if(no_such_fileor_dir.test(error.message)) {
					resolve({ "error": 404 });
					return;
				}
				resolve({ "error": error.message });
				return;
			}
			if(stderr) {
				resolve({ "error": "Failed to communicate with git!" });
				return;
			}

			const log = stdout.split('\n');
			log.forEach((entry, index) => log[index] = JSON.parse(entry));

			resolve({ "data": log });
		});
	});
}

function getTimeSinceLatestCommit(path)
{
	return new Promise((resolve) =>
	{
		exec(`git -C ${path} log -n 1 --date=unix'`, (error, stdout, stderr) =>
		{
			if(error) {
				const no_such_fileor_dir = new RegExp(`cannot change to '${path.replace('/', "\/")}': No such file or directory\\n$`);

				if(no_such_fileor_dir.test(error.message)) {
					resolve({ "error": 404 });
					return;
				}
				resolve({ "error": error.message });
				return;
			}
			if(stderr) {
				resolve({ "error": "Failed to communicate with git!" });
				return;
			}

			const 

			resolve({ "data": log });
		});
	});
}

module.exports.getLog = getLog;