Skip to content

Commit 8dace45

Browse files
DCodeBotdector
andcommitted
feat(footer): add latest commit hash link with build timestamp
Co-authored-by: D <[email protected]>
1 parent 8b6f9db commit 8dace45

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/layouts/HtmlLayout.astro

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
import "../styles/global.css";
33
import { G } from "../lib/globals.js";
4+
import { buildMeta } from "../lib/build-meta.js";
45
56
interface Props {
67
title: string;
@@ -44,6 +45,11 @@ const canonicalUrl = canonical
4445
: new URL(Astro.url.pathname, site).href;
4546
const socialImageUrl = new URL(socialImage, site).href;
4647
const rssUrl = new URL("/rss.xml", site).href;
48+
const commitUrlTemplate = G.CommitUrlTemplate ?? "";
49+
const buildCommitUrl = commitUrlTemplate.replace(
50+
"{{COMMIT}}",
51+
encodeURIComponent(buildMeta.fullHash),
52+
);
4753
---
4854

4955
<html lang={lang} class="min-h-full">
@@ -98,7 +104,8 @@ const rssUrl = new URL("/rss.xml", site).href;
98104

99105
<body
100106
class:list={[
101-
"min-h-full bg-background font-sans leading-6 text-normal",
107+
"min-h-screen bg-background font-sans leading-6 text-normal",
108+
"flex flex-col",
102109
bodyClass,
103110
]}
104111
>
@@ -156,8 +163,19 @@ const rssUrl = new URL("/rss.xml", site).href;
156163
}
157164
</div>
158165
</header>
159-
<div class:list={["pt-10", hasSectionTitleRow && "pt-20"]}>
166+
<div class:list={["pt-10", hasSectionTitleRow && "pt-20", "flex-1"]}>
160167
<slot />
161168
</div>
169+
170+
<footer class="mx-auto w-full max-w-4xl px-3 py-3 text-center lg:px-0">
171+
<a
172+
href={buildCommitUrl}
173+
class="font-mono text-xs text-muted no-underline hover:text-normal-lighter"
174+
target="_blank"
175+
rel="noopener noreferrer"
176+
>
177+
{buildMeta.label}
178+
</a>
179+
</footer>
162180
</body>
163181
</html>

src/lib/build-meta.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { execSync } from "node:child_process";
2+
3+
function getFullHash() {
4+
try {
5+
return execSync("git rev-parse HEAD", {
6+
encoding: "utf8",
7+
stdio: ["ignore", "pipe", "ignore"],
8+
}).trim();
9+
} catch {
10+
return "unknown";
11+
}
12+
}
13+
14+
function pad(value) {
15+
return String(value).padStart(2, "0");
16+
}
17+
18+
function formatBuildStamp(date = new Date()) {
19+
const year = date.getFullYear();
20+
const month = pad(date.getMonth() + 1);
21+
const day = pad(date.getDate());
22+
const hours = pad(date.getHours());
23+
const minutes = pad(date.getMinutes());
24+
const seconds = pad(date.getSeconds());
25+
26+
return `${year}${month}${day}-${hours}:${minutes}:${seconds}`;
27+
}
28+
29+
const fullHash = getFullHash();
30+
const shortHash = fullHash === "unknown" ? fullHash : fullHash.slice(0, 8);
31+
const buildStamp = formatBuildStamp();
32+
const label = `${shortHash}-${buildStamp}`;
33+
34+
export const buildMeta = {
35+
fullHash,
36+
shortHash,
37+
buildStamp,
38+
label,
39+
};

0 commit comments

Comments
 (0)