gitbucket-cli 0.6.1

A command-line tool for GitBucket, inspired by GitHub CLI
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
# GitBucket CLI (`gb`) Specification

## 1. Overview

`gb` is a command-line tool for operating GitBucket from the terminal.
It follows the command design style of GitHub CLI (`gh`) and uses the GitBucket REST API (`/api/v3`).

- **Command name:** `gb`
- **Language:** Rust (edition 2021)
- **Target platforms:** Linux / macOS / Windows
- **License:** MIT

---

## 2. Design principles

### 2.1 Compatibility with GitHub CLI concepts

| GitHub CLI | GitBucket CLI | Status | Notes |
| --- | --- | --- | --- |
| `gh auth` | `gb auth` | ✅ Implemented | PAT-based authentication |
| `gh repo` | `gb repo` | ✅ Implemented | Core repo operations |
| `gh issue` | `gb issue` | ✅ Implemented | Issue create/view/edit/comment/state workflows |
| `gh pr` | `gb pr` | ✅ Implemented | PR create/view/edit/comment/state/merge/worktree workflows |
| `gh browse` | `gb browse` | ✅ Implemented | Opens browser |
| `gh label` | `gb label` | ✅ Implemented | Label definition CRUD |
| `gh api` | `gb api` | ✅ Implemented | Raw REST API access |
| `gh config` | `gb config` | ✅ Implemented | Local config inspection and updates |
| `gh completion` | `gb completion` | ✅ Implemented | Shell completion generation |
| `gh gist` / `gh project` / `gh codespace` | — | ❌ Out of scope | No corresponding GitBucket feature/API |
| `gh run` / `gh workflow` / `gh cache` | — | ❌ Out of scope | No Actions-like CI feature in GitBucket core |

### 2.2 GitBucket-specific opportunities (planned)

| Command | Description | Status |
| --- | --- | --- |
| `gb milestone` | Milestone management | ✅ Implemented | REST read + GitBucket-compatible write fallback |
| `gb user` | User/admin workflows | 💤 Backlog |
| `gb webhook` | Webhook management | 💤 Backlog |
| `gb repo collaborator` | Collaborator management | 💤 Backlog |

---

## 3. Command reference

### 3.1 Global options

Available on all commands:

| Option | Short | Env var | Description |
| --- | --- | --- | --- |
| `--hostname <HOST>` | `-H` | `GB_HOST` | GitBucket host or base URL (e.g. `gitbucket.example.com` or `https://gitbucket.example.com/gitbucket`) |
| `--repo <OWNER/REPO>` | `-R` | `GB_REPO` | Target repository |
| `--profile <NAME>` | — | `GB_PROFILE` | Configuration profile |
| `--json-errors` | — | `GB_JSON_ERRORS` | Print command failures as structured JSON on stderr |
| `--help` | `-h` | — | Show help |
| `--version` | `-V` | — | Show version |

---

### 3.2 `gb auth` — authentication

#### `gb auth login`

Authenticate to a GitBucket instance.

```text
gb auth login [OPTIONS]
```

| Option | Short | Default | Description |
| --- | --- | --- | --- |
| `--hostname <HOST>` | `-H` | interactive prompt | Hostname or base URL |
| `--token <TOKEN>` | `-t` | interactive prompt | Personal access token |
| `--protocol <PROTOCOL>` | — | `https` | `https` or `http` |

Behavior:

1. Prompt for hostname/token when omitted.
2. Validate credentials by calling `GET /user`.
3. Save the host profile to `config.toml` on success and mark it as the default host.

Examples:

```bash
gb auth login
gb auth login -H gitbucket.example.com -t <TOKEN>
gb auth login -H localhost:8080 --protocol http
gb auth login -H https://gitbucket.example.com/gitbucket -t <TOKEN>
```

#### `gb auth logout`

Remove saved credentials for a host.

```text
gb auth logout [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--hostname <HOST>` | `-H` | Host to remove (defaults to current default host) |

#### `gb auth status`

Show current authentication entries.

```text
gb auth status [--json]
```

`--profile <NAME>` limits output to that profile and prints the profile's effective actor. `--json` prints structured output with a top-level `effective_actor` object containing `host`, `user`, `protocol`, and `credential_source`.

Example output:

```text
gitbucket.example.com
  ✓ Logged in as alice
  Protocol: https
```

Profile example:

```text
Profile: work
  Default host: gitbucket.example.com
  Default repo: alice/project
  Effective actor: alice @ gitbucket.example.com (profile credentials)
```

#### `gb auth token`

Print token for script integration.

```text
gb auth token [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--hostname <HOST>` | `-H` | Host to print token for |

---

### 3.3 `gb config` — local configuration

`gb config` manages the local CLI configuration file and saved host metadata.
It operates on stored values only and does not read `GB_HOST`, `GB_TOKEN`, or other runtime overrides.

#### `gb config path`

```text
gb config path
```

Print the resolved path to `config.toml`.

#### `gb config list`

```text
gb config list [--json]
```

Behavior:

- Prints the config file path
- Prints the stored `default_host` and `default_profile` when set
- Lists saved host and profile entries with `user`, `protocol`, and whether a token is configured
- Redacts token values; use `gb auth token` when the raw token is needed

#### `gb config get default-host`

```text
gb config get default-host
```

Print the stored `default_host` value.

#### `gb config get default-profile`

```text
gb config get default-profile
```

Print the stored `default_profile` value.

#### `gb config get host`

```text
gb config get host --host <HOST> [--field <FIELD>] [--json]
```

| Option | Description |
| --- | --- |
| `--host <HOST>` | Host or base URL to inspect |
| `--field <FIELD>` | `user`, `protocol`, or `has-token` |
| `--json` | Output the full host summary as JSON |

Host lookup is canonical, so equivalent forms such as `https://host/path`, `host/path`, and `https://host/path/api/v3` resolve to the same saved entry.

#### `gb config get profile`

```text
gb config get profile <NAME> [--json]
```

Print profile defaults and profile-scoped saved hosts. Token values are redacted.

#### `gb config set default-host`

```text
gb config set default-host <HOST>
```

Set the stored default host. The target host must already exist in saved config.

#### `gb config set default-profile`

```text
gb config set default-profile <NAME>
```

Set the stored default profile. The target profile must already exist in saved config.

#### `gb config set host`

```text
gb config set host --host <HOST> [--user <USER>] [--protocol <PROTOCOL>] [--default]
```

| Option | Description |
| --- | --- |
| `--host <HOST>` | Host or base URL to update |
| `--user <USER>` | Username stored for GitBucket web-session fallbacks |
| `--protocol <PROTOCOL>` | Stored protocol (`http` or `https`) |
| `--default` | Also make this host the stored default host |

The target host must already exist in saved config.

#### `gb config set profile`

```text
gb config set profile <NAME> [--default-host <HOST>] [--default-repo <OWNER/REPO>]
```

Create or update a saved profile. `--default-repo` must use `OWNER/REPO` format.

#### `gb config unset default-host`

```text
gb config unset default-host
```

Clear the stored `default_host` value.

#### `gb config unset default-profile`

```text
gb config unset default-profile
```

Clear the stored `default_profile` value.

---

### 3.4 `gb api` — raw REST API access

`gb api` calls the GitBucket REST API directly using the configured host and token.
It is intended as a low-level escape hatch for endpoints that do not yet have a dedicated top-level command.

```text
gb api <ENDPOINT> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--method <METHOD>` | `-X` | HTTP method to use (`GET` by default, or `POST` when `--input` is present) |
| `--input <PATH>` | `-i` | JSON request body file path, or `-` to read JSON from stdin |

Behavior:

- Relative endpoints are resolved under `/api/v3`, so `user` becomes `/api/v3/user`
- Paths prefixed with `/api/v3` are accepted without duplicating the prefix
- Absolute URLs are allowed only when they stay under the configured GitBucket API base
- Successful JSON responses are printed to stdout as pretty JSON
- Empty success responses print `null`

Examples:

```bash
gb api user
gb api /api/v3/user
gb api repos/alice/project/issues --input body.json
echo '{"state":"closed"}' | gb api repos/alice/project/issues/1 -X PATCH --input -
```

---

### 3.5 `gb completion` — shell completion generation

`gb completion` prints shell completion scripts to stdout.

```text
gb completion <SHELL>
```

Supported shells:

- `bash`
- `zsh`
- `fish`
- `powershell`

Behavior:

- Generates completions from the current CLI command tree
- Writes the script to stdout
- Does not install or source the script automatically

Examples:

```bash
gb completion bash > ~/.local/share/bash-completion/completions/gb
gb completion zsh > ~/.zfunc/_gb
```

---

### 3.6 `gb repo` — repository operations

#### `gb repo list`

List repositories.

```text
gb repo list [OWNER] [OPTIONS]
```

| Argument/Option | Description |
| --- | --- |
| `OWNER` | User or group name (omit to list your repos) |
| `--json` | Print JSON |

Table output columns:

- `NAME`
- `DESCRIPTION`
- `VISIBILITY`

#### `gb repo view`

Show repository details.

```text
gb repo view [OWNER/REPO] [OPTIONS]
```

| Argument/Option | Short | Description |
| --- | --- | --- |
| `OWNER/REPO` | — | Repository (omit to auto-detect from git remote or pass with `-R/--repo`) |
| `--repo <OWNER/REPO>` | `-R` | Target repository |
| `--web` | `-w` | Open in browser |

#### `gb repo create`

Create a repository.

```text
gb repo create [NAME] [OPTIONS]
```

| Argument/Option | Short | Default | Description |
| --- | --- | --- | --- |
| `NAME` | — | interactive prompt | Repository name |
| `--description <TEXT>` | `-d` | none | Repository description |
| `--private` | — | `false` | Create as private |
| `--add-readme` | — | `false` | Auto-initialize README |
| `--group <GROUP>` | — | none | Create under a group (`--org` remains accepted as an alias) |

#### `gb repo clone`

Clone a repository with `git clone`.

```text
gb repo clone <REPO> [DIRECTORY]
```

`<REPO>` supports either:

- `OWNER/REPO`
- Full clone URL

#### `gb repo delete`

Delete a repository.

```text
gb repo delete [OWNER/REPO] [OPTIONS]
```

| Argument/Option | Description |
| --- | --- |
| `OWNER/REPO` | Repository to delete (explicit repository required; or pass globally with `gb -R OWNER/REPO repo delete`) |
| `--yes` | Skip confirmation prompt |

#### `gb repo fork`

Fork a repository.

```text
gb repo fork [OWNER/REPO] [OPTIONS]
```

| Argument/Option | Short | Description |
| --- | --- | --- |
| `OWNER/REPO` | — | Repository to fork (or pass globally with `gb -R OWNER/REPO repo fork`) |
| `--group <GROUP>` | — | Group to fork into (defaults to your user; `--org` remains accepted as an alias) |

Implementation detail: if GitBucket returns `404` for the REST fork endpoint, `gb` falls back to the web fork flow and signs in with the configured username or `GB_USER`.

`gb repo delete` behaves similarly: if GitBucket returns `404` for `DELETE /repos/{owner}/{repo}`, `gb` retries through the web danger-zone form.

---

### 3.7 `gb label` — label operations

`gb label` manages repository label definitions in the target repository.

#### `gb label list`

```text
gb label list [--json]
```

Behavior:

- Lists labels in the current repository
- Human output prints `NAME`, `COLOR`, and `DESCRIPTION`
- `--json` prints the raw API payload

#### `gb label view`

```text
gb label view <NAME>
```

Shows the label name, color, description when present, and API URL when present.

#### `gb label create`

```text
gb label create [NAME] [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--color <HEX>` | `-c` | 6-digit hex color (prompted when omitted) |
| `--description <TEXT>` | `-d` | Optional label description |

Behavior:

- Prompts for the label name when omitted
- Accepts colors with or without a leading `#`
- Normalizes colors to lowercase 6-digit hex before sending the API request

#### `gb label edit`

```text
gb label edit <NAME> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--name <NAME>` | — | Rename the label |
| `--color <HEX>` | `-c` | New 6-digit hex color |
| `--description <TEXT>` | `-d` | New label description |
| `--remove-description` | — | Clear the label description |

Behavior:

- Requires at least one change option
- Accepts colors with or without a leading `#`
- Normalizes colors to lowercase 6-digit hex before sending the API request
- Percent-encodes the current label name in the API path

#### `gb label delete`

```text
gb label delete <NAME> [OPTIONS]
```

| Option | Description |
| --- | --- |
| `--yes` | Skip confirmation prompt |

---

### 3.8 `gb milestone` — milestone operations

`gb milestone` manages repository milestones in the target repository.

#### `gb milestone list`

```text
gb milestone list [OPTIONS]
```

| Option | Short | Default | Description |
| --- | --- | --- | --- |
| `--state <STATE>` | `-s` | `open` | Filter: `open`, `closed`, `all` |
| `--json` | — | `false` | Print JSON |

Human output columns:

- `#`
- `STATE`
- `TITLE`
- `DUE`
- `OPEN`
- `CLOSED`

#### `gb milestone view`

```text
gb milestone view <NUMBER>
```

Shows the milestone title, state, due date when present, issue counts, description, and URL when present.

#### `gb milestone create`

```text
gb milestone create [TITLE] [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--description <TEXT>` | `-d` | Optional milestone description |
| `--due-on <DATE>` | — | Due date as `YYYY-MM-DD` or RFC3339 |

Behavior:

- Prompts for the title when omitted
- Normalizes `--due-on` to GitBucket-compatible API/web values
- Falls back to the GitBucket web milestone form when the REST create endpoint returns `404`

#### `gb milestone edit`

```text
gb milestone edit <NUMBER> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--title <TEXT>` | `-t` | Updated title |
| `--description <TEXT>` | `-d` | Updated description |
| `--due-on <DATE>` | — | Updated due date as `YYYY-MM-DD`, RFC3339, or an empty string to clear |
| `--state <STATE>` | `-s` | Updated state: `open` or `closed` |

Behavior:

- Requires at least one explicit change
- Falls back to the GitBucket web milestone edit/state routes when the REST update endpoint returns `404`

#### `gb milestone delete`

```text
gb milestone delete <NUMBER> [OPTIONS]
```

| Option | Description |
| --- | --- |
| `--yes` | Skip confirmation prompt |

Behavior:

- Tries `DELETE /repos/{owner}/{repo}/milestones/{number}` first
- Falls back to the GitBucket web delete route when the REST delete endpoint returns `404`

---

### 3.9 `gb issue` — issue operations

#### `gb issue list`

List issues in the target repository.

```text
gb issue list [OPTIONS]
```

| Option | Short | Default | Description |
| --- | --- | --- | --- |
| `--state <STATE>` | `-s` | `open` | Filter: `open`, `closed`, `all` |
| `--json` | — | `false` | Print JSON |
| `--no-pager` | — | `false` | Do not use a pager |

Table output columns:

- `#`
- `STATE`
- `TITLE`
- `AUTHOR`
- `LABELS`

#### `gb issue view`

View issue details.

```text
gb issue view <NUMBER> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--comments` | `-c` | Include comments |
| `--web` | `-w` | Open in browser |
| `--json` | — | Print JSON |
| `--no-pager` | — | Do not use a pager |

Shows title, state, author, created date, labels, assignees, milestone when present, body, and optional comments.
Comments are hidden by default; pass `--comments` to include them in the CLI output.

#### `gb issue create`

Create an issue.

```text
gb issue create [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--title <TEXT>` | `-t` | Issue title (prompted when omitted) |
| `--body <TEXT>` | `-b` | Issue body |
| `--label <LABEL>` | `-l` | Repeatable or comma-separated label option |
| `--assignee <USER>` | `-a` | Repeatable or comma-separated assignee option |

#### `gb issue edit`

Edit an issue.

```text
gb issue edit <NUMBER> [OPTIONS]
```

| Option | Description |
| --- | --- |
| `--title <TEXT>` | Updated title |
| `--body <TEXT>` | Updated body |
| `--add-label <LABEL>` | Add a label; repeatable or comma-separated |
| `--remove-label <LABEL>` | Remove a label; repeatable or comma-separated |
| `--add-assignee <USER>` | Add an assignee; repeatable or comma-separated |
| `--remove-assignee <USER>` | Remove an assignee; repeatable or comma-separated |
| `--milestone <NUMBER>` | Set the milestone |
| `--remove-milestone` | Clear the current milestone |
| `--state <STATE>` | Updated state: `open` or `closed` |

Behavior:

- Requires at least one explicit change
- Fetches the current issue first so label/assignee changes can be merged safely
- Rejects `--milestone` and `--remove-milestone` together before any API call
- If GitBucket does not support REST issue editing and `gb` falls back to the web UI flow, title/body/assignee/milestone/state updates are supported; label edits still require REST issue edit support

#### `gb issue close`

```text
gb issue close <NUMBER>
```

Implementation detail: if GitBucket returns `404` for the REST issue update endpoint, `gb` falls back to the web issue state flow and signs in with the configured username or `GB_USER`.

#### `gb issue reopen`

```text
gb issue reopen <NUMBER>
```

Implementation detail: same fallback behavior as `gb issue close`.

#### `gb issue comment`

```text
gb issue comment <NUMBER> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--body <TEXT>` | `-b` | Comment body (prompted when omitted) |
| `--edit-last` | — | Edit your last comment instead of adding a new one |

---

### 3.10 `gb pr` — pull request operations

#### `gb pr list`

List pull requests.

```text
gb pr list [OPTIONS]
```

| Option | Short | Default | Description |
| --- | --- | --- | --- |
| `--state <STATE>` | `-s` | `open` | Filter: `open`, `closed`, `all` |
| `--json` | — | `false` | Print JSON |
| `--no-pager` | — | `false` | Do not use a pager |

With `--state open --json`, `gb` fetches each listed pull request detail before printing JSON so fork `head`/`base` repository information is as reliable as `gb pr view --json`. The issue-listing recovery for GitBucket versions that omit repository-visible open PRs from `/pulls` still applies.

Table output columns:

- `#`
- `STATE`
- `TITLE`
- `BRANCH`
- `AUTHOR`

#### `gb pr view`

```text
gb pr view <NUMBER> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--comments` | `-c` | Include comments |
| `--web` | `-w` | Open in browser |
| `--json` | — | Print JSON |
| `--no-pager` | — | Do not use a pager |

With `--json`, `gb` prints the pull request API payload. When `--comments` is also passed, the JSON object includes a top-level `comments` array fetched from the PR comments endpoint.

#### `gb pr create`

Create a pull request.

```text
gb pr create [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--title <TEXT>` | `-t` | PR title (prompted when omitted) |
| `--body <TEXT>` | `-b` | PR body |
| `--head <BRANCH>` | — | Head branch (uses current git branch when omitted). For cross-repo PRs, pass `OWNER:BRANCH` or use `--head-owner OWNER --head BRANCH` |
| `--head-owner <OWNER>` | — | Owner for the head branch; sends the head as `OWNER:BRANCH` |
| `--base <BRANCH>` | `-B` | Base branch (prompts, default `main`) |
| `--json` | — | Print the created pull request as JSON |
| `--detect-existing` | — | Return an existing open PR for the same head/base instead of creating a duplicate |

On normal text output, `gb pr create` prints the created PR number/title, resolved `Head:` and `Base:` repository refs when the API returns them, and a canonical browser URL using `/pull/{number}`.
With `--detect-existing`, `gb` checks open pull requests for a matching head/base before prompting for title/body or creating a new PR. If the pull request listing is incomplete or lacks comparable refs, `gb` also checks PRs exposed through the issue listing and fetches PR details before deciding no match exists. If create fails after that check, `gb` checks again and returns the matching open PR when one exists.

#### `gb pr edit`

Edit a pull request through the issue endpoint.

```text
gb pr edit <NUMBER> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--title <TEXT>` | `-t` | New PR title |
| `--body <TEXT>` | `-b` | New PR body |
| `--add-assignee <USER>` | — | Add an assignee; repeatable or comma-separated |
| `--remove-assignee <USER>` | — | Remove an assignee; repeatable or comma-separated |
| `--state <STATE>` | — | Update PR state: `open` or `closed` |
| `--web` | — | Allow GitBucket web UI fallback when REST PR edit is unavailable |

Implementation detail: PR title/body/state/assignee metadata is edited by calling the **issues** endpoint (`PATCH /repos/{owner}/{repo}/issues/{number}`). If GitBucket returns `404` for that REST update, `gb pr edit` fails without performing a web session. Passing `--web` explicitly allows the GitBucket web UI fallback routes for PR title/body/state/assignee updates.

#### `gb pr close`

```text
gb pr close <NUMBER>
```

Implementation detail: this operation updates PR state by calling the **issues** endpoint (`PATCH /repos/{owner}/{repo}/issues/{number}` with `state=closed`).

#### `gb pr merge`

```text
gb pr merge <NUMBER> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--message <TEXT>` | `-m` | Merge commit message |

#### `gb pr checkout`

Checkout the PR head branch locally.

```text
gb pr checkout <NUMBER>
```

Execution flow:

1. Fetch PR metadata from API.
2. Resolve the fetch source from the PR head repository clone URL when available, otherwise use `origin`.
3. Run `git fetch <fetch-source> <head-branch>`.
4. Run `git checkout -B <head-branch> FETCH_HEAD`.

#### `gb pr diff`

Show PR diff locally.

```text
gb pr diff <NUMBER> [--no-pager]
```

| Option | Short | Description |
| --- | --- | --- |
| `--no-pager` | — | Do not use a pager |

Execution flow:

1. Fetch PR metadata from API.
2. Resolve the base and head fetch sources from PR repository clone URLs when available, otherwise use matching local remotes or `origin`.
3. Run `git fetch <base-fetch-source> <base>`.
4. Run `git fetch <fetch-source> <head>`.
5. Run `git diff <base-ref>...<head-ref>`.
6. If the live branch diff is unavailable, fetch a saved PR diff from `diff_url`/`patch_url`, the issue PR metadata, or the GitBucket web diff URL.

If the API response lacks the PR head/base refs, a branch fetch fails, or a non-open PR produces an empty local diff and no saved PR diff can be fetched, `gb pr diff` exits non-zero and reports that the diff is unavailable on stderr. With `--json-errors`, the error includes `code: "diff_unavailable"` and a machine-readable `cause` such as `missing_head_ref`, `base_fetch_failed`, `head_fetch_failed`, or `empty_live_diff`.

#### `gb pr comment`

```text
gb pr comment <NUMBER> [OPTIONS]
gb pr comment list <NUMBER> [OPTIONS]
```

| Option | Short | Description |
| --- | --- | --- |
| `--body <TEXT>` | `-b` | Comment body (prompted when omitted) |
| `--edit-last` | — | Edit your last comment instead of adding a new one |
| `--json` | — | Print the created or edited comment object as JSON |
| `--no-pager` | — | Do not use a pager for `gb pr comment list` |

On normal text output, `gb pr comment` prints the comment ID. If the API returns `html_url`, it also prints the comment URL.

`gb pr comment list <NUMBER>` lists comments on the pull request. Pass `--json` to print the full comment array, which can be used to verify whether a previous comment mutation succeeded before retrying. Pass `--no-pager` to force direct stdout output.

---

### 3.11 `gb browse`

Open the repository page in your browser.

```text
gb browse
```

The repository is resolved from:

1. `--repo/-R`
2. `GB_REPO`
3. Active profile's `default_repo`
4. Git remote (`origin`)

---

## 4. Authentication and configuration

### 4.1 Authentication method

- Personal Access Token (PAT) only.
- Stored per host.
- Verified during login via `GET /user`.
- Successful login updates the saved `default_host`.
- When a profile is selected, successful login writes the host entry under that profile and updates the profile's `default_host`.
- Path-prefixed GitBucket deployments are supported by passing a base URL such as `https://gitbucket.example.com/gitbucket`.

### 4.2 Configuration file

Default path:

```text
~/.config/gb/config.toml
```

Override base directory with `GB_CONFIG_DIR`.

Example:

```toml
default_host = "https://gitbucket.example.com/gitbucket"
default_profile = "work"

[hosts."https://gitbucket.example.com/gitbucket"]
token = "your-token"
user = "alice"
protocol = "https"

[profiles.work]
default_host = "gitbucket.example.com"
default_repo = "alice/project"

[profiles.work.hosts."gitbucket.example.com"]
token = "your-work-token"
user = "alice"
protocol = "https"
```

Path-prefixed instances can also be stored as plain host-plus-path keys:

```toml
[hosts."gitbucket.example.com/gitbucket"]
token = "your-token"
user = "alice"
protocol = "https"
```

On Unix-like systems, `config.toml` is written with `0600` permissions.

### 4.3 Credential precedence

1. `GB_TOKEN` environment variable
2. Profile-scoped host entry in `config.toml`
3. Global host entry in `config.toml`

When `GB_TOKEN` is set, protocol resolution uses this order:

1. URL scheme embedded in `--hostname/-H` or `GB_HOST`
2. `GB_PROTOCOL`
3. Matching stored host configuration from `config.toml`
4. Default `https`

### 4.4 Profile resolution order

1. `--profile`
2. `GB_PROFILE`
3. Saved `default_profile` in `config.toml`
4. No active profile

If a selected profile does not exist, commands fail with a clear configuration error.

### 4.5 Hostname resolution order

1. `--hostname/-H`
2. `GB_HOST`
3. Active profile's `default_host`
4. Saved `default_host` in `config.toml` when no profile is active
5. Lexicographically first configured host in `config.toml` as a backward-compatible fallback when no profile is active

Stored hosts are matched canonically, so equivalent forms such as `https://host/path`, `host/path`, and `https://host/path/api/v3` resolve to the same saved entry.

---

## 5. Repository auto-detection

When `--repo/-R` is omitted, `gb` tries to parse `git remote get-url origin`.

### 5.1 Supported git remote formats

- HTTPS: `https://host/owner/repo.git`
- GitBucket HTTPS with `/git/`: `https://host/git/owner/repo.git`
- Path-prefixed HTTPS: `https://gitbucket.example.com/gitbucket/owner/repo.git`
- Path-prefixed GitBucket HTTPS with `/git/`: `https://gitbucket.example.com/gitbucket/git/owner/repo.git`
- SSH: `git@host:owner/repo.git`

### 5.2 Resolution order

1. `--repo/-R`
2. `GB_REPO`
3. Active profile's `default_repo`
4. `git remote get-url origin`

If parsing fails, `RepoNotFound` is returned.

---

## 6. Output formats

### 6.1 Table output (default)

- Human-readable columns with auto width.
- ANSI-aware width calculations.
- Unicode-safe truncation for long text fields.
- Colored states:
  - `OPEN` (green)
  - `CLOSED` (red)
  - `MERGED` (magenta)

### 6.2 JSON output

Available on list commands (`repo list`, `issue list`, `pr list`, `label list`, `milestone list`, `config list`) using `--json`.

### 6.3 Browser output

Available on view/browse flows using `--web` or `gb browse`.

### 6.4 Web fallback notices

When a command falls back from a missing REST API endpoint to a GitBucket web UI route, `gb` writes a `Notice:` line to stderr before performing the web session flow. Unsupported fields must return an explicit error that distinguishes "REST API unavailable" from "web fallback does not support this field."

---

## 7. Environment variables

| Variable | Description |
| --- | --- |
| `GB_HOST` | Default hostname or base URL |
| `GB_REPO` | Default repository (`OWNER/REPO`) |
| `GB_PROFILE` | Default configuration profile |
| `GB_TOKEN` | Access token override |
| `GB_PROTOCOL` | Protocol override when `GB_TOKEN` is used with a plain hostname |
| `GB_USER` | Username for GitBucket web-session fallbacks |
| `GB_PASSWORD` | Password for GitBucket web-session fallbacks |
| `GB_CONFIG_DIR` | Custom config directory |
| `GB_JSON_ERRORS` | Print command failures as structured JSON on stderr |
| `NO_COLOR` | Disable colored output (terminal/toolchain dependent) |

---

## 8. Error handling and exit codes

### 8.1 Error categories (`GbError`)

- `Auth(String)`
- `Api { status, message }`
- `Config(String)`
- `NotAuthenticated`
- `RepoNotFound`
- `DiffUnavailable { number, cause, message }`
- `Http(reqwest::Error)`
- `Io(std::io::Error)`
- `Json(serde_json::Error)`
- `TomlSer(toml::ser::Error)`
- `TomlDe(toml::de::Error)`
- `UrlParse(url::ParseError)`
- `Dialoguer(dialoguer::Error)`
- `Other(String)`

### 8.2 Exit codes

- `0`: Success
- `1`: Error (`main` prints `Error: ...` and exits with code 1)

Pass global `--json-errors` (or set `GB_JSON_ERRORS=true`) to print failures as a single JSON object on stderr:

```json
{"error":{"code":"diff_unavailable","message":"Diff unavailable for pull request #9: ...","cause":"empty_live_diff","exit_code":1}}
```

---

## 9. Technical specification

### 9.1 Project layout

```text
src/
  main.rs
  error.rs
  cli/
    mod.rs
    common.rs
    auth.rs
    repo.rs
    issue.rs
    pr.rs
  api/
    mod.rs
    client.rs
    repository.rs
    issue.rs
    pull_request.rs
  models/
    mod.rs
    user.rs
    repository.rs
    issue.rs
    pull_request.rs
    comment.rs
  config/
    mod.rs
    auth.rs
  output/
    mod.rs
    table.rs
```

### 9.2 Main dependencies

| Crate | Purpose |
| --- | --- |
| `clap` | CLI parsing |
| `reqwest` | HTTP client (`rustls-tls-native-roots`) |
| `tokio` | Async runtime |
| `serde` / `serde_json` | Serialization |
| `toml` | Config encoding |
| `dialoguer` | Interactive prompts |
| `colored` | Colorized terminal output |
| `open` | Browser launching |
| `url` | URL parsing |
| `thiserror` | Error definitions |
| `dirs` | Config directory discovery |

### 9.3 API client behavior

- Base API URL: normalized to `{scheme}://{host}{optional-path}/api/v3`
- Auth header: `Authorization: token <PAT>`
- Common methods: `get`, `post`, `patch`, `put`, `delete`
- Raw request helper: `raw_request` (used by `gb api`)
- `web_url(path)`: converts API base URL to browser base URL by stripping `/api/v3`

### 9.4 Current endpoint mapping

Authentication:

- `GET /user` (token validation, login flow)

Repository:

- `GET /users/{owner}/repos`
- `GET /user/repos`
- `GET /repos/{owner}/{repo}`
- `POST /user/repos`
- `POST /orgs/{org}/repos`
- `DELETE /repos/{owner}/{repo}`
- `POST /repos/{owner}/{repo}/forks`

Issue:

- `GET /repos/{owner}/{repo}/issues?state={state}`
- `GET /repos/{owner}/{repo}/issues/{number}`
- `POST /repos/{owner}/{repo}/issues`
- `PATCH /repos/{owner}/{repo}/issues/{number}`
- `GET /repos/{owner}/{repo}/issues/{number}/comments`
- `POST /repos/{owner}/{repo}/issues/{number}/comments`
- `PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}`

Pull request:

- `GET /repos/{owner}/{repo}/pulls?state={state}`
- `GET /repos/{owner}/{repo}/pulls/{number}`
- `POST /repos/{owner}/{repo}/pulls`
- `PUT /repos/{owner}/{repo}/pulls/{number}/merge`
- `GET /repos/{owner}/{repo}/issues/{number}/comments` (PR comments)
- `POST /repos/{owner}/{repo}/issues/{number}/comments` (PR comments)
- `PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}` (PR comment edit)
- `PATCH /repos/{owner}/{repo}/issues/{number}` (PR title/body/state/assignees)
- `POST /{owner}/{repo}/issues/edit_title/{number}` (PR edit web fallback)
- `POST /{owner}/{repo}/issues/edit/{number}` (PR edit web fallback)
- `POST /{owner}/{repo}/issues/{number}/assignee/{new|delete}` (PR edit web fallback)
- `POST /{owner}/{repo}/issue_comments/state` (PR state web fallback)

Label:

- `GET /repos/{owner}/{repo}/labels`
- `GET /repos/{owner}/{repo}/labels/{name}`
- `POST /repos/{owner}/{repo}/labels`
- `PATCH /repos/{owner}/{repo}/labels/{name}`
- `DELETE /repos/{owner}/{repo}/labels/{name}`

Milestone:

- `GET /repos/{owner}/{repo}/milestones?state={state}`
- `GET /repos/{owner}/{repo}/milestones/{number}`
- `POST /repos/{owner}/{repo}/milestones`
- `PATCH /repos/{owner}/{repo}/milestones/{number}`
- `DELETE /repos/{owner}/{repo}/milestones/{number}`
- `POST /{owner}/{repo}/issues/milestones/new` (GitBucket web fallback)
- `POST /{owner}/{repo}/issues/milestones/{number}/edit` (GitBucket web fallback)
- `GET /{owner}/{repo}/issues/milestones/{number}/open` (GitBucket web fallback)
- `GET /{owner}/{repo}/issues/milestones/{number}/close` (GitBucket web fallback)
- `GET /{owner}/{repo}/issues/milestones/{number}/delete` (GitBucket web fallback)

---

## 10. Roadmap

### Phase 1 next

- Keep strengthening test and E2E coverage around the current command set
- Keep `cargo clippy --all-targets --all-features -- -D warnings` green in CI
- Keep release automation and package metadata aligned before tagging

### Phase 2

- Expand issue/PR metadata handling where GitBucket exposes additional compatible APIs

### Phase 3

- Re-evaluate webhook and collaborator operations after the current command set stabilizes

### Phase 4

- Documentation refinements

### Backlog / re-evaluate later

- `gb user`
- `gb webhook`
- `gb repo collaborator`