Skip to content

Commit

Permalink
Allow \0 escape for nulls
Browse files Browse the repository at this point in the history
This allows safely getting e.g. multiple descriptions, and knowing where the
boundaries are
  • Loading branch information
Dr-Emann committed Sep 13, 2023
1 parent 63b02ab commit dcbb54e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `jj op log` now supports `--no-graph`.

* Templates now support an additional escape: `\0`. This will output a literal
null byte. This may be useful for e.g.
`jj log -T 'description ++ "\0"' --no-graph` to output descriptions only, but
be able to tell where the boundaries are

### Fixed bugs

## [0.9.0] - 2023-09-06
Expand Down
2 changes: 1 addition & 1 deletion cli/src/template.pest
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

whitespace = _{ " " | "\t" | "\r" | "\n" | "\x0c" }

escape = @{ "\\" ~ ("t" | "r" | "n" | "\"" | "\\") }
escape = @{ "\\" ~ ("t" | "r" | "n" | "0" | "\"" | "\\") }
literal_char = @{ !("\"" | "\\") ~ ANY }
raw_literal = @{ literal_char+ }
literal = { "\"" ~ (raw_literal | escape)* ~ "\"" }
Expand Down
13 changes: 7 additions & 6 deletions cli/src/template_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,13 @@ fn parse_string_literal(pair: Pair<Rule>) -> String {
Rule::raw_literal => {
result.push_str(part.as_str());
}
Rule::escape => match part.as_str().as_bytes()[1] as char {
'"' => result.push('"'),
'\\' => result.push('\\'),
't' => result.push('\t'),
'r' => result.push('\r'),
'n' => result.push('\n'),
Rule::escape => match &part.as_str()[1..] {
"\"" => result.push('"'),
"\\" => result.push('\\'),
"t" => result.push('\t'),
"r" => result.push('\r'),
"n" => result.push('\n'),
"0" => result.push('\0'),
char => panic!("invalid escape: \\{char:?}"),
},
_ => panic!("unexpected part of string: {part:?}"),
Expand Down
36 changes: 36 additions & 0 deletions cli/tests/test_log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,42 @@ fn test_log_with_or_without_diff() {
"###);
}

#[test]
fn test_log_null_terminate_multiline_descriptions() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");

test_env.jj_cmd_success(
&repo_path,
&["commit", "-m", "commit 1 line 1", "-m", "commit 1 line 2"],
);
test_env.jj_cmd_success(
&repo_path,
&["commit", "-m", "commit 2 line 1", "-m", "commit 2 line 2"],
);
test_env.jj_cmd_success(
&repo_path,
&["describe", "-m", "commit 3 line 1", "-m", "commit 3 line 2"],
);

let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-r",
"~root()",
"-T",
r#"description ++ "\0""#,
"--no-graph",
],
);
insta::assert_debug_snapshot!(
stdout,
@r###""commit 3 line 1\n\ncommit 3 line 2\n\0commit 2 line 1\n\ncommit 2 line 2\n\0commit 1 line 1\n\ncommit 1 line 2\n\0""###
)
}

#[test]
fn test_log_shortest_accessors() {
let test_env = TestEnvironment::default();
Expand Down
21 changes: 21 additions & 0 deletions cli/tests/test_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ fn test_op_log_no_graph() {
"###);
}

#[test]
fn test_op_log_no_graph_null_terminated() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "message1"]);
test_env.jj_cmd_success(&repo_path, &["commit", "-m", "message2"]);

let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"op",
"log",
"--no-graph",
"--template",
r#"id.short(4) ++ "\0""#,
],
);
insta::assert_debug_snapshot!(stdout, @r###""c8b0\07277\019b8\0f1c4\0""###);
}

#[test]
fn test_op_log_template() {
let test_env = TestEnvironment::default();
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/test_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ fn test_templater_list_method() {

insta::assert_snapshot!(render(r#""".lines().join("|")"#), @"");
insta::assert_snapshot!(render(r#""a\nb\nc".lines().join("|")"#), @"a|b|c");
// Null seperator

Check failure on line 282 in cli/tests/test_templater.rs

View workflow job for this annotation

GitHub Actions / Codespell

seperator ==> separator
insta::assert_snapshot!(render(r#""a\nb\nc".lines().join("\0")"#), @"a\0b\0c");
// Keyword as separator
insta::assert_snapshot!(render(r#""a\nb\nc".lines().join(commit_id.short(2))"#), @"a00b00c");

Expand Down

0 comments on commit dcbb54e

Please sign in to comment.