Skip to content

Commit 0817321

Browse files
committed
feat: add clean-branch script to automatically remove stale git branches
1 parent b250c00 commit 0817321

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"clean": "git clean -xdf node_modules",
4343
"clean-cache": "node scripts/clean-with-deps.js clean-cache",
4444
"clean-hard": "node scripts/clean-hard.js --no-install",
45-
"clean-reinstall": "node scripts/clean-hard.js && node scripts/link-vercel.js",
45+
"clean-reinstall": "node scripts/clean-hard.js && node scripts/clean-branch.js && node scripts/link-vercel.js",
46+
"clean-branch": "node scripts/clean-branch.js",
4647
"dedupe": "pnpm dedupe",
4748
"prepare": "husky",
4849
"// Compliance": "",

scripts/clean-branch.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env node
2+
/**
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
const { execSync } = require("child_process");
7+
8+
try {
9+
console.log("🔄 Fetching and pruning remote branches...");
10+
execSync("git fetch -p", { stdio: "inherit" });
11+
12+
console.log("🔍 Checking for gone branches...");
13+
const output = execSync("git branch -vv", { encoding: "utf8" });
14+
15+
const goneBranches = output
16+
.split("\n")
17+
.filter((line) => line.includes(": gone]"))
18+
.map((line) => line.trim().split(" ")[0]);
19+
20+
if (goneBranches.length === 0) {
21+
console.log("✅ No gone branches found.");
22+
process.exit(0);
23+
}
24+
25+
goneBranches.forEach((branch) => {
26+
console.log(`🗑️ Deleting local branch: ${branch}`);
27+
execSync(`git branch -D ${branch}`, { stdio: "inherit" });
28+
});
29+
30+
console.log("✅ Cleanup complete.");
31+
} catch (error) {
32+
console.error("❌ Error during cleanup:", error.message);
33+
process.exit(1);
34+
}

0 commit comments

Comments
 (0)