Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions src/stack_pr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
check_gh_installed,
get_current_branch_name,
get_gh_username,
get_repo_root,
get_uncommitted_changes,
is_rebase_in_progress,
)
Expand Down Expand Up @@ -1612,15 +1613,16 @@ 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)
return config


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)
Expand Down
21 changes: 21 additions & 0 deletions src/stack_pr/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
Loading