1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
use crate::{commands, config::Config, tui, update};
use anyhow::Result;
use clap::{Args, Parser, Subcommand, ValueEnum};
#[derive(Parser)]
#[command(name = "stax")]
#[command(version)]
#[command(about = "Fast stacked Git branches and PRs", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Args, Clone)]
struct SubmitOptions {
/// Create PRs as drafts
#[arg(short, long)]
draft: bool,
/// Only push, don't create/update PRs
#[arg(long)]
no_pr: bool,
/// Skip git fetch and use cached remote-tracking refs
#[arg(long = "no-fetch", action = clap::ArgAction::SetTrue)]
no_fetch: bool,
/// Skip restack check and submit anyway
#[arg(short, long)]
force: bool,
/// Auto-approve prompts
#[arg(long)]
yes: bool,
/// Disable interactive prompts (use defaults)
#[arg(long)]
no_prompt: bool,
/// Assign reviewers (comma-separated or repeat)
#[arg(long, value_delimiter = ',')]
reviewers: Vec<String>,
/// Add labels (comma-separated or repeat)
#[arg(long, value_delimiter = ',')]
labels: Vec<String>,
/// Assign users (comma-separated or repeat)
#[arg(long, value_delimiter = ',')]
assignees: Vec<String>,
/// Suppress extra output
#[arg(long)]
quiet: bool,
/// Open the current branch PR in browser after submit
#[arg(long, conflicts_with = "no_pr")]
open: bool,
/// Show detailed output
#[arg(short, long)]
verbose: bool,
/// Specify template by name (skip picker)
#[arg(long)]
template: Option<String>,
/// Skip template selection (no template)
#[arg(long)]
no_template: bool,
/// Always open editor for PR body
#[arg(long)]
edit: bool,
/// Generate PR body using AI (claude, codex, or gemini)
#[arg(long)]
ai_body: bool,
/// Re-request review from existing reviewers when updating PRs
#[arg(long)]
rerequest_review: bool,
}
#[derive(Clone, Copy, Debug, ValueEnum)]
enum RestackSubmitAfter {
Ask,
Yes,
No,
}
impl From<RestackSubmitAfter> for commands::restack::SubmitAfterRestack {
fn from(value: RestackSubmitAfter) -> Self {
match value {
RestackSubmitAfter::Ask => commands::restack::SubmitAfterRestack::Ask,
RestackSubmitAfter::Yes => commands::restack::SubmitAfterRestack::Yes,
RestackSubmitAfter::No => commands::restack::SubmitAfterRestack::No,
}
}
}
#[derive(Subcommand)]
enum Commands {
/// Show all stacks (simple tree view)
#[command(visible_aliases = ["s", "ls"])]
Status {
/// Output JSON for scripting
#[arg(long)]
json: bool,
/// Show only the stack for this branch
#[arg(long)]
stack: Option<String>,
/// Show only the current stack
#[arg(short, long)]
current: bool,
/// Compact output for scripts
#[arg(long)]
compact: bool,
/// Suppress extra output
#[arg(long)]
quiet: bool,
},
/// Show all stacks with PR URLs and full details
#[command(name = "ll")]
Ll {
/// Output JSON for scripting
#[arg(long)]
json: bool,
/// Show only the stack for this branch
#[arg(long)]
stack: Option<String>,
/// Show only the current stack
#[arg(short, long)]
current: bool,
/// Compact output for scripts
#[arg(long)]
compact: bool,
/// Suppress extra output
#[arg(long)]
quiet: bool,
},
/// Show all stacks with commits and PR info
#[command(visible_alias = "l")]
Log {
/// Output JSON for scripting
#[arg(long)]
json: bool,
/// Show only the stack for this branch
#[arg(long)]
stack: Option<String>,
/// Show only the current stack
#[arg(short, long)]
current: bool,
/// Compact output for scripts
#[arg(long)]
compact: bool,
/// Suppress extra output
#[arg(long)]
quiet: bool,
},
/// Submit stack - push branches and create/update PRs
#[command(visible_alias = "ss")]
Submit {
#[command(flatten)]
submit: SubmitOptions,
},
/// Merge PRs from bottom of stack up to current branch
Merge {
/// Merge entire stack (ignore current position)
#[arg(long)]
all: bool,
/// Show merge plan without merging
#[arg(long)]
dry_run: bool,
/// Merge method: squash, merge, rebase
#[arg(long, default_value = "squash")]
method: String,
/// Keep branches after merge (don't delete)
#[arg(long)]
no_delete: bool,
/// Fail if CI pending (don't poll/wait)
#[arg(long)]
no_wait: bool,
/// Max wait time for CI per PR in minutes
#[arg(long, default_value = "30")]
timeout: u64,
/// Wait for each PR to be ready (CI + approval) before merging
#[arg(long, conflicts_with_all = ["dry_run", "no_wait"])]
when_ready: bool,
/// Polling interval in seconds for --when-ready mode
#[arg(long, default_value = "15", requires = "when_ready")]
interval: u64,
/// Skip post-merge sync (`stax rs`)
#[arg(long)]
no_sync: bool,
/// Skip confirmation prompt
#[arg(short, long)]
yes: bool,
/// Minimal output
#[arg(short, long)]
quiet: bool,
},
/// Deprecated: use `stax merge --when-ready`
#[command(name = "merge-when-ready", visible_alias = "mwr", hide = true)]
MergeWhenReady {
/// Merge entire stack (include descendants above current)
#[arg(long)]
all: bool,
/// Merge method: squash, merge, rebase
#[arg(long, default_value = "squash")]
method: String,
/// Max wait time per PR in minutes (default: 30)
#[arg(long, default_value = "30")]
timeout: u64,
/// Polling interval in seconds (default: 15)
#[arg(long, default_value = "15")]
interval: u64,
/// Keep branches after merge (don't delete)
#[arg(long)]
no_delete: bool,
/// Skip post-merge sync (`stax rs`)
#[arg(long)]
no_sync: bool,
/// Skip confirmation prompt
#[arg(short, long)]
yes: bool,
/// Minimal output
#[arg(short, long)]
quiet: bool,
},
/// Sync repo - pull trunk, delete merged branches
#[command(visible_alias = "rs")]
Sync {
/// Also restack branches after syncing
#[arg(short, long)]
restack: bool,
/// Prune stale remote-tracking refs during fetch
#[arg(long)]
prune: bool,
/// Don't delete merged branches
#[arg(long)]
no_delete: bool,
/// Also delete local branches whose upstream is gone
#[arg(long)]
delete_upstream_gone: bool,
/// Force sync without prompts
#[arg(short, long)]
force: bool,
/// Avoid hard reset when updating trunk
#[arg(long)]
safe: bool,
/// Continue after resolving restack conflicts
#[arg(long)]
r#continue: bool,
/// Suppress extra output
#[arg(long)]
quiet: bool,
/// Show detailed output including git errors
#[arg(short, long)]
verbose: bool,
/// Auto-stash and auto-pop dirty target worktrees during restack operations
#[arg(long)]
auto_stash_pop: bool,
},
/// Restack (rebase) the current branch onto its parent
Restack {
/// Restack all branches in the stack
#[arg(short, long)]
all: bool,
/// Continue after resolving conflicts
#[arg(long)]
r#continue: bool,
/// Preview predicted conflicts without rebasing
#[arg(long)]
dry_run: bool,
/// Skip conflict confirmation prompt
#[arg(short, long)]
yes: bool,
/// Suppress extra output
#[arg(long)]
quiet: bool,
/// Auto-stash and auto-pop dirty target worktrees during restack operations
#[arg(long)]
auto_stash_pop: bool,
/// After restack, submit stack updates (`ask`, `yes`, `no`)
#[arg(long, value_enum, default_value_t = RestackSubmitAfter::Ask)]
submit_after: RestackSubmitAfter,
},
/// Restack from the bottom and submit updates
Cascade {
/// Push branches to remote but skip PR creation/updates
#[arg(long)]
no_pr: bool,
/// Skip all remote interaction (restack locally only)
#[arg(long)]
no_submit: bool,
/// Auto-stash and auto-pop dirty target worktrees during restack
#[arg(long)]
auto_stash_pop: bool,
},
/// Checkout a branch in the stack
#[command(visible_aliases = ["co", "bco"])]
Checkout {
/// Branch name (interactive if not provided)
branch: Option<String>,
/// Jump directly to trunk
#[arg(long)]
trunk: bool,
/// Jump to parent of current branch
#[arg(long)]
parent: bool,
/// Jump to child branch by index (1-based)
#[arg(long)]
child: Option<usize>,
},
/// Continue after resolving conflicts
#[command(visible_alias = "cont")]
Continue,
/// Resolve in-progress rebase conflicts using AI and continue automatically
Resolve {
/// AI agent override (claude, codex, gemini, opencode)
#[arg(long)]
agent: Option<String>,
/// Model override for the selected agent
#[arg(long)]
model: Option<String>,
/// Maximum AI resolve rounds before stopping
#[arg(long, default_value_t = 5)]
max_rounds: usize,
},
/// Abort an in-progress rebase/conflict resolution
Abort,
/// Stage all changes and amend them to the current commit
#[command(visible_alias = "m")]
Modify {
/// New commit message (keeps existing if not provided)
#[arg(short, long)]
message: Option<String>,
/// Suppress extra output
#[arg(long)]
quiet: bool,
},
/// Authenticate with GitHub
Auth {
/// GitHub personal access token
#[arg(short, long, conflicts_with = "from_gh")]
token: Option<String>,
/// Import token from GitHub CLI (`gh auth token`)
#[arg(long)]
from_gh: bool,
#[command(subcommand)]
command: Option<AuthSubcommand>,
},
/// Show config file path and contents
Config {
/// Clear saved AI agent/model defaults so stax prompts again later
#[arg(long)]
reset_ai: bool,
/// Clear saved AI defaults without opening the interactive picker
#[arg(long, requires = "reset_ai")]
no_prompt: bool,
/// Skip confirmation when used with --reset-ai
#[arg(short, long, requires = "reset_ai")]
yes: bool,
},
/// Initialize stax or reconfigure the repo trunk branch
Init {
/// Set the trunk branch directly instead of prompting
#[arg(long)]
trunk: Option<String>,
},
/// Show diffs for each branch vs parent plus an aggregate stack diff
Diff {
/// Show only the stack for this branch
#[arg(long)]
stack: Option<String>,
/// Show all stacks
#[arg(long)]
all: bool,
},
/// Show range-diff for branches that need restack
RangeDiff {
/// Show only the stack for this branch
#[arg(long)]
stack: Option<String>,
/// Show all stacks
#[arg(long)]
all: bool,
},
/// Check stax configuration and repo health
Doctor,
/// Switch to the trunk branch
#[command(visible_alias = "t")]
Trunk,
/// Move up the stack (to child branch)
#[command(visible_alias = "u")]
Up {
/// Number of branches to move up (default: 1)
count: Option<usize>,
},
/// Move down the stack (to parent branch)
#[command(visible_alias = "d")]
Down {
/// Number of branches to move down (default: 1)
count: Option<usize>,
},
/// Move to the top of the stack (tip/leaf branch)
Top,
/// Move to the bottom of the stack (first branch above trunk)
Bottom,
/// Switch to the previous branch (like git checkout -)
#[command(visible_alias = "p")]
Prev,
/// Branch management commands
#[command(subcommand, visible_alias = "b")]
Branch(BranchCommands),
/// Upstack commands (operate on descendants)
#[command(subcommand, visible_alias = "us")]
Upstack(UpstackCommands),
/// Downstack commands (operate on ancestors)
#[command(subcommand, visible_alias = "ds")]
Downstack(DownstackCommands),
/// Create a new branch stacked on current
#[command(visible_alias = "c")]
Create {
/// Name for the new branch
name: Option<String>,
/// Stage all changes (like git commit --all)
#[arg(short, long)]
all: bool,
/// Commit message (also used as branch name if no name provided)
#[arg(short, long)]
message: Option<String>,
/// Base branch to create from (defaults to current)
#[arg(long)]
from: Option<String>,
/// Override branch prefix (e.g. "feature/")
#[arg(long)]
prefix: Option<String>,
},
/// Open the PR for the current branch in browser
Pr,
/// Open the repository in browser
Open,
/// Show comments on the current branch's PR
Comments {
/// Output raw markdown without rendering
#[arg(long)]
plain: bool,
},
/// Show CI status for all branches in the stack
Ci {
/// Show all tracked branches (not just current stack)
#[arg(long)]
all: bool,
/// Show all branches in the current stack (not just current branch)
#[arg(long, short)]
stack: bool,
/// Output JSON for scripting
#[arg(long)]
json: bool,
/// Force refresh (bypass cache)
#[arg(long)]
refresh: bool,
/// Watch CI until completion (polls periodically)
#[arg(long, short)]
watch: bool,
/// Polling interval in seconds (default: 15)
#[arg(long, default_value = "15")]
interval: u64,
/// Show compact summary cards instead of the full per-check table
#[arg(long, short)]
verbose: bool,
},
/// Split the current branch into multiple stacked branches (interactive)
Split,
/// Copy branch name or PR URL to clipboard
Copy {
/// Copy PR URL instead of branch name
#[arg(long)]
pr: bool,
},
/// Remove a branch from its stack (reparent children to parent)
Detach {
/// Branch to detach (defaults to current)
branch: Option<String>,
/// Skip confirmation prompt
#[arg(long)]
yes: bool,
},
/// Interactively reorder branches within a stack
Reorder {
/// Skip confirmation prompts
#[arg(long)]
yes: bool,
},
/// Validate stack metadata health
Validate,
/// Auto-repair broken metadata
Fix {
/// Show what would be fixed without changing anything
#[arg(long)]
dry_run: bool,
/// Auto-approve prompts
#[arg(long)]
yes: bool,
},
/// Run a command on each branch in the stack
Run {
/// Command to run
#[arg(trailing_var_arg = true, required = true)]
cmd: Vec<String>,
/// Run on all tracked branches (not just current stack)
#[arg(long)]
all: bool,
/// Run only one stack (current stack by default, or a specific branch's stack with --stack=<branch>)
#[arg(long, num_args = 0..=1, require_equals = true)]
stack: Option<Option<String>>,
/// Stop after first failure
#[arg(long)]
fail_fast: bool,
},
/// Backward-compatible alias for `run`
#[command(hide = true)]
Test {
/// Command to run
#[arg(trailing_var_arg = true, required = true)]
cmd: Vec<String>,
/// Run on all tracked branches (not just current stack)
#[arg(long)]
all: bool,
/// Run only one stack (current stack by default, or a specific branch's stack with --stack=<branch>)
#[arg(long, num_args = 0..=1, require_equals = true)]
stack: Option<Option<String>>,
/// Stop after first failure
#[arg(long)]
fail_fast: bool,
},
/// Interactive tutorial (no auth or repo needed)
Demo,
/// Generate standup summary of recent activity
Standup {
/// Output raw JSON (standup data, or summary JSON when combined with --summary)
#[arg(long)]
json: bool,
/// Show all stacks (not just current)
#[arg(long)]
all: bool,
/// Time window in hours (default: 24)
#[arg(long, default_value = "24")]
hours: i64,
/// Summarize standup using AI agent
#[arg(long)]
summary: bool,
/// Include Jira sprint context from `jit` (tickets with PRs + next backlog items)
#[arg(long)]
jit: bool,
/// AI agent to use (claude, codex, gemini, opencode). Defaults to config or auto-detect
#[arg(long)]
agent: Option<String>,
/// Output plain text with no colors or spinner (useful for piping)
#[arg(long)]
plain_text: bool,
},
/// Generate content using AI
Generate {
/// Generate PR body from diff and update the PR
#[arg(long)]
pr_body: bool,
/// Open editor to review before updating
#[arg(long)]
edit: bool,
/// Disable interactive prompts (use defaults)
#[arg(long)]
no_prompt: bool,
/// AI agent to use (claude, codex, gemini, opencode). Defaults to config or auto-detect
#[arg(long)]
agent: Option<String>,
/// Model to use with the AI agent. Defaults to config or agent's default
#[arg(long)]
model: Option<String>,
},
/// Generate changelog between two refs
Changelog {
/// Starting ref (tag, branch, or commit)
from: String,
/// Ending ref (defaults to HEAD)
#[arg(default_value = "HEAD")]
to: String,
/// Filter commits to those touching this path
#[arg(long)]
path: Option<String>,
/// Output JSON for scripting
#[arg(long)]
json: bool,
},
/// Rename the current branch
Rename {
/// New branch name (interactive if not provided)
name: Option<String>,
/// Edit the commit message
#[arg(short, long)]
edit: bool,
/// Push new branch and delete old remote (non-interactive)
#[arg(short, long)]
push: bool,
/// Use name literally without applying prefix
#[arg(long, hide = true)]
literal: bool,
},
/// Undo the last stax operation (or a specific one)
Undo {
/// Operation ID to undo (defaults to last)
op_id: Option<String>,
/// Auto-approve prompts
#[arg(long)]
yes: bool,
/// Don't restore remote refs (local only)
#[arg(long)]
no_push: bool,
/// Suppress extra output
#[arg(long)]
quiet: bool,
},
/// Redo the last undone stax operation
Redo {
/// Operation ID to redo (defaults to last)
op_id: Option<String>,
/// Auto-approve prompts
#[arg(long)]
yes: bool,
/// Don't restore remote refs (local only)
#[arg(long)]
no_push: bool,
/// Suppress extra output
#[arg(long)]
quiet: bool,
},
/// Manage parallel AI agent worktrees
#[command(subcommand, visible_alias = "ag")]
Agent(AgentCommands),
/// Manage worktrees for parallel branch development
#[command(subcommand, visible_alias = "wt")]
Worktree(WorktreeCommands),
/// Output shell integration snippet (add to shell config: eval "$(stax shell-setup)")
#[command(name = "shell-setup")]
ShellSetup {
/// Auto-append to your shell config file (~/.zshrc, ~/.bashrc, etc.)
#[arg(long)]
install: bool,
},
// Hidden top-level shortcuts for convenience
#[command(hide = true)]
Bc {
name: Option<String>,
/// Stage all changes (like git commit --all)
#[arg(short, long)]
all: bool,
#[arg(short, long)]
message: Option<String>,
/// Base branch to create from (defaults to current)
#[arg(long)]
from: Option<String>,
/// Override branch prefix (e.g. "feature/")
#[arg(long)]
prefix: Option<String>,
},
#[command(hide = true)]
Bu {
/// Number of branches to move up
count: Option<usize>,
},
#[command(hide = true)]
Bd {
/// Number of branches to move down
count: Option<usize>,
},
#[command(hide = true)]
Bs {
#[command(flatten)]
submit: SubmitOptions,
},
/// List worktrees (alias for `stax worktree list`)
#[command(hide = true)]
W,
#[command(hide = true)]
Wtc {
branch: Option<String>,
#[arg(long)]
name: Option<String>,
},
#[command(hide = true)]
Wtls,
#[command(hide = true)]
Wtgo { name: String },
#[command(hide = true)]
Wtrm {
name: String,
#[arg(short, long)]
force: bool,
},
}
#[derive(Subcommand, Clone)]
enum AuthSubcommand {
/// Show which auth source is currently active
Status,
}
#[derive(Subcommand)]
enum BranchCommands {
/// Create a new branch stacked on current
#[command(visible_alias = "c")]
Create {
/// Name for the new branch
name: Option<String>,
/// Stage all changes (like git commit --all)
#[arg(short, long)]
all: bool,
/// Commit message (also used as branch name if no name provided)
#[arg(short, long)]
message: Option<String>,
/// Base branch to create from (defaults to current)
#[arg(long)]
from: Option<String>,
/// Override branch prefix (e.g. "feature/")
#[arg(long)]
prefix: Option<String>,
},
/// Checkout a branch in the stack
#[command(visible_alias = "co")]
Checkout {
/// Branch name (interactive if not provided)
branch: Option<String>,
/// Jump directly to trunk
#[arg(long)]
trunk: bool,
/// Jump to parent of current branch
#[arg(long)]
parent: bool,
/// Jump to child branch by index (1-based)
#[arg(long)]
child: Option<usize>,
},
/// Track an existing branch (set its parent)
Track {
/// Parent branch name
#[arg(short, long, conflicts_with = "all_prs")]
parent: Option<String>,
/// Track all open PRs authored by you
#[arg(long)]
all_prs: bool,
},
/// Stop tracking a branch (remove stax metadata only)
#[command(visible_alias = "ut")]
Untrack {
/// Branch to untrack (defaults to current branch)
branch: Option<String>,
},
/// Change the parent of a tracked branch
Reparent {
/// Branch to reparent (defaults to current)
#[arg(short, long)]
branch: Option<String>,
/// New parent branch name
#[arg(short, long)]
parent: Option<String>,
},
/// Rename the current branch
#[command(visible_alias = "r")]
Rename {
/// New branch name (interactive if not provided)
name: Option<String>,
/// Edit the commit message
#[arg(short, long)]
edit: bool,
/// Push new branch and delete old remote (non-interactive)
#[arg(short, long)]
push: bool,
/// Use name literally without applying prefix
#[arg(long, hide = true)]
literal: bool,
},
/// Delete a branch and its metadata
#[command(visible_alias = "d")]
Delete {
/// Branch to delete
branch: Option<String>,
/// Force delete even if not merged
#[arg(short, long)]
force: bool,
},
/// Squash all commits on current branch into one
#[command(visible_alias = "sq")]
Squash {
/// Commit message for the squashed commit
#[arg(short, long)]
message: Option<String>,
/// Skip confirmation prompt
#[arg(long)]
yes: bool,
},
/// Fold current branch into its parent
#[command(visible_alias = "f")]
Fold {
/// Keep the branch after folding (don't delete)
#[arg(short, long)]
keep: bool,
/// Skip confirmation prompt
#[arg(long)]
yes: bool,
},
/// Move up the stack (to child branch)
#[command(visible_alias = "u")]
Up {
/// Number of branches to move up (default: 1)
count: Option<usize>,
},
/// Move down the stack (to parent branch)
Down {
/// Number of branches to move down (default: 1)
count: Option<usize>,
},
/// Move to the top of the stack (tip/leaf branch)
Top,
/// Move to the bottom of the stack (first branch above trunk)
Bottom,
/// Submit the current branch only
Submit {
#[command(flatten)]
submit: SubmitOptions,
},
}
#[derive(Subcommand)]
enum UpstackCommands {
/// Restack all branches above current
Restack {
/// Auto-stash and auto-pop dirty target worktrees during restack operations
#[arg(long)]
auto_stash_pop: bool,
},
/// Submit current branch and descendants
Submit {
#[command(flatten)]
submit: SubmitOptions,
},
}
#[derive(Subcommand)]
enum DownstackCommands {
/// Show branches below current
Get,
/// Submit ancestors and current branch
Submit {
#[command(flatten)]
submit: SubmitOptions,
},
}
#[derive(Subcommand)]
enum AgentCommands {
/// Create a new agent worktree + stacked branch
Create {
/// Human title — slugified into branch name and folder (e.g. "Add dark mode")
title: String,
/// Base branch to create from (defaults to current)
#[arg(long)]
base: Option<String>,
/// Stack on this branch (alias for --base)
#[arg(long)]
stack_on: Option<String>,
/// Open the worktree in the default editor after creation
#[arg(long)]
open: bool,
/// Open in Cursor after creation
#[arg(long)]
open_cursor: bool,
/// Open in Codex after creation
#[arg(long)]
open_codex: bool,
/// Skip post-create hook even if configured
#[arg(long)]
no_hook: bool,
},
/// Open (reattach to) an agent worktree in the editor
#[command(visible_alias = "attach")]
Open {
/// Name or slug of the worktree (interactive picker if omitted)
name: Option<String>,
},
/// List all registered agent worktrees
#[command(visible_aliases = ["ls"])]
List,
/// Register the current directory as a managed agent worktree
Register,
/// Remove an agent worktree (and optionally its branch)
Remove {
/// Name or slug of the worktree (interactive picker if omitted)
name: Option<String>,
/// Force removal even if the worktree has uncommitted changes
#[arg(short, long)]
force: bool,
/// Also delete the branch and its stax metadata
#[arg(long)]
delete_branch: bool,
},
/// Remove stale registry entries and run git worktree prune
Prune,
/// Restack all registered agent worktrees
Sync,
}
#[derive(Subcommand)]
enum WorktreeCommands {
/// Create a new worktree for a branch
#[command(visible_alias = "c")]
Create {
/// Branch name (interactive picker if omitted)
branch: Option<String>,
/// Override the short name for the worktree directory
#[arg(long)]
name: Option<String>,
},
/// List all worktrees
#[command(visible_aliases = ["ls"])]
List,
/// Navigate to a worktree (requires shell integration)
Go { name: String },
/// Print the absolute path of a worktree (for scripting / shell integration)
Path { name: String },
/// Remove a worktree
#[command(visible_alias = "rm")]
Remove {
name: String,
/// Force removal even if the worktree has uncommitted changes
#[arg(short, long)]
force: bool,
},
}
fn run_submit(submit: SubmitOptions, scope: commands::submit::SubmitScope) -> Result<()> {
commands::submit::run(
scope,
submit.draft,
submit.no_pr,
submit.no_fetch,
submit.force,
submit.yes,
submit.no_prompt,
submit.reviewers,
submit.labels,
submit.assignees,
submit.quiet,
submit.open,
submit.verbose,
submit.template,
submit.no_template,
submit.edit,
submit.ai_body,
submit.rerequest_review,
)
}
pub fn run() -> Result<()> {
// Ensure config exists (creates default on first run)
let _ = Config::ensure_exists();
let cli = Cli::parse();
// No command = launch TUI
let command = match cli.command {
Some(cmd) => cmd,
None => {
// TUI requires initialized repo
commands::init::ensure_initialized()?;
let result = tui::run();
update::show_update_notification();
update::check_in_background();
return result;
}
};
// Commands that don't need repo initialization
match &command {
Commands::Auth {
token,
from_gh,
command,
} => {
if command.is_some() && (token.is_some() || *from_gh) {
anyhow::bail!("`stax auth status` cannot be combined with --token or --from-gh.");
}
let result = match command {
Some(AuthSubcommand::Status) => commands::auth::status(),
None => commands::auth::run(token.clone(), *from_gh),
};
update::show_update_notification();
update::check_in_background();
return result;
}
Commands::Config {
reset_ai,
no_prompt,
yes,
} => {
let result = commands::config::run(*reset_ai, *no_prompt, *yes);
update::show_update_notification();
update::check_in_background();
return result;
}
Commands::Init { trunk } => {
let result = commands::init::run(trunk.clone());
update::show_update_notification();
update::check_in_background();
return result;
}
Commands::Doctor => {
let result = commands::doctor::run();
update::show_update_notification();
update::check_in_background();
return result;
}
Commands::Demo => {
let result = commands::demo::run();
update::show_update_notification();
update::check_in_background();
return result;
}
_ => {}
}
// Ensure repo is initialized for all other commands
commands::init::ensure_initialized()?;
let result = match command {
Commands::Status {
json,
stack,
current,
compact,
quiet,
} => commands::status::run(json, stack, current, compact, quiet, false),
Commands::Ll {
json,
stack,
current,
compact,
quiet,
} => commands::status::run(json, stack, current, compact, quiet, true),
Commands::Log {
json,
stack,
current,
compact,
quiet,
} => commands::log::run(json, stack, current, compact, quiet),
Commands::Submit { submit } => run_submit(submit, commands::submit::SubmitScope::Stack),
Commands::Merge {
all,
dry_run,
method,
no_delete,
no_wait,
timeout,
when_ready,
interval,
no_sync,
yes,
quiet,
} => {
let merge_method = method.parse().unwrap_or_default();
if when_ready {
commands::merge_when_ready::run(
all,
merge_method,
timeout,
interval,
no_delete,
no_sync,
yes,
quiet,
)
} else {
commands::merge::run(
all,
dry_run,
merge_method,
no_delete,
no_wait,
timeout,
no_sync,
yes,
quiet,
)
}
}
Commands::MergeWhenReady {
all,
method,
timeout,
interval,
no_delete,
no_sync,
yes,
quiet,
} => {
let merge_method = method.parse().unwrap_or_default();
commands::merge_when_ready::run(
all,
merge_method,
timeout,
interval,
no_delete,
no_sync,
yes,
quiet,
)
}
Commands::Sync {
restack,
prune,
no_delete,
delete_upstream_gone,
force,
safe,
r#continue,
quiet,
verbose,
auto_stash_pop,
} => commands::sync::run(
restack,
prune,
!no_delete,
delete_upstream_gone,
force,
safe,
r#continue,
quiet,
verbose,
auto_stash_pop,
),
Commands::Restack {
all,
r#continue,
dry_run,
yes,
quiet,
auto_stash_pop,
submit_after,
} => commands::restack::run(
all,
r#continue,
dry_run,
yes,
quiet,
auto_stash_pop,
submit_after.into(),
),
Commands::Cascade {
no_pr,
no_submit,
auto_stash_pop,
} => commands::cascade::run(no_pr, no_submit, auto_stash_pop),
Commands::Checkout {
branch,
trunk,
parent,
child,
} => commands::checkout::run(branch, trunk, parent, child),
Commands::Continue => commands::continue_cmd::run(),
Commands::Resolve {
agent,
model,
max_rounds,
} => commands::resolve::run(agent, model, max_rounds),
Commands::Abort => commands::abort::run(),
Commands::Modify { message, quiet } => commands::modify::run(message, quiet),
Commands::Auth { .. } => unreachable!(), // Handled above
Commands::Config { .. } => unreachable!(), // Handled above
Commands::Init { .. } => unreachable!(), // Handled above
Commands::Diff { stack, all } => commands::diff::run(stack, all),
Commands::RangeDiff { stack, all } => commands::range_diff::run(stack, all),
Commands::Doctor => unreachable!(), // Handled above
Commands::Trunk => commands::checkout::run(None, true, false, None),
Commands::Up { count } => commands::navigate::up(count),
Commands::Down { count } => commands::navigate::down(count),
Commands::Top => commands::navigate::top(),
Commands::Bottom => commands::navigate::bottom(),
Commands::Prev => commands::navigate::prev(),
Commands::Create {
name,
all,
message,
from,
prefix,
} => commands::branch::create::run(name, message, from, prefix, all),
Commands::Pr => commands::pr::run(),
Commands::Open => commands::open::run(),
Commands::Comments { plain } => commands::comments::run(plain),
Commands::Ci {
all,
stack,
json,
refresh,
watch,
interval,
verbose,
} => commands::ci::run(all, stack, json, refresh, watch, interval, verbose),
Commands::Split => commands::split::run(),
Commands::Copy { pr } => {
let target = if pr {
commands::copy::CopyTarget::Pr
} else {
commands::copy::CopyTarget::Branch
};
commands::copy::run(target)
}
Commands::Detach { branch, yes } => commands::detach::run(branch, yes),
Commands::Reorder { yes } => commands::reorder::run(yes),
Commands::Validate => commands::stack_cmd::run_validate(),
Commands::Fix { dry_run, yes } => commands::stack_cmd::run_fix(dry_run, yes),
Commands::Run {
cmd,
all,
stack,
fail_fast,
}
| Commands::Test {
cmd,
all,
stack,
fail_fast,
} => commands::stack_cmd::run_test(cmd, all, stack, fail_fast),
Commands::Demo => unreachable!(), // Handled above
Commands::Standup {
json,
all,
hours,
summary,
jit,
agent,
plain_text,
} => commands::standup::run(json, all, hours, summary, jit, agent, plain_text),
Commands::Generate {
pr_body,
edit,
no_prompt,
agent,
model,
} => {
if !pr_body {
anyhow::bail!("Please specify what to generate. Usage: stax generate --pr-body");
}
commands::generate::run(edit, no_prompt, agent, model)
}
Commands::Changelog {
from,
to,
path,
json,
} => commands::changelog::run(from, to, path, json),
Commands::Rename {
name,
edit,
push,
literal,
} => commands::branch::rename::run(name, edit, push, literal),
Commands::Undo {
op_id,
yes,
no_push,
quiet,
} => commands::undo::run(op_id, yes, no_push, quiet),
Commands::Redo {
op_id,
yes,
no_push,
quiet,
} => commands::redo::run(op_id, yes, no_push, quiet),
Commands::Branch(cmd) => match cmd {
BranchCommands::Create {
name,
all,
message,
from,
prefix,
} => commands::branch::create::run(name, message, from, prefix, all),
BranchCommands::Checkout {
branch,
trunk,
parent,
child,
} => commands::checkout::run(branch, trunk, parent, child),
BranchCommands::Track { parent, all_prs } => {
commands::branch::track::run(parent, all_prs)
}
BranchCommands::Untrack { branch } => commands::branch::untrack::run(branch),
BranchCommands::Reparent { branch, parent } => {
commands::branch::reparent::run(branch, parent)
}
BranchCommands::Rename {
name,
edit,
push,
literal,
} => commands::branch::rename::run(name, edit, push, literal),
BranchCommands::Delete { branch, force } => {
commands::branch::delete::run(branch, force)
}
BranchCommands::Squash { message, yes } => commands::branch::squash::run(message, yes),
BranchCommands::Fold { keep, yes } => commands::branch::fold::run(keep, yes),
BranchCommands::Up { count } => commands::navigate::up(count),
BranchCommands::Down { count } => commands::navigate::down(count),
BranchCommands::Top => commands::navigate::top(),
BranchCommands::Bottom => commands::navigate::bottom(),
BranchCommands::Submit { submit } => {
run_submit(submit, commands::submit::SubmitScope::Branch)
}
},
Commands::Upstack(cmd) => match cmd {
UpstackCommands::Restack { auto_stash_pop } => {
commands::upstack::restack::run(auto_stash_pop)
}
UpstackCommands::Submit { submit } => {
run_submit(submit, commands::submit::SubmitScope::Upstack)
}
},
Commands::Downstack(cmd) => match cmd {
DownstackCommands::Get => {
commands::status::run(false, None, false, false, false, false)
}
DownstackCommands::Submit { submit } => {
run_submit(submit, commands::submit::SubmitScope::Downstack)
}
},
// Hidden shortcuts
Commands::Bc {
name,
all,
message,
from,
prefix,
} => commands::branch::create::run(name, message, from, prefix, all),
Commands::Bu { count } => commands::navigate::up(count),
Commands::Bd { count } => commands::navigate::down(count),
Commands::Bs { submit } => run_submit(submit, commands::submit::SubmitScope::Branch),
Commands::Agent(cmd) => match cmd {
AgentCommands::Create {
title,
base,
stack_on,
open,
open_cursor,
open_codex,
no_hook,
} => commands::agent::create::run(
title,
base,
stack_on,
open,
open_cursor,
open_codex,
no_hook,
),
AgentCommands::Open { name } => commands::agent::open::run(name),
AgentCommands::List => commands::agent::list::run(),
AgentCommands::Register => commands::agent::register::run(),
AgentCommands::Remove {
name,
force,
delete_branch,
} => commands::agent::remove::run(name, force, delete_branch),
AgentCommands::Prune => commands::agent::prune::run(),
AgentCommands::Sync => commands::agent::sync::run(),
},
Commands::Worktree(cmd) => match cmd {
WorktreeCommands::Create { branch, name } => {
commands::worktree::create::run(branch, name)
}
WorktreeCommands::List => commands::worktree::list::run(),
WorktreeCommands::Go { name } => commands::worktree::go::run_go(&name),
WorktreeCommands::Path { name } => commands::worktree::go::run_path(&name),
WorktreeCommands::Remove { name, force } => {
commands::worktree::remove::run(&name, force)
}
},
Commands::ShellSetup { install } => commands::shell_setup::run(install),
// Hidden worktree shortcuts
Commands::W => commands::worktree::list::run(),
Commands::Wtc { branch, name } => commands::worktree::create::run(branch, name),
Commands::Wtls => commands::worktree::list::run(),
Commands::Wtgo { name } => commands::worktree::go::run_path(&name),
Commands::Wtrm { name, force } => commands::worktree::remove::run(&name, force),
};
// Show update notification (from cache, instant) and spawn background check for next run
update::show_update_notification();
update::check_in_background();
result
}