Skip to content

Commit

Permalink
Convert creates tempdir in parent of destination
Browse files Browse the repository at this point in the history
`git prole convert` now creates a tempdir in the parent of the
determined destination of the converted repo.
This prevents some errors when the /tmp directory is on a different
filesystem, such as a tmpfs.

The tempdir will be deleted if and only if it is empty after conversion.
This should always be the case and is to prevent any data loss.

Fixes issue #96
  • Loading branch information
lordkekz committed Oct 28, 2024
1 parent c204a0b commit 35b6cc7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
18 changes: 17 additions & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ where
// suffix removed
// - Otherwise just use the git dir path.

let tempdir = Utf8TempDir::new()?.into_path();
// Tests:
// - `convert_from_bare`
// - `convert_bare_dot_git`
Expand All @@ -227,8 +226,14 @@ where
let destination_name = destination
.file_name()
.ok_or_else(|| miette!("Destination has no basename: {destination}"))?;
let destination_parent = destination
.parent()
.ok_or_else(|| miette!("Destination has no parent: {destination}"))?
.to_path_buf();
tracing::debug!(%destination, "Destination determined");

let tempdir = Utf8TempDir::new(&destination_parent)?.into_path();

let default_branch = match opts.default_branch {
// Tests:
// - `convert_explicit_default_branch`
Expand Down Expand Up @@ -553,6 +558,17 @@ where
);
tracing::info!("You may need to `cd .` to refresh your shell");

// Make sure to delete the tempdir if it's empty
match fs_err::read_dir(&self.tempdir) {
Ok(rd) => {
if rd.count() == 1 {
tracing::info!("Temporary directory isn't empty: {0}", self.tempdir)
}
}
Err(err) => miette::bail!(err),
};
fs::remove_dir(&self.tempdir)?;

Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/utf8tempdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct Utf8TempDir {
}

impl Utf8TempDir {
pub fn new() -> miette::Result<Self> {
let inner = tempfile::tempdir().into_diagnostic()?;
pub fn new(parent_dir: &Utf8PathBuf) -> miette::Result<Self> {
let inner = tempfile::tempdir_in(parent_dir).into_diagnostic()?;
let path = inner.path().to_owned().try_into().into_diagnostic()?;
Ok(Self {
inner: Some(inner),
Expand Down
2 changes: 1 addition & 1 deletion test-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct GitProle {

impl GitProle {
pub fn new() -> miette::Result<Self> {
let mut tempdir = Utf8TempDir::new()?;
let mut tempdir = Utf8TempDir::new(&Utf8PathBuf::from("./"))?;

if std::env::var("KEEP_TEMP").is_ok() {
tempdir.persist();
Expand Down

0 comments on commit 35b6cc7

Please sign in to comment.