GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — forbid-new-submodules fixes by m-khvoinitsky · Pull Request #619 · pre-commit/pre-commit-hooks

Conversation

@m-khvoinitsky

@m-khvoinitsky m-khvoinitsky commented Jun 23, 2021

Copy link
Copy Markdown
Contributor

Fixes for #609.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
asottile
asottile reviewed Jul 3, 2021
View reviewed changes
Comment thread pre_commit_hooks/forbid_new_submodules.py Outdated
def main(argv: Optional[Sequence[str]] = None) -> int:
# `argv` is ignored, pre-commit will send us a list of files that we
# don't care about
def main(argv: Sequence[str] = sys.argv[1:]) -> int:

@asottile asottile Jul 3, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

this sets a mutable default value which does not work as you would expect in python -- please don't change this, the original argument handling was correct

>>> def f(x=[]):
...     x.append('wat')
...     print(x)
... 
>>> f(['hi'])
['hi', 'wat']
>>> f(['hi'])
['hi', 'wat']
>>> f()
['wat']
>>> f()
['wat', 'wat']
>>> f()
['wat', 'wat', 'wat']
>>> f()
['wat', 'wat', 'wat', 'wat']
>>> f()
['wat', 'wat', 'wat', 'wat', 'wat']

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

m-khvoinitsky reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@m-khvoinitsky m-khvoinitsky Jul 6, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yep, I'm aware of the behavior of mutable defaults in python. In this case it's not a problem because the function will always be called only once and without arguments by setuptools machinery. However, I agree that using it may be fragile, for example, in tests.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pre_commit_hooks/forbid_new_submodules.py
Comment on lines +11 to 27
if (
'PRE_COMMIT_FROM_REF' in os.environ and
'PRE_COMMIT_TO_REF' in os.environ
):
diff_args: Sequence[str] = (
'--merge-base',
os.environ['PRE_COMMIT_FROM_REF'],
os.environ['PRE_COMMIT_TO_REF'],
)
else:
diff_args = ('--staged',)
added_diff = cmd_output(
'git', 'diff', '--staged', '--diff-filter=A', '--raw',
'git', 'diff', '--diff-filter=A', '--raw', *diff_args, '--', *argv,
)

@asottile asottile Jul 3, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

this shouldn't be needed -- use the arguments that are passed from pre-commit directly (you'll want to set up an argparse.ArgumentParser -- see how the rest of the tools do this)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@m-khvoinitsky m-khvoinitsky Jul 6, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Initially I thought that too and wanted to rewrite it using git status instead of git diff to query mode. However, the hook is called "forbid new submodules" which apparently means that bumping somehow already existent submodules should be allowed. That's why we need --diff-filter=A and we need to query PRE_COMMIT_*_REF variables in order to ensure that no submodules have been added in any commits being validated, not only the last one.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/forbid_new_submodules_test.py Outdated
Comment on lines +54 to +56
finally:
del os.environ['PRE_COMMIT_FROM_REF']
del os.environ['PRE_COMMIT_TO_REF']

@asottile asottile Jul 3, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

use mock.patch.dict, though I think this code will be deleted anyway

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

m-khvoinitsky reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@m-khvoinitsky
m-khvoinitsky force-pushed the master branch from 7e72488 to 4227f13 Compare July 6, 2021 22:28
@m-khvoinitsky
m-khvoinitsky force-pushed the master branch 2 times, most recently from 1886f67 to fcf2962 Compare July 19, 2021 23:03
@m-khvoinitsky

m-khvoinitsky commented Jul 19, 2021

Copy link
Copy Markdown
Contributor Author

Finally managed to take a look into tests failure on Windows and fix it. Please, take a look.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

asottile
asottile reviewed Jul 24, 2021
View reviewed changes
Comment thread pre_commit_hooks/forbid_new_submodules.py Outdated
Comment on lines +14 to +15
if not args.filenames:
return 0

@asottile asottile Jul 24, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

pre-commit will never call this with zero arguments

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

m-khvoinitsky reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@m-khvoinitsky m-khvoinitsky Jul 25, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yeah, I've added it because of git diff or git status behavior difference with one or more path arguments (check them) vs. zero (check all, not none). But I agree that this is not needed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pre_commit_hooks/forbid_new_submodules.py Outdated
'PRE_COMMIT_FROM_REF' in os.environ and
'PRE_COMMIT_TO_REF' in os.environ
):
diff_args: Sequence[str] = (

@asottile asottile Jul 24, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Tuple[str, ...]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

m-khvoinitsky reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread tests/forbid_new_submodules_test.py Outdated
Comment on lines +44 to +45
REV_PARSE_HEAD = ('git', 'rev-parse', 'HEAD')
FROM = subprocess.check_output(REV_PARSE_HEAD).decode().strip()

@asottile asottile Jul 24, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

why are you capitalizing local variables

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@m-khvoinitsky m-khvoinitsky Jul 25, 2021

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Fixed FROM and TO, preserved REV_PARSE_HEAD because it's a constant. If you insist, I can put it to the top of the module (since it only needed in this function I've put it there).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@m-khvoinitsky
m-khvoinitsky force-pushed the master branch from fcf2962 to 83ec0fb Compare July 25, 2021 12:54
@asottile

asottile commented Aug 2, 2021

Copy link
Copy Markdown
Member

tests fail on git 2.25.1

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile
asottile force-pushed the master branch from 83ec0fb to e53e6ca Compare August 2, 2021 20:43
@m-khvoinitsky
forbid-new-submodules: fix triggering failure when only a submodule i…
10c5e4e
…s committed (without any other file); support --from-ref and --to-ref; fixes pre-commit#609
@m-khvoinitsky
m-khvoinitsky force-pushed the master branch from e53e6ca to 10c5e4e Compare August 13, 2021 18:32
@m-khvoinitsky

m-khvoinitsky commented Aug 13, 2021

Copy link
Copy Markdown
Contributor Author

tests fail on git 2.25.1

Fixed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

asottile
asottile approved these changes Aug 15, 2021
View reviewed changes

@asottile asottile left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

m-khvoinitsky reacted with hooray emoji
All reactions
  • 🎉 1 reaction
@asottile
asottile merged commit 25d61d4 into pre-commit:master Aug 15, 2021
@ChiefGokhlayeh ChiefGokhlayeh mentioned this pull request Dec 27, 2021
forbid-new-submodules marked not applicable on repo w/o submodules #707
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@asottile asottile asottile approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@m-khvoinitsky @asottile
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.