ocomment 0.1.0

Fast, byte-preserving comment checker and remover
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use crate::config::ResolvedConfig;
use anyhow::{Context, Result, anyhow};
use globset::{Glob, GlobSet, GlobSetBuilder};
use ignore::WalkBuilder;
use ocomment_core::{
    DeclarativeProfile, Detection, Dialect, Language, TransformOptions, detect_language,
};
use std::{
    env, fs,
    path::{Path, PathBuf},
};

#[derive(Clone, Debug)]
pub struct SourceFile {
    pub path: PathBuf,
    pub source: Vec<u8>,
    pub language: Language,
    pub dialect: Dialect,
    /// The path-resolved policy and layout used for this source. Resolution is
    /// part of discovery so the parallel scan does not repeat it.
    pub options: TransformOptions,
    pub profile: Option<DeclarativeProfile>,
    pub plugin: Option<String>,
}

#[derive(Clone, Debug)]
pub struct SkippedFile {
    pub path: PathBuf,
    pub reason: String,
    pub error: bool,
    /// The path itself was named on the command line. Such a skip is always
    /// reported on its own line; a skip found while walking a directory is
    /// folded into the end-of-run summary instead.
    pub explicit: bool,
}

#[derive(Default)]
pub struct Discovery {
    pub files: Vec<SourceFile>,
    pub skipped: Vec<SkippedFile>,
    /// A configuration resolution failure applies to the run rather than to
    /// one unreadable path. It is carried out of the walk and returned after
    /// traversal unwinds, instead of being reported as an I/O skip.
    fatal: Option<anyhow::Error>,
}

/// The path standard input is reported under. It is not a real file name: the
/// renderers print it, and the configuration override matcher sees it, exactly
/// as it reads here.
pub const STDIN_PATH: &str = "<stdin>";

/// What both `strip` and a `-` target say when the bytes carry no signature to
/// detect a language from. Standard input has no name to fall back on, so the
/// only way forward is for the caller to name the language.
pub const STDIN_LANGUAGE_HELP: &str = "cannot detect the language of standard input; \
pass --language <LANGUAGE> (see `ocomment languages`)";

/// Why a file OComment has no scanner for is passed over, and the two ways out
/// of it: consult the list of what is built in, or name a language anyway.
///
/// The end-of-run summary must not repeat this sentence once per file, so it
/// folds the reason onto a short key of its own; `output::skip_label` is what
/// ties the two together.
pub const NO_LANGUAGE: &str =
    "no built-in language for this file (see `ocomment languages`; use --language to force)";

/// Why a named path was not found. A relative path is resolved against the
/// working directory, which is exactly what a caller who typed it from the
/// wrong place cannot see, so the directory that was searched is named.
fn missing_path_reason() -> String {
    env::current_dir().map_or_else(
        |_| "path does not exist".to_owned(),
        |cwd| {
            format!(
                "path does not exist (checked relative to {})",
                cwd.display()
            )
        },
    )
}

/// Turn the bytes read from standard input into a source file the ordinary
/// pipeline can process, or the skip that says why it cannot. Detection has no
/// path to work with, so it is driven by `--language` or by the contents.
///
/// Declarative profiles and plugins route on a file extension, which standard
/// input does not have; a pipe is therefore always handled by a built-in
/// language or not at all.
pub fn stdin_source(
    bytes: Vec<u8>,
    resolved: &ResolvedConfig,
    forced_language: Option<Language>,
    forced_dialect: Option<Dialect>,
) -> Result<SourceFile, SkippedFile> {
    let skipped = |reason: &str, error: bool| SkippedFile {
        path: PathBuf::from(STDIN_PATH),
        reason: reason.to_owned(),
        error,
        /* NOTE: Standard input was named on the command line, so its skip is always
         * reported on its own line rather than folded into the summary. */
        explicit: true,
    };
    if bytes.iter().take(8192).any(|byte| *byte == 0) {
        return Err(skipped("binary file (NUL byte)", false));
    }
    let detection = forced_language
        .map(|language| Detection {
            language,
            dialect: forced_dialect.unwrap_or(Dialect::Standard),
            reason: "command-line",
        })
        .or_else(|| detect_language(None, &bytes));
    let Some(Detection {
        language, dialect, ..
    }) = detection
    else {
        return Err(skipped(STDIN_LANGUAGE_HELP, true));
    };
    let (language, options) = resolved
        .for_path(
            Path::new(STDIN_PATH),
            language,
            forced_dialect.unwrap_or(dialect),
        )
        .map_err(|error| skipped(&error.to_string(), true))?;
    if forced_language.is_none() && !resolved.language_is_enabled(language) {
        return Err(skipped("language disabled by configuration", false));
    }
    Ok(SourceFile {
        path: PathBuf::from(STDIN_PATH),
        source: bytes,
        language,
        dialect: options.scan.dialect,
        options,
        profile: None,
        plugin: None,
    })
}

/// What a command with no PATH walks.
///
/// The project root is where the configuration was found, not what the caller
/// is looking at: a command run from a subdirectory checks that subdirectory,
/// the way every other file-walking developer tool does. Reaching back up to
/// the root would put files the caller cannot see — and, with `fix`, files
/// they did not mean to rewrite — into the run.
pub const DEFAULT_TARGET: &str = ".";

/// The one name a walk never offers, whatever else was asked for.
///
/// `.git` is git's own storage rather than source, and `git` itself never
/// treats it as a candidate for anything. Neither may a tool that rewrites
/// files in place: `ocomment fix .` in a fresh repository would otherwise
/// rewrite every sample hook git had just written into `.git/hooks`. Naming
/// a directory lifts the hidden-file rule and so does `files.hidden`, so the
/// exclusion cannot hang off either of them.
///
/// A submodule or a linked worktree keeps its `.git` as a *file* pointing at
/// the storage instead of holding it, which is why the name is matched rather
/// than the file type.
const GIT_DIRECTORY: &str = ".git";

pub fn discover(
    paths: &[PathBuf],
    resolved: &ResolvedConfig,
    forced_language: Option<Language>,
    forced_dialect: Option<Dialect>,
) -> Result<Discovery> {
    let implicit = [PathBuf::from(DEFAULT_TARGET)];
    /* NOTE: The substituted target stands in for an argument nobody typed, so it is
     * walked with the ordinary limits: only a path the caller actually named
     * is a request to look past the hidden-file and size rules. */
    let (paths, explicit) = if paths.is_empty() {
        (&implicit[..], false)
    } else {
        (paths, true)
    };
    discover_with_scope(paths, resolved, forced_language, forced_dialect, explicit)
}

/// Discover workspace roots with normal traversal limits. Unlike explicit CLI
/// paths, an LSP workspace folder must still honor hidden-file and size rules.
pub fn discover_workspace(paths: &[PathBuf], resolved: &ResolvedConfig) -> Result<Discovery> {
    discover_with_scope(paths, resolved, None, None, false)
}

fn discover_with_scope(
    paths: &[PathBuf],
    resolved: &ResolvedConfig,
    forced_language: Option<Language>,
    forced_dialect: Option<Dialect>,
    explicit_arguments: bool,
) -> Result<Discovery> {
    let include = compile_globs(&resolved.config.files.include)?;
    let exclude = compile_globs(&resolved.config.files.exclude)?;
    let mut discovery = Discovery::default();
    /* NOTE: Only an editor asking for its workspace arrives here without a target;
     * `discover` gives a command line the current directory instead. */
    let targets: Vec<_> = if paths.is_empty() {
        vec![(resolved.root.clone(), false)]
    } else {
        paths
            .iter()
            .cloned()
            .map(|path| (path, explicit_arguments))
            .collect()
    };
    for (path, explicit_scope) in targets {
        if path.is_file()
            || path
                .symlink_metadata()
                .is_ok_and(|metadata| metadata.file_type().is_symlink())
        {
            load_one(
                &path,
                explicit_scope,
                explicit_scope,
                resolved,
                forced_language,
                forced_dialect,
                &include,
                &exclude,
                &mut discovery,
            );
        } else if path.is_dir() {
            let mut builder = WalkBuilder::new(&path);
            let ignore = resolved.config.files.ignore;
            builder
                .follow_links(resolved.config.files.follow_symlinks)
                .standard_filters(ignore)
                /* NOTE: `standard_filters` also resets the hidden-file flag, so this
                 * must come afterwards for explicitly named directories. */
                .hidden(!explicit_scope && !resolved.config.files.hidden)
                .git_ignore(ignore)
                .git_global(ignore)
                .git_exclude(ignore)
                .ignore(ignore)
                .parents(ignore);
            if ignore {
                builder.add_custom_ignore_filename(".ocommentignore");
            }
            /* NOTE: The filter is never asked about the walk root, so a caller who
             * names a path inside `.git` — or `.git` itself — is still
             * answered; only what a walk *wanders* into is excluded. */
            builder.filter_entry(|entry| entry.file_name() != GIT_DIRECTORY);
            for entry in builder.build() {
                match entry {
                    Ok(entry) if entry.file_type().is_some_and(|kind| kind.is_file()) => {
                        load_one(
                            entry.path(),
                            explicit_scope,
                            false,
                            resolved,
                            forced_language,
                            forced_dialect,
                            &include,
                            &exclude,
                            &mut discovery,
                        );
                    }
                    Ok(_) => {}
                    Err(error) => discovery.skipped.push(SkippedFile {
                        path: path.clone(),
                        reason: error.to_string(),
                        error: true,
                        explicit: explicit_scope,
                    }),
                }
            }
        } else {
            discovery.skipped.push(SkippedFile {
                path,
                reason: missing_path_reason(),
                error: true,
                explicit: explicit_scope,
            });
        }
    }
    if let Some(error) = discovery.fatal.take() {
        return Err(error);
    }
    discovery
        .files
        .sort_by(|left, right| left.path.cmp(&right.path));
    discovery
        .files
        .dedup_by(|left, right| left.path == right.path);
    /* INVARIANT: A path is reached twice whenever it is named beside a directory holding
     * it, and it is one file either way: `files` says so with the sort and the
     * dedup above, and a skip is one file just as much — a report that
     * annotates the same path twice reads as two problems with it. Which of
     * the two entries survives is not arbitrary. An error decides the exit
     * code, and a path the caller actually typed is answered on a line of its
     * own rather than folded into the summary, so the entry that says the most
     * is sorted to the front of its path and is the one the dedup keeps. */
    discovery.skipped.sort_by(|left, right| {
        left.path
            .cmp(&right.path)
            .then(right.error.cmp(&left.error))
            .then(right.explicit.cmp(&left.explicit))
    });
    discovery
        .skipped
        .dedup_by(|left, right| left.path == right.path);
    Ok(discovery)
}

/// The name a walked file is reported under.
///
/// The implicit target is `.`, so a walk rooted there hands back every entry
/// as `./name`. `ocomment` and `ocomment check name` report one file, and a
/// reader — or a `git apply` reading the patch — is owed one spelling of it,
/// so the prefix the walk root contributed is dropped. The target itself is
/// left alone: `.` names a directory, and `` names nothing.
fn reported_path(path: &Path) -> PathBuf {
    match path.strip_prefix(DEFAULT_TARGET) {
        Ok(stripped) if !stripped.as_os_str().is_empty() => stripped.to_path_buf(),
        _ => path.to_path_buf(),
    }
}

#[allow(clippy::too_many_arguments)]
fn load_one(
    path: &Path,
    explicit_scope: bool,
    explicit_path: bool,
    resolved: &ResolvedConfig,
    forced_language: Option<Language>,
    forced_dialect: Option<Dialect>,
    include: &GlobSet,
    exclude: &GlobSet,
    discovery: &mut Discovery,
) {
    let path = &reported_path(path);
    /* NOTE: The globs are written relative to the root; the path was typed — or
     * walked — relative to the working directory, so it is measured against
     * the root before either set is asked about it. */
    let relative = resolved.relative_to_root(path);
    if (!include.is_empty() && !include.is_match(&relative)) || exclude.is_match(&relative) {
        return;
    }
    let link_metadata = match path.symlink_metadata() {
        Ok(value) => value,
        Err(error) => {
            discovery.skipped.push(skip(path, explicit_path, error));
            return;
        }
    };
    let metadata = if link_metadata.file_type().is_symlink() {
        if !resolved.config.files.follow_symlinks {
            discovery.skipped.push(SkippedFile {
                path: path.to_path_buf(),
                reason: "symbolic link".into(),
                error: false,
                explicit: explicit_path,
            });
            return;
        }
        match path.metadata() {
            Ok(metadata) if metadata.is_file() => metadata,
            Ok(_) => return,
            Err(error) => {
                discovery.skipped.push(skip(path, explicit_path, error));
                return;
            }
        }
    } else {
        link_metadata
    };
    /* NOTE: Every path under an explicitly named directory is explicit for hidden and
     * size handling. */
    if !explicit_scope && metadata.len() > resolved.config.files.max_size {
        discovery.skipped.push(SkippedFile {
            path: path.to_path_buf(),
            reason: format!("larger than {} bytes", resolved.config.files.max_size),
            error: false,
            explicit: explicit_path,
        });
        return;
    }
    let source = match fs::read(path) {
        Ok(value) => value,
        Err(error) => {
            discovery.skipped.push(skip(path, explicit_path, error));
            return;
        }
    };
    if source.iter().take(8192).any(|byte| *byte == 0) {
        discovery.skipped.push(SkippedFile {
            path: path.to_path_buf(),
            reason: "binary file (NUL byte)".into(),
            error: false,
            explicit: explicit_path,
        });
        return;
    }
    let built_in = forced_language
        .map(|language| Detection {
            language,
            dialect: forced_dialect.unwrap_or(Dialect::Standard),
            reason: "command-line",
        })
        .or_else(|| detect_language(Some(path), &source));
    let Detection {
        language: detected_language,
        dialect: detected_dialect,
        ..
    } = built_in.unwrap_or(Detection {
        language: Language::Unknown,
        dialect: Dialect::Standard,
        reason: "configuration-routing",
    });
    let (language, options) = match resolved.for_path(path, detected_language, detected_dialect) {
        Ok(value) => value,
        Err(error) => {
            if discovery.fatal.is_none() {
                discovery.fatal = Some(error);
            }
            return;
        }
    };
    if !resolved.language_is_enabled(language) {
        discovery.skipped.push(SkippedFile {
            path: path.to_path_buf(),
            reason: "language disabled by configuration".into(),
            error: false,
            explicit: explicit_path,
        });
        return;
    }
    let profile = if language == Language::Unknown {
        profile_for_path(path, resolved)
    } else {
        None
    };
    let plugin = if language == Language::Unknown && profile.is_none() {
        plugin_for_path(path, resolved)
    } else {
        None
    };
    if language == Language::Unknown && profile.is_none() && plugin.is_none() {
        discovery.skipped.push(SkippedFile {
            path: path.to_path_buf(),
            reason: NO_LANGUAGE.into(),
            error: false,
            explicit: explicit_path,
        });
        return;
    }
    discovery.files.push(SourceFile {
        path: path.to_path_buf(),
        source,
        language,
        dialect: options.scan.dialect,
        options,
        profile,
        plugin,
    });
}

pub fn plugin_for_path(path: &Path, resolved: &ResolvedConfig) -> Option<String> {
    let extension = path.extension()?.to_str()?.trim_start_matches('.');
    resolved
        .config
        .plugins
        .routes
        .get(&extension.to_ascii_lowercase())
        .cloned()
}

pub fn profile_for_path(path: &Path, resolved: &ResolvedConfig) -> Option<DeclarativeProfile> {
    let extension = path.extension()?.to_str()?.trim_start_matches('.');
    resolved
        .config
        .profiles
        .values()
        .find(|profile| {
            profile.extensions.iter().any(|candidate| {
                candidate
                    .trim_start_matches('.')
                    .eq_ignore_ascii_case(extension)
            })
        })
        .cloned()
}

/// Compile one of the `[files]` glob lists.
///
/// A walk asks for these in `discover_with_scope` and a staged run asks for
/// them in `git::run_staged`; both measure a path against the project root
/// first, so both get the same answer for the same path.
pub(crate) fn compile_globs(patterns: &[String]) -> Result<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    for pattern in patterns {
        let glob = Glob::new(pattern).map_err(|error| {
            /* INVARIANT: Both halves of this line came out of a file in the project: the
             * pattern the caller wrote, and a `globset` parse error that
             * quotes that same pattern straight back. Neither may reach a
             * terminal verbatim, and the line stays one line. The pattern
             * keeps the spacing it was written with, because a reader who is
             * shown something else cannot find it in the file. It is the same
             * treatment `config::validate_regexes` gives the other pattern a
             * project file carries. */
            anyhow!(
                "invalid file glob `{}`: {}",
                crate::output::sanitize_path(pattern),
                crate::output::sanitize_message(&error.to_string())
            )
        })?;
        builder.add(glob);
    }
    builder.build().context("cannot compile file globs")
}

fn skip(path: &Path, explicit: bool, error: impl std::fmt::Display) -> SkippedFile {
    SkippedFile {
        path: path.to_path_buf(),
        reason: error.to_string(),
        error: true,
        explicit,
    }
}