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
//! What:
//!  Install the corrections a converged `git cli-git fix` settled on into the
//!       worktree,
//!  all of them or none,
//!  and never touch the index.
//! Why:
//!  The installed wrapper's `direct-fix-install.ts` decides this protocol:
//!  every
//!      corrected file must still hold the bytes the fix read,
//!  each replacement is written
//!      beside its file and renamed over it,
//!  a copy of the old file is kept until the end,
//!      and the real index must hold the same bytes afterwards.
//!  Any failure restores every
//!      file already replaced.
//!  Failure messages name no file,
//!  because a pathname can hold
//!      the text a scan exists to keep out of output.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // await installDirectFix({ scope, changedPaths, finalCandidates, originals });
//! ```

/// Import the candidate's mode,
///  which decides the replacement's permissions.
use super::candidate_object::CandidateMode;
/// Import the unique name part shared with the private index directories.
use super::candidate_private_index::unique_suffix;
/// Import the conversion of Git pathname bytes to a native path.
use super::git_metadata::path_from_git_bytes;
/// `Path`/`PathBuf` are borrowed/owned filesystem paths.
use std::path::{Path, PathBuf};
/// `Rc<T>` is a shared,
///  read-only handle.
use std::rc::Rc;

/// The name prefix of the files a fix writes beside each corrected file.
pub const INSTALL_SIBLING_PREFIX: &str = ".cli-git-direct-fix-";

/// The start of every installation failure message.
const INSTALL_FAILED: &str = "cli-git fix could not install its corrections";

/// What:
///  One file to correct:
///  its pathname,
///  mode,
///  the bytes the fix read and the bytes to
///       write.
///  `#[derive(...)]` generates cloning,
///  debug printing and `==`.
/// Why:
///   The read bytes prove nobody changed the file while the fix ran.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type InstallChange = { path: Uint8Array; mode: CandidateMode; original: Uint8Array; replacement: Uint8Array };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct InstallChange {
    /// The pathname,
    ///  relative to the repository's top level,
    ///  as Git's raw bytes.
    pub path: Vec<u8>,
    /// The file's mode;
    ///  executable files stay executable.
    pub mode: CandidateMode,
    /// The bytes the fix read;
    ///  the worktree file must still hold them.
    pub original: Rc<[u8]>,
    /// The corrected bytes.
    pub replacement: Rc<[u8]>,
}

/// What:
///  One prepared replacement:
///  the file,
///  the new bytes beside it,
///  and a copy of the
///       old file beside it.
/// Why:
///   Renaming the new file over the old one replaces it in one step,
///  and the copy
///       restores it if a later step fails.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type PreparedReplacement = { destination: string; prepared: string; backup: string };
/// ```
#[derive(Debug)]
pub struct Replacement {
    /// The worktree file.
    destination: PathBuf,
    /// The corrected bytes,
    ///  written beside it.
    prepared: PathBuf,
    /// A copy of the file as it was,
    ///  beside it.
    backup: PathBuf,
}

/// What:
///  An installation failure message:
///  the shared start,
///  then what happened.
/// Why:
///   Every failure reads the same way and names no file.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const installFailure = (detail: string) => `cli-git fix could not install its corrections: ${detail}`;
/// ```
fn install_failure(detail: &str) -> String {
    return format!("{INSTALL_FAILED}: {detail}");
}

/// What:
///  The real index's bytes,
///  or nothing when it does not exist.
///       `std::io::Result<Option<Vec<u8>>>` is "bytes,
///  absent,
///  or a read failure".
/// Why:
///   A repository without an index file has an empty index,
///  and must still have none
///       after the fix.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function readIndex(path: string): Promise<Uint8Array | typeof INDEX_ABSENT>;
/// ```
pub fn read_index(path: &Path) -> std::io::Result<Option<Vec<u8>>> {
    match std::fs::read(path) {
        Ok(bytes) => return Ok(Some(bytes)),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => return Err(error),
    }
}

/// What:
///  Remove the prepared and backup files of each replacement,
///  ignoring files that
///       were never written.
/// Why:
///   Neither may stay behind after the fix ends,
///  whichever way it ended.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function cleanup(replacements): Promise<void>;
/// ```
fn cleanup(replacements: &[Replacement]) {
    for replacement in replacements {
        // What: `let _ = ...` discards each removal's `Result` on purpose.
        // Why:  A file that was never written cannot be removed, and that is fine.
        //
        // In TS you'd write (pseudocode):
        // ```ts
        // await rm(prepared, { force: true });
        // ```
        let _ = std::fs::remove_file(replacement.prepared.as_path());
        let _ = std::fs::remove_file(replacement.backup.as_path());
    }
}

/// What:
///  Rename each installed file's backup over it,
///  last installed first.
///       `Result<(), String>` is success or the first restore that failed.
/// Why:
///   The worktree returns to the bytes it held before the fix.
///  A backup that cannot
///       be restored is left in place,
///  so its bytes are not lost.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function rollback(installed): Promise<void>;
/// ```
fn rollback(installed: &[Replacement]) -> Result<(), String> {
    // `.iter().rev()` visits the installed files in reverse order.
    for replacement in installed.iter().rev() {
        if let Err(error) = std::fs::rename(
            replacement.backup.as_path(),
            replacement.destination.as_path(),
        ) {
            return Err(format!(
                "restoring a corrected file failed ({error}); its original bytes remain beside it in a file whose name starts with {INSTALL_SIBLING_PREFIX}"
            ));
        }
    }
    return Ok(());
}

/// What:
///  Write `bytes` to a new file at `path` with permissions for `mode`.
/// Why:
///   The file must not exist yet,
///  so no other file is overwritten;
///  executable
///       files get `0755` and others `0644`,
///  before the umask,
///  as the installed wrapper
///       writes them.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await writeFile(path, bytes, { mode: mode === 'executable' ? 0o755 : 0o644, flag: 'wx' });
/// ```
fn write_new(path: &Path, bytes: &[u8], mode: CandidateMode) -> std::io::Result<()> {
    /// `Write` adds `write_all` to the open file.
    use std::io::Write;
    let mut options: std::fs::OpenOptions = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        /// `OpenOptionsExt` adds `mode`,
        ///  the permissions a new file is created with.
        use std::os::unix::fs::OpenOptionsExt;
        let permissions: u32 = if mode == CandidateMode::Executable {
            0o755
        } else {
            0o644
        };
        options.mode(permissions);
    }
    #[cfg(not(unix))]
    {
        // Only Unix records an executable bit; the mode decides nothing elsewhere.
        let _ = mode;
    }
    return options.open(path)?.write_all(bytes);
}

/// What:
///  Prepare every replacement:
///  check the file still holds the bytes the fix read,
///       copy it beside itself,
///  and write the corrected bytes beside it.
/// Why:
///   Nothing in the worktree changes until every file is prepared,
///  so a file that
///       changed under the fix stops it before any file is replaced.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function prepareReplacements(root, changes): Promise<PreparedReplacement[]>;
/// ```
pub fn prepare_replacements(
    root: &Path,
    changes: &[InstallChange],
) -> Result<Vec<Replacement>, String> {
    let mut replacements: Vec<Replacement> = Vec::new();
    for change in changes {
        let Some(relative) = path_from_git_bytes(change.path.as_slice()) else {
            cleanup(replacements.as_slice());
            return Err(install_failure(
                "Git named a corrected file this system cannot represent, so no file was changed.",
            ));
        };
        let destination: PathBuf = root.join(relative);
        let current: Vec<u8> = match std::fs::read(destination.as_path()) {
            Ok(bytes) => bytes,
            Err(error) => {
                cleanup(replacements.as_slice());
                return Err(install_failure(
                    format!("reading a corrected file failed ({error}), so no file was changed.")
                        .as_str(),
                ));
            }
        };
        if current.as_slice() != &*change.original {
            cleanup(replacements.as_slice());
            return Err(install_failure(
                "a selected file changed while cli-git fix ran, so no file was changed. Run cli-git fix again.",
            ));
        }
        let parent: PathBuf = match destination.parent() {
            Some(found) => found.to_path_buf(),
            None => root.to_path_buf(),
        };
        let prefix: String = format!("{INSTALL_SIBLING_PREFIX}{}", unique_suffix());
        let replacement: Replacement = Replacement {
            prepared: parent.join(format!("{prefix}.new")),
            backup: parent.join(format!("{prefix}.old")),
            destination,
        };
        let written: std::io::Result<()> = match std::fs::copy(
            replacement.destination.as_path(),
            replacement.backup.as_path(),
        ) {
            Ok(_) => write_new(
                replacement.prepared.as_path(),
                &change.replacement,
                change.mode,
            ),
            Err(error) => Err(error),
        };
        // Recorded before the failure check, so a failure still removes what was written.
        replacements.push(replacement);
        if let Err(error) = written {
            cleanup(replacements.as_slice());
            return Err(install_failure(
                format!("writing a corrected file beside its original failed ({error}), so no file was changed.").as_str(),
            ));
        }
    }
    return Ok(replacements);
}

/// What:
///  Rename every prepared file over its destination,
///  then prove the real index still
///       holds `index_before`.
///  `&Option<Vec<u8>>` borrows the bytes read before,
///  or absence.
/// Why:
///   The fix changes the worktree only.
///  A rename that fails,
///  an index that changed,
///       or an index that cannot be read restores every file already replaced.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function installPrepared(replacements, realIndexPath, indexBefore): Promise<void>;
/// ```
pub fn install_prepared(
    replacements: &[Replacement],
    real_index: &Path,
    index_before: &Option<Vec<u8>>,
) -> Result<(), String> {
    let mut installed: usize = 0;
    let mut failure: Option<String> = None;
    for replacement in replacements {
        if let Err(error) = std::fs::rename(
            replacement.prepared.as_path(),
            replacement.destination.as_path(),
        ) {
            failure = Some(format!("replacing a corrected file failed ({error})"));
            break;
        }
        installed += 1;
    }
    if failure.is_none() {
        failure = match read_index(real_index) {
            Ok(after) if after == *index_before => None,
            Ok(_) => Some(String::from("the index changed while cli-git fix ran")),
            Err(error) => Some(format!("reading the index again failed ({error})")),
        };
    }
    let Some(cause) = failure else {
        cleanup(replacements);
        return Ok(());
    };
    // `..installed` is the files already replaced, which the rollback restores.
    let restored: Result<(), String> = rollback(&replacements[..installed]);
    match restored {
        Ok(()) => {
            cleanup(replacements);
            return Err(install_failure(
                format!("{cause}, so every corrected file was restored. Run cli-git fix again.")
                    .as_str(),
            ));
        }
        Err(restore_failure) => {
            // Prepared files are removed; backups stay, because one of them is needed.
            for replacement in replacements {
                let _ = std::fs::remove_file(replacement.prepared.as_path());
            }
            return Err(install_failure(
                format!("{cause}, and {restore_failure}.").as_str(),
            ));
        }
    }
}

/// What:
///  Install every change,
///  or none:
///  read the real index,
///  prepare,
///  then rename and
///       verify.
///  `Result<(), String>` is success or the failure message to report.
/// Why:
///   The whole protocol of the installed wrapper's `installDirectFix`,
///  apart from the
///       landing lock,
///  which the native wrapper does not take.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function installCorrections(root, realIndexPath, changes): Promise<void>;
/// ```
pub fn install_corrections(
    root: &Path,
    real_index: &Path,
    changes: &[InstallChange],
) -> Result<(), String> {
    let index_before: Option<Vec<u8>> = match read_index(real_index) {
        Ok(found) => found,
        Err(error) => {
            return Err(install_failure(
                format!("reading the index failed ({error}), so no file was changed.").as_str(),
            ));
        }
    };
    let replacements: Vec<Replacement> = prepare_replacements(root, changes)?;
    return install_prepared(replacements.as_slice(), real_index, &index_before);
}

/// Installation,
///  rollback and cleanup controls stay out of the release executable.
#[cfg(test)]
#[path = "direct_fix_install_tests.rs"]
mod tests;