-
Notifications
You must be signed in to change notification settings - Fork 3
/
sweep.js
129 lines (112 loc) · 3.6 KB
/
sweep.js
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
import fs from 'fs';
import path from 'path';
import moment from 'moment';
import NodeGit from 'nodegit';
function getConfiguredIgnoresIfExist(repoPath) {
return new Promise((resolve) => {
fs.readFile(path.join(repoPath, '.gitsweepignore'), 'utf8', (err, data) => {
if (err) {
return resolve([]);
}
resolve(data.split('\n'));
})
});
}
function isIgnored(ref, ignoreList) {
return ignoreList.includes(ref.replace(/^refs\/remotes\//, ''));
}
function getCutoffMoment(age) {
const [, year, month, day] = age.match(/(?:(\d+)y)?(?:(\d+)m)?(?:(\d+)d)?/);
return moment()
.subtract(year, 'years')
.subtract(month, 'months')
.subtract(day, 'days');
}
async function sweep({
path,
remote = 'origin',
preview = false,
ignore = 'origin/master',
age = '1m',
password
}) {
if (!path) {
throw new Error('Path is required');
}
try {
const cutoffMoment = age ? getCutoffMoment(age) : null;
const ignoreList = await getConfiguredIgnoresIfExist(path);
ignoreList.push(...ignore);
const repo = await NodeGit.Repository.open(path);
const authTrials = {
agent: false,
userpassPlaintext: false
};
await repo.fetch(remote, {
callbacks: {
certificateCheck: function() { return 1; },
credentials: function(url, username) {
// Do not try ssh-agent if password is specified
if (password) {
if (!authTrials.userpassPlaintext) {
authTrials.userpassPlaintext = true;
return NodeGit.Cred.userpassPlaintextNew(username, password);
}
} else if (!authTrials.agent) {
authTrials.agent = true;
return NodeGit.Cred.sshKeyFromAgent(username);
}
}
},
prune: NodeGit.Fetch.PRUNE.GIT_FETCH_PRUNE
});
const refNames = await repo.getReferenceNames(NodeGit.Reference.TYPE.LISTALL);
const sweepRefs = [];
for (const ref of refNames) {
if (ref.startsWith(`refs/remotes/${remote}`) && !isIgnored(ref, ignoreList)) {
const commit = await repo.getReferenceCommit(ref);
const commitMoment = moment(commit.date());
if (!cutoffMoment || commitMoment.isBefore(cutoffMoment)) {
console.log(`- ${ref}\t${commitMoment.fromNow()}`);
sweepRefs.push(`:${ref.replace(`remotes/${remote}`, 'heads')}`);
}
}
}
if (sweepRefs.length < 1) {
console.log('No matching remote branch to sweep');
return;
} else if (!!cutoffMoment) {
console.log(`${sweepRefs.length} branch found with last commit before ${cutoffMoment.toString()}`);
} else {
console.log(`${sweepRefs.length} branch found`);
}
if (!preview) {
const authTrials = {
agent: false,
userpassPlaintext: false
};
const theRemote = await repo.getRemote(remote);
await theRemote.push(sweepRefs, {
callbacks: {
certificateCheck: function() { return 1; },
credentials: function(url, username) {
// Do not try ssh-agent if password is specified
if (password) {
if (!authTrials.userpassPlaintext) {
authTrials.userpassPlaintext = true;
return NodeGit.Cred.userpassPlaintextNew(username, password);
}
} else if (!authTrials.agent) {
authTrials.agent = true;
return NodeGit.Cred.sshKeyFromAgent(username);
}
}
}
});
console.log(`${sweepRefs.length} branch removed`);
}
} catch (e) {
console.error(e.message);
}
}
export default sweep;