Skip to content

Commit 5e4aebf

Browse files
committed
Implement binary file detection for workspace and commit generation utilities and remove obsolete ignore patterns
1 parent c05e867 commit 5e4aebf

7 files changed

Lines changed: 75 additions & 86 deletions

File tree

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const build_commit_message_prompt = async (
3131
let final_diff_content = full_file_diff
3232
let file_path: string | undefined
3333
let is_deleted = false
34+
const is_binary = lines.some((l) => l.startsWith('Binary files '))
3435

3536
if (new_path && new_path != '/dev/null') {
3637
file_path = new_path
@@ -75,11 +76,19 @@ export const build_commit_message_prompt = async (
7576
}
7677
}
7778

78-
const hunk_start_index = final_diff_content.indexOf('\n@@ ')
79-
if (hunk_start_index != -1) {
80-
final_diff_content = final_diff_content.substring(hunk_start_index + 1)
81-
} else if (!final_diff_content.startsWith('@@ ')) {
82-
final_diff_content = ''
79+
if (is_binary) {
80+
if (status == 'created') final_diff_content = 'Binary file created'
81+
else if (status == 'deleted') final_diff_content = 'Binary file deleted'
82+
else final_diff_content = 'Binary file modified'
83+
} else {
84+
const hunk_start_index = final_diff_content.indexOf('\n@@ ')
85+
if (hunk_start_index != -1) {
86+
final_diff_content = final_diff_content.substring(
87+
hunk_start_index + 1
88+
)
89+
} else if (!final_diff_content.startsWith('@@ ')) {
90+
final_diff_content = ''
91+
}
8392
}
8493

8594
changes_content += `<file path="${file_path}" status="${status}"`
@@ -89,17 +98,19 @@ export const build_commit_message_prompt = async (
8998
changes_content += '>\n'
9099
changes_content += `<![CDATA[\n${final_diff_content.trimEnd()}\n]]>\n`
91100

92-
if (!is_deleted && file_path) {
101+
if (!is_deleted && file_path && !is_binary) {
93102
try {
94103
let full_content = ''
95104
try {
96105
full_content = await repository.show('', file_path)
97106
} catch (e) {
98107
const uri = vscode.Uri.joinPath(repository.rootUri, file_path)
99108
const content = await vscode.workspace.fs.readFile(uri)
100-
full_content = Buffer.from(content).toString('utf8')
109+
if (!content.includes(0)) {
110+
full_content = Buffer.from(content).toString('utf8')
111+
}
101112
}
102-
if (full_content) {
113+
if (full_content && !full_content.includes('\0')) {
103114
const full_content_tokens = Math.ceil(full_content.length / 4)
104115
if (full_content_tokens <= MAX_FILE_TOKENS_FOR_COMMIT_MESSAGE) {
105116
changes_content += `<![CDATA[\n${full_content.trimEnd()}\n]]>\n`

apps/editor/src/constants/ignore-patterns.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

apps/editor/src/context/context-initialization.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,6 @@ export const context_initialization = async (
252252
const file_uri = vscode.Uri.file(file_path)
253253
const content_uint8_array =
254254
await vscode.workspace.fs.readFile(file_uri)
255-
let content = new TextDecoder().decode(content_uint8_array)
256-
257-
const range = workspace_provider.get_range(file_path)
258-
if (range) {
259-
content = workspace_provider.apply_range_to_content(
260-
content,
261-
range
262-
)
263-
}
264255

265256
let display_path: string
266257
const workspace_folder =
@@ -276,6 +267,24 @@ export const context_initialization = async (
276267
display_path = vscode.workspace.asRelativePath(file_path)
277268
}
278269

270+
if (content_uint8_array.includes(0)) {
271+
context_text += `<file path="${display_path.replace(
272+
/\\/g,
273+
'/'
274+
)}">\nBinary file\n</file>\n`
275+
continue
276+
}
277+
278+
let content = new TextDecoder().decode(content_uint8_array)
279+
280+
const range = workspace_provider.get_range(file_path)
281+
if (range) {
282+
content = workspace_provider.apply_range_to_content(
283+
content,
284+
range
285+
)
286+
}
287+
279288
context_text += `<file path="${display_path.replace(
280289
/\\/g,
281290
'/'

apps/editor/src/context/providers/workspace/modules/token-calculator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,12 @@ export class TokenCalculator implements vscode.Disposable {
305305
}
306306

307307
try {
308-
let content = await fs.promises.readFile(file_path, 'utf8')
308+
const buffer = await fs.promises.readFile(file_path)
309+
const is_binary = buffer.includes(0)
309310

310-
if (range) {
311+
let content = is_binary ? 'Binary file' : buffer.toString('utf8')
312+
313+
if (range && !is_binary) {
311314
content = this._provider.apply_range_to_content(content, range)
312315
}
313316

apps/editor/src/context/providers/workspace/workspace-provider.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
88
RANGES_STATE_KEY
99
} from '@/constants/state-keys'
10-
import { IGNORE_PATTERNS } from '@/constants/ignore-patterns'
1110
import { natural_sort } from '@/utils/natural-sort'
1211
import { dictionary } from '@shared/constants/dictionary'
1312
import { Logger } from '@shared/utils/logger'
@@ -1613,7 +1612,6 @@ export class WorkspaceProvider
16131612
this._user_ignore_patterns.add(patterns)
16141613
}
16151614

1616-
this._user_ignore_patterns.add(IGNORE_PATTERNS)
16171615
this._user_ignore_patterns.add('node_modules')
16181616

16191617
const allow_patterns = config.get<string[]>('allowPatterns')

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ export const prepare_staged_changes = async (
128128
// UNTRACKED
129129
status = 'created'
130130
const content = await vscode.workspace.fs.readFile(change.uri)
131-
full_content = Buffer.from(content).toString('utf8')
132-
const lines = full_content.split('\n')
133-
final_diff_content =
134-
`@@ -0,0 +1,${lines.length} @@\n` +
135-
lines.map((l: string) => '+' + l).join('\n')
131+
if (content.includes(0)) {
132+
final_diff_content = 'Binary file added'
133+
} else {
134+
full_content = Buffer.from(content).toString('utf8')
135+
const lines = full_content.split('\n')
136+
final_diff_content =
137+
`@@ -0,0 +1,${lines.length} @@\n` +
138+
lines.map((l: string) => '+' + l).join('\n')
139+
}
136140
} else {
137141
const raw_diff = execSync(`git diff -- "${change.uri.fsPath}"`, {
138142
cwd: repository.rootUri.fsPath
@@ -152,17 +156,30 @@ export const prepare_staged_changes = async (
152156
status = 'created'
153157
}
154158

155-
const hunk_start_index = raw_diff.indexOf('\n@@ ')
156-
if (hunk_start_index !== -1) {
157-
final_diff_content = raw_diff.substring(hunk_start_index + 1)
158-
} else if (raw_diff.startsWith('@@ ')) {
159-
final_diff_content = raw_diff
159+
if (
160+
raw_diff.includes('\nBinary files ') ||
161+
raw_diff.startsWith('Binary files ')
162+
) {
163+
final_diff_content = 'Binary file modified'
164+
} else {
165+
const hunk_start_index = raw_diff.indexOf('\n@@ ')
166+
if (hunk_start_index !== -1) {
167+
final_diff_content = raw_diff.substring(
168+
hunk_start_index + 1
169+
)
170+
} else if (raw_diff.startsWith('@@ ')) {
171+
final_diff_content = raw_diff
172+
}
173+
174+
try {
175+
const content = await vscode.workspace.fs.readFile(
176+
change.uri
177+
)
178+
if (!content.includes(0)) {
179+
full_content = Buffer.from(content).toString('utf8')
180+
}
181+
} catch (e) {}
160182
}
161-
162-
try {
163-
const content = await vscode.workspace.fs.readFile(change.uri)
164-
full_content = Buffer.from(content).toString('utf8')
165-
} catch (e) {}
166183
}
167184
}
168185
} catch (e) {}

apps/editor/src/views/panel/backend/message-handlers/handle-commit-changes/utils/file-utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as path from 'path'
33
import * as fs from 'fs'
44
import { Logger } from '@shared/utils/logger'
55
import { GitRepository } from '@/utils/git-repository-utils'
6-
import { IGNORE_PATTERNS } from '@/constants/ignore-patterns'
76
import ignore from 'ignore'
87

98
export interface FileData {
@@ -23,7 +22,7 @@ export const collect_affected_files_with_metadata = async (params: {
2322
const files_data: FileData[] = []
2423
const config = vscode.workspace.getConfiguration('codeWebChat')
2524
const user_ignore_patterns = config.get<string[]>('ignorePatterns') ?? []
26-
const ig = ignore().add(user_ignore_patterns).add(IGNORE_PATTERNS)
25+
const ig = ignore().add(user_ignore_patterns)
2726

2827
for (const change of staged_files) {
2928
const file_path = change.uri.fsPath

0 commit comments

Comments
 (0)