-
-
Notifications
You must be signed in to change notification settings - Fork 310
fix(config): ensure the actually used config file is correct, fix single config file being detected as duplicated, better test coverage #1784
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
Open
bearomorphism
wants to merge
1
commit into
master
Choose a base branch
from
fix-multiple-config-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−90
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,62 +1,64 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from commitizen import defaults, git, out | ||
| from commitizen.config.factory import create_config | ||
| from commitizen.exceptions import ConfigFileIsEmpty, ConfigFileNotFound | ||
|
|
||
| from .base_config import BaseConfig | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Generator | ||
|
|
||
|
|
||
| def _resolve_config_paths(filepath: str | None = None) -> Generator[Path, None, None]: | ||
| if filepath is not None: | ||
| out_path = Path(filepath) | ||
| if not out_path.exists(): | ||
| raise ConfigFileNotFound() | ||
|
|
||
| yield out_path | ||
| return | ||
|
|
||
| def _resolve_config_paths() -> list[Path]: | ||
| git_project_root = git.find_git_project_root() | ||
| cfg_search_paths = [Path(".")] | ||
| if git_project_root: | ||
|
|
||
| if git_project_root and not cfg_search_paths[0].samefile(git_project_root): | ||
| cfg_search_paths.append(git_project_root) | ||
|
|
||
| for path in cfg_search_paths: | ||
| # The following algorithm is ugly, but we need to ensure that the order of the candidates are preserved before v5. | ||
| # Also, the number of possible config files is limited, so the complexity is not a problem. | ||
| candidates: list[Path] = [] | ||
| for dir in cfg_search_paths: | ||
| for filename in defaults.CONFIG_FILES: | ||
| out_path = path / Path(filename) | ||
| if out_path.exists(): | ||
| yield out_path | ||
| out_path = dir / Path(filename) | ||
| if out_path.exists() and all(not out_path.samefile(p) for p in candidates): | ||
| candidates.append(out_path) | ||
| return candidates | ||
|
|
||
|
|
||
| def _create_config_from_path(path: Path) -> BaseConfig: | ||
| with open(path, "rb") as f: | ||
| data: bytes = f.read() | ||
|
|
||
| return create_config(data=data, path=path) | ||
|
|
||
|
|
||
| def read_cfg(filepath: str | None = None) -> BaseConfig: | ||
| config_candidates = list(_resolve_config_paths(filepath)) | ||
| if filepath is not None: | ||
| conf_path = Path(filepath) | ||
| if not conf_path.exists(): | ||
| raise ConfigFileNotFound() | ||
| conf = _create_config_from_path(conf_path) | ||
| if conf.is_empty_config: | ||
| raise ConfigFileIsEmpty() | ||
| return conf | ||
|
|
||
| config_candidate_paths = _resolve_config_paths() | ||
|
|
||
| # Check for multiple config files and warn the user | ||
| config_candidates_exclude_pyproject = [ | ||
| path for path in config_candidates if path.name != "pyproject.toml" | ||
| path for path in config_candidate_paths if path.name != "pyproject.toml" | ||
| ] | ||
| if len(config_candidates_exclude_pyproject) > 1: | ||
| filenames = [path.name for path in config_candidates_exclude_pyproject] | ||
| out.warn( | ||
| f"Multiple config files detected: {', '.join(filenames)}. " | ||
| f"Using config file: '{filenames[0]}'." | ||
| ) | ||
|
|
||
| for filename in config_candidates: | ||
| with open(filename, "rb") as f: | ||
| data: bytes = f.read() | ||
|
|
||
| conf = create_config(data=data, path=filename) | ||
|
|
||
| for config_candidate_path in config_candidate_paths: | ||
| conf = _create_config_from_path(config_candidate_path) | ||
| if not conf.is_empty_config: | ||
| if len(config_candidates_exclude_pyproject) > 1: | ||
| out.warn( | ||
| f"Multiple config files detected: {', '.join(map(str, config_candidates_exclude_pyproject))}. " | ||
| f"Using config file: '{config_candidate_path}'." | ||
| ) | ||
| return conf | ||
|
|
||
| if filepath is not None: | ||
| raise ConfigFileIsEmpty() | ||
|
|
||
| return BaseConfig() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.