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
//! What:
//! The environment variables cli-git adds for every real Git it starts.
//! Why:
//! Native Git must write lock-owner PID files,
//! and a wrapper must be able to
//! notice that another wrapper selected it as "real Git".
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // spawn(gitPath, args, { env: { ...process.env, ...childEnvironmentOverlay(process.env, gitPath) } });
//! ```
/// What:
/// `OsStr` is borrowed operating-system text;
/// `OsString` is its owned form.
/// They hold raw OS bytes (siblings `&str`/`String` must be valid UTF-8).
/// Why:
/// Environment names and values need not be UTF-8 and are passed through unchanged.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// // Node exposes process.env as strings; there is no byte-preserving equivalent.
/// ```
use ;
/// `Path` is a borrowed filesystem path made of the same raw OS bytes.
use Path;
/// Git's count variable for environment-supplied configuration.
pub const COUNT_VARIABLE: &str = "GIT_CONFIG_COUNT";
/// What:
/// The variable naming the executable this wrapper selected as real Git.
/// Why:
/// If the selected executable is itself a cli-git wrapper (for example another
/// build that file identity cannot recognise),
/// it finds its own path here and
/// stops instead of forwarding back and forth forever.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const FORWARD_TARGET_VARIABLE = 'CLI_GIT_NATIVE_FORWARD_TARGET';
/// ```
pub const FORWARD_TARGET_VARIABLE: &str = "CLI_GIT_NATIVE_FORWARD_TARGET";
/// Key spelling written into the environment;
/// Git compares it case-insensitively.
const LOCKFILE_PID_KEY: &str = "core.lockfilePid";
/// What:
/// Largest count Git accepts (`INT_MAX`),
/// as an unsigned 64-bit integer.
/// Siblings:
/// `u32`,
/// `usize`,
/// `i64`.
/// Why:
/// `u64` holds every value C's `strtoul` can return on a 64-bit platform,
/// so
/// the comparison Git makes can be repeated without wrapping.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const GIT_MAX_CONFIG_COUNT = 2_147_483_647;
/// ```
const GIT_MAX_CONFIG_COUNT: u64 = 2_147_483_647;
/// What:
/// Look one variable up the way C's `getenv` does:
/// the first entry wins.
/// `&[(OsString, OsString)]` borrows a list of name/value pairs.
/// `Option<OsString>` is "an owned value or nothing".
/// Why:
/// The overlay must read exactly what the Git child will read.
/// Returning an
/// owned copy keeps the function free of borrow bookkeeping for the caller.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function environmentValue(environment: [string, string][], name: string): string | undefined;
/// ```
/// What:
/// Parse `GIT_CONFIG_COUNT` exactly as Git 2.56.0 `config.c` does with
/// `strtoul(env, &endp, 10)` followed by its `*endp` and `INT_MAX` checks.
/// `Option<u64>` is `Some(count)` when Git accepts the value,
/// `None` when Git
/// would report "bogus count" or "too many entries".
/// Why:
/// The overlay must append after the entries Git will really read,
/// and must
/// leave a value Git rejects untouched so Git reports the caller's own mistake.
/// `strtoul` skips leading whitespace and accepts one sign;
/// a negated nonzero
/// value wraps above `INT_MAX`,
/// so only `-0` survives with a minus sign.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function parseConfigCount(value: string): number | undefined;
/// ```
/// What:
/// Report whether a configuration value is one of the true spellings cli-git
/// itself writes or recognises (`true`,
/// `yes`,
/// `on`,
/// `1`,
/// any ASCII case).
/// Why:
/// When the last numbered `core.lockfilePid` entry already reads as true,
/// a
/// nested cli-git must not grow the list again.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function isTrueSpelling(value: string): boolean;
/// ```
/// What:
/// Compute the variables that append `core.lockfilePid=true` to an environment's
/// numbered Git configuration.
/// `Vec<(OsString, OsString)>` is an owned list of
/// name/value pairs;
/// it is empty when nothing must change.
/// Why:
/// Native Git then writes an owner PID file beside each lock it takes,
/// which
/// lock-ownership checks rely on.
/// Numbered entries are read before
/// `GIT_CONFIG_PARAMETERS`,
/// so a caller's explicit `-c core.lockfilePid=false`
/// still wins.
/// A count Git would reject,
/// or a numbered entry Git would report
/// as missing,
/// is left untouched so Git reports the caller's own mistake.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function lockfilePidOverlay(environment: [string, string][]): [string, string][];
/// ```
/// The prefix of a variable in the `environment` of an in-process command that the Git children of that command receive
/// without the prefix: `CLI_GIT_CHILD_SET_GIT_EDITOR` becomes `GIT_EDITOR` for them. The wrapper's second merge pass uses it to
/// run the continuing step of a rebase without an editor, as the rebase would have run without a conflict. Nothing of the
/// wrapper's own process environment is changed.
pub const CHILD_SET_PREFIX: &str = "CLI_GIT_CHILD_SET_";
/// What:
/// Compute every variable cli-git sets for a real Git child:
/// the lock PID
/// injection plus the forward-target marker naming `real_git`.
/// Why:
/// Every Git the wrapper starts,
/// forwarded or queried,
/// gets the same additions;
/// the rest of the caller's environment is inherited unchanged.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function childEnvironmentOverlay(environment: [string, string][], realGit: string): [string, string][];
/// ```
/// What:
/// The complete environment of a Git child that is started with a cleared environment:
/// the caller's variables, with every name the overlay sets replaced by the overlay's value.
/// Why:
/// The incumbent's push coordination, push and manual-push probe inherit the whole process environment
/// (`HOME`, `PATH`, `SSH_AUTH_SOCK`, the user's `GIT_*` variables) plus the overlay,
/// and a credential helper or transport needs them;
/// the overlay alone would start Git with an almost empty environment.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// ({ ...process.env, ...Object.fromEntries(overlay) })
/// ```
/// Lookup and count-parsing controls,
/// checked against real Git.
/// Overlay controls and the shared environment builder stay out of the release executable.