1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! The one trusty-search index id derived from a tm registry project (#10530).
//!
//! Why: three derivations of an index id coexisted. `derive_index_id` is the
//! root basename (what registration creates), `ProjectIdentity::index_id` is
//! `<owner-repo>-<16 hex>` over origin, root and operator (what tm published in
//! the active set), and operators hand-name. The published id matched no
//! registered index, and both derived forms changed on `mv`, a repo rename or
//! `git remote remove`. See the index-hygiene design brief, section 1.
//! What: [`RegisteredProject`] is the `(name, project_key)` pair tm stores once
//! at register. [`derive_registry_index_id`] turns it into
//! `<slug(name)>-<8 hex of sha256("v2:" + project_key)>`. [`index_id_for_root`]
//! is the single choice every caller routes through. It returns the legacy
//! basename until [`REGISTRY_IDS_ADOPTED`] flips (D7), because trusty-code cannot
//! yet look up a registry key and the two must name one index. Pure; no I/O.
//! Test: `registry_index_id_tests.rs`.
use Path;
/// Scheme tag mixed into the digest preimage so a later scheme is disjoint.
const SCHEME_TAG: &str = "v2:";
/// Hex digits of digest in the id. Matches the existing suffixed ids.
const DIGEST_HEX: usize = 8;
/// Longest label, in characters, before the digest.
const MAX_LABEL_LEN: usize = 40;
/// Label used when the project name slugifies to nothing.
const FALLBACK_LABEL: &str = "project";
/// A project as the tm registry records it: a name and a key stored once.
///
/// Why: `trusty-common` cannot read tm's registry, so tm hands the pair across.
/// What: `project_key` is `owner/repo` or `local:<uuid>`, assigned at register
/// and never recomputed; `name` is the registry key (cosmetic label only).
/// Test: `registry_index_id_tests.rs`.
/// Derive `<slug(name)>-<8 hex of sha256("v2:" + project_key)>`.
///
/// Why: the id must survive `mv`, a repo rename and `git remote remove`, so it
/// takes only values tm stores once. The label is cosmetic and the digest
/// carries uniqueness, so two projects sharing a name still differ.
/// What: slugifies `name` (truncated to 40 chars, `project` when empty) and
/// appends the first 8 hex digits of the SHA-256 of `"v2:" + project_key`.
///
/// # Postconditions
/// Non-empty; one URL path segment matching `[a-z0-9][a-z0-9-]*`; a pure
/// function of its two arguments.
/// Test: `derivation_is_pinned_for_a_known_input`,
/// `same_key_gives_same_id_whatever_the_name_spelling_of_the_path`,
/// `different_keys_give_different_ids`.
/// Whether index NAMING has switched to the registry-derived id (#10530).
///
/// Why: tm and trusty-code share one daemon index per root. trusty-code has no
/// registry access, so if tm alone named indexes by registry id, each tm project
/// would get two indexes (a pile-up D1 itself would cause). Both sides switch in
/// the same change (D7); until then every caller keeps the legacy basename.
/// What: `false`. Flip it only in the change that also gives trusty-code the
/// registry lookup, and update the tests that pin the legacy behaviour.
/// Test: `index_id_for_root_stays_legacy_until_adoption`.
pub const REGISTRY_IDS_ADOPTED: bool = false;
/// The registry-derived id for `root` when `registered` owns it, else the
/// legacy basename. Ignores [`REGISTRY_IDS_ADOPTED`].
///
/// Why: the adopted behaviour must be testable, and callers that must accept
/// either form (the doctor) need the registry id by name.
/// Test: `registry_id_for_root_prefers_the_registered_identity`,
/// `registry_id_for_root_falls_back_to_the_legacy_basename`.
/// The index id every caller names `root`'s index by.
///
/// Why: registration, tm's published active set and the doctor probe must name
/// one index per project, and trusty-code must name the same one. Routing all
/// of them through this function makes that true by construction.
/// What: the legacy [`crate::derive_index_id`] basename while
/// [`REGISTRY_IDS_ADOPTED`] is `false`; [`registry_id_for_root`] once it is
/// `true`. `root` must already be the resolved project root
/// ([`crate::resolve_project_root`]).
/// Test: `index_id_for_root_stays_legacy_until_adoption`.