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
//! Controls for the private commit index: copies, each selection mode, pathspec files and listings.

use super::*;
use crate::capture_invocation::{capture_invocation_base, capture_invocation_layout};
use crate::test_support::{fixture, git, git_context, git_text, remove, repository};
use crate::transaction_journal::RefFormat;
use std::time::{Duration, SystemTime};

/// A repository with `a.txt` and `b.txt` committed, and the private indexes beside the real index.
fn committed_repository(root: &Path) -> (PathBuf, CaptureIndexes, String) {
    let repo: PathBuf = repository(root, "repo");
    std::fs::write(repo.join("a.txt"), "a\n").expect("a");
    std::fs::write(repo.join("b.txt"), "b\n").expect("b");
    git(repo.as_path(), &["add", "a.txt", "b.txt"]);
    git(repo.as_path(), &["commit", "--quiet", "--message=files"]);
    let base: String = git_text(repo.as_path(), &["rev-parse", "HEAD"]);
    let indexes: CaptureIndexes = CaptureIndexes {
        captured_index: repo.join(".git").join("captured.index"),
        commit_index: repo.join(".git").join("commit.index"),
        object_directory: repo.join(".git").join("objects"),
    };
    return (repo, indexes, base);
}

/// Builds the selection of one mode with the given pathspecs and base.
fn selection<'a>(
    mode: TransactionMode,
    pathspecs: &'a [String],
    base: &'a str,
) -> IndexSelection<'a> {
    return IndexSelection {
        mode,
        pathspecs,
        pathspec_file: None,
        pathspec_file_nul: false,
        stage_into_index: false,
        stage_tracked_changes: false,
        base_revision: base,
    };
}

/// Runs initialization of the private index with the real index of the repository.
fn initialize(repo: &Path, indexes: &CaptureIndexes, choice: &IndexSelection) {
    initialize_commit_index(&git_context(), repo, repo.join(".git").join("index").as_path(), indexes, choice)
        .expect("initialize");
}

/// The repository paths the private index changes against the base.
fn changed(repo: &Path, indexes: &CaptureIndexes, base: &str) -> Vec<String> {
    return list_changed_index_paths(
        &git_context(),
        repo,
        indexes.commit_index.as_path(),
        base,
        indexes.object_directory.as_path(),
    )
    .expect("changed");
}

/// A copy keeps the source's modification time, and a missing source is an error.
#[test]
fn copy_keeps_the_source_modification_time() {
    let root: PathBuf = fixture("commit-index-copy-time");
    let source: PathBuf = root.join("source");
    std::fs::write(&source, "index").expect("source");
    let past: SystemTime = SystemTime::now() - Duration::from_secs(3600);
    std::fs::File::options()
        .write(true)
        .open(&source)
        .and_then(|file| { return file.set_times(FileTimes::new().set_modified(past)); })
        .expect("age the source");
    let destination: PathBuf = root.join("copy");
    copy_index_with_timestamps(source.as_path(), destination.as_path()).expect("copy");
    assert_eq!(
        std::fs::metadata(&destination).expect("copy").modified().expect("mtime"),
        std::fs::metadata(&source).expect("source").modified().expect("mtime")
    );
    assert!(copy_index_with_timestamps(root.join("absent").as_path(), destination.as_path()).is_err());
    remove(root.as_path());
}

/// Explicit-path selection stages exactly the named paths against the recorded base.
#[test]
fn explicit_path_stages_only_the_named_paths() {
    let root: PathBuf = fixture("commit-index-explicit");
    let (repo, indexes, base) = committed_repository(root.as_path());
    std::fs::write(repo.join("a.txt"), "changed\n").expect("change a");
    std::fs::write(repo.join("b.txt"), "changed\n").expect("change b");
    let pathspecs: Vec<String> = vec![String::from("a.txt")];
    initialize(&repo, &indexes, &selection(TransactionMode::ExplicitPath, &pathspecs, &base));
    assert_eq!(changed(&repo, &indexes, &base), vec![String::from("a.txt")]);
    remove(root.as_path());
}

/// An index commit without staging copies the real index unchanged.
#[test]
fn index_without_staging_matches_the_real_index() {
    let root: PathBuf = fixture("commit-index-index-plain");
    let (repo, indexes, base) = committed_repository(root.as_path());
    std::fs::write(repo.join("a.txt"), "changed\n").expect("change a");
    git(repo.as_path(), &["add", "a.txt"]);
    std::fs::write(repo.join("u.txt"), "u\n").expect("untracked");
    let pathspecs: Vec<String> = Vec::new();
    initialize(&repo, &indexes, &selection(TransactionMode::Index, &pathspecs, &base));
    let real_tree: String = write_private_tree(
        &git_context(),
        repo.as_path(),
        repo.join(".git").join("index").as_path(),
        indexes.object_directory.as_path(),
    )
    .expect("real tree");
    let private_tree: String = write_private_tree(
        &git_context(),
        repo.as_path(),
        indexes.commit_index.as_path(),
        indexes.object_directory.as_path(),
    )
    .expect("private tree");
    assert_eq!(private_tree, real_tree);
    remove(root.as_path());
}

/// `commit -a` staging stages every tracked change and leaves untracked files out.
#[test]
fn index_with_tracked_staging_stages_only_tracked_changes() {
    let root: PathBuf = fixture("commit-index-tracked");
    let (repo, indexes, base) = committed_repository(root.as_path());
    std::fs::write(repo.join("a.txt"), "changed\n").expect("change a");
    std::fs::write(repo.join("u.txt"), "u\n").expect("untracked");
    let pathspecs: Vec<String> = Vec::new();
    let mut choice: IndexSelection = selection(TransactionMode::Index, &pathspecs, &base);
    choice.stage_tracked_changes = true;
    initialize(&repo, &indexes, &choice);
    assert_eq!(changed(&repo, &indexes, &base), vec![String::from("a.txt")]);
    remove(root.as_path());
}

/// An include stages the named paths into the copy, and the real index stays as it was.
#[test]
fn include_stages_the_named_path_into_the_copy_only() {
    let root: PathBuf = fixture("commit-index-include");
    let (repo, indexes, base) = committed_repository(root.as_path());
    std::fs::write(repo.join("b.txt"), "changed\n").expect("change b");
    std::fs::write(repo.join("a.txt"), "changed\n").expect("change a");
    let real_before: Vec<u8> = std::fs::read(repo.join(".git").join("index")).expect("index");
    let pathspecs: Vec<String> = vec![String::from("b.txt")];
    let mut choice: IndexSelection = selection(TransactionMode::Index, &pathspecs, &base);
    choice.stage_into_index = true;
    initialize(&repo, &indexes, &choice);
    assert_eq!(changed(&repo, &indexes, &base), vec![String::from("b.txt")]);
    assert_eq!(std::fs::read(repo.join(".git").join("index")).expect("index"), real_before);
    remove(root.as_path());
}

/// A pathspec file read with NUL records stages each of its paths.
#[test]
fn pathspec_file_with_nul_records_selects_each_path() {
    let root: PathBuf = fixture("commit-index-nul-file");
    let (repo, indexes, base) = committed_repository(root.as_path());
    std::fs::write(repo.join("a.txt"), "changed\n").expect("change a");
    std::fs::write(repo.join("b.txt"), "changed\n").expect("change b");
    let list: PathBuf = root.join("list");
    std::fs::write(&list, b"a.txt\0b.txt\0").expect("list");
    let pathspecs: Vec<String> = Vec::new();
    let mut choice: IndexSelection = selection(TransactionMode::ExplicitPath, &pathspecs, &base);
    choice.pathspec_file = Some(list.as_path());
    choice.pathspec_file_nul = true;
    initialize(&repo, &indexes, &choice);
    assert_eq!(
        changed(&repo, &indexes, &base),
        vec![String::from("a.txt"), String::from("b.txt")]
    );
    remove(root.as_path());
}

/// A pathspec file read with newline records is the same selection as a list of positional paths.
#[test]
fn pathspec_file_with_newline_records_selects_one_path() {
    let root: PathBuf = fixture("commit-index-newline-file");
    let (repo, indexes, base) = committed_repository(root.as_path());
    std::fs::write(repo.join("a.txt"), "changed\n").expect("change a");
    std::fs::write(repo.join("b.txt"), "changed\n").expect("change b");
    let list: PathBuf = root.join("list");
    std::fs::write(&list, "a.txt\n").expect("list");
    let pathspecs: Vec<String> = Vec::new();
    let mut choice: IndexSelection = selection(TransactionMode::ExplicitPath, &pathspecs, &base);
    choice.pathspec_file = Some(list.as_path());
    initialize(&repo, &indexes, &choice);
    assert_eq!(changed(&repo, &indexes, &base), vec![String::from("a.txt")]);
    remove(root.as_path());
}

/// A relative pathspec file source resolves against the invocation directory,
///  an absolute one stays as written, and an absent source is no file.
#[test]
fn pathspec_source_resolves_against_the_effective_directory() {
    let root: PathBuf = fixture("commit-index-source");
    let mut empty: &[u8] = &[];
    assert_eq!(
        materialize_pathspec_file(root.as_path(), root.join("cwd").as_path(), Some("list"), &mut empty).expect("relative"),
        Some(root.join("cwd").join("list"))
    );
    assert_eq!(
        materialize_pathspec_file(root.as_path(), root.as_path(), Some("/abs/list"), &mut empty).expect("absolute"),
        Some(PathBuf::from("/abs/list"))
    );
    assert_eq!(
        materialize_pathspec_file(root.as_path(), root.as_path(), None, &mut empty).expect("absent"),
        None
    );
    remove(root.as_path());
}

/// Standard input is copied once into the transaction, with private mode, and its bytes are kept exactly.
#[test]
fn pathspec_from_standard_input_is_copied_once() {
    let root: PathBuf = fixture("commit-index-stdin");
    let mut input: &[u8] = b"a.txt\0b.txt\0";
    let path: PathBuf = materialize_pathspec_file(root.as_path(), root.as_path(), Some("-"), &mut input)
        .expect("stdin")
        .expect("path");
    assert_eq!(path, root.join(PATHSPEC_INPUT_NAME));
    assert_eq!(std::fs::read(&path).expect("copy"), b"a.txt\0b.txt\0");
    remove(root.as_path());
}

/// The written tree is the tree Git reports for the private index, and it is a tree object.
#[test]
fn private_tree_is_the_tree_of_the_private_index() {
    let root: PathBuf = fixture("commit-index-tree");
    let (repo, indexes, base) = committed_repository(root.as_path());
    std::fs::write(repo.join("a.txt"), "changed\n").expect("change a");
    let pathspecs: Vec<String> = vec![String::from("a.txt")];
    initialize(&repo, &indexes, &selection(TransactionMode::ExplicitPath, &pathspecs, &base));
    let oid: String = write_private_tree(
        &git_context(),
        repo.as_path(),
        indexes.commit_index.as_path(),
        indexes.object_directory.as_path(),
    )
    .expect("tree");
    assert_eq!(git_text(repo.as_path(), &["cat-file", "-t", oid.as_str()]), "tree");
    assert_ne!(oid, git_text(repo.as_path(), &["rev-parse", "HEAD^{tree}"]));
    remove(root.as_path());
}

/// A conflicted path is listed once, even though it has several unmerged stages.
#[test]
fn unmerged_paths_are_listed_once() {
    let root: PathBuf = fixture("commit-index-unmerged");
    let (repo, indexes, _base) = committed_repository(root.as_path());
    let blob: String = git_text(repo.as_path(), &["hash-object", "-w", "a.txt"]);
    let info: String = format!("100644 {blob} 1\ta.txt\n100644 {blob} 2\ta.txt\n100644 {blob} 3\ta.txt\n");
    let mut request = indexed_request(
        repo.as_path(),
        &["update-index", "--index-info"],
        Some(indexes.commit_index.as_path()),
        Some(indexes.object_directory.as_path()),
    );
    request.input = Some(info.into_bytes());
    run_git_checked(&git_context(), &request).expect("conflict stages");
    assert_eq!(
        list_unmerged_index_paths(&git_context(), repo.as_path(), indexes.commit_index.as_path()).expect("unmerged"),
        vec![String::from("a.txt")]
    );
    remove(root.as_path());
}

/// The layout of a repository and the capture of its base give the same base the selection uses.
#[test]
fn selection_base_is_the_recorded_base() {
    let root: PathBuf = fixture("commit-index-base");
    let (repo, _indexes, base) = committed_repository(root.as_path());
    let layout = capture_invocation_layout(&git_context(), repo.as_path(), false).expect("layout");
    let capture = capture_invocation_base(&git_context(), repo.as_path(), layout).expect("capture");
    assert_eq!(capture.layout.ref_format, RefFormat::Files);
    assert_eq!(crate::capture_invocation::base_revision(&capture), base);
    remove(root.as_path());
}


/// The incumbent's committed tree when one file is selected, recorded by `native-interop/capture-differential.mjs`.
const INCUMBENT_ONE_FILE_TREE: &str = "4f40a1ec2c64780454051d9d363c6870a6ef2f6e";
/// The incumbent's committed tree when both changed files are selected.
const INCUMBENT_TWO_FILE_TREE: &str = "efe77971c159b0a29a5d97e7169b8c35fa33a1f1";

/// Builds the state the incumbent differential starts from: two committed files, both changed, one untracked.
fn differential_state(root: &Path, stage: &[&str]) -> (PathBuf, CaptureIndexes, String) {
    std::fs::create_dir_all(root).expect("state root");
    let repo: PathBuf = repository(root, "repo");
    std::fs::write(repo.join("a.txt"), "a\n").expect("a");
    std::fs::write(repo.join("b.txt"), "b\n").expect("b");
    git(repo.as_path(), &["add", "a.txt", "b.txt"]);
    git(repo.as_path(), &["commit", "--quiet", "--message=base"]);
    std::fs::write(repo.join("a.txt"), "a-changed\n").expect("a changed");
    std::fs::write(repo.join("b.txt"), "b-changed\n").expect("b changed");
    std::fs::write(repo.join("u.txt"), "u\n").expect("untracked");
    if !stage.is_empty() {
        let mut arguments: Vec<&str> = vec!["add"];
        arguments.extend(stage.iter().copied());
        git(repo.as_path(), &arguments);
    }
    let base: String = git_text(repo.as_path(), &["rev-parse", "HEAD"]);
    let transaction: PathBuf = root.join("transaction");
    std::fs::create_dir(&transaction).expect("transaction");
    let indexes: CaptureIndexes = CaptureIndexes {
        captured_index: transaction.join("captured.index"),
        commit_index: transaction.join("commit.index"),
        object_directory: repo.join(".git").join("objects"),
    };
    return (repo, indexes, base);
}

/// Initializes the private index for one selection and returns the tree written from it.
fn native_tree(repo: &Path, indexes: &CaptureIndexes, choice: &IndexSelection) -> String {
    initialize_commit_index(&git_context(), repo, repo.join(".git").join("index").as_path(), indexes, choice)
        .expect("initialize");
    return write_private_tree(
        &git_context(),
        repo,
        indexes.commit_index.as_path(),
        indexes.object_directory.as_path(),
    )
    .expect("tree");
}

/// Explicit-path selections, with a pathspec, a newline pathspec file, a NUL pathspec file and stdin,
///  yield the incumbent's one-file tree, and the two-file selections yield the incumbent's two-file tree.
#[test]
fn private_index_trees_match_the_incumbent_for_each_selection() {
    let root: PathBuf = fixture("commit-index-incumbent-explicit");
    {
    let (repo, indexes, base) = differential_state(root.join("p").as_path(), &[]);
    let pathspecs: Vec<String> = vec![String::from("a.txt")];
    assert_eq!(
        native_tree(&repo, &indexes, &selection(TransactionMode::ExplicitPath, &pathspecs, &base)),
        INCUMBENT_ONE_FILE_TREE,
        "explicit-path"
    );
    }
    remove(root.as_path());
}

/// The index-mode and tracked-staging selections match the incumbent's trees.
#[test]
fn index_mode_trees_match_the_incumbent() {
    let root: PathBuf = fixture("commit-index-incumbent-index");
    let none: Vec<String> = Vec::new();

    {
    let (repo, indexes, base) = differential_state(root.join("plain").as_path(), &["a.txt"]);
    assert_eq!(
        native_tree(&repo, &indexes, &selection(TransactionMode::Index, &none, &base)),
        INCUMBENT_ONE_FILE_TREE,
        "index-plain"
    );

    }
    {
    let (repo, indexes, base) = differential_state(root.join("tracked").as_path(), &[]);
    let mut tracked: IndexSelection = selection(TransactionMode::Index, &none, &base);
    tracked.stage_tracked_changes = true;
    assert_eq!(native_tree(&repo, &indexes, &tracked), INCUMBENT_TWO_FILE_TREE, "tracked-all");

    }
    {
    let (repo, indexes, base) = differential_state(root.join("include").as_path(), &["a.txt"]);
    let include_paths: Vec<String> = vec![String::from("b.txt")];
    let mut include: IndexSelection = selection(TransactionMode::Index, &include_paths, &base);
    include.stage_into_index = true;
    assert_eq!(native_tree(&repo, &indexes, &include), INCUMBENT_TWO_FILE_TREE, "include");
    }
    remove(root.as_path());
}

/// Pathspec files in both record forms, and standard input, select the same paths the incumbent did.
#[test]
fn pathspec_file_trees_match_the_incumbent() {
    let root: PathBuf = fixture("commit-index-incumbent-files");
    let none: Vec<String> = Vec::new();

    {
    let (repo, indexes, base) = differential_state(root.join("newline").as_path(), &[]);
    let list: PathBuf = root.join("newline.list");
    std::fs::write(&list, "a.txt\n").expect("list");
    let mut newline: IndexSelection = selection(TransactionMode::ExplicitPath, &none, &base);
    newline.pathspec_file = Some(list.as_path());
    assert_eq!(native_tree(&repo, &indexes, &newline), INCUMBENT_ONE_FILE_TREE, "newline file");

    }
    {
    let (repo, indexes, base) = differential_state(root.join("nul").as_path(), &[]);
    let list: PathBuf = root.join("nul.list");
    std::fs::write(&list, b"a.txt\0b.txt\0").expect("nul list");
    let mut nul: IndexSelection = selection(TransactionMode::ExplicitPath, &none, &base);
    nul.pathspec_file = Some(list.as_path());
    nul.pathspec_file_nul = true;
    assert_eq!(native_tree(&repo, &indexes, &nul), INCUMBENT_TWO_FILE_TREE, "nul file");

    }
    {
    let (repo, indexes, base) = differential_state(root.join("stdin").as_path(), &[]);
    let mut input: &[u8] = b"a.txt\n";
    let stdin_path: PathBuf = materialize_pathspec_file(indexes.commit_index.parent().expect("transaction"), &repo, Some("-"), &mut input)
        .expect("stdin")
        .expect("path");
    let mut from_stdin: IndexSelection = selection(TransactionMode::ExplicitPath, &none, &base);
    from_stdin.pathspec_file = Some(stdin_path.as_path());
    assert_eq!(native_tree(&repo, &indexes, &from_stdin), INCUMBENT_ONE_FILE_TREE, "stdin");
    }
    remove(root.as_path());
}

/// A pathspec file that cannot be read makes the interactive run fail, and the failure names the command.
#[test]
fn interactive_selection_reports_a_failed_git_add() {
    let root: PathBuf = fixture("commit-index-interactive-fails");
    let (repo, indexes, base) = differential_state(root.join("state").as_path(), &[]);
    let none: Vec<String> = Vec::new();
    let missing: PathBuf = root.join("missing.list");
    let mut choice: IndexSelection = selection(TransactionMode::ExplicitPath, &none, &base);
    choice.pathspec_file = Some(missing.as_path());
    let error: GitFailure = prepare_interactive_selection(
        &git_context(),
        repo.as_path(),
        indexes.commit_index.as_path(),
        indexes.object_directory.as_path(),
        true,
        &choice,
    )
    .expect_err("missing pathspec file");
    assert!(error.0.starts_with("git add --patch --pathspec-from-file="), "{}", error.0);
    assert!(error.0.contains("exited"), "{}", error.0);
    remove(root.as_path());
}

/// An interactive run over an index with no changes finishes without prompting and succeeds.
#[test]
fn interactive_selection_succeeds_without_changes() {
    let root: PathBuf = fixture("commit-index-interactive-clean");
    let repo: PathBuf = repository(root.as_path(), "repo");
    std::fs::write(repo.join("a.txt"), "a\n").expect("a");
    git(repo.as_path(), &["add", "a.txt"]);
    git(repo.as_path(), &["commit", "--quiet", "--message=a"]);
    let base: String = git_text(repo.as_path(), &["rev-parse", "HEAD"]);
    let transaction: PathBuf = root.join("transaction");
    std::fs::create_dir(&transaction).expect("transaction");
    let commit_index: PathBuf = transaction.join("commit.index");
    copy_index_with_timestamps(repo.join(".git").join("index").as_path(), commit_index.as_path()).expect("copy");
    let none: Vec<String> = Vec::new();
    let choice: IndexSelection = selection(TransactionMode::ExplicitPath, &none, &base);
    prepare_interactive_selection(
        &git_context(),
        repo.as_path(),
        commit_index.as_path(),
        repo.join(".git").join("objects").as_path(),
        false,
        &choice,
    )
    .expect("clean interactive run");
    remove(root.as_path());
}