version badge: display commit hashes GitHub-style (7 chars)

Staging tags are "staging-<full 40-char sha>" — shorten any embedded
full hash for display only; the corresponding-source link keeps the
full sha, release tags pass through untouched.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011gJ3M1RpeZLeNUUj8jKC4h
This commit is contained in:
Gergő Törcsvári 2026-08-20 11:49:56 +02:00
commit 745b8f413a
No known key found for this signature in database
GPG key ID: 8E75F2CDE64E5322
2 changed files with 30 additions and 1 deletions

View file

@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";
import { shortBuildTag } from "./VersionBadge";
describe("shortBuildTag (version badge display)", () => {
it("shortens a staging tag's full sha to 7 chars", () => {
expect(shortBuildTag("staging-75bb0d7e4f2a9c1b8d3e6f0a2b4c6d8e0f1a2b3c")).toBe(
"staging-75bb0d7",
);
});
it("shortens a bare full sha", () => {
expect(shortBuildTag("75bb0d7e4f2a9c1b8d3e6f0a2b4c6d8e0f1a2b3c")).toBe("75bb0d7");
});
it("leaves release tags and dev untouched", () => {
expect(shortBuildTag("2.7.7")).toBe("2.7.7");
expect(shortBuildTag("dev")).toBe("dev");
// Short hex-ish fragments (< 12 chars) are not treated as hashes.
expect(shortBuildTag("demo-local")).toBe("demo-local");
expect(shortBuildTag("beta-abc123")).toBe("beta-abc123");
});
});

View file

@ -10,8 +10,15 @@ import { APP_GIT_SHA, APP_TAG, LANDING_URL, REPO_URL } from "@/lib/config";
* loaded editor. `fixed` keeps it viewport-anchored on every route; z-20 sits
* under the editor's boot overlay (z-30) so it's hidden until the tool is up.
*/
/** Display form of a build tag: any embedded full commit hash shortens to the
* GitHub-style 7 chars ("staging-<40 hex>" "staging-abc1234"); release tags
* ("2.7.7") pass through untouched. Display only links keep the full sha. */
export function shortBuildTag(tag: string): string {
return tag.replace(/\b[0-9a-f]{12,40}\b/i, (h) => h.slice(0, 7));
}
export function VersionBadge() {
const tag = APP_TAG ?? "dev";
const tag = shortBuildTag(APP_TAG ?? "dev");
const versionUrl = APP_GIT_SHA
? `${REPO_URL}/commit/${APP_GIT_SHA}`
: APP_TAG