11/**
22 * Extension module.
33 *
4- * This sets up the extension's command entry point and applies the prepare commit message module to
5- * a target branch.
4+ * This sets up the VS Code extension's command entry- point and applies logic in
5+ * the prepareCommitMsg module to a target branch.
66 */
77import * as vscode from "vscode" ;
88import { API } from "./api/git" ;
99import { makeAndFillCommitMsg } from "./autofill" ;
1010import { getGitExtension } from "./gitExtension" ;
1111
12+ function _validateFoundRepos ( git : API ) {
13+ let msg = "" ;
14+
15+ if ( ! git ) {
16+ msg = "Unable to load Git Extension" ;
17+ } else if ( git . repositories . length === 0 ) {
18+ msg =
19+ "No repos found. Please open a repo or run `git init` then try again." ;
20+ }
21+
22+ if ( msg ) {
23+ vscode . window . showErrorMessage ( msg ) ;
24+
25+ throw new Error ( msg ) ;
26+ }
27+ }
28+
1229/**
13- * Flow for multiple repos in workspace and selecting just one. This is a rare flow.
30+ * Run autofill against one of multiples in the workspace.
31+ *
32+ * This is a rare flow.
33+ *
34+ * @param sourceControl Of type `vscode.SourceControl` with public `.rootUri`
35+ * and private `.rootUri`.
1436 */
15- async function _handleRepos ( git : API , uri : any ) {
16- // FIXME: Unfortunately this seems to only pick up the first repo and not find second etc.
37+ async function _handleRepos ( git : API , sourceControl : any ) {
38+ // FIXME: Unfortunately this seems to only pick up the first repo and not find
39+ // second, etc.
1740 const selectedRepository = git . repositories . find ( repository => {
18- return repository . rootUri . path === uri . _rootUri . path ;
41+ const uri = sourceControl . _rootUri ;
42+ if ( ! uri ) {
43+ console . warn ( "_rootUri not set" ) ;
44+ }
45+ return repository . rootUri . path === uri . path ;
1946 } ) ;
2047
2148 if ( selectedRepository ) {
@@ -26,28 +53,19 @@ async function _handleRepos(git: API, uri: any) {
2653}
2754
2855/**
29- * Flow for a single or zero repos in the workspace.
56+ * Run autofill flow for a single repo in the workspace.
3057 */
3158async function _handleRepo ( git : API ) {
3259 const targetRepo = git . repositories [ 0 ] ;
33-
3460 await makeAndFillCommitMsg ( targetRepo ) ;
3561}
3662
37- async function _autofill ( uri ?: string ) {
38- const git = getGitExtension ( ) ;
39-
40- if ( ! git ) {
41- vscode . window . showErrorMessage ( "Unable to load Git Extension" ) ;
42- return ;
43- }
44-
45- if ( git . repositories . length === 0 ) {
46- vscode . window . showErrorMessage (
47- "No repos found. Please open a repo or run git init then try this extension again."
48- ) ;
49- return ;
50- }
63+ /**
64+ * Choose the relevant repo and apply autofill logic on files there.
65+ */
66+ async function _chooseRepoForAutofill ( uri ?: vscode . Uri ) {
67+ const git = getGitExtension ( ) ! ;
68+ _validateFoundRepos ( git ) ;
5169
5270 vscode . commands . executeCommand ( "workbench.view.scm" ) ;
5371
@@ -59,16 +77,20 @@ async function _autofill(uri?: string) {
5977}
6078
6179/**
62- * Set up this extension's autofill command to run when triggered.
80+ * Set up the extension activation.
81+ *
82+ * The autofill command as configured in `package.json` will be triggered
83+ * and run the autofill logic for a repo.
6384 */
6485export function activate ( context : vscode . ExtensionContext ) {
6586 const disposable = vscode . commands . registerCommand (
6687 "commitMsg.autofill" ,
67- _autofill
88+ _chooseRepoForAutofill
6889 ) ;
6990
7091 context . subscriptions . push ( disposable ) ;
7192}
7293
94+ // prettier-ignore
7395// eslint-disable-next-line @typescript-eslint/no-empty-function
74- export function deactivate ( ) { }
96+ export function deactivate ( ) { }
0 commit comments