Skip to content

Commit c55c1c7

Browse files
committed
Clean up stale commit message data when git changes occur
- 'commit message details' could contain stale data - e.g. a file was commited outside of the feature - user could commit some changes outside of the feature, is `apps/editor/src/utils/commit-message-details.ts` properly auto-clearing? └── apps └── editor └── src ├── commands │ └── generate-commit-message-commands │ ├── actions │ │ ├── get-prompt-data.ts │ │ └── run-generate-action.ts │ ├── generate-commit-message-commands.ts │ ├── index.ts │ └── utils │ ├── build-commit-message-prompt.ts │ ├── generate-commit-message-with-api.ts │ ├── get-commit-message-config.ts │ └── strip-wrapping-quotes.ts ├── extension.ts ├── services │ └── git-discard-file-watcher.ts ├── utils │ ├── commit-message-details.ts │ └── git-repository-utils.ts └── views └── prompt └── backend └── utils └── persistent-prompt-storage.ts
1 parent a691249 commit c55c1c7

4 files changed

Lines changed: 108 additions & 8 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export const get_prompt_data = async (params: {
2424
}) => {
2525
await vscode.workspace.saveAll()
2626
await params.repository.status()
27+
28+
CommitMessageDetails.clean_up_stale_data({
29+
extension_context: params.extension_context,
30+
repository: params.repository
31+
})
32+
2733
const current_empty_stage =
2834
(params.repository.state.indexChanges || []).length === 0
2935
const was_empty_stage = current_empty_stage || !!params.files_staged_by_action

‎apps/editor/src/services/git-discard-file-watcher.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,26 @@ export const setup_git_discard_file_watcher = (
3535
)
3636
current_changes.add(relative_path)
3737
}
38+
if (change.originalUri) {
39+
const original_relative_path = normalize_path(
40+
path.relative(workspace_root, change.originalUri.fsPath)
41+
)
42+
current_changes.add(original_relative_path)
43+
}
3844
}
3945

4046
const previous_changes = repo_states.get(workspace_root)
4147

4248
if (previous_changes) {
43-
for (const prev_change of previous_changes) {
44-
if (!current_changes.has(prev_change)) {
45-
CommitMessageDetails.remove_file_path({
46-
extension_context,
47-
file_path: prev_change,
48-
workspace_root
49-
})
50-
}
49+
const has_removed_files = Array.from(previous_changes).some(
50+
(prev_change) => !current_changes.has(prev_change)
51+
)
52+
53+
if (has_removed_files) {
54+
CommitMessageDetails.clean_up_stale_data({
55+
extension_context,
56+
repository: repo
57+
})
5158
}
5259
}
5360

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,92 @@ export namespace CommitMessageDetails {
224224
}
225225
}
226226

227+
export const clean_up_stale_data = (params: {
228+
extension_context: vscode.ExtensionContext
229+
repository: {
230+
rootUri: vscode.Uri
231+
state: {
232+
indexChanges: any[]
233+
workingTreeChanges: any[]
234+
mergeChanges: any[]
235+
}
236+
}
237+
}) => {
238+
const workspace_root = params.repository.rootUri.fsPath
239+
const all_prompts = load_all(params.extension_context)
240+
if (!all_prompts[workspace_root]) return
241+
242+
const current_changes = new Set<string>()
243+
const all_git_changes = [
244+
...(params.repository.state.indexChanges || []),
245+
...(params.repository.state.workingTreeChanges || []),
246+
...(params.repository.state.mergeChanges || [])
247+
]
248+
249+
for (const change of all_git_changes) {
250+
if (change.uri) {
251+
const relative_path = normalize_path(
252+
path.relative(workspace_root, change.uri.fsPath)
253+
)
254+
current_changes.add(relative_path)
255+
}
256+
if (change.originalUri) {
257+
const original_relative_path = normalize_path(
258+
path.relative(workspace_root, change.originalUri.fsPath)
259+
)
260+
current_changes.add(original_relative_path)
261+
}
262+
}
263+
264+
let changed = false
265+
const original_length = all_prompts[workspace_root].length
266+
267+
all_prompts[workspace_root] = all_prompts[workspace_root]
268+
.map((p) => {
269+
const filter_fn = (f: string) => {
270+
const rel_path = path.isAbsolute(f)
271+
? normalize_path(path.relative(workspace_root, f))
272+
: normalize_path(f)
273+
return current_changes.has(rel_path)
274+
}
275+
276+
const filtered_files = p.files.filter(filter_fn)
277+
const filtered_selected_files = (p.selected_files || []).filter(
278+
filter_fn
279+
)
280+
281+
if (
282+
filtered_files.length !== p.files.length ||
283+
filtered_selected_files.length !== (p.selected_files || []).length
284+
) {
285+
changed = true
286+
return {
287+
...p,
288+
files: filtered_files,
289+
selected_files: filtered_selected_files
290+
}
291+
}
292+
return p
293+
})
294+
.filter((p) => p.files.length > 0)
295+
296+
if (all_prompts[workspace_root].length !== original_length) {
297+
changed = true
298+
}
299+
300+
if (all_prompts[workspace_root].length === 0) {
301+
delete all_prompts[workspace_root]
302+
changed = true
303+
}
304+
305+
if (changed) {
306+
save_all({
307+
extension_context: params.extension_context,
308+
prompts: all_prompts
309+
})
310+
}
311+
}
312+
227313
export const remove_committed_files = (params: {
228314
extension_context: vscode.ExtensionContext
229315
workspace_root: string

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type GitRepository = {
2323
state: {
2424
indexChanges: any[]
2525
workingTreeChanges: any[]
26+
mergeChanges: any[]
2627
}
2728
add: (files: string[]) => Promise<void>
2829
status: () => Promise<void>

0 commit comments

Comments
 (0)