Skip to content

Commit 4bcba91

Browse files
committed
new performant way to calculate the last modified date #824
1 parent f2b5a30 commit 4bcba91

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

core/utils/date.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,31 @@ export function getZonedDateTime(
4444
return Temporal.PlainDateTime.from(date).toZonedDateTime(timezone);
4545
}
4646
}
47+
48+
/**
49+
* Thanks to https://meiert.com/blog/eleventy-git-last-modified/
50+
*/
51+
export function getGitLastModified(path = "."): Map<string, string> {
52+
const dates = new Map<string, string>();
53+
const args = ["log", "-1", "--format=%at", "--", path];
54+
const { stdout, success } = new Deno.Command("git", { args }).outputSync();
55+
if (!success) {
56+
return dates;
57+
}
58+
let currentDate: string | undefined;
59+
const str = new TextDecoder().decode(stdout);
60+
for (const line of str.split("\n")) {
61+
const text = line.trim();
62+
63+
if (text.startsWith("DATE:")) {
64+
currentDate = text.slice(5).trim();
65+
} else if (text && currentDate) {
66+
// First commits, last modification
67+
if (!dates.has(text)) {
68+
dates.set(text, currentDate);
69+
}
70+
}
71+
}
72+
73+
return dates;
74+
}

0 commit comments

Comments
 (0)