splice 2.6.2

Span-safe refactoring kernel for 7 languages with Magellan code graph integration
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Backup and undo support for Splice operations.
//!
//! This module provides the ability to create backups before patching
//! and restore from those backups later. Backups are stored in
//! `.splice-backup/<operation_id>/` directories with a manifest
//! tracking the original file locations and hashes.

use crate::error::{Result, SpliceError};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};

/// Metadata about a backed-up file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupEntry {
    /// Original path of the file (relative to workspace root).
    pub replaced_path: PathBuf,
    /// SHA-256 hash of the original file content.
    pub hash: String,
    /// Byte count of the original file.
    pub size: u64,
}

/// Manifest describing a backup operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupManifest {
    /// Unique identifier for this operation.
    pub operation_id: String,
    /// Timestamp when the backup was created (ISO 8601).
    pub timestamp: String,
    /// Files that were backed up.
    pub files: Vec<BackupEntry>,
    /// Absolute path to the backup directory.
    #[serde(skip)]
    pub backup_dir: PathBuf,
}

impl BackupManifest {
    /// Create a new backup manifest.
    pub fn new(operation_id: String, backup_dir: PathBuf) -> Self {
        let timestamp = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
        BackupManifest {
            operation_id,
            timestamp,
            files: Vec::new(),
            backup_dir,
        }
    }

    /// Add a file entry to the manifest.
    pub fn add_file(&mut self, replaced_path: PathBuf, hash: String, size: u64) {
        self.files.push(BackupEntry {
            replaced_path,
            hash,
            size,
        });
    }

    /// Save the manifest to a file in the backup directory.
    pub fn save(&self) -> Result<()> {
        let manifest_path = self.backup_dir.join("manifest.json");
        let json = serde_json::to_string_pretty(self)
            .map_err(|e| SpliceError::Other(format!("Failed to serialize manifest: {}", e)))?;
        fs::write(&manifest_path, json).map_err(|e| SpliceError::Io {
            path: manifest_path,
            source: e,
        })?;
        Ok(())
    }

    /// Load a manifest from a file.
    pub fn load(manifest_path: &Path) -> Result<Self> {
        let json = fs::read_to_string(manifest_path).map_err(|e| SpliceError::Io {
            path: manifest_path.to_path_buf(),
            source: e,
        })?;

        let mut manifest: BackupManifest = serde_json::from_str(&json)
            .map_err(|e| SpliceError::Other(format!("Failed to parse manifest: {}", e)))?;

        manifest.backup_dir = manifest_path
            .parent()
            .ok_or_else(|| SpliceError::Other("Manifest has no parent directory".to_string()))?
            .to_path_buf();

        Ok(manifest)
    }
}

/// Writer for creating backups of files before patching.
pub struct BackupWriter {
    manifest: BackupManifest,
    workspace_root: PathBuf,
}

impl BackupWriter {
    /// Create a new backup writer.
    ///
    /// # Arguments
    /// * `workspace_root` - Root directory of the workspace
    /// * `operation_id` - Unique identifier for the operation (or UUID v4 if None)
    pub fn new(workspace_root: &Path, operation_id: Option<String>) -> Result<Self> {
        let op_id = operation_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

        let backup_dir = workspace_root.join(".splice-backup").join(&op_id);

        // Create backup directory
        fs::create_dir_all(&backup_dir).map_err(|e| SpliceError::Io {
            path: backup_dir.clone(),
            source: e,
        })?;

        let manifest = BackupManifest::new(op_id, backup_dir);

        Ok(BackupWriter {
            manifest,
            workspace_root: workspace_root.to_path_buf(),
        })
    }

    /// Get the operation ID for this backup.
    pub fn operation_id(&self) -> &str {
        &self.manifest.operation_id
    }

    /// Get the path to the manifest file.
    pub fn manifest_path(&self) -> PathBuf {
        self.manifest.backup_dir.join("manifest.json")
    }

    /// Backup a single file.
    ///
    /// The file is copied to the backup directory with its original
    /// filename preserved (to avoid collisions, files with the same
    /// name from different directories are stored in subdirectories).
    pub fn backup_file(&mut self, file_path: &Path) -> Result<()> {
        // Read original file
        let content = fs::read(file_path).map_err(|e| SpliceError::Io {
            path: file_path.to_path_buf(),
            source: e,
        })?;

        // Compute hash
        let hash = compute_hash(&content);
        let size = content.len() as u64;

        // Compute relative path from workspace root
        let relative = file_path.strip_prefix(&self.workspace_root).map_err(|_| {
            SpliceError::Other(format!(
                "File '{}' is not under workspace root '{}'",
                file_path.display(),
                self.workspace_root.display()
            ))
        })?;

        // Create backup path preserving directory structure
        let backup_path = self.manifest.backup_dir.join(relative);

        // Create parent directories if needed
        if let Some(parent) = backup_path.parent() {
            fs::create_dir_all(parent).map_err(|e| SpliceError::Io {
                path: parent.to_path_buf(),
                source: e,
            })?;
        }

        // Copy file to backup location
        fs::write(&backup_path, &content).map_err(|e| SpliceError::Io {
            path: backup_path.clone(),
            source: e,
        })?;

        // Add entry to manifest
        self.manifest.add_file(relative.to_path_buf(), hash, size);

        Ok(())
    }

    /// Finalize the backup by writing the manifest file.
    ///
    /// Returns the path to the manifest file.
    pub fn finalize(self) -> Result<PathBuf> {
        self.manifest.save()?;
        Ok(self.manifest_path())
    }

    /// Get the backup directory path.
    pub fn backup_dir(&self) -> &Path {
        &self.manifest.backup_dir
    }
}

/// Restore files from a backup manifest.
pub fn restore_from_manifest(manifest_path: &Path, workspace_root: &Path) -> Result<usize> {
    let manifest = BackupManifest::load(manifest_path)?;

    let mut restored = 0;

    for entry in &manifest.files {
        let replaced_path = workspace_root.join(&entry.replaced_path);
        let backup_path = manifest.backup_dir.join(&entry.replaced_path);

        // Verify backup file exists
        if !backup_path.exists() {
            return Err(SpliceError::Other(format!(
                "Backup file missing: {}",
                backup_path.display()
            )));
        }

        // Read backup content
        let content = fs::read(&backup_path).map_err(|e| SpliceError::Io {
            path: backup_path.clone(),
            source: e,
        })?;

        // Verify hash matches
        let actual_hash = compute_hash(&content);
        if actual_hash != entry.hash {
            return Err(SpliceError::Other(format!(
                "Hash mismatch for {}: expected {}, got {}",
                entry.replaced_path.display(),
                entry.hash,
                actual_hash
            )));
        }

        // Create parent directory if needed
        if let Some(parent) = replaced_path.parent() {
            fs::create_dir_all(parent).map_err(|e| SpliceError::Io {
                path: parent.to_path_buf(),
                source: e,
            })?;
        }

        // Write to original location
        fs::write(&replaced_path, &content).map_err(|e| SpliceError::Io {
            path: replaced_path.clone(),
            source: e,
        })?;

        restored += 1;
    }

    Ok(restored)
}

/// Compute SHA-256 hash of bytes.
fn compute_hash(bytes: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(bytes);
    let result = hasher.finalize();
    format!("{:x}", result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_backup_writer_creates_manifest() {
        let workspace = TempDir::new().expect("Failed to create temp dir");
        let workspace_root = workspace.path();

        // Create a test file
        let test_file = workspace_root.join("test.txt");
        fs::write(&test_file, b"hello world").expect("Failed to write test file");

        // Create backup
        let mut writer = BackupWriter::new(workspace_root, Some("test-op-123".to_string()))
            .expect("Failed to create BackupWriter");

        writer
            .backup_file(&test_file)
            .expect("Failed to backup file");

        let manifest_path = writer.finalize().expect("Failed to finalize backup");

        // Verify manifest exists
        assert!(manifest_path.exists(), "Manifest file should exist");

        // Verify backup file exists
        let backup_file = workspace_root.join(".splice-backup/test-op-123/test.txt");
        assert!(backup_file.exists(), "Backup file should exist");

        // Verify content matches
        let backup_content = fs::read_to_string(&backup_file).expect("Failed to read backup file");
        assert_eq!(backup_content, "hello world");
    }

    #[test]
    fn test_restore_from_manifest_restores_files() {
        let workspace = TempDir::new().expect("Failed to create temp dir");
        let workspace_root = workspace.path();

        // Create test files
        let test_file = workspace_root.join("test.txt");
        fs::write(&test_file, b"replaced content").expect("Failed to write test file");

        // Create backup
        let mut writer = BackupWriter::new(workspace_root, Some("restore-test".to_string()))
            .expect("Failed to create BackupWriter");

        writer
            .backup_file(&test_file)
            .expect("Failed to backup file");

        let manifest_path = writer.finalize().expect("Failed to finalize backup");

        // Modify original file
        fs::write(&test_file, b"modified content").expect("Failed to modify file");

        // Restore from backup
        let restored =
            restore_from_manifest(&manifest_path, workspace_root).expect("Failed to restore");

        assert_eq!(restored, 1, "Should restore one file");

        // Verify content was restored
        let content = fs::read_to_string(&test_file).expect("Failed to read file");
        assert_eq!(content, "replaced content", "Content should be restored");
    }

    #[test]
    fn test_restore_hash_mismatch_fails() {
        let workspace = TempDir::new().expect("Failed to create temp dir");
        let workspace_root = workspace.path();

        // Create test file
        let test_file = workspace_root.join("test.txt");
        fs::write(&test_file, b"replaced").expect("Failed to write test file");

        // Create backup
        let mut writer = BackupWriter::new(workspace_root, Some("hash-test".to_string()))
            .expect("Failed to create BackupWriter");

        writer
            .backup_file(&test_file)
            .expect("Failed to backup file");

        let manifest_path = writer.finalize().expect("Failed to finalize backup");

        // Tamper with the backup file
        let backup_file = workspace_root.join(".splice-backup/hash-test/test.txt");
        fs::write(&backup_file, b"tampered").expect("Failed to tamper with backup");

        // Attempt to restore should fail due to hash mismatch
        let result = restore_from_manifest(&manifest_path, workspace_root);

        assert!(result.is_err(), "Restore should fail on hash mismatch");
        match result {
            Err(SpliceError::Other(msg)) if msg.contains("Hash mismatch") => {
                // Expected
            }
            other => {
                panic!("Expected hash mismatch error, got: {:?}", other);
            }
        }
    }

    #[test]
    fn test_backup_with_subdirectories() {
        let workspace = TempDir::new().expect("Failed to create temp dir");
        let workspace_root = workspace.path();

        // Create directory structure
        let src_dir = workspace_root.join("src");
        fs::create_dir(&src_dir).expect("Failed to create src dir");

        let test_file = src_dir.join("lib.rs");
        fs::write(&test_file, b"fn main() {}").expect("Failed to write test file");

        // Create backup
        let mut writer = BackupWriter::new(workspace_root, Some("subdir-test".to_string()))
            .expect("Failed to create BackupWriter");

        writer
            .backup_file(&test_file)
            .expect("Failed to backup file");

        let manifest_path = writer.finalize().expect("Failed to finalize backup");

        // Verify backup preserves directory structure
        let backup_file = workspace_root.join(".splice-backup/subdir-test/src/lib.rs");
        assert!(
            backup_file.exists(),
            "Backup should preserve directory structure"
        );

        // Verify can be restored
        fs::write(&test_file, b"modified").expect("Failed to modify");
        let restored =
            restore_from_manifest(&manifest_path, workspace_root).expect("Failed to restore");

        assert_eq!(restored, 1);
        let content = fs::read_to_string(&test_file).expect("Failed to read");
        assert_eq!(content, "fn main() {}");
    }

    #[test]
    fn test_manifest_save_and_load() {
        let workspace = TempDir::new().expect("Failed to create temp dir");
        let workspace_root = workspace.path();

        let mut manifest = BackupManifest::new(
            "test-manifest".to_string(),
            workspace_root.join(".splice-backup").join("test-manifest"),
        );

        manifest.add_file(PathBuf::from("src/lib.rs"), "abc123".to_string(), 1024);

        let manifest_path = workspace_root.join(".splice-backup/test-manifest/manifest.json");

        // Save
        if let Some(parent) = manifest_path.parent() {
            fs::create_dir_all(parent).expect("Failed to create dir");
        } else {
            fs::create_dir_all(&manifest_path).expect("Failed to create manifest dir");
        }
        manifest.save().expect("Failed to save manifest");

        // Load
        let loaded = BackupManifest::load(&manifest_path).expect("Failed to load manifest");

        assert_eq!(loaded.operation_id, "test-manifest");
        assert_eq!(loaded.files.len(), 1);
        assert_eq!(loaded.files[0].replaced_path, PathBuf::from("src/lib.rs"));
        assert_eq!(loaded.files[0].hash, "abc123");
        assert_eq!(loaded.files[0].size, 1024);
    }
}