Skip to content

Commit 469bca7

Browse files
committed
Filter selected context files by file existence on disk in commit message details and generation
- it's been reported ascii trees are no longer included in commit messages
1 parent 1d168cf commit 469bca7

3 files changed

Lines changed: 53 additions & 22 deletions

File tree

‎apps/editor/src/commands/generate-commit-message-commands/actions/get-prompt-data.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from 'fs'
12
import * as path from 'path'
23
import * as vscode from 'vscode'
34
import {
@@ -102,7 +103,16 @@ export const get_prompt_data = async (params: {
102103
? normalize_path(path.relative(workspace_root, f))
103104
: normalize_path(f)
104105
if (!diff_file_paths.includes(rel_path) && !diff_file_paths.includes(f)) {
105-
context_files_set.add(rel_path)
106+
const abs_path = path.isAbsolute(f)
107+
? f
108+
: path.join(workspace_root, rel_path)
109+
try {
110+
if (fs.statSync(abs_path).isFile()) {
111+
context_files_set.add(rel_path)
112+
}
113+
} catch {
114+
// Ignore non-existent or inaccessible paths
115+
}
106116
}
107117
}
108118
}

‎apps/editor/src/commands/generate-commit-message-commands/actions/run-generate-action.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from 'fs'
12
import * as path from 'path'
23
import * as vscode from 'vscode'
34
import { get_repository_for_commit } from '../../../utils/git-repository-utils'
@@ -759,7 +760,14 @@ export const run_generate_action = async (params: {
759760
const selected_files_set = new Set<string>()
760761
for (const p of [...selected_prompts, ...empty_prompts]) {
761762
for (const f of p.selected_files || []) {
762-
selected_files_set.add(f)
763+
const abs_path = path.isAbsolute(f) ? f : path.join(workspace_root, f)
764+
try {
765+
if (fs.statSync(abs_path).isFile()) {
766+
selected_files_set.add(f)
767+
}
768+
} catch {
769+
// Ignore non-existent or inaccessible paths
770+
}
763771
}
764772
}
765773
const selected_files_to_attach = Array.from(selected_files_set)

‎apps/editor/src/utils/commit-message-details.ts‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import { normalize_path } from './normalize-path'
1010
export namespace CommitMessageDetails {
1111
const COMMIT_MESSAGE_DETAILS_FILENAME = 'commit-message-details.json'
1212

13+
const is_existing_file = (abs_path: string): boolean => {
14+
try {
15+
return fs.statSync(abs_path).isFile()
16+
} catch {
17+
return false
18+
}
19+
}
20+
1321
export type Prompt = {
1422
prompt: string
1523
files: string[]
@@ -189,7 +197,10 @@ export namespace CommitMessageDetails {
189197

190198
const filtered_files = p.files.filter(filter_fn)
191199
const filtered_selected_files = (p.selected_files || []).filter(
192-
filter_fn
200+
(f) => {
201+
const abs_path = path.isAbsolute(f) ? f : path.join(root, f)
202+
return is_existing_file(abs_path)
203+
}
193204
)
194205

195206
if (
@@ -266,17 +277,17 @@ export namespace CommitMessageDetails {
266277

267278
all_prompts[workspace_root] = all_prompts[workspace_root]
268279
.map((p) => {
269-
const filter_fn = (f: string) => {
280+
const filtered_files = p.files.filter((f) => {
270281
const rel_path = path.isAbsolute(f)
271282
? normalize_path(path.relative(workspace_root, f))
272283
: normalize_path(f)
273284
return current_changes.has(rel_path)
274-
}
285+
})
275286

276-
const filtered_files = p.files.filter(filter_fn)
277-
const filtered_selected_files = (p.selected_files || []).filter(
278-
filter_fn
279-
)
287+
const filtered_selected_files = (p.selected_files || []).filter((f) => {
288+
const abs_path = path.isAbsolute(f) ? f : path.join(workspace_root, f)
289+
return is_existing_file(abs_path)
290+
})
280291

281292
if (
282293
filtered_files.length !== p.files.length ||
@@ -331,19 +342,21 @@ export namespace CommitMessageDetails {
331342

332343
all_prompts[params.workspace_root] = all_prompts[params.workspace_root]
333344
.map((p) => {
334-
if (!prompt_set.has(p.prompt)) return p
335-
336-
const filter_fn = (f: string) => {
337-
const rel_path = path.isAbsolute(f)
338-
? normalize_path(path.relative(params.workspace_root, f))
339-
: normalize_path(f)
340-
return !committed_set.has(rel_path)
341-
}
342-
343-
const filtered_files = p.files.filter(filter_fn)
344-
const filtered_selected_files = (p.selected_files || []).filter(
345-
filter_fn
346-
)
345+
const filtered_files = prompt_set.has(p.prompt)
346+
? p.files.filter((f) => {
347+
const rel_path = path.isAbsolute(f)
348+
? normalize_path(path.relative(params.workspace_root, f))
349+
: normalize_path(f)
350+
return !committed_set.has(rel_path)
351+
})
352+
: p.files
353+
354+
const filtered_selected_files = (p.selected_files || []).filter((f) => {
355+
const abs_path = path.isAbsolute(f)
356+
? f
357+
: path.join(params.workspace_root, f)
358+
return is_existing_file(abs_path)
359+
})
347360

348361
if (
349362
filtered_files.length !== p.files.length ||

0 commit comments

Comments
 (0)