ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: leverage tempfile to create commit description file with random suffix

This should be more reliable as there might be a stale description file
having the exact same random suffix.

Also removes panic on io::Error.
This commit is contained in:
Yuya Nishihara 2022-12-21 10:19:13 +09:00
parent 0285a203a2
commit cc2091aad6

View file

@ -1796,25 +1796,22 @@ fn edit_description(
repo: &ReadonlyRepo,
description: &str,
) -> Result<String, CommandError> {
let random: u32 = rand::random();
let description_file_path = repo.repo_path().join(format!("description-{random}.txt"));
{
let mut description_file = OpenOptions::new()
.write(true)
.create_new(true)
.truncate(true)
.open(&description_file_path)
.unwrap_or_else(|_| {
panic!(
"failed to open {} for write",
description_file_path.display()
)
});
description_file.write_all(description.as_bytes()).unwrap();
description_file
.write_all(b"\nJJ: Lines starting with \"JJ: \" (like this one) will be removed.\n")
.unwrap();
}
let description_file_path = (|| -> Result<_, io::Error> {
let mut file = tempfile::Builder::new()
.prefix("description-")
.suffix(".txt")
.tempfile_in(repo.repo_path())?;
file.write_all(description.as_bytes())?;
file.write_all(b"\nJJ: Lines starting with \"JJ: \" (like this one) will be removed.\n")?;
let (_, path) = file.keep().map_err(|e| e.error)?;
Ok(path)
})()
.map_err(|e| {
user_error(format!(
r#"Failed to create description file in "{path}": {e}"#,
path = repo.repo_path().display()
))
})?;
let editor: FullCommandArgs = ui
.settings()