From a631d0a90f70805226b9ff8c663fc297cff69cd0 Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 16 Nov 2026 13:01:30 +0100 Subject: [PATCH] fix(core): adopt upstream when a dirty worktree is identical to it A worktree with another machine's snapshot applied stayed BehindDirty forever after the author committed and pushed that work. When the worktree content is byte-identical to the upstream tree, a mixed reset moves the branch without touching any file. Co-Authored-By: Claude Sonnet 5.5 --- crates/grove-core/src/receive.rs | 28 +++++++++++++++++++++++++- crates/grove-core/src/snapshot.rs | 33 ++++++++++++++++++++----------- crates/grove-core/tests/sync.rs | 31 ++++++++++++++++++++++------- 3 files changed, 73 insertions(+), 19 deletions(-) diff --git a/crates/grove-core/src/receive.rs b/crates/grove-core/src/receive.rs index 9491255..8a43e8d 100644 --- a/crates/grove-core/src/receive.rs +++ b/crates/grove-core/src/receive.rs @@ -109,13 +109,39 @@ pub enum Received { Declined(String), } +/// A dirty worktree that is behind its upstream is normally left alone. The exception is when its +/// content is exactly the upstream tree (typical: another machine's snapshot was applied here and +/// its author then committed and pushed it). Then moving the branch and index to upstream with a +/// mixed reset changes no file and loses nothing. +fn adopt_if_identical( + g: &Git, + machine: &str, + stamp: &str, + st: State, +) -> Result { + let worktree_tree = crate::snapshot::worktree_tree(g.dir())?; + let upstream_tree = g.run(&["rev-parse", "@{upstream}^{tree}"])?; + if worktree_tree != upstream_tree { + return Ok(Received::Skipped(st)); + } + let from = g.run(&["rev-parse", "HEAD"])?; + let bref = backup_ref(machine, stamp, &format!("receive/{}", current_branch(g)?)); + g.run(&["update-ref", &bref, &from])?; + g.run(&["reset", "-q", "@{upstream}"])?; + let to = g.run(&["rev-parse", "HEAD"])?; + Ok(Received::FastForwarded { from, to }) +} + /// Fast-forward a clean, strictly-behind worktree to its upstream. Anything else is skipped. pub fn receive(worktree: &Path, machine: &str, stamp: &str) -> Result { let st = state(worktree); + let g = Git::new(worktree); + if st == State::BehindDirty { + return adopt_if_identical(&g, machine, stamp, st); + } if st != State::Behind { return Ok(Received::Skipped(st)); } - let g = Git::new(worktree); let clash = ignored_conflicts(&g, "@{upstream}")?; if !clash.is_empty() { return Ok(Received::Declined(format!( diff --git a/crates/grove-core/src/snapshot.rs b/crates/grove-core/src/snapshot.rs index 3717947..ff592bb 100644 --- a/crates/grove-core/src/snapshot.rs +++ b/crates/grove-core/src/snapshot.rs @@ -39,17 +39,12 @@ fn temp_index_path() -> PathBuf { std::env::temp_dir().join(format!("grove-index-{}-{nanos}", std::process::id())) } -/// Take a snapshot of `worktree` for `machine`. `message_stamp` is embedded in the -/// commit message (callers pass a timestamp; keeping it a parameter keeps this testable). -pub fn take(worktree: &Path, machine: &str, message_stamp: &str) -> Result { - let git = Git::new(worktree); +/// Tree of everything in the worktree (tracked changes and untracked files, not ignored ones), +/// 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> { let gitdir = PathBuf::from(git.run(&["rev-parse", "--absolute-git-dir"])?); - let branch = git - .probe(&["symbolic-ref", "--short", "-q", "HEAD"])? - .ok_or_else(|| GitError::Other("detached HEAD: cannot snapshot".into()))?; - let refname = ref_name(machine, &branch); - - // Work on a copy of the real index so the user's index is never modified. let tmp = TempIndex(temp_index_path()); let real_index = gitdir.join("index"); if real_index.exists() { @@ -62,9 +57,25 @@ pub fn take(worktree: &Path, machine: &str, message_stamp: &str) -> Result Result { + Ok(stage(&Git::new(worktree))?.2) +} + +/// Take a snapshot of `worktree` for `machine`. `message_stamp` is embedded in the +/// commit message (callers pass a timestamp; keeping it a parameter keeps this testable). +pub fn take(worktree: &Path, machine: &str, message_stamp: &str) -> Result { + let git = Git::new(worktree); + let branch = git + .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 prev = snap.probe(&["rev-parse", "-q", "--verify", &refname])?; if let Some(p) = &prev { diff --git a/crates/grove-core/tests/sync.rs b/crates/grove-core/tests/sync.rs index 594329a..08207c4 100644 --- a/crates/grove-core/tests/sync.rs +++ b/crates/grove-core/tests/sync.rs @@ -2,6 +2,7 @@ mod common; use common::*; use grove_core::git::Git; use grove_core::receive::Applied; +use grove_core::receive::Received; use grove_core::state::State; use grove_core::sync::{Options, sync_repo}; use std::fs; @@ -70,20 +71,36 @@ fn two_machines_converge_without_manual_git() { assert_eq!(fs::read_to_string(db.join("a.txt")).unwrap(), "1\nwork\n"); assert!(db.join("scratch.txt").exists()); - // A commits and pushes by hand; B (now dirty from the applied WIP) must not be touched. + // A commits and pushes by hand. B's applied copy is byte-identical to that commit, so B + // adopts it (mixed reset, no file touched) and ends clean. ga.run(&["add", "-A"]).unwrap(); ga.run(&["commit", "-qm", "work"]).unwrap(); ga.run(&["push", "-q"]).unwrap(); let rb2 = sync_repo(&db, &opts("B", "t3")).unwrap(); - assert_eq!(rb2.worktrees[0].state, State::BehindDirty); + assert_eq!(rb2.worktrees[0].state, State::Synced); + assert!(matches!( + rb2.worktrees[0].received, + Some(Received::FastForwarded { .. }) + )); assert_eq!(fs::read_to_string(db.join("a.txt")).unwrap(), "1\nwork\n"); + assert!( + Git::new(&db) + .run(&["status", "--porcelain"]) + .unwrap() + .is_empty() + ); - // Discarding B's applied copy makes it clean; the next sync fast-forwards it. - Git::new(&db).run(&["checkout", "-q", "--", "."]).unwrap(); - fs::remove_file(db.join("scratch.txt")).unwrap(); + // If B has its own extra edit when the next upstream commit arrives, it must not be touched. + fs::write(da.join("a.txt"), "1\nwork\nmore\n").unwrap(); + ga.run(&["commit", "-qam", "more"]).unwrap(); + ga.run(&["push", "-q"]).unwrap(); + fs::write(db.join("a.txt"), "1\nwork\nlocal\n").unwrap(); let rb3 = sync_repo(&db, &opts("B", "t4")).unwrap(); - assert_eq!(rb3.worktrees[0].state, State::Synced); - assert!(db.join("scratch.txt").exists()); + assert_eq!(rb3.worktrees[0].state, State::BehindDirty); + assert_eq!( + fs::read_to_string(db.join("a.txt")).unwrap(), + "1\nwork\nlocal\n" + ); } #[test]