预提交无法在嵌套的 go 模块中发现任何 golang 文件

我有一个大的回购,喜欢github.com/myusername/myrepo


现在,在该存储库中(出于超出此问题范围的原因),我初始化了以下模块:gogithub.com/myusername/myrepo/eck-user-mgmt


这是我的github.com/myusername/myrepo/eck-user-mgmt/.pre-commit-config.yaml


---

files: ^eck-user-mgmt/

repos:

  - repo: git://github.com/dnephin/pre-commit-golang

    rev: v0.4.0

    hooks:

      - id: go-fmt

      - id: go-imports

      - id: go-unit-tests

      - id: go-mod-tidy

      - id: golangci-lint

但是,没有发现任何文件/模块/任何内容go


▶ g add .pre-commit-config.yaml && pre-commit run

go fmt...............................................(no files to check)Skipped

go imports...........................................(no files to check)Skipped

go-unit-tests........................................(no files to check)Skipped

go-mod-tidy..........................................(no files to check)Skipped

golangci-lint........................................(no files to check)Skipped

这是从居住的地方运行的,即pre-commit-config.yaml~/work/myrepo/eck-user-mgmt


为什么?


p.s. 我知道这不是模块管理方面的最佳实践,我只想知道是否有办法在特定设置的上下文中使用make工作gopre-commitgo


婷婷同学_
浏览 174回答 3
3回答

侃侃无极

go 命令通常在主模块及其依赖项的上下文中运行。它通过在当前工作目录及其父目录中查找文件来查找主模块。go.mod如果您的预提交是在存储库的根目录中运行的,并且那里没有文件,则命令将在任何模块之外运行。因此,例如,不会做任何事情。go.modgo mod tidygo test ./...您可能需要在每个模块中运行这些命令。您可以在 shell 脚本中找到包含文件的目录:go.modmodfiles=($(find . -type f -name go.mod))for f in "${modfiles[@]}"; do  pushd "$(dirname "$f")"  ### presubmit commands  popddone

红颜莎娜

只是加起来就是康罗德@Jay建议的方法。由于似乎情况与单存储库不太融洽,并且考虑到这变得更加复杂,因此我选择了以下内容pre-commitgo.git/pre-commitGOMODULESFILENAME="gomodules"declare -a gomodarrayprecommit() {&nbsp; &nbsp; go mod tidy&nbsp; &nbsp; go fmt&nbsp; &nbsp; go test ./... -coverprofile cover.out;&nbsp; &nbsp; go tool cover -func cover.out&nbsp; &nbsp; rm cover.out}while read -r line;do&nbsp;&nbsp;&nbsp; &nbsp; gomodarray+=("$(echo "$line")")done<gomodulesfor f in "${gomodarray[@]}"; do&nbsp; &nbsp; &nbsp; &nbsp; echo "Running golang pre-commit actions for project: $f"&nbsp; &nbsp; &nbsp; &nbsp; sleep 1&nbsp; &nbsp; &nbsp; &nbsp; pushd "$f"&nbsp; &nbsp; &nbsp; &nbsp; precommit&nbsp; &nbsp; &nbsp; &nbsp; popddone其中 是存储库根部的文件,逐行列出存储库的所有 go 模块。gomodules

德玛西亚99

(no files to check)Skipped表示没有任何东西与特定钩子的 / / 集匹配typesfilesexclude看看你的设置:** 顶级 **:files: ^eck-user-mgmt/在每个钩子上(有一个混合的文件: \.go$&nbsp;和类型: [go])它们组合在一起,因此将在存储库中匹配的文件必须同时匹配并以^eck-user-mgmt/.go如果你运行它应该~粗略地复制预提交所做的事情来处理文件匹配git ls-files | grep '^eck-user-mgmt' | grep '\.go$'另请注意,您运行了 -- 默认情况下,这只处理暂存的文件。添加新钩子时,您可能希望运行预提交运行 --all-filepre-commit run免责声明:我是预提交的作者
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go