Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ transport-adjacent. The rest, imperatively:
and abort removes listed paths the restored tree does not track as plain files. The `<path>.theirs`
sidecars it wrote are listed apart in `.sc/CONFLICT_SIDECARS`; abort, `rebase
--continue` and `sc resolve` remove only those, never a `.theirs` file the stop
did not write.
did not write; the completing commit leaves them out of the tree and removes them.
- **Every ref-moving or commit-creating op (including all secret/protect
policy ops) is guarded against an in-progress merge/pick/rebase** — finish
or abort the in-progress op first.
Expand Down
15 changes: 9 additions & 6 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,15 @@ scale-&-reach horizon):
rebase abort, `rebase --continue` and `sc resolve` unlink only those (and
never a path the restored tree tracks), so an untracked user `foo.txt.theirs`
survives whatever the conflict kind.
- **Completion commits a leftover `.theirs` sidecar.** A binary conflict's
`<path>.theirs` is working-tree scratch, but if the user resolves by editing
`<path>` (not `sc resolve`) and leaves the sidecar, the completing `sc commit`
or `rebase --continue` snapshots it as a tracked file (reproduced on main with
a merge). Exclude recorded sidecars (`.sc/CONFLICT_SIDECARS`) that no input
root tracks from the completion snapshot.
- **Completion commits a leftover `.theirs` sidecar — fixed.** The completion
snapshot (`sc commit`, `rebase --continue`) leaves out the sidecars the stop
recorded in `.sc/CONFLICT_SIDECARS` unless a root tracks that path, and the
commit then removes them; resolving by hand-editing no longer commits the
scratch file.
Known limit: the sweep retires the record before it unlinks, so a crash
between the two leaves the scratch file on disk with no record, and a retried
`sc commit` would take it. The opposite order would risk deleting a file the
user recreated at that path; closing both needs a journal.
- **In-progress guard for the RECEIVING repo on push.**
`LocalTransport::update_ref` moves a branch tip with only a CAS/ff gate
and no in-progress check on the receiving side — a push into a repo
Expand Down
179 changes: 174 additions & 5 deletions crates/repo/src/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,11 +1212,9 @@ impl Repo {
// `merge_abort`'s cleanup): once resolved, they're pure
// working-tree litter — never part of the tracked tree, so
// `materialize` never touches them itself.
// Not a file any of the trees involved tracks (that one is the
// user's own): see `remove_conflict_sidecars`.
// Not a file the completion's parent tracks (that one is the
// user's own): see `remove_conflict_sidecars`.
self.remove_conflict_sidecars(&[self.snapshot(&st.acc_tip)?.root])?;
// Not a file the completed commit tracks (that one is the user's
// own): see `remove_conflict_sidecars`.
self.remove_conflict_sidecars(&[self.snapshot(&new_tip)?.root])?;
// Advance state BEFORE attempting the fold, so an error from the
// fold below (on a later commit) leaves a retryable, idempotent
// `--continue` rather than losing resumability. `rebase_abort`
Expand Down Expand Up @@ -3123,6 +3121,177 @@ mod tests {
remove_root(&root);
}

/// Two branches that conflict as binary on `bin.dat`, `main` checked out.
fn binary_conflict(name: &str) -> (std::path::PathBuf, Repo) {
let root = tmp_root(name);
let repo = Repo::init(&root).unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfebase\n").unwrap();
repo.commit("me", "base").unwrap();
repo.branch("work").unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfeOURS\n").unwrap();
repo.commit("me", "main").unwrap();
repo.switch("work").unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfeTHEIRS\n").unwrap();
repo.commit("me", "work").unwrap();
repo.switch("main").unwrap();
(root, repo)
}

/// Resolving by editing the file and leaving the sidecar on disk must not
/// commit the sidecar, and the next commit must not take it either.
#[test]
fn a_merge_commit_leaves_the_leftover_sidecar_out_and_removes_it() {
let (root, repo) = binary_conflict("sweep-commit-merge");
repo.merge("work", "me").unwrap_err();
assert!(root.join("bin.dat.theirs").exists(), "setup: sidecar");
std::fs::write(root.join("bin.dat"), b"\xff\xferesolved\n").unwrap();
repo.commit("me", "merge").unwrap();
assert!(!repo.tracked_paths().unwrap().contains("bin.dat.theirs"));
assert!(!root.join("bin.dat.theirs").exists(), "scratch removed");
std::fs::write(root.join("other.txt"), b"x\n").unwrap();
repo.commit("me", "next").unwrap();
assert!(!repo.tracked_paths().unwrap().contains("bin.dat.theirs"));
drop(repo);
remove_root(&root);
}

/// A sidecar that cannot be removed fails the completing commit before
/// anything moves; the record survives, and the retry finishes.
#[test]
fn a_completion_whose_sidecar_cannot_be_removed_fails_and_retries() {
let (root, repo) = binary_conflict("sweep-commit-fails");
let ours = repo.head_tip().unwrap();
repo.merge("work", "me").unwrap_err();
std::fs::write(root.join("bin.dat"), b"\xff\xferesolved\n").unwrap();
std::fs::remove_file(root.join("bin.dat.theirs")).unwrap();
std::fs::create_dir(root.join("bin.dat.theirs")).unwrap();
repo.commit("me", "merge").unwrap_err();
assert_eq!(repo.head_tip().unwrap(), ours, "the ref did not move");
assert!(repo.merge_in_progress());
assert!(
crate::conflict_plaintext::read_sidecars(&repo.layout)
.unwrap()
.contains("bin.dat.theirs"),
"the record survives for the retry"
);
std::fs::remove_dir(root.join("bin.dat.theirs")).unwrap();
repo.commit("me", "merge").unwrap();
assert!(!repo.merge_in_progress());
assert!(!repo.tracked_paths().unwrap().contains("bin.dat.theirs"));
drop(repo);
remove_root(&root);
}

/// Base and `work` track `bin.dat.theirs`, `main` deletes it, and
/// `bin.dat` conflicts as binary: the stop writes scratch at the path the
/// result no longer tracks. Completion must not re-add it because
/// `work` once tracked it.
#[test]
fn a_path_the_result_deleted_is_not_kept_because_theirs_tracked_it() {
let root = tmp_root("sweep-deleted-by-ours");
let repo = Repo::init(&root).unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfebase\n").unwrap();
std::fs::write(root.join("bin.dat.theirs"), b"old\n").unwrap();
repo.commit("me", "base").unwrap();
repo.branch("work").unwrap();
std::fs::remove_file(root.join("bin.dat.theirs")).unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfeOURS\n").unwrap();
repo.commit("me", "main").unwrap();
repo.switch("work").unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfeTHEIRS\n").unwrap();
repo.commit("me", "work").unwrap();
repo.switch("main").unwrap();
repo.merge("work", "me").unwrap_err();
assert!(
root.join("bin.dat.theirs").exists(),
"setup: scratch written"
);
std::fs::write(root.join("bin.dat"), b"\xff\xferesolved\n").unwrap();
repo.commit("me", "merge").unwrap();
assert!(!repo.tracked_paths().unwrap().contains("bin.dat.theirs"));
assert!(!root.join("bin.dat.theirs").exists());
drop(repo);
remove_root(&root);
}

/// The target tracks `bin.dat.theirs`, the replayed commit deletes it and
/// conflicts with the target on `bin.dat`. The stop writes scratch at a
/// path the result no longer tracks; the target's old tracking of it must
/// not make completion commit the scratch.
#[test]
fn a_rebase_that_deletes_a_target_tracked_path_does_not_commit_the_scratch() {
let root = tmp_root("sweep-rebase-deleted");
let repo = Repo::init(&root).unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfebase\n").unwrap();
std::fs::write(root.join("bin.dat.theirs"), b"keep\n").unwrap();
repo.commit("me", "base").unwrap();
repo.branch("work").unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfeMAIN\n").unwrap();
repo.commit("me", "main").unwrap();
repo.switch("work").unwrap();
std::fs::remove_file(root.join("bin.dat.theirs")).unwrap();
std::fs::write(root.join("bin.dat"), b"\xff\xfeWORK\n").unwrap();
repo.commit("me", "work").unwrap();
assert!(matches!(
repo.rebase("main", "me", None).unwrap(),
RebaseResult::Stopped { .. }
));
assert!(root.join("bin.dat.theirs").exists(), "setup: scratch");
std::fs::write(root.join("bin.dat"), b"\xff\xferesolved\n").unwrap();
repo.rebase_continue("me", None).unwrap();
assert!(!repo.tracked_paths().unwrap().contains("bin.dat.theirs"));
assert!(!root.join("bin.dat.theirs").exists());
drop(repo);
remove_root(&root);
}

#[test]
fn a_pick_commit_leaves_the_leftover_sidecar_out() {
let (root, repo) = binary_conflict("sweep-commit-pick");
repo.cherry_pick("work", "me", None, None).unwrap_err();
assert!(root.join("bin.dat.theirs").exists(), "setup: sidecar");
std::fs::write(root.join("bin.dat"), b"\xff\xferesolved\n").unwrap();
repo.commit("me", "pick").unwrap();
assert!(!repo.tracked_paths().unwrap().contains("bin.dat.theirs"));
assert!(!root.join("bin.dat.theirs").exists());
drop(repo);
remove_root(&root);
}

#[test]
fn rebase_continue_leaves_the_leftover_sidecar_out() {
let (root, repo) = binary_conflict("sweep-commit-rebase");
repo.switch("work").unwrap();
assert!(matches!(
repo.rebase("main", "me", None).unwrap(),
RebaseResult::Stopped { .. }
));
assert!(root.join("bin.dat.theirs").exists(), "setup: sidecar");
std::fs::write(root.join("bin.dat"), b"\xff\xferesolved\n").unwrap();
repo.rebase_continue("me", None).unwrap();
assert!(!repo.tracked_paths().unwrap().contains("bin.dat.theirs"));
assert!(!root.join("bin.dat.theirs").exists());
drop(repo);
remove_root(&root);
}

/// A sidecar record left by a crash mid-stop (no operation in progress)
/// names nothing of ours: an ordinary commit takes a file at that path.
#[test]
fn an_orphan_sidecar_record_does_not_touch_an_ordinary_commit() {
let root = tmp_root("sweep-orphan-record");
let repo = Repo::init(&root).unwrap();
std::fs::write(root.join("a.txt"), b"a\n").unwrap();
repo.commit("me", "base").unwrap();
crate::conflict_plaintext::write_sidecars(&repo.layout, &["x.theirs".to_string()]).unwrap();
std::fs::write(root.join("x.theirs"), b"mine\n").unwrap();
repo.commit("me", "adds x.theirs").unwrap();
assert!(repo.tracked_paths().unwrap().contains("x.theirs"));
assert_eq!(std::fs::read(root.join("x.theirs")).unwrap(), b"mine\n");
drop(repo);
remove_root(&root);
}

#[test]
fn merge_abort_keeps_a_user_theirs_file_beside_a_text_conflict() {
let (root, repo) = text_conflict_with_user_theirs_file("sweep-merge-abort");
Expand Down
73 changes: 72 additions & 1 deletion crates/repo/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ impl Repo {
}
};

// On a completion only (an ordinary commit, amend or workspace harvest
// has none of these): a sidecar the stop wrote is working-tree scratch,
// so it must reach neither the scanner nor the tree, whether or not the user resolved by
// hand and left it on disk. A tracked path of the same name is the
// user's own file and stays.
let files = if merge_head.is_some() || pick_head.is_some() || decided_root.is_some() {
self.without_recorded_sidecars(files, &[tip], decided_root)?
Comment thread
tonibergholm marked this conversation as resolved.
Resolved
} else {
files
};

// Single pass: split protected files (capturing each rule's recipients, so
// the encryption loop needn't look the prefix up again) from plaintext.
let mut plain: Vec<(String, Vec<u8>, scl_core::FileMode)> = Vec::new();
Expand Down Expand Up @@ -899,6 +910,14 @@ impl Repo {
id
}
};
// The stop's `.theirs` sidecars were left out of the commit (see
// `snapshot_files`); remove them before the ref moves, or the next
// commit would take them. A removal failure fails this commit with
// nothing moved and the record intact, so it can be retried.
if merging || picking {
let root = self.snapshot(&id)?.root;
self.remove_conflict_sidecars(&[root])?;
}
refs::write_branch_tip(&self.layout, &head, &id)?;
crate::merge_state::clear(&self.layout)?;
crate::pick_state::clear(&self.layout)?;
Expand Down Expand Up @@ -1931,6 +1950,58 @@ impl Repo {
Ok(())
}

/// The sidecars the in-progress stop recorded writing. A record with no
/// merge, pick or rebase in progress is residue of a crash mid-stop, and
/// names nothing that is ours: it reads as empty.
fn recorded_sidecars(&self) -> Result<std::collections::BTreeSet<String>> {
if self.active_conflict_op()?.is_none() {
return Ok(std::collections::BTreeSet::new());
}
crate::conflict_plaintext::read_sidecars(&self.layout)
}

/// Drop from `files` the sidecars the stop recorded writing
/// (`conflict_plaintext::read_sidecars`) that no snapshot in `snapshots`
/// nor `decided_root` tracks. The other side's history does not count: a path
/// the result deleted stays deleted, however the input tracked it. No
/// record, no change.
fn without_recorded_sidecars(
&self,
files: Vec<(String, Vec<u8>, scl_core::FileMode)>,
snapshots: &[Option<ObjectId>],
decided_root: Option<ObjectId>,
) -> Result<Vec<(String, Vec<u8>, scl_core::FileMode)>> {
let sidecars = self.recorded_sidecars()?;
if sidecars.is_empty() {
return Ok(files);
}
// The decided root is the operation's outcome and takes precedence: a
// parent that tracks the path says nothing about a result that deleted
// it. Without one, the tip is the only owner.
let mut roots: Vec<ObjectId> = decided_root.into_iter().collect();
if roots.is_empty() {
for id in snapshots.iter().flatten() {
roots.push(self.snapshot(id)?.root);
}
}
let partial = self.promisor()?.is_some();
let store_arc = self.vfs.store();
let mut store = store_arc.lock().unwrap();
let mut kept = Vec::with_capacity(files.len());
for file in files {
let scratch = sidecars.contains(&file.0)
&& !roots.iter().any(|root| {
// A failed lookup counts as tracked: keep the file.
worktree::tree_entry_at(&mut store, *root, &file.0, partial)
.map_or(true, |e| e.is_some())
});
if !scratch {
kept.push(file);
}
}
Ok(kept)
}

/// Remove the `<path>.theirs` sidecars the stop recorded writing
/// (`conflict_plaintext::read_sidecars`), except a file any of `roots`
/// tracks: that one is the user's own and was just restored. A `.theirs`
Expand All @@ -1940,7 +2011,7 @@ impl Repo {
/// a restore that fails leaves the sidecars to retry. The record is retired
/// once swept.
pub(crate) fn remove_conflict_sidecars(&self, roots: &[ObjectId]) -> Result<()> {
let sidecars = crate::conflict_plaintext::read_sidecars(&self.layout)?;
let sidecars = self.recorded_sidecars()?;
if sidecars.is_empty() {
return Ok(());
}
Expand Down
Loading