ethers.js/lib.esm/_admin/utils/git.js

78 lines
2.3 KiB
JavaScript
Raw Normal View History

import { dirname } from "path";
import { fileURLToPath } from "url";
2022-09-27 10:45:27 +03:00
import { run } from "./run.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
2022-09-27 10:45:27 +03:00
// Returns the most recent git commit hash for a given filename
export async function getGitTag(filename) {
const result = await run("git", ["log", "-n", "1", "--", filename], __dirname);
2022-09-27 10:45:27 +03:00
if (!result.ok) {
throw new Error(`git log error`);
}
let log = result.stdout.trim();
if (!log) {
return null;
}
const hashMatch = log.match(/^commit\s+([0-9a-f]{40})\n/i);
if (!hashMatch) {
return null;
}
return hashMatch[1];
}
export async function getModifiedTime(filename) {
const result = await run("git", ["log", "-n", "1", "--", filename], __dirname);
if (!result.ok) {
throw new Error(`git log error`);
}
let log = result.stdout.trim();
if (!log) {
return null;
}
for (let line of log.split("\n")) {
line = line.trim();
if (!line) {
break;
}
const match = line.match(/^date:\s+(.*)$/i);
if (match) {
return (new Date(match[1].trim())).getTime();
;
}
}
return null;
}
2022-09-30 06:19:40 +03:00
export async function getGitLog(filename, limit) {
if (limit == null) {
limit = 100;
}
const result = await run("git", ["log", "-n", String(limit), "--", filename]);
if (!result.ok) {
throw new Error(`git log error`);
}
let log = result.stdout.trim();
if (!log) {
return [];
}
const logs = [{ commit: "", author: "", date: "", body: "" }];
for (const line of log.split("\n")) {
const hashMatch = line.match(/^commit\s+([0-9a-f]{40})/i);
if (hashMatch) {
logs.push({ commit: hashMatch[1], author: "", date: "", body: "" });
}
else {
if (line.startsWith("Author:")) {
logs[logs.length - 1].author = line.substring(7).trim();
}
else if (line.startsWith("Date:")) {
logs[logs.length - 1].date = line.substring(5).trim();
}
else {
logs[logs.length - 1].body = (logs[logs.length - 1].body + " " + line).trim();
}
}
}
// Nix the bootstrap entry
logs.shift();
return logs;
}
2022-09-27 10:45:27 +03:00
//# sourceMappingURL=git.js.map