-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.rs
More file actions
178 lines (161 loc) · 5.82 KB
/
Copy pathpatch.rs
File metadata and controls
178 lines (161 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Unified diff patching: parse and apply patches.
use anyhow::{Context, Result};
use super::sandbox::Sandbox;
/// A parsed hunk from a unified diff.
pub(crate) struct DiffHunk {
pub(crate) file_path: String,
pub(crate) old_start: usize,
pub(crate) lines: Vec<(DiffLineType, String)>,
}
#[derive(PartialEq)]
pub(crate) enum DiffLineType {
Context,
Remove,
Add,
}
/// Parse a unified diff into hunks.
///
/// Supports the standard format:
/// --- a/file.rs
/// +++ b/file.rs
/// @@ -start,count +start,count @@
/// context line
/// -removed line
/// +added line
pub(crate) fn parse_unified_diff(text: &str) -> Vec<DiffHunk> {
let mut hunks = Vec::new();
let mut current_file: Option<String> = None;
let mut current_hunk: Option<DiffHunk> = None;
for line in text.lines() {
if let Some(rest) = line.strip_prefix("--- ") {
let path = rest.trim().strip_prefix("a/").unwrap_or(rest.trim());
current_file = Some(path.to_string());
} else if let Some(rest) = line.strip_prefix("+++ ") {
let path = rest.trim().strip_prefix("b/").unwrap_or(rest.trim());
current_file = Some(path.to_string());
} else if line.starts_with("@@ ") {
if let Some(h) = current_hunk.take() {
hunks.push(h);
}
let file = current_file.clone().unwrap_or_default();
let parts: Vec<&str> = line.split_whitespace().collect();
let old_start = parts
.get(1)
.and_then(|s| s.strip_prefix('-'))
.and_then(|s| s.split(',').next())
.and_then(|s| s.parse().ok())
.unwrap_or(1);
current_hunk = Some(DiffHunk {
file_path: file,
old_start,
lines: Vec::new(),
});
} else if let Some(h) = current_hunk.as_mut() {
if line.starts_with('-') && !line.starts_with("---") {
h.lines.push((DiffLineType::Remove, line[1..].to_string()));
} else if line.starts_with('+') && !line.starts_with("+++") {
h.lines.push((DiffLineType::Add, line[1..].to_string()));
} else if line.starts_with(' ') || line.is_empty() {
h.lines.push((
DiffLineType::Context,
line.trim_start_matches(' ').to_string(),
));
}
}
}
if let Some(h) = current_hunk.take() {
hunks.push(h);
}
hunks
}
/// Apply a single hunk to file content.
///
/// Finds the context lines in the file content starting at old_start,
/// replaces removed lines with added lines.
pub(crate) fn apply_hunk(content: &str, hunk: &DiffHunk) -> Result<String> {
let lines: Vec<&str> = content.lines().collect();
let start_idx = hunk.old_start.saturating_sub(1);
let mut expected: Vec<&str> = Vec::new();
let mut replacement: Vec<String> = Vec::new();
for (line_type, text) in &hunk.lines {
match line_type {
DiffLineType::Context => {
expected.push(text.as_str());
replacement.push(text.clone());
}
DiffLineType::Remove => {
expected.push(text.as_str());
}
DiffLineType::Add => {
replacement.push(text.clone());
}
}
}
if start_idx + expected.len() > lines.len() {
return Ok(format!(
"Error: patch context exceeds file length for {}",
hunk.file_path
));
}
for (i, expected_line) in expected.iter().enumerate() {
let file_line = lines[start_idx + i].trim_end();
let exp = expected_line.trim_end();
if file_line != exp {
return Ok(format!(
"Error: patch context mismatch in {} at line {}: expected {:?}, got {:?}",
hunk.file_path,
start_idx + i + 1,
exp,
file_line
));
}
}
let mut result = Vec::new();
for &line in &lines[..start_idx] {
result.push(line.to_string());
}
for line in &replacement {
result.push(line.clone());
}
for &line in &lines[start_idx + expected.len()..] {
result.push(line.to_string());
}
Ok(result.join("\n") + if content.ends_with('\n') { "\n" } else { "" })
}
impl Sandbox {
/// Apply a unified diff patch to files in the workspace.
///
/// Supports multiple hunks and files. Rejects if context lines don't match.
pub fn apply_patch(&self, patch_text: &str) -> Result<String> {
let hunks = parse_unified_diff(patch_text);
if hunks.is_empty() {
return Ok("Error: no valid hunks found in patch".into());
}
let mut changed_files = Vec::new();
for hunk in &hunks {
let path = self.safe_resolve(&hunk.file_path)?;
if !path.is_file() {
return Ok(format!(
"Error: {} is not a file (cannot patch)",
hunk.file_path
));
}
let content = std::fs::read_to_string(&path).context("read file for patch")?;
let new_content = apply_hunk(&content, hunk)?;
if new_content.starts_with("Error:") {
return Ok(new_content);
}
let mut bak_name = path.file_name().unwrap_or_default().to_os_string();
bak_name.push(".bak");
let bak_path = path.with_file_name(bak_name);
std::fs::copy(&path, &bak_path).context("create backup before patch")?;
std::fs::write(&path, new_content)?;
changed_files.push(hunk.file_path.clone());
}
Ok(format!(
"Patched {} file(s): {}",
changed_files.len(),
changed_files.join(", ")
))
}
}