monochromatic-git-policy-cli 0.1.0

Repository-owned Git policy wrapper: forwards Git commands and checks commits against configured policies.
Documentation
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
//! What:
//!  Single-flight auto-push per branch: one owner lock serializes the pushes of a branch,
//!  and the `last-pushed` record names the most recent attempt.
//! Why:
//!  A commit that a recorded successful push already covers finishes without pushing,
//!  and a commit that waits behind a live pusher joins the push it waited for,
//!  so concurrent landings on one branch cost fewer pushes than commits.
//!  The incumbent's `src/auto-push-single-flight.ts` decides these joins,
//!  and the lock and record paths must match its paths byte for byte.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! await runSingleFlightPush({ gitPath, cwd, branchRef, landedOid, push, onWait });
//! ```

/// The branch key, the record and the owner lock this module coordinates through.
use super::auto_push_branch_key::encode_branch_key;
use super::auto_push_record::{
    LAST_PUSHED_SUFFIX, LastPushedOutcome, LastPushedRead, LastPushedRecord,
    read_last_pushed_record, write_last_pushed_record,
};
use super::diagnostic_log::debug;
use super::js_text::trim_javascript;
use super::owner_lock::{
    DEFAULT_POLL_DELAY, OwnerLock, OwnerLockError, WaitNotice, acquire_owner_lock,
};
/// Owned OS strings for Git arguments and the child environment.
use std::ffi::OsString;
/// `Path` and `PathBuf` are borrowed and owned filesystem paths.
use std::path::{Path, PathBuf};
/// `Command` starts real Git; `Stdio` connects its streams.
use std::process::{Command, Stdio};

/// Tag used for diagnostics from this module.
const TAG: &str = "cli-git";

/// Exit code `git merge-base --is-ancestor` uses for "not an ancestor".
const NOT_ANCESTOR_EXIT_CODE: i32 = 1;

/// Mode of the coordination directory, shared with the incumbent.
const PRIVATE_DIRECTORY_MODE: u32 = 0o700;

/// What:
///  Why a single-flight push could not finish.
/// Why:
///   Every variant is a coordination failure, which the caller reports and keeps the commit for.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// SubprocessError | TypeError | OwnerLockError | NodeJS.ErrnoException
/// ```
#[derive(Debug)]
pub enum SingleFlightError {
    /// Git could not be started, or failed in a way the incumbent does not model as an exit status.
    Start(std::io::Error),
    /// A Git query exited with a status the caller did not expect.
    Git {
        /// Arguments of the query, for the diagnostic.
        arguments: String,
        /// Exit code, or nothing when a signal ended the process.
        code: Option<i32>,
        /// Complete standard error text of the query.
        stderr: String,
    },
    /// A coordination file could not be read or published.
    Io(std::io::Error),
    /// The owner lock could not be acquired.
    Lock(OwnerLockError),
}

impl std::fmt::Display for SingleFlightError {
    /// Writes the diagnostic text of the failure.
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        return match self {
            Self::Start(error) => write!(formatter, "git could not be started: {error}"),
            Self::Git {
                arguments,
                code,
                stderr,
            } => write!(
                formatter,
                "git {arguments} exited with {code:?}: {}",
                stderr.trim_end()
            ),
            Self::Io(error) => write!(formatter, "{error}"),
            Self::Lock(error) => write!(formatter, "{error}"),
        };
    }
}

impl std::error::Error for SingleFlightError {}

/// What:
///  One push this invocation ran itself.
/// Why:
///   `output` is Git's interleaved output with one trailing line break removed, as the incumbent's
///  `nano-spawn` `output` is; a successful push's output is never recorded.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type PushAttempt = { exitCode: number; output: string };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PushAttempt {
    /// Push exit code, `0` on success.
    pub exit_code: i64,
    /// Complete interleaved push output.
    pub output: String,
}

/// What:
///  How a landed commit's auto-push finished.
/// Why:
///   The four kinds keep "this invocation pushed" apart from "another invocation's push decided",
///  because only the owner of a failed push prints its full output.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type SingleFlightKind = 'pushed' | 'failed' | 'joined-pushed' | 'joined-failed';
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SingleFlightKind {
    /// This invocation's push succeeded.
    Pushed,
    /// This invocation's push failed.
    Failed,
    /// Another invocation's successful push covers this commit.
    JoinedPushed,
    /// Another invocation's failed push covers this commit.
    JoinedFailed,
}

/// What:
///  How a landed commit's auto-push finished, with the deciding push's facts.
/// Why:
///   The caller prints the output and the uncovered note from these fields alone.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type SingleFlightOutcome = { kind; tip; exitCode; output; covered };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SingleFlightOutcome {
    /// Which of the four outcomes this is.
    pub kind: SingleFlightKind,
    /// Branch tip the deciding push resolved.
    pub tip: String,
    /// Deciding push exit code.
    pub exit_code: i64,
    /// Complete output of the deciding push; empty for a joined success.
    pub output: String,
    /// Whether the deciding push's tip contains the landed commit.
    pub covered: bool,
}

/// What:
///  Paths of one branch's coordination files.
/// Why:
///   The lock and the record live beside each other under the common Git directory.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type PushCoordinationPaths = { lockDirectory: string; recordPath: string };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PushCoordinationPaths {
    /// Owner-lock directory.
    pub lock_directory: PathBuf,
    /// `last-pushed` record file.
    pub record_path: PathBuf,
}

/// What:
///  The real Git a coordinator runs, where, and with which environment.
/// Why:
///   The incumbent's coordination inherits the wrapper's environment, so the caller passes it in
///  and nothing is read from the process environment here.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type PushGit = { gitPath: string; cwd: string; environment: NodeJS.ProcessEnv };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PushGit {
    /// Absolute real Git executable.
    pub real_git: PathBuf,
    /// Directory the commit landed in.
    pub cwd: PathBuf,
    /// Environment every Git child receives.
    pub environment: Vec<(OsString, OsString)>,
}

/// What:
///  Build a real Git command with the given arguments, directory and environment.
/// Why:
///   Every coordination query and push starts Git this way, so none re-enters the wrapper.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// nanoSpawn(gitPath, args, { cwd })
/// ```
pub fn git_command(git: &PushGit, arguments: &[OsString]) -> Command {
    let mut command = Command::new(&git.real_git);
    command
        .current_dir(&git.cwd)
        .env_clear()
        .envs(git.environment.iter().map(|pair| {
            return (&pair.0, &pair.1);
        }))
        .args(arguments);
    return command;
}

/// The arguments as one display string, for diagnostics.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// args.join(' ')
/// ```
fn describe_arguments(arguments: &[OsString]) -> String {
    return arguments
        .iter()
        .map(|argument| {
            return argument.to_string_lossy().into_owned();
        })
        .collect::<Vec<String>>()
        .join(" ");
}

/// What:
///  Run a read-only Git query and return its trimmed standard output.
///       `Result<A, B>` is either the output or a failure.
/// Why:
///   Any failed query is a coordination failure; it is never read as an answer.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// (await nanoSpawn(gitPath, args, { cwd })).stdout.trim();
/// ```
pub fn git_output(git: &PushGit, arguments: &[OsString]) -> Result<String, SingleFlightError> {
    let output = git_command(git, arguments)
        .stdin(Stdio::null())
        .output()
        .map_err(SingleFlightError::Start)?;
    if !output.status.success() {
        return Err(SingleFlightError::Git {
            arguments: describe_arguments(arguments),
            code: output.status.code(),
            stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
        });
    }
    let text = String::from_utf8_lossy(&output.stdout);
    return Ok(trim_javascript(&text).to_owned());
}

/// Argument list from string literals.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// ['rev-parse', '--verify', ...]
/// ```
fn argv(parts: &[&str]) -> Vec<OsString> {
    return parts.iter().map(OsString::from).collect();
}

/// What:
///  Resolve one branch's coordination file paths under the common Git directory and create that directory.
/// Why:
///   Linked worktrees share one common directory, so every worktree of a repository coordinates on one lock.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function resolvePushCoordinationPaths({ gitPath, cwd, branchRef }): Promise<PushCoordinationPaths>;
/// ```
pub fn resolve_push_coordination_paths(
    git: &PushGit,
    branch_ref: &str,
) -> Result<PushCoordinationPaths, SingleFlightError> {
    let reported = git_output(
        git,
        &argv(&["rev-parse", "--path-format=absolute", "--git-common-dir"]),
    )?;
    let common = if Path::new(&reported).is_absolute() {
        PathBuf::from(&reported)
    } else {
        git.cwd.join(&reported)
    };
    let directory = common.join("cli-git").join("push");
    create_directory(&directory).map_err(SingleFlightError::Io)?;
    let key = encode_branch_key(branch_ref);
    return Ok(PushCoordinationPaths {
        lock_directory: directory.join(format!("{key}.lock")),
        record_path: directory.join(format!("{key}{LAST_PUSHED_SUFFIX}")),
    });
}

/// What:
///  Create a directory tree with mode `0700`, as the incumbent's `mkdir(..., { recursive, mode })`.
/// Why:
///   Coordination state is private to the account.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await mkdir(directory, { recursive: true, mode: 0o700 });
/// ```
fn create_directory(directory: &Path) -> std::io::Result<()> {
    use std::os::unix::fs::DirBuilderExt;
    return std::fs::DirBuilder::new()
        .recursive(true)
        .mode(PRIVATE_DIRECTORY_MODE)
        .create(directory);
}

/// What:
///  Whether a commit is the given tip or one of its ancestors.
///       `Result<A, B>` is "answer or failure".
/// Why:
///   Only Git's "not an ancestor" status answers no; every other failure stops the caller.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function tipContains({ gitPath, cwd, oid, tip }): Promise<boolean>; // throws
/// ```
pub fn tip_contains(git: &PushGit, oid: &str, tip: &str) -> Result<bool, SingleFlightError> {
    let output = git_command(git, &argv(&["merge-base", "--is-ancestor", oid, tip]))
        .stdin(Stdio::null())
        .output()
        .map_err(SingleFlightError::Start)?;
    return match output.status.code() {
        Some(0) => Ok(true),
        Some(NOT_ANCESTOR_EXIT_CODE) => Ok(false),
        code => Err(SingleFlightError::Git {
            arguments: format!("merge-base --is-ancestor {oid} {tip}"),
            code,
            stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
        }),
    };
}

/// What:
///  The joined outcome of a record another invocation wrote.
/// Why:
///   A joined commit is covered by the record's tip by construction; the output is the record's own.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function joinedOutcome(record: LastPushedRecord): SingleFlightOutcome;
/// ```
fn joined_outcome(record: LastPushedRecord) -> SingleFlightOutcome {
    let kind = match record.outcome {
        LastPushedOutcome::Pushed => SingleFlightKind::JoinedPushed,
        LastPushedOutcome::Failed => SingleFlightKind::JoinedFailed,
    };
    return SingleFlightOutcome {
        kind,
        tip: record.tip,
        exit_code: record.exit_code,
        output: record.output,
        covered: true,
    };
}

/// What:
///  Adapts a closure to the owner lock's wait notice.
/// Why:
///   The lock module stays silent; this coordinator reports its own wait through the closure.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// onWait: () => void
/// ```
struct WaitObserver<'observer> {
    /// Called once when the first acquisition attempt finds the lock held.
    on_wait: &'observer mut dyn FnMut(),
}

impl WaitNotice for WaitObserver<'_> {
    /// Forwards the wait to the observer.
    fn waiting(&mut self) {
        (self.on_wait)();
    }
}

/// What:
///  Finish auto-push of one landed commit: join a push that covers it, or push the branch tip itself.
/// Why:
///   Three joins are possible, and each keeps the commit from pushing twice:
///  an early join on a recorded covering success, a join after waiting for the lock,
///  and the rule that a failed record joins only when a push finished after the first read.
///   The record is written before the lock is released, so the next waiter reads this attempt.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function runSingleFlightPush({ gitPath, cwd, branchRef, landedOid, push, onWait }): Promise<SingleFlightOutcome>;
/// ```
pub fn run_single_flight_push(
    git: &PushGit,
    branch_ref: &str,
    landed_oid: &str,
    push: &mut dyn FnMut() -> Result<PushAttempt, SingleFlightError>,
    on_wait: &mut dyn FnMut(),
) -> Result<SingleFlightOutcome, SingleFlightError> {
    let paths = resolve_push_coordination_paths(git, branch_ref)?;
    let before = read_record(&paths.record_path)?;
    let early = match &before {
        LastPushedRead::Record(record)
            if record.outcome == LastPushedOutcome::Pushed
                && tip_contains(git, landed_oid, &record.tip)? =>
        {
            Some(record.clone())
        }
        LastPushedRead::Record(_) | LastPushedRead::Absent => None,
    };
    if let Some(record) = early {
        debug(
            TAG,
            &format!(
                "{landed_oid} already delivered by the push of {}",
                record.tip
            ),
        );
        return Ok(joined_outcome(record));
    }
    let mut observer = WaitObserver { on_wait };
    let lock: OwnerLock =
        acquire_owner_lock(&paths.lock_directory, DEFAULT_POLL_DELAY, &mut observer)
            .map_err(SingleFlightError::Lock)?;
    let after = read_record(&paths.record_path)?;
    let late = match &after {
        LastPushedRead::Record(record)
            if (record.outcome == LastPushedOutcome::Pushed
                || finished_while_waiting(&before, record))
                && tip_contains(git, landed_oid, &record.tip)? =>
        {
            Some(record.clone())
        }
        LastPushedRead::Record(_) | LastPushedRead::Absent => None,
    };
    if let Some(record) = late {
        debug(
            TAG,
            &format!(
                "{landed_oid} joined the {} push of {}",
                record.outcome.as_str(),
                record.tip
            ),
        );
        return Ok(joined_outcome(record));
    }
    let tip = git_output(
        git,
        &argv(&["rev-parse", "--verify", &format!("{branch_ref}^{{commit}}")]),
    )?;
    debug(
        TAG,
        &format!("pushing {branch_ref} at {tip} for {landed_oid}"),
    );
    let attempt = push()?;
    let pushed = attempt.exit_code == 0;
    let record = LastPushedRecord {
        tip: tip.clone(),
        outcome: if pushed {
            LastPushedOutcome::Pushed
        } else {
            LastPushedOutcome::Failed
        },
        owner_token: lock.token().to_owned(),
        owner_pid: i64::from(std::process::id()),
        exit_code: attempt.exit_code,
        output: if pushed {
            String::new()
        } else {
            attempt.output.clone()
        },
    };
    write_last_pushed_record(&paths.record_path, &record).map_err(SingleFlightError::Io)?;
    let covered = tip_contains(git, landed_oid, &tip)?;
    drop(lock);
    return Ok(SingleFlightOutcome {
        kind: if pushed {
            SingleFlightKind::Pushed
        } else {
            SingleFlightKind::Failed
        },
        tip,
        exit_code: attempt.exit_code,
        output: attempt.output,
        covered,
    });
}

/// Whether a push finished after the commit first read the record: the record's owner changed,
/// or the first read found nothing.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// (before === LAST_PUSHED_ABSENT) || (before.ownerToken !== after.ownerToken)
/// ```
fn finished_while_waiting(before: &LastPushedRead, after: &LastPushedRecord) -> bool {
    return match before {
        LastPushedRead::Absent => true,
        LastPushedRead::Record(earlier) => earlier.owner_token != after.owner_token,
    };
}

/// Read a record; an I/O failure other than absence stops the coordinator.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await readLastPushedRecord(recordPath)
/// ```
fn read_record(record_path: &Path) -> Result<LastPushedRead, SingleFlightError> {
    return read_last_pushed_record(record_path).map_err(SingleFlightError::Io);
}

/// Coordination behavior is controlled in the test module.
#[cfg(test)]
#[path = "auto_push_single_flight_tests.rs"]
mod tests;