Skip to main content

blyte_cli/
comments_cmd.rs

1//! Implementation of the `blyte comments` command.
2
3use crate::comment_lint::{added_lines, comments_in, is_supported, Comment};
4use std::collections::{BTreeMap, BTreeSet};
5use std::path::Path;
6use std::process::Command;
7
8/// Checks the working tree in `dir` against `base` for added comments.
9pub fn run(base: &str, dir: &Path) -> i32 {
10    let diff_out = match Command::new("git")
11        .args(["diff", "--no-color", "--no-ext-diff", "--unified=0", base])
12        .current_dir(dir)
13        .output()
14    {
15        Ok(o) => o,
16        Err(e) => {
17            eprintln!("blyte comments: git diff failed: {e}");
18            return 2;
19        }
20    };
21
22    if !diff_out.status.success() {
23        let stderr = String::from_utf8_lossy(&diff_out.stderr);
24        let first_line = stderr.lines().next().unwrap_or("").trim();
25        eprintln!("blyte comments: git diff failed: {first_line}");
26        return 2;
27    }
28
29    let diff_text = match String::from_utf8(diff_out.stdout) {
30        Ok(s) => s,
31        Err(e) => {
32            eprintln!("blyte comments: git diff failed: {e}");
33            return 2;
34        }
35    };
36
37    let ls_out = match Command::new("git")
38        .args(["ls-files", "--others", "--exclude-standard"])
39        .current_dir(dir)
40        .output()
41    {
42        Ok(o) => o,
43        Err(e) => {
44            eprintln!("blyte comments: git ls-files failed: {e}");
45            return 2;
46        }
47    };
48
49    if !ls_out.status.success() {
50        let stderr = String::from_utf8_lossy(&ls_out.stderr);
51        let first_line = stderr.lines().next().unwrap_or("").trim();
52        eprintln!("blyte comments: git ls-files failed: {first_line}");
53        return 2;
54    }
55
56    let ls_text = match String::from_utf8(ls_out.stdout) {
57        Ok(s) => s,
58        Err(e) => {
59            eprintln!("blyte comments: git ls-files failed: {e}");
60            return 2;
61        }
62    };
63
64    let mut tracked_added: BTreeMap<String, Option<BTreeSet<usize>>> = added_lines(&diff_text)
65        .into_iter()
66        .map(|(path, lines)| (path, Some(lines)))
67        .collect();
68
69    for raw in ls_text.lines() {
70        let path = raw.trim();
71        if !path.is_empty() {
72            tracked_added.insert(path.to_string(), None);
73        }
74    }
75
76    let mut found: Vec<Comment> = Vec::new();
77    for (path, lines_opt) in tracked_added {
78        if !is_supported(&path) {
79            continue;
80        }
81        let source = match std::fs::read_to_string(dir.join(&path)) {
82            Ok(s) => s,
83            Err(e) => {
84                eprintln!("blyte comments: cannot read {path}: {e}");
85                return 2;
86            }
87        };
88        for comment in comments_in(&path, &source) {
89            let is_added = match &lines_opt {
90                Some(lines) => lines.contains(&comment.line),
91                None => true,
92            };
93            if is_added {
94                found.push(comment);
95            }
96        }
97    }
98
99    if found.is_empty() {
100        return 0;
101    }
102
103    println!(
104        "comments added ({}). Remove each one. If it explained an assumption, a shortcut or a deviation from the task, fix that or say it in your final report.",
105        found.len()
106    );
107    for c in &found {
108        println!("{}:{}: {}", c.file, c.line, c.text);
109    }
110    println!(
111        "Not counted: documentation comments (///, //!, /** */) and tool directives (noqa, eslint-disable, @ts-expect-error, SAFETY:)."
112    );
113
114    1
115}