我试图强制执行git commit消息策略,以使我的存储库保持整洁。我看过有关服务器端和客户端挂钩的官方文档,然后碰到了husky。
到目前为止,我可以与第一个一起工作,但无法设置沙哑,我还有很多东西要学习。主要思想是能够在新的工作站上工作而不必手动设置任何客户端挂钩。
有人可以解释我如何设置哈士奇检查我的提交消息,甚至举个例子吗?
这是project-root/githooks文件夹中的commit-msg钩子:
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)
$regex = /([resolved|fixed]) #([0-9])* ([A-Z])\w+/
if !$regex.match(message)
puts "[POLICY] Your message is not formatted correctly!"
puts "Message format must be like:"
puts "resolved #123 Case title (for features)"
puts "fixed #123 Case title (for bugs)"
puts "First letter of 'Case title' must be capitalized!"
exit 1
end
Run Code Online (Sandbox Code Playgroud)
我试图将脚本添加到package.json中:
"scripts": {
... : ...,
"commitmsg": "sh hooks/commit-msg",
... : ...
}
Run Code Online (Sandbox Code Playgroud)
挂钩不起作用。所有消息均通过。如果放在.git / hooks中,它将正常工作。 …
我的文件中有一个 husky 预提交钩子package.json:
"husky": {\n "hooks": {\n "pre-commit": "npx pretty-quick --staged"\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n我的项目中还有一个.prettierrc文件:
{\n "printWidth": 100,\n "singleQuote": false,\n "trailingComma": "all"\n}\nRun Code Online (Sandbox Code Playgroud)\n我的问题是,当我更改文件(已正确格式化)并运行时,npx pretty-quick --staged我会看到我所期望的内容:
Finding changed files since git revision 88264bc2a.\n Found 1 changed file.\n\xe2\x9c\x85 Everything is awesome!\nRun Code Online (Sandbox Code Playgroud)\n但是当我尝试使用git commit钩子运行相同的命令时,我得到以下输出:
husky > pre-commit (node v14.17.0)\n Finding changed files since git revision 88264bc2a.\n Found 1 changed file.\n\xe2\x9c\x8d\xef\xb8\x8f Fixing up src/somefile.ts.\n\xe2\x9c\x85 Everything is awesome!\nRun Code Online (Sandbox Code Playgroud)\n它不应该修复该文件,因为它已经正确格式化。我检查了配置选项pretty-quick,没有选项可以确定它正在使用哪个更漂亮的配置,而且我也无法明确告诉它要使用哪个配置(尽管我只有一个 …
在提交之前我使用husky来检查JS.在我的package.json我有
"scripts": {
"lintStyles": "stylelint app/**/*.scss",
"fixStylesLinting": "stylelint app/**/*.scss --fix",
"lintJS": "eslint app/**/*.js",
"fixJSLinting": "eslint --fix app/**/*.js",
"precommit": "npm run lintJS"
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理,我不明白的是我如何运行lintJS,lintStyles命令.
我正面临一个奇怪的lint-staged插件问题。早些时候它工作正常。
所以问题是当我运行npm run test它时会生成覆盖率报告。
"test": "cross-env CI=true react-scripts test --coverage",
但是当我使用husky预提交运行相同的命令时,lint-staged它不起作用。当我检查控制台时,我发现它正在针对已修改的文件运行。
> portal@0.1.0 test /Users/carlos/Desktop/portal
> cross-env CI=true react-scripts test --coverage "/Users/carlos/Desktop/portal/src/container/Auth/Login.page.js"
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/carlos/Desktop/portal
44 files checked.
testMatch: /Users/carlos/Desktop/portal/src/**/__tests__/**/*.{js,jsx,ts,tsx}, /Users/carlos/Desktop/portal/src/**/*.{spec,test}.{js,jsx,ts,tsx} - 6 matches
testPathIgnorePatterns: /node_modules/ - 44 matches
testRegex: - 0 matches
Pattern: /Users/carlos/Desktop/portal/src/container/Auth/Login.page.js - 0 matches
npm ERR! code ELIFECYCLE
npm ERR! errno 1
Run Code Online (Sandbox Code Playgroud)
有明显的区别
当我跑 …
编写以下脚本是为了在预提交挂钩上按键对 JSON 文件进行排序:
/*
* Will reorder all files in given path.
*/
const sortJson = require("sort-json");
const fs = require("fs");
const chalk = require("chalk");
const log = console.log;
const translationsPath = process.argv.slice(2).join(" ");
function readFiles(dirname) {
try {
return fs.readdirSync(dirname);
} catch (e) {
log(chalk.red(`Failed reading files from path; ${e}`));
}
}
log(
chalk.bgGreen(
`Running json sort pre-commit hook on path: ${translationsPath}`
)
);
const files = readFiles(translationsPath);
files.forEach((file) => {
log(chalk.yellow(`Sorting ${file}...`));
try {
sortJson.overwrite(`${translationsPath}\\${file}`);
log(chalk.green(`Finished sorting ${file}`)); …Run Code Online (Sandbox Code Playgroud) 我有一个 .Net 5 应用程序,想要使用 dotnet 格式。首先,我将commitlint、husky和lint-staged添加到存储库中。文件夹结构如下
\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyCsharpProject\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MyCsharpProject.sln\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Assembly1\nRun Code Online (Sandbox Code Playgroud)\n配置的 package.json 文件如下所示
\n{\n "lint-staged": {\n "*.cs": "dotnet format --include"\n },\n "devDependencies": {\n "@commitlint/cli": "^12.1.1",\n "@commitlint/config-conventional": "^12.1.1",\n "husky": "^6.0.0",\n "lint-staged": "^10.5.4"\n },\n "scripts": {\n "prepare": "husky install"\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n挂钩失败并显示此输出
\n> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -\n[STARTED] Preparing...\n[SUCCESS] Preparing...\n[STARTED] Running tasks...\n[STARTED] Running tasks for *.cs\n[STARTED] dotnet format --include\n[FAILED] dotnet format --include [FAILED]\n[FAILED] dotnet format --include [FAILED]\n[SUCCESS] Running …Run Code Online (Sandbox Code Playgroud) 环境:
git 版本 2.25.1.windows.1
节点 12.x
Npm 2011 年 6 月 14 日
哈士奇7.0.4
视窗 10
如何正确设置 husky 和预提交钩子?就我而言,我只想添加npm test要运行的脚本,因此每次提交都会运行测试。
我已经安装了 husky,几个月来它工作正常,然后今天重新启动后突然出现此错误:
husky - command not found in PATH=/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:/usr/bin:/bin:/usr/sbin:/sbin
.husky/pre-commit: line 4: npx: command not found
Run Code Online (Sandbox Code Playgroud)
我不知道是什么原因造成的,但我无法提交更改。显然我可以绕过这个钩子,但我想解决这个问题。
我正在开发一个依赖 Husky 的项目。当我尝试在 GitHub Desktop 上提交更改或切换分支时,出现以下错误:
2022-11-16T12:18:08.885Z - error: [ui] `git commit -F -` exited with an unexpected code: 1.
stderr:
/c/Users/user/AppData/Roaming/npm/yarn: line 5: cygpath: command not found
node:internal/modules/cjs/loader:988
throw err;
^
Error: Cannot find module 'C:\Users\user\AppData\Local\GitHubDesktop\app-3.1.2\resources\app\git\node_modules\yarn\bin\yarn.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Module._load (node:internal/modules/cjs/loader:833:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Node.js v18.10.0
husky - pre-commit hook exited with code 1 (error)
Run Code Online (Sandbox Code Playgroud)
操作系统:Windows 10 Pro
GitHub 桌面:版本 3.1.2
Git:版本 2.38.0.windows.1
如果我使用 Powershell …
我正在使用赫斯基与Lint-staged和Stylelint
"scripts": {
"precommit": "lint-staged",
},
"lint-staged": {
"*.scss": ["stylelint --syntax scss"
]
},
Run Code Online (Sandbox Code Playgroud)
OS - 最新的OSX
节点 - 6.10.0
NPM - 3.10.00
我收到了这个错误 git commit
> husky - npm run -s precommit
/Users/jitendravyas/app/node_modules/lint-staged/src/index.js:6
const path = require('path')
^^^^^
SyntaxError: Use of const in strict mode.
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/jitendravyas/app/node_modules/lint-staged/index.js:2:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js …Run Code Online (Sandbox Code Playgroud) husky ×10
git ×4
lint-staged ×3
node.js ×3
javascript ×2
npm ×2
stylelint ×2
.net ×1
c# ×1
eslint ×1
git-husky ×1
jestjs ×1
npx ×1
pre-commit ×1
prettier ×1
typescript ×1
yarnpkg ×1