miwa-cli 0.5.0

doc / req / test の3層の仕様乖離(ドリフト)を検知する CLI
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Abstraction over "a snapshot of the repository" (working tree or a git rev).

/// Lists and reads files of a repository snapshot. Paths are repo-relative,
/// `/`-separated, and sorted. Symlinks are not included. Paths under `.git`,
/// `target`, `.miwa`, `.worktrees`, `node_modules`, `obj`, and `dist` are
/// excluded (directory names at any depth; files with those names are not).
/// Further directory names can be excluded via `exclude` in the config, which
/// is applied in `build_graph`.
pub trait Source {
    fn list_files(&self) -> anyhow::Result<Vec<String>>;
    fn read(&self, path: &str) -> anyhow::Result<Vec<u8>>;
}

pub struct FsSource {
    #[allow(dead_code)]
    root: std::path::PathBuf,
}

/// Whether any parent-directory segment of `path` (the file name itself is not
/// considered) equals one of `names`.
pub fn is_under_dir_named<S: AsRef<str>>(path: &str, names: &[S]) -> bool {
    let mut segments = path.split('/').collect::<Vec<_>>();
    segments.pop();
    segments
        .iter()
        .any(|seg| names.iter().any(|n| n.as_ref() == *seg))
}

/// Directory names always excluded from scanning.
pub const DEFAULT_EXCLUDED_DIRS: [&str; 7] = [
    ".git",
    "target",
    ".miwa",
    ".worktrees",
    "node_modules",
    "obj",
    "dist",
];

impl FsSource {
    pub fn new(root: impl Into<std::path::PathBuf>) -> FsSource {
        FsSource { root: root.into() }
    }
}

/// Rejects paths that are empty, absolute, or contain a `..` component.
fn reject_unsafe_path(path: &str) -> anyhow::Result<()> {
    if path.is_empty() {
        anyhow::bail!("empty path is not allowed");
    }
    let p = std::path::Path::new(path);
    if p.is_absolute() {
        anyhow::bail!("absolute path is not allowed: {path}");
    }
    if p.components()
        .any(|c| matches!(c, std::path::Component::ParentDir))
    {
        anyhow::bail!("path must not contain '..': {path}");
    }
    Ok(())
}

impl Source for FsSource {
    fn list_files(&self) -> anyhow::Result<Vec<String>> {
        let root = &self.root;
        let mut files = Vec::new();
        let walker = walkdir::WalkDir::new(root)
            .follow_links(false)
            .into_iter()
            .filter_entry(|entry| {
                if entry.depth() == 0 {
                    return true;
                }
                if entry.file_type().is_dir() {
                    if let Some(name) = entry.file_name().to_str() {
                        if DEFAULT_EXCLUDED_DIRS.contains(&name) {
                            return false;
                        }
                    }
                }
                true
            });
        for entry in walker {
            let entry = entry?;
            if !entry.file_type().is_file() {
                continue;
            }
            let rel = entry.path().strip_prefix(root)?;
            let rel_str = rel
                .components()
                .map(|c| c.as_os_str().to_string_lossy().into_owned())
                .collect::<Vec<_>>()
                .join("/");
            files.push(rel_str);
        }
        files.sort();
        Ok(files)
    }

    fn read(&self, path: &str) -> anyhow::Result<Vec<u8>> {
        reject_unsafe_path(path)?;
        Ok(std::fs::read(self.root.join(path))?)
    }
}

pub struct GitTreeSource {
    repo: std::path::PathBuf,
    rev: String,
    /// The full, unfiltered `path -> blob oid` map for `rev` (no directory
    /// exclusions applied). `list_files` filters this; `read` looks paths up
    /// in it directly, so files under excluded directories (e.g. `.miwa/`)
    /// can still be read even though they are not listed.
    files: std::collections::BTreeMap<String, String>,
}

pub(crate) fn run_git(
    repo: &std::path::Path,
    args: &[&str],
) -> anyhow::Result<std::process::Output> {
    let output = std::process::Command::new("git")
        .args(args)
        .current_dir(repo)
        .output()?;
    Ok(output)
}

impl GitTreeSource {
    /// Validates `rev` against `repo` and eagerly lists every blob in its
    /// tree (unfiltered).
    pub fn new(repo: impl Into<std::path::PathBuf>, rev: &str) -> anyhow::Result<GitTreeSource> {
        let repo = repo.into();
        let output = run_git(
            &repo,
            &["rev-parse", "--verify", &format!("{rev}^{{commit}}")],
        )?;
        if !output.status.success() {
            anyhow::bail!(
                "git rev-parse --verify failed for {rev}: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        let ls_output = run_git(&repo, &["ls-tree", "-r", "-z", "--full-tree", rev])?;
        if !ls_output.status.success() {
            anyhow::bail!(
                "git ls-tree failed: {}",
                String::from_utf8_lossy(&ls_output.stderr)
            );
        }
        let stdout = String::from_utf8_lossy(&ls_output.stdout);
        let mut files = std::collections::BTreeMap::new();
        for entry in stdout.split('\0') {
            if entry.is_empty() {
                continue;
            }
            let Some((meta, path)) = entry.split_once('\t') else {
                continue;
            };
            let mut parts = meta.split_whitespace();
            let mode = parts.next().unwrap_or("");
            let obj_type = parts.next().unwrap_or("");
            let oid = parts.next().unwrap_or("");
            if obj_type != "blob" {
                continue;
            }
            // Mode 120000 is a symlink; `Source` never follows symlinks
            // (matches `FsSource`'s `follow_links(false)`), so exclude them.
            if mode == "120000" {
                continue;
            }
            files.insert(path.to_string(), oid.to_string());
        }

        Ok(GitTreeSource {
            repo,
            rev: rev.to_string(),
            files,
        })
    }
}

impl Source for GitTreeSource {
    fn list_files(&self) -> anyhow::Result<Vec<String>> {
        let mut files: Vec<String> = self
            .files
            .keys()
            .filter(|path| !is_under_dir_named(path, &DEFAULT_EXCLUDED_DIRS))
            .cloned()
            .collect();
        files.sort();
        Ok(files)
    }

    fn read(&self, path: &str) -> anyhow::Result<Vec<u8>> {
        let Some(oid) = self.files.get(path) else {
            return Err(anyhow::Error::new(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("{path} does not exist at {}", self.rev),
            )));
        };
        let output = run_git(&self.repo, &["cat-file", "blob", oid])?;
        if !output.status.success() {
            anyhow::bail!(
                "git cat-file blob failed for {path}: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
        Ok(output.stdout)
    }
}

/// Whether `err` is a `Source::read` failure caused by the path not existing
/// (as opposed to, say, a `git` invocation failure). Callers use this to
/// distinguish "file absent" from a real execution error.
pub fn is_not_found(err: &anyhow::Error) -> bool {
    err.downcast_ref::<std::io::Error>()
        .is_some_and(|e| e.kind() == std::io::ErrorKind::NotFound)
}

/// Returns the first line of `git merge-base <base> HEAD` run in `repo`.
pub fn merge_base(repo: &std::path::Path, base: &str) -> anyhow::Result<String> {
    let output = run_git(repo, &["merge-base", base, "HEAD"])?;
    if !output.status.success() {
        anyhow::bail!(
            "git merge-base failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    let first_line = stdout.lines().next().unwrap_or("").trim().to_string();
    if first_line.is_empty() {
        anyhow::bail!("git merge-base returned no output");
    }
    Ok(first_line)
}

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

    // [FR-018]
    #[test]
    fn 除外ディレクトリ配下のファイルは列挙されない() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir_all(root.join("req")).unwrap();
        fs::write(root.join("req/fr-001.md"), "ok").unwrap();
        for excluded in [
            ".git",
            "target",
            ".miwa",
            ".worktrees",
            "node_modules",
            "obj",
            "dist",
        ] {
            let sub = root.join(excluded).join("nested");
            fs::create_dir_all(&sub).unwrap();
            fs::write(sub.join("file.md"), "should not be listed").unwrap();
        }
        fs::create_dir_all(root.join("proj/obj/Debug")).unwrap();
        fs::write(root.join("proj/obj/Debug/X.cs"), "generated").unwrap();
        fs::create_dir_all(root.join("src/bin")).unwrap();
        fs::write(root.join("src/bin/main.rs"), "kept").unwrap();

        let source = FsSource::new(root);
        let files = source.list_files().unwrap();
        assert!(files.contains(&"req/fr-001.md".to_string()));
        assert!(files.contains(&"src/bin/main.rs".to_string()), "{files:?}");
        assert!(
            !files.contains(&"proj/obj/Debug/X.cs".to_string()),
            "nested obj leaked: {files:?}"
        );
        for excluded in [
            ".git",
            "target",
            ".miwa",
            ".worktrees",
            "node_modules",
            "obj",
            "dist",
        ] {
            assert!(
                !files.iter().any(|f| f.starts_with(&format!("{excluded}/"))),
                "excluded dir {excluded} leaked into: {files:?}"
            );
        }
    }

    // [FR-018]
    #[cfg(unix)]
    #[test]
    fn シンボリックリンクの先は辿らない() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir_all(root.join("req")).unwrap();
        fs::write(root.join("req/fr-001.md"), "ok").unwrap();

        let outside = tempfile::tempdir().unwrap();
        fs::write(outside.path().join("secret.md"), "should not be visible").unwrap();
        std::os::unix::fs::symlink(outside.path(), root.join("req/linked")).unwrap();

        let source = FsSource::new(root);
        let files = source.list_files().unwrap();
        assert!(!files.iter().any(|f| f.contains("secret.md")));
    }

    // [FR-018]
    #[test]
    fn fssourceのreadは絶対パスやドットドットや空パスを拒否する() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::write(root.join("a.txt"), "ok").unwrap();

        let source = FsSource::new(root);
        assert!(source.read("").is_err());
        assert!(source.read("../a.txt").is_err());
        assert!(source.read("sub/../../a.txt").is_err());
        #[cfg(unix)]
        assert!(source.read("/etc/passwd").is_err());
        assert!(source.read("a.txt").is_ok());
    }

    fn git(dir: &std::path::Path, args: &[&str]) -> String {
        let output = std::process::Command::new("git")
            .args(args)
            .current_dir(dir)
            .output()
            .unwrap_or_else(|e| panic!("failed to run git {args:?}: {e}"));
        assert!(
            output.status.success(),
            "git {args:?} failed:\nstdout: {}\nstderr: {}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        String::from_utf8_lossy(&output.stdout).trim().to_string()
    }

    fn init_repo(root: &std::path::Path) {
        git(root, &["init", "-q", "-b", "main"]);
        git(root, &["config", "user.name", "Test User"]);
        git(root, &["config", "user.email", "test@example.com"]);
    }

    // [FR-021]
    #[test]
    fn gittreesourceは除外ディレクトリ配下もreadでは読める() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        init_repo(root);
        fs::create_dir_all(root.join(".miwa")).unwrap();
        fs::write(root.join(".miwa/config.yaml"), "docs:\n  - docs\n").unwrap();
        fs::create_dir_all(root.join("req")).unwrap();
        fs::write(root.join("req/fr1.md"), "content").unwrap();
        git(root, &["add", "-A"]);
        git(root, &["commit", "-q", "-m", "init"]);

        let source = GitTreeSource::new(root, "HEAD").unwrap();
        let files = source.list_files().unwrap();
        assert!(
            !files.iter().any(|f| f.starts_with(".miwa/")),
            ".miwa should be excluded from list_files: {files:?}"
        );
        let content = source.read(".miwa/config.yaml").unwrap();
        assert_eq!(String::from_utf8(content).unwrap(), "docs:\n  - docs\n");
    }

    // [FR-021]
    #[test]
    fn gittreesourceのreadは存在しないパスでnotfoundエラーを返す() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        init_repo(root);
        fs::write(root.join("a.txt"), "hi").unwrap();
        git(root, &["add", "-A"]);
        git(root, &["commit", "-q", "-m", "init"]);

        let source = GitTreeSource::new(root, "HEAD").unwrap();
        let err = source.read("does/not/exist.md").unwrap_err();
        let io_err = err
            .downcast_ref::<std::io::Error>()
            .expect("error should be a std::io::Error");
        assert_eq!(io_err.kind(), std::io::ErrorKind::NotFound);
    }

    // [FR-018]
    #[test]
    fn gittreesourceは除外ディレクトリ配下をlist_filesに含めない() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        init_repo(root);
        fs::create_dir_all(root.join("target/nested")).unwrap();
        fs::write(root.join("target/nested/file.txt"), "x").unwrap();
        fs::create_dir_all(root.join("obj/nested")).unwrap();
        fs::write(root.join("obj/nested/file.txt"), "x").unwrap();
        fs::create_dir_all(root.join("proj/obj/Debug")).unwrap();
        fs::write(root.join("proj/obj/Debug/X.cs"), "x").unwrap();
        fs::create_dir_all(root.join("dist/nested")).unwrap();
        fs::write(root.join("dist/nested/file.txt"), "x").unwrap();
        fs::create_dir_all(root.join("src/bin")).unwrap();
        fs::write(root.join("src/bin/main.rs"), "x").unwrap();
        fs::write(root.join("keep.txt"), "y").unwrap();
        git(root, &["add", "-A"]);
        git(root, &["commit", "-q", "-m", "init"]);

        let source = GitTreeSource::new(root, "HEAD").unwrap();
        let files = source.list_files().unwrap();
        assert!(files.contains(&"keep.txt".to_string()));
        assert!(
            !files.iter().any(|f| f.starts_with("target/")
                || f.starts_with("obj/")
                || f.contains("/obj/")
                || f.starts_with("dist/")),
            "target/ obj/ dist/ should be excluded: {files:?}"
        );
        assert!(files.contains(&"src/bin/main.rs".to_string()), "{files:?}");
    }

    // [FR-021]
    #[test]
    fn gittreesourceのreadはコミット時点の内容を返す() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        init_repo(root);
        fs::write(root.join("a.txt"), "v1").unwrap();
        git(root, &["add", "-A"]);
        git(root, &["commit", "-q", "-m", "v1"]);
        let rev = git(root, &["rev-parse", "HEAD"]);

        fs::write(root.join("a.txt"), "v2").unwrap();
        git(root, &["add", "-A"]);
        git(root, &["commit", "-q", "-m", "v2"]);

        let source = GitTreeSource::new(root, &rev).unwrap();
        let content = source.read("a.txt").unwrap();
        assert_eq!(String::from_utf8(content).unwrap(), "v1");
    }

    // [FR-018]
    #[cfg(unix)]
    #[test]
    fn gittreesourceはシンボリックリンクをlist_filesに含めない() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        init_repo(root);
        fs::write(root.join("real.txt"), "content").unwrap();
        std::os::unix::fs::symlink("real.txt", root.join("linked.txt")).unwrap();
        git(root, &["add", "-A"]);
        git(root, &["commit", "-q", "-m", "init"]);

        let source = GitTreeSource::new(root, "HEAD").unwrap();
        let files = source.list_files().unwrap();
        assert!(files.contains(&"real.txt".to_string()));
        assert!(
            !files.contains(&"linked.txt".to_string()),
            "symlinks should be excluded: {files:?}"
        );
    }

    // [FR-018]
    #[test]
    fn 既定除外名と同名のファイルはfssourceで除外されない() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        fs::create_dir_all(root.join("tests")).unwrap();
        fs::write(root.join("tests/dist"), "file named dist").unwrap();
        fs::create_dir_all(root.join("dist")).unwrap();
        fs::write(root.join("dist/x.txt"), "excluded").unwrap();

        let files = FsSource::new(root).list_files().unwrap();
        assert!(files.contains(&"tests/dist".to_string()), "{files:?}");
        assert!(!files.contains(&"dist/x.txt".to_string()), "{files:?}");
    }

    // [FR-018]
    #[test]
    fn 既定除外名と同名のファイルはgittreesourceで除外されない() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        init_repo(root);
        fs::create_dir_all(root.join("tests")).unwrap();
        fs::write(root.join("tests/dist"), "file named dist").unwrap();
        fs::create_dir_all(root.join("dist")).unwrap();
        fs::write(root.join("dist/x.txt"), "excluded").unwrap();
        git(root, &["add", "-A"]);
        git(root, &["commit", "-q", "-m", "init"]);

        let files = GitTreeSource::new(root, "HEAD")
            .unwrap()
            .list_files()
            .unwrap();
        assert!(files.contains(&"tests/dist".to_string()), "{files:?}");
        assert!(!files.contains(&"dist/x.txt".to_string()), "{files:?}");
    }
}