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

Baseline pre-commit hooks and Git files #1

Merged
merged 5 commits into from
Feb 25, 2020
Merged

Conversation

ShahradR
Copy link
Owner

@ShahradR ShahradR commented Feb 25, 2020

Git dotfiles

This pull request introduces baseline .gitignore and .gitattributes files.

.gitignore

The .gitignore file has been generated on gitignore.io, and ignores common files generated by:

  • Windows
  • Linux
  • Visual Studio Code

.gitattributes

The .gitattributes file was introduced to manage file endings on files pushed to the repository. We used the Common.gitattributes file from gitattributes/gitattributes@5a965d3 as a baseline, which sets many other attributes, including appropriate line endings for common file types. Details on the attributes set by this file can be found in commit 18aa76f

pre-commit

This pull request introduces pre-commit as a Git hook management tool.

Specifically, this PR introduces eight hooks, listed below. These hooks are to be used in all repositories, and act as a baseline set of checks before commits are pushed upstream. These hooks are:

The sections below outline the different hooks introduced in this pull request.

Check Yaml

This hook checks .yaml files for valid syntax, using the ruamel.yaml YAML loader/dumper for Python.

asciicast

Fix End of Files

UNIX applications typically expect a new line at the end of every file (some examples are the GNU diff and sort tools – see this StackExchange question for a detailed explanation). This hook ensures that all files pushed to the repository end with a new line.

asciicast

Trim Trailing Whitespace

This hook removes invisible whitespace characters at the end of a line.

asciicast

Check for case conflicts

File names in Linux operating systems are case-sensitive – the files FILE.txt and file.txt are considered as two distinct files. Windows files, however, are case-insensitive.

To prevent conflicts between the two operating systems, this hook ensures that no two files have the same name, regardless of the case used.

asciicast

Detect Private Key

This hook detects private keys and other secrets that might have been inadvertantly staged, and blocks the commit.

Note: This hook only checks files for certain keywords – if a private key is exported as binary (by omitting the --armor flag when exporting PGP keys, for example), this hook will not flag the file and allow potentially sensitive data to be committed. Investigating the use of Talisman as an alternative.

asciicast

Mixed line endings

This hook flags files that contain both Windows (CRLF) and Linux (LF) file endings.

asciicast

Prettier

This hook runs the Prettier code formatter on all files before committing.

asciicast

commitlint

This commit-msg hook verifies that commit messages follow the Conventional Commits specification. A full list of checks can be found here.

Note: This commit hook needs to be installed separately, using pre-commit install --hook-type commit-msg

asciicast

Add a .gitattributes file to the repository, to assign attributes to
files and folders in the repository.

This file was taken from gitattributes/gitattributes@5a965d3. This
specific .gitattributes file, amongst other things:
- auto-detects text files and performs LF normalization
- explicitly declares common files as either text and binary
- explicitly declares known OS-specific files as ending with CRLF or LF
- converts complex file types to text (doc, pdf, etc) before a diff
- ignore the .gitignore and .gitattribute files when exporting the repo

A guide on the .gitignore file's capabilities can be found on David
Laing's blog, at https://bit.ly/36QX6UD

As that repository is licensed under MIT, the repo's license notice was
added to the top of the file as a comment.
Add a .gitignore file, generated at www.gitignore.io. This particular
gitignore prevents tracking for common files generated by:
- Windows
- Linux
- Visual Studio Code

The generated file can be viewed and modified by navigating to
https://bit.ly/37SkQcr
Add support for pre-commit (www.pre-commit.com) to the project
repository, to manage the project's Git hooks.

With this commits, the following pre-commit hooks are introduced, from
the pre-commit/pre-commit-hooks repository.
- check-yaml
- end-of-file-fixer
- trailing-whitespace
- check-case-conflict
- detect-private-key
- mixed-line-ending
Add support for commitlint (https://commitlint.js.org) to the
repository, enforcing the Conventional Commit specification for Git
commit messages (https://www.conventionalcommits.org).

The pre-commit repo for the hook can be found at
alessandrojcm/commitlint-pre-commit-hook

The Git hook must be installed before use by running the following
command:
pre-commit install --hook-type commit-msg
Add a Git hook to run Prettier, an opiniated code formatter, to
re-format staged files at commit time, standardizing formatting across
files and repositories.

Details about Prettier can be found at https://prettier.io/

Details about Prettier with pre-commit hooks can be found at
https://bit.ly/38qPdGZ
@ShahradR ShahradR changed the title Pre-commit hooks Baseline pre-commit hooks and Git files Feb 25, 2020
@ShahradR ShahradR self-assigned this Feb 25, 2020
@ShahradR ShahradR merged this pull request into master Feb 25, 2020
@ShahradR ShahradR deleted the feature/pre-commit branch February 25, 2020 07:17
@ShahradR ShahradR restored the feature/pre-commit branch May 14, 2020 11:49
@ShahradR ShahradR deleted the feature/pre-commit branch May 16, 2020 13:36
@github-actions
Copy link
Contributor

github-actions bot commented Jul 1, 2020

🎉 This PR is included in version 1.0.0-develop.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@github-actions
Copy link
Contributor

github-actions bot commented Jul 2, 2020

🎉 This PR is included in version 1.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

ShahradR added a commit that referenced this pull request Oct 21, 2022
Add documentation for the baseline pre-commit hooks introduced in the
ShahradR/git-template#GH-1 pull request to the docs/ folder, and serve
it using MkDocs rather than pointing users to the pull request.
ShahradR added a commit that referenced this pull request Dec 5, 2022
Update the sed command when deleting lines from the .pre-commit-config
file with the correct syntax.

Without adding `d` to the line number, the sed invocation fails with the
following error message:

`sed: -e expression #1, char 2: unknown command: `;'`
ShahradR added a commit that referenced this pull request Dec 5, 2022
Update the sed command when deleting lines from the .pre-commit-config
file with the correct syntax.

Without adding `d` to the line number, the sed invocation fails with the
following error message:

`sed: -e expression #1, char 2: unknown command: ';'`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant