diff --git a/Cargo.lock b/Cargo.lock index db7c6e8a2..843b8a63e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -559,6 +559,7 @@ dependencies = [ "pest_derive", "protobuf", "protobuf-codegen-pure", + "rand 0.8.0", "regex", "serde_json", "tempfile", diff --git a/Cargo.toml b/Cargo.toml index 1f60b1b3a..0f105d475 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ pest = "2.1.3" pest_derive = "2.1.0" protobuf = { version = "2.22.1", features = ["with-bytes"] } protobuf-codegen-pure = "2.22.1" +rand = "0.8.0" serde_json = "1.0.60" tempfile = "3.1.0" thiserror = "1.0.22" diff --git a/src/commands.rs b/src/commands.rs index 3ce369072..7c1aa2c6d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1310,13 +1310,12 @@ fn cmd_obslog( } fn edit_description(repo: &ReadonlyRepo, description: &str) -> String { - // TODO: Where should this file live? The current location prevents two - // concurrent `jj describe` calls. - let description_file_path = repo.repo_path().join("description"); + let random: u32 = rand::random(); + let description_file_path = repo.repo_path().join(format!("description-{}", random)); { let mut description_file = OpenOptions::new() .write(true) - .create(true) + .create_new(true) .truncate(true) .open(&description_file_path) .unwrap_or_else(|_| panic!("failed to open {:?} for write", &description_file_path)); @@ -1342,7 +1341,11 @@ fn edit_description(repo: &ReadonlyRepo, description: &str) -> String { .unwrap_or_else(|_| panic!("failed to open {:?} for read", &description_file_path)); let mut buf = vec![]; description_file.read_to_end(&mut buf).unwrap(); - String::from_utf8(buf).unwrap() + let description = String::from_utf8(buf).unwrap(); + // Delete the file only if everything went well. + // TODO: Tell the user the name of the file we left behind. + std::fs::remove_file(description_file_path).ok(); + description } fn cmd_describe(