简体   繁体   English

如何在 git-hook 中应用 eslint/prettier 自动修复

[英]How to apply eslint/prettier auto-fixes in git-hook

I am wondering how can I apply eslint and prettier auto fixes in git-hook pre-push phase.我想知道如何在git-hook pre-push阶段应用eslintprettier自动修复。 I tried something like this:我试过这样的事情:

./node_modules/.bin/eslint index.js --fix
git add .
git commit --amend --no-edit

It fixes problems in my local repo but doesn't push it to the remote.它修复了我本地存储库中的问题,但不会将其推送到远程。 Then also after I type git status it is written that I have 1 and 1 different commits each, respectively and I have to use git pull to fix my problems.然后,在我输入git status它写到我分别有 1 个和 1 个不同的提交,我必须使用git pull来解决我的问题。 This generates super mess in the repo.这会在回购中产生超级混乱。

Do you have any idea how can I achieve this ?你知道我怎么能做到这一点吗?

Part of your mess comes from the git commit --amend : with --amend , git always rewrites the HEAD commit, so your HEAD commit after you have run git push will always be different than what it was before.你的混乱部分来自git commit --amend :使用--amend ,git总是重写HEAD提交,所以你运行git push后的HEAD提交总是与之前的不同。

One other part of the mess is that, since your hook runs git add .混乱的另一部分是,因为您的钩子运行git add . (instead of trying to selectively choose files, or chunks), it will always add everything in your worktree. (而不是尝试有选择地选择文件或块),它将始终在您的工作树中添加所有内容。 No more git add <subdir>/ or git add -p ... for you : you pre-push hook will always add all that's on your disk.不再需要git add <subdir>/git add -p ...给你:你的pre-push hook 将始终添加磁盘上的所有内容


Hooks (pre-commit and pre-push) are IMHO better suited to be read only actions, with the ability to cancel the action if necessary.钩子(预提交和预推送)恕我直言更适合只读操作,并能够在必要时取消操作。

Eg : you can have a lint script, with a check mode (check lint rules and exit with non zero code if rules are not respected) and a update mode (apply known fixes for the rules).例如:您可以拥有一个lint脚本,带有check模式(检查 lint 规则,如果不遵守规则,则以非零代码退出)和update模式(应用已知的规则修复程序)。 Note that, depending on your linters, some rules may need some manual action anyway.请注意,根据您的短绒,某些规则可能需要一些手动操作。

Use the check mode in your pre-push hook, and possibly include the update mode in a script that will additionally git add your files ;在 pre-push 钩子中使用check模式,并可能在脚本中包含update模式,该脚本将另外git add你的文件; at least, you will re-gain the opportunity to review what you have committed before pushing.至少,你将重新获得机会在推动之前回顾你所做的事情

As suggested, you can also use the check mode in a hook on the server side to reject commits that don't adhere to the lint rules you chose.根据建议,您还可以在服务器端的挂钩中使用check模式来拒绝不符合您选择的 lint 规则的提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM