Skip to content

Commit

Permalink
fix: Include untracked files with --gitignore option
Browse files Browse the repository at this point in the history
The current `--gitignore` option correctly ignores files which are
"ignored" by git, but the `--ignore` option to `git ls-files` does not
include untracked files in it's output.

These can be detected using `git ls-files --others --exclude-standard`.

Combine the two calls into a single deduplicated gitignore list to
ignore all files properly.
  • Loading branch information
willcl-ark committed Jul 22, 2024
1 parent 8f2df53 commit b25bb81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Another example is to call *mlc* on a certain directory or file:
mlc ./docs
```

Alternatively you may want to ignore all files currently ignored by `git` (requires `git` binary to be found on $PATH) and set a root-dir for relative links:
Alternatively you may want to ignore all files currently ignored or untracked by `git` (requires `git` binary to be found on $PATH) and set a root-dir for relative links:

```bash
mlc --gitignore --root-dir .
Expand All @@ -126,7 +126,7 @@ The following arguments are available:
| `--match-file-extension` | `-e` | Set the flag, if the file extension shall be checked as well. For example the following markup link `[link](dir/file)` matches if for example a file called `file.md` exists in `dir`, but would fail when the `--match-file-extension` flag is set. |
| `--version` | `-V` | Print current version of mlc |
| `--ignore-path` | `-p` | Comma separated list of directories or files which shall be ignored. For example |
| `--gitignore` | `-g` | Ignore all files currently ignored by git (requires `git` binary to be available on $PATH). |
| `--gitignore` | `-g` | Ignore all files currently ignored or untracked by git (requires `git` binary to be available on $PATH). |
| `--ignore-links` | `-i` | Comma separated list of links which shall be ignored. Use simple `?` and `*` wildcards. For example `--ignore-links "http*://crates.io*"` will skip all links to the crates.io website. See the [used lib](https://github.com/becheran/wildmatch) for more information. |
| `--markup-types` | `-t` | Comma separated list list of markup types which shall be checked [possible values: md, html] |
| `--root-dir` | `-r` | All links to the file system starting with a slash on linux or backslash on windows will use another virtual root dir. For example the link in a file `[link](/dir/other/file.md)` checked with the cli arg `--root-dir /env/another/dir` will let *mlc* check the existence of `/env/another/dir/dir/other/file.md`. |
Expand Down
38 changes: 27 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::link_validator::resolve_target_link;
use crate::markup::MarkupFile;
use serde::Deserialize;
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
use std::fmt;
use std::fs;
Expand Down Expand Up @@ -132,23 +133,20 @@ fn find_all_links(config: &Config) -> Vec<MarkupLink> {
links
}

fn find_git_ignored_files() -> Option<Vec<PathBuf>> {
fn get_git_files(args: &[&str]) -> Option<Vec<PathBuf>> {
let output = Command::new("git")
.arg("ls-files")
.arg("--ignored")
.arg("--others")
.arg("--exclude-standard")
.output()
.expect("Failed to execute 'git' command");
.arg("ls-files")
.args(args)
.output()
.expect("Failed to execute 'git' command");

if output.status.success() {
let ignored_files = String::from_utf8(output.stdout)
let files = String::from_utf8(output.stdout)
.expect("Invalid UTF-8 sequence")
.lines()
.filter(|line| line.ends_with(".md") || line.ends_with(".html"))
.filter_map(|line| fs::canonicalize(Path::new(line.trim())).ok())
.collect::<Vec<_>>();
Some(ignored_files)
.collect();
Some(files)
} else {
eprintln!(
"git ls-files command failed: {}",
Expand All @@ -158,6 +156,24 @@ fn find_git_ignored_files() -> Option<Vec<PathBuf>> {
}
}

fn find_git_ignored_files() -> Option<Vec<PathBuf>> {
let ignored_files = get_git_files(&["--ignored", "--others", "--exclude-standard"])?;
let untracked_files = get_git_files(&["--others", "--exclude-standard"])?;

let mut combined_files: HashSet<PathBuf> = HashSet::new();
combined_files.extend(ignored_files);
combined_files.extend(untracked_files);

let result = combined_files
.into_iter()
.filter(|path| {
path.extension()
.map_or(false, |ext| ext == "md" || ext == "html")
})
.collect();

Some(result)
}

fn print_helper(
link: &MarkupLink,
Expand Down

0 comments on commit b25bb81

Please sign in to comment.