Repository navigation
Expand file tree
/
Copy pathghost_commit.rs
More file actions
412 lines (346 loc) · 12.5 KB
/
Copy pathghost_commit.rs
File metadata and controls
412 lines (346 loc) · 12.5 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
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
//! Ghost commit creation and management.
use crate::{GhostConfig, GhostError, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;
use tokio::process::Command;
use tracing::{debug, info, warn};
/// Default timeout for git operations in seconds
const DEFAULT_GIT_TIMEOUT_SECS: u64 = 30;
/// Get the configured git timeout duration
fn get_git_timeout() -> Duration {
std::env::var("CORTEX_GIT_TIMEOUT_SECS")
.ok()
.and_then(|s| s.parse::<u64>().ok())
.map(Duration::from_secs)
.unwrap_or(Duration::from_secs(DEFAULT_GIT_TIMEOUT_SECS))
}
/// Execute a git command with timeout
async fn git_command_with_timeout(args: &[&str], cwd: &PathBuf) -> Result<std::process::Output> {
let timeout = get_git_timeout();
let future = Command::new("git").args(args).current_dir(cwd).output();
match tokio::time::timeout(timeout, future).await {
Ok(result) => result.map_err(GhostError::Io),
Err(_) => Err(GhostError::GitTimeout {
command: format!("git {}", args.join(" ")),
timeout_secs: timeout.as_secs(),
}),
}
}
/// Execute a git command with timeout and return status
async fn git_status_with_timeout(args: &[&str], cwd: &PathBuf) -> Result<std::process::ExitStatus> {
let timeout = get_git_timeout();
let future = Command::new("git")
.args(args)
.current_dir(cwd)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
match tokio::time::timeout(timeout, future).await {
Ok(result) => result.map_err(GhostError::Io),
Err(_) => Err(GhostError::GitTimeout {
command: format!("git {}", args.join(" ")),
timeout_secs: timeout.as_secs(),
}),
}
}
/// A ghost commit that can be used for undo.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GhostCommit {
/// Commit SHA.
pub sha: String,
/// Turn ID this commit is associated with.
pub turn_id: String,
/// Session ID.
pub session_id: String,
/// Message ID (if any).
pub message_id: Option<String>,
/// Timestamp.
pub created_at: DateTime<Utc>,
/// Description.
pub description: Option<String>,
/// Files included in snapshot.
pub files: Vec<String>,
}
/// Report of ghost snapshot creation.
#[derive(Debug, Default, Clone)]
pub struct GhostSnapshotReport {
/// Large untracked directories skipped.
pub skipped_large_dirs: Vec<(PathBuf, i64)>,
/// Large files skipped.
pub skipped_large_files: Vec<(PathBuf, i64)>,
/// Total files included.
pub files_included: usize,
}
/// Manager for ghost commits.
pub struct GhostCommitManager {
repo_path: PathBuf,
config: GhostConfig,
commits: Vec<GhostCommit>,
}
impl GhostCommitManager {
pub fn new(repo_path: impl Into<PathBuf>, config: GhostConfig) -> Self {
Self {
repo_path: repo_path.into(),
config,
commits: Vec::new(),
}
}
/// Check if the path is a git repository.
pub async fn is_git_repo(&self) -> bool {
git_status_with_timeout(&["rev-parse", "--git-dir"], &self.repo_path)
.await
.map(|s| s.success())
.unwrap_or(false)
}
/// Create a ghost commit for the current state.
pub async fn create_ghost_commit(
&mut self,
session_id: &str,
turn_id: &str,
message_id: Option<&str>,
description: Option<&str>,
) -> Result<(GhostCommit, GhostSnapshotReport)> {
if !self.config.enabled {
return Err(GhostError::GitFailed("Ghost commits disabled".into()));
}
if !self.is_git_repo().await {
return Err(GhostError::NotGitRepo(self.repo_path.display().to_string()));
}
let mut report = GhostSnapshotReport::default();
// Get list of files to include
let files = self.collect_files_for_snapshot(&mut report).await?;
report.files_included = files.len();
// Stage all files for the ghost commit
self.stage_files(&files).await?;
// Create the ghost commit
let commit_msg = format!(
"ghost: {} - {}",
turn_id,
description.unwrap_or("automatic snapshot")
);
let sha = self.create_commit(&commit_msg).await?;
let ghost = GhostCommit {
sha: sha.clone(),
turn_id: turn_id.to_string(),
session_id: session_id.to_string(),
message_id: message_id.map(String::from),
created_at: Utc::now(),
description: description.map(String::from),
files: files
.iter()
.map(|p| p.to_string_lossy().to_string())
.collect(),
};
self.commits.push(ghost.clone());
info!("Created ghost commit {} for turn {}", sha, turn_id);
Ok((ghost, report))
}
/// Get the most recent ghost commit.
pub fn get_latest(&self) -> Option<&GhostCommit> {
self.commits.last()
}
/// Get ghost commit by turn ID.
pub fn get_by_turn(&self, turn_id: &str) -> Option<&GhostCommit> {
self.commits.iter().rev().find(|c| c.turn_id == turn_id)
}
/// Get all ghost commits for a session.
pub fn get_session_commits(&self, session_id: &str) -> Vec<&GhostCommit> {
self.commits
.iter()
.filter(|c| c.session_id == session_id)
.collect()
}
/// Collect files to include in the snapshot.
async fn collect_files_for_snapshot(
&self,
report: &mut GhostSnapshotReport,
) -> Result<Vec<PathBuf>> {
let mut files = Vec::new();
let ignored: HashSet<_> = self.config.ignored_dirs.iter().collect();
// Get tracked files
let tracked = self.get_tracked_files().await?;
files.extend(tracked);
// Get untracked files (respecting limits)
let untracked = self.get_untracked_files().await?;
for file in untracked {
let file_path = self.repo_path.join(&file);
// Check if in ignored directory
if file.components().any(|c| {
if let std::path::Component::Normal(name) = c {
ignored.contains(&name.to_string_lossy().to_string())
} else {
false
}
}) {
continue;
}
// Check file size
if let Ok(metadata) = tokio::fs::metadata(&file_path).await {
if metadata.is_file() {
let size = metadata.len() as i64;
if size > self.config.max_untracked_file_size {
report.skipped_large_files.push((file.clone(), size));
continue;
}
}
}
files.push(file);
}
Ok(files)
}
/// Get list of tracked files.
async fn get_tracked_files(&self) -> Result<Vec<PathBuf>> {
let output = git_command_with_timeout(&["ls-files"], &self.repo_path).await?;
if !output.status.success() {
return Err(GhostError::GitFailed("Failed to list tracked files".into()));
}
let files = String::from_utf8_lossy(&output.stdout)
.lines()
.map(PathBuf::from)
.collect();
Ok(files)
}
/// Get list of untracked files.
async fn get_untracked_files(&self) -> Result<Vec<PathBuf>> {
let output = git_command_with_timeout(
&["ls-files", "--others", "--exclude-standard"],
&self.repo_path,
)
.await?;
if !output.status.success() {
return Err(GhostError::GitFailed(
"Failed to list untracked files".into(),
));
}
let files = String::from_utf8_lossy(&output.stdout)
.lines()
.map(PathBuf::from)
.collect();
Ok(files)
}
/// Stage files for commit.
async fn stage_files(&self, files: &[PathBuf]) -> Result<()> {
if files.is_empty() {
return Ok(());
}
// Use git add with --intent-to-add for new files, then add all
let output = git_command_with_timeout(&["add", "-A"], &self.repo_path).await?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
warn!("Git add warning: {}", stderr);
}
Ok(())
}
/// Create a commit and return its SHA.
async fn create_commit(&self, message: &str) -> Result<String> {
// Check if there are changes to commit
let status = git_command_with_timeout(&["status", "--porcelain"], &self.repo_path).await?;
if status.stdout.is_empty() {
// No changes, create empty commit
let output = git_command_with_timeout(
&["commit", "--allow-empty", "-m", message],
&self.repo_path,
)
.await?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(GhostError::GitFailed(stderr.to_string()));
}
} else {
// Commit changes
let output =
git_command_with_timeout(&["commit", "-m", message], &self.repo_path).await?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(GhostError::GitFailed(stderr.to_string()));
}
}
// Get the commit SHA
let sha_output = git_command_with_timeout(&["rev-parse", "HEAD"], &self.repo_path).await?;
let sha = String::from_utf8_lossy(&sha_output.stdout)
.trim()
.to_string();
Ok(sha)
}
/// Load existing ghost commits from git log.
pub async fn load_history(&mut self) -> Result<()> {
let output = git_command_with_timeout(
&["log", "--oneline", "--all", "--grep=ghost:"],
&self.repo_path,
)
.await?;
if output.status.success() {
let log = String::from_utf8_lossy(&output.stdout);
for line in log.lines() {
if let Some((sha, rest)) = line.split_once(' ') {
if rest.starts_with("ghost:") {
// Parse turn_id from message
if let Some(turn_id) = rest
.strip_prefix("ghost: ")
.and_then(|s| s.split(" - ").next())
{
self.commits.push(GhostCommit {
sha: sha.to_string(),
turn_id: turn_id.to_string(),
session_id: String::new(),
message_id: None,
created_at: Utc::now(),
description: None,
files: Vec::new(),
});
}
}
}
}
}
debug!("Loaded {} ghost commits from history", self.commits.len());
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[tokio::test]
async fn test_ghost_commit_manager() {
let dir = tempdir().unwrap();
// Initialize git repo
Command::new("git")
.args(["init"])
.current_dir(dir.path())
.output()
.await
.unwrap();
Command::new("git")
.args(["config", "user.email", "test@test.com"])
.current_dir(dir.path())
.output()
.await
.unwrap();
Command::new("git")
.args(["config", "user.name", "Test"])
.current_dir(dir.path())
.output()
.await
.unwrap();
let config = GhostConfig::default();
let mut manager = GhostCommitManager::new(dir.path(), config);
assert!(manager.is_git_repo().await);
// Create a file
tokio::fs::write(dir.path().join("test.txt"), "hello")
.await
.unwrap();
// Create ghost commit
let (ghost, report) = manager
.create_ghost_commit("session-1", "turn-1", None, Some("test commit"))
.await
.unwrap();
assert!(!ghost.sha.is_empty());
assert_eq!(ghost.turn_id, "turn-1");
assert!(report.files_included > 0);
}
}