Apply a stash to the working copy: restore bytes and recorded deletions. Pure filesystem operation — no graph interaction. Returns the applied paths.
(
&self,
reference: Option<&str>,
)
| 477 | /// Pure filesystem operation — no graph interaction. Returns the |
| 478 | /// applied paths. |
| 479 | pub fn stash_apply( |
| 480 | &self, |
| 481 | reference: Option<&str>, |
| 482 | ) -> Result<(StashEntry, Vec<String>), String> { |
| 483 | let stash = self.stash_resolve(reference)?; |
| 484 | let stash_dir = self.dot_dir().join("stashes").join(&stash.view_name); |
| 485 | let manifest_path = stash_dir.join("MANIFEST"); |
| 486 | if !manifest_path.exists() { |
| 487 | return Err(format!( |
| 488 | "stash sidecar not found: {}", |
| 489 | manifest_path.display() |
| 490 | )); |
| 491 | } |
| 492 | let manifest = std::fs::read_to_string(&manifest_path) |
| 493 | .map_err(|e| format!("failed to read stash manifest: {e}"))?; |
| 494 | |
| 495 | let manifest = if let Some(json) = manifest.strip_prefix(MANIFEST_V2) { |
| 496 | serde_json::from_str::<StashManifest>(json) |
| 497 | .map_err(|e| format!("invalid stash manifest: {e}"))? |
| 498 | } else { |
| 499 | StashManifest { |
| 500 | files: manifest |
| 501 | .lines() |
| 502 | .map(str::trim) |
| 503 | .filter(|p| !p.is_empty()) |
| 504 | .map(str::to_string) |
| 505 | .collect(), |
| 506 | deleted: Vec::new(), |
| 507 | } |
| 508 | }; |
| 509 | |
| 510 | // Validate every deletion before modifying files. A failed apply must |
| 511 | // leave the stash available for recovery, rather than let pop drop it. |
| 512 | let mut deletions = Vec::new(); |
| 513 | for deletion in &manifest.deleted { |
| 514 | let dst = deletion_path(self.root(), &deletion.path)?; |
| 515 | match std::fs::read(&dst) { |
| 516 | Ok(content) if Hash::of(&content).to_base32() == deletion.recorded_hash => {} |
| 517 | Ok(_) => { |
| 518 | let path = &deletion.path; |
| 519 | return Err(format!( |
| 520 | "cannot restore stashed deletion of '{path}': file changed since it was stashed" |
| 521 | )); |
| 522 | } |
| 523 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} |
| 524 | Err(e) => { |
| 525 | return Err(format!( |
| 526 | "cannot restore stashed deletion of '{}': {e}", |
| 527 | deletion.path |
| 528 | )) |
| 529 | } |
| 530 | } |
| 531 | deletions.push((deletion.path.clone(), dst)); |
| 532 | } |
| 533 | |
| 534 | let mut applied: Vec<String> = Vec::new(); |
| 535 | for path in &manifest.files { |
| 536 | let src = stash_entry_path(&stash_dir, path)?; |