From c8d350ccb35d4ccc82f6f12d2a766b668dbc12e5 Mon Sep 17 00:00:00 2001 From: Devanarayanan Date: Sat, 5 Dec 2026 19:10:45 +0000 Subject: [PATCH 1/2] chore: add branch pruning scripts and update workspace initialization --- .devcontainer/init_workspace.sh | 3 +- .husky/post-checkout | 6 + .husky/post-merge | 3 + package.json | 1 + scripts/__tests__/prune-branches.test.ts | 62 +++++++ scripts/prune-branches.js | 200 +++++++++++++++++++++++ 6 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 .husky/post-merge create mode 100644 scripts/__tests__/prune-branches.test.ts create mode 100644 scripts/prune-branches.js diff --git a/.devcontainer/init_workspace.sh b/.devcontainer/init_workspace.sh index 40cde052..6232ef70 100644 --- a/.devcontainer/init_workspace.sh +++ b/.devcontainer/init_workspace.sh @@ -19,10 +19,11 @@ dev_init_common "$TARGET_DIR" \ "$TARGET_DIR/mobile/.dart_tool" \ "$TARGET_DIR/mobile/build" -# --- 5. GIT SAFE DIRECTORY & PUSH DEFAULTS --- +# --- 5. GIT SAFE DIRECTORY & PUSH/FETCH DEFAULTS --- git config --global --add safe.directory "$TARGET_DIR" 2>/dev/null || true git config --global --add safe.directory /workspaces/GhostClass 2>/dev/null || true git config --global push.autoSetupRemote true 2>/dev/null || true +git config --global fetch.prune true 2>/dev/null || true if [ -f "$TARGET_DIR/package.json" ] && [ -f "$TARGET_DIR/package-lock.json" ]; then echo "📦 Synchronizing Node/NPM dependencies..." diff --git a/.husky/post-checkout b/.husky/post-checkout index e69de29b..f67a983c 100644 --- a/.husky/post-checkout +++ b/.husky/post-checkout @@ -0,0 +1,6 @@ +#!/usr/bin/env sh +set -e + +if [ "$3" = "1" ]; then + node scripts/prune-branches.js --only-main +fi diff --git a/.husky/post-merge b/.husky/post-merge new file mode 100644 index 00000000..6220530b --- /dev/null +++ b/.husky/post-merge @@ -0,0 +1,3 @@ +set -e + +node scripts/prune-branches.js --only-main diff --git a/package.json b/package.json index d456ba37..6a35c37c 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "generate:firebase-json": "node scripts/generate-firebase-json.js", "version:bump": "node scripts/bump-version.js", "version:sync": "node scripts/sync-version.js", + "branches:prune": "node scripts/prune-branches.js", "prepare": "husky" }, "browserslist": [ diff --git a/scripts/__tests__/prune-branches.test.ts b/scripts/__tests__/prune-branches.test.ts new file mode 100644 index 00000000..9eaf61bd --- /dev/null +++ b/scripts/__tests__/prune-branches.test.ts @@ -0,0 +1,62 @@ +/** @vitest-environment node */ +import { describe, expect, it } from "vitest"; + +const { + parseBranchRefs, + selectPrunableBranches, +} = require("../prune-branches"); + +describe("prune-branches", () => { + it("parses git for-each-ref branch lines", () => { + const parsed = parseBranchRefs( + ["main|", "fix/session|[gone]", "feat/ui|[ahead 1]", "4.7.2|"].join("\n"), + ); + + expect(parsed).toEqual([ + { name: "main", track: "" }, + { name: "fix/session", track: "[gone]" }, + { name: "feat/ui", track: "[ahead 1]" }, + { name: "4.7.2", track: "" }, + ]); + }); + + it("selects [gone] branches while protecting main and current branch", () => { + const branches = [ + { name: "main", track: "[gone]" }, + { name: "master", track: "[gone]" }, + { name: "feat/current", track: "[gone]" }, + { name: "fix/merged-pr", track: "[gone]" }, + { name: "feat/active", track: "[ahead 2]" }, + { name: "4.7.2", track: "" }, + ]; + + const result = selectPrunableBranches({ + branches, + currentBranch: "feat/current", + mergedBranches: new Set(["4.7.2", "fix/merged-pr", "main"]), + includeMerged: false, + }); + + expect(result.gone).toEqual(["fix/merged-pr"]); + expect(result.merged).toEqual([]); + }); + + it("includes merged non-gone branches only when includeMerged is true", () => { + const branches = [ + { name: "main", track: "" }, + { name: "fix/merged-pr", track: "[gone]" }, + { name: "4.7.2", track: "" }, + { name: "feat/wip", track: "" }, + ]; + + const result = selectPrunableBranches({ + branches, + currentBranch: "main", + mergedBranches: new Set(["main", "fix/merged-pr", "4.7.2"]), + includeMerged: true, + }); + + expect(result.gone).toEqual(["fix/merged-pr"]); + expect(result.merged).toEqual(["4.7.2"]); + }); +}); diff --git a/scripts/prune-branches.js b/scripts/prune-branches.js new file mode 100644 index 00000000..7bab4a0a --- /dev/null +++ b/scripts/prune-branches.js @@ -0,0 +1,200 @@ +#!/usr/bin/env node + +const { execFileSync } = require("node:child_process"); +const path = require("node:path"); + +const ROOT_DIR = path.join(__dirname, ".."); +const PROTECTED_BRANCHES = new Set(["main", "master", "develop"]); + +function tryGit(args, rootDir = ROOT_DIR) { + try { + return execFileSync("git", args, { + cwd: rootDir, + encoding: "utf8", + stdio: ["ignore", "pipe", "pipe"], + }).trim(); + } catch { + return null; + } +} + +/** + * Parses `git for-each-ref --format="%(refname:short)|%(upstream:track)" refs/heads` output. + * @param {string} raw + * @returns {Array<{ name: string, track: string }>} + */ +function parseBranchRefs(raw) { + return raw + .split(/\r?\n/) + .map((line) => line.trim()) + .filter(Boolean) + .map((line) => { + const idx = line.indexOf("|"); + if (idx === -1) return { name: line, track: "" }; + return { + name: line.slice(0, idx).trim(), + track: line.slice(idx + 1).trim(), + }; + }); +} + +/** + * Selects local branches eligible for cleanup. + * @param {object} params + * @param {Array<{ name: string, track: string }>} params.branches + * @param {string} params.currentBranch + * @param {Set} [params.mergedBranches] + * @param {boolean} [params.includeMerged] + * @returns {{ gone: string[], merged: string[] }} + */ +function selectPrunableBranches({ + branches, + currentBranch, + mergedBranches = new Set(), + includeMerged = false, +}) { + const gone = []; + const merged = []; + const seen = new Set(); + + for (const { name, track } of branches) { + if (!name || PROTECTED_BRANCHES.has(name) || name === currentBranch) { + continue; + } + if (track === "[gone]") { + gone.push(name); + seen.add(name); + } + } + + if (includeMerged) { + for (const { name } of branches) { + if ( + !name || + seen.has(name) || + PROTECTED_BRANCHES.has(name) || + name === currentBranch + ) { + continue; + } + if (mergedBranches.has(name)) { + merged.push(name); + } + } + } + + return { gone, merged }; +} + +function pruneBranches(options = {}) { + const rootDir = options.rootDir || ROOT_DIR; + const dryRun = Boolean(options.dryRun); + const force = Boolean(options.force); + const includeMerged = Boolean(options.includeMerged); + const onlyMain = Boolean(options.onlyMain); + const quiet = Boolean(options.quiet); + + const currentBranch = tryGit(["branch", "--show-current"], rootDir) || ""; + if (onlyMain && currentBranch !== "main") { + return { deleted: [], skipped: [], dryRun }; + } + + const refsRaw = tryGit( + [ + "for-each-ref", + "--format=%(refname:short)|%(upstream:track)", + "refs/heads", + ], + rootDir, + ); + if (!refsRaw) { + return { deleted: [], skipped: [], dryRun }; + } + + const branches = parseBranchRefs(refsRaw); + const mergedBranches = new Set(); + + if (includeMerged) { + const baseBranch = tryGit(["rev-parse", "--verify", "main"], rootDir) + ? "main" + : "HEAD"; + const mergedRaw = tryGit( + ["branch", "--merged", baseBranch, "--format=%(refname:short)"], + rootDir, + ); + if (mergedRaw) { + for (const b of mergedRaw.split(/\r?\n/)) { + const name = b.trim(); + if (name) mergedBranches.add(name); + } + } + } + + const { gone, merged } = selectPrunableBranches({ + branches, + currentBranch, + mergedBranches, + includeMerged, + }); + + const candidates = [ + ...gone.map((name) => ({ name, reason: "remote gone" })), + ...merged.map((name) => ({ name, reason: "merged into main" })), + ]; + + const deleted = []; + const skipped = []; + + for (const { name, reason } of candidates) { + if (dryRun) { + if (!quiet) { + console.log(`[dry-run] Would delete branch "${name}" (${reason})`); + } + deleted.push(name); + continue; + } + + const deleteFlag = force && reason === "remote gone" ? "-D" : "-d"; + try { + execFileSync("git", ["branch", deleteFlag, name], { + cwd: rootDir, + stdio: ["ignore", "pipe", "pipe"], + }); + deleted.push(name); + if (!quiet) { + console.log(`🧹 Deleted local branch "${name}" (${reason})`); + } + } catch { + skipped.push(name); + if (!quiet) { + console.log( + `⚠️ Kept "${name}" (${reason}, has unmerged commits; run 'git branch -D ${name}' if squash-merged)`, + ); + } + } + } + + return { deleted, skipped, dryRun }; +} + +function parseCliArgs(argv) { + return { + dryRun: argv.includes("--dry-run"), + force: argv.includes("--force"), + includeMerged: argv.includes("--merged"), + onlyMain: argv.includes("--only-main"), + quiet: argv.includes("--quiet") || argv.includes("-q"), + }; +} + +if (require.main === module) { + const opts = parseCliArgs(process.argv.slice(2)); + pruneBranches(opts); +} + +module.exports = { + PROTECTED_BRANCHES, + parseBranchRefs, + selectPrunableBranches, + pruneBranches, +}; From 9a4339eb2c50b247c6c42679d7d850c880434714 Mon Sep 17 00:00:00 2001 From: Devanarayanan Date: Sat, 5 Dec 2026 19:11:03 +0000 Subject: [PATCH 2/2] chore(version): web 4.7.4 --- package-lock.json | 4 ++-- package.json | 2 +- public/openapi/openapi.yaml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index fae1e800..f66c13e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ghostclass", - "version": "4.7.3", + "version": "4.7.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ghostclass", - "version": "4.7.3", + "version": "4.7.4", "dependencies": { "@hookform/resolvers": "^7.0.6", "@radix-ui/react-alert-dialog": "^1.1.23", diff --git a/package.json b/package.json index 6a35c37c..52267bde 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ghostclass", - "version": "4.7.3", + "version": "4.7.4", "private": true, "engines": { "node": ">=22.12.0", diff --git a/public/openapi/openapi.yaml b/public/openapi/openapi.yaml index a45a4644..e25299c2 100644 --- a/public/openapi/openapi.yaml +++ b/public/openapi/openapi.yaml @@ -6,7 +6,7 @@ openapi: 3.1.0 info: title: GhostClass API - version: 4.7.3 + version: 4.7.4 description: | **GhostClass API** provides endpoints for authentication, profile synchronization, attendance integrations with EzyGo, telemetry, and build provenance.