Skip to content

Commit 99b0d30

Browse files
authored
Merge pull request #10359 from Byron/fix2
Use a normal tempfile instead of a `gix` lock to avoid name clashes with tracked files
2 parents d851b59 + 9aee932 commit 99b0d30

File tree

3 files changed

+23
-35
lines changed

3 files changed

+23
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/but-workspace/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ md5 = "0.8.0"
3232
tracing.workspace = true
3333
# For SPMC channel
3434
flume = "0.11.1"
35+
tempfile.workspace = true
3536

3637
[dev-dependencies]
3738
but-testsupport.workspace = true

crates/but-workspace/src/tree_manipulation/discard_worktree_changes.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ mod file {
196196
use gix::filter::plumbing::driver::apply::Delay;
197197
use gix::object::tree::EntryKind;
198198
use gix::prelude::ObjectIdExt;
199+
use gix::tempfile::create_dir::Retries;
199200
use std::path::{Path, PathBuf};
200201

201202
pub enum RestoreMode {
@@ -240,29 +241,29 @@ mod file {
240241
let file_path = path_check.verified_path_allow_nonexisting(rela_path)?;
241242
match state.kind {
242243
EntryKind::Blob | EntryKind::BlobExecutable => {
243-
let mut dest_lock_file = locked_resource_at(wt_root, &file_path, state.kind)?;
244+
let mut tempfile = tempfile_in_root_with_permissions_at(wt_root, state.kind)?;
244245
let obj_in_git = state.id.attach(repo).object()?;
245246
let mut stream =
246247
pipeline.convert_to_worktree(&obj_in_git.data, rela_path, Delay::Forbid)?;
247-
std::io::copy(&mut stream, &mut dest_lock_file)?;
248-
249-
let (file_path, maybe_file) = match dest_lock_file.commit() {
248+
std::io::copy(&mut stream, &mut tempfile)?;
249+
gix::tempfile::create_dir::all(
250+
file_path.parent().context("encountered strange filepath")?,
251+
Retries::default(),
252+
)?;
253+
let file = match tempfile.persist(&file_path) {
250254
Ok(res) => res,
251255
Err(err) => {
252256
if err.error.kind() == std::io::ErrorKind::IsADirectory {
253257
// It's OK to remove everything that's in the way.
254258
// Alternatives to this is to let it be handled by the stack.
255-
std::fs::remove_dir_all(err.instance.resource_path())?;
256-
err.instance.commit()?
259+
std::fs::remove_dir_all(&file_path)?;
260+
err.file.persist(file_path)?
257261
} else {
258262
return Err(err.into());
259263
}
260264
}
261265
};
262-
update_index(match maybe_file {
263-
None => gix::index::fs::Metadata::from_path_no_follow(&file_path)?,
264-
Some(file) => gix::index::fs::Metadata::from_file(&file)?,
265-
})?;
266+
update_index(gix::index::fs::Metadata::from_file(&file)?)?;
266267
}
267268
EntryKind::Link => {
268269
let link_path = file_path;
@@ -525,34 +526,19 @@ mod file {
525526
}
526527
}
527528

528-
#[cfg(unix)]
529-
fn locked_resource_at(
529+
fn tempfile_in_root_with_permissions_at(
530530
root: PathBuf,
531-
path: &Path,
532531
kind: EntryKind,
533-
) -> anyhow::Result<gix::lock::File> {
534-
use std::os::unix::fs::PermissionsExt;
535-
Ok(
536-
gix::lock::File::acquire_to_update_resource_with_permissions(
537-
path,
538-
gix::lock::acquire::Fail::Immediately,
539-
Some(root),
540-
|| std::fs::Permissions::from_mode(kind as u32),
541-
)?,
542-
)
543-
}
532+
) -> anyhow::Result<tempfile::NamedTempFile> {
533+
#[cfg_attr(not(unix), allow(unused_mut))]
534+
let mut builder = tempfile::Builder::new();
544535

545-
#[cfg(windows)]
546-
fn locked_resource_at(
547-
root: PathBuf,
548-
path: &Path,
549-
_kind: EntryKind,
550-
) -> anyhow::Result<gix::lock::File> {
551-
Ok(gix::lock::File::acquire_to_update_resource(
552-
path,
553-
gix::lock::acquire::Fail::Immediately,
554-
Some(root),
555-
)?)
536+
#[cfg(unix)]
537+
{
538+
use std::os::unix::fs::PermissionsExt;
539+
builder.permissions(std::fs::Permissions::from_mode(kind as u32));
540+
}
541+
Ok(builder.tempfile_in(root)?)
556542
}
557543
}
558544

0 commit comments

Comments
 (0)