Repository navigation
fix(repo): delete or overwrite only the stop's own files on materialize and abort #259
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
Changes from all commits
63c6bde
2e026b4
8029830
bdef9cc
a5bc39b
c36b178
7623528
b25488f
96f61b8
016e778
9ddd6dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -394,10 +394,25 @@ impl Repo { | |
| &target.keys().cloned().collect(), | ||
| )?; | ||
| } | ||
| // A path outside the view is never laid out, so a copy on disk is the | ||
| // user's own (#256). A failed removal is an error: the file may be | ||
| // decrypted plaintext that must not linger. | ||
| worktree::refuse_blocked_parents( | ||
| &self.layout.root, | ||
| target.keys().filter(|p| sparse.matches(p)), | ||
| &|c| old_paths.contains(c) && !target.contains_key(c), | ||
| )?; | ||
| for p in old_paths { | ||
| if !target.contains_key(p) || !sparse.matches(p) { | ||
| if !target.contains_key(p) | ||
| && sparse.matches(p) | ||
| && !worktree::under_symlink(&self.layout.root, p) | ||
| { | ||
| let full = worktree::safe_join(&self.layout.root, p)?; | ||
| let _ = std::fs::remove_file(full); | ||
| match std::fs::remove_file(full) { | ||
| Ok(()) => {} | ||
| Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} | ||
| Err(e) => return Err(e.into()), | ||
| } | ||
| } | ||
| } | ||
| let mut skipped = Vec::new(); | ||
|
|
@@ -880,7 +895,10 @@ impl Repo { | |
| /// only the checkout needs restoring. It needs the identity (the tip is a | ||
| /// manifest); a caller who cannot open the branch is refused before any | ||
| /// write, leaving the stop intact for someone who can. | ||
| pub(crate) fn merge_abort_private(&self, identity: Option<&SecretKey>) -> Result<Vec<String>> { | ||
| pub(crate) fn merge_abort_private( | ||
| &self, | ||
| identity: Option<&SecretKey>, | ||
| ) -> Result<crate::repo::AbortReport> { | ||
| let (branch, mid, manifest) = self | ||
| .head_private()? | ||
| .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; | ||
|
|
@@ -889,20 +907,56 @@ impl Repo { | |
| let theirs_id = crate::merge_state::read_merge_head(&self.layout)? | ||
| .expect("in_progress is true but MERGE_HEAD is absent"); | ||
| let sparse = self.sparse_spec()?; | ||
| let plaintext = crate::conflict_plaintext::read(&self.layout)?; | ||
| let conflicted: BTreeSet<String> = crate::merge_state::read_conflicts(&self.layout)? | ||
| .into_iter() | ||
| .collect(); | ||
|
|
||
| let store_arc = self.vfs.store(); | ||
| let mut store = store_arc.lock().unwrap(); | ||
| let inner_tip = ctx.inner_tip()?; | ||
| let ours = self.get_inner_snapshot(&mut store, &ctx, &inner_tip)?; | ||
| let theirs = store.get_snapshot(&theirs_id)?; | ||
| let prior = self.inner_tree_files(&mut store, &ctx, ours.root)?; | ||
| // The stop wrote files from ours and from theirs, in view only; theirs' | ||
| // adds are not in the restored tree, so the removal pass must know | ||
| // them (as the public abort does by passing theirs' root). | ||
| let mut written: BTreeSet<String> = prior.keys().cloned().collect(); | ||
| written | ||
| .extend(worktree::tree_file_entries_with_perms(&mut store, theirs.root)?.into_keys()); | ||
| written.retain(|p| sparse.matches(p)); | ||
| // The stop wrote files from theirs, in view only; those the restored | ||
| // tip does not track must go, but only the stop's own: a conflicted | ||
| // path or recorded plaintext, or a file that is still theirs' bytes. | ||
| // A stop keeps no decided root for a private branch (it would be | ||
| // unsealed), so a conflicted path cannot be compared by content. | ||
| // Anything else at such a path is the user's and is reported (#255). | ||
| let mut written: BTreeSet<String> = prior | ||
| .keys() | ||
| .filter(|p| sparse.matches(p)) | ||
| .cloned() | ||
| .collect(); | ||
| let mut kept = Vec::new(); | ||
| for (p, (blob_id, _, perms)) in | ||
| worktree::tree_file_entries_with_perms(&mut store, theirs.root)? | ||
| { | ||
| if prior.contains_key(&p) || !sparse.matches(&p) { | ||
| continue; | ||
| } | ||
| let full = worktree::safe_join(&self.layout.root, &p)?; | ||
| // Not knowing what stands there must stop the abort before it | ||
| // clears the stop's records (decrypted text could linger). | ||
| let Some(meta) = worktree::lstat(&full)? else { | ||
| continue; | ||
| }; | ||
| let recorded = plaintext.contains(&p); | ||
| // The stop wrote regular files: a symlink there is the user's. | ||
| let ours = meta.is_file() | ||
| && !worktree::under_symlink(&self.layout.root, &p) | ||
| && if perms & PROTECTED != 0 { | ||
| recorded | ||
| } else { | ||
| recorded || conflicted.contains(&p) || worktree::disk_is_blob(&full, &blob_id) | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| if ours { | ||
| written.insert(p); | ||
| } else { | ||
| kept.push(p); | ||
| } | ||
| } | ||
| let skipped = self.materialize_private( | ||
| "merge --abort", | ||
| &mut store, | ||
|
|
@@ -917,7 +971,7 @@ impl Repo { | |
| // After the restore, so a failed restore leaves the sidecars to retry. | ||
| self.remove_conflict_sidecars_unless(&mut |p| prior.contains_key(p))?; | ||
| crate::merge_state::clear(&self.layout)?; | ||
| Ok(skipped) | ||
| Ok(crate::repo::AbortReport { skipped, kept }) | ||
| } | ||
|
|
||
| /// Build inner trees + snapshot from a decided file map, seal everything | ||
|
|
@@ -1522,6 +1576,21 @@ impl Repo { | |
| } | ||
| } | ||
| } | ||
| // Drop the prior-tip paths the merge deleted, with the removals | ||
| // above and before any conflict output is written: a failure here | ||
| // then leaves no markers without a merge state, and is never | ||
| // swallowed (completion would resurrect the file from disk). | ||
| for p in prior_paths.difference(&merged_paths) { | ||
| if !sparse.matches(p) { | ||
| continue; | ||
| } | ||
| let full = worktree::safe_join(&self.layout.root, p)?; | ||
| match std::fs::remove_file(full) { | ||
| Ok(()) => {} | ||
| Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} | ||
| Err(e) => return Err(e.into()), | ||
|
Comment on lines
+1587
to
+1591
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a conflicted private merge deletes multiple prior-tip paths and a later unlink fails, this loop has already removed earlier paths but AGENTS.md reference: AGENTS.md:L11-L13 Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
| // Record what is about to land on disk in plaintext, and the | ||
| // sidecars, before writing it: completion and abort need both | ||
| // (see `conflict_plaintext`). Nothing here is sealed, and it names | ||
|
|
@@ -1563,13 +1632,6 @@ impl Repo { | |
| let full = worktree::safe_join(&self.layout.root, name)?; | ||
| std::fs::write(&full, bytes)?; | ||
| } | ||
| for p in prior_paths.difference(&merged_paths) { | ||
| if !sparse.matches(p) { | ||
| continue; | ||
| } | ||
| let full = worktree::safe_join(&self.layout.root, p)?; | ||
| let _ = std::fs::remove_file(full); | ||
| } | ||
| crate::merge_state::write(&self.layout, &theirs, &fm.conflicts, None)?; | ||
| return Err(Error::MergeConflicts(fm.conflicts.len())); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a user edits an ordinary, non-conflicted private-tip path after a private merge stops, adding every prior path to
writtencausesmaterialize_privateto overwrite that edit unconditionally with the tip version. Unlike the public abort path, this never compares the file with the stopped or restored tree and never adds it toAbortReport::kept, so aborting a private merge silently loses unrelated post-stop work; apply the same ownership comparison before restoring target paths.AGENTS.md reference: AGENTS.md:L3-L5
Useful? React with 👍 / 👎.