Skip to content

Preparatory work & clean-up for #219 - #223

Merged
asottile merged 1 commit into
pre-commit:masterfrom
Lucas-C:master
May 8, 2015
Merged

asottile merged 1 commit into
pre-commit:masterfrom
Lucas-C:master

Conversation

@Lucas-C

@Lucas-C Lucas-C commented May 5, 2015

Copy link
Copy Markdown
Contributor

This is an attempt to address #219.
As it is a first draft, it misses some tests and a bit of code cleaning, but I'd like an initial design review before fixing the final glitches.

Example of valid _ .pre-commit-config.yaml_ using this feature :

- local-hooks:
  - id: no-todo
    name: No TODO allowed in commited files
    entry: grep -iI todo
    expected_return_value: 1
    language: system
    files: ''

Pros / Cons of the solution choosed:

  • + end-user-friendly: the new "local-hooks" field schema is the same as hooks.yaml schema
  • + nice reuse of MANIFEST_JSON_SCHEMA
  • + no more _run_hook / _run_hooks code duplication
  • + all the regressions tests pass
  • - "local-hooks" and classical repo/sha/hooks objects are mixed together in .pre-commit-config.yaml
  • - the hooks_and_invokers list of tuples is not entirely satisfying. Maybe it'd be worth having a proper Hook class ?

@asottile

asottile commented May 5, 2015

Copy link
Copy Markdown
Member

I like the idea.

I wonder if we can simplify this further and reuse existing logic by making local-hooks slightly less special to configure:

  • Make a special value for repo (for instance pre-commit-locally-configured or maybe something less verbose)
  • If that special repo is encountered, its backing manifest is a list which always contains all hook ids with basically a Null configuration
  • The code already allows overriding of all the keys presented in the backing configuration so the rest would just work

I also like the cleanup work that was done for _run_hooks it might be easier to think about this review if we split this into two PRs as well

👍

@Lucas-C

Lucas-C commented May 6, 2015

Copy link
Copy Markdown
Contributor Author

Agreed, I'll split it in two commits.

Your solution sounds good, but it would make the sha field no more mandatory. In the JSON schema. But we could add an extra validation step so it's not a blocker.

I'm ok to move to this solution, but don't you think the current JSON schemas definition kind of overlap ? There is some code duplication for the allowed overrides, I'll try to make it more DRY.

@asottile

asottile commented May 6, 2015

Copy link
Copy Markdown
Member

Yeah the json schemas do kind of overlap. I really hope to get rid of them eventually (as we've discussed in the other issue) and replace them with a bunch of functions.

imo aim for correctness and readability instead of working hard at DRY, I've found that in a lot of cases expressiveness / correctness kind of work against DRY-ness. Though I think replacing the jsonchemas will actually make a lot of things cleaner / more flexible but that's definitely out of scope for this change I would think :)

I'm cool with making sha optional as long as we require it in the normal case

@Lucas-C

Lucas-C commented May 6, 2015

Copy link
Copy Markdown
Contributor Author

A comment on something you wrote "The code already allows overriding of all the keys presented in the backing configuration so the rest would just work" : actually the hooks definition in the manifest & config schemas differ quite a bit : some fields actually cannot be overridden, only id is required in the config, and it also has an additional exclude field, etc. Due to this differences (manly the missing fields) I feel like we cannot actually implement "local hooks" without modifying the schema.
What would you thing of something like this in validate_config.py :

from pre_commit.clientlib.validate_manifest import MANIFEST_JSON_SCHEMA
CONFIG_JSON_SCHEMA = {
    'type': 'array',
    'minItems': 1,
    'items': {
        'type': 'object',
        'properties': {
            'repo': {'type': 'string'},
            'sha': {'type': 'string'},
            'hooks': MANIFEST_JSON_SCHEMA
        },
        'required': ['repo', 'hooks'],
    }
}

With possibly more tweaking (like adding the exclude field to the manifest).

@Lucas-C
Lucas-C force-pushed the master branch 3 times, most recently from 4a3efb9 to d2691be Compare May 6, 2015 23:21
@Lucas-C Lucas-C changed the title Adding "local-hooks" entry to .pre-commit-config.yaml Preparatory work & clean-up for #219 May 6, 2015
@Lucas-C

Lucas-C commented May 6, 2015

Copy link
Copy Markdown
Contributor Author

I updated the pull request to only contains preliminary changes.
Please don't merge it until you answer my question above :)

Also you wrote "I really hope to get rid of [JSON schemas] eventually, as we've discussed in the other issue". I don't think you mentioned that before in an issue I was part of. Are you refering to issue #157 or something else ?

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 100.0% when pulling a5259ab on Lucas-C:master into aec1c55 on pre-commit:master.

@asottile

asottile commented May 7, 2015

Copy link
Copy Markdown
Member

I'll comment on the second thing when I'm not on the phone :D

The first however: though the fields are not specified in the config schema, the way the runtime hook dictionary is made is by merging the config dict on top of the manifest dict. JSON schema is a bit permissive in that it let's you add keys even if they aren't in the schema.

Comment thread pre_commit/runner.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are the changes to this file necessary? I don't see them used elsewhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll remove them from this commit

@asottile

asottile commented May 7, 2015

Copy link
Copy Markdown
Member

Address comment for pre_commit/runner.py and I'll merge

👍

@asottile

asottile commented May 7, 2015

Copy link
Copy Markdown
Member

Ah yeah as for jsonschema, I totally got you confused with another person. I'm terrible sorry about that >.< Here's the comment I mentioned it in: #157 (comment)

@Lucas-C

Lucas-C commented May 7, 2015

Copy link
Copy Markdown
Contributor Author

the way the runtime hook dictionary is made is by merging the config dict on top of the manifest dict.

Are you refering to the following code snippet ?
https://github.com/pre-commit/pre-commit/blob/master/pre_commit/repository.py#L48

Then ok, but we won't be able to reuse that code for the local hooks, as it's nested in the Repository class.
But we can simply copy that merge logic.

clientlib/validate_config.py: moving "sha" field requirement out of JSON schema
commands/run.py: code simplification
runner.py: made .config a cached_property
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 100.0% when pulling 7dd8c6a on Lucas-C:master into aec1c55 on pre-commit:master.

asottile added a commit that referenced this pull request May 8, 2015
Preparatory work & clean-up for #219
@asottile
asottile merged commit 17a9a82 into pre-commit:master May 8, 2015
@asottile

asottile commented May 9, 2015

Copy link
Copy Markdown
Member

Oh forgot to mention, please PR to add your name on http://github.com/pre-commit/pre-commit.github.io :D

Thanks for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants