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

Apply .gitignore correctly in every source entry #3336

Merged
merged 3 commits into from Nov 5, 2022

Conversation

aaossa
Copy link
Contributor

@aaossa aaossa commented Oct 17, 2022

Description

Fix #3291 : incorrectly ignoring .gitignore presence when more than one source directory is specified. Commit message of the commit that fixed the bug:

When passing multiple src directories, the root gitignore was only applied to the first processed source. The reason is that, in the first source, exclude is None, but then the value gets overridden by re_compile_maybe_verbose(DEFAULT_EXCLUDES), so in the next iteration where the source is a directory, the condition is not met and sets the value of gitignore to None.

To fix this problem, we set the new value of exclude is a new variable (_exclude). This prevents entering the wrong condition on the second iteration and also keeps the origina value if the condition is not met.

Also, the value of both _exclude and gitignore is calculated only once by using None as default value. This help us to reduce the number of computations (only calculate the value once, if needed), and also retains the original value of gitignore, which is then merged with the value of p_gitignore. That's why we also define _gitignore to contain the result of the merge (add) operation.

Checklist - did you ...

  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

src/black/__init__.py Outdated Show resolved Hide resolved
@aaossa
Copy link
Contributor Author

aaossa commented Oct 20, 2022

I'm not sure about why the checks failed 🤔 These seems to be not related with my changes, maybe this requires running the checks again?

  • Error: Invalid value for 'analysis': File 'preview-changes-pr-3336-f55a81e9e0.json' does not exist.
  • Error: Invalid value for 'analysis': File 'assert-no-changes-pr-3336-f55a81e9e0.json' does not exist.

EDIT: NVM, got the actual problem: src/black/__init__.py:630: error: Local variable "gitignore" has inferred type None; add an annotation

@aaossa aaossa marked this pull request as draft October 20, 2022 18:29
src/black/__init__.py Outdated Show resolved Hide resolved
@github-actions
Copy link

github-actions bot commented Oct 20, 2022

diff-shades reports zero changes comparing this PR (8475676) to main (2704dc7).


What is this? | Workflow run | diff-shades documentation

@aaossa aaossa marked this pull request as ready for review October 20, 2022 19:02
src/black/__init__.py Outdated Show resolved Hide resolved
src/black/__init__.py Outdated Show resolved Hide resolved
@aaossa aaossa marked this pull request as draft November 2, 2022 03:05
@aaossa aaossa force-pushed the issue-3291 branch 4 times, most recently from 556fa41 to 8475676 Compare November 2, 2022 16:23
When passing multiple src directories, the root gitignore was only
applied to the first processed source. The reason is that, in the
first source, exclude is `None`, but then the value gets overridden by
`re_compile_maybe_verbose(DEFAULT_EXCLUDES)`, so in the next iteration
where the source is a directory, the condition is not met and sets the
value of `gitignore` to `None`.

To fix this problem, we store a boolean indicating if `exclude` is
`None` and set the value of `exclude` to its default value if that's
the case. This makes sure that the flow enters the correct condition on
following iterations and also keeps the original value if the condition
is not met.

Also, the value of `gitignore` is initialized as `None` and overriden
if necessary. The value of `root_gitignore` is always calculated to
avoid using additional variables (at the small cost of additional
computations).

Signed-off-by: Antonio Ossa Guerra <aaossa@uc.cl>
The test creates a fake context and collects the files from two
sources. Both sources are directories in the root path, where a
gitignore file ignores a filename that is present in both subdirs.

Before the fix introduced in the previous commit, this test was
expected to fail: a file that should be ignores was still visible for
Black. Now, the test is passed.

Signed-off-by: Antonio Ossa Guerra <aaossa@uc.cl>
Add entry about fixed bug: .gitignore being skipped when more than one
source directory was given

Signed-off-by: Antonio Ossa Guerra <aaossa@uc.cl>
@aaossa aaossa marked this pull request as ready for review November 2, 2022 16:37
@aaossa aaossa requested review from JelleZijlstra and removed request for ichard26 November 2, 2022 16:38
@JelleZijlstra JelleZijlstra merged commit 0e9d29a into psf:main Nov 5, 2022
@aaossa aaossa deleted the issue-3291 branch November 5, 2022 12:25
Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

Hey I just wanted to say thanks for the PR. I really appreciate it! Also thank you @JelleZijlstra for the review! 🧡

The PR overall looked good, but two things stood out to me on a quick scan.

src/black/__init__.py Show resolved Hide resolved
src/black/__init__.py Show resolved Hide resolved
@aaossa
Copy link
Contributor Author

aaossa commented Nov 6, 2022

Hey I just wanted to say thanks for the PR. I really appreciate it! Also thank you @JelleZijlstra for the review! 🧡

The PR overall looked good, but two things stood out to me on a quick scan.

I'll tackle your suggestions while solving conflicts on #3338 . One of the conflicts is around the same area, so solving them there is "free"

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.

.gitignore is ignored by black if more than one source location is specified
3 participants