Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] Can't match git diff output #1118

Open
zaufi opened this issue Oct 9, 2024 · 9 comments
Open

[Q] Can't match git diff output #1118

zaufi opened this issue Oct 9, 2024 · 9 comments
Labels
S-triage Status: New; needs maintainer attention.

Comments

@zaufi
Copy link

zaufi commented Oct 9, 2024

I want to exclude some lines from Git commit message files (see #1117) and have this in my .typos.toml for now:

[default]
extend-ignore-re = [ "(?Rm)^index [0-9a-f]{9}\\.\\.[0-9a-f]{9} [0-9]+$" ]

However, the COMMIT_EDITMSG like this does not match and causes an error:

test: `typos` exclude check
# NOTE: Leave the next line empty to separate the title from the body

# Explain why this change is being made below... (max 120 chars per line)
Playing w/ `extend-ignore-re`

#---[The diff inserted by Git is shown below]-------------------------
…
# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.
diff --git a/tests/010-simple-secret.sh b/tests/010-simple-secret.sh
index 7f5ba185f..0ebe349ef 100644
--- a/tests/010-simple-secret.sh
+++ b/tests/010-simple-secret.sh
…

result in the error:

error: `ba` should be `by`, `be`
  --> path/to/my/repo/.git/worktrees/my-work-tree/COMMIT_EDITMSG:12:10
   |
12 | index 7f5ba185f..0ebe349ef 100644
   |          ^^

It doesn't match even if I reduce regex to the trivial as ^index .*$!

Please help…

@epage
Copy link
Collaborator

epage commented Oct 9, 2024

I put that config in a directory with that block of text in a file and typos succeeded. To test my setup, I removed the config and it failed.

The only thin I can think of is that typos isn't finding the config. If you could run typos with -vvv that might provide some things.

We determine where to load the config based on the file or directory path passed to typos

// Note paths are passed through stdin, `-` is treated like a normal path
let cwd = if path == std::path::Path::new("-") {
if args.file_list.is_some() {
return Err(proc_exit::sysexits::USAGE_ERR.with_message(
"Can't use `-` (stdin) while using `--file_list` provided paths",
));
};
global_cwd.clone()
} else if path.is_file() {
let mut cwd = path
.canonicalize()
.map_err(|err| {
let kind = err.kind();
std::io::Error::new(kind, format!("argument `{}` is not found", path.display()))
})
.with_code(proc_exit::sysexits::USAGE_ERR)?;
cwd.pop();
cwd
} else {
path.canonicalize()
.map_err(|err| {
let kind = err.kind();
std::io::Error::new(kind, format!("argument `{}` is not found", path.display()))
})
.with_code(proc_exit::sysexits::USAGE_ERR)?
};
engine
.init_dir(&cwd)
.with_code(proc_exit::sysexits::CONFIG_ERR)?;

@zaufi
Copy link
Author

zaufi commented Feb 10, 2025

I'm using typos as a pre-commit hook to spellcheck commit messages. So, I've added the -vvv as an extra arg, and debug messages show that it successfully found the config file from the repo root:

[debug] Loading .../myrepo/master/.typos.toml
[debug] glob converted to regex: ...
...
[debug] built glob set; 67 literals, 0 basenames, 0 extensions, 0 prefixes, 0 suffixes, 400 required extensions, 38 regexes
[debug] opened gitignore file: /home/myuser/.config/git/ignore
[debug] built glob set; 0 literals, 1 basenames, 2 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 0 regexes
[debug] .../myrepo/master/.git/COMMIT_EDITMSG: default policy for `<unknown>` file type
error: `ba` should be `be`, `by`
  --> .git/COMMIT_EDITMSG:59:21
   |
59 | index aaa3297..00000ba
   |                     ^^
   |

I intentionally changed 0000000 to 00000ba in the attached Git diff

@zaufi
Copy link
Author

zaufi commented Feb 10, 2025

I also tried another solution: to change my Git message template to add smth like # noqa: spellcheck off till the EOF but also fail ;-(

Please advise 🙏🙏🙏

@epage
Copy link
Collaborator

epage commented Feb 10, 2025

Can you test your config outside of a pre-commit setup to make sure it is working?

@zaufi
Copy link
Author

zaufi commented Feb 10, 2025

Can you test your config outside of a pre-commit setup to make sure it is working?

Yes, it works %-)

$ cat COMMIT_MESSAGE
Test `typos` config

# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.
diff --git a/tests/010-simple-secret.sh b/tests/010-simple-secret.sh
index 7f5ba185f..0ebe349ef 100644

some other text
$ cat .typos.toml
[default]
extend-ignore-re = [
    "(?Rm)^index [0-9a-f]{9}\\.\\.[0-9a-f]{9} [0-9]+$"
  ]
$ typos COMMIT_MESSAGE
$ 

@epage
Copy link
Collaborator

epage commented Feb 10, 2025

Is there a meaningful difference in the verbose output?

@zaufi
Copy link
Author

zaufi commented Feb 10, 2025

Is there a meaningful difference in the verbose output?

Nope... just the same bunch of glob converted to regex: messages.

@zaufi
Copy link
Author

zaufi commented Feb 10, 2025

Sorry! I've got it! %)
The hash is 7 characters here... not 9!


However, I still need advice on how to ignore text since a marker text (e.g. "# Please enter the commit message for your changes.") up to the end of a file.

@epage
Copy link
Collaborator

epage commented Feb 10, 2025

Could you specify your custom commit message template and say which sections you want checked?

Also, at the bottom of https://github.com/crate-ci/typos/blob/master/docs/reference.md#example-configurations we have example regexes you may want to look at.

@epage epage added the S-triage Status: New; needs maintainer attention. label Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-triage Status: New; needs maintainer attention.
Projects
None yet
Development

No branches or pull requests

2 participants