mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 23:36:11 +00:00
feat: resolve diverged worktrees, status --plan, snapshot size limit
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b86342b1d0
commit
398b959ffe
13 files changed
+443
-17
No files matched your search
@@ -3,6 +3,7 @@
|
||||
|
||||
use crate::git::{Git, GitError};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -10,6 +11,16 @@ pub struct Snapshot {
|
||||
pub commit: String,
|
||||
pub tree: String,
|
||||
pub refname: String,
|
||||
/// Changed files left out because they exceed the size limit.
|
||||
pub skipped: Vec<String>,
|
||||
}
|
||||
|
||||
static MAX_FILE_BYTES: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
/// Process-wide limit: changed files larger than this are left out of snapshots (0 = no limit).
|
||||
/// Set once at startup from `sync.max_file_mb`.
|
||||
pub fn set_max_file_bytes(n: u64) {
|
||||
MAX_FILE_BYTES.store(n, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -43,7 +54,7 @@ fn temp_index_path() -> PathBuf {
|
||||
/// built in a temporary copy of the index so the real index is never modified. The returned
|
||||
/// guard keeps the temporary index alive; `Git` has it set as `GIT_INDEX_FILE` and the author
|
||||
/// identity forced.
|
||||
fn stage(git: &Git) -> Result<(TempIndex, Git, String), GitError> {
|
||||
fn stage(git: &Git) -> Result<(TempIndex, Git, String, Vec<String>), GitError> {
|
||||
let gitdir = PathBuf::from(git.run(&["rev-parse", "--absolute-git-dir"])?);
|
||||
let tmp = TempIndex(temp_index_path());
|
||||
let real_index = gitdir.join("index");
|
||||
@@ -58,8 +69,49 @@ fn stage(git: &Git) -> Result<(TempIndex, Git, String), GitError> {
|
||||
.with_env("GIT_COMMITTER_NAME", "grove")
|
||||
.with_env("GIT_COMMITTER_EMAIL", "grove@localhost");
|
||||
snap.run(&["add", "-A"])?;
|
||||
let skipped = drop_oversized(&snap, git.dir())?;
|
||||
let tree = snap.run(&["write-tree"])?;
|
||||
Ok((tmp, snap, tree))
|
||||
Ok((tmp, snap, tree, skipped))
|
||||
}
|
||||
|
||||
/// Reverts oversized changed files in the temporary index to their HEAD version (or removes them
|
||||
/// when HEAD has none). Returns their paths.
|
||||
fn drop_oversized(snap: &Git, worktree: &Path) -> Result<Vec<String>, GitError> {
|
||||
let limit = MAX_FILE_BYTES.load(Ordering::Relaxed);
|
||||
if limit == 0 {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let has_head = snap
|
||||
.probe(&["rev-parse", "-q", "--verify", "HEAD"])?
|
||||
.is_some();
|
||||
let changed = if has_head {
|
||||
snap.run(&[
|
||||
"diff",
|
||||
"--cached",
|
||||
"--name-only",
|
||||
"-z",
|
||||
"--no-renames",
|
||||
"HEAD",
|
||||
])?
|
||||
} else {
|
||||
snap.run(&["ls-files", "-z"])?
|
||||
};
|
||||
let mut skipped = Vec::new();
|
||||
for rel in changed.split('\0').filter(|p| !p.is_empty()) {
|
||||
let big = std::fs::symlink_metadata(worktree.join(rel))
|
||||
.map(|m| m.is_file() && m.len() > limit)
|
||||
.unwrap_or(false);
|
||||
if !big {
|
||||
continue;
|
||||
}
|
||||
if has_head {
|
||||
snap.run(&["reset", "-q", "HEAD", "--", rel])?;
|
||||
} else {
|
||||
snap.run(&["rm", "--cached", "-q", "--ignore-unmatch", "--", rel])?;
|
||||
}
|
||||
skipped.push(rel.to_string());
|
||||
}
|
||||
Ok(skipped)
|
||||
}
|
||||
|
||||
/// The tree a snapshot of `worktree` would have, without creating any object besides the tree.
|
||||
@@ -75,7 +127,7 @@ pub fn take(worktree: &Path, machine: &str, message_stamp: &str) -> Result<Outco
|
||||
.probe(&["symbolic-ref", "--short", "-q", "HEAD"])?
|
||||
.ok_or_else(|| GitError::Other("detached HEAD: cannot snapshot".into()))?;
|
||||
let refname = ref_name(machine, &branch);
|
||||
let (_tmp, snap, tree) = stage(&git)?;
|
||||
let (_tmp, snap, tree, skipped) = stage(&git)?;
|
||||
|
||||
let prev = snap.probe(&["rev-parse", "-q", "--verify", &refname])?;
|
||||
if let Some(p) = &prev {
|
||||
@@ -101,5 +153,6 @@ pub fn take(worktree: &Path, machine: &str, message_stamp: &str) -> Result<Outco
|
||||
commit,
|
||||
tree,
|
||||
refname,
|
||||
skipped,
|
||||
}))
|
||||
}
|
||||
Reference in new issue
Block a user