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

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.
This commit is contained in:
Yuya Nishihara 2023-01-21 16:52:17 +09:00
parent 1a4b5c5ee6
commit a7541e1ba4
5 changed files with 38 additions and 23 deletions

View file

@ -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<Store> {

View file

@ -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)
"###);
}

View file

@ -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)
"###);
}

View file

@ -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!(

View file

@ -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###"