| 1 |
// Copyright 2023 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use itertools::Itertools as _; |
| 16 |
use jj_lib::backend::ChangeId; |
| 17 |
use jj_lib::backend::CommitId; |
| 18 |
use jj_lib::backend::MillisSinceEpoch; |
| 19 |
use jj_lib::backend::Signature; |
| 20 |
use jj_lib::backend::Timestamp; |
| 21 |
use jj_lib::config::ConfigLayer; |
| 22 |
use jj_lib::config::ConfigSource; |
| 23 |
use jj_lib::id_prefix::IdPrefixContext; |
| 24 |
use jj_lib::id_prefix::IdPrefixIndex; |
| 25 |
use jj_lib::index::ResolvedChangeTargets; |
| 26 |
use jj_lib::object_id::HexPrefix; |
| 27 |
use jj_lib::object_id::ObjectId as _; |
| 28 |
use jj_lib::object_id::PrefixResolution::AmbiguousMatch; |
| 29 |
use jj_lib::object_id::PrefixResolution::NoMatch; |
| 30 |
use jj_lib::object_id::PrefixResolution::SingleMatch; |
| 31 |
use jj_lib::op_store::RefTarget; |
| 32 |
use jj_lib::repo::MutableRepo; |
| 33 |
use jj_lib::repo::Repo as _; |
| 34 |
use jj_lib::revset::RevsetExpression; |
| 35 |
use jj_lib::settings::UserSettings; |
| 36 |
use pollster::FutureExt as _; |
| 37 |
use testutils::CommitBuilderExt as _; |
| 38 |
use testutils::TestRepo; |
| 39 |
use testutils::TestRepoBackend; |
| 40 |
use testutils::TestResult; |
| 41 |
|
| 42 |
fn stable_settings() -> UserSettings { |
| 43 |
let mut config = testutils::base_user_config(); |
| 44 |
let mut layer = ConfigLayer::empty(ConfigSource::User); |
| 45 |
layer |
| 46 |
.set_value("debug.commit-timestamp", "2001-02-03T04:05:06+07:00") |
| 47 |
.unwrap(); |
| 48 |
config.add_layer(layer); |
| 49 |
UserSettings::from_config(config).unwrap() |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn test_id_prefix() -> TestResult { |
| 54 |
let test_repo = TestRepo::init_with_backend(TestRepoBackend::Git); |
| 55 |
let repo = &test_repo.repo; |
| 56 |
let root_commit_id = repo.store().root_commit_id(); |
| 57 |
let root_change_id = repo.store().root_change_id(); |
| 58 |
|
| 59 |
let mut tx = repo.start_transaction(); |
| 60 |
let mut create_commit = |parent_id: &CommitId| { |
| 61 |
let signature = Signature { |
| 62 |
name: "Some One".to_string(), |
| 63 |
email: "[email protected]".to_string(), |
| 64 |
timestamp: Timestamp { |
| 65 |
timestamp: MillisSinceEpoch(0), |
| 66 |
tz_offset: 0, |
| 67 |
}, |
| 68 |
}; |
| 69 |
tx.repo_mut() |
| 70 |
.new_commit(vec![parent_id.clone()], repo.store().empty_merged_tree()) |
| 71 |
.set_author(signature.clone()) |
| 72 |
.set_committer(signature) |
| 73 |
.write_unwrap() |
| 74 |
}; |
| 75 |
let mut commits = vec![create_commit(root_commit_id)]; |
| 76 |
for _ in 0..25 { |
| 77 |
commits.push(create_commit(commits.last().unwrap().id())); |
| 78 |
} |
| 79 |
let repo = tx.commit("test").block_on()?; |
| 80 |
|
| 81 |
// Print the commit IDs and change IDs for reference |
| 82 |
let commit_prefixes = commits |
| 83 |
.iter() |
| 84 |
.enumerate() |
| 85 |
.map(|(i, commit)| format!("{} {}", &commit.id().hex()[..3], i)) |
| 86 |
.sorted() |
| 87 |
.join("\n"); |
| 88 |
insta::assert_snapshot!(commit_prefixes, @" |
| 89 |
0c8 9 |
| 90 |
18f 7 |
| 91 |
19a 10 |
| 92 |
37a 13 |
| 93 |
3b4 21 |
| 94 |
3c0 1 |
| 95 |
4ee 16 |
| 96 |
51f 4 |
| 97 |
56e 14 |
| 98 |
711 17 |
| 99 |
761 3 |
| 100 |
7b1 11 |
| 101 |
7c6 24 |
| 102 |
7f4 8 |
| 103 |
846 23 |
| 104 |
8d7 25 |
| 105 |
960 15 |
| 106 |
a30 12 |
| 107 |
b51 19 |
| 108 |
b97 22 |
| 109 |
b9d 5 |
| 110 |
bb4 2 |
| 111 |
c3a 18 |
| 112 |
c47 0 |
| 113 |
d3c 6 |
| 114 |
d54 20 |
| 115 |
"); |
| 116 |
let change_prefixes = commits |
| 117 |
.iter() |
| 118 |
.enumerate() |
| 119 |
.map(|(i, commit)| format!("{} {}", &commit.change_id().hex()[..3], i)) |
| 120 |
.sorted() |
| 121 |
.join("\n"); |
| 122 |
insta::assert_snapshot!(change_prefixes, @" |
| 123 |
026 9 |
| 124 |
030 13 |
| 125 |
1b5 6 |
| 126 |
26b 3 |
| 127 |
26c 8 |
| 128 |
271 10 |
| 129 |
439 2 |
| 130 |
44a 17 |
| 131 |
4e9 16 |
| 132 |
5b2 23 |
| 133 |
6c2 19 |
| 134 |
781 0 |
| 135 |
79f 14 |
| 136 |
7d5 24 |
| 137 |
86b 20 |
| 138 |
871 7 |
| 139 |
896 5 |
| 140 |
9e4 18 |
| 141 |
a2c 1 |
| 142 |
a63 22 |
| 143 |
b19 11 |
| 144 |
b93 4 |
| 145 |
bf5 21 |
| 146 |
c24 15 |
| 147 |
d64 12 |
| 148 |
fee 25 |
| 149 |
"); |
| 150 |
|
| 151 |
let prefix = |x| HexPrefix::try_from_hex(x).unwrap(); |
| 152 |
let shortest_commit_prefix_len = |index: &IdPrefixIndex, commit_id| { |
| 153 |
index |
| 154 |
.shortest_commit_prefix_len(repo.as_ref(), commit_id) |
| 155 |
.unwrap() |
| 156 |
}; |
| 157 |
let resolve_commit_prefix = |index: &IdPrefixIndex, prefix: HexPrefix| { |
| 158 |
index.resolve_commit_prefix(repo.as_ref(), &prefix).unwrap() |
| 159 |
}; |
| 160 |
let shortest_change_prefix_len = |index: &IdPrefixIndex, change_id| { |
| 161 |
index |
| 162 |
.shortest_change_prefix_len(repo.as_ref(), change_id) |
| 163 |
.block_on() |
| 164 |
.unwrap() |
| 165 |
}; |
| 166 |
let resolve_change_prefix = |index: &IdPrefixIndex, prefix: HexPrefix| { |
| 167 |
index |
| 168 |
.resolve_change_prefix(repo.as_ref(), &prefix) |
| 169 |
.block_on() |
| 170 |
.unwrap() |
| 171 |
.filter_map(ResolvedChangeTargets::into_visible) |
| 172 |
}; |
| 173 |
|
| 174 |
// Without a disambiguation revset |
| 175 |
// --------------------------------------------------------------------------------------------- |
| 176 |
let context = IdPrefixContext::default(); |
| 177 |
let index = context.populate(repo.as_ref())?; |
| 178 |
|
| 179 |
assert_eq!(shortest_commit_prefix_len(&index, commits[7].id()), 2); |
| 180 |
assert_eq!(shortest_commit_prefix_len(&index, commits[16].id()), 1); |
| 181 |
assert_eq!(resolve_commit_prefix(&index, prefix("1")), AmbiguousMatch); |
| 182 |
assert_eq!( |
| 183 |
resolve_commit_prefix(&index, prefix("18")), |
| 184 |
SingleMatch(commits[7].id().clone()) |
| 185 |
); |
| 186 |
assert_eq!(resolve_commit_prefix(&index, prefix("10")), NoMatch); |
| 187 |
assert_eq!(resolve_commit_prefix(&index, prefix("180")), NoMatch); |
| 188 |
assert_eq!( |
| 189 |
shortest_change_prefix_len(&index, commits[2].change_id()), |
| 190 |
2 |
| 191 |
); |
| 192 |
assert_eq!( |
| 193 |
shortest_change_prefix_len(&index, commits[6].change_id()), |
| 194 |
1 |
| 195 |
); |
| 196 |
assert_eq!(resolve_change_prefix(&index, prefix("4")), AmbiguousMatch); |
| 197 |
assert_eq!( |
| 198 |
resolve_change_prefix(&index, prefix("43")), |
| 199 |
SingleMatch(vec![commits[2].id().clone()]) |
| 200 |
); |
| 201 |
assert_eq!(resolve_change_prefix(&index, prefix("40")), NoMatch); |
| 202 |
assert_eq!(resolve_change_prefix(&index, prefix("430")), NoMatch); |
| 203 |
|
| 204 |
// Disambiguate within a revset |
| 205 |
// --------------------------------------------------------------------------------------------- |
| 206 |
let expression = |
| 207 |
RevsetExpression::commits(vec![commits[7].id().clone(), commits[2].id().clone()]); |
| 208 |
let context = context.disambiguate_within(expression); |
| 209 |
let index = context.populate(repo.as_ref())?; |
| 210 |
// The prefix is now shorter |
| 211 |
assert_eq!(shortest_commit_prefix_len(&index, commits[7].id()), 1); |
| 212 |
// Shorter prefix within the set can be used |
| 213 |
assert_eq!( |
| 214 |
resolve_commit_prefix(&index, prefix("1")), |
| 215 |
SingleMatch(commits[7].id().clone()) |
| 216 |
); |
| 217 |
// Can still resolve commits outside the set |
| 218 |
assert_eq!( |
| 219 |
resolve_commit_prefix(&index, prefix("19")), |
| 220 |
SingleMatch(commits[10].id().clone()) |
| 221 |
); |
| 222 |
assert_eq!( |
| 223 |
shortest_change_prefix_len(&index, commits[2].change_id()), |
| 224 |
1 |
| 225 |
); |
| 226 |
assert_eq!( |
| 227 |
resolve_change_prefix(&index, prefix("4")), |
| 228 |
SingleMatch(vec![commits[2].id().clone()]) |
| 229 |
); |
| 230 |
|
| 231 |
// Single commit in revset. Length 0 is unambiguous, but we pretend 1 digit is |
| 232 |
// needed. |
| 233 |
// --------------------------------------------------------------------------------------------- |
| 234 |
let expression = RevsetExpression::commit(root_commit_id.clone()); |
| 235 |
let context = context.disambiguate_within(expression); |
| 236 |
let index = context.populate(repo.as_ref())?; |
| 237 |
assert_eq!(shortest_commit_prefix_len(&index, root_commit_id), 1); |
| 238 |
assert_eq!(resolve_commit_prefix(&index, prefix("")), AmbiguousMatch); |
| 239 |
assert_eq!( |
| 240 |
resolve_commit_prefix(&index, prefix("0")), |
| 241 |
SingleMatch(root_commit_id.clone()) |
| 242 |
); |
| 243 |
assert_eq!(shortest_change_prefix_len(&index, root_change_id), 1); |
| 244 |
assert_eq!(resolve_change_prefix(&index, prefix("")), AmbiguousMatch); |
| 245 |
assert_eq!( |
| 246 |
resolve_change_prefix(&index, prefix("0")), |
| 247 |
SingleMatch(vec![root_commit_id.clone()]) |
| 248 |
); |
| 249 |
|
| 250 |
// Disambiguate within revset that fails to evaluate |
| 251 |
// --------------------------------------------------------------------------------------------- |
| 252 |
let expression = RevsetExpression::symbol("nonexistent".to_string()); |
| 253 |
let context = context.disambiguate_within(expression); |
| 254 |
assert!(context.populate(repo.as_ref()).is_err()); |
| 255 |
Ok(()) |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn test_id_prefix_divergent() -> TestResult { |
| 260 |
let test_repo = TestRepo::init_with_backend(TestRepoBackend::Git); |
| 261 |
let repo = &test_repo.repo; |
| 262 |
let root_commit_id = repo.store().root_commit_id(); |
| 263 |
|
| 264 |
let mut tx = repo.start_transaction(); |
| 265 |
let mut create_commit_with_change_id = |
| 266 |
|parent_id: &CommitId, description: &str, change_id: ChangeId| { |
| 267 |
let signature = Signature { |
| 268 |
name: "Some One".to_string(), |
| 269 |
email: "[email protected]".to_string(), |
| 270 |
timestamp: Timestamp { |
| 271 |
timestamp: MillisSinceEpoch(0), |
| 272 |
tz_offset: 0, |
| 273 |
}, |
| 274 |
}; |
| 275 |
tx.repo_mut() |
| 276 |
.new_commit(vec![parent_id.clone()], repo.store().empty_merged_tree()) |
| 277 |
.set_description(description) |
| 278 |
.set_author(signature.clone()) |
| 279 |
.set_committer(signature) |
| 280 |
.set_change_id(change_id) |
| 281 |
.write_unwrap() |
| 282 |
}; |
| 283 |
|
| 284 |
let first_change_id = ChangeId::from_hex("a5333333333333333333333333333333"); |
| 285 |
let second_change_id = ChangeId::from_hex("a5000000000000000000000000000000"); |
| 286 |
|
| 287 |
let first_commit = create_commit_with_change_id(root_commit_id, "first", first_change_id); |
| 288 |
let second_commit = |
| 289 |
create_commit_with_change_id(first_commit.id(), "second", second_change_id.clone()); |
| 290 |
let third_commit_divergent_with_second = |
| 291 |
create_commit_with_change_id(first_commit.id(), "third", second_change_id); |
| 292 |
let commits = [ |
| 293 |
first_commit.clone(), |
| 294 |
second_commit.clone(), |
| 295 |
third_commit_divergent_with_second.clone(), |
| 296 |
]; |
| 297 |
let repo = tx.commit("test").block_on()?; |
| 298 |
|
| 299 |
// Print the commit IDs and change IDs for reference |
| 300 |
let change_prefixes = commits |
| 301 |
.iter() |
| 302 |
.enumerate() |
| 303 |
.map(|(i, commit)| format!("{} {}", &commit.change_id().hex()[..4], i)) |
| 304 |
.join("\n"); |
| 305 |
insta::assert_snapshot!(change_prefixes, @" |
| 306 |
a533 0 |
| 307 |
a500 1 |
| 308 |
a500 2 |
| 309 |
"); |
| 310 |
let commit_prefixes = commits |
| 311 |
.iter() |
| 312 |
.enumerate() |
| 313 |
.map(|(i, commit)| format!("{} {}", &commit.id().hex()[..4], i)) |
| 314 |
.join("\n"); |
| 315 |
insta::assert_snapshot!(commit_prefixes, @" |
| 316 |
e2b9 0 |
| 317 |
f8d1 1 |
| 318 |
c596 2 |
| 319 |
"); |
| 320 |
|
| 321 |
let prefix = |x| HexPrefix::try_from_hex(x).unwrap(); |
| 322 |
let shortest_change_prefix_len = |index: &IdPrefixIndex, change_id| { |
| 323 |
index |
| 324 |
.shortest_change_prefix_len(repo.as_ref(), change_id) |
| 325 |
.block_on() |
| 326 |
.unwrap() |
| 327 |
}; |
| 328 |
let resolve_change_prefix = |index: &IdPrefixIndex, prefix: HexPrefix| { |
| 329 |
index |
| 330 |
.resolve_change_prefix(repo.as_ref(), &prefix) |
| 331 |
.block_on() |
| 332 |
.unwrap() |
| 333 |
.filter_map(ResolvedChangeTargets::into_visible) |
| 334 |
}; |
| 335 |
|
| 336 |
// Without a disambiguation revset |
| 337 |
// -------------------------------- |
| 338 |
let context = IdPrefixContext::default(); |
| 339 |
let index = context.populate(repo.as_ref())?; |
| 340 |
assert_eq!( |
| 341 |
shortest_change_prefix_len(&index, commits[0].change_id()), |
| 342 |
3 |
| 343 |
); |
| 344 |
assert_eq!( |
| 345 |
shortest_change_prefix_len(&index, commits[1].change_id()), |
| 346 |
3 |
| 347 |
); |
| 348 |
assert_eq!( |
| 349 |
shortest_change_prefix_len(&index, commits[2].change_id()), |
| 350 |
3 |
| 351 |
); |
| 352 |
assert_eq!(resolve_change_prefix(&index, prefix("a5")), AmbiguousMatch); |
| 353 |
assert_eq!( |
| 354 |
resolve_change_prefix(&index, prefix("a53")), |
| 355 |
SingleMatch(vec![first_commit.id().clone()]) |
| 356 |
); |
| 357 |
assert_eq!( |
| 358 |
resolve_change_prefix(&index, prefix("a50")), |
| 359 |
SingleMatch(vec![ |
| 360 |
third_commit_divergent_with_second.id().clone(), |
| 361 |
second_commit.id().clone(), |
| 362 |
]) |
| 363 |
); |
| 364 |
|
| 365 |
// Now, disambiguate within the revset containing only the second commit |
| 366 |
// ---------------------------------------------------------------------- |
| 367 |
let expression = RevsetExpression::commits(vec![second_commit.id().clone()]); |
| 368 |
let context = context.disambiguate_within(expression); |
| 369 |
let index = context.populate(repo.as_ref())?; |
| 370 |
// The prefix is now shorter |
| 371 |
assert_eq!( |
| 372 |
shortest_change_prefix_len(&index, second_commit.change_id()), |
| 373 |
1 |
| 374 |
); |
| 375 |
// This tests two issues, both important: |
| 376 |
// - We find both commits with the same change id, even though |
| 377 |
// `third_commit_divergent_with_second` is not in the short prefix set |
| 378 |
// (#2476). |
| 379 |
// - The short prefix set still works: we do *not* find the first commit and the |
| 380 |
// match is not ambiguous, even though the first commit's change id would also |
| 381 |
// match the prefix. |
| 382 |
assert_eq!( |
| 383 |
resolve_change_prefix(&index, prefix("a")), |
| 384 |
SingleMatch(vec![ |
| 385 |
third_commit_divergent_with_second.id().clone(), |
| 386 |
second_commit.id().clone(), |
| 387 |
]) |
| 388 |
); |
| 389 |
|
| 390 |
// We can still resolve commits outside the set |
| 391 |
assert_eq!( |
| 392 |
resolve_change_prefix(&index, prefix("a53")), |
| 393 |
SingleMatch(vec![first_commit.id().clone()]) |
| 394 |
); |
| 395 |
assert_eq!( |
| 396 |
shortest_change_prefix_len(&index, first_commit.change_id()), |
| 397 |
3 |
| 398 |
); |
| 399 |
Ok(()) |
| 400 |
} |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn test_id_prefix_hidden() -> TestResult { |
| 404 |
let test_repo = TestRepo::init_with_backend(TestRepoBackend::Git); |
| 405 |
let repo = &test_repo.repo; |
| 406 |
let root_commit_id = repo.store().root_commit_id(); |
| 407 |
|
| 408 |
let mut tx = repo.start_transaction(); |
| 409 |
let mut commits = vec![]; |
| 410 |
for i in 0..10 { |
| 411 |
let signature = Signature { |
| 412 |
name: "Some One".to_string(), |
| 413 |
email: "[email protected]".to_string(), |
| 414 |
timestamp: Timestamp { |
| 415 |
timestamp: MillisSinceEpoch(i), |
| 416 |
tz_offset: 0, |
| 417 |
}, |
| 418 |
}; |
| 419 |
let commit = tx |
| 420 |
.repo_mut() |
| 421 |
.new_commit( |
| 422 |
vec![root_commit_id.clone()], |
| 423 |
repo.store().empty_merged_tree(), |
| 424 |
) |
| 425 |
.set_author(signature.clone()) |
| 426 |
.set_committer(signature) |
| 427 |
.write_unwrap(); |
| 428 |
commits.push(commit); |
| 429 |
} |
| 430 |
|
| 431 |
// Print the commit IDs and change IDs for reference |
| 432 |
let commit_prefixes = commits |
| 433 |
.iter() |
| 434 |
.enumerate() |
| 435 |
.map(|(i, commit)| format!("{} {}", &commit.id().hex()[..3], i)) |
| 436 |
.sorted() |
| 437 |
.join("\n"); |
| 438 |
insta::assert_snapshot!(commit_prefixes, @" |
| 439 |
3ae 6 |
| 440 |
64c 5 |
| 441 |
84e 2 |
| 442 |
906 8 |
| 443 |
912 7 |
| 444 |
9d1 3 |
| 445 |
a6b 1 |
| 446 |
c47 0 |
| 447 |
d9b 4 |
| 448 |
f5f 9 |
| 449 |
"); |
| 450 |
let change_prefixes = commits |
| 451 |
.iter() |
| 452 |
.enumerate() |
| 453 |
.map(|(i, commit)| format!("{} {}", &commit.change_id().hex()[..3], i)) |
| 454 |
.sorted() |
| 455 |
.join("\n"); |
| 456 |
insta::assert_snapshot!(change_prefixes, @" |
| 457 |
026 9 |
| 458 |
1b5 6 |
| 459 |
26b 3 |
| 460 |
26c 8 |
| 461 |
439 2 |
| 462 |
781 0 |
| 463 |
871 7 |
| 464 |
896 5 |
| 465 |
a2c 1 |
| 466 |
b93 4 |
| 467 |
"); |
| 468 |
|
| 469 |
let hidden_commit = &commits[8]; |
| 470 |
tx.repo_mut().record_abandoned_commit(hidden_commit); |
| 471 |
tx.repo_mut().rebase_descendants().block_on()?; |
| 472 |
let repo = tx.commit("test").block_on()?; |
| 473 |
|
| 474 |
let prefix = |x: &str| HexPrefix::try_from_hex(x).unwrap(); |
| 475 |
let shortest_commit_prefix_len = |index: &IdPrefixIndex, commit_id| { |
| 476 |
index |
| 477 |
.shortest_commit_prefix_len(repo.as_ref(), commit_id) |
| 478 |
.unwrap() |
| 479 |
}; |
| 480 |
let resolve_commit_prefix = |index: &IdPrefixIndex, prefix: HexPrefix| { |
| 481 |
index.resolve_commit_prefix(repo.as_ref(), &prefix).unwrap() |
| 482 |
}; |
| 483 |
let shortest_change_prefix_len = |index: &IdPrefixIndex, change_id| { |
| 484 |
index |
| 485 |
.shortest_change_prefix_len(repo.as_ref(), change_id) |
| 486 |
.block_on() |
| 487 |
.unwrap() |
| 488 |
}; |
| 489 |
let resolve_change_prefix = |index: &IdPrefixIndex, prefix: HexPrefix| { |
| 490 |
index |
| 491 |
.resolve_change_prefix(repo.as_ref(), &prefix) |
| 492 |
.block_on() |
| 493 |
.unwrap() |
| 494 |
.filter_map(ResolvedChangeTargets::into_visible) |
| 495 |
}; |
| 496 |
|
| 497 |
// Without a disambiguation revset |
| 498 |
// -------------------------------- |
| 499 |
let context = IdPrefixContext::default(); |
| 500 |
let index = context.populate(repo.as_ref())?; |
| 501 |
assert_eq!(shortest_commit_prefix_len(&index, hidden_commit.id()), 2); |
| 502 |
assert_eq!( |
| 503 |
shortest_change_prefix_len(&index, hidden_commit.change_id()), |
| 504 |
3 |
| 505 |
); |
| 506 |
assert_eq!( |
| 507 |
resolve_commit_prefix(&index, prefix(&hidden_commit.id().hex()[..1])), |
| 508 |
AmbiguousMatch |
| 509 |
); |
| 510 |
assert_eq!( |
| 511 |
resolve_commit_prefix(&index, prefix(&hidden_commit.id().hex()[..2])), |
| 512 |
SingleMatch(hidden_commit.id().clone()) |
| 513 |
); |
| 514 |
assert_eq!( |
| 515 |
resolve_change_prefix(&index, prefix(&hidden_commit.change_id().hex()[..2])), |
| 516 |
AmbiguousMatch |
| 517 |
); |
| 518 |
assert_eq!( |
| 519 |
resolve_change_prefix(&index, prefix(&hidden_commit.change_id().hex()[..3])), |
| 520 |
NoMatch |
| 521 |
); |
| 522 |
|
| 523 |
// Disambiguate within hidden |
| 524 |
// -------------------------- |
| 525 |
let expression = RevsetExpression::commit(hidden_commit.id().clone()); |
| 526 |
let context = context.disambiguate_within(expression); |
| 527 |
let index = context.populate(repo.as_ref())?; |
| 528 |
assert_eq!(shortest_commit_prefix_len(&index, hidden_commit.id()), 1); |
| 529 |
assert_eq!( |
| 530 |
shortest_change_prefix_len(&index, hidden_commit.change_id()), |
| 531 |
1 |
| 532 |
); |
| 533 |
// Short commit id can be resolved even if it's hidden. |
| 534 |
assert_eq!( |
| 535 |
resolve_commit_prefix(&index, prefix(&hidden_commit.id().hex()[..1])), |
| 536 |
SingleMatch(hidden_commit.id().clone()) |
| 537 |
); |
| 538 |
// OTOH, hidden change id should never be found. The resolution might be |
| 539 |
// ambiguous if hidden commits were excluded from the disambiguation set. |
| 540 |
// In that case, shortest_change_prefix_len() shouldn't be 1. |
| 541 |
assert_eq!( |
| 542 |
resolve_change_prefix(&index, prefix(&hidden_commit.change_id().hex()[..1])), |
| 543 |
NoMatch |
| 544 |
); |
| 545 |
assert_eq!( |
| 546 |
resolve_change_prefix(&index, prefix(&hidden_commit.change_id().hex()[..2])), |
| 547 |
NoMatch |
| 548 |
); |
| 549 |
Ok(()) |
| 550 |
} |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn test_id_prefix_shadowed_by_ref() { |
| 554 |
let settings = stable_settings(); |
| 555 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 556 |
let repo = &test_repo.repo; |
| 557 |
let root_commit_id = repo.store().root_commit_id(); |
| 558 |
|
| 559 |
let mut tx = repo.start_transaction(); |
| 560 |
let commit = tx |
| 561 |
.repo_mut() |
| 562 |
.new_commit( |
| 563 |
vec![root_commit_id.clone()], |
| 564 |
repo.store().empty_merged_tree(), |
| 565 |
) |
| 566 |
.write_unwrap(); |
| 567 |
|
| 568 |
let commit_id_sym = commit.id().to_string(); |
| 569 |
let change_id_sym = commit.change_id().to_string(); |
| 570 |
insta::assert_snapshot!(commit_id_sym, @"b06a01f026da65ac5821"); |
| 571 |
insta::assert_snapshot!(change_id_sym, @"sryyqqkqmuumyrlruupspprvnulvovzm"); |
| 572 |
|
| 573 |
let context = IdPrefixContext::default(); |
| 574 |
let index = context.populate(tx.repo()).unwrap(); |
| 575 |
let shortest_commit_prefix_len = |
| 576 |
|repo: &MutableRepo, commit_id| index.shortest_commit_prefix_len(repo, commit_id).unwrap(); |
| 577 |
let shortest_change_prefix_len = |repo: &MutableRepo, change_id| { |
| 578 |
index |
| 579 |
.shortest_change_prefix_len(repo, change_id) |
| 580 |
.block_on() |
| 581 |
.unwrap() |
| 582 |
}; |
| 583 |
|
| 584 |
assert_eq!(shortest_commit_prefix_len(tx.repo(), commit.id()), 1); |
| 585 |
assert_eq!(shortest_change_prefix_len(tx.repo(), commit.change_id()), 1); |
| 586 |
|
| 587 |
// Longer symbol doesn't count |
| 588 |
let dummy_target = RefTarget::normal(root_commit_id.clone()); |
| 589 |
tx.repo_mut() |
| 590 |
.set_local_tag_target(commit_id_sym[..2].as_ref(), dummy_target.clone()); |
| 591 |
tx.repo_mut() |
| 592 |
.set_local_tag_target(change_id_sym[..2].as_ref(), dummy_target.clone()); |
| 593 |
assert_eq!(shortest_commit_prefix_len(tx.repo(), commit.id()), 1); |
| 594 |
assert_eq!(shortest_change_prefix_len(tx.repo(), commit.change_id()), 1); |
| 595 |
|
| 596 |
// 1-char conflict with bookmark, 2-char with tag |
| 597 |
tx.repo_mut() |
| 598 |
.set_local_bookmark_target(commit_id_sym[..1].as_ref(), dummy_target.clone()); |
| 599 |
tx.repo_mut() |
| 600 |
.set_local_bookmark_target(change_id_sym[..1].as_ref(), dummy_target.clone()); |
| 601 |
assert_eq!(shortest_commit_prefix_len(tx.repo(), commit.id()), 3); |
| 602 |
assert_eq!(shortest_change_prefix_len(tx.repo(), commit.change_id()), 3); |
| 603 |
|
| 604 |
// Many-char conflicts |
| 605 |
for n in 3..commit_id_sym.len() { |
| 606 |
tx.repo_mut() |
| 607 |
.set_local_tag_target(commit_id_sym[..n].as_ref(), dummy_target.clone()); |
| 608 |
} |
| 609 |
for n in 3..change_id_sym.len() { |
| 610 |
tx.repo_mut() |
| 611 |
.set_local_tag_target(change_id_sym[..n].as_ref(), dummy_target.clone()); |
| 612 |
} |
| 613 |
assert_eq!( |
| 614 |
shortest_commit_prefix_len(tx.repo(), commit.id()), |
| 615 |
commit_id_sym.len() |
| 616 |
); |
| 617 |
assert_eq!( |
| 618 |
shortest_change_prefix_len(tx.repo(), commit.change_id()), |
| 619 |
change_id_sym.len() |
| 620 |
); |
| 621 |
|
| 622 |
// Full-char conflicts |
| 623 |
tx.repo_mut() |
| 624 |
.set_local_tag_target(commit_id_sym.as_ref(), dummy_target.clone()); |
| 625 |
tx.repo_mut() |
| 626 |
.set_local_tag_target(change_id_sym.as_ref(), dummy_target.clone()); |
| 627 |
assert_eq!( |
| 628 |
shortest_commit_prefix_len(tx.repo(), commit.id()), |
| 629 |
commit_id_sym.len() |
| 630 |
); |
| 631 |
assert_eq!( |
| 632 |
shortest_change_prefix_len(tx.repo(), commit.change_id()), |
| 633 |
change_id_sym.len() |
| 634 |
); |
| 635 |
} |