Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .devcontainer/init_workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down
6 changes: 6 additions & 0 deletions .husky/post-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env sh
set -e

if [ "$3" = "1" ]; then
node scripts/prune-branches.js --only-main
fi
3 changes: 3 additions & 0 deletions .husky/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set -e

node scripts/prune-branches.js --only-main
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ghostclass",
"version": "4.7.3",
"version": "4.7.4",
"private": true,
"engines": {
"node": ">=22.12.0",
Expand Down Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion public/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
62 changes: 62 additions & 0 deletions scripts/__tests__/prune-branches.test.ts
Original file line number Diff line number Diff line change
@@ -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"]);
});
});
200 changes: 200 additions & 0 deletions scripts/prune-branches.js
Original file line number Diff line number Diff line change
@@ -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<string>} [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,
};
Loading