Skip to content

Commit 71eb215

Browse files
committed
Refactor git utility and command functions to accept parameters via an options object
- [Fragment] should accept 'params' variable. don't destructure inside the body - remove disprepancy in names stage_all_if_none_staged: is_source_control_action, - [Fragment] should accept 'params' variable. don't destructure inside the body - current behavior: identical prompts can be attached to commit message. expected behavior: filter-out repeated prompts
1 parent 8a8f0b3 commit 71eb215

2 files changed

Lines changed: 62 additions & 42 deletions

File tree

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

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,34 @@ const truncate_prompt = (text: string): string => {
2323
export const generate_commit_message_command = (
2424
context: vscode.ExtensionContext
2525
) => {
26-
const get_prompt_data = async (
27-
repository: GitRepository,
28-
is_source_control_action: boolean,
26+
const get_prompt_data = async (params: {
27+
repository: GitRepository
28+
stage_all_if_none_staged: boolean
2929
selection_state?: { files?: string[] }
30-
) => {
30+
}) => {
3131
await vscode.workspace.saveAll()
32-
await repository.status()
33-
const was_empty_stage = (repository.state.indexChanges || []).length == 0
34-
const working_tree_changes = repository.state.workingTreeChanges || []
32+
await params.repository.status()
33+
const was_empty_stage =
34+
(params.repository.state.indexChanges || []).length == 0
35+
const working_tree_changes =
36+
params.repository.state.workingTreeChanges || []
3537
const is_single_change = was_empty_stage && working_tree_changes.length == 1
36-
const diff = await prepare_staged_changes(
37-
repository,
38-
is_source_control_action,
39-
selection_state
40-
)
38+
const diff = await prepare_staged_changes({
39+
repository: params.repository,
40+
stage_all_if_none_staged: params.stage_all_if_none_staged,
41+
selection_state: params.selection_state
42+
})
4143
if (!diff) return null
42-
const message_prompt = await build_commit_message_prompt(diff, repository)
43-
return { repository, was_empty_stage, message_prompt, is_single_change }
44+
const message_prompt = await build_commit_message_prompt(
45+
diff,
46+
params.repository
47+
)
48+
return {
49+
repository: params.repository,
50+
was_empty_stage,
51+
message_prompt,
52+
is_single_change
53+
}
4454
}
4555

4656
const run_generate_action = async (params: {
@@ -56,11 +66,11 @@ export const generate_commit_message_command = (
5666
if (!repository) return
5767

5868
while (true) {
59-
const data = await get_prompt_data(
69+
const data = await get_prompt_data({
6070
repository,
61-
!!params.source_control,
71+
stage_all_if_none_staged: !!params.source_control,
6272
selection_state
63-
)
73+
})
6474
if (!data) return
6575
const { message_prompt, is_single_change } = data
6676
let { was_empty_stage } = data
@@ -137,9 +147,12 @@ export const generate_commit_message_command = (
137147
.getConfiguration('codeWebChat')
138148
.get<boolean>('includePromptsInCommitMessages', true)
139149

140-
const relevant_prompts = all_prompts.filter((p) =>
141-
p.files.some((file) => staged_files.includes(file))
142-
)
150+
const relevant_prompts = all_prompts
151+
.filter((p) => p.files.some((file) => staged_files.includes(file)))
152+
.filter(
153+
(p, index, self) =>
154+
index == self.findIndex((sp) => sp.prompt == p.prompt)
155+
)
143156

144157
if (params.should_commit) {
145158
const edited_message = await new Promise<string | 'back' | undefined>(
@@ -293,7 +306,10 @@ export const generate_commit_message_command = (
293306
async (source_control?: vscode.SourceControl) => {
294307
const repository = await get_repository_for_commit(source_control)
295308
if (!repository) return
296-
const data = await get_prompt_data(repository, !!source_control)
309+
const data = await get_prompt_data({
310+
repository,
311+
stage_all_if_none_staged: !!source_control
312+
})
297313
if (!data) return
298314
const { was_empty_stage, message_prompt } = data
299315

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,33 @@ export const get_repository_for_commit = async (
137137
return selected.repository
138138
}
139139

140-
export const prepare_staged_changes = async (
141-
repository: GitRepository,
142-
stage_all_if_none_staged: boolean = false,
140+
export const prepare_staged_changes = async (params: {
141+
repository: GitRepository
142+
stage_all_if_none_staged?: boolean
143143
selection_state?: { files?: string[] }
144-
): Promise<string | null> => {
145-
await repository.status()
146-
const staged_changes = repository.state.indexChanges || []
144+
}): Promise<string | null> => {
145+
await params.repository.status()
146+
const staged_changes = params.repository.state.indexChanges || []
147147

148148
if (
149149
staged_changes.length == 0 &&
150-
repository.state.workingTreeChanges.length > 0
150+
params.repository.state.workingTreeChanges.length > 0
151151
) {
152152
let files_to_stage: string[] = []
153153

154-
if (stage_all_if_none_staged) {
155-
files_to_stage = repository.state.workingTreeChanges.map(
154+
if (params.stage_all_if_none_staged) {
155+
files_to_stage = params.repository.state.workingTreeChanges.map(
156156
(change: any) => change.uri.fsPath
157157
)
158-
} else if (repository.state.workingTreeChanges.length == 1) {
159-
files_to_stage = [repository.state.workingTreeChanges[0].uri.fsPath]
158+
} else if (params.repository.state.workingTreeChanges.length == 1) {
159+
files_to_stage = [
160+
params.repository.state.workingTreeChanges[0].uri.fsPath
161+
]
160162
} else {
161163
const items = await Promise.all(
162-
repository.state.workingTreeChanges.map(async (change: any) => {
164+
params.repository.state.workingTreeChanges.map(async (change: any) => {
163165
const relative_path = path.relative(
164-
repository.rootUri.fsPath,
166+
params.repository.rootUri.fsPath,
165167
change.uri.fsPath
166168
)
167169
const dir_name = path.dirname(relative_path)
@@ -187,7 +189,7 @@ export const prepare_staged_changes = async (
187189
}
188190
} else {
189191
const raw_diff = execSync(`git diff -- "${change.uri.fsPath}"`, {
190-
cwd: repository.rootUri.fsPath
192+
cwd: params.repository.rootUri.fsPath
191193
}).toString()
192194

193195
if (
@@ -279,9 +281,9 @@ export const prepare_staged_changes = async (
279281
const quick_pick = vscode.window.createQuickPick<any>()
280282
quick_pick.items = items
281283

282-
if (selection_state?.files) {
284+
if (params.selection_state?.files) {
283285
quick_pick.selectedItems = items.filter((i) =>
284-
selection_state.files!.includes(i.fsPath)
286+
params.selection_state!.files!.includes(i.fsPath)
285287
)
286288
} else {
287289
quick_pick.selectedItems = items
@@ -319,8 +321,10 @@ export const prepare_staged_changes = async (
319321

320322
quick_pick.onDidAccept(() => {
321323
const selected_items = Array.from(quick_pick.selectedItems)
322-
if (selection_state) {
323-
selection_state.files = selected_items.map((i: any) => i.fsPath)
324+
if (params.selection_state) {
325+
params.selection_state.files = selected_items.map(
326+
(i: any) => i.fsPath
327+
)
324328
}
325329
resolve(selected_items)
326330
quick_pick.hide()
@@ -345,13 +349,13 @@ export const prepare_staged_changes = async (
345349
.map((file: string) => `"${file.replace(/"/g, '\\"')}"`)
346350
.join(' ')
347351
execSync(`git add -- ${file_args}`, {
348-
cwd: repository.rootUri.fsPath
352+
cwd: params.repository.rootUri.fsPath
349353
})
350-
await repository.status()
354+
await params.repository.status()
351355
}
352356

353357
const diff = execSync('git diff --staged', {
354-
cwd: repository.rootUri.fsPath
358+
cwd: params.repository.rootUri.fsPath
355359
}).toString()
356360

357361
if (!diff || diff.length == 0) {

0 commit comments

Comments
 (0)