Skip to content

Commit 75de393

Browse files
authored
recover (#296)
1 parent 2e11a64 commit 75de393

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

crates/chat-cli/src/cli/chat/tools/fs_write.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,11 @@ impl FsWrite {
172172
match self {
173173
FsWrite::Create { path, .. } => {
174174
let file_text = self.canonical_create_command_text();
175-
let relative_path = format_path(cwd, path);
176-
let prev = if ctx.fs.exists(path) {
177-
let file = ctx.fs.read_to_string_sync(path)?;
178-
stylize_output_if_able(ctx, path, &file)
175+
let path = sanitize_path_tool_arg(ctx, path);
176+
let relative_path = format_path(cwd, &path);
177+
let prev = if ctx.fs.exists(&path) {
178+
let file = ctx.fs.read_to_string_sync(&path)?;
179+
stylize_output_if_able(ctx, &path, &file)
179180
} else {
180181
Default::default()
181182
};
@@ -188,8 +189,9 @@ impl FsWrite {
188189
insert_line,
189190
new_str,
190191
} => {
191-
let relative_path = format_path(cwd, path);
192-
let file = ctx.fs.read_to_string_sync(&relative_path)?;
192+
let path = sanitize_path_tool_arg(ctx, path);
193+
let relative_path = format_path(cwd, &path);
194+
let file = ctx.fs.read_to_string_sync(&path)?;
193195

194196
// Diff the old with the new by adding extra context around the line being inserted
195197
// at.
@@ -207,8 +209,9 @@ impl FsWrite {
207209
Ok(())
208210
},
209211
FsWrite::StrReplace { path, old_str, new_str } => {
210-
let relative_path = format_path(cwd, path);
211-
let file = ctx.fs.read_to_string_sync(&relative_path)?;
212+
let path = sanitize_path_tool_arg(ctx, path);
213+
let relative_path = format_path(cwd, &path);
214+
let file = ctx.fs.read_to_string_sync(&path)?;
212215
let (start_line, _) = match line_number_at(&file, old_str) {
213216
Some((start_line, end_line)) => (start_line, end_line),
214217
_ => (0, 0),
@@ -220,8 +223,9 @@ impl FsWrite {
220223
Ok(())
221224
},
222225
FsWrite::Append { path, new_str } => {
223-
let relative_path = format_path(cwd, path);
224-
let start_line = ctx.fs.read_to_string_sync(&relative_path)?.lines().count() + 1;
226+
let path = sanitize_path_tool_arg(ctx, path);
227+
let relative_path = format_path(cwd, &path);
228+
let start_line = ctx.fs.read_to_string_sync(&path)?.lines().count() + 1;
225229
let file = stylize_output_if_able(ctx, &relative_path, new_str);
226230
print_diff(output, &Default::default(), &file, start_line)?;
227231
Ok(())

0 commit comments

Comments
 (0)