Skip to content

Commit 8d8543c

Browse files
committed
but: target description
1 parent 0ae3eca commit 8d8543c

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

completers/common/but_completer/cmd/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ func init() {
2121
rootCmd.AddCommand(describeCmd)
2222

2323
carapace.Gen(describeCmd).PositionalCompletion(
24-
but.ActionLocalBranches(), // TODO commit id
24+
but.ActionTargets(),
2525
)
2626
}

pkg/actions/tools/but/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
// ActionLocalBranches completes local branches
1111
//
12-
// mybranch
13-
// another
12+
// branch (branch description)
13+
// another (another description)
1414
func ActionLocalBranches() carapace.Action {
1515
return carapace.ActionExecCommand("but", "branch", "list", "--local")(func(output []byte) carapace.Action {
1616
lines := strings.Split(string(output), "\n")

pkg/actions/tools/but/commit.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package but
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/carapace-sh/carapace"
7+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
8+
"github.com/carapace-sh/carapace-bin/pkg/styles"
9+
)
10+
11+
type logEntry struct {
12+
BranchDetails []struct {
13+
Commits []struct {
14+
ID string `json:"id"`
15+
Message string `json:"message"`
16+
HasConflicts bool `json:"hasConflicts"`
17+
State struct {
18+
Type string `json:"type"`
19+
} `json:"state"`
20+
CreatedAt int64 `json:"createdAt"`
21+
} `json:"commits"`
22+
} `json:"branchDetails"`
23+
}
24+
25+
// ActionCommits completes commits
26+
//
27+
// 36ae34b (some commit)
28+
// e1b2490 (another commit)
29+
func ActionCommits() carapace.Action {
30+
return carapace.ActionExecCommand("but", "--json", "log")(func(output []byte) carapace.Action {
31+
var entries []logEntry
32+
if err := json.Unmarshal(output, &entries); err != nil {
33+
return carapace.ActionMessage(err.Error())
34+
}
35+
36+
vals := make([]string, 0)
37+
for _, entry := range entries {
38+
for _, branch := range entry.BranchDetails {
39+
for _, commit := range branch.Commits {
40+
vals = append(vals, commit.ID[:7], commit.Message, styles.Git.Commit)
41+
}
42+
}
43+
}
44+
return carapace.ActionStyledValuesDescribed(vals...)
45+
}).Tag("commits").UidF(git.Uid("ref")) // TODO custom uid
46+
}

pkg/actions/tools/but/target.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package but
2+
3+
import "github.com/carapace-sh/carapace"
4+
5+
// ActionTargets completes targets
6+
//
7+
// 36ae34b (some commit)
8+
// branch (some branch)
9+
func ActionTargets() carapace.Action {
10+
return carapace.Batch(
11+
ActionCommits(),
12+
ActionLocalBranches(),
13+
).ToA()
14+
}

0 commit comments

Comments
 (0)