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

31 pre commit docs #36

Merged
merged 7 commits into from Dec 3, 2021
Merged

31 pre commit docs #36

merged 7 commits into from Dec 3, 2021

Conversation

BryanGK
Copy link

@BryanGK BryanGK commented Dec 3, 2021

PR adds:

Docs relating to using pre-commit locally

@BryanGK BryanGK linked an issue Dec 3, 2021 that may be closed by this pull request
### 3 versions of every file that make up our git authoring lifecycle (HEAD, Staged, Unstaged)

### Running the pre-commit hook directly from the .git folder
- run `bash .git/hooks/pre-commit` to execute pre-commit hook without committing
Copy link
Collaborator

@BCerki BCerki Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I figured out why this didn't work as expected for me yesterday--I believe this command runs the hook only on the files that are staged, and I didn't have anything staged at the time. If you want to run pre-commit on everything in the repo regardless of staging, it's pre-commit run --all-files

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly! Let's get that added to the docs so it's clear when we'd choose to run each command?

@BryanGK BryanGK marked this pull request as ready for review December 3, 2021 18:39
@BryanGK BryanGK requested a review from BCerki December 3, 2021 19:07
@BryanGK
Copy link
Author

BryanGK commented Dec 3, 2021

I think this doc is in a good spot right now, but I'm still open to suggestions. Otherwise let's merge it in!

Copy link
Collaborator

@BCerki BCerki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to ask someone else to look at this too; I found some parts confusing but that might just be because I'm new to pre-commit

Comment on lines 4 to 9
- Install:
- Using pip: `pip install pre-commit`
- Using Homebrew: `brew install pre-commit`

- Setup
- run `pre-commit install` to set up the git hook script
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just link to the pre-commit docs, in case they change their instructions later

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully support both linking to docs and excerpting key text over here (in case the docs are moved or deleted later)

Comment on lines 13 to 14
Running hooks on unstaged changes can lead to both false-positives and false-negatives during committing. pre-commit only runs on the staged contents of files by temporarily saving the contents of your files at commit time and stashing the unstaged changes while running hooks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If pre-commit only runs on staged contents (2nd sentence), then how would you be a position to run hooks on unstaged changes (1st sentence)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a direct quote from the docs. https://pre-commit.com/#pre-commit-during-commits I'm not sure how to answer that question

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no way to run hooks on unstaged changes. This is part of the pre-commit design philosophy and is very important: pre-commit has side effects and by only running on staged changes you can always see the difference between what you wrote and what pre-commit changed.


Running hooks on unstaged changes can lead to both false-positives and false-negatives during committing. pre-commit only runs on the staged contents of files by temporarily saving the contents of your files at commit time and stashing the unstaged changes while running hooks.

> pre-commit itself will never touch the staging area. These are good ways to silently break commits. In my mind this is one of the worst things that lint-staged does and suggests -- hooks are very frequently not perfect and magically changing what's being committed should not be taken lightly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this a quote from? I find it a bit confusing, though that could just be shaky understanding of pre-commit

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alec quoted this pre-commit/pre-commit#747 (comment), so I'll add a link to give more context


> pre-commit itself will never touch the staging area. These are good ways to silently break commits. In my mind this is one of the worst things that lint-staged does and suggests -- hooks are very frequently not perfect and magically changing what's being committed should not be taken lightly.

Always stage the the changes you intend to commit before running `pre-commit`. If a hook changes our code and fails you will need to manually stage the changes made by `pre-commit`, viewing them one by one with `git add -p` or your preferred git GUI.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "before running pre-commit" mean? Running it with the bash script below? (Once I've done pre-commit install, it runs automatically every time I try to commit, so everything is always already staged.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

means anytime pre-commit hooks are run, whether running pre-commit explicitly or by committing. but ill clarify it


### Write commit a message so it doesn't get blown away by a failed pre-commit hook

- run `git commit` without a comment when that will then run pre-commit and should it fail you won't lose your commit message. Once it passes it will prompt you to input a commit message.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- run `git commit` without a comment when that will then run pre-commit and should it fail you won't lose your commit message. Once it passes it will prompt you to input a commit message.
- run `git commit` without a comment and that will then run pre-commit and should it fail you won't lose your commit message. Once it passes it will prompt you to input a commit message.

### Write commit a message so it doesn't get blown away by a failed pre-commit hook

- run `git commit` without a comment when that will then run pre-commit and should it fail you won't lose your commit message. Once it passes it will prompt you to input a commit message.
- **Recomend** on the first commit of a branch to make a commit message body that refs the issue your branch is related. When opening a pull-request this will automatically link your PR to the issue on GitHub. This can be accomplished by creating the commit message in an editor and adding new line at the end of the message and writing `ref #<issue>`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **Recomend** on the first commit of a branch to make a commit message body that refs the issue your branch is related. When opening a pull-request this will automatically link your PR to the issue on GitHub. This can be accomplished by creating the commit message in an editor and adding new line at the end of the message and writing `ref #<issue>`
- **Recomend** on the first commit of a branch, make a commit message body that refs the issue your branch is related to. When opening a pull-request this will automatically link your PR to the issue on GitHub. This can be accomplished by creating the commit message in an editor and adding new line at the end of the message and writing `ref #<issue>`

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you can end sentences with prepositions, I just don't like to. I'll make the change :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any grammar/spelling suggestions from me will only be for clarity--I have no prescriptive feelings about preposition placement, oxford commas, etc.

@BryanGK BryanGK requested a review from BCerki December 3, 2021 21:54
BCerki
BCerki previously approved these changes Dec 3, 2021
docs/pre-commit.md Outdated Show resolved Hide resolved
docs/pre-commit.md Outdated Show resolved Hide resolved
Co-authored-by: Brianna Cerkiewicz <77306764+BCerki@users.noreply.github.com>
Co-authored-by: Brianna Cerkiewicz <77306764+BCerki@users.noreply.github.com>
@BryanGK BryanGK requested a review from BCerki December 3, 2021 23:09
@BryanGK BryanGK merged commit 32edb68 into main Dec 3, 2021
@BryanGK BryanGK deleted the 31-pre-commit-docs branch December 3, 2021 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Working with pre-commit
3 participants