commit.ts

This commit is contained in:
2026-02-07 01:31:25 +08:00
parent 7d3bbfbc26
commit 0a52ccc601
3 changed files with 146 additions and 144 deletions

139
libraries/deno-git-mod.ts Normal file
View File

@@ -0,0 +1,139 @@
import {execCommand} from "https://script.hatter.ink/@29/deno-commons-mod.ts";
import {existsSync} from "node:fs";
export async function getGitRemoteRev(): Promise<string> {
const currentBranch = await getGitCurrentBranch();
const lsGitRemoteOrigin = await execCommand("git", [
"ls-remote",
"origin",
currentBranch,
]);
lsGitRemoteOrigin.assertSuccess();
return lsGitRemoteOrigin.stdout.trim().split(/\s+/)[0].trim();
}
export async function getGitCurrentBranch(): Promise<string> {
const gitCurrentBranch = await execCommand("git", [
"branch",
"--show-current",
]);
gitCurrentBranch.assertSuccess();
return gitCurrentBranch.stdout.trim();
}
export async function getGitLocalRev(): Promise<string> {
const gitLocalRev = await execCommand("git", [
"rev-parse",
"HEAD",
]);
gitLocalRev.assertSuccess();
return gitLocalRev.stdout.trim();
}
export class GitStatusResult {
newfile: string[];
modified: string[];
deleted: string[];
untracked: string[];
constructor(
newfile: string[],
modified: string[],
deleted: string[],
untracked: string[],
) {
this.newfile = newfile;
this.modified = modified;
this.deleted = deleted;
this.untracked = untracked;
}
}
// On branch main
// Your branch is up to date with 'origin/main'.
//
// nothing to commit, working tree clean
// ----------------------------------------------------------------------
// On branch main
// Your branch is up to date with 'origin/main'.
//
// Changes to be committed:
// (use "git restore --staged <file>..." to unstage)
// new file: single-scripts/commit.ts
// ----------------------------------------------------------------------
// On branch main
// Your branch is up to date with 'origin/main'.
//
// Changes not staged for commit:
// (use "git add/rm <file>..." to update what will be committed)
// (use "git restore <file>..." to discard changes in working directory)
// modified: README.md
// deleted: script-config.json
//
// Untracked files:
// (use "git add <file>..." to include in what will be committed)
// single-scripts/commit.ts
//
// no changes added to commit (use "git add" and/or "git commit -a")
export async function getGitStatus(): Promise<GitStatusResult | null> {
const newfile: string[] = [];
const modified: string[] = [];
const deleted: string[] = [];
const untracked: string[] = [];
const gitStatus = await execCommand("git", ["status"]);
gitStatus.assertSuccess();
const gitStatusStdout = gitStatus.stdout.trim();
if (gitStatusStdout.includes("nothing to commit, working tree clean")) {
return null;
}
let inChangesNotStaged = false;
let inUntrackedFiles = false;
gitStatusStdout.split("\n").forEach((line) => {
if (
line.startsWith("Changes to be committed:") ||
line.startsWith("Changes not staged for commit:")
) {
inChangesNotStaged = true;
inUntrackedFiles = false;
return;
}
if (line.startsWith("Untracked files:")) {
inChangesNotStaged = false;
inUntrackedFiles = true;
return;
}
if (inChangesNotStaged) {
const modifiedOrDeletedLine = line.trim();
if (modifiedOrDeletedLine.startsWith("new file:")) {
const newFile = modifiedOrDeletedLine.substring(
"new file:".length,
).trim();
newfile.push(newFile);
} else if (modifiedOrDeletedLine.startsWith("modified:")) {
const modifiedFile = modifiedOrDeletedLine.substring(
"modified:".length,
).trim();
modified.push(modifiedFile);
} else if (modifiedOrDeletedLine.startsWith("deleted:")) {
const deletedFile = modifiedOrDeletedLine.substring(
"deleted:".length,
).trim();
deleted.push(deletedFile);
}
}
if (inUntrackedFiles) {
const newFile = line.trim();
if (newFile && existsSync(newFile)) {
untracked.push(newFile);
}
}
});
if (
newfile.length === 0 && modified.length === 0 && deleted.length === 0 &&
untracked.length === 0
) {
// WILL THIS HAPPEN?
return null;
}
return new GitStatusResult(newfile, modified, deleted, untracked);
}

View File

@@ -53,12 +53,12 @@
},
"commit.ts": {
"script_name": "commit.ts",
"script_length": 7147,
"script_sha256": "c5e714f35183c8a517c2fedf9979b12ea6fd281d5e5a083d10b690ce919054b9",
"script_length": 2755,
"script_sha256": "96e18cf15582a3ec5a4b8890c5384f803d9a58b033d391ac17cfed14993bfa17",
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/commit.ts",
"single_script_file": true,
"publish_time": 1769318306585,
"update_time": 1770398679122
"update_time": 1770399079953
},
"decode-uri-component.ts": {
"script_name": "decode-uri-component.ts",

View File

@@ -1,146 +1,9 @@
#!/usr/bin/env runts -- --allow-import --allow-run --allow-read
import {existsSync} from "node:fs";
import {createInterface} from "node:readline/promises";
import {stdin, stdout} from "node:process";
import {execCommand, execCommandShell, log, ProcessBar, term,} from "https://script.hatter.ink/@29/deno-commons-mod.ts";
async function getGitRemoteRev(): Promise<string> {
const currentBranch = await getGitCurrentBranch();
const lsGitRemoteOrigin = await execCommand("git", [
"ls-remote",
"origin",
currentBranch,
]);
lsGitRemoteOrigin.assertSuccess();
return lsGitRemoteOrigin.stdout.trim().split(/\s+/)[0].trim();
}
async function getGitCurrentBranch(): Promise<string> {
const gitCurrentBranch = await execCommand("git", [
"branch",
"--show-current",
]);
gitCurrentBranch.assertSuccess();
return gitCurrentBranch.stdout.trim();
}
async function getGitLocalRev(): Promise<string> {
const gitLocalRev = await execCommand("git", [
"rev-parse",
"HEAD",
]);
gitLocalRev.assertSuccess();
return gitLocalRev.stdout.trim();
}
class GitStatusResult {
newfile: string[];
modified: string[];
deleted: string[];
untracked: string[];
constructor(
newfile: string[],
modified: string[],
deleted: string[],
untracked: string[],
) {
this.newfile = newfile;
this.modified = modified;
this.deleted = deleted;
this.untracked = untracked;
}
}
// On branch main
// Your branch is up to date with 'origin/main'.
//
// nothing to commit, working tree clean
// ----------------------------------------------------------------------
// On branch main
// Your branch is up to date with 'origin/main'.
//
// Changes to be committed:
// (use "git restore --staged <file>..." to unstage)
// new file: single-scripts/commit.ts
// ----------------------------------------------------------------------
// On branch main
// Your branch is up to date with 'origin/main'.
//
// Changes not staged for commit:
// (use "git add/rm <file>..." to update what will be committed)
// (use "git restore <file>..." to discard changes in working directory)
// modified: README.md
// deleted: script-config.json
//
// Untracked files:
// (use "git add <file>..." to include in what will be committed)
// single-scripts/commit.ts
//
// no changes added to commit (use "git add" and/or "git commit -a")
async function getGitStatus(): Promise<GitStatusResult | null> {
const newfile: string[] = [];
const modified: string[] = [];
const deleted: string[] = [];
const untracked: string[] = [];
const gitStatus = await execCommand("git", ["status"]);
gitStatus.assertSuccess();
const gitStatusStdout = gitStatus.stdout.trim();
if (gitStatusStdout.includes("nothing to commit, working tree clean")) {
return null;
}
let inChangesNotStaged = false;
let inUntrackedFiles = false;
gitStatusStdout.split("\n").forEach((line) => {
if (
line.startsWith("Changes to be committed:") ||
line.startsWith("Changes not staged for commit:")
) {
inChangesNotStaged = true;
inUntrackedFiles = false;
return;
}
if (line.startsWith("Untracked files:")) {
inChangesNotStaged = false;
inUntrackedFiles = true;
return;
}
if (inChangesNotStaged) {
const modifiedOrDeletedLine = line.trim();
if (modifiedOrDeletedLine.startsWith("new file:")) {
const newFile = modifiedOrDeletedLine.substring(
"new file:".length,
).trim();
newfile.push(newFile);
} else if (modifiedOrDeletedLine.startsWith("modified:")) {
const modifiedFile = modifiedOrDeletedLine.substring(
"modified:".length,
).trim();
modified.push(modifiedFile);
} else if (modifiedOrDeletedLine.startsWith("deleted:")) {
const deletedFile = modifiedOrDeletedLine.substring(
"deleted:".length,
).trim();
deleted.push(deletedFile);
}
}
if (inUntrackedFiles) {
const newFile = line.trim();
if (newFile && existsSync(newFile)) {
untracked.push(newFile);
}
}
});
if (
newfile.length === 0 && modified.length === 0 && deleted.length === 0 &&
untracked.length === 0
) {
// WILL THIS HAPPEN?
return null;
}
return new GitStatusResult(newfile, modified, deleted, untracked);
}
import {execCommandShell, log, ProcessBar, term,} from "https://script.hatter.ink/@29/deno-commons-mod.ts";
import {getGitLocalRev, getGitRemoteRev, getGitStatus,} from "https://script.hatter.ink/@0/deno-git-mod.ts";
async function checkRev(): Promise<boolean> {
const localRev = await getGitLocalRev();
@@ -213,5 +76,5 @@ main().catch((err) => {
process.exit(0);
}).then(() => process.exit(0));
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260207T012415+08:00.MEUCIQDk3pTgBJpQ1OAiG8n0
// VJ5ecHW3eAOyuiKwZZA+gXdJMQIgQVL+HR3hqBW0wYW/yQ1yv8D9sz5KBrGl8znrT78Z0yk=
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260207T013110+08:00.MEYCIQDXoa6SfDaJ5KsDplYF
// zBZXJkEuC9HndsMQrmLMFTAXdQIhALMYnHVZNFOTYo6+COVCOmjnMPayKHNHlgfEa0siKPvb