Skip to content

Commit 16927d8

Browse files
authored
Merge pull request #3244 from carapace-sh/but-uncomitted
but: cliId - include committed files
2 parents 85e2a98 + f63102f commit 16927d8

File tree

4 files changed

+66
-50
lines changed

4 files changed

+66
-50
lines changed

completers/common/but_completer/cmd/stage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func init() {
2323
carapace.Batch(
2424
git.ActionChanges(git.ChangeOpts{}.Default()),
2525
but.ActionCliIds(but.CliIdsOpts{Changes: true}),
26-
).ToA(),
26+
).ToA().UniqueList(","),
2727
carapace.Batch(
2828
but.ActionLocalBranches(),
2929
but.ActionCliIds(but.CliIdsOpts{Branches: true}),

pkg/actions/tools/but/cli.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package but
22

33
import (
44
"net/url"
5+
"strings"
56

67
"github.com/carapace-sh/carapace"
78
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
@@ -29,46 +30,55 @@ func (o CliIdsOpts) Default() CliIdsOpts {
2930
// na
3031
// c3
3132
func ActionCliIds(opts CliIdsOpts) carapace.Action {
32-
return actionStatus(func(status butStatus) carapace.Action {
33-
batch := carapace.Batch()
33+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
34+
return actionStatus(strings.Contains(c.Value, ":"), func(status butStatus) carapace.Action {
35+
batch := carapace.Batch()
3436

35-
if opts.Changes {
36-
for _, unassigned := range status.UnassignedChanges {
37-
batch = append(batch, carapace.ActionStyledValuesDescribed(unassigned.CliID, unassigned.Description(), styleForChangeType(unassigned.ChangeType)).UidF(
38-
func(s string, uc uid.Context) (*url.URL, error) {
39-
return git.Uid("change")(unassigned.FilePath, uc)
40-
}))
41-
}
42-
}
43-
44-
for _, stack := range status.Stacks {
4537
if opts.Changes {
46-
for _, assigned := range stack.AssignedChanges {
47-
batch = append(batch, carapace.ActionStyledValuesDescribed(assigned.CliID, assigned.Description(), styleForChangeType(assigned.ChangeType)).UidF(
38+
for _, unassigned := range status.UnassignedChanges {
39+
batch = append(batch, carapace.ActionStyledValuesDescribed(unassigned.CliID, unassigned.Description(), styleForChangeType(unassigned.ChangeType)).UidF(
4840
func(s string, uc uid.Context) (*url.URL, error) {
49-
return git.Uid("change")(assigned.FilePath, uc)
41+
return git.Uid("change")(unassigned.FilePath, uc)
5042
}))
5143
}
52-
5344
}
54-
if opts.Stacks && !opts.Branches { // TODO stack has the same id as its branch anyway
55-
batch = append(batch, carapace.ActionValues(stack.CliID)) // TODO style (details from the corresponding branch)
56-
}
57-
for _, branch := range stack.Branches {
58-
if opts.Branches {
59-
batch = append(batch, carapace.ActionStyledValuesDescribed(branch.CliID, branch.Name, styles.Git.Branch).UidF(
60-
func(s string, uc uid.Context) (*url.URL, error) {
61-
return git.Uid("local-branch")(branch.Name, uc)
62-
}))
45+
46+
for _, stack := range status.Stacks {
47+
if opts.Changes {
48+
for _, assigned := range stack.AssignedChanges {
49+
batch = append(batch, carapace.ActionStyledValuesDescribed(assigned.CliID, assigned.Description(), styleForChangeType(assigned.ChangeType)).UidF(
50+
func(s string, uc uid.Context) (*url.URL, error) {
51+
return git.Uid("change")(assigned.FilePath, uc)
52+
}))
53+
}
54+
6355
}
64-
for _, commit := range branch.Commits {
65-
batch = append(batch, carapace.ActionValuesDescribed(commit.CliID, commit.Message).UidF(
66-
func(s string, uc uid.Context) (*url.URL, error) {
67-
return git.Uid("ref")(commit.CommitID, uc)
68-
}))
56+
if opts.Stacks && !opts.Branches { // TODO stack has the same id as its branch anyway
57+
batch = append(batch, carapace.ActionValues(stack.CliID)) // TODO style (details from the corresponding branch)
58+
}
59+
for _, branch := range stack.Branches {
60+
if opts.Branches {
61+
batch = append(batch, carapace.ActionStyledValuesDescribed(branch.CliID, branch.Name, styles.Git.Branch).UidF(
62+
func(s string, uc uid.Context) (*url.URL, error) {
63+
return git.Uid("local-branch")(branch.Name, uc)
64+
}))
65+
}
66+
for _, commit := range branch.Commits {
67+
batch = append(batch, carapace.ActionValuesDescribed(commit.CliID, commit.Message).UidF(
68+
func(s string, uc uid.Context) (*url.URL, error) {
69+
return git.Uid("ref")(commit.CommitID, uc)
70+
}))
71+
72+
for _, committed := range commit.Changes {
73+
batch = append(batch, carapace.ActionStyledValuesDescribed(committed.CliID, committed.Description(), styleForChangeType(committed.ChangeType)).UidF(
74+
func(s string, uc uid.Context) (*url.URL, error) {
75+
return git.Uid("change")(committed.FilePath, uc)
76+
}))
77+
}
78+
}
6979
}
7080
}
71-
}
72-
return batch.ToA()
73-
}).Tag("cli ids")
81+
return batch.ToA().MultiParts(":")
82+
}).Tag("cli ids")
83+
})
7484
}

pkg/actions/tools/but/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// 36ae34b (some commit)
1212
// e1b2490 (another commit)
1313
func ActionCommits() carapace.Action {
14-
return actionStatus(func(status butStatus) carapace.Action {
14+
return actionStatus(false, func(status butStatus) carapace.Action {
1515
vals := make([]string, 0)
1616
for _, stack := range status.Stacks {
1717
for _, branch := range stack.Branches {

pkg/actions/tools/but/status.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ func (c butChange) Description() string {
2525
}
2626

2727
type butCommit struct {
28-
CliID string `json:"cliId"`
29-
CommitID string `json:"commitId"`
30-
CreatedAt time.Time `json:"createdAt"`
31-
Message string `json:"message"`
32-
AuthorName string `json:"authorName"`
33-
AuthorEmail string `json:"authorEmail"`
34-
Conflicted bool `json:"conflicted"`
35-
ReviewID any `json:"reviewId"`
36-
Changes any `json:"changes"`
28+
CliID string `json:"cliId"`
29+
CommitID string `json:"commitId"`
30+
CreatedAt time.Time `json:"createdAt"`
31+
Message string `json:"message"`
32+
AuthorName string `json:"authorName"`
33+
AuthorEmail string `json:"authorEmail"`
34+
Conflicted bool `json:"conflicted"`
35+
ReviewID any `json:"reviewId"`
36+
Changes []butChange `json:"changes"`
3737
}
3838

3939
type butBranch struct {
@@ -62,13 +62,19 @@ type butStatus struct {
6262
} `json:"upstreamState"`
6363
}
6464

65-
func actionStatus(f func(status butStatus) carapace.Action) carapace.Action {
66-
return carapace.ActionExecCommand("but", "status", "--json")(func(output []byte) carapace.Action {
67-
var status butStatus
68-
if err := json.Unmarshal(output, &status); err != nil {
69-
return carapace.ActionMessage(err.Error())
65+
func actionStatus(includeCommitted bool, f func(status butStatus) carapace.Action) carapace.Action {
66+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
67+
args := []string{"status", "--json"}
68+
if includeCommitted {
69+
args = append(args, "-f")
7070
}
71-
return f(status)
71+
return carapace.ActionExecCommand("but", args...)(func(output []byte) carapace.Action {
72+
var status butStatus
73+
if err := json.Unmarshal(output, &status); err != nil {
74+
return carapace.ActionMessage(err.Error())
75+
}
76+
return f(status)
77+
})
7278
})
7379
}
7480

0 commit comments

Comments
 (0)