From 22cac7d3e67a294eb0246072ee02c338a5f91985 Mon Sep 17 00:00:00 2001 From: Laszlo Kindrat Date: Mon, 17 Nov 2025 21:53:29 -0500 Subject: [PATCH] Change default `.stack-pr.cfg` location to repo root stack-info: PR: https://github.com/modular/stack-pr/pull/104, branch: laszlokindrat/stack/1 --- README.md | 2 +- src/stack_pr/cli.py | 6 ++++-- src/stack_pr/git.py | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f3fe2eb..9fcedfd 100644 --- a/README.md +++ b/README.md @@ -409,7 +409,7 @@ stack-pr config land.style=disable stack-pr config land.style=bottom-only ``` -The config command modifies the config file (`.stack-pr.cfg` by default, or the path specified by `STACKPR_CONFIG` environment variable). If the file doesn't exist, it will be created. If a setting already exists, it will be updated. +The config command modifies the config file (the `.stack-pr.cfg` file in the repo root by default, or the path specified by `STACKPR_CONFIG` environment variable). If the file doesn't exist, it will be created. If a setting already exists, it will be updated. ### Config files diff --git a/src/stack_pr/cli.py b/src/stack_pr/cli.py index a9f00e5..86a3c77 100755 --- a/src/stack_pr/cli.py +++ b/src/stack_pr/cli.py @@ -68,6 +68,7 @@ check_gh_installed, get_current_branch_name, get_gh_username, + get_repo_root, get_uncommitted_changes, is_rebase_in_progress, ) @@ -1612,7 +1613,7 @@ def create_argparser( return parser -def load_config(config_file: str) -> configparser.ConfigParser: +def load_config(config_file: str | Path) -> configparser.ConfigParser: config = configparser.ConfigParser() if Path(config_file).is_file(): config.read(config_file) @@ -1620,7 +1621,8 @@ def load_config(config_file: str) -> configparser.ConfigParser: def main() -> None: # noqa: PLR0912 - config_file = os.getenv("STACKPR_CONFIG", ".stack-pr.cfg") + repo_config_file = get_repo_root() / ".stack-pr.cfg" + config_file = os.getenv("STACKPR_CONFIG", repo_config_file) config = load_config(config_file) parser = create_argparser(config) diff --git a/src/stack_pr/git.py b/src/stack_pr/git.py index cd4714a..89252c9 100644 --- a/src/stack_pr/git.py +++ b/src/stack_pr/git.py @@ -101,6 +101,27 @@ def get_current_branch_name(repo_dir: Path | None = None) -> str: raise +def get_repo_root(repo_dir: Path | None = None) -> Path: + """Returns the root of the git repository. + + Args: + repo_dir: path to the repo. Defaults to the current working directory. + + Returns: + The root of the given git repository. + """ + try: + return Path( + get_command_output( + ["git", "rev-parse", "--show-toplevel"], cwd=repo_dir + ).strip() + ) + except subprocess.CalledProcessError as e: + if e.returncode == GIT_NOT_A_REPO_ERROR: + raise GitError("Not inside a valid git repository.") from e + raise + + def get_uncommitted_changes( repo_dir: Path | None = None, ) -> dict[str, list[str]]: