diff --git a/script-meta-v2.json b/script-meta-v2.json index 55bd2f7..bc3ba17 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -96,6 +96,15 @@ "publish_time": 1769010420927, "update_time": 1769097609467 }, + "git-check.ts": { + "script_name": "git-check.ts", + "script_length": 2773, + "script_sha256": "272c74107d4d92238061f78bcda49357fa2f9032939d39289fa223e58a41059f", + "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/git-check.ts", + "single_script_file": true, + "publish_time": 1770541896791, + "update_time": 1770541896791 + }, "helloworld-bun.ts": { "script_name": "helloworld-bun.ts", "script_length": 237, diff --git a/single-scripts/git-check.ts b/single-scripts/git-check.ts new file mode 100755 index 0000000..4da85af --- /dev/null +++ b/single-scripts/git-check.ts @@ -0,0 +1,92 @@ +#!/usr/bin/env runts -- --allow-import --allow-run --allow-read + +import { + execCommand, + log, + ProcessBar, +} from "https://script.hatter.ink/@29/deno-commons-mod.ts"; +import { + getGitLocalRev, + getGitRemoteRev, +} from "https://script.hatter.ink/@0/deno-git-mod.ts"; +import { + formatHumanTime, + readFileToString, + writeStringToFile, +} from "../libraries/deno-commons-mod.ts"; + +async function sha256OfString(input: string): Promise { + const data = new TextEncoder().encode(input); + const hashBuffer = await crypto.subtle.digest("SHA-256", data); + const hashArray = Array.from(new Uint8Array(hashBuffer)); + return hashArray.map((b) => b.toString(16).padStart(2, "0")).join( + "", + ); +} +async function getPwd(): Promise { + const pwd = await execCommand("pwd"); + pwd.assertSuccess(); + return pwd.stdout.trim(); +} + +interface GitCheckCache { + path: string; + lastCheckTime: number; +} + +async function main() { + const pwd = await getPwd(); + const pwdHash = await sha256OfString(pwd); + const gitCheckCacheFile = `~/.cache/git-check-${ + pwdHash.substring(0, 20) + }.json`; + log.debug(`Check git cache file: ${gitCheckCacheFile}`); + const gitCheckCacheContent = await readFileToString(gitCheckCacheFile); + const gitCheckCache = gitCheckCacheContent + ? JSON.parse(gitCheckCacheContent) as GitCheckCache + : null; + if (gitCheckCache) { + const timeBefore = Date.now() - gitCheckCache.lastCheckTime; + if (timeBefore < 60 * 60 * 1000) { + log.info( + `Last check at ${new Date( + gitCheckCache.lastCheckTime, + )}, in ${ + formatHumanTime(timeBefore) + }, skip check git remote rev`, + ); + return; + } + } + + const localRev = await getGitLocalRev(); + const remoteRev = await new ProcessBar("Checking rev").call( + async (): Promise => { + return await getGitRemoteRev(); + }, + ); + if (localRev === remoteRev) { + log.success(`Check rev successfully, rev: ${localRev}`); + await writeStringToFile( + gitCheckCacheFile, + JSON.stringify( + { + path: pwd, + lastCheckTime: Date.now(), + } as GitCheckCache, + null, + 2, + ), + ); + } else { + log.error( + `Check rev failed, local rev: ${localRev} vs remote rev: ${remoteRev}`, + ); + Deno.exit(1); + } +} + +await main(); + +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260208T171132+08:00.MEQCIE+nwuOV0aljgeKyGzFE +// eSZrNDvMwRpta2s+C26F0PKHAiBdgS0xvwSjiVrl+82PtaSVsDnxJ8/AsAfb3D1t4fFaeA==