Skip to content

Commit abe7650

Browse files
committed
Implement get_repository_for_commit to filter for repositories with active changes when generating commit messages
1 parent d7e3cf4 commit abe7650

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

apps/editor/src/commands/generate-commit-message-command/generate-commit-message-command.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
33
import {
4-
get_git_repository,
54
prepare_staged_changes,
6-
GitRepository
5+
GitRepository,
6+
get_repository_for_commit
77
} from '../../utils/git-repository-utils'
88
import { display_token_count } from '../../utils/display-token-count'
99
import { get_commit_message_config } from './utils/get-commit-message-config'
@@ -14,6 +14,7 @@ import axios from 'axios'
1414
import { PromptsForCommitMessagesUtils } from '../../utils/prompts-for-commit-messages-utils'
1515
import { simplify_prompt_symbols } from '@shared/utils/simplify-prompt-symbols'
1616
import { MAX_PROMPT_CHARS_IN_COMMIT_MESSAGE } from '@/constants/values'
17+
import { dictionary } from '@shared/constants/dictionary'
1718

1819
const truncate_prompt = (text: string): string => {
1920
if (text.length <= MAX_PROMPT_CHARS_IN_COMMIT_MESSAGE) return text
@@ -52,7 +53,7 @@ export const generate_commit_message_command = (
5253
let force_quick_pick = false
5354
const selection_state: { files?: string[] } = {}
5455

55-
const repository = await get_git_repository(params.source_control)
56+
const repository = await get_repository_for_commit(params.source_control)
5657
if (!repository) return
5758

5859
while (true) {
@@ -291,7 +292,7 @@ export const generate_commit_message_command = (
291292
const copy_command = vscode.commands.registerCommand(
292293
'codeWebChat.copyCommitMessagePrompt',
293294
async (source_control?: vscode.SourceControl) => {
294-
const repository = await get_git_repository(source_control)
295+
const repository = await get_repository_for_commit(source_control)
295296
if (!repository) return
296297
const data = await get_prompt_data(repository, !!source_control)
297298
if (!data) return

apps/editor/src/utils/git-repository-utils.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ export type GitRepository = {
2020
show: (ref: string, path: string) => Promise<string>
2121
}
2222

23-
export const get_git_repository = async (
24-
source_control?: vscode.SourceControl
25-
): Promise<GitRepository | null> => {
23+
export const get_all_git_repositories = (): GitRepository[] | null => {
2624
const git_extension = vscode.extensions.getExtension('vscode.git')
2725
if (!git_extension) {
2826
vscode.window.showErrorMessage(
@@ -41,6 +39,54 @@ export const get_git_repository = async (
4139
return null
4240
}
4341

42+
return repositories
43+
}
44+
45+
export const get_git_repository = async (
46+
source_control?: vscode.SourceControl
47+
): Promise<GitRepository | null> => {
48+
const repositories = get_all_git_repositories()
49+
if (!repositories) return null
50+
51+
if (source_control?.rootUri) {
52+
const repository = repositories.find(
53+
(repo) => repo.rootUri.toString() === source_control.rootUri!.toString()
54+
)
55+
if (repository) {
56+
return repository
57+
}
58+
}
59+
60+
if (repositories.length == 1) {
61+
return repositories[0]
62+
}
63+
64+
const picks = repositories.map((repo) => {
65+
const folder = vscode.workspace.getWorkspaceFolder(repo.rootUri)
66+
return {
67+
label: folder ? folder.name : path.basename(repo.rootUri.fsPath),
68+
description: repo.rootUri.fsPath,
69+
repository: repo
70+
}
71+
})
72+
73+
const selected = await vscode.window.showQuickPick(picks, {
74+
placeHolder: t('command.commit-message.select-repository')
75+
})
76+
77+
if (!selected) {
78+
return null
79+
}
80+
81+
return selected.repository
82+
}
83+
84+
export const get_repository_for_commit = async (
85+
source_control?: vscode.SourceControl
86+
): Promise<GitRepository | null> => {
87+
const repositories = get_all_git_repositories()
88+
if (!repositories) return null
89+
4490
if (source_control?.rootUri) {
4591
const repository = repositories.find(
4692
(repo) => repo.rootUri.toString() === source_control.rootUri!.toString()

0 commit comments

Comments
 (0)