mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-18 14:31:30 +03:00
use gix::tempfile
in place of the tempfile
.
This allows to unify handling of filewrites in more places.
This commit is contained in:
parent
813ba8f2d6
commit
6033a62a94
@ -8,6 +8,7 @@ publish = false
|
||||
[dev-dependencies]
|
||||
once_cell = "1.19"
|
||||
pretty_assertions = "1.4"
|
||||
tempfile = "3.10"
|
||||
gitbutler-testsupport.workspace = true
|
||||
|
||||
[dependencies]
|
||||
@ -52,7 +53,6 @@ urlencoding = "2.1.3"
|
||||
uuid.workspace = true
|
||||
walkdir = "2.5.0"
|
||||
zip = "0.6.5"
|
||||
tempfile = "3.10"
|
||||
gitbutler-git.workspace = true
|
||||
|
||||
[features]
|
||||
|
@ -54,11 +54,7 @@ impl Storage {
|
||||
AutoRemove::Tempfile,
|
||||
)?;
|
||||
tempfile.write_all(content.as_bytes())?;
|
||||
match tempfile.persist(file_path) {
|
||||
Ok(Some(_opened_file)) => Ok(()),
|
||||
Ok(None) => unreachable!("BUG: a signal has caused the tempfile to be removed, but we didn't install a handler"),
|
||||
Err(err) => Err(err.error)
|
||||
}
|
||||
persist_tempfile(tempfile, file_path)
|
||||
}
|
||||
|
||||
/// Delete the file or directory at `rela_path`.
|
||||
@ -84,3 +80,16 @@ impl Storage {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn persist_tempfile(
|
||||
tempfile: gix::tempfile::Handle<gix::tempfile::handle::Writable>,
|
||||
to_path: impl AsRef<Path>,
|
||||
) -> std::io::Result<()> {
|
||||
match tempfile.persist(to_path) {
|
||||
Ok(Some(_opened_file)) => Ok(()),
|
||||
Ok(None) => unreachable!(
|
||||
"BUG: a signal has caused the tempfile to be removed, but we didn't install a handler"
|
||||
),
|
||||
Err(err) => Err(err.error),
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use gix::tempfile::{AutoRemove, ContainingDirectory};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
@ -5,6 +6,7 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::storage;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{target::Target, Branch};
|
||||
@ -151,10 +153,11 @@ impl VirtualBranchesHandle {
|
||||
|
||||
fn write<P: AsRef<Path>>(file_path: P, virtual_branches: &VirtualBranches) -> anyhow::Result<()> {
|
||||
let contents = toml::to_string(&virtual_branches)?;
|
||||
let temp_file = tempfile::NamedTempFile::new_in(file_path.as_ref().parent().unwrap())?;
|
||||
let (mut file, temp_path) = temp_file.keep()?;
|
||||
file.write_all(contents.as_bytes())?;
|
||||
drop(file);
|
||||
std::fs::rename(temp_path, file_path.as_ref())?;
|
||||
Ok(())
|
||||
let mut temp_file = gix::tempfile::new(
|
||||
file_path.as_ref().parent().unwrap(),
|
||||
ContainingDirectory::Exists,
|
||||
AutoRemove::Tempfile,
|
||||
)?;
|
||||
temp_file.write_all(contents.as_bytes())?;
|
||||
Ok(storage::persist_tempfile(temp_file, file_path)?)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user