Skip to content

Commit c77c121

Browse files
MichaelCurrinCopilot
andcommitted
docs: add docstrings
Co-authored-by: Copilot <copilot@github.com>
1 parent 8d06368 commit c77c121

10 files changed

Lines changed: 132 additions & 23 deletions

File tree

src/cli/diffIndexGenerate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Options:
1818
--verbose Show debug output for this run.
1919
`;
2020

21+
/**
22+
* Get the current repository by running git command.
23+
*
24+
* Executes git command to determine the root directory of the current Git
25+
* repository.
26+
*
27+
* @returns A Repository object with the root URI of the current Git repository.
28+
* @throws Error if not in a Git repository or if the git command fails.
29+
*/
2130
function _getCurrentRepository(): Repository {
2231
return {
2332
rootUri: {

src/cli/diffIndexGenerateCommit.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ Options:
2121
`;
2222

2323
/**
24-
* Command-line entry-point.
24+
* Generate a commit message and execute a Git commit with that message.
25+
*
26+
* Steps:
27+
* 1. Check Git changes and generate a commit message.
28+
* 2. Run `git commit` with the generated message.
29+
* 3. On failure, retry with current directory specified.
30+
*
31+
* @param argv Command-line arguments.
2532
*/
2633
async function main(argv: string[]): Promise<void> {
2734
if (shouldShowHelp(argv)) {

src/cli/generate.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
import { generateMsg } from "../prepareCommitMsg";
1313

14+
import { shouldShowHelp } from "./utils";
15+
1416
const HELP_TEXT: string = `Usage: auto_commit_msg_generate DIFF_INDEX_OUTPUT
1517
1618
Generate a commit message from given input.
@@ -22,14 +24,15 @@ Options:
2224
--help, -h Show this help and exit.`;
2325

2426
/**
25-
* Command-line entry-point.
27+
* Generate a commit message from Git diff-index output.
2628
*
27-
* Returns a suitable generated commit message as text.
28-
*/
29-
import { shouldShowHelp } from "./utils";
30-
31-
/**
32-
* Entry-point for the CLI. Handles help flag and input validation.
29+
* Parses the provided diff-index output and generates a suitable commit message.
30+
* Throws an error if no arguments or no changes are found.
31+
*
32+
* @param args Command-line arguments. First argument should be the diff-index
33+
* output.
34+
* @throws Error if no arguments provided or no file changes found in the
35+
* output.
3336
*/
3437
function main(args: string[]): void {
3538
if (shouldShowHelp(args)) {

src/cli/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
* Determine whether help should be displayed.
77
*
88
* @param argv Command-line arguments passed to the CLI.
9-
*
10-
* @returns boolean True when `--help` or `-h` is present.
9+
* @returns True when `--help` or `-h` is present in the arguments.
1110
*/
1211
export function shouldShowHelp(argv: string[]): boolean {
1312
return argv.includes("--help") || argv.includes("-h");

src/extension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { API, Repository } from "./api/git";
99
import { makeAndFillCommitMsg } from "./autofill";
1010
import { getGitExtension } from "./gitExtension";
1111

12+
/**
13+
* Validate that the Git extension is available and at least one repository is open.
14+
*
15+
* @param git The Git extension API object.
16+
* @throws Error if Git extension is not available or no repositories are found.
17+
*/
1218
function _validateFoundRepos(git: API) {
1319
let msg = "";
1420

src/generate/convCommit.ts

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ export class ConventionalCommit {
5252
}
5353

5454
/**
55-
* Check if doc-related.
55+
* Check if a file is documentation-related.
5656
*
57-
* Return true for `.rst`, README files, and anything in the docs directory.
57+
* Return true for `.rst` files, README files, and anything in the docs
58+
* directory.
5859
*
5960
* TODO: For static sites, not all `.md` files are docs but that could be
6061
* configured with a global flag. Or recognize presence of Jekyll config file.
62+
*
63+
* @returns True if the file is documentation-related, false otherwise.
6164
*/
6265
isDocsRelated(): boolean {
6366
if (this.extension === ".rst") {
@@ -68,6 +71,14 @@ export class ConventionalCommit {
6871
return DOC_NAMES.includes(lowerName) || this.dirPath.startsWith("docs");
6972
}
7073

74+
/**
75+
* Check if a file is test-related.
76+
*
77+
* Returns true for files in test directories (at any nesting level) or
78+
* matching test-related file patterns.
79+
*
80+
* @returns True if the file is test-related, false otherwise.
81+
*/
7182
isTestRelated(): boolean {
7283
const dir = this.dirPath;
7384
const name = this.name;
@@ -102,17 +113,29 @@ export class ConventionalCommit {
102113
return false;
103114
}
104115

116+
/**
117+
* Check if a file is CI/CD configuration-related.
118+
*
119+
* Returns true for files in CI directories or matching CI configuration file
120+
* names.
121+
*
122+
* @returns True if the file is CI/CD related, false otherwise.
123+
*/
105124
isCIRelated(): boolean {
106125
// Assume flat structure and don't check for nesting in subdirs of
107126
// `CI_DIRS`.
108127
return CI_DIRS.includes(this.dirPath) || CI_NAMES.includes(this.name);
109128
}
110129

111-
// Broadly match eslint configs with any extension e.g. `.json` or `.yml`.
112-
// See https://eslint.org/docs/user-guide/configuring
113-
// Same for prettier configs https://prettier.io/docs/en/configuration.html
114-
// And `tslint*` as JSON or YAML and `webpack*`.
115-
// See https://github.com/vscode-icons/vscode-icons/blob/master/src/iconsManifest/supportedExtensions.ts
130+
/**
131+
* Check if a file is configuration-related.
132+
*
133+
* Matches ESLint, Prettier, TSLint, Webpack configs and other configuration
134+
* files with various extensions. See https://eslint.org/docs/user-guide/configuring
135+
* and https://prettier.io/docs/en/configuration.html for more details.
136+
*
137+
* @returns True if the file is configuration-related, false otherwise.
138+
*/
116139
isConfigRelated(): boolean {
117140
return (
118141
CONFIG_EXTENSIONS.includes(this.extension) ||
@@ -127,31 +150,54 @@ export class ConventionalCommit {
127150
);
128151
}
129152

153+
/**
154+
* Check if a file is package management-related.
155+
*
156+
* @returns True if the file is a package manifest file, false otherwise.
157+
*/
130158
isPackageRelated(): boolean {
131159
return PACKAGE_NAMES.includes(this.name);
132160
}
133161

162+
/**
163+
* Check if a file is build-related.
164+
*
165+
* @returns True if the file is a build tool configuration or artifact, false
166+
* otherwise.
167+
*/
134168
isBuildRelated(): boolean {
135169
return (
136170
BUILD_NAMES.includes(this.name) ||
137171
BUILD_EXTENSIONS.includes(this.extension)
138172
);
139173
}
140174

175+
/**
176+
* Check if a file is license-related.
177+
*
178+
* @returns True if the file is a license file, false otherwise.
179+
*/
141180
isLicenseRelated(): boolean {
142181
return LICENSE_NAMES.includes(this.name);
143182
}
144183

184+
/**
185+
* Check if a file is chore-related (license or configuration files).
186+
*
187+
* @returns True if the file is license or configuration-related, false
188+
* otherwise.
189+
*/
145190
isChoreRelated(): boolean {
146191
return this.isLicenseRelated() || this.isConfigRelated();
147192
}
148193

149194
/**
150-
* Return Conventional Commit type.
195+
* Determine the Conventional Commit type for this file based on its path and content.
151196
*
152-
* Order of checks is important here.
197+
* Order of checks is important here: CI, package management, build, chore,
198+
* documentation, and test-related files are checked in that order.
153199
*
154-
* Return the unknown/null value if no rule matches.
200+
* @returns A CONVENTIONAL_TYPE value. Returns UNKNOWN if no rule matches.
155201
*/
156202
getType() {
157203
if (this.isCIRelated()) {

src/generate/count.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export function _countByAction(changes: FileChange[]) {
4747
return result;
4848
}
4949

50+
/**
51+
* Return the appropriate plural suffix based on a count.
52+
*
53+
* @param count The count value.
54+
* @returns An empty string if count is 1, otherwise 's' for plural.
55+
*/
5056
export function _pluralS(count: number) {
5157
return count === 1 ? "" : "s";
5258
}

src/git/cli.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const DIFF_INDEX_OPTIONS = [
2222
];
2323

2424
// Allow debug messages to be shown (VS Code extension) or hidden (CLI tool).
25+
/**
26+
* Log debug messages when debugging is enabled via environment variable.
27+
*
28+
* @param args Arguments to log.
29+
*/
2530
function debug(...args: unknown[]): void {
2631
if (process.env.ACM_DEBUG === "1") {
2732
console.debug(...args);
@@ -30,6 +35,11 @@ function debug(...args: unknown[]): void {
3035

3136
/**
3237
* Run a `git` subcommand and return the result, with stdout and stderr available.
38+
*
39+
* @param cwd The working directory to run the command in.
40+
* @param subcommand The git subcommand to execute (e.g., 'diff-index').
41+
* @param options Optional array of command-line options to pass to the subcommand.
42+
* @returns A promise resolving to the execution result with stdout and stderr.
3343
*/
3444
function _execute(cwd: string, subcommand: string, options: string[] = []) {
3545
const command = `git ${QUOTE_PATH} ${subcommand} ${options.join(" ")}`;
@@ -49,7 +59,12 @@ function _execute(cwd: string, subcommand: string, options: string[] = []) {
4959
* command-line data comes in or got split.
5060
*
5161
* The output already seems to never have color info, from testing, but the
52-
* no-color flagged is added still to be safe.
62+
* no-color flag is added still to be safe.
63+
*
64+
* @param repository The repository to run the command in.
65+
* @param options Optional array of additional git options (e.g., ['--cached']).
66+
* @returns A promise resolving to an array of non-empty output lines from the
67+
* git diff-index command.
5368
*/
5469
async function _diffIndex(
5570
repository: Repository,
@@ -76,7 +91,8 @@ async function _diffIndex(
7691
* Always excludes untracked files - make sure to stage a file so it becomes
7792
* tracked, especially in the case of a rename.
7893
*
79-
* Returns an array of strings.
94+
* @param repository The repository to get changes for.
95+
* @returns A promise resolving to an array of strings describing file changes.
8096
*/
8197
export async function getChanges(repository: Repository) {
8298
const stagedChanges = await _diffIndex(repository, ["--cached"]);

src/lib/paths.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export function splitPath(filePath: string): SplitPathResult {
3636
};
3737
}
3838

39-
/** Format to add quotes if the values contains spaces. */
39+
/**
40+
* Format to add quotes if the value contains spaces.
41+
*
42+
* @param value The string value to format.
43+
* @returns The value wrapped in single quotes if it contains spaces and is not
44+
* the root directory, otherwise the value unchanged.
45+
*/
4046
export function quoteForSpaces(value: string) {
4147
if (value.includes(" ") && value !== ROOT) {
4248
return `'${value}'`;
@@ -68,7 +74,12 @@ export function friendlyFile(filePath: string) {
6874
/**
6975
* Join a list of items using commas and an "and" word.
7076
*
77+
* For example: ['a', 'b', 'c'] becomes 'a, b and c'.
7178
* These don't have to be file paths but usually are for this project.
79+
*
80+
* @param items An array of strings to join.
81+
* @returns A formatted string with items joined by commas and "and", or an
82+
* empty string if the array is empty.
7283
*/
7384
export function _join(items: string[]) {
7485
if (!items.length) {

src/workspace.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { workspace } from "vscode";
22

3+
/**
4+
* Get the root folder path of the current workspace.
5+
*
6+
* @returns The file system path of the first workspace folder, or an empty
7+
* string if no workspace folders are open.
8+
*/
39
export function getWorkspaceFolder(): string {
410
const { workspaceFolders } = workspace;
511

0 commit comments

Comments
 (0)