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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// githttp-fs
//
// Git-based Content Management System
// Copyright: 2026, Valerian Saliou <valerian@valeriansaliou.name>
// License: Mozilla Public License v2.0 (MPL v2.0)
//! The per-directory file-order index: its reserved path, its stored format,
//! and the structural validation of a caller-supplied order.
//!
//! Git has no ordering of its own — tree entries are name-sorted by
//! definition and carry no metadata slot — so a presentation order has to be
//! stored as data. It is stored **per directory**: one `.order.json` holding
//! the leaf names of that directory's entries, in the order the caller wants
//! them presented. Ordering is a sibling-level concern, and scoping the
//! storage the same way is what keeps two costs bounded:
//!
//! - **A reorder touches one small file**, so its commit and its hook payload
//! are proportional to one directory rather than to the repository.
//! - **A folder move needs no index rewriting at all**: entries are leaf
//! names, so every index inside a relocated subtree is still correct once it
//! travels with the subtree.
//!
//! Two rules make a stored index robust, and both live here in spirit rather
//! than in code:
//!
//! - **Sparse.** An index need not list every sibling. Listed entries come
//! first, in index order; unlisted ones follow in the ordinary listing order
//! (directories first, then alphabetical).
//! - **Stale-tolerant on read.** An entry naming something that is no longer
//! there is silently ignored. Writes are validated strictly against HEAD, so
//! staleness should not arise — but a revert or a rollback can restore an
//! older index from history, and that must never turn a read into an error.
//!
//! The index is not addressable through the `/files` routes. It is a separate
//! resource, owned by `routes/order.rs`, and letting a client `PUT` it as an
//! ordinary file would bypass exactly the validation this module exists to
//! perform. `git.rs` enforces that invisibility — see [`is_order_file`].
use ;
use crateAppError;
/// Reserved leaf name of the order index. Dot-prefixed by Unix convention for
/// metadata; the prefix is a courtesy to humans browsing a repository rather
/// than a mechanism, since the index is filtered out of every `/files` route
/// regardless of `include_hidden_files`.
pub const ORDER_FILE_NAME: &str = ".order.json";
/// The position reported for an entry the stored order does not name (or whose
/// directory has no order at all). Positions are zero-based, so `-1` cannot
/// collide with a real one, and a sentinel rather than a `null` keeps the field
/// one number always — a client comparing or sorting on it needs no branch for
/// the unordered case, and `implicit_order_default_index` on the listing route
/// already lets a caller say where unordered entries belong.
pub const UNLISTED_POSITION: i64 = -1;
/// The stored document, deserialising side. An object rather than a bare
/// array so per-entry metadata (a title, a hidden flag, a group) can be added
/// later without a format version bump.
/// The stored document, serialising side — borrows the order rather than
/// cloning it on every write.
/// Repo-root-relative path of the index that orders `directory`
/// (`""` being the repository root).
/// Whether `path` names an order index. Compared on the leaf name alone, so
/// it holds at any depth — which is what makes the invisibility rule in
/// `git.rs` a single check per tree entry.
/// The directory an order index orders, or `None` when `path` is not an
/// index. The repository root's index yields `""`.
///
/// This is the funnel the hook layer classifies on: an event's kind is
/// derived from the *path* a commit touched, never from the route that
/// produced it, so a revert, a rollback and a recursive folder move all
/// classify exactly like an explicit order write.
/// Splits a repo-root-relative path into its parent directory and its leaf
/// name. A path with no slash sits at the repository root, so its parent is
/// the empty string.
/// The entry an index line refers to, with the directory-marking trailing
/// slash (if any) stripped. Two spellings of one name are the same entry, so
/// every comparison — duplicate detection, rank lookup, upkeep matching —
/// goes through this.
/// Whether an entry is hidden by the Unix dot convention — the same test the
/// listing's `include_hidden_files` applies, run on the index entry's own
/// spelling so the directory-marking trailing slash is irrelevant.
///
/// Hidden entries are kept out of an index by default: an index is a
/// *presentation* order, and a dot-file is by convention not presented, so
/// pinning one is noise a caller has to have asked for. Every way into an index
/// enforces that on its own terms — `PUT /order` rejects a hidden entry, and a
/// reorder leaves hidden siblings out of the index it materialises — each behind
/// its own opt-in flag.
/// Canonical index spelling of a directory entry: the trailing slash marks it
/// as a folder, so a stored index is readable on its own without resolving
/// every name against a tree.
/// How a directory is named in messages and errors — the repository root has
/// no path of its own, so it shows as `/`.
/// Serialises an order list into the stored document. Pretty-printed with a
/// trailing newline so the file reads as a sensible git diff when a human
/// inspects the repository.
/// Parses a stored document, or `None` when it cannot be read as one.
///
/// A malformed index is treated as no index at all rather than as an error:
/// only this server's order route writes the file, so the only way to get a
/// broken one is a hand-edited commit — and that must not be able to turn
/// every listing of that directory into a `500`.
/// Structural validation of a caller-supplied order, run before the
/// repository is opened. This covers everything that can be judged from the
/// values alone; whether each entry actually *exists* is checked in `git.rs`,
/// against HEAD's tree, under the tenant write lock.
///
/// An empty order is rejected rather than accepted as "no ordering": it would
/// mean exactly what deleting the index means, and one operation with two
/// spellings is a worse API than two operations with one each.
///
/// `allow_hidden_files` is the caller's opt-in to naming dot-prefixed entries.
/// Off (the default) a hidden entry is a `400` rather than being dropped
/// silently: the caller sent a name and a position for it, and quietly storing
/// a different order than the one they wrote would be worse than telling them
/// the rule. The index file itself stays rejected either way — the flag opens
/// hidden *entries*, not the one path that is not an entry at all.