-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mts
executable file
·202 lines (176 loc) · 5.4 KB
/
index.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env npx --yes --package=ts-node -- ts-node-esm --swc
import { question, chalk, path, fs, argv, echo } from "zx";
import { $, ProcessOutput } from "zx/core";
const {
d: debug,
r: removeRemote,
u: removeUnmerged,
h: help,
n: dryRun,
base,
} = argv;
const age = argv.age || "2 weeks";
if (help) {
echo(`cleanup-branches`);
echo(`A cli tool to interactively remove old branches from git.
It automatically removes merged branches, locally and remotely. It prompts for each unmerged branch whether you want to remove it or not.
By default, it is assumed that the main branch that should be merged to is named main, not master. If this is not correct, use the --branch parameter (e.g. --branch=master).
The name and commit hash of deleted branches are printed to stdout and logged to the log file at .local/state/cleanup-branches/log.txt
Usage: cleanup-branches [options]
Options:
-h Show this help message and exit
-r Also remove remote branches
-u Also remove unmerged branches, interactively
--age=<age> Minimum age to remove unmerged branches, e.g. "5 days" or "1 month". Defaults to "2 weeks".
-n Dry-run, do nothing, just print what would be done
-d Verbose debug output, including git commands
--base=<branch> Use "branch" as the merge target to compare with, instead of "main"
`);
echo(`Usage: cleanup-branches [options]`);
process.exit(0);
}
$.verbose = !!debug;
const mergeBase: string = base || "main";
const logDir = path.join($.env.HOME!, ".local/state/cleanup-branches");
const logFile = path.join(logDir, "log.txt");
if (!dryRun) {
await fs.mkdir(logDir, { recursive: true });
const { stdout: pwd } = await $`pwd`;
await fs.appendFile(logFile, `\n${pwd}`);
}
// suppress quoting, it doesn't allow for dynamic commands
const q = $.quote;
$.quote = (v) => v;
const linesToArray = (lines: ProcessOutput) =>
lines.stdout
.split("\n")
.map((b) => b.trim())
.filter((b) => b.length > 0);
const neverDelete = `'^\\*|^\\+| HEAD |^[ ]*(master|main|${mergeBase}|develop|hotfix|temp|[0-9]task)$'`;
const logStdout = (stdout: string) => {
if (!debug) {
const trimmedOut = stdout.trim();
if (trimmedOut.length > 0) {
console.log(trimmedOut);
}
}
};
const tooNew = async (
merged: boolean,
remote: boolean,
branch: string,
age: string
): Promise<boolean> => {
if (merged) {
return false;
}
const { stdout } = await $`git log --no-walk --before="${age}" ${
(remote ? "origin/" : "") + branch
}`;
return stdout.trim().length === 0;
};
const showLog = async (merged: boolean, remote: boolean, branch: string) => {
if (!merged) {
const { stdout } = await $`git log origin/${mergeBase}..${
(remote ? "origin/" : "") + branch
}`;
logStdout(stdout);
}
};
const remoteDeletionLog = async (remote: boolean, branch: string) => {
if (remote) {
const { stdout } = await $`git log -1 --format=%h origin/${branch}`;
const remoteDeleteLog = `Deleted branch origin/${branch} (was ${stdout.trim()}).`;
console.log(remoteDeleteLog);
await fs.appendFile(logFile, remoteDeleteLog + "\n");
}
};
const deleteBranches = async ({
merged,
remote,
ask,
}: {
merged: boolean;
remote: boolean;
ask: boolean;
}) => {
const getBranches = `git branch ${remote ? "-r" : ""} ${
merged ? "--merged" : "--no-merged"
} ${remote ? `origin/${mergeBase}` : mergeBase} ${
remote ? ' | sd origin/ ""' : ""
}`;
const cmd = `${getBranches} | egrep -v ${neverDelete}`;
let branches: string[] = [];
try {
const branchLines = await $`${cmd}`;
branches = linesToArray(branchLines);
} catch (p: any) {
if (p.exitCode !== 1) {
throw p;
}
}
const oldBranches: string[] = [];
for (const branch of branches) {
if (await tooNew(merged, remote, branch, age)) {
continue;
}
oldBranches.push(branch);
}
if (oldBranches.length === 0) {
console.log(`No branches to delete`);
return;
}
const deleteBranch = remote
? "git push origin --delete"
: "git branch " + (merged ? "-d" : "-D");
console.warn("Deleting branches: ", oldBranches);
for (const branch of oldBranches) {
if (await tooNew(merged, remote, branch, age)) {
continue;
}
await showLog(merged, remote, branch);
const shouldDelete = ask
? await question(`delete "${branch}"? [y/N] `)
: "y";
if (shouldDelete && shouldDelete[0].toLowerCase() === "y") {
if (dryRun) {
console.log(`Would delete ${branch}`);
} else {
await remoteDeletionLog(remote, branch);
const { stdout } = await $`${deleteBranch.split(" ")} ${branch}`;
logStdout(stdout);
await fs.appendFile(logFile, stdout);
}
}
}
};
console.log("-----------------> Delete local merged");
await deleteBranches({
merged: true,
remote: false,
ask: false,
});
if (removeRemote) {
console.log(chalk.bold("-----------------> Delete remote merged"));
await deleteBranches({
merged: true,
remote: true,
ask: false,
});
}
if (removeUnmerged) {
console.log(chalk.yellow("-----------------> Delete local unmerged"));
await deleteBranches({
merged: false,
remote: false,
ask: true,
});
if (removeRemote) {
console.log(chalk.yellow.bold("-----------------> Delete remote unmerged"));
await deleteBranches({
merged: false,
remote: true,
ask: true,
});
}
}