git_policy_cli/action.rs
1//! What:
2//! The decision one wrapper invocation ends in.
3//! Why:
4//! Forwarding replaces the process and cannot run inside a unit test;
5//! returning
6//! the decision as data lets every path be checked directly.
7//!
8//! In TS you'd write (pseudocode):
9//! ```ts
10//! // type Action = { kind: 'forward'; ... } | { kind: 'exit'; code: number; stdout: string; stderr: string };
11//! ```
12
13/// What:
14/// `OsString` is owned operating-system text of raw OS bytes (sibling `String`
15/// must be UTF-8).
16/// Why:
17/// Environment additions are passed to Git without decoding.
18///
19/// In TS you'd write (pseudocode):
20/// ```ts
21/// // [string, string][] environment pairs, but byte-preserving.
22/// ```
23use std::ffi::OsString;
24/// `PathBuf` is an owned filesystem path of raw OS bytes.
25use super::index_writer_forward::CoordinatedForward;
26use super::worktree_copy_forward::CopyFacts;
27use std::path::PathBuf;
28
29/// What:
30/// Exit code for a usage,
31/// configuration or wrapper failure that kept Git from running.
32/// `i32` is a signed 32-bit integer,
33/// the type of process exit codes.
34/// Why:
35/// The wrapper's contract reserves 2 for these,
36/// distinct from 1 for policy findings.
37///
38/// In TS you'd write (pseudocode):
39/// ```ts
40/// const ENGINE_FAILURE_EXIT_CODE = 2;
41/// ```
42pub const ENGINE_FAILURE_EXIT_CODE: i32 = 2;
43
44/// What:
45/// What a plain push that may be integrated needs to run and, when it fails on a divergence, to merge.
46/// Why:
47/// The run keeps the caller's streams like the settled one does, so the integration step starts from Git's status
48/// and the same arguments, environment and (inside a linked worktree) settlement as an unstacked push would have had.
49///
50/// In TS you'd write (pseudocode):
51/// ```ts
52/// type PushForward = { realGit: string; args: string[]; overlay: [string, string][]; copy?: CopyFacts; subcommandIndex: number; environment: [string, string][] };
53/// ```
54#[derive(Clone, Debug, Eq, PartialEq)]
55pub struct PushForward {
56 /// The selected real Git executable.
57 pub real_git: PathBuf,
58 /// The arguments Git receives, global options included.
59 pub arguments: Vec<OsString>,
60 /// Variables added to the inherited environment.
61 pub overlay: Vec<(OsString, OsString)>,
62 /// The linked-worktree settlement the run keeps when one applies.
63 pub copy: Option<Box<CopyFacts>>,
64 /// Index of `push` in `arguments`.
65 pub subcommand_index: usize,
66 /// The invocation's environment, which the integration step starts Git with.
67 pub environment: Vec<(OsString, OsString)>,
68}
69
70/// What:
71/// What the executable must do for one invocation.
72/// `Vec<OsString>` is an owned argument list;
73/// `Vec<(OsString, OsString)>` is an
74/// owned list of name/value pairs;
75/// `String` is owned UTF-8 text.
76/// Why:
77/// The executable performs exactly one of these:
78/// become Git,
79/// or print and exit.
80///
81/// In TS you'd write (pseudocode):
82/// ```ts
83/// type Action = { kind: 'forward'; realGit: string; args: string[]; overlay: [string, string][]; stderr: string }
84/// | { kind: 'exit'; code: number; stdout: string; stderr: string };
85/// ```
86#[derive(Clone, Debug, Eq, PartialEq)]
87pub enum Action {
88 /// Become real Git.
89 Forward {
90 /// The selected real Git executable.
91 real_git: PathBuf,
92 /// The arguments Git receives:
93 /// no wrapper control,
94 /// fixed transforms applied.
95 arguments: Vec<OsString>,
96 /// Variables added to the inherited environment.
97 overlay: Vec<(OsString, OsString)>,
98 /// Complete warning event lines,
99 /// written to standard error before Git starts.
100 stderr: String,
101 },
102 /// Forward an index writer under its landing lock:
103 /// coordinate,
104 /// run Git as a child,
105 /// release.
106 ForwardCoordinated {
107 /// What the coordinated run needs.
108 forward: Box<CoordinatedForward>,
109 /// Warning events written to standard error before the coordination starts.
110 stderr: String,
111 },
112 /// Forward a command that the linked-worktree copy settles:
113 /// run Git as a child that carries the settlement lease,
114 /// recover an interrupted copy first,
115 /// copy the ignored state into the worktrees the command created.
116 ForwardSettled {
117 /// The selected real Git executable.
118 real_git: PathBuf,
119 /// The arguments Git receives.
120 arguments: Vec<OsString>,
121 /// Variables added to the inherited environment.
122 overlay: Vec<(OsString, OsString)>,
123 /// What the copy needs to know about the invocation.
124 copy: Box<CopyFacts>,
125 /// Warning events written to standard error before Git starts.
126 stderr: String,
127 },
128 /// Forward a plain push as a child, so a push that lost a race to another session can be integrated:
129 /// run Git with the caller's streams, then merge in what landed on the remote and publish the merge.
130 ForwardPush {
131 /// What the run and the integration need.
132 forward: Box<PushForward>,
133 /// Warning events written to standard error before Git starts.
134 stderr: String,
135 },
136 /// Stop without running the caller's command.
137 Exit {
138 /// Process exit code.
139 code: i32,
140 /// Complete text for standard output,
141 /// already line-terminated.
142 stdout: String,
143 /// Complete text for standard error,
144 /// already line-terminated.
145 stderr: String,
146 },
147}
148
149/// What:
150/// Build the stop action for a wrapper failure described in prose.
151/// `&str` borrows the message;
152/// the action owns its formatted copy.
153/// Why:
154/// These failures (no real Git,
155/// a forwarding loop,
156/// an unreadable repository)
157/// print one prefixed,
158/// line-terminated diagnostic on standard error and exit 2.
159///
160/// In TS you'd write (pseudocode):
161/// ```ts
162/// function failure(message: string): Action;
163/// ```
164pub fn failure(message: &str) -> Action {
165 return Action::Exit {
166 code: ENGINE_FAILURE_EXIT_CODE,
167 // `String::new()` is empty owned text: nothing goes to standard output.
168 stdout: String::new(),
169 // `format!` builds the owned, line-terminated text.
170 stderr: format!("cli-git: {message}\n"),
171 };
172}