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
5 changes: 4 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ transport-adjacent. The rest, imperatively:
- **A stopped merge/pick/rebase lists the paths it wrote as plaintext in
`.sc/CONFLICT_PLAINTEXT`.** Protected conflict text never enters the CAS, so no
tree records it; completion treats a listed path absent from disk as deleted,
and abort removes listed paths the restored tree does not track as plain files.
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.
- **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
17 changes: 11 additions & 6 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,17 @@ scale-&-reach horizon):
limitation across different Git repos).
- **Richer trust models** beyond trusted-key lists (delegation, expiry) —
P22 ships the key-list model.
- **Sidecar-cleanup footgun in `rebase_continue` — half fixed (P37c).**
`remove_conflict_sidecars` now skips paths the completion's parent tree
tracks, but it still has no non-text-kind gate: a text conflict writes no
`.theirs` sidecar, so an untracked user file named `foo.txt.theirs` is
still deleted when the stop's conflict path is `foo.txt`. Pass the conflict
kind (or the written sidecar list) to the sweep to close it.
- **Sidecar-cleanup footgun in `rebase_continue` — fixed.** A stop records the
`.theirs` sidecars it actually wrote in `.sc/CONFLICT_SIDECARS`; merge/pick/
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.
- **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
76 changes: 74 additions & 2 deletions crates/repo/src/conflict_plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,63 @@ pub(crate) fn read(layout: &Layout) -> Result<BTreeSet<String>> {
}
}

/// Drop the record.
/// Drop both records.
pub(crate) fn clear(layout: &Layout) -> Result<()> {
match std::fs::remove_file(path(layout)) {
remove_if_present(&path(layout))?;
remove_if_present(&sidecar_path(layout))
}

fn remove_if_present(p: &std::path::Path) -> Result<()> {
match std::fs::remove_file(p) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}

fn sidecar_path(layout: &Layout) -> std::path::PathBuf {
layout.dot_sc.join("CONFLICT_SIDECARS")
}

/// Record the `<path>.theirs` sidecars a stop wrote, by full path. They are
/// kept apart from the plaintext list because completion counts a plaintext
/// path as tracked, and a sidecar must never be committed. An empty list
/// removes the record.
pub(crate) fn write_sidecars(layout: &Layout, paths: &[String]) -> Result<()> {
if paths.is_empty() {
return remove_if_present(&sidecar_path(layout));
}
let tmp = layout.dot_sc.join("CONFLICT_SIDECARS.tmp");
std::fs::write(&tmp, paths.join("\0"))?;
std::fs::rename(&tmp, sidecar_path(layout))?;
Ok(())
}

/// Drop one sidecar from the record once it has been dealt with, so a file
/// the user later creates at that path is not mistaken for ours.
pub(crate) fn retire_sidecar(layout: &Layout, sidecar: &str) -> Result<()> {
let mut paths = read_sidecars(layout)?;
if paths.remove(sidecar) {
write_sidecars(layout, &paths.into_iter().collect::<Vec<_>>())?;
Comment thread
tonibergholm marked this conversation as resolved.
Resolved
}
Ok(())
}

/// The recorded sidecar paths, empty when there is no record. Only the
/// sidecars a stop actually wrote are listed: a `.theirs` file beside a
/// conflict that wrote none is the user's own.
pub(crate) fn read_sidecars(layout: &Layout) -> Result<BTreeSet<String>> {
match std::fs::read_to_string(sidecar_path(layout)) {
Ok(text) => Ok(text
.split('\0')
.filter(|p| !p.is_empty())
.map(str::to_string)
.collect()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeSet::new()),
Err(e) => Err(e.into()),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -83,8 +131,32 @@ mod tests {
write(&layout, &[]).unwrap();
assert!(read(&layout).unwrap().is_empty(), "an empty list clears");
write(&layout, &["x".into()]).unwrap();
write_sidecars(&layout, &["x.theirs".into()]).unwrap();
assert_eq!(
read_sidecars(&layout)
.unwrap()
.into_iter()
.collect::<Vec<_>>(),
vec!["x.theirs"]
);
write_sidecars(&layout, &["x.theirs".into(), "y.theirs".into()]).unwrap();
retire_sidecar(&layout, "x.theirs").unwrap();
assert_eq!(
read_sidecars(&layout)
.unwrap()
.into_iter()
.collect::<Vec<_>>(),
vec!["y.theirs"]
);
retire_sidecar(&layout, "y.theirs").unwrap();
assert!(
read_sidecars(&layout).unwrap().is_empty(),
"last one removes the file"
);
write_sidecars(&layout, &["x.theirs".into()]).unwrap();
clear(&layout).unwrap();
assert!(read(&layout).unwrap().is_empty());
assert!(read_sidecars(&layout).unwrap().is_empty());
std::fs::remove_dir_all(&root).unwrap();
}
}
52 changes: 26 additions & 26 deletions crates/repo/src/conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,32 +292,32 @@ impl Repo {
},
}

// Drop the `.theirs` sidecar this system may have written — the only
// sidecar extension ever written (`.base` and `.ours` are never
// produced, so removing them was both dead code and a footgun: a
// repo can legitimately track a real file named
// `foo.txt.base`/`foo.txt.ours`, and blind-unlinking it would
// silently destroy user data). `.theirs` is written only for
// BINARY/PROTECTED-with-binary-content conflicts (merge.rs writes
// `{path}.theirs` in the binary-conflict arm) — a TEXT conflict
// never writes one, so a `.theirs`-named file sitting untracked next
// to a text conflict is not this system's scratch, it's the user's;
// removing it there would silently and unrecoverably delete
// untracked data. So only consider removal when the conflict kind is
// NOT Text (`!= Text`, not `== Binary`: a protected binary conflict
// still writes a plaintext `.theirs` sidecar and classifies as
// Protected, so it must still be cleaned up). And even then, only
// remove it when it is NOT a tracked path — a genuine sidecar is
// untracked scratch by construction, so a tracked `{path}.theirs`
// must be a user's real file and is left alone.
if kind != ConflictKind::Text {
let theirs_sidecar_path = format!("{path}.theirs");
if !self.tracked_paths()?.contains(&theirs_sidecar_path) {
let sidecar = safe_join(&self.layout.root, &theirs_sidecar_path)?;
match std::fs::remove_file(&sidecar) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(e.into()),
// Drop the `.theirs` sidecar the stop recorded writing for this path.
// Only a recorded sidecar is ours: the kind of conflict does not say
// whether one was written (a protected UTF-8 conflict and a binary
// delete/modify write none), and a `.theirs` file the stop did not
// write is the user's, as is a tracked `{path}.theirs` (a repo can
// legitimately track `foo.txt.theirs`, `.base` or `.ours`).
let theirs_sidecar_path = format!("{path}.theirs");
if crate::conflict_plaintext::read_sidecars(&self.layout)?.contains(&theirs_sidecar_path)
&& !self.tracked_paths()?.contains(&theirs_sidecar_path)
Comment thread
tonibergholm marked this conversation as resolved.
Resolved
{
let sidecar = safe_join(&self.layout.root, &theirs_sidecar_path)?;
// Retire first: a crash after the unlink must not leave a record
// that names a path the user may recreate. A failed unlink puts
// the entry back.
crate::conflict_plaintext::retire_sidecar(&self.layout, &theirs_sidecar_path)?;
match std::fs::remove_file(&sidecar) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => {
let mut paths = crate::conflict_plaintext::read_sidecars(&self.layout)?;
paths.insert(theirs_sidecar_path);
let _ = crate::conflict_plaintext::write_sidecars(
&self.layout,
&paths.into_iter().collect::<Vec<_>>(),
);
return Err(e.into());
}
}
}
Expand Down
Loading
Loading