-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathextension.ts
98 lines (82 loc) · 2.43 KB
/
extension.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
/**
* Extension module.
*
* This sets up the VS Code extension's command entry-point and applies logic in
* the `prepareCommitMsg` module to a target branch.
*/
import * as vscode from "vscode";
import { API, Repository } from "./api/git";
import { makeAndFillCommitMsg } from "./autofill";
import { getGitExtension } from "./gitExtension";
function _validateFoundRepos(git: API) {
let msg = "";
if (!git) {
msg = "Unable to load Git Extension";
} else if (git.repositories.length === 0) {
msg =
"No repos found. Please open a repo or run `git init` then try again.";
}
if (msg) {
vscode.window.showErrorMessage(msg);
throw new Error(msg);
}
}
/**
* Get current repo when using multiple repos in the workspace.
*
* Or when using GitLens on a single repo, based on a reported issue.
*/
async function _handleRepos(
git: API,
sourceControl: vscode.SourceControl,
): Promise<Repository | false> {
const selectedRepo = git.repositories.find(repository => {
const uri = sourceControl.rootUri;
if (!uri) {
console.warn("rootUri not set for current repo");
return false;
}
const repoPath = repository.rootUri.path;
return repoPath === uri.path;
});
return selectedRepo ?? false;
}
/**
* Return a repo for single repo in the workspace.
*/
async function _handleRepo(git: API): Promise<Repository> {
return git.repositories[0];
}
/**
* Choose the relevant repo and apply autofill logic on files there.
*/
async function _chooseRepoForAutofill(sourceControl?: vscode.SourceControl) {
const git = getGitExtension()!;
_validateFoundRepos(git);
vscode.commands.executeCommand("workbench.view.scm");
const selectedRepo = sourceControl
? await _handleRepos(git, sourceControl)
: await _handleRepo(git);
if (!selectedRepo) {
const msg = "No repos found";
vscode.window.showErrorMessage(msg);
throw new Error(msg);
}
await makeAndFillCommitMsg(selectedRepo);
}
/**
* Set up the extension activation.
*
* The `autofill` command as configured in `package.json` will be triggered
* and run the autofill logic for a repo.
*/
export function activate(context: vscode.ExtensionContext) {
const disposable = vscode.commands.registerCommand(
"commitMsg.autofill",
_chooseRepoForAutofill,
);
context.subscriptions.push(disposable);
}
// prettier-ignore
// eslint-disable-next-line @typescript-eslint/no-empty-function
export function deactivate() { }