Skip to main content

vtcode_core/tools/editing/patch/
mod.rs

1use std::path::Path;
2
3use anyhow::anyhow;
4
5mod applicator;
6mod error;
7mod matcher;
8mod parser;
9mod path;
10mod semantic;
11
12pub use error::PatchError;
13#[doc(hidden)]
14pub(crate) use semantic::resolve_ast_grep_binary_path;
15#[doc(hidden)]
16pub use semantic::{AstGrepBinaryOverrideGuard, set_ast_grep_binary_override_for_tests};
17
18/// Represents a single diff line inside a patch hunk.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum PatchLine {
21    Context(String),
22    Addition(String),
23    Removal(String),
24}
25
26impl PatchLine {
27    pub fn as_str(&self) -> &str {
28        match self {
29            PatchLine::Context(text) | PatchLine::Addition(text) | PatchLine::Removal(text) => text,
30        }
31    }
32}
33
34/// Represents a chunk of changes within an update operation.
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub struct PatchChunk {
37    pub change_context: Option<String>,
38    pub lines: Vec<PatchLine>,
39    pub is_end_of_file: bool,
40}
41
42impl PatchChunk {
43    pub fn lines(&self) -> &[PatchLine] {
44        &self.lines
45    }
46
47    pub fn change_context(&self) -> Option<&str> {
48        self.change_context.as_deref()
49    }
50
51    pub fn is_end_of_file(&self) -> bool {
52        self.is_end_of_file
53    }
54
55    pub(crate) fn to_segments(&self) -> (Vec<String>, Vec<String>) {
56        let cap = self.lines.len();
57        let mut old_lines = Vec::with_capacity(cap);
58        let mut new_lines = Vec::with_capacity(cap);
59
60        for line in &self.lines {
61            match line {
62                PatchLine::Context(text) => {
63                    old_lines.push(text.clone());
64                    new_lines.push(text.clone());
65                }
66                PatchLine::Addition(text) => {
67                    new_lines.push(text.clone());
68                }
69                PatchLine::Removal(text) => {
70                    old_lines.push(text.clone());
71                }
72            }
73        }
74
75        (old_lines, new_lines)
76    }
77
78    pub(crate) fn has_old_lines(&self) -> bool {
79        self.lines
80            .iter()
81            .any(|line| matches!(line, PatchLine::Context(_) | PatchLine::Removal(_)))
82    }
83
84    pub fn parse_line_number(&self) -> Option<usize> {
85        let ctx = self.change_context()?;
86        // Format is typically: -old_start,old_count +new_start,new_count @@
87        let parts: Vec<&str> = ctx.split_whitespace().collect();
88        let old_part = if !parts.is_empty() && parts[0].starts_with('-') {
89            Some(parts[0])
90        } else if parts.len() >= 2 && parts[1].starts_with('-') {
91            Some(parts[1])
92        } else {
93            None
94        }?;
95
96        let range_str = old_part.strip_prefix('-')?;
97        let range_parts: Vec<&str> = range_str.split(',').collect();
98        let start_str = range_parts.first()?;
99        start_str.parse::<usize>().ok()
100    }
101}
102
103pub type PatchHunk = PatchChunk;
104
105/// Represents a patch operation.
106#[derive(Debug, Clone, PartialEq, Eq)]
107pub enum PatchOperation {
108    AddFile {
109        path: String,
110        content: String,
111    },
112    DeleteFile {
113        path: String,
114    },
115    UpdateFile {
116        path: String,
117        new_path: Option<String>,
118        chunks: Vec<PatchChunk>,
119    },
120}
121
122/// Represents a complete patch comprised of multiple operations.
123#[derive(Debug, Clone, PartialEq, Eq)]
124pub struct Patch {
125    operations: Vec<PatchOperation>,
126}
127
128impl Patch {
129    pub fn parse(input: &str) -> anyhow::Result<Self> {
130        let operations = parser::parse(input).map_err(|err| anyhow!(err))?;
131        Ok(Self { operations })
132    }
133
134    pub fn operations(&self) -> &[PatchOperation] {
135        &self.operations
136    }
137
138    pub fn is_empty(&self) -> bool {
139        self.operations.is_empty()
140    }
141
142    pub fn into_operations(self) -> Vec<PatchOperation> {
143        self.operations
144    }
145
146    pub async fn apply(&self, root: &Path) -> anyhow::Result<Vec<String>> {
147        applicator::apply(root, &self.operations)
148            .await
149            .map_err(|err| anyhow!(err))
150    }
151}
152
153pub async fn render_patch_update_content(
154    source_path: &Path,
155    content: &str,
156    chunks: &[PatchChunk],
157    path: &str,
158) -> anyhow::Result<String> {
159    applicator::render_updated_content(source_path, content, chunks, path)
160        .await
161        .map_err(|err| anyhow!(err))
162}
163
164#[cfg(test)]
165mod tests {
166    use super::*;
167    use tempfile::TempDir;
168
169    #[test]
170    fn parse_add_file() {
171        let patch = Patch::parse("*** Begin Patch\n*** Add File: hello.txt\n+hello\n*** End Patch")
172            .unwrap();
173        assert_eq!(patch.operations().len(), 1);
174        matches!(patch.operations()[0], PatchOperation::AddFile { .. });
175    }
176
177    #[tokio::test]
178    async fn apply_add_file() {
179        let temp_dir = TempDir::new().unwrap();
180        let patch =
181            Patch::parse("*** Begin Patch\n*** Add File: file.txt\n+content\n*** End Patch")
182                .unwrap();
183
184        let result = patch.apply(temp_dir.path()).await.unwrap();
185        assert_eq!(
186            result,
187            vec!["[1/1] Added file: file.txt (8 bytes)".to_string()]
188        );
189        let written = tokio::fs::read_to_string(temp_dir.path().join("file.txt"))
190            .await
191            .unwrap();
192        assert_eq!(written, "content\n");
193    }
194}