175175
176176Please 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+ """
178187UPDATE_STACK_TIP = """
179188If 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