Repository navigation
fix(repo): honor deletions and clean up plaintext in stopped merges, picks and rebases #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))?; | ||
| 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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.