diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 43d395d4c..4db653688 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -17,7 +17,7 @@ use std::fmt::{Debug, Formatter}; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::sync::Arc; -use std::{cmp, fs, io}; +use std::{fs, io}; use itertools::Itertools; use once_cell::sync::OnceCell; @@ -113,10 +113,10 @@ impl<'a> RepoRef<'a> { } } - pub fn shortest_unique_id_prefix_len(&self, target_id_bytes: &[u8]) -> usize { + pub fn shortest_unique_change_id_prefix_len(&self, target_id: &ChangeId) -> usize { match self { - RepoRef::Readonly(repo) => repo.shortest_unique_id_prefix_len(target_id_bytes), - RepoRef::Mutable(_) => target_id_bytes.len() * 2, // TODO + RepoRef::Readonly(repo) => repo.shortest_unique_change_id_prefix_len(target_id), + RepoRef::Mutable(_) => target_id.as_bytes().len() * 2, // TODO } } } @@ -292,17 +292,8 @@ impl ReadonlyRepo { .resolve_prefix_with(prefix, |&pos| index.entry_by_pos(pos)) } - pub fn shortest_unique_id_prefix_len(&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.index() - .shortest_unique_commit_id_prefix_len(&CommitId::from_bytes(target_id_bytes)), - self.change_id_index() - .shortest_unique_prefix_len(&ChangeId::from_bytes(target_id_bytes)), - ) + pub fn shortest_unique_change_id_prefix_len(&self, target_id: &ChangeId) -> usize { + self.change_id_index().shortest_unique_prefix_len(target_id) } pub fn store(&self) -> &Arc { diff --git a/src/templater.rs b/src/templater.rs index 65ca7b870..8198f7610 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cmp::{max, min}; +use std::cmp::max; use std::io; use itertools::Itertools; @@ -583,17 +583,15 @@ impl<'a> CommitOrChangeId<'a> { /// The length of the id printed will be the maximum of `total_len` and the /// length of the shortest unique prefix pub fn shortest(&self, total_len: usize) -> ShortestIdPrefix { - // If this id is a prefix of another id in different space, (e.g. - // change_id.starts_with(commit_id)) shortest_unique_id_prefix_len() - // exceeds the actual hex.len(). Note that the full id is ambiguous - // in that case. - // - // TODO: maybe split commit_id/change_id spaces and remove min(hex.len())? let mut hex = self.hex(); - let prefix_len = min( - self.repo.shortest_unique_id_prefix_len(&self.id_bytes), - hex.len(), - ); + let prefix_len = if self.is_commit_id { + self.repo + .index() + .shortest_unique_commit_id_prefix_len(&CommitId::from_bytes(&self.id_bytes)) + } else { + self.repo + .shortest_unique_change_id_prefix_len(&ChangeId::from_bytes(&self.id_bytes)) + }; hex.truncate(max(prefix_len, total_len)); let rest = hex.split_off(prefix_len); ShortestIdPrefix { prefix: hex, rest } diff --git a/tests/test_commit_template.rs b/tests/test_commit_template.rs index d8e0ae212..e92d58f2b 100644 --- a/tests/test_commit_template.rs +++ b/tests/test_commit_template.rs @@ -87,9 +87,9 @@ fn test_log_default() { &["log", "--config-toml", "ui.unique-prefixes='brackets'"], ); insta::assert_snapshot!(stdout, @r###" - @ k[kmpptxzrspx] test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9d[e54178d59d] + @ k[kmpptxzrspx] test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9[de54178d59d] │ (empty) description 1 - o qp[vuntsmwlqt] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae97] + o q[pvuntsmwlqt] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[291e264ae97] │ add a file o z[zzzzzzzzzzz] 1970-01-01 00:00:00.000 +00:00 0[00000000000] (empty) (no description set) @@ -105,9 +105,9 @@ fn test_log_default() { ], ); insta::assert_snapshot!(stdout, @r###" - @ k[k] test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9d + @ k[k] test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9[d] │ (empty) description 1 - o qp test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[2] + o q[p] test.user@example.com 2001-02-03 04:05:08.000 +07:00 4[2] │ add a file o z[z] 1970-01-01 00:00:00.000 +00:00 0[0] (empty) (no description set) @@ -124,9 +124,9 @@ fn test_log_default() { ], ); insta::assert_snapshot!(stdout, @r###" - @ kkmpptxzrspx test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9de54178d59d + @ kkmpptxzrspx test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9de54178d59d │ (empty) description 1 - o qpvuntsmwlqt test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97 + o qpvuntsmwlqt test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97 │ add a file o zzzzzzzzzzzz 1970-01-01 00:00:00.000 +00:00 000000000000 (empty) (no description set) @@ -143,9 +143,9 @@ fn test_log_default() { ], ); insta::assert_snapshot!(stdout, @r###" - @ k test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9d + @ k test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9 │ (empty) description 1 - o qp test.user@example.com 2001-02-03 04:05:08.000 +07:00 4 + o q test.user@example.com 2001-02-03 04:05:08.000 +07:00 4 │ add a file o z 1970-01-01 00:00:00.000 +00:00 0 (empty) (no description set) @@ -186,9 +186,9 @@ fn test_log_default() { // Color let stdout = test_env.jj_cmd_success(&repo_path, &["log", "--color=always"]); insta::assert_snapshot!(stdout, @r###" - @ kkmpptxzrspx test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9de54178d59d + @ kkmpptxzrspx test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9de54178d59d │ (empty) description 1 - o qpvuntsmwlqt test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97 + o qpvuntsmwlqt test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97 │ add a file o zzzzzzzzzzzz 1970-01-01 00:00:00.000 +00:00 000000000000 (empty) (no description set) @@ -197,9 +197,9 @@ fn test_log_default() { // Color without graph let stdout = test_env.jj_cmd_success(&repo_path, &["log", "--color=always", "--no-graph"]); insta::assert_snapshot!(stdout, @r###" - kkmpptxzrspx test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9de54178d59d + kkmpptxzrspx test.user@example.com 2001-02-03 04:05:09.000 +07:00 my-branch 9de54178d59d (empty) description 1 - qpvuntsmwlqt test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97 + qpvuntsmwlqt test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97 add a file zzzzzzzzzzzz 1970-01-01 00:00:00.000 +00:00 000000000000 (empty) (no description set) diff --git a/tests/test_log_command.rs b/tests/test_log_command.rs index a6d04041c..791424f57 100644 --- a/tests/test_log_command.rs +++ b/tests/test_log_command.rs @@ -332,49 +332,49 @@ fn test_log_prefix_highlight_brackets() { insta::assert_snapshot!( test_env.jj_cmd_success(&repo_path, &["log", "-r", ":@", "-T", &prefix_format(Some(12))]), @r###" - @ Change wqn[wkozpkust] commit9 03f[51310b83e] + @ Change wq[nwkozpkust] commit9 03[f51310b83e] o Change km[kuslswpqwq] commit8 f7[7fb1909080] - o Change kpq[xywonksrl] commit7 e7[15ad5db646] - o Change znkk[psqqskkl] commit6 38[622e54e2e5] - o Change yo[stqsxwqrlt] commit5 0cf4[2f60199c] + o Change kp[qxywonksrl] commit7 e7[15ad5db646] + o Change zn[kkpsqqskkl] commit6 38[622e54e2e5] + o Change yo[stqsxwqrlt] commit5 0cf[42f60199c] o Change vr[uxwmqvtpmx] commit4 9e[6015e4e622] o Change yq[osqzytrlsw] commit3 06f[34d9b1475] - o Change royx[mykxtrkr] commit2 1f[99a5e19891] + o Change ro[yxmykxtrkr] commit2 1f[99a5e19891] o Change mz[vwutvlkqwt] commit1 7b[1f7dee65b4] o Change qpv[untsmwlqt] initial ba1[a30916d29] original - o Change zzz[zzzzzzzzz] 000[000000000] + o Change zzz[zzzzzzzzz] 00[0000000000] "### ); insta::assert_snapshot!( test_env.jj_cmd_success(&repo_path, &["log", "-r", ":@", "-T", &prefix_format(Some(3))]), @r###" - @ Change wqn commit9 03f + @ Change wq[n] commit9 03[f] o Change km[k] commit8 f7[7] - o Change kpq commit7 e7[1] - o Change znkk commit6 38[6] - o Change yo[s] commit5 0cf4 + o Change kp[q] commit7 e7[1] + o Change zn[k] commit6 38[6] + o Change yo[s] commit5 0cf o Change vr[u] commit4 9e[6] o Change yq[o] commit3 06f - o Change royx commit2 1f[9] + o Change ro[y] commit2 1f[9] o Change mz[v] commit1 7b[1] o Change qpv initial ba1 original - o Change zzz 000 + o Change zzz 00[0] "### ); insta::assert_snapshot!( test_env.jj_cmd_success(&repo_path, &["log", "-r", ":@", "-T", &prefix_format(None)]), @r###" - @ Change wqn commit9 03f + @ Change wq commit9 03 o Change km commit8 f7 - o Change kpq commit7 e7 - o Change znkk commit6 38 - o Change yo commit5 0cf4 + o Change kp commit7 e7 + o Change zn commit6 38 + o Change yo commit5 0cf o Change vr commit4 9e o Change yq commit3 06f - o Change royx commit2 1f + o Change ro commit2 1f o Change mz commit1 7b o Change qpv initial ba1 original - o Change zzz 000 + o Change zzz 00 "### ); } @@ -438,17 +438,17 @@ fn test_log_prefix_highlight_styled() { ); insta::assert_snapshot!(stdout, @r###" - @ Change wqnwkozpkust commit9 03f51310b83e + @ Change wqnwkozpkust commit9 03f51310b83e o Change kmkuslswpqwq commit8 f77fb1909080 - o Change kpqxywonksrl commit7 e715ad5db646 - o Change znkkpsqqskkl commit6 38622e54e2e5 - o Change yostqsxwqrlt commit5 0cf42f60199c + o Change kpqxywonksrl commit7 e715ad5db646 + o Change znkkpsqqskkl commit6 38622e54e2e5 + o Change yostqsxwqrlt commit5 0cf42f60199c o Change vruxwmqvtpmx commit4 9e6015e4e622 o Change yqosqzytrlsw commit3 06f34d9b1475 - o Change royxmykxtrkr commit2 1f99a5e19891 + o Change royxmykxtrkr commit2 1f99a5e19891 o Change mzvwutvlkqwt commit1 7b1f7dee65b4 o Change qpvuntsmwlqt initial ba1a30916d29 original - o Change zzzzzzzzzzzz 000000000000 + o Change zzzzzzzzzzzz 000000000000 "### ); let stdout = test_env.jj_cmd_success( @@ -464,17 +464,17 @@ fn test_log_prefix_highlight_styled() { ); insta::assert_snapshot!(stdout, @r###" - @ Change wqn commit9 03f + @ Change wqn commit9 03f o Change kmk commit8 f77 - o Change kpq commit7 e71 - o Change znkk commit6 386 - o Change yos commit5 0cf4 + o Change kpq commit7 e71 + o Change znk commit6 386 + o Change yos commit5 0cf o Change vru commit4 9e6 o Change yqo commit3 06f - o Change royx commit2 1f9 + o Change roy commit2 1f9 o Change mzv commit1 7b1 o Change qpv initial ba1 original - o Change zzz 000 + o Change zzz 000 "### ); let stdout = test_env.jj_cmd_success( @@ -490,17 +490,17 @@ fn test_log_prefix_highlight_styled() { ); insta::assert_snapshot!(stdout, @r###" - @ Change wqn commit9 03f + @ Change wq commit9 03 o Change km commit8 f7 - o Change kpq commit7 e7 - o Change znkk commit6 38 - o Change yo commit5 0cf4 + o Change kp commit7 e7 + o Change zn commit6 38 + o Change yo commit5 0cf o Change vr commit4 9e o Change yq commit3 06f - o Change royx commit2 1f + o Change ro commit2 1f o Change mz commit1 7b o Change qpv initial ba1 original - o Change zzz 000 + o Change zzz 00 "### ); } @@ -538,10 +538,10 @@ fn test_log_prefix_highlight_counts_hidden_commits() { insta::assert_snapshot!( test_env.jj_cmd_success(&repo_path, &["log", "-T", prefix_format]), @r###" - @ Change wqnw[kozpkust] 44[4c3c5066d3] - │ o Change qp[vuntsmwlqt] initial ba[1a30916d29] original + @ Change w[qnwkozpkust] 44[4c3c5066d3] + │ o Change q[pvuntsmwlqt] initial ba[1a30916d29] original ├─╯ - o Change zz[zzzzzzzzzz] 00[0000000000] + o Change z[zzzzzzzzzzz] 00[0000000000] "### ); insta::assert_snapshot!( @@ -553,7 +553,7 @@ fn test_log_prefix_highlight_counts_hidden_commits() { insta::assert_snapshot!( test_env.jj_cmd_success(&repo_path, &["log", "-r", "d0", "-T", prefix_format]), @r###" - o Change psz[srknsmxqw] extra d0[947f34cec4] + o Change p[szsrknsmxqw] extra d0[947f34cec4] │ ~ "###