gitoxide_core/repository/attributes/
validate_baseline.rs

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
use crate::OutputFormat;

pub struct Options {
    pub format: OutputFormat,
    pub statistics: bool,
    pub ignore: bool,
}

pub(crate) mod function {
    use std::{
        collections::BTreeSet,
        io,
        io::{BufRead, Write},
        iter::Peekable,
        ops::Sub,
        path::PathBuf,
        sync::atomic::Ordering,
    };

    use anyhow::{anyhow, bail};
    use gix::{attrs::Assignment, bstr::BString, Count, Progress};

    use crate::{
        repository::attributes::{query::attributes_cache, validate_baseline::Options},
        OutputFormat,
    };

    pub fn validate_baseline(
        repo: gix::Repository,
        paths: Option<impl Iterator<Item = BString> + Send + 'static>,
        mut progress: impl gix::NestedProgress + 'static,
        mut out: impl io::Write,
        mut err: impl io::Write,
        Options {
            format,
            statistics,
            mut ignore,
        }: Options,
    ) -> anyhow::Result<()> {
        if format != OutputFormat::Human {
            bail!("JSON output isn't implemented yet");
        }

        if repo.is_bare() {
            writeln!(
                err,
                "Repo {:?} is bare - disabling git-ignore baseline as `git check-ignore` needs a worktree",
                repo.path()
            )
            .ok();
            ignore = false;
        }
        let mut num_entries = None;
        let paths = paths.map_or_else(
            {
                let repo = repo.clone();
                let num_entries = &mut num_entries;
                move || -> anyhow::Result<_> {
                    let index = repo.index_or_load_from_head()?.into_owned();
                    let (entries, path_backing) = index.into_parts().0.into_entries();
                    *num_entries = Some(entries.len());
                    let iter = Box::new(entries.into_iter().map(move |e| e.path_in(&path_backing).to_owned()));
                    Ok(iter as Box<dyn Iterator<Item = BString> + Send + 'static>)
                }
            },
            |paths| anyhow::Result::Ok(Box::new(paths)),
        )?;

        let (tx_base, rx_base) = std::sync::mpsc::channel::<(String, Baseline)>();
        let feed_attrs = {
            let (tx, rx) = std::sync::mpsc::sync_channel::<BString>(100);
            std::thread::spawn({
                let path = repo.path().to_owned();
                let tx_base = tx_base.clone();
                let mut progress = progress.add_child("attributes");
                move || -> anyhow::Result<()> {
                    let mut child =
                        std::process::Command::from(gix::command::prepare(gix::path::env::exe_invocation()))
                            .args(["check-attr", "--stdin", "-a"])
                            .stdin(std::process::Stdio::piped())
                            .stdout(std::process::Stdio::piped())
                            .stderr(std::process::Stdio::null())
                            .current_dir(path)
                            .spawn()?;

                    std::thread::spawn({
                        let mut stdin = child.stdin.take().expect("we configured it");
                        move || -> anyhow::Result<()> {
                            progress.init(num_entries, gix::progress::count("paths"));
                            let start = std::time::Instant::now();
                            for path in rx {
                                progress.inc();
                                stdin.write_all(&path)?;
                                stdin.write_all(b"\n")?;
                            }
                            progress.show_throughput(start);
                            Ok(())
                        }
                    });

                    let stdout = std::io::BufReader::new(child.stdout.take().expect("we configured it"));
                    let mut lines = stdout.lines().map_while(Result::ok).peekable();
                    while let Some(baseline) = parse_attributes(&mut lines) {
                        if tx_base.send(baseline).is_err() {
                            child.kill().ok();
                            break;
                        }
                    }

                    Ok(())
                }
            });
            tx
        };
        let work_dir = ignore
            .then(|| {
                repo.work_dir()
                    .map(ToOwned::to_owned)
                    .ok_or_else(|| anyhow!("repository at {:?} must have a worktree checkout", repo.path()))
            })
            .transpose()?;
        let feed_excludes = ignore.then(|| {
            let (tx, rx) = std::sync::mpsc::sync_channel::<BString>(100);
            std::thread::spawn({
                let path = work_dir.expect("present if we are here");
                let tx_base = tx_base.clone();
                let mut progress = progress.add_child("excludes");
                move || -> anyhow::Result<()> {
                    let mut child =
                        std::process::Command::from(gix::command::prepare(gix::path::env::exe_invocation()))
                            .args(["check-ignore", "--stdin", "-nv", "--no-index"])
                            .stdin(std::process::Stdio::piped())
                            .stdout(std::process::Stdio::piped())
                            .stderr(std::process::Stdio::null())
                            .current_dir(path)
                            .spawn()?;

                    std::thread::spawn({
                        let mut stdin = child.stdin.take().expect("we configured it");
                        move || -> anyhow::Result<()> {
                            progress.init(num_entries, gix::progress::count("paths"));
                            let start = std::time::Instant::now();
                            for path in rx {
                                progress.inc();
                                stdin.write_all(path.as_ref())?;
                                stdin.write_all(b"\n")?;
                            }
                            progress.show_throughput(start);
                            Ok(())
                        }
                    });

                    let stdout = std::io::BufReader::new(child.stdout.take().expect("we configured it"));
                    for line in stdout.lines() {
                        let line = line?;
                        if let Some(baseline) = parse_exclude(&line) {
                            if tx_base.send(baseline).is_err() {
                                child.kill().ok();
                                break;
                            }
                        } else {
                            eprintln!("Failed to parse line {line:?} - ignored");
                        }
                    }

                    Ok(())
                }
            });
            tx
        });
        drop(tx_base);

        std::thread::spawn(move || {
            for path in paths {
                if feed_attrs.send(path.clone()).is_err() {
                    break;
                }
                if let Some(ch) = feed_excludes.as_ref() {
                    if ch.send(path).is_err() {
                        break;
                    }
                }
            }
        });

        let (mut cache, _index) = attributes_cache(&repo)?;
        let mut matches = cache.attribute_matches();
        let mut progress = progress.add_child("validate");
        let mut mismatches = Vec::new();
        let start = std::time::Instant::now();
        progress.init(
            num_entries.map(|n| n + if ignore { n } else { 0 }),
            gix::progress::count("paths"),
        );

        for (rela_path, baseline) in rx_base {
            let entry = cache.at_entry(rela_path.as_str(), None)?;
            match baseline {
                Baseline::Attribute { assignments: expected } => {
                    entry.matching_attributes(&mut matches);
                    let fast_path_mismatch = matches
                        .iter()
                        .map(|m| m.assignment)
                        .zip(expected.iter().map(Assignment::as_ref))
                        .any(|(a, b)| a != b);
                    if fast_path_mismatch {
                        let actual_set = BTreeSet::from_iter(matches.iter().map(|m| m.assignment));
                        let expected_set = BTreeSet::from_iter(expected.iter().map(Assignment::as_ref));
                        let too_few_or_too_many =
                            !(expected_set.sub(&actual_set).is_empty() && actual_set.sub(&expected_set).is_empty());
                        if too_few_or_too_many {
                            mismatches.push((
                                rela_path,
                                Mismatch::Attributes {
                                    actual: matches.iter().map(|m| m.assignment.to_owned()).collect(),
                                    expected,
                                },
                            ));
                        }
                    }
                }
                Baseline::Exclude { location } => {
                    let match_ = entry.matching_exclude_pattern();
                    if match_.is_some() != location.is_some() {
                        mismatches.push((
                            rela_path,
                            Mismatch::Exclude {
                                actual: match_.map(Into::into),
                                expected: location,
                            },
                        ));
                    }
                }
            }
            progress.inc();
        }

        if let Some(stats) = statistics.then(|| cache.take_statistics()) {
            out.flush()?;
            writeln!(err, "{stats:#?}").ok();
        }
        progress.show_throughput(start);

        if mismatches.is_empty() {
            Ok(())
        } else {
            for (rela_path, mm) in &mismatches {
                writeln!(err, "{rela_path}: {mm:#?}").ok();
            }
            bail!(
                "{}: Validation failed with {} mismatches out of {}",
                gix::path::realpath(repo.work_dir().unwrap_or(repo.git_dir()))?.display(),
                mismatches.len(),
                progress.counter().load(Ordering::Relaxed)
            );
        }
    }

    enum Baseline {
        Attribute { assignments: Vec<gix::attrs::Assignment> },
        Exclude { location: Option<ExcludeLocation> },
    }

    #[derive(Debug)]
    // See note on `Mismatch`
    #[allow(dead_code)]
    pub struct ExcludeLocation {
        pub line: usize,
        pub rela_source_file: String,
        pub pattern: String,
    }

    #[derive(Debug)]
    // We debug-print this structure, which makes all fields 'used', but it doesn't count.
    // TODO: find a way to not have to do more work, but make the warning go away.
    #[allow(dead_code)]
    pub enum Mismatch {
        Attributes {
            actual: Vec<gix::attrs::Assignment>,
            expected: Vec<gix::attrs::Assignment>,
        },
        Exclude {
            actual: Option<ExcludeMatch>,
            expected: Option<ExcludeLocation>,
        },
    }

    #[derive(Debug)]
    // See note on `Mismatch`
    #[allow(dead_code)]
    pub struct ExcludeMatch {
        pub pattern: gix::glob::Pattern,
        pub source: Option<PathBuf>,
        pub sequence_number: usize,
    }

    impl From<gix::ignore::search::Match<'_>> for ExcludeMatch {
        fn from(value: gix::ignore::search::Match<'_>) -> Self {
            ExcludeMatch {
                pattern: value.pattern.clone(),
                source: value.source.map(ToOwned::to_owned),
                sequence_number: value.sequence_number,
            }
        }
    }

    fn parse_exclude(line: &str) -> Option<(String, Baseline)> {
        let (left, value) = line.split_at(line.find('\t')?);
        let value = &value[1..];

        let location = if left == "::" {
            None
        } else {
            let mut tokens = left.split(':');
            let source = tokens.next()?;
            let line_number: usize = tokens.next()?.parse().ok()?;
            let pattern = tokens.next()?;
            Some(ExcludeLocation {
                line: line_number,
                rela_source_file: source.into(),
                pattern: pattern.into(),
            })
        };
        Some((value.to_string(), Baseline::Exclude { location }))
    }

    fn parse_attributes(lines: &mut Peekable<impl Iterator<Item = String>>) -> Option<(String, Baseline)> {
        let first = lines.next()?;
        let mut out = Vec::new();
        let (path, assignment) = parse_attribute_line(&first)?;

        let current = path.to_owned();
        out.push(assignment.to_owned());
        loop {
            let next_line = match lines.peek() {
                None => break,
                Some(l) => l,
            };
            let (next_path, next_assignment) = parse_attribute_line(next_line)?;
            if next_path != current {
                return Some((current, Baseline::Attribute { assignments: out }));
            } else {
                out.push(next_assignment.to_owned());
                lines.next();
            }
        }
        Some((current, Baseline::Attribute { assignments: out }))
    }

    fn parse_attribute_line(line: &str) -> Option<(&str, gix::attrs::AssignmentRef<'_>)> {
        use gix::{attrs::StateRef, bstr::ByteSlice};

        let mut prev = None;
        let mut tokens = line.splitn(3, |b| {
            let is_match = b == ' ' && prev.take() == Some(':');
            prev = Some(b);
            is_match
        });
        if let Some(((mut path, attr), info)) = tokens.next().zip(tokens.next()).zip(tokens.next()) {
            let state = match info {
                "set" => StateRef::Set,
                "unset" => StateRef::Unset,
                "unspecified" => StateRef::Unspecified,
                _ => StateRef::from_bytes(info.as_bytes()),
            };
            path = path.trim_end_matches(':');
            let attr = attr.trim_end_matches(':');
            let assignment = gix::attrs::AssignmentRef {
                name: gix::attrs::NameRef::try_from(attr.as_bytes().as_bstr()).ok()?,
                state,
            };
            Some((path, assignment))
        } else {
            None
        }
    }
}