MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / compute_diff

Method compute_diff

crates/opencode-session/src/revert.rs:221–294  ·  view source on GitHub ↗

Compute file diffs from session messages. Mirrors the TS `SessionSummary.computeDiff` behavior: 1. Scans messages for snapshot hashes stored in metadata (keys `"step_start_snapshot"` / `"step_finish_snapshot"`). If both an earliest "from" and a latest "to" snapshot are found, delegates to `Snapshot::diff_full` (git diff between two refs). 2. Falls back to aggregating `Patch` parts by filepath, co

(&self, messages: &[crate::SessionMessage])

Source from the content-addressed store, hash-verified

219 /// 2. Falls back to aggregating `Patch` parts by filepath, counting
220 /// per-line additions and deletions from `old_string` / `new_string`.
221 pub fn compute_diff(&self, messages: &[crate::SessionMessage]) -> Result<Vec<FileDiff>> {
222 // --- Strategy 1: snapshot-based diff (matches TS step-start / step-finish) ---
223 let mut from_snapshot: Option<String> = None;
224 let mut to_snapshot: Option<String> = None;
225
226 for msg in messages {
227 // Check message-level metadata for snapshot hashes
228 if from_snapshot.is_none() {
229 if let Some(serde_json::Value::String(s)) = msg.metadata.get("step_start_snapshot")
230 {
231 if !s.is_empty() {
232 from_snapshot = Some(s.clone());
233 }
234 }
235 }
236 if let Some(serde_json::Value::String(s)) = msg.metadata.get("step_finish_snapshot") {
237 if !s.is_empty() {
238 to_snapshot = Some(s.clone());
239 }
240 }
241
242 // Also scan parts: StepStart / StepFinish carry id+name / id+output,
243 // but the metadata on the *message* is the canonical place for snapshots
244 // in the v1 format. We also check part-level metadata stored in the
245 // message metadata under "snapshot" as a generic key.
246 if from_snapshot.is_none() {
247 if let Some(serde_json::Value::String(s)) = msg.metadata.get("snapshot") {
248 if !s.is_empty() {
249 from_snapshot = Some(s.clone());
250 }
251 }
252 }
253 }
254
255 if let (Some(ref from), Some(ref to)) = (&from_snapshot, &to_snapshot) {
256 let diffs = Snapshot::diff_full(&self.worktree, from, to)?;
257 if !diffs.is_empty() {
258 return Ok(diffs.into_iter().map(FileDiff::from).collect());
259 }
260 }
261
262 // --- Strategy 2: aggregate Patch parts ---
263 let mut file_stats: HashMap<String, (u64, u64)> = HashMap::new();
264
265 for msg in messages {
266 for part in &msg.parts {
267 if let crate::PartType::Patch {
268 ref old_string,
269 ref new_string,
270 ref filepath,
271 } = part.part_type
272 {
273 let (additions, deletions) = count_line_changes(old_string, new_string);
274 let entry = file_stats.entry(filepath.clone()).or_insert((0, 0));
275 entry.0 += additions;
276 entry.1 += deletions;
277 }
278 }

Callers

nothing calls this directly

Calls 5

newFunction · 0.85
count_line_changesFunction · 0.85
is_emptyMethod · 0.80
getMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected