Skip to content

Commit a3d0f55

Browse files
committed
Add commit message extraction and cleaning to commit symbol replacement
- should include commit message below the heading "# Commit [hash]". When encountered ascii tree in the commit message, remove it - place remove_ascii_tree in `apps/editor/src/utils/ascii-tree/ascii-tree.ts` - rename remove_ascii_tree to 'strip from text' └── apps └── editor └── src ├── utils │ ├── ascii-tree │ │ └── ascii-tree.ts │ └── git-repository-utils.ts └── views └── prompt └── backend └── utils └── symbols ├── fragment │ └── replace-fragment-symbol.ts ├── git │ └── replace-git-symbols.ts ├── image │ └── replace-image-symbol.ts ├── pasted-text │ └── replace-pasted-text-symbol.ts ├── replace-symbols.ts ├── saved-context │ └── replace-saved-context-symbol.ts ├── selection │ └── replace-selection-symbol.ts ├── skill │ └── replace-skill-symbol.ts └── website └── replace-website-symbol.ts
1 parent 70d2286 commit a3d0f55

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎apps/editor/src/utils/ascii-tree/ascii-tree.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,32 @@ export namespace AsciiTree {
4747

4848
return Array.from(found_paths)
4949
}
50+
51+
export const strip_from_text = (text: string): string => {
52+
const lines = text.split(/\r?\n/)
53+
const result: string[] = []
54+
55+
for (let i = 0; i < lines.length; i++) {
56+
const line = lines[i]
57+
const idx1 = line.indexOf('├── ')
58+
const idx2 = line.indexOf('└── ')
59+
const idx =
60+
idx1 != -1 && idx2 != -1 ? Math.min(idx1, idx2) : Math.max(idx1, idx2)
61+
62+
if (idx != -1) {
63+
const prefix = line.substring(0, idx)
64+
if (/^[│\s]*$/.test(prefix)) {
65+
continue
66+
}
67+
} else if (/^[│\s]+$/.test(line)) {
68+
continue
69+
}
70+
71+
result.push(line)
72+
}
73+
74+
let cleaned = result.join('\n')
75+
cleaned = cleaned.replace(/```[a-zA-Z]*\s*```/g, '')
76+
return cleaned.replace(/\n{3,}/g, '\n\n').trim()
77+
}
5078
}

‎apps/editor/src/views/prompt/backend/utils/symbols/git/replace-git-symbols.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
22
import { execSync } from 'child_process'
33
import { get_git_repository } from '@/utils/git-repository-utils'
4+
import { AsciiTree } from '@/utils/ascii-tree/ascii-tree'
45
import { Logger } from '@shared/utils/logger'
56
import { dictionary } from '@shared/constants/dictionary'
67
import { t } from '@/i18n'
@@ -329,7 +330,8 @@ const build_commit_changes_markdown = (
329330
diff: string,
330331
cwd: string,
331332
commit_hash: string,
332-
path_prefix?: string
333+
path_prefix?: string,
334+
commit_message?: string
333335
): string => {
334336
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
335337

@@ -408,7 +410,8 @@ const build_commit_changes_markdown = (
408410
if (changes_content) {
409411
const short_hash = commit_hash.substring(0, 7)
410412
const title_text = `Commit ${short_hash}`
411-
return `# ${title_text}\n\n${changes_content}`
413+
const msg_text = commit_message ? `${commit_message}\n\n` : ''
414+
return `# ${title_text}\n\n${msg_text}${changes_content}`
412415
}
413416
return ''
414417
}
@@ -464,11 +467,27 @@ export const replace_commit_symbol = async (params: {
464467
continue
465468
}
466469

470+
let commit_message_body = ''
471+
try {
472+
const raw_msg = execSync(`git show -s --format=%B ${commit_hash}`, {
473+
cwd: target_folder.uri.fsPath,
474+
encoding: 'utf-8'
475+
}).toString()
476+
commit_message_body = AsciiTree.strip_from_text(raw_msg)
477+
} catch (error) {
478+
Logger.warn({
479+
function_name: 'replace_commit_symbol',
480+
message: `Failed to read commit message for ${commit_hash}`,
481+
data: error
482+
})
483+
}
484+
467485
const replacement_text = build_commit_changes_markdown(
468486
diff,
469487
target_folder.uri.fsPath,
470488
commit_hash,
471-
workspace_folders.length > 1 ? folder_name : undefined
489+
workspace_folders.length > 1 ? folder_name : undefined,
490+
commit_message_body
472491
)
473492
commit_definitions += replacement_text
474493

0 commit comments

Comments
 (0)