forked from autofix-dev/autofix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofix.js
158 lines (131 loc) · 5.06 KB
/
autofix.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
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
// Copyright © 2018 Jan Keromnes.
// The following code is covered by the MIT license.
const fs = require('fs');
const minimist = require('minimist');
const exec = require('./lib/exec');
const argv = minimist(process.argv.slice(2));
// Parse tiers (e.g. --tiers=0,1,2 gives [0, 1, 2]).
const tiers = String(argv.tiers || 0).split(',').map(tier => parseInt(tier, 10)).sort();
// Detect and register available fixer modules.
const fixers = [ [], [], [], [] ];
console.log(`Registering fixer modules`);
Promise.all(fs.readdirSync(`${__dirname}/fixers`).map(path => Object.assign(require(`${__dirname}/fixers/${path}`), {path})).map(async (fixer) => {
try {
if (argv.verbose) {
console.log(` Registering ${fixer.path}...`);
}
await fixer.register(fixers);
} catch (error) {
// If a fixer fails to register itself, log the error but don't exit.
console.error(` Failed to register fixer module ${fixer.path}`, argv.verbose ? error : '(use --verbose to see error)');
}
})).then(async () => {
console.log('Running fixers');
// Ensure the current Git status is clean.
const status = await exec('git status --porcelain', true);
if (status.trim().length > 0) {
throw `You have uncommitted changes. Aborting!\n${status}`;
}
// Try to detect the current Git branch.
let baseBranch = null;
try {
baseBranch = await exec('git branch | grep \\* | cut -d " " -f2', true);
baseBranch = baseBranch.trim();
} catch (error) {
if (argv.branches || argv.push) {
// Can't create branches or push without a current Git branch!
throw error;
}
}
// Determine whether any commits should be pushed to a remote Git repository.
let pushRemote = null;
if (argv.push) {
// If --push=myremote was passed, push to that remote.
pushRemote = argv.push;
} else if (argv['pull-request']) {
// If --push=myremote was not passed, but --pull-request was passed, push to 'origin' by default.
pushRemote = 'origin';
}
// Execute all fixers by enabled tier.
for (const tier of tiers) {
if (!Array.isArray(fixers[tier])) {
// This is not a valid tier.
const availableTiers = [...new Array(fixers.length).keys()]; // [0, 1, 2, ...]
console.error(`Unknown tier: ${tier} (available tiers: ${availableTiers})`);
continue;
}
const fixersCount = fixers[tier].length;
console.log(`Tier ${tier} has ${fixersCount} fixer${fixersCount === 1 ? '' : 's'}:`);
for (const fixer of fixers[tier]) {
console.log(`Running fixer "${fixer.id}"`);
const fixBranch = argv.branches ? `autofix-${fixer.id}${argv['branch-suffix'] ? '-' + argv['branch-suffix'] : ''}` : baseBranch;
if (argv.branches) {
// If --branches was passed, create a dedicated branch for this fixer.
await exec(`git checkout -b ${fixBranch} ${baseBranch} 2>&1`);
}
// Try to run the fixer's command.
try {
await exec(fixer.cmd);
} catch (error) {
console.error(`Failed to run fixer ${fixer.id}: ${fixer.cmd}`, error);
}
// Attempt to commit any changes (will fail if there is no change).
let committed = false;
try {
await exec(`git commit -a${argv.signoff ? 's' : ''}m "Autofix: ${fixer.id}"`);
committed = true;
if (!argv.dry) {
console.log(` Fixes committed!`);
}
} catch (error) {
if (argv.verbose) {
console.error(error);
}
console.log(' No fixes to commit')
}
if (committed && pushRemote) {
// If fixes were committed, and --push=myremote or --pull-request were passed, push to the appropriate remote.
await exec(`git push ${pushRemote} ${fixBranch} 2>&1`);
if (argv['pull-request']) {
// If --pull-request was passed, open a Pull Request from the pushed branch to the upstream repository's default branch.
await exec(`hub pull-request --head "${pushRemote}:${fixBranch}" --no-edit`);
}
}
if (argv.branches) {
// If --branches was passed, return to the original Git branch.
await exec(`git checkout ${baseBranch} 2>&1`);
// Also return any Git submodules to their original state.
await exec(`git submodule update --force`);
if (!committed) {
// If no fixes were committed, delete the dedicated branch again.
await exec(`git branch -D ${fixBranch}`)
}
}
}
}
}).catch(error => {
console.error(error);
process.exit(1);
});
// Tier 0
'find . -not -iwholename "*.git*" -type f -print0 | xargs -0 perl -pi -e "s/\\s+$//"'
'./mach lint .'
'./mach static-analysis check .'
'performance-faster-string-find'
'git submodule foreach git fetch && git submodule update --remote'
// Tier 1
'codespell -w 2>/dev/null'
// Tier 2
'android-cloexec-accept'
'android-cloexec-accept4'
'android-cloexec-creat'
'android-cloexec-dup'
'android-cloexec-epoll-create'
'android-cloexec-epoll-create1'
'android-cloexec-fopen'
'android-cloexec-inotify-init'
'android-cloexec-inotify-init1'
'android-cloexec-memfd-create'
'android-cloexec-open'
'android-cloexec-socket'
// Tier 3