-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathpre-commit.ts
331 lines (318 loc) · 8 KB
/
pre-commit.ts
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import YAML from "yaml";
import { gitGenerators } from "./git";
const hooksInConfig: Fig.Generator = {
script: ["cat", ".pre-commit-config.yaml"],
postProcess: (output) => {
const suggestions: Fig.Suggestion[] = [];
try {
const repos = YAML.parse(output).repos;
if (repos) {
repos.forEach((repo) => {
repo.hooks.forEach((hookId) => {
suggestions.push({
name: `${hookId.id}`,
}) as Fig.Suggestion;
});
});
}
return suggestions;
} catch (e) {
console.log(e);
}
},
};
const help: Fig.Option = {
name: ["-h", "--help"],
description: "Show help message and exit",
};
const color: Fig.Option = {
name: "--color",
description: "Whether to use color in output. Defaults to `auto`",
args: {
name: "color",
suggestions: ["auto", "always", "never"],
},
};
const config: Fig.Option = {
name: ["--config", "-c"],
description: "Path to alternate config file",
args: {
name: "CONFIG",
template: "filepaths",
},
};
const globalOptions: Fig.Option[] = [help, color, config];
const hookStages = [
"pre-commit",
"pre-merge-commit",
"pre-push",
"prepare-commit-msg",
"commit-msg",
"post-commit",
"post-checkout",
"post-merge",
"post-rewrite",
];
const hookType: Fig.Option = {
name: ["-t", "--hook-type"],
description: "Type of hook to install",
args: {
name: "HOOK_TYPE",
suggestions: hookStages,
},
};
const commonRepoOptions: Fig.Option[] = [
{
name: ["--verbose", "-v"],
},
{
name: ["--all-files", "-a"],
description: "Run all files in the repo",
},
{
name: "--files",
description: "Specific filenames to run hooks on",
args: {
name: "FILES",
isVariadic: true,
template: ["filepaths", "folders"],
},
},
{
name: "--show-diff-on-failure",
description: "When hooks fail, run `git diff` directly afterward",
},
{
name: "--hook-stage",
description: "The stage during which the hook is fired",
args: {
suggestions: hookStages,
},
},
{
name: "--remote-branch",
description: "Remote branch ref used by `git push`",
args: {
name: "REMOTE_BRANCH",
generators: gitGenerators.remoteLocalBranches,
},
},
{
name: "--local-branch",
description: "Local branch ref used by `git push`",
args: {
name: "LOCAL_BRANCH",
generators: gitGenerators.localBranches,
},
},
{
name: ["--from-ref", "--source", "-s"],
description:
"Represents the original ref in a `from_ref...to_ref` diff expression. For `pre-push` hooks, this represents the branch you are pushing to. For `post-checkout` hooks, this represents the branch that was previously checked out",
args: {
name: "FROM_REF",
generators: gitGenerators.revs,
},
},
{
name: ["--to-ref", "--origin", "-o"],
description:
"Represents the destination ref in a `from_ref...to_ref` diff expression. For `pre-push` hooks, this represents the branch being pushed. For `post-checkout` hooks, this represents the branch that is now checked out",
args: {
name: "TO_REF",
generators: gitGenerators.revs,
},
},
{
name: "--commit-msg-filename",
description: "Filename to check when running during `commit-msg`",
args: {
name: "COMMIT_MSG_FILENAME",
},
},
{
name: "--remote-name",
description: "Remote name used by `git push`",
args: {
name: "REMOTE_NAME",
generators: gitGenerators.remotes,
},
},
{
name: "--remote-url",
description: "Remote URL used by `git push`",
args: {
name: "REMOTE_URL",
},
},
{
name: "--checkout-type",
description:
"Indicates whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0)",
args: {
name: "CHECKOUT_TYPE",
},
},
{
name: "--is-squash-merge",
description:
"During a post-merge hook, indicates whether the merge was a squash merge",
args: {
name: "IS_SQUASH_MERGE",
},
},
{
name: "--rewrite-command",
description:
"During a post-rewrite hook, specifies the command that invoked the rewrite",
args: {
name: "REWRITE_COMMAND",
},
},
];
const completionSpec: Fig.Spec = {
name: "pre-commit",
description: "Pre-commit",
args: {},
options: [
help,
{
name: ["-V", "--version"],
description: "Show program's version number and exit",
},
],
subcommands: [
{
name: "autoupdate",
description:
"Auto-update pre-commit config to the latest repos' versions",
options: [
...globalOptions,
{
name: "--bleeding-edge",
description:
"Update to the bleeding edge of `master` instead of the latest tagged version (the default behavior)",
},
{
name: "--freeze",
description: "Store 'frozen' hashes in `rev` instead of tag names",
},
{
name: "--repo",
description:
"Only update this repository -- may be specified multiple times",
args: {
name: "REPO",
},
isRepeatable: true,
},
],
},
{
name: "clean",
description: "Clean out pre-commit files",
options: globalOptions,
},
{
name: "gc",
description: "Clean unused cached repos",
options: globalOptions,
},
{
name: "init-templatedir",
description:
"Install hook script in a directory intended for use with `git config init.templateDir`",
options: [
...globalOptions,
hookType,
{
name: "--no-allow-missing-config",
description: "Assume cloned repos should have a `pre-commit` config",
},
],
},
{
name: "install",
description: "Install the pre-commit script",
options: [
...globalOptions,
{
name: ["-f", "--overwrite"],
description: "Overwrite existing hooks / remove migration mode",
},
{
name: "install-hooks",
description:
"Whether to install hook environments for all environments in the config file",
},
hookType,
{
name: "--allow-missing-config",
description:
"Whether to allow a missing `pre-commit` configuration file or exit with a failure code",
},
],
},
{
name: "migrate-config",
description: "Migrate list configuration to new map configuration",
options: globalOptions,
},
{
name: "run",
description: "Run hooks",
options: [...globalOptions, ...commonRepoOptions],
args: {
name: "hook",
description: "A single hook-id to run",
isOptional: true,
generators: hooksInConfig,
},
},
{
name: "sample-config",
description: "Produce a sample .pre-commit-config.yaml file",
options: globalOptions,
},
{
name: "try-repo",
description:
"Try the hooks in a repository, useful for developing new hooks",
args: [
{
name: "repo",
description: "Repository to source hooks from",
},
{
name: "hook",
description: "A single hook-id to run",
isOptional: true,
generators: hooksInConfig,
},
],
options: [
...globalOptions,
...commonRepoOptions,
{
name: ["--ref", "--rev"],
description:
"Manually select a rev to run against, otherwise the `HEAD` revision will be used",
args: {
name: "REV",
},
},
],
},
{
name: "uninstall",
description: "Uninstall the pre-commit script",
options: [...globalOptions, hookType],
},
{
name: "help",
description: "Show help for a specific command",
options: globalOptions,
},
],
};
export default completionSpec;