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
4 changes: 4 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ transport-adjacent. The rest, imperatively:
- **`sc ws harvest` lands on the currently-checked-out branch** (default the
session's base; `--into` must also be checked out) — it refuses with a
`sc switch` hint otherwise.
- **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.
- **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
90 changes: 90 additions & 0 deletions crates/repo/src/conflict_plaintext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! The paths a stopped merge, cherry-pick or rebase wrote to the working tree
//! as plaintext (`.sc/CONFLICT_PLAINTEXT`, NUL-separated: a path can hold a
//! newline but never a NUL).
//!
//! A protected path's conflict text, or a clean content merge of one, is never
//! put in the CAS (ADR-0025), so no tree records that the file was written.
//! Two things need to know: completion, which would otherwise read a path the
//! user deleted from disk as "absent because the committer cannot read it" and
//! carry the parent's ciphertext back, and abort, whose removal walk only sees
//! the trees and would leave the decrypted text behind (ADR-0049 q16, q18).
//!
//! One file serves all three operations because only one can be in progress.
//! It is replaced at every stop and cleared with the operation's own state.
//! Read it only while an operation is in progress: residue from a crash is
//! otherwise inert.

use std::collections::BTreeSet;

use crate::error::Result;
use crate::layout::Layout;

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

/// Record `paths` as written to the working tree in plaintext. An empty list
/// removes the record.
pub(crate) fn write(layout: &Layout, paths: &[String]) -> Result<()> {
if paths.is_empty() {
return clear(layout);
}
let tmp = layout.dot_sc.join("CONFLICT_PLAINTEXT.tmp");
std::fs::write(&tmp, paths.join("\0"))?;
std::fs::rename(&tmp, path(layout))?;
Comment thread
tonibergholm marked this conversation as resolved.
Resolved
Ok(())
}

/// The recorded paths, empty when there is no record.
pub(crate) fn read(layout: &Layout) -> Result<BTreeSet<String>> {
match std::fs::read_to_string(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()),
}
}

/// Drop the record.
pub(crate) fn clear(layout: &Layout) -> Result<()> {
match std::fs::remove_file(path(layout)) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn write_read_clear_roundtrip() {
let root =
std::env::temp_dir().join(format!("scl-conflict-plaintext-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let layout = Layout::at(&root);
std::fs::create_dir_all(&layout.dot_sc).unwrap();
assert!(read(&layout).unwrap().is_empty());
write(&layout, &["a/b.txt".into(), "c.txt".into()]).unwrap();
assert_eq!(
read(&layout).unwrap().into_iter().collect::<Vec<_>>(),
vec!["a/b.txt", "c.txt"]
);
write(&layout, &["odd\nname.txt".into()]).unwrap();
assert_eq!(
read(&layout).unwrap().into_iter().collect::<Vec<_>>(),
vec!["odd\nname.txt"],
"a newline in a path survives"
);
write(&layout, &[]).unwrap();
assert!(read(&layout).unwrap().is_empty(), "an empty list clears");
write(&layout, &["x".into()]).unwrap();
clear(&layout).unwrap();
assert!(read(&layout).unwrap().is_empty());
std::fs::remove_dir_all(&root).unwrap();
}
}
1 change: 1 addition & 0 deletions crates/repo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

pub mod bucket_transport;
pub(crate) mod cache;
pub(crate) mod conflict_plaintext;
pub(crate) mod conflicts;
pub mod diff3;
pub mod error;
Expand Down
1 change: 1 addition & 0 deletions crates/repo/src/merge_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn clear(layout: &Layout) -> Result<()> {
remove_if_exists(&merge_head_path(layout))?;
remove_if_exists(&merge_conflicts_path(layout))?;
remove_if_exists(&decided_root_path(layout))?;
crate::conflict_plaintext::clear(layout)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions crates/repo/src/pick_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub fn clear(layout: &Layout) -> Result<()> {
remove_if_exists(&pick_conflicts_path(layout))?;
remove_if_exists(&decided_root_path(layout))?;
remove_if_exists(&mainline_base_path(layout))?;
crate::conflict_plaintext::clear(layout)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions crates/repo/src/rebase_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub fn clear(layout: &Layout) -> Result<()> {
remove_if_exists(&state_path(layout))?;
remove_if_exists(&conflicts_path(layout))?;
remove_if_exists(&decided_root_path(layout))?;
crate::conflict_plaintext::clear(layout)?;
Ok(())
}

Expand Down
Loading
Loading