Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Kyle Harrison
Kyle Harrison

Posted on

     

Chickity-check yo self before you wreck yo self!

Have you ever found yourself in a situation where you pushed a commit to a branch for review, only to discover you made a spelling mistake, used outdated syntax, or forgot to lint your code again after making that one last change?

I've wasted countless hours in the past doing exactly this! It is slow and time-consuming to identify an issue after the fact and it also slows down other folks in your team performing the code review. There must be a better way!

Pre-commit solves this problem by providing pre-commit hooks that run after every commit which solves all of these common, repeatable problems that machines are good at solving.

However finding an awesome new tool to enhance your workflow is only half the challenge, convincing one or more teams to adopt it another.

Using a tool - without imposing it upon a team

Usingpre-commit requires a.pre-commit-config.yaml to be placed in a repository however to avoid updating the.gitignore file for every new repository you wish to interactive with, the best solution i found was to add the following to a global.gitignore.

> vim ~/.gitignore.pre-commit-config.yaml.codespellrc
Enter fullscreen modeExit fullscreen mode

This allows for the file to be ignored from all repositories I interact with, without the risk of potentially committing it or needing to update the repository to use it. This is especially useful when I need to add confirmation files for some of the additional pre-commit hooks that I'm using such ascodespell.

Finally within your repository you can create and use use pre-commit:

---# .pre-commit-config.yaml# ========================## pre-commit clean# pre-commit install# pre-commit install-hooks## precommit hooks installation## - pre-commit autoupdate## - pre-commit run black## continuous integration# ======================## - pre-commit run --all-files#repos:-repo:https://github.com/pre-commit/pre-commit-hooksrev:v4.0.1hooks:-id:trailing-whitespace-id:end-of-file-fixer-id:debug-statements-id:check-merge-conflict-id:sort-simple-yaml-id:fix-encoding-pragmaargs:["--remove"]-id:forbid-new-submodules-id:mixed-line-endingargs:["--fix=lf"]description:Forces to replace line ending by the UNIX 'lf' character.-id:check-added-large-filesargs:["--maxkb=500"]-id:no-commit-to-branchargs:[--branch,master]-id:check-yaml-id:check-jsonfiles:^tests/app/-id:pretty-format-jsonargs:["--no-sort-keys","--autofix"]files:^tests/app/-repo:metahooks:-id:check-hooks-apply-id:check-useless-excludes-repo:https://github.com/ambv/blackrev:21.5b1hooks:-id:blacklanguage_version:python3.9-repo:https://github.com/PyCQA/banditrev:1.7.0hooks:-id:banditdescription:Security oriented static analyser for python codeexclude:tests/|scripts/args:--s-B101-repo:https://github.com/codespell-project/codespellrev:v2.1.0hooks:-id:codespellname:codespelldescription:Checks for common misspellings in text files.entry:codespelllanguage:pythontypes:[text]-repo:https://github.com/asottile/pyupgraderev:v2.19.4hooks:-id:pyupgrade
Enter fullscreen modeExit fullscreen mode

If you also want to usecodespell, you will need to add a.codespellrc file to the root of your repository:

[codespell]quiet-level= 3count=check-hidden=check-filenames=enable-colors=builtin= clear,rare,informal,code,namesignore-words-list= pullrequest
Enter fullscreen modeExit fullscreen mode

Now that pre-commit is configured, you can install and run it:

pre-commitinstallpre-commit run--allTrim Trailing Whitespace.................................................PassedFix End of Files.........................................................PassedDebug Statements(Python)................................................PassedCheckformerge conflicts................................................PassedSort simple YAML files...............................(no files to check)SkippedFix python encoding pragma...............................................PassedForbid new submodules....................................................PassedMixed line ending........................................................PassedCheckforadded large files..............................................PassedDon't commit to branch...................................................PassedCheck Yaml...............................................................PassedCheck JSON...........................................(no files to check)SkippedPretty format JSON...................................(no files to check)SkippedCheck hooks apply to the repository..................(no files to check)SkippedCheck for useless excludes...........................(no files to check)Skippedblack....................................................................Passedbandit...................................................................Passedcodespell................................................................Passedpyupgrade................................................................Passed
Enter fullscreen modeExit fullscreen mode

Lastly, if pre-commit is ever blocking your workflow and for any reason you don't want to run it, you can append a--no-verify flag onto your commit to skip it.

git commit-m"do the thing"--no-verify
Enter fullscreen modeExit fullscreen mode

Now all your future commits are protected from common gotcha's and save time in code review.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jonasbn profile image
Jonas Brømsø
Computer programmer, runner, LEGO builder, powernapper, yakshaver and father of 2 boys all squeezed in the few hours available.
  • Location
    Copenhagen, Denmark
  • Work
    Product Manager at Domutech A/S
  • Joined

Hi Kyle,

Great article, I only just recently discoveredpre-commit myself and dida short write up myself.

I am working onmy own pre-commit hooks, currently only having two:

  • spellcheck
  • prohibit-commit-main

When I went over your configuration I discovered:no-commit-to-branch, which does somewhat the same as the last one in my list, so I will be change to be using that instead.

Anyway I noted that your example output, somewhatbroke due to the' (apostrophe). I do not see this as a serious issue, but in case you did not know you can overwrite the text output and avoid the single apostrophe in: "Don't commit to branch", which is the text emitted default by the hook.

-id:no-commit-to-branchname:"Donotcommittomain"args:[--branch,main]
Enter fullscreen modeExit fullscreen mode

And it will output:

Do not commit to main....................................................Passed
Enter fullscreen modeExit fullscreen mode

Thanks for the article, I learn something new and expanded my toolbox.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Programmer, Tech Enthusiast, Maker, Geeky Dad
  • Location
    Belfast
  • Education
    Bsc Computer Science (Hons)
  • Work
    Staff Software Engineer at Slice
  • Joined

More fromKyle Harrison

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp