From b8cc6fc3c86556bf9e6f2921d93903a73e29f886 Mon Sep 17 00:00:00 2001 From: Vamsi Avula Date: Sat, 12 Aug 2023 10:19:27 +0000 Subject: [PATCH] cli: trim the description from editor before using it We anyway trim the newlines eventually and this just does that eagerly so we output the "correct" description back to stdout (on describe for example, we'd now print the first non empty line). --- cli/src/commands/mod.rs | 7 +++---- cli/tests/test_describe_command.rs | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index f2edc96b6..a9701b16e 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -1922,13 +1922,12 @@ fn edit_description( // 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(); - // Normalize line ending, remove trailing blank lines. - let mut description = description + // Normalize line ending, remove leading and trailing blank lines. + let description = description .lines() .filter(|line| !line.starts_with("JJ: ")) .join("\n"); - description.truncate(description.trim_end_matches('\n').len()); - Ok(text_util::complete_newline(description)) + Ok(text_util::complete_newline(description.trim_matches('\n'))) } fn edit_sparse( diff --git a/cli/tests/test_describe_command.rs b/cli/tests/test_describe_command.rs index 05c1da5f2..d49d74370 100644 --- a/cli/tests/test_describe_command.rs +++ b/cli/tests/test_describe_command.rs @@ -94,10 +94,24 @@ fn test_describe() { Nothing changed. "###); + // Multi-line description starting with newlines + std::fs::write(&edit_script, "write\n\n\nline1\nline2").unwrap(); + let stdout = test_env.jj_cmd_success(&repo_path, &["describe"]); + insta::assert_snapshot!(stdout, @r#" + Working copy now at: qpvuntsm 13f903c1 (empty) line1 + Parent commit : zzzzzzzz 00000000 (empty) (no description set) + "#); + let stdout = + test_env.jj_cmd_success(&repo_path, &["log", "--no-graph", "-r@", "-Tdescription"]); + insta::assert_snapshot!(stdout, @r#" + line1 + line2 + "#); + // Clear description let stdout = test_env.jj_cmd_success(&repo_path, &["describe", "-m", ""]); insta::assert_snapshot!(stdout, @r###" - Working copy now at: qpvuntsm d6957294 (empty) (no description set) + Working copy now at: qpvuntsm 3196270d (empty) (no description set) Parent commit : zzzzzzzz 00000000 (empty) (no description set) "###); std::fs::write(&edit_script, "write\n").unwrap();