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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! What:
//! The dispatch plan of one commit transaction:
//! which hooks run, how they run, and under which lock.
//! Why:
//! Inside native `git commit` the hook entries see only the transaction's overrides,
//! so the plan is computed by the wrapper from the real repository and the caller's environment.
//! The incumbent (`src/hook-dispatch/hook-dispatch-plan.ts`) and both dispatchers read the same
//! `plan.json`, so its field order and spelling are part of the on-disk format.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! const plan: HookDispatchPlan = await computeHookDispatchPlan({ gitPath, cwd, globalArgs, commonDir, ... });
//! ```
/// Config reads and the per-transaction Git context.
use super::transaction_git::{GitContext, GitRequest, run_git};
/// Lenient typed readers over the plan's JSON object.
use super::json_record::{ObjectWriter, parse_object, safe_integer_field, string_array, string_field};
/// Unguessable tokens for preparation leases.
use super::random_id::random_uuid;
/// Lease spelling shared with the transaction lease validator.
use super::transaction_lease::format_preparation_lease;
/// `OsString` is owned operating-system text of raw bytes.
use std::ffi::OsString;
/// `Path`/`PathBuf` are borrowed/owned filesystem paths.
use std::path::{Component, Path, PathBuf};
/// Plan schema version written and required on read.
pub const PLAN_SCHEMA_VERSION: i64 = 1;
/// Name of the plan inside a transaction's `hooks` directory.
pub const PLAN_FILENAME: &str = "plan.json";
/// Events the shim dispatches during preparation; `post-commit` runs once after landing instead.
pub const PREPARATION_HOOK_EVENTS: [&str; 3] = ["pre-commit", "prepare-commit-msg", "commit-msg"];
/// Every commit event the transaction silences inside native `git commit`.
pub const SILENCED_HOOK_EVENTS: [&str; 4] =
["pre-commit", "prepare-commit-msg", "commit-msg", "post-commit"];
/// Environment variable that carries Git's config parameters into hooks.
pub const CONFIG_PARAMETERS_VARIABLE: &str = "GIT_CONFIG_PARAMETERS";
/// Environment variable naming the worktree root for hooks.
pub const WORK_TREE_VARIABLE: &str = "GIT_WORK_TREE";
/// Largest plan a dispatcher reads.
pub const PLAN_LIMIT: u64 = 1024 * 1024;
/// What:
/// Why planning or encoding a plan failed.
/// Why:
/// The caller stops the commit with the message;
/// a plan is never partially written.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// throw new TypeError(`git config --get ${key} failed: ${stderr.trim()}`);
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HookPlanError {
/// A `git config --get` read failed with a code other than 1; holds Git's stderr text.
ConfigRead(String),
/// A value the plan records is not UTF-8, so it cannot be written as JSON text.
NotUtf8(&'static str),
/// The random source for a preparation lease token failed.
Random(String),
}
/// What:
/// One line for each planning failure.
/// Why:
/// The caller prints it as the diagnostic.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// error.message
/// ```
impl std::fmt::Display for HookPlanError {
/// Writes one sentence naming what failed.
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ConfigRead(stderr) => {
return write!(formatter, "git config failed: {stderr}");
}
Self::NotUtf8(what) => {
return write!(formatter, "the {what} is not UTF-8 and cannot be recorded");
}
Self::Random(message) => {
return write!(formatter, "no random token for the preparation lease: {message}");
}
}
}
}
/// What:
/// The value the dispatcher restores as `GIT_CONFIG_PARAMETERS`, or its absence.
/// Why:
/// Git reads the variable as text; an absent variable must stay absent in hooks.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type ConfigParameters = { kind: 'absent' } | { kind: 'present'; value: string };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ConfigParameters {
/// No config parameters were inherited or given.
Absent,
/// The combined parameter text.
Present(String),
}
/// What:
/// The plan written to `<transaction>/hooks/plan.json`.
/// Why:
/// Both dispatchers and the lease validator read this one record,
/// so every field keeps the incumbent's name and meaning.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type HookDispatchPlan = { schemaVersion: 1; gitPath: string; hooksPath: string; ... };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HookDispatchPlan {
/// Absolute real Git executable the dispatcher runs `git hook run` with.
pub git_path: String,
/// Repository's own absolute hooks directory.
pub hooks_path: String,
/// Events the user disabled with `hook.<event>.enabled=false`, in the silenced list's order.
pub disabled_events: Vec<String>,
/// Caller's config parameters including its global `-c` options.
pub config_parameters: ConfigParameters,
/// Absolute worktree root exported as `GIT_WORK_TREE`.
pub worktree_root: String,
/// Preparation lease exported to hook descendants.
pub lease: String,
/// Absolute hook lock directory.
pub hook_lock_path: String,
/// Whether hooks skip the hook lock.
pub skip_hook_lock: bool,
}
/// What:
/// The inputs of one plan computation.
/// Why:
/// Named fields keep the many inputs apart; the context carries the caller's global options
/// as its global prefix, so every config read sees them.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// computeHookDispatchPlan({ gitPath, cwd, globalArgs, commonDir, worktreeRoot, lease, ... });
/// ```
#[derive(Clone, Copy, Debug)]
pub struct HookPlanInputs<'a> {
/// Real Git and the caller's global options.
pub context: &'a GitContext,
/// Owning worktree directory where the config reads run.
pub cwd: &'a Path,
/// Caller's global options, whose `-c` values join the config parameters.
pub global_arguments: &'a [OsString],
/// Absolute common Git directory.
pub common_directory: &'a Path,
/// Absolute worktree root.
pub worktree_root: &'a Path,
/// Preparation lease token text, formatted by `new_preparation_lease`.
pub lease: &'a str,
/// `hooks.concurrentCommits`.
pub concurrent_commits: bool,
/// Whether this invocation runs under a valid outer preparation lease.
pub inherited_lease_valid: bool,
/// Caller environment, whose `GIT_CONFIG_PARAMETERS` is inherited.
pub environment: &'a [(OsString, OsString)],
}
/// What:
/// Quote one `key=value` entry for Git's `GIT_CONFIG_PARAMETERS`.
/// Why:
/// Git's `sq_quote_buf` closes the quote around `'` and `!`;
/// the parser accepts that form in every supported version.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// quoteConfigParameter("user.name=O'Brien"); // "'user.name=O'\\''Brien'"
/// ```
pub fn quote_config_parameter(value: &str) -> String {
// `mut` lets the quoted text grow one character at a time.
let mut body: String = String::with_capacity(value.len() + 2);
body.push('\'');
for character in value.chars() {
match character {
'\'' => body.push_str("'\\''"),
'!' => body.push_str("'\\!'"),
other => body.push(other),
}
}
body.push('\'');
return body;
}
/// What:
/// Collect the value of every global `-c` option, in order.
/// Why:
/// The incumbent pairs each `-c` with the token after it, even when that token is itself `-c`.
/// A non-UTF-8 value cannot be recorded, so it is refused.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// globalArgs.flatMap((token, i) => (token === '-c') && globalArgs[i + 1] !== undefined ? [globalArgs[i + 1]] : []);
/// ```
pub fn global_config_overrides(global_arguments: &[OsString]) -> Result<Vec<String>, HookPlanError> {
// `mut` lets the list grow one value at a time.
let mut values: Vec<String> = Vec::new();
for (index, token) in global_arguments.iter().enumerate() {
let Some(next) = global_arguments.get(index + 1) else {
continue;
};
if token != "-c" {
continue;
}
let Some(text) = next.to_str() else {
return Err(HookPlanError::NotUtf8("global -c option"));
};
values.push(String::from(text));
}
return Ok(values);
}
/// What:
/// Read the caller's inherited `GIT_CONFIG_PARAMETERS`, last entry winning.
/// Why:
/// An empty value counts as none, as the incumbent's `?? ''` and the joining rule do.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// environment.GIT_CONFIG_PARAMETERS ?? ''
/// ```
pub fn inherited_config_parameters(
environment: &[(OsString, OsString)],
) -> Result<String, HookPlanError> {
// `Option` starts absent; each matching entry replaces the earlier one.
let mut found: Option<&OsString> = None;
for (name, value) in environment {
if name == CONFIG_PARAMETERS_VARIABLE {
found = Some(value);
}
}
let Some(value) = found else {
return Ok(String::new());
};
let Some(text) = value.to_str() else {
return Err(HookPlanError::NotUtf8("inherited GIT_CONFIG_PARAMETERS"));
};
return Ok(String::from(text));
}
/// What:
/// Join the inherited parameters and the quoted global `-c` values.
/// Why:
/// Git hands hooks the inherited entries first, then the command-line options;
/// no entry means the variable is absent.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// combineConfigParameters({ inherited, overrides });
/// ```
pub fn combine_config_parameters(
inherited: &str,
overrides: &[String],
) -> Result<ConfigParameters, HookPlanError> {
// `mut` lets the entry list grow in order.
let mut entries: Vec<String> = Vec::new();
if !inherited.is_empty() {
entries.push(String::from(inherited));
}
for value in overrides {
entries.push(quote_config_parameter(value.as_str()));
}
if entries.is_empty() {
return Ok(ConfigParameters::Absent);
}
return Ok(ConfigParameters::Present(entries.join(" ")));
}
/// What:
/// Normalize a path lexically, the way Node's `path.join` and `path.resolve` do.
/// Why:
/// Recorded paths must match the incumbent's spelling byte for byte,
/// and `PathBuf::join` keeps `.` and `..`.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// path.resolve(path);
/// ```
pub fn normalized_path(path: &Path) -> PathBuf {
// `mut` lets the result grow and shrink as components arrive.
let mut result: PathBuf = PathBuf::new();
for component in path.components() {
match component {
Component::Prefix(_) | Component::RootDir => {
result.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
// A `..` above the root stays at the root, as Node clamps it.
let at_root: bool = result.parent().is_none();
if !at_root && result.file_name().is_some() {
result.pop();
}
}
Component::Normal(name) => {
result.push(name);
}
}
}
return result;
}
/// What:
/// The hooks directory Git uses when `core.hooksPath` is unset.
/// Why:
/// The incumbent's `join(commonDir, 'hooks')`.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// join(commonDir, 'hooks')
/// ```
pub fn default_hooks_directory(common_directory: &Path) -> PathBuf {
return normalized_path(common_directory.join("hooks").as_path());
}
/// What:
/// The hooks directory a configured `core.hooksPath` names.
/// Why:
/// A relative value resolves against the worktree root,
/// where Git runs hooks; absolute values stay absolute.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// isAbsolute(configured) ? configured : resolve(worktreeRoot, configured);
/// ```
pub fn resolve_hooks_path(worktree_root: &Path, configured: &Path) -> PathBuf {
if configured.is_absolute() {
return normalized_path(configured);
}
return normalized_path(worktree_root.join(configured).as_path());
}
/// What:
/// The hook lock directory under the common Git directory.
/// Why:
/// The incumbent's `join(commonDir, 'cli-git', 'hook.lock')`; the same lock serializes both wrappers.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// join(commonDir, 'cli-git', 'hook.lock')
/// ```
pub fn hook_lock_directory(common_directory: &Path) -> PathBuf {
return normalized_path(common_directory.join("cli-git").join("hook.lock").as_path());
}
/// What:
/// Read one config value through the caller's Git context; exit code 1 means unset.
/// Why:
/// The incumbent's `readConfig`: unset is not an error, any other failure stops planning,
/// and one terminating newline is removed from the value.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function readConfig({ gitPath, cwd, globalArgs, type, key }): Promise<string | typeof CONFIG_UNSET>;
/// ```
fn read_config(
context: &GitContext,
cwd: &Path,
kind: &str,
key: &str,
) -> Result<Option<String>, HookPlanError> {
// The context's global prefix precedes `config`, as the incumbent's `[...globalArgs, 'config']`.
let arguments: Vec<String> = vec![
String::from("config"),
format!("--type={kind}"),
String::from("--get"),
String::from(key),
];
let request: GitRequest = GitRequest::new(cwd, arguments.as_slice());
let output = match run_git(context, &request) {
Ok(finished) => finished,
Err(error) => {
return Err(HookPlanError::ConfigRead(format!(
"git config --get {key} could not start: {error}"
)));
}
};
if output.code == Some(1) {
return Ok(None);
}
if !output.succeeded() {
return Err(HookPlanError::ConfigRead(format!(
"git config --get {key} failed: {}",
String::from_utf8_lossy(output.stderr.as_slice()).trim()
)));
}
let Ok(text) = String::from_utf8(output.stdout) else {
return Err(HookPlanError::ConfigRead(format!(
"git config --get {key} printed bytes that are not UTF-8"
)));
};
return Ok(Some(String::from(text.strip_suffix('\n').unwrap_or(text.as_str()))));
}
/// What:
/// Compute the dispatch plan of one transaction from the real repository.
/// Why:
/// The incumbent's `computeHookDispatchPlan`:
/// the hooks path, the event switches (only the exact value `false` disables an event),
/// the combined config parameters, the lock path and whether the lock is skipped.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function computeHookDispatchPlan(inputs): Promise<HookDispatchPlan>;
/// ```
pub fn compute_hook_dispatch_plan(
inputs: &HookPlanInputs<'_>,
) -> Result<HookDispatchPlan, HookPlanError> {
let configured_hooks: Option<String> = read_config(
inputs.context,
inputs.cwd,
"path",
"core.hooksPath",
)?;
let hooks_path: PathBuf = match configured_hooks {
None => default_hooks_directory(inputs.common_directory),
Some(value) => resolve_hooks_path(inputs.worktree_root, Path::new(value.as_str())),
};
// `mut` lets the disabled list grow in the silenced list's order.
let mut disabled_events: Vec<String> = Vec::new();
for event in SILENCED_HOOK_EVENTS {
let switch: Option<String> = read_config(
inputs.context,
inputs.cwd,
"bool",
format!("hook.{event}.enabled").as_str(),
)?;
if switch.as_deref() == Some("false") {
disabled_events.push(String::from(event));
}
}
let inherited: String = inherited_config_parameters(inputs.environment)?;
let overrides: Vec<String> = global_config_overrides(inputs.global_arguments)?;
let config_parameters: ConfigParameters = combine_config_parameters(
inherited.as_str(),
overrides.as_slice(),
)?;
let plan: HookDispatchPlan = HookDispatchPlan {
git_path: path_text(inputs.context.real_git.as_path(), "real Git path")?,
hooks_path: path_text(hooks_path.as_path(), "hooks path")?,
disabled_events,
config_parameters,
worktree_root: path_text(inputs.worktree_root, "worktree root")?,
lease: String::from(inputs.lease),
hook_lock_path: path_text(
hook_lock_directory(inputs.common_directory).as_path(),
"hook lock path",
)?,
skip_hook_lock: inputs.concurrent_commits || inputs.inherited_lease_valid,
};
return Ok(plan);
}
/// What:
/// A path as UTF-8 text, or the refusal naming what it was.
/// Why:
/// The plan's JSON strings hold text; lossy conversion would name another path.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// // paths are strings in TypeScript
/// ```
fn path_text(path: &Path, what: &'static str) -> Result<String, HookPlanError> {
let Some(text) = path.to_str() else {
return Err(HookPlanError::NotUtf8(what));
};
return Ok(String::from(text));
}
/// What:
/// Spell a plan as `JSON.stringify` writes it, without the trailing newline.
/// Why:
/// The incumbent writes `JSON.stringify(plan)` followed by one newline,
/// and both dispatchers and the lease validator read the same bytes.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// JSON.stringify(plan)
/// ```
pub fn encode_hook_dispatch_plan(plan: &HookDispatchPlan) -> String {
// `mut` allows appending one field at a time.
let mut writer: ObjectWriter = ObjectWriter::new();
writer.integer("schemaVersion", PLAN_SCHEMA_VERSION);
writer.string("gitPath", plan.git_path.as_str());
writer.string("hooksPath", plan.hooks_path.as_str());
writer.raw(
"disabledEvents",
string_array(plan.disabled_events.as_slice()).as_str(),
);
writer.raw(
"configParameters",
encode_config_parameters(&plan.config_parameters).as_str(),
);
writer.string("worktreeRoot", plan.worktree_root.as_str());
writer.string("lease", plan.lease.as_str());
writer.string("hookLockPath", plan.hook_lock_path.as_str());
writer.boolean("skipHookLock", plan.skip_hook_lock);
return writer.finish();
}
/// What:
/// Spell the config parameters as the incumbent's tagged object.
/// Why:
/// The dispatcher distinguishes an absent variable from an empty one.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// configParameters.kind === 'absent' ? { kind: 'absent' } : { kind: 'present', value }
/// ```
fn encode_config_parameters(parameters: &ConfigParameters) -> String {
// `match` picks the spelling by variant.
match parameters {
ConfigParameters::Absent => {
let mut writer: ObjectWriter = ObjectWriter::new();
writer.string("kind", "absent");
return writer.finish();
}
ConfigParameters::Present(value) => {
let mut writer: ObjectWriter = ObjectWriter::new();
writer.string("kind", "present").string("value", value.as_str());
return writer.finish();
}
}
}
/// What:
/// Read a plan from its JSON text, refusing any other shape.
/// Why:
/// The dispatcher and the lease validator must agree on what a plan is;
/// an unknown event or schema makes the plan unusable.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const plan = JSON.parse(text) as HookDispatchPlan; // checked fields
/// ```
pub fn parse_hook_dispatch_plan(text: &str) -> Option<HookDispatchPlan> {
let object = parse_object(text)?;
if safe_integer_field(&object, "schemaVersion") != Some(PLAN_SCHEMA_VERSION) {
return None;
}
let events_value = object.get("disabledEvents")?.as_array()?;
// `mut` lets the event list grow in file order.
let mut disabled_events: Vec<String> = Vec::new();
for item in events_value {
let name: &str = item.as_str()?;
if !SILENCED_HOOK_EVENTS.contains(&name) {
return None;
}
disabled_events.push(String::from(name));
}
let parameters = object.get("configParameters")?.as_object()?;
let config_parameters: ConfigParameters = match string_field(parameters, "kind")? {
"absent" => ConfigParameters::Absent,
"present" => ConfigParameters::Present(String::from(string_field(parameters, "value")?)),
_ => return None,
};
return Some(HookDispatchPlan {
git_path: String::from(string_field(&object, "gitPath")?),
hooks_path: String::from(string_field(&object, "hooksPath")?),
disabled_events,
config_parameters,
worktree_root: String::from(string_field(&object, "worktreeRoot")?),
lease: String::from(string_field(&object, "lease")?),
hook_lock_path: String::from(string_field(&object, "hookLockPath")?),
skip_hook_lock: object.get("skipHookLock")?.as_bool()?,
});
}
/// What:
/// A fresh preparation lease for one transaction directory.
/// Why:
/// The incumbent's `formatPreparationLease({ directory, token })` with an unguessable token.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// formatPreparationLease({ directory, token: randomUUID() });
/// ```
pub fn new_preparation_lease(transaction_directory: &Path) -> Result<String, HookPlanError> {
let directory: String = path_text(transaction_directory, "transaction directory")?;
let token: String = random_uuid().map_err(|error| { return HookPlanError::Random(format!("{error:?}")); })?;
return Ok(format_preparation_lease(directory.as_str(), token.as_str()));
}
#[cfg(test)]
#[path = "hook_dispatch_plan_tests.rs"]
mod tests;