Repository navigation
Expand file tree
/
Copy pathgit_snapshot.rs
More file actions
242 lines (200 loc) · 7.77 KB
/
Copy pathgit_snapshot.rs
File metadata and controls
242 lines (200 loc) · 7.77 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
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
//! Git-based snapshot for fast undo/redo tracking.
//!
//! Uses git's plumbing commands (write-tree, read-tree) for instant snapshots.
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::error::{CortexError, Result};
/// Git-based snapshot manager.
pub struct GitSnapshot {
/// Path to the separate git directory for snapshots.
git_dir: PathBuf,
/// Working tree (project directory).
work_tree: PathBuf,
}
impl GitSnapshot {
/// Create a new git snapshot manager.
pub fn new(cortex_home: &Path, project_id: &str, work_tree: PathBuf) -> Result<Self> {
let git_dir = cortex_home.join("snapshots").join("git").join(project_id);
Ok(Self { git_dir, work_tree })
}
/// Initialize the snapshot git repository if needed.
pub fn init(&self) -> Result<()> {
if !self.git_dir.exists() {
std::fs::create_dir_all(&self.git_dir)?;
// Initialize bare-ish git repo
let output = Command::new("git")
.arg("init")
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.output()?;
if !output.status.success() {
tracing::warn!(
"Failed to init snapshot git: {}",
String::from_utf8_lossy(&output.stderr)
);
}
// Disable autocrlf for consistent snapshots on Windows
let _ = Command::new("git")
.args(["config", "core.autocrlf", "false"])
.env("GIT_DIR", &self.git_dir)
.output();
tracing::info!("Initialized git snapshot at {:?}", self.git_dir);
}
Ok(())
}
/// Track current state and return a snapshot hash.
/// This is very fast because it uses git's index.
pub fn track(&self) -> Result<String> {
self.init()?;
// Stage all files (respects .gitignore automatically)
let add_output = Command::new("git")
.args(["add", "."])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output()?;
if !add_output.status.success() {
tracing::warn!(
"git add failed: {}",
String::from_utf8_lossy(&add_output.stderr)
);
}
// Create tree object - this is instant!
let output = Command::new("git")
.arg("write-tree")
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output()?;
if !output.status.success() {
return Err(CortexError::Snapshot(format!(
"git write-tree failed: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
let hash = String::from_utf8_lossy(&output.stdout).trim().to_string();
tracing::debug!("Snapshot tracked: {}", hash);
Ok(hash)
}
/// Get list of files changed since a snapshot.
pub fn changed_files(&self, hash: &str) -> Result<Vec<PathBuf>> {
// Stage current state
let _ = Command::new("git")
.args(["add", "."])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output();
// Get diff
let output = Command::new("git")
.args(["diff", "--name-only", hash, "--", "."])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output()?;
if !output.status.success() {
return Ok(Vec::new());
}
let files: Vec<PathBuf> = String::from_utf8_lossy(&output.stdout)
.lines()
.filter(|s| !s.is_empty())
.map(|s| self.work_tree.join(s))
.collect();
Ok(files)
}
/// Get unified diff since a snapshot.
pub fn diff(&self, hash: &str) -> Result<String> {
// Stage current state
let _ = Command::new("git")
.args(["add", "."])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output();
let output = Command::new("git")
.args(["diff", "--no-ext-diff", hash, "--", "."])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output()?;
Ok(String::from_utf8_lossy(&output.stdout).to_string())
}
/// Restore files to a snapshot state.
pub fn restore(&self, hash: &str) -> Result<()> {
let output = Command::new("git")
.args(["read-tree", hash])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output()?;
if !output.status.success() {
return Err(CortexError::Snapshot(format!(
"git read-tree failed: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
let output = Command::new("git")
.args(["checkout-index", "-a", "-f"])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output()?;
if !output.status.success() {
return Err(CortexError::Snapshot(format!(
"git checkout-index failed: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
tracing::info!("Restored to snapshot: {}", hash);
Ok(())
}
/// Restore a specific file to a snapshot state.
pub fn restore_file(&self, hash: &str, file: &Path) -> Result<()> {
let relative = file.strip_prefix(&self.work_tree).unwrap_or(file);
let output = Command::new("git")
.args(["checkout", hash, "--", relative.to_string_lossy().as_ref()])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.current_dir(&self.work_tree)
.output()?;
if !output.status.success() {
// File might not exist in snapshot - check and delete if so
let check = Command::new("git")
.args(["ls-tree", hash, "--", relative.to_string_lossy().as_ref()])
.env("GIT_DIR", &self.git_dir)
.env("GIT_WORK_TREE", &self.work_tree)
.output()?;
if check.stdout.is_empty() {
// File didn't exist in snapshot, delete it
let _ = std::fs::remove_file(file);
tracing::debug!("Deleted file that didn't exist in snapshot: {:?}", file);
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_git_snapshot_basic() {
let temp = TempDir::new().unwrap();
let work_tree = temp.path().to_path_buf();
let cortex_home = temp.path().join(".cortex");
// Create a test file
std::fs::write(work_tree.join("test.txt"), "hello").unwrap();
let snapshot = GitSnapshot::new(&cortex_home, "test-project", work_tree.clone()).unwrap();
// Track initial state
let hash1 = snapshot.track().unwrap();
assert!(!hash1.is_empty());
// Modify file
std::fs::write(work_tree.join("test.txt"), "world").unwrap();
// Track new state
let hash2 = snapshot.track().unwrap();
assert_ne!(hash1, hash2);
// Check changed files
let changed = snapshot.changed_files(&hash1).unwrap();
assert!(changed.iter().any(|p| p.ends_with("test.txt")));
}
}