commit.ts
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user