From a7541e1ba42ecc75104373e60b026959082a30fd Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 21 Jan 2023 16:52:17 +0900 Subject: [PATCH] repo: add workaround for shortest prefix calculation of root ids This is ugly, but we need a special case because root_change_id and root_commit_id aren't equal but share the same prefix bytes. In practice, no one would care for the shortest root id prefix, but we'll need to deal with a similar problem when migrating prefix id resolution to repo layer. --- lib/src/repo.rs | 35 +++++++++++++++++++++++++---------- tests/test_alias.rs | 2 +- tests/test_commit_template.rs | 14 +++++++------- tests/test_log_command.rs | 4 ++-- tests/test_revset_output.rs | 6 +++--- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 91778446f..b63e6db7c 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -270,16 +270,31 @@ impl ReadonlyRepo { } pub fn shortest_unique_prefix_length(&self, target_id_bytes: &[u8]) -> usize { - // For `len = index.shortest(id)`, a prefix of length `len` will disambiguate - // `id` from all other ids in the index. This will be just as true for - // `max(len, anything_else)`, so a max of such lengths will disambiguate in all - // indices. - cmp::max( - self.commit_id_index() - .shortest_unique_prefix_len(target_id_bytes), - self.change_id_index() - .shortest_unique_prefix_len(target_id_bytes), - ) + let root_commit_id = self.store().root_commit_id(); + let root_change_id = backend::root_change_id(); + if target_id_bytes == root_commit_id.as_bytes() + || target_id_bytes == root_change_id.as_bytes() + { + // The root change/commit ids share the same prefix, and they are found in both + // indices with different lengths. So we have to feed bytes of valid lengths. + cmp::max( + self.commit_id_index() + .shortest_unique_prefix_len(root_commit_id.as_bytes()), + self.change_id_index() + .shortest_unique_prefix_len(root_change_id.as_bytes()), + ) + } else { + // For `len = index.shortest(id)`, a prefix of length `len` will disambiguate + // `id` from all other ids in the index. This will be just as true for + // `max(len, anything_else)`, so a max of such lengths will disambiguate in all + // indices. + cmp::max( + self.commit_id_index() + .shortest_unique_prefix_len(target_id_bytes), + self.change_id_index() + .shortest_unique_prefix_len(target_id_bytes), + ) + } } pub fn store(&self) -> &Arc { diff --git a/tests/test_alias.rs b/tests/test_alias.rs index 4f1e437e3..3b75a1cb3 100644 --- a/tests/test_alias.rs +++ b/tests/test_alias.rs @@ -131,7 +131,7 @@ fn test_alias_cannot_override_builtin() { // Alias should be ignored let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "root"]); insta::assert_snapshot!(stdout, @r###" - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); } diff --git a/tests/test_commit_template.rs b/tests/test_commit_template.rs index b1e802c08..bc0fc7b24 100644 --- a/tests/test_commit_template.rs +++ b/tests/test_commit_template.rs @@ -68,7 +68,7 @@ fn test_log_default() { | (empty) description 1 o 9a[45c67d3e] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae] | add a file - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); @@ -78,7 +78,7 @@ fn test_log_default() { | (empty) description 1 o 9a[45c67d3e] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae] | add a file - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); @@ -99,7 +99,7 @@ fn test_log_default() { | (empty) description 1 o 9a[45c67d3e] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae] | add a file - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); @@ -110,7 +110,7 @@ fn test_log_default() { (empty) description 1 9a[45c67d3e] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae] add a file - 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); } @@ -128,7 +128,7 @@ fn test_log_default_divergence() { insta::assert_snapshot!(stdout, @r###" @ 9[a45c67d3e] test.user@example.com 2001-02-03 04:05:08.000 +07:00 7[a17d52e63] | description 1 - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); @@ -144,7 +144,7 @@ fn test_log_default_divergence() { | description 2 | @ 9[a45c67d3e]?? test.user@example.com 2001-02-03 04:05:08.000 +07:00 7[a17d52e63] |/ description 1 - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); @@ -155,7 +155,7 @@ fn test_log_default_divergence() { | description 2 | @ 9[a45c67d3e]?? test.user@example.com 2001-02-03 04:05:08.000 +07:00 7[a17d52e63] |/ description 1 - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); } diff --git a/tests/test_log_command.rs b/tests/test_log_command.rs index c45b7a608..b8b11ab51 100644 --- a/tests/test_log_command.rs +++ b/tests/test_log_command.rs @@ -356,7 +356,7 @@ fn test_log_prefix_highlight_counts_hidden_commits() { test_env.jj_cmd_success(&repo_path, &["log", "-r", "all()", "-T", prefix_format]), @r###" @ Change 9[a45c67d3e] initial b[a1a30916d] original - o Change 000000000000 (no description set) 000000000000 + o Change 0[000000000] (no description set) 0[000000000] "### ); for i in 1..100 { @@ -376,7 +376,7 @@ fn test_log_prefix_highlight_counts_hidden_commits() { test_env.jj_cmd_success(&repo_path, &["log", "-r", "all()", "-T", prefix_format]), @r###" @ Change 9a4[5c67d3e] commit99 de[3177d2ac] original - o Change 000000000000 (no description set) 000000000000 + o Change 000[0000000] (no description set) 000[0000000] "### ); insta::assert_snapshot!( diff --git a/tests/test_revset_output.rs b/tests/test_revset_output.rs index 1f5407f4d..c2834c312 100644 --- a/tests/test_revset_output.rs +++ b/tests/test_revset_output.rs @@ -161,13 +161,13 @@ fn test_alias() { let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "my-root"]); insta::assert_snapshot!(stdout, @r###" - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "identity(my-root)"]); insta::assert_snapshot!(stdout, @r###" - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); @@ -263,7 +263,7 @@ fn test_bad_alias_decl() { .assert() .success(); insta::assert_snapshot!(get_stdout_string(&assert), @r###" - o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + o 0[000000000] 1970-01-01 00:00:00.000 +00:00 0[000000000] (empty) (no description set) "###); insta::assert_snapshot!(get_stderr_string(&assert), @r###"