export const prerender = true;

import { execSync } from 'child_process';

const GITLOGCMD = `
git log --pretty=format:"COMMIT:%h|%ad|%s|%an|%aE" --all --numstat --date=unix | awk '
                                                  /^COMMIT:/ {
                                                    if (commit) print commit "|" add "|" del
                                                    commit = substr($0, 8)
                                                    add = 0; del = 0
                                                  }
                                                  /^[0-9]/ { add += $1; del += $2 }
                                                  END { if (commit) print commit "|" add "|" del }'

`

// Example Commit Output
// 2c72bac|1779833088|just file, docker file and website css fixes|Doloro1978|doloroo@proton.me|39|2

export type commit = {
	hash: string,
	date: Date,
	title: string,
	auther: string,
	email: string,
	additions: number,
	deletions: number,
}

export async function load() {
	const raw = execSync(GITLOGCMD).toString();
	const lines = raw.split('\n');
	console.log(lines);
	const commits: commit[] = []
	for (var x of lines) {
		const [hash, unix, title, auther, email, add, del] = x.split("|")
		var date = new Date(+unix * 1000);
		commits.push({
			hash: hash,
			date: date,
			title: title,
			auther: auther,
			email: email,
			additions: +add,
			deletions: +del,
		})
	}
	return {
		data: {
			commits: commits
		}
	};
}
