Skip to content

Commit f29da60

Browse files
committed
Add 'stack-pr config' command
stack-info: PR: #99, branch: ZolotukhinM/stack/1
1 parent ab1339e commit f29da60

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ That's it!
116116
117117
### Commands
118118

119-
`stack-pr` has four main commands:
119+
`stack-pr` has five main commands:
120120

121121
- `submit` (or `export`) - create a new stack of PRs from the given set of
122-
commits. One can think of this as push my local changes to the corresponding
122+
commits. One can think of this as "push my local changes to the corresponding
123123
remote branches and update the corresponding PRs (or create new PRs if they
124-
dont exist yet).
124+
don't exist yet)".
125125
- `view` - inspect the given set of commits and find the linked PRs. This
126126
command does not push any changes anywhere and does not change any commits.
127127
It can be used to examine what other commands did or will do.
@@ -130,6 +130,9 @@ That's it!
130130
the corresponding local and remote branches and closes the PRs.
131131
- `land` - merge the bottom-most PR in the current stack and rebase the rest of
132132
the stack on the latest main.
133+
- `config` - set configuration values in the config file. Similar to `git config`,
134+
it takes a setting in the format `<section>.<key>=<value>` and updates the
135+
config file (`.stack-pr.cfg` by default).
133136

134137
A usual workflow is the following:
135138

@@ -372,6 +375,32 @@ Inspect the current stack
372375
373376
Takes no additional arguments beyond common ones.
374377
378+
#### config
379+
380+
Set a configuration value in the config file.
381+
382+
Arguments:
383+
384+
- `setting` (required): Configuration setting in format `<section>.<key>=<value>`
385+
386+
Examples:
387+
388+
```bash
389+
# Set verbose mode
390+
stack-pr config common.verbose=True
391+
392+
# Set target branch
393+
stack-pr config repo.target=master
394+
395+
# Set default reviewer(s)
396+
stack-pr config repo.reviewer=user1,user2
397+
398+
# Set custom branch name template
399+
stack-pr config repo.branch_name_template=$USERNAME/stack
400+
```
401+
402+
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.
403+
375404
### Config files
376405
377406
Default values for command line options can be specified via a config file.

src/stack_pr/cli.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@
175175
176176
Please complete or abort the current rebase first.
177177
"""
178+
ERROR_CONFIG_INVALID_FORMAT = """Invalid config format.
179+
180+
Usage: stack-pr config <section>.<key>=<value>
181+
182+
Examples:
183+
stack-pr config common.verbose=True
184+
stack-pr config repo.target=main
185+
stack-pr config repo.reviewer=user1,user2
186+
"""
178187
UPDATE_STACK_TIP = """
179188
If you'd like to push your local changes first, you can use the following command to update the stack:
180189
$ stack-pr export -B {top_commit}~{stack_size} -H {top_commit}"""
@@ -1364,6 +1373,43 @@ def command_view(args: CommonArgs) -> None:
13641373
log(h(blue("SUCCESS!")))
13651374

13661375

1376+
# ===----------------------------------------------------------------------=== #
1377+
# CONFIG
1378+
# ===----------------------------------------------------------------------=== #
1379+
def command_config(config_file: str, setting: str) -> None:
1380+
"""Set a configuration value in the config file.
1381+
1382+
Args:
1383+
config_file: Path to the config file
1384+
setting: Setting in the format "section.key=value"
1385+
"""
1386+
if "=" not in setting:
1387+
error(ERROR_CONFIG_INVALID_FORMAT)
1388+
sys.exit(1)
1389+
1390+
key_path, value = setting.split("=", 1)
1391+
1392+
if "." not in key_path:
1393+
error(ERROR_CONFIG_INVALID_FORMAT)
1394+
sys.exit(1)
1395+
1396+
section, key = key_path.split(".", 1)
1397+
1398+
config = configparser.ConfigParser()
1399+
if Path(config_file).is_file():
1400+
config.read(config_file)
1401+
1402+
if not config.has_section(section):
1403+
config.add_section(section)
1404+
1405+
config.set(section, key, value)
1406+
1407+
with Path(config_file).open("w") as f:
1408+
config.write(f)
1409+
1410+
print(f"Set {section}.{key} = {value}")
1411+
1412+
13671413
# ===----------------------------------------------------------------------=== #
13681414
# Main entry point
13691415
# ===----------------------------------------------------------------------=== #
@@ -1467,6 +1513,15 @@ def create_argparser(
14671513
parents=[common_parser],
14681514
)
14691515

1516+
parser_config = subparsers.add_parser(
1517+
"config",
1518+
help="Set a configuration value",
1519+
)
1520+
parser_config.add_argument(
1521+
"setting",
1522+
help="Configuration setting in format <section>.<key>=<value>",
1523+
)
1524+
14701525
return parser
14711526

14721527

@@ -1489,6 +1544,11 @@ def main() -> None: # noqa: PLR0912
14891544
parser.print_help()
14901545
return
14911546

1547+
# Handle config command early since it doesn't need git repo setup
1548+
if args.command == "config":
1549+
command_config(config_file, args.setting)
1550+
return
1551+
14921552
# Make sure "$ID" is present in the branch name template and append it if not
14931553
args.branch_name_template = fix_branch_name_template(args.branch_name_template)
14941554
common_args = CommonArgs.from_args(args)

0 commit comments

Comments
 (0)