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

90 lines
2.8 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;
}
2023-02-23 05:53:56 +03:00
export async function getLogs(files, range, limit) {
const args = ["log", "-n", String((limit != null) ? limit : 100)];
if (range) {
args.push(`${range.tag0}..${range.tag1}`);
2022-09-30 06:19:40 +03:00
}
2023-02-23 05:53:56 +03:00
if (files) {
args.push("--");
files.forEach((f) => args.push(f));
}
const exec = await run("git", args);
if (!exec.ok) {
2022-09-30 06:19:40 +03:00
throw new Error(`git log error`);
}
2023-02-23 05:53:56 +03:00
const log = exec.stdout.trim();
2022-09-30 06:19:40 +03:00
if (!log) {
return [];
}
2023-02-23 05:53:56 +03:00
const results = [{ commit: "", author: "", date: "", body: "" }];
2022-09-30 06:19:40 +03:00
for (const line of log.split("\n")) {
const hashMatch = line.match(/^commit\s+([0-9a-f]{40})/i);
if (hashMatch) {
2023-02-23 05:53:56 +03:00
results.push({ commit: hashMatch[1], author: "", date: "", body: "" });
2022-09-30 06:19:40 +03:00
}
else {
if (line.startsWith("Author:")) {
2023-02-23 05:53:56 +03:00
results[results.length - 1].author = line.substring(7).trim();
2022-09-30 06:19:40 +03:00
}
else if (line.startsWith("Date:")) {
2023-02-23 05:53:56 +03:00
results[results.length - 1].date = line.substring(5).trim();
2022-09-30 06:19:40 +03:00
}
else {
2023-02-23 05:53:56 +03:00
results[results.length - 1].body = (results[results.length - 1].body + " " + line).trim();
2022-09-30 06:19:40 +03:00
}
}
}
// Nix the bootstrap entry
2023-02-23 05:53:56 +03:00
results.shift();
return results;
}
export async function getDiff(filename, tag0, tag1) {
const result = await run("git", ["diff", `${tag0}..${tag1}`, "--", filename]);
if (!result.ok) {
throw new Error(`git log error`);
}
return result.stdout.trim();
2022-09-30 06:19:40 +03:00
}
2022-09-27 10:45:27 +03:00
//# sourceMappingURL=git.js.map