Skip to content

Commit 9298b62

Browse files
fix: hash computation for externally modified files (#1979)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent f8b1581 commit 9298b62

File tree

13 files changed

+126
-67
lines changed

13 files changed

+126
-67
lines changed

crates/forge_app/src/changed_files.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mod tests {
8888

8989
use super::*;
9090
use crate::services::Content;
91-
use crate::{EnvironmentService, FsReadService, ReadOutput};
91+
use crate::{EnvironmentService, FsReadService, ReadOutput, compute_hash};
9292

9393
#[derive(Clone, Default)]
9494
struct TestServices {
@@ -106,11 +106,15 @@ mod tests {
106106
) -> anyhow::Result<ReadOutput> {
107107
self.files
108108
.get(&path)
109-
.map(|content| ReadOutput {
110-
content: Content::File(content.clone()),
111-
start_line: 1,
112-
end_line: 1,
113-
total_lines: 1,
109+
.map(|content| {
110+
let hash = compute_hash(content);
111+
ReadOutput {
112+
content: Content::file(content.clone()),
113+
start_line: 1,
114+
end_line: 1,
115+
total_lines: 1,
116+
content_hash: hash,
117+
}
114118
})
115119
.ok_or_else(|| anyhow::anyhow!(std::io::Error::from(std::io::ErrorKind::NotFound)))
116120
}

crates/forge_app/src/file_tracking.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ mod tests {
146146
start_line: 1,
147147
end_line: 1,
148148
total_lines: 1,
149+
content_hash: compute_hash(content),
149150
})
150151
} else {
151152
Err(anyhow::anyhow!(std::io::Error::from(

crates/forge_app/src/fmt/fmt_output.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ mod tests {
7979

8080
#[test]
8181
fn test_fs_read_single_line() {
82+
let content = "Hello, world!";
8283
let fixture = ToolOperation::FsRead {
8384
input: forge_domain::FSRead {
8485
path: "/home/user/test.txt".to_string(),
@@ -87,10 +88,11 @@ mod tests {
8788
show_line_numbers: true,
8889
},
8990
output: ReadOutput {
90-
content: Content::File("Hello, world!".to_string()),
91+
content: Content::file(content),
9192
start_line: 1,
9293
end_line: 1,
9394
total_lines: 5,
95+
content_hash: crate::compute_hash(content),
9496
},
9597
};
9698
let env = fixture_environment();
@@ -103,6 +105,7 @@ mod tests {
103105

104106
#[test]
105107
fn test_fs_read_multiple_lines() {
108+
let content = "Line 1\nLine 2\nLine 3";
106109
let fixture = ToolOperation::FsRead {
107110
input: forge_domain::FSRead {
108111
path: "/home/user/test.txt".to_string(),
@@ -111,10 +114,11 @@ mod tests {
111114
show_line_numbers: true,
112115
},
113116
output: ReadOutput {
114-
content: Content::File("Line 1\nLine 2\nLine 3".to_string()),
117+
content: Content::file(content),
115118
start_line: 2,
116119
end_line: 4,
117120
total_lines: 10,
121+
content_hash: crate::compute_hash(content),
118122
},
119123
};
120124
let env = fixture_environment();
@@ -127,16 +131,18 @@ mod tests {
127131

128132
#[test]
129133
fn test_fs_create_new_file() {
134+
let content = "New file content";
130135
let fixture = ToolOperation::FsCreate {
131136
input: forge_domain::FSWrite {
132137
path: "/home/user/project/new_file.txt".to_string(),
133-
content: "New file content".to_string(),
138+
content: content.to_string(),
134139
overwrite: false,
135140
},
136141
output: FsCreateOutput {
137142
path: "/home/user/project/new_file.txt".to_string(),
138143
before: None,
139144
warning: None,
145+
content_hash: crate::compute_hash(content),
140146
},
141147
};
142148
let env = fixture_environment();
@@ -149,16 +155,18 @@ mod tests {
149155

150156
#[test]
151157
fn test_fs_create_overwrite() {
158+
let content = "new content";
152159
let fixture = ToolOperation::FsCreate {
153160
input: forge_domain::FSWrite {
154161
path: "/home/user/project/existing_file.txt".to_string(),
155-
content: "new content".to_string(),
162+
content: content.to_string(),
156163
overwrite: true,
157164
},
158165
output: FsCreateOutput {
159166
path: "/home/user/project/existing_file.txt".to_string(),
160167
before: Some("old content".to_string()),
161168
warning: None,
169+
content_hash: crate::compute_hash(content),
162170
},
163171
};
164172
let env = fixture_environment();
@@ -175,16 +183,18 @@ mod tests {
175183

176184
#[test]
177185
fn test_fs_create_with_warning() {
186+
let content = "File content";
178187
let fixture = ToolOperation::FsCreate {
179188
input: forge_domain::FSWrite {
180189
path: "/home/user/project/file.txt".to_string(),
181-
content: "File content".to_string(),
190+
content: content.to_string(),
182191
overwrite: false,
183192
},
184193
output: FsCreateOutput {
185194
path: "/home/user/project/file.txt".to_string(),
186195
before: None,
187196
warning: Some("File created outside project directory".to_string()),
197+
content_hash: crate::compute_hash(content),
188198
},
189199
};
190200
let env = fixture_environment();
@@ -293,6 +303,7 @@ mod tests {
293303

294304
#[test]
295305
fn test_fs_patch_success() {
306+
let after_content = "Hello universe\nThis is a test\nNew line";
296307
let fixture = ToolOperation::FsPatch {
297308
input: forge_domain::FSPatch {
298309
path: "/home/user/project/test.txt".to_string(),
@@ -303,7 +314,8 @@ mod tests {
303314
output: PatchOutput {
304315
warning: None,
305316
before: "Hello world\nThis is a test".to_string(),
306-
after: "Hello universe\nThis is a test\nNew line".to_string(),
317+
after: after_content.to_string(),
318+
content_hash: crate::compute_hash(after_content),
307319
},
308320
};
309321
let env = fixture_environment();
@@ -314,6 +326,7 @@ mod tests {
314326

315327
#[test]
316328
fn test_fs_patch_with_warning() {
329+
let after_content = "line1\nnew line\nline2";
317330
let fixture = ToolOperation::FsPatch {
318331
input: forge_domain::FSPatch {
319332
path: "/home/user/project/large_file.txt".to_string(),
@@ -324,7 +337,8 @@ mod tests {
324337
output: PatchOutput {
325338
warning: Some("Large file modification".to_string()),
326339
before: "line1\nline2".to_string(),
327-
after: "line1\nnew line\nline2".to_string(),
340+
after: after_content.to_string(),
341+
content_hash: crate::compute_hash(after_content),
328342
},
329343
};
330344
let env = fixture_environment();

0 commit comments

Comments
 (0)