Skip to main content

git_policy_cli/
auto_push.rs

1//! What:
2//!  Post-commit auto-push of a landed commit: which push runs, what its output shows,
3//!  and how a coordination failure is reported without failing the commit.
4//! Why:
5//!  Every commit in this repository is backed up (`AGENTS.md` rule APG),
6//!  and the incumbent's `src/auto-push.ts` decides the push argv from the upstream state,
7//!  prints only GitHub `remote:` lines on success and the full output on failure,
8//!  and keeps the commit's exit status when the push fails.
9//!  The landed commit is an input, so the integrator can call this after any landing.
10//!
11//! In TS you'd write (pseudocode):
12//! ```ts
13//! await autoPush({ gitPath, cwd, landedOid });
14//! ```
15
16/// The reconcile of a push that a moved remote branch rejected, and the lines it adds to the output.
17use super::auto_push_reconcile::{
18    RECONCILE_FAILED_PREFIX, RECONCILE_LINE_PREFIX, destination_push, named_destination_push, reconcile_divergent_push,
19    tracked_push,
20};
21/// The single-flight coordinator, its Git command builder and its error type.
22use super::auto_push_single_flight::{
23    PushAttempt, PushGit, SingleFlightError, SingleFlightKind, SingleFlightOutcome,
24    resolve_push_coordination_paths, run_query, run_single_flight_push_repeating,
25};
26/// The deadline-bound push runner and the limits it applies.
27use super::bounded_push::{LOCK_WAIT_VARIABLE, lock_wait_limit, push_deadline};
28/// What a waiting notice says about the process it waits for.
29use super::lock_holder_facts::holder_facts;
30/// The auto-push's bounded `git push` with the Git LFS upload.
31use super::lfs_bounded_push::run_push;
32/// The diagnostic log of the wrapper.
33use super::diagnostic_log::debug;
34/// The lock record whose owner the waiting notice names.
35use super::owner_lock_record::{PublishedOwner, read_owner_lock_record};
36/// Owned OS strings for Git arguments.
37use std::ffi::OsString;
38/// `Write` is the stderr sink the terminal lines go to.
39use std::io::Write;
40
41/// Tag used for diagnostics from this module.
42const TAG: &str = "cli-git";
43
44/// Prefix of the GitHub server-side lines a clean push surfaces.
45const REMOTE_LINE_PREFIX: &str = "remote: ";
46
47/// Note printed when HEAD is detached: pushing `HEAD` needs a branch.
48pub const DETACHED_HEAD_NOTE: &str = "cli-git: HEAD is detached (mid-rebase/cherry-pick or a detached checkout); skipping auto-push.";
49
50/// Note printed after every failed push, owned or joined: the commit stays local.
51pub const PUSH_FAILED_NOTE: &str =
52    "cli-git: commit saved locally, but auto-push to origin failed; run `git push` when ready.";
53
54/// What Git prints when another process holds the lock of a reference the push updates.
55const REFERENCE_LOCK_REFUSAL: &str = "cannot lock ref";
56
57/// How many times the service pushes again when a held reference lock refused the update of a remote-tracking reference.
58pub const PUSH_LOCK_RETRIES: u32 = 3;
59
60/// The pause before the first of those pushes; each later one waits a multiple of it.
61const PUSH_LOCK_PAUSE: std::time::Duration = std::time::Duration::from_millis(250);
62
63/// Push argv for a branch without an upstream: create it on origin and record the upstream.
64const FIRST_PUSH_ARGUMENTS: &[&str] = &["push", "--set-upstream", "origin", "HEAD"];
65
66/// Push argv for a branch with an upstream: follow the configured upstream, wherever it lives.
67const UPSTREAM_PUSH_ARGUMENTS: &[&str] = &["push"];
68
69/// What:
70///  Which branch the auto-push took.
71/// Why:
72///   `Skipped` covers both "nowhere to back up to" and "detached HEAD"; both exit 0 silently or with a note.
73///
74/// In TS you'd write (pseudocode):
75/// ```ts
76/// type AutoPushOutcome = 'skipped' | 'pushed' | 'failed';
77/// ```
78#[derive(Clone, Copy, Debug, Eq, PartialEq)]
79pub enum AutoPushOutcome {
80    /// Nothing was pushed.
81    Skipped,
82    /// A successful push delivered the commit.
83    Pushed,
84    /// The push that would deliver the commit failed or could not run.
85    Failed,
86}
87
88/// What:
89///  The result of one auto-push attempt.
90/// Why:
91///   `shown` is what the terminal received, so tests compare the surfaced text directly.
92///
93/// In TS you'd write (pseudocode):
94/// ```ts
95/// type AutoPushResult = { outcome; exitCode; shown; joined };
96/// ```
97#[derive(Clone, Debug, Eq, PartialEq)]
98pub struct AutoPushResult {
99    /// Which branch the auto-push took.
100    pub outcome: AutoPushOutcome,
101    /// Push exit code; `0` when skipped or pushed, Git's code (or `1`) on failure.
102    pub exit_code: i64,
103    /// Text surfaced to stderr; empty on a skip, a quiet clean push, or a joined success.
104    pub shown: String,
105    /// Whether another invocation's push decided the outcome.
106    pub joined: bool,
107}
108
109/// What:
110///  Select the output a push shows: only `remote:` lines after a clean push, everything after a failure.
111/// Why:
112///   A rejection, a hook refusal or an offline error stays diagnosable only when the full output shows.
113///
114/// In TS you'd write (pseudocode):
115/// ```ts
116/// function filterPushOutput({ output, exitCode }): string;
117/// ```
118pub fn filter_push_output(output: &str, exit_code: i64) -> String {
119    if exit_code != 0 {
120        return output.to_owned();
121    }
122    return output
123        .split('\n')
124        .filter(|line| {
125            return line.starts_with(REMOTE_LINE_PREFIX) || line.starts_with(RECONCILE_LINE_PREFIX);
126        })
127        .collect::<Vec<&str>>()
128        .join("\n");
129}
130
131/// What:
132///  Whether a read-only Git query exits zero.
133/// Why:
134///   A non-zero exit is an answer ("no upstream", "no origin"); a failure to start Git is not an answer.
135///
136/// In TS you'd write (pseudocode):
137/// ```ts
138/// await gitQuerySucceeds({ gitPath, cwd, args }); // false on SubprocessError only
139/// ```
140fn query_succeeds(git: &PushGit, arguments: &[&str]) -> Result<bool, SingleFlightError> {
141    let argv: Vec<OsString> = arguments.iter().map(OsString::from).collect();
142    return Ok(run_query(git, &argv)?.code == Some(0));
143}
144
145/// What:
146///  The branch HEAD names, or nothing when HEAD is detached.
147/// Why:
148///   `symbolic-ref --quiet HEAD` exits non-zero exactly when HEAD is not a branch.
149///
150/// In TS you'd write (pseudocode):
151/// ```ts
152/// readSymbolicHead({ gitPath, cwd }): Promise<string | typeof HEAD_DETACHED>;
153/// ```
154fn read_symbolic_head(git: &PushGit) -> Result<Option<String>, SingleFlightError> {
155    let argv: Vec<OsString> = ["symbolic-ref", "--quiet", "HEAD"]
156        .iter()
157        .map(OsString::from)
158        .collect();
159    let output = run_query(git, &argv)?;
160    if output.code != Some(0) {
161        return Ok(None);
162    }
163    return Ok(Some(
164        super::js_text::trim_javascript(&String::from_utf8_lossy(&output.stdout)).to_owned(),
165    ));
166}
167
168/// What:
169///  Run one real `git push` and capture its interleaved output, with one trailing line break removed,
170///  stopping it when its deadline passes.
171/// Why:
172///   Git's output of stdout and stderr shares one file so the lines interleave in the order they were written,
173///  the trailing line break is removed as the incumbent's captured output is,
174///  and a push that stalls must not hold the branch's push lock forever.
175///
176/// In TS you'd write (pseudocode):
177/// ```ts
178/// const { exitCode, output } = await nanoSpawn(gitPath, pushArgs, { cwd }) // SubprocessError -> { exitCode ?? 1, output }
179/// ```
180fn run_push_command(git: &PushGit, arguments: &[&str]) -> Result<PushAttempt, SingleFlightError> {
181    let argv: Vec<OsString> = arguments.iter().map(OsString::from).collect();
182    return run_push(git, &argv);
183}
184
185/// What:
186///  The stderr line shown when this commit has to wait behind another process's push.
187/// Why:
188///   A silent wait looks like a hang; the line names the process to look at and how long the wait lasts,
189///  and says the commit is already saved.
190///
191/// In TS you'd write (pseudocode):
192/// ```ts
193/// `cli-git: waiting for process ${pid} to finish pushing ${branchRef}; giving up after ${seconds} seconds`
194/// ```
195fn waiting_line(git: &PushGit, branch_ref: &str, waited: Option<std::time::Duration>) -> String {
196    let (pid, facts): (Option<i64>, String) = match holder(git, branch_ref) {
197        Some((pid, lock_directory)) => (
198            Some(pid),
199            holder_facts(pid, lock_directory.as_path(), push_deadline(&git.environment)),
200        ),
201        None => (None, String::new()),
202    };
203    let holder: String = match pid {
204        Some(number) => format!("process {number}"),
205        None => String::from("another cli-git process"),
206    };
207    // A repeat names how long the wait has lasted; the first line has just begun.
208    let progress: String = match waited {
209        Some(elapsed) => format!(" ({} seconds so far)", elapsed.as_secs()),
210        None => String::new(),
211    };
212    return format!(
213        "cli-git: waiting for {holder} to finish pushing {branch_ref}{progress}; giving up after {} seconds \
214         ({LOCK_WAIT_VARIABLE} changes this). The commit is already saved.{facts}",
215        lock_wait_limit(&git.environment).as_secs()
216    );
217}
218
219/// The process that holds the branch's push lock and the lock's directory, when the record can be read.
220///
221/// In TS you'd write (pseudocode):
222/// ```ts
223/// (await readOwnerLockRecord(lockDirectory))?.ownerPid
224/// ```
225fn holder(git: &PushGit, branch_ref: &str) -> Option<(i64, std::path::PathBuf)> {
226    let paths = resolve_push_coordination_paths(git, branch_ref).ok()?;
227    return match read_owner_lock_record(&paths.lock_directory) {
228        Ok(PublishedOwner::Owner(record)) => Some((record.owner_pid, paths.lock_directory)),
229        Ok(PublishedOwner::Busy) | Err(_) => None,
230    };
231}
232
233/// What:
234///  Surface a settled single-flight outcome and convert it into the auto-push result.
235/// Why:
236///   Lines go to `err` exactly as Git printed them, followed by the uncovered note and the failure note,
237///  so the commit's terminal output matches the incumbent's.
238///
239/// In TS you'd write (pseudocode):
240/// ```ts
241/// function reportSingleFlightOutcome({ outcome, landedOid, branchRef }): AutoPushResult;
242/// ```
243fn report_outcome(
244    outcome: &SingleFlightOutcome,
245    landed_oid: &str,
246    branch_ref: &str,
247    err: &mut dyn Write,
248) -> std::io::Result<AutoPushResult> {
249    let joined = matches!(
250        outcome.kind,
251        SingleFlightKind::JoinedPushed | SingleFlightKind::JoinedFailed
252    );
253    let failed = matches!(
254        outcome.kind,
255        SingleFlightKind::Failed | SingleFlightKind::JoinedFailed
256    );
257    let shown = if outcome.kind == SingleFlightKind::JoinedPushed {
258        String::new()
259    } else {
260        filter_push_output(&outcome.output, outcome.exit_code)
261    };
262    if !shown.is_empty() {
263        writeln!(err, "{shown}")?;
264    }
265    if !outcome.covered {
266        writeln!(
267            err,
268            "cli-git: landed commit {landed_oid} is no longer on {branch_ref}; auto-push pushed {} without it.",
269            outcome.tip
270        )?;
271    }
272    // A reconcile failure already says why the commit is unpublished; "run `git push`" would repeat a push that cannot succeed.
273    if failed && !outcome.output.contains(RECONCILE_FAILED_PREFIX) {
274        writeln!(err, "{PUSH_FAILED_NOTE}")?;
275    }
276    return Ok(AutoPushResult {
277        outcome: if failed {
278            AutoPushOutcome::Failed
279        } else {
280            AutoPushOutcome::Pushed
281        },
282        exit_code: outcome.exit_code,
283        shown,
284        joined,
285    });
286}
287
288/// What a push plan decided before anything was pushed.
289enum Planned {
290    /// Nothing to push; the result says why.
291    Skip(AutoPushResult),
292    /// Push the branch with these arguments.
293    Push {
294        /// The branch HEAD names.
295        branch_ref: String,
296        /// Whether the branch has an upstream, which makes a moved remote branch reconcilable.
297        has_upstream: bool,
298        /// The `git push` arguments.
299        arguments: Vec<String>,
300    },
301}
302
303/// What:
304///  Decide whether and how to push: no upstream and no origin skips, a detached HEAD skips with a note, otherwise the
305///  push arguments follow the upstream state.
306/// Why:
307///   The wrapper's own auto-push and the service's push consumer decide identically; only how the push is coordinated
308///  differs.
309///
310/// In TS you'd write (pseudocode):
311/// ```ts
312/// const plan = await planPush({ gitPath, cwd });
313/// ```
314fn plan_push(git: &PushGit, err: &mut dyn Write) -> Result<Planned, SingleFlightError> {
315    let has_upstream = query_succeeds(
316        git,
317        &[
318            "rev-parse",
319            "--abbrev-ref",
320            "--symbolic-full-name",
321            "@{upstream}",
322        ],
323    )?;
324    if !has_upstream && !query_succeeds(git, &["remote", "get-url", "origin"])? {
325        debug(TAG, "no upstream and no origin remote; skipping auto-push");
326        return Ok(Planned::Skip(AutoPushResult {
327            outcome: AutoPushOutcome::Skipped,
328            exit_code: 0,
329            shown: String::new(),
330            joined: false,
331        }));
332    }
333    let Some(branch_ref) = read_symbolic_head(git)? else {
334        // Terminal output error: the note itself is the only diagnostic; a failed write to stderr is not a push failure.
335        let _written = writeln!(err, "{DETACHED_HEAD_NOTE}");
336        return Ok(Planned::Skip(AutoPushResult {
337            outcome: AutoPushOutcome::Skipped,
338            exit_code: 0,
339            shown: DETACHED_HEAD_NOTE.to_owned(),
340            joined: false,
341        }));
342    };
343    let named: Option<Vec<String>> = if has_upstream {
344        named_destination_push(git, &branch_ref)?
345    } else {
346        None
347    };
348    let named_arguments: Vec<String> = named.into_iter().flatten().collect();
349    let arguments: Vec<String> = if !named_arguments.is_empty() {
350        named_arguments
351    } else if has_upstream {
352        UPSTREAM_PUSH_ARGUMENTS.iter().map(|argument| { return (*argument).to_owned(); }).collect()
353    } else {
354        FIRST_PUSH_ARGUMENTS.iter().map(|argument| { return (*argument).to_owned(); }).collect()
355    };
356    return Ok(Planned::Push { branch_ref, has_upstream, arguments });
357}
358
359/// What:
360///  Decide how to push a recorded branch whatever the worktree has checked out: no upstream and no origin skips, otherwise
361///  the push names the branch on both sides and never reads `HEAD`.
362/// Why:
363///   The service pushes after the command returned, so the person may switch branches or detach the head at any instant,
364///  including between a check and the push; the record names the branch that received the commit, and the plain
365///  `git push` would publish the checked-out one instead.
366///
367/// In TS you'd write (pseudocode):
368/// ```ts
369/// const plan = await planNamedPush({ gitPath, cwd }, branchRef);
370/// ```
371fn plan_named_push(git: &PushGit, branch_ref: &str, err: &mut dyn Write) -> Result<Planned, SingleFlightError> {
372    let skipped = |shown: String| -> Planned {
373        return Planned::Skip(AutoPushResult { outcome: AutoPushOutcome::Skipped, exit_code: 0, shown, joined: false });
374    };
375    let upstream_name: String = format!("{branch_ref}@{{upstream}}");
376    let has_upstream = query_succeeds(git, &["rev-parse", "--abbrev-ref", "--symbolic-full-name", upstream_name.as_str()])?;
377    if !has_upstream && !query_succeeds(git, &["remote", "get-url", "origin"])? {
378        debug(TAG, "no upstream and no origin remote; skipping auto-push");
379        return Ok(skipped(String::new()));
380    }
381    let arguments: Vec<String> = if has_upstream {
382        match destination_push(git, branch_ref)? {
383            Some(named) => named,
384            None => match tracked_push(git, branch_ref)? {
385                Some(tracked) => tracked,
386                None => {
387                    let note: String = format!(
388                        "cli-git: the branch {branch_ref} tracks no remote branch that can be named; skipping auto-push. Run `git push` on that branch."
389                    );
390                    // Terminal output error: the note itself is the only diagnostic; a failed write to stderr is not a push failure.
391                    let _written = writeln!(err, "{note}");
392                    return Ok(skipped(note));
393                }
394            },
395        }
396    } else {
397        vec![String::from("push"), String::from("--set-upstream"), String::from("origin"), format!("{branch_ref}:{branch_ref}")]
398    };
399    return Ok(Planned::Push { branch_ref: branch_ref.to_owned(), has_upstream, arguments });
400}
401
402/// What:
403///  Push a landed commit's branch once, with no coordination with other processes.
404/// Why:
405///   The service's push consumer is the only pusher of the repositories it serves and coalesces pending pushes of one
406///  branch into this single call, so it needs no lock; the output and the failure note are those of `auto_push`.
407///
408/// In TS you'd write (pseudocode):
409/// ```ts
410/// const result = await pushOnce({ gitPath, cwd }, landedOid);
411/// ```
412pub fn push_once(git: &PushGit, branch_ref: &str, landed_oid: &str, err: &mut dyn Write) -> Result<AutoPushResult, SingleFlightError> {
413    // The record names the branch, and the worktree may switch away from it at any instant, including between a check of
414    //  `HEAD` and the push itself: the plan never reads `HEAD`, so what is pushed is always the recorded branch.
415    let planned: Planned = plan_named_push(git, branch_ref, err)?;
416    let (pushed_ref, has_upstream, arguments): (String, bool, Vec<String>) = match planned {
417        Planned::Skip(result) => return Ok(result),
418        Planned::Push { branch_ref: planned_ref, has_upstream, arguments } => (planned_ref, has_upstream, arguments),
419    };
420    let argument_views: Vec<&str> = arguments.iter().map(String::as_str).collect();
421    debug(TAG, &format!("pushing {landed_oid} with {argument_views:?} on {pushed_ref}"));
422    let mut attempt: PushAttempt = run_push_command(git, argument_views.as_slice())?;
423    // The instances of two worktrees share the repository's references. While another holds the lock of the remote-tracking
424    //  reference this push updates, Git still delivers the push, prints the refusal to update the reference, and exits 0, which
425    //  leaves the reference stale; the same push again finds nothing to deliver and brings the reference up to date.
426    let mut pause: u32 = 0;
427    while attempt.output.contains(REFERENCE_LOCK_REFUSAL) && pause < PUSH_LOCK_RETRIES {
428        pause += 1;
429        std::thread::sleep(PUSH_LOCK_PAUSE * pause);
430        attempt = run_push_command(git, argument_views.as_slice())?;
431    }
432    if has_upstream && attempt.exit_code != 0 {
433        attempt = reconcile_divergent_push(git, &pushed_ref, attempt)?;
434    }
435    let failed: bool = attempt.exit_code != 0;
436    let shown: String = filter_push_output(&attempt.output, attempt.exit_code);
437    if !shown.is_empty() {
438        writeln!(err, "{shown}").map_err(SingleFlightError::Io)?;
439    }
440    if failed && !attempt.output.contains(RECONCILE_FAILED_PREFIX) {
441        writeln!(err, "{PUSH_FAILED_NOTE}").map_err(SingleFlightError::Io)?;
442    }
443    return Ok(AutoPushResult {
444        outcome: if failed { AutoPushOutcome::Failed } else { AutoPushOutcome::Pushed },
445        exit_code: attempt.exit_code,
446        shown,
447        joined: false,
448    });
449}
450
451/// What:
452///  Back up a landed commit by pushing its branch, single-flight per branch.
453/// Why:
454///   The commit has happened by the time this runs and Git ignores a post-commit hook's status,
455///  so a failed push or a failed coordination is printed and returned as `Failed`, and never fails the commit.
456///   Failures before the push decision (an unreadable repository, a Git that cannot start) are returned as errors.
457///
458/// In TS you'd write (pseudocode):
459/// ```ts
460/// async function autoPush({ gitPath, cwd, landedOid }): Promise<AutoPushResult>;
461/// ```
462pub fn auto_push(
463    git: &PushGit,
464    landed_oid: &str,
465    err: &mut dyn Write,
466) -> Result<AutoPushResult, SingleFlightError> {
467    let (branch_ref, has_upstream, owned_arguments): (String, bool, Vec<String>) = match plan_push(git, err)? {
468        Planned::Skip(result) => return Ok(result),
469        Planned::Push { branch_ref, has_upstream, arguments } => (branch_ref, has_upstream, arguments),
470    };
471    let push_arguments: &[&str] = &owned_arguments.iter().map(String::as_str).collect::<Vec<&str>>();
472    debug(
473        TAG,
474        &format!("auto-pushing {landed_oid} with {push_arguments:?} on {branch_ref}"),
475    );
476    let coordinated = {
477        let mut push = || {
478            let attempt = run_push_command(git, push_arguments)?;
479            // Only a branch with an upstream can be rejected for a moved remote branch that a merge cures.
480            if has_upstream && attempt.exit_code != 0 {
481                return reconcile_divergent_push(git, &branch_ref, attempt);
482            }
483            return Ok(attempt);
484        };
485        let mut on_wait = |waited: Option<std::time::Duration>| {
486            let line = waiting_line(git, &branch_ref, waited);
487            debug(TAG, &line);
488            // Terminal output error: the notice is advisory; a failed write to stderr must not stop the wait.
489            let _written = writeln!(err, "{line}");
490        };
491        run_single_flight_push_repeating(git, &branch_ref, landed_oid, &mut push, &mut on_wait)
492    };
493    return match coordinated {
494        Ok(outcome) => {
495            report_outcome(&outcome, landed_oid, &branch_ref, err).map_err(SingleFlightError::Io)
496        }
497        Err(error) => {
498            debug(TAG, &format!("auto-push coordination failed: {error}"));
499            let _written = writeln!(
500                err,
501                "cli-git: auto-push of {landed_oid} could not run: {error}"
502            );
503            let _noted = writeln!(err, "{PUSH_FAILED_NOTE}");
504            Ok(AutoPushResult {
505                outcome: AutoPushOutcome::Failed,
506                exit_code: 1,
507                shown: error.to_string(),
508                joined: false,
509            })
510        }
511    };
512}
513
514/// Auto-push behavior is controlled in the test module.
515#[cfg(test)]
516#[path = "auto_push_tests.rs"]
517mod tests;
518
519/// Racing auto-push processes on one branch, killed pushers and joins.
520#[cfg(test)]
521#[path = "auto_push_concurrency_tests.rs"]
522mod concurrency_tests;