Skip to content

Commit c8952ac

Browse files
Merge pull request #92 from MichaelCurrin/feat-support-multiple-repos
feat: Support multiple repos
2 parents 09fab3f + cf96b03 commit c8952ac

5 files changed

Lines changed: 23 additions & 24 deletions

File tree

docs/development/advanced/sandbox.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ Follow these steps:
1717
1. Go to the _Debug_ tab.
1818
1. Select one of two tasks:
1919
- _Run Extension_ task.
20-
- This will start in a default directory such as your user's home directory.
21-
- You might want to use File / Open to change the sandbox window to a repo what has more content to play with. This will be remembered on later runs. Unfortunately if you changed your VS Code settings to open in a new window on Open, then the extension setup will be undone.
20+
- This will start in a default directory - such as your user's home directory.
21+
- You might want to use _File_ / _Open_ to change the sandbox window to a repo what has more content to play with. This will be _remembered_ on later runs. Unfortunately if you changed your VS Code settings to open in a new window on Open, then the extension setup will be undone.
2222
- _Start in Sandbox repo_ task.
2323
- For more reliable and consistent behavior.
2424
- This will run against `sandbox` directory in the project, which is a Git repo where you can make files and commits as you like.

docs/features.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A roadmap of features and whether done or not.
1515
- Handle **staged** files if there are any, so you can change a few files and then generate messages for those. But if there are zero staged changes, the extension will fall back to the working tree of unstaged changes.
1616
- Note that **new** files (including when doing a rename) should **always** be staged so that the extension can pick them up and so git can see that two paths for a renamed file are the same file.
1717
- [x] Generate a single-line commit message for a file to be committed, using action verbs (e.g. `Create`, `Update`, `Delete`)
18-
- [x] Handle changes from a single changed file
18+
- [x] Handle changes from a single changed file.
1919
- [ ] Handle changes from two or more files.
2020
- [x] As a list of the same nature e.g. `update foo.txt and fizz/bar.txt`, `feat: create foo.txt, fizz/bar.txt and buzz.js` (including prefix) and `Various changes to foo.txt and fizz/bar.txt` (for one updated and one new file). See [#29](https://github.com/MichaelCurrin/auto-commit-msg/pull/29).
2121
- [x] As a count. e.g. `update 3 files`. See [#38](https://github.com/MichaelCurrin/auto-commit-msg/issues/38).
@@ -24,6 +24,7 @@ A roadmap of features and whether done or not.
2424
- [ ] As a count with a conventional commit message. See [#51](https://github.com/MichaelCurrin/auto-commit-msg/issues/51).
2525
- [ ] As a count with a label. e.g. `update 3 config files`. See [#13](https://github.com/MichaelCurrin/auto-commit-msg/issues/13).
2626
- [ ] As count that uses the old message. See [#55](https://github.com/MichaelCurrin/auto-commit-msg/issues/55)
27+
- [x] Support using multiple repos in one VS Code window.
2728
- [x] Keep user-entered value as a prefix e.g. Keep `docs:` (or ticket number) so message becomes `docs: Update README.md`
2829
- [x] Use conventional commits e.g. `chore: Update package.json`
2930
- [x] Support directories or filenames with spaces in them by adding quotes. e.g. `chore: rename foo.txt to 'foo bar.txt'`

src/autofill.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ Try saving your files or stage any new (untracked) files.\
1717
*
1818
* Steps:
1919
*
20-
* 1. Read git command output and the message in the Git Extension commit message box.
20+
* 1. Read Git command output and the message in the Git Extension commit message box.
2121
* 2. Generate a message.
2222
* 3. Push message value to the commit message box.
2323
*
2424
* This is based on `prefixCommit` from the `git-prefix` extension.
2525
*/
2626
export async function makeAndFillCommitMsg(repository: Repository) {
27-
const fileChanges = await getChanges();
27+
const fileChanges = await getChanges(repository);
2828

2929
console.debug("diff-index:", fileChanges);
3030

src/extension.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ function _validateFoundRepos(git: API) {
3030
* Run autofill against one of multiples in the workspace or when using GitLens on a single repo.
3131
*/
3232
async function _handleRepos(git: API, sourceControl: vscode.SourceControl) {
33-
// FIXME: Unfortunately this seems to only pick up the first repo and not find
34-
// second, etc. If you repository through to makeAndFillCommitMsg, getChanges
35-
// and diffIndex etc. and replace "getWorkspaceFolder" usage then that will work.
3633
const selectedRepo = git.repositories.find(repository => {
3734
const uri = sourceControl.rootUri;
3835
if (!uri) {
3936
console.warn("rootUri not set for current repo");
4037
return false;
4138
}
42-
return repository.rootUri.path === uri.path;
39+
const repoPath = repository.rootUri.path;
40+
41+
return repoPath === uri.path;
4342
});
4443

4544
if (selectedRepo) {

src/git/cli.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
*/
66
import util = require("util");
77
import childProcess = require("child_process");
8-
9-
import { getWorkspaceFolder } from "../workspace";
8+
import { Repository } from "../api/git";
109

1110
const exec = util.promisify(childProcess.exec);
1211

1312
/**
14-
* Run a `git` subcommand with options and return output.
13+
* Run a `git` subcommand and return output.
1514
*/
1615
function _execute(cwd: string, subcommand: string, options: string[] = []) {
1716
const command = `git ${subcommand} ${options.join(" ")}`;
1817

19-
const result = exec(command, { cwd });
18+
console.debug(`Running command: ${command}, cwd: ${cwd}`);
2019

21-
return result;
20+
return exec(command, { cwd });
2221
}
2322

2423
/**
@@ -33,9 +32,12 @@ function _execute(cwd: string, subcommand: string, options: string[] = []) {
3332
* The output already seems to never have color info, from my testing, but the
3433
* no-color flagged is added still to be safe.
3534
*/
36-
async function _diffIndex(options: string[] = []): Promise<Array<string>> {
35+
async function _diffIndex(
36+
repository: Repository,
37+
options: string[] = []
38+
): Promise<Array<string>> {
39+
const cwd = repository.rootUri.path;
3740
const cmd = "diff-index";
38-
3941
const fullOptions = [
4042
"--name-status",
4143
"--find-renames",
@@ -44,11 +46,8 @@ async function _diffIndex(options: string[] = []): Promise<Array<string>> {
4446
...options,
4547
"HEAD",
4648
];
47-
const { stdout, stderr } = await _execute(
48-
getWorkspaceFolder(),
49-
cmd,
50-
fullOptions
51-
);
49+
50+
const { stdout, stderr } = await _execute(cwd, cmd, fullOptions);
5251

5352
if (stderr) {
5453
console.debug(`stderr for 'git ${cmd}' command:`, stderr);
@@ -68,8 +67,8 @@ async function _diffIndex(options: string[] = []): Promise<Array<string>> {
6867
*
6968
* Returns an array of strings.
7069
*/
71-
export async function getChanges() {
72-
const stagedChanges = await _diffIndex(["--cached"]);
70+
export async function getChanges(repository: Repository) {
71+
const stagedChanges = await _diffIndex(repository, ["--cached"]);
7372

7473
if (stagedChanges.length) {
7574
console.debug("Found staged changes");
@@ -81,7 +80,7 @@ export async function getChanges() {
8180
"Staging area is empty. Using unstaged files (tracked files only still)."
8281
);
8382

84-
const allChanges = await _diffIndex();
83+
const allChanges = await _diffIndex(repository);
8584

8685
if (!allChanges.length) {
8786
console.debug("No changes found");

0 commit comments

Comments
 (0)