Skip to content

Commit 2221e05

Browse files
author
Laszlo Kindrat
committed
Change default .stack-pr.cfg location to repo root
stack-info: PR: #104, branch: laszlokindrat/stack/1
1 parent a4dc185 commit 2221e05

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ stack-pr config land.style=disable
409409
stack-pr config land.style=bottom-only
410410
```
411411
412-
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.
412+
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.
413413
414414
### Config files
415415

src/stack_pr/cli.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
branch_exists,
6868
check_gh_installed,
6969
get_current_branch_name,
70+
get_repo_root,
7071
get_gh_username,
7172
get_uncommitted_changes,
7273
is_rebase_in_progress,
@@ -1612,15 +1613,16 @@ def create_argparser(
16121613
return parser
16131614

16141615

1615-
def load_config(config_file: str) -> configparser.ConfigParser:
1616+
def load_config(config_file: str | Path) -> configparser.ConfigParser:
16161617
config = configparser.ConfigParser()
16171618
if Path(config_file).is_file():
16181619
config.read(config_file)
16191620
return config
16201621

16211622

16221623
def main() -> None: # noqa: PLR0912
1623-
config_file = os.getenv("STACKPR_CONFIG", ".stack-pr.cfg")
1624+
repo_config_file = get_repo_root() / ".stack-pr.cfg"
1625+
config_file = os.getenv("STACKPR_CONFIG", repo_config_file)
16241626
config = load_config(config_file)
16251627

16261628
parser = create_argparser(config)

src/stack_pr/git.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ def get_current_branch_name(repo_dir: Path | None = None) -> str:
101101
raise
102102

103103

104+
def get_repo_root(repo_dir: Path | None = None) -> Path:
105+
"""Returns the root of the git repository.
106+
107+
Args:
108+
repo_dir: path to the repo. Defaults to the current working directory.
109+
110+
Returns:
111+
The root of the given git repository.
112+
"""
113+
try:
114+
return Path(
115+
get_command_output(
116+
["git", "rev-parse", "--show-toplevel"], cwd=repo_dir
117+
).strip()
118+
)
119+
except subprocess.CalledProcessError as e:
120+
if e.returncode == GIT_NOT_A_REPO_ERROR:
121+
raise GitError("Not inside a valid git repository.") from e
122+
raise
123+
124+
104125
def get_uncommitted_changes(
105126
repo_dir: Path | None = None,
106127
) -> dict[str, list[str]]:

0 commit comments

Comments
 (0)