rargo 0.7.10

The rargo client: syncs a worktree and its environment to a rargo build server and brings back the results
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
463
464
465
466
use super::index::Entry;
use super::{Kind, Source};
use std::collections::BTreeSet;
use std::path::Path;

#[derive(Debug, PartialEq, Eq)]
enum Wild {
    Match,
    NoMatch,
    AbortAll,
    AbortToStarStar,
}

fn special(c: u8) -> bool {
    matches!(c, b'*' | b'?' | b'[' | b'\\')
}

fn class(name: &[u8], c: u8) -> Option<bool> {
    Some(match name {
        b"alnum" => c.is_ascii_alphanumeric(),
        b"alpha" => c.is_ascii_alphabetic(),
        b"blank" => c == b' ' || c == b'\t',
        b"cntrl" => c.is_ascii_control(),
        b"digit" => c.is_ascii_digit(),
        b"graph" => c.is_ascii_graphic(),
        b"lower" => c.is_ascii_lowercase(),
        b"print" => c.is_ascii_graphic() || c == b' ',
        b"punct" => c.is_ascii_punctuation(),
        b"space" => c.is_ascii_whitespace() || c == 0x0b,
        b"upper" => c.is_ascii_uppercase(),
        b"xdigit" => c.is_ascii_hexdigit(),
        _ => return None,
    })
}

fn dowild(p: &[u8], text: &[u8], pathname: bool) -> Wild {
    let at = |s: &[u8], i: usize| s.get(i).copied().unwrap_or(0);
    let (mut pi, mut ti) = (0usize, 0usize);
    while pi < p.len() {
        let mut p_ch = p[pi];
        let t_ch = at(text, ti);
        if t_ch == 0 && p_ch != b'*' {
            return Wild::AbortAll;
        }
        match p_ch {
            b'?' => {
                if pathname && t_ch == b'/' {
                    return Wild::NoMatch;
                }
            }
            b'*' => {
                let match_slash;
                pi += 1;
                if at(p, pi) == b'*' {
                    let prev = pi.checked_sub(2);
                    while at(p, pi) == b'*' {
                        pi += 1;
                    }
                    let next = at(p, pi);
                    if (prev.is_none() || p[prev.unwrap()] == b'/') && (next == 0 || next == b'/' || (next == b'\\' && at(p, pi + 1) == b'/')) {
                        if next == b'/' && dowild(&p[pi + 1..], &text[ti..], pathname) == Wild::Match {
                            return Wild::Match;
                        }
                        match_slash = true;
                    } else {
                        match_slash = false;
                    }
                } else {
                    match_slash = !pathname;
                }
                if pi >= p.len() {
                    if !match_slash && text[ti..].contains(&b'/') {
                        return Wild::NoMatch;
                    }
                    return Wild::Match;
                } else if !match_slash && p[pi] == b'/' {
                    let Some(slash) = text[ti..].iter().position(|&c| c == b'/') else { return Wild::NoMatch };
                    ti += slash;
                    pi += 1;
                    ti += 1;
                    continue;
                }
                let mut t = ti;
                loop {
                    let mut t_ch = at(text, t);
                    if t_ch == 0 {
                        break;
                    }
                    if !special(p[pi]) {
                        let lit = p[pi];
                        while t < text.len() && (match_slash || text[t] != b'/') {
                            if text[t] == lit {
                                break;
                            }
                            t += 1;
                        }
                        t_ch = at(text, t);
                        if t_ch != lit {
                            if match_slash {
                                return Wild::AbortAll;
                            }
                            break;
                        }
                    }
                    let matched = dowild(&p[pi..], &text[t..], pathname);
                    if matched != Wild::NoMatch {
                        if !match_slash || matched != Wild::AbortToStarStar {
                            return matched;
                        }
                    } else if !match_slash && t_ch == b'/' {
                        return Wild::AbortToStarStar;
                    }
                    t += 1;
                }
                return Wild::AbortAll;
            }
            b'[' => {
                pi += 1;
                p_ch = at(p, pi);
                if p_ch == b'^' {
                    p_ch = b'!';
                }
                let negated = p_ch == b'!';
                if negated {
                    pi += 1;
                    p_ch = at(p, pi);
                }
                let mut prev_ch = 0u8;
                let mut matched = false;
                loop {
                    if p_ch == 0 {
                        return Wild::AbortAll;
                    }
                    if p_ch == b'\\' {
                        pi += 1;
                        p_ch = at(p, pi);
                        if p_ch == 0 {
                            return Wild::AbortAll;
                        }
                        if t_ch == p_ch {
                            matched = true;
                        }
                    } else if p_ch == b'-' && prev_ch != 0 && at(p, pi + 1) != 0 && at(p, pi + 1) != b']' {
                        pi += 1;
                        p_ch = at(p, pi);
                        if p_ch == b'\\' {
                            pi += 1;
                            p_ch = at(p, pi);
                            if p_ch == 0 {
                                return Wild::AbortAll;
                            }
                        }
                        if t_ch <= p_ch && t_ch >= prev_ch {
                            matched = true;
                        }
                        p_ch = 0;
                    } else if p_ch == b'[' && at(p, pi + 1) == b':' {
                        let s = pi + 2;
                        let mut q = s;
                        while at(p, q) != 0 && at(p, q) != b']' {
                            q += 1;
                        }
                        if at(p, q) == 0 {
                            return Wild::AbortAll;
                        }
                        if q < s + 1 || p[q - 1] != b':' {
                            pi = s - 2;
                            p_ch = b'[';
                            if t_ch == p_ch {
                                matched = true;
                            }
                            prev_ch = p_ch;
                            pi += 1;
                            p_ch = at(p, pi);
                            if p_ch == b']' {
                                break;
                            }
                            continue;
                        }
                        pi = q;
                        match class(&p[s..q - 1], t_ch) {
                            Some(true) => matched = true,
                            Some(false) => {}
                            None => return Wild::AbortAll,
                        }
                        p_ch = 0;
                    } else if t_ch == p_ch {
                        matched = true;
                    }
                    prev_ch = p_ch;
                    pi += 1;
                    p_ch = at(p, pi);
                    if p_ch == b']' {
                        break;
                    }
                }
                if matched == negated || (pathname && t_ch == b'/') {
                    return Wild::NoMatch;
                }
            }
            _ => {
                if p_ch == b'\\' {
                    pi += 1;
                    p_ch = at(p, pi);
                }
                if t_ch != p_ch {
                    return Wild::NoMatch;
                }
            }
        }
        pi += 1;
        ti += 1;
    }
    if ti < text.len() { Wild::NoMatch } else { Wild::Match }
}

pub fn wildmatch(pattern: &[u8], text: &[u8], pathname: bool) -> bool {
    dowild(pattern, text, pathname) == Wild::Match
}

#[derive(Clone, Debug)]
pub struct Pattern {
    pattern: Vec<u8>,
    base: Vec<u8>,
    negative: bool,
    must_be_dir: bool,
    no_dir: bool,
}

fn trim_trailing_spaces(line: &[u8]) -> &[u8] {
    let mut last_space = None;
    let mut i = 0;
    while i < line.len() {
        match line[i] {
            b' ' => {
                if last_space.is_none() {
                    last_space = Some(i);
                }
            }
            b'\\' => {
                i += 1;
                if i >= line.len() {
                    return line;
                }
                last_space = None;
            }
            _ => last_space = None,
        }
        i += 1;
    }
    &line[..last_space.unwrap_or(line.len())]
}

pub fn patterns(bytes: &[u8], base: &[u8]) -> Vec<Pattern> {
    let bytes = bytes.strip_prefix(b"\xef\xbb\xbf").unwrap_or(bytes);
    let mut out = Vec::new();
    for line in bytes.split(|&b| b == b'\n') {
        if line.is_empty() || line[0] == b'#' {
            continue;
        }
        let line = line.strip_suffix(b"\r").unwrap_or(line);
        let mut p = trim_trailing_spaces(line);
        let negative = p.first() == Some(&b'!');
        if negative {
            p = &p[1..];
        }
        let must_be_dir = p.last() == Some(&b'/');
        if must_be_dir {
            p = &p[..p.len() - 1];
        }
        let no_dir = !p.contains(&b'/');
        out.push(Pattern { pattern: p.to_vec(), base: base.to_vec(), negative, must_be_dir, no_dir });
    }
    out
}

impl Pattern {
    fn matches(&self, path: &[u8], is_dir: bool) -> bool {
        if self.must_be_dir && !is_dir {
            return false;
        }
        if self.no_dir {
            let name = path.rsplit(|&b| b == b'/').next().unwrap_or(path);
            return wildmatch(&self.pattern, name, false);
        }
        let pattern = self.pattern.strip_prefix(b"/").unwrap_or(&self.pattern);
        let name = if self.base.is_empty() {
            path
        } else {
            match path.strip_prefix(self.base.as_slice()) {
                Some(rest) => rest,
                None => return false,
            }
        };
        wildmatch(pattern, name, true)
    }
}

pub fn excluded(lists: &[Vec<Pattern>], path: &[u8], is_dir: bool) -> bool {
    lists.iter().rev().flat_map(|l| l.iter().rev()).find(|p| p.matches(path, is_dir)).is_some_and(|p| !p.negative)
}

struct Walk<'a, S> {
    src: &'a S,
    root: &'a Path,
    tracked: BTreeSet<&'a [u8]>,
    tracked_dirs: BTreeSet<&'a [u8]>,
    out: Vec<Vec<u8>>,
}

fn join(dir: &[u8], name: &[u8]) -> Vec<u8> {
    if dir.is_empty() { name.to_vec() } else { [dir, b"/", name].concat() }
}

impl<S: Source> Walk<'_, S> {
    fn at(&self, rel: &[u8]) -> std::path::PathBuf {
        use std::os::unix::ffi::OsStrExt;
        if rel.is_empty() { self.root.to_path_buf() } else { self.root.join(std::ffi::OsStr::from_bytes(rel)) }
    }

    fn nested_repo(&self, rel: &[u8]) -> bool {
        let dot_git = self.at(&join(rel, b".git"));
        self.src.read(&dot_git.join("HEAD")).is_some()
            || self.src.read(&dot_git).is_some_and(|b| b.as_ref().starts_with(b"gitdir:"))
    }

    fn visit(&mut self, dir: &[u8], lists: &mut Vec<Vec<Pattern>>, parent_excluded: bool) -> bool {
        let mut entries = self.src.list(&self.at(dir)).unwrap_or_default();
        entries.retain(|(n, _)| n != b".git");
        entries.sort();
        let loaded = !parent_excluded && entries.iter().any(|(n, k)| n == b".gitignore" && *k == Kind::File);
        if loaded {
            let base = if dir.is_empty() { Vec::new() } else { [dir, b"/"].concat() };
            let text = self.src.read(&self.at(&join(dir, b".gitignore"))).map(|b| b.as_ref().to_vec()).unwrap_or_default();
            lists.push(patterns(&text, &base));
        }
        let mut all = !entries.is_empty();
        for (name, kind) in entries {
            let path = join(dir, &name);
            if kind == Kind::Dir {
                if self.tracked.contains(path.as_slice()) {
                    all = false;
                    continue;
                }
                if self.tracked_dirs.contains(path.as_slice()) {
                    let excluded_here = parent_excluded || excluded(lists, &path, true);
                    self.visit(&path, lists, excluded_here);
                    all = false;
                    continue;
                }
                if parent_excluded || excluded(lists, &path, true) {
                    self.out.push([path.as_slice(), b"/"].concat());
                    continue;
                }
                if self.nested_repo(&path) {
                    all = false;
                    continue;
                }
                if self.visit(&path, lists, false) {
                    self.out.push([path.as_slice(), b"/"].concat());
                } else {
                    all = false;
                }
            } else if self.tracked.contains(path.as_slice()) {
                all = false;
            } else if parent_excluded || excluded(lists, &path, false) {
                self.out.push(path);
            } else {
                all = false;
            }
        }
        if loaded {
            lists.pop();
        }
        all
    }
}

pub fn ignored<S: Source>(src: &S, root: &Path, index: &[Entry], base_lists: Vec<Vec<Pattern>>) -> Vec<Vec<u8>> {
    let mut tracked = BTreeSet::new();
    let mut tracked_dirs = BTreeSet::new();
    for e in index {
        let path = e.path.strip_suffix(b"/").unwrap_or(&e.path);
        tracked.insert(path);
        let mut at = path;
        while let Some(i) = at.iter().rposition(|&b| b == b'/') {
            at = &at[..i];
            if !tracked_dirs.insert(at) {
                break;
            }
        }
    }
    let mut walk = Walk { src, root, tracked, tracked_dirs, out: Vec::new() };
    let mut lists = base_lists;
    walk.visit(b"", &mut lists, false);
    walk.out.sort();
    walk.out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn wildmatch_agrees_with_gits_own_cases() {
        for (expect, text, pattern, pathname) in [
            (true, "foo", "foo", true),
            (false, "bar", "foo", true),
            (true, "foo", "???", true),
            (true, "foo", "*f*", true),
            (true, "foo/bar", "foo/*", true),
            (false, "foo/bar/baz", "foo/*", true),
            (true, "foo/bar/baz", "foo/*", false),
            (true, "foo/bar/baz", "foo/**", true),
            (true, "foo/bar", "foo/**/bar", true),
            (true, "foo/a/b/bar", "foo/**/bar", true),
            (true, "a/b/deep2/c", "**/deep2/**", true),
            (false, "a/b/deep2", "**/deep2/**", true),
            (true, "deep/x", "**/x", true),
            (true, "x", "**/x", true),
            (false, "foo/bar", "foo?bar", true),
            (true, "foo/bar", "foo?bar", false),
            (true, "a]", "[]a]]", true),
            (true, "-", "[a-]", true),
            (true, "b1.txt", "[a-c]?.txt", true),
            (false, "d2.txt", "[a-c]?.txt", true),
            (true, "x", "[!a-c]", true),
            (true, "x", "[^a-c]", true),
            (false, "b", "[!a-c]", true),
            (true, "a1", "[[:alpha:]][[:digit:]]", true),
            (false, "1a", "[[:alpha:]][[:digit:]]", true),
            (true, "[", "[[]", true),
            (true, "#hash", "\\#hash", true),
            (true, "*", "\\*", true),
            (false, "a", "\\*", true),
            (true, "", "*", true),
            (true, "foo.log", "*.log", true),
            (false, "foo.logx", "*.log", true),
            (true, "XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1", "XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*", true),
            (false, "abc/def", "a*b*c", true),
            (true, "ab/cd", "**/cd", true),
        ] {
            assert_eq!(wildmatch(pattern.as_bytes(), text.as_bytes(), pathname), expect, "{pattern} {text} {pathname}");
        }
    }

    #[test]
    fn pattern_lines_are_parsed_as_git_parses_them() {
        let p = patterns(b"\xef\xbb\xbf#c\n\\#hash\nfoo \\ \nbar  \r\n!keep/\n/top\n\n", b"sub/");
        let shown: Vec<(String, bool, bool, bool)> =
            p.iter().map(|p| (String::from_utf8_lossy(&p.pattern).into_owned(), p.negative, p.must_be_dir, p.no_dir)).collect();
        assert_eq!(
            shown,
            [
                ("\\#hash".into(), false, false, true),
                ("foo \\ ".into(), false, false, true),
                ("bar".into(), false, false, true),
                ("keep".into(), true, true, true),
                ("/top".into(), false, false, false),
            ]
        );
        assert!(p[4].matches(b"sub/top", false) && !p[4].matches(b"top", false) && !p[4].matches(b"sub/x/top", false));
        assert!(p[2].matches(b"a/b/bar", false));
        assert!(p[3].matches(b"x/keep", true) && !p[3].matches(b"x/keep", false));
    }
}