Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 9fb6f0c

Browse files
feat: add git top level option (#420)
* feat: add git top level option Adds optional git root config locations. New option defaults to false. * fix: remove cluttered log line * docs: fix typo in description.
1 parent de0f0dd commit 9fb6f0c

2 files changed

Lines changed: 51 additions & 23 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
"default": false,
5656
"description": "Set to true to capitalize the Windows drive letter in Git's working directory path. Git treats paths as case sensitive and may ignore [includeIf] attributes in your .gitconfig if drive letters are the wrong case."
5757
},
58+
"commitizen.useGitRoot": {
59+
"type": "boolean",
60+
"default": false,
61+
"description": "Use git root to locate .cz-config.js file via `git rev-parse --show-toplevel`"
62+
},
5863
"commitizen.shell": {
5964
"type": "boolean",
6065
"default": true,

src/extension.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { exec } from 'child_process';
12
import execa from 'execa';
23
import { join } from 'path';
34
import * as sander from 'sander';
@@ -13,6 +14,7 @@ interface Configuration {
1314
showOutputChannel: 'off' | 'always' | 'onError';
1415
quoteMessageInGitCommit: boolean;
1516
capitalizeWindowsDriveLetter: boolean;
17+
useGitRoot: boolean;
1618
shell: boolean;
1719
}
1820

@@ -40,11 +42,9 @@ export async function activate(
4042
await ccm.getBody();
4143
await ccm.getBreaking();
4244
await ccm.getFooter();
43-
if (ccm.complete && vscode.workspace.workspaceFolders) {
44-
await commit(
45-
vscode.workspace.workspaceFolders[0].uri.fsPath,
46-
ccm.message.trim()
47-
);
45+
const lookupPath = await findLookupPath();
46+
if (ccm.complete && lookupPath) {
47+
await commit(lookupPath, ccm.message.trim());
4848
}
4949
})
5050
);
@@ -73,30 +73,55 @@ interface CzConfig {
7373
skipQuestions?: string[];
7474
}
7575

76+
let gitRoot: string | undefined = undefined;
77+
async function findLookupPath(): Promise<string | undefined> {
78+
if (getConfiguration().useGitRoot && gitRoot === undefined) {
79+
gitRoot = await new Promise<string>((resolve, reject) =>
80+
exec('git rev-parse --show-toplevel', (err, stdout, stderr) => {
81+
if (err) {
82+
reject({ err, stderr });
83+
} else if (stdout) {
84+
channel.appendLine(`Found git root at: ${stdout}`);
85+
resolve(stdout.trim());
86+
} else {
87+
reject({ err: 'Unable to find git root' });
88+
}
89+
})
90+
).catch((e) => {
91+
channel.appendLine(e.err.toString());
92+
if (e.stderr) {
93+
channel.appendLine(e.stderr.toString());
94+
}
95+
return undefined;
96+
});
97+
}
98+
99+
if (gitRoot) {
100+
return gitRoot;
101+
} else if (!vscode.workspace.workspaceFolders) {
102+
return undefined;
103+
} else {
104+
return vscode.workspace.workspaceFolders[0].uri.fsPath;
105+
}
106+
}
107+
76108
async function readCzConfig(): Promise<CzConfig | undefined> {
77-
if (!vscode.workspace.workspaceFolders) {
109+
const lookupPath = await findLookupPath();
110+
if (!lookupPath) {
78111
return undefined;
79112
}
80-
let configPath = join(
81-
vscode.workspace.workspaceFolders[0].uri.fsPath,
82-
'.cz-config.js'
83-
);
113+
let configPath = join(lookupPath, '.cz-config.js');
114+
84115
if (await sander.exists(configPath)) {
85116
return require(configPath) as CzConfig;
86117
}
87118
const pkg = await readPackageJson();
88119
if (!pkg) {
89120
return undefined;
90121
}
91-
configPath = join(
92-
vscode.workspace.workspaceFolders[0].uri.fsPath,
93-
'.cz-config.js'
94-
);
122+
configPath = join(lookupPath, '.cz-config.js');
95123
if (hasCzConfig(pkg)) {
96-
configPath = join(
97-
vscode.workspace.workspaceFolders[0].uri.fsPath,
98-
pkg.config['cz-customizable'].config
99-
);
124+
configPath = join(lookupPath, pkg.config['cz-customizable'].config);
100125
}
101126
if (!(await sander.exists(configPath))) {
102127
return undefined;
@@ -105,13 +130,11 @@ async function readCzConfig(): Promise<CzConfig | undefined> {
105130
}
106131

107132
async function readPackageJson(): Promise<object | undefined> {
108-
if (!vscode.workspace.workspaceFolders) {
133+
const lookupPath = await findLookupPath();
134+
if (!lookupPath) {
109135
return undefined;
110136
}
111-
const pkgPath = join(
112-
vscode.workspace.workspaceFolders[0].uri.fsPath,
113-
'package.json'
114-
);
137+
const pkgPath = join(lookupPath, 'package.json');
115138
if (!(await sander.exists(pkgPath))) {
116139
return undefined;
117140
}

0 commit comments

Comments
 (0)