Skip to content

Commit 3a15317

Browse files
authored
Merge pull request #487 from rsteube/git-show-complete-tree
git: complete tree in show
2 parents 3a719b0 + d875a7a commit 3a15317

File tree

4 files changed

+83
-33
lines changed

4 files changed

+83
-33
lines changed

completers/git_completer/cmd/remote_add.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@ func init() {
2828
carapace.Gen(remote_addCmd).FlagCompletion(carapace.ActionMap{
2929
"mirror": carapace.ActionValues("push", "fetch"),
3030
"master": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
31-
return git.ActionLsRemoteRefs(c.Args[1], git.LsRemoteRefOption{Branches: true})
31+
if len(c.Args) > 1 {
32+
return git.ActionLsRemoteRefs(c.Args[1], git.LsRemoteRefOption{Branches: true})
33+
} else {
34+
return carapace.ActionValues()
35+
}
3236
}),
3337
"track": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
34-
return git.ActionLsRemoteRefs(c.Args[1], git.LsRemoteRefOption{Branches: true})
38+
if len(c.Args) > 1 {
39+
return git.ActionLsRemoteRefs(c.Args[1], git.LsRemoteRefOption{Branches: true})
40+
} else {
41+
return carapace.ActionValues()
42+
}
3543
}),
3644
})
3745

completers/git_completer/cmd/show.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ func init() {
3737
})
3838

3939
carapace.Gen(showCmd).PositionalAnyCompletion(
40-
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
41-
return git.ActionRefs(git.RefOptionDefault).Invoke(c).Filter(c.Args).ToA()
40+
carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action {
41+
switch len(c.Parts) {
42+
case 0:
43+
return git.ActionRefs(git.RefOptionDefault)
44+
case 1:
45+
return git.ActionRefFiles(c.Parts[0])
46+
default:
47+
return carapace.ActionFiles()
48+
}
4249
}),
4350
)
4451
}

pkg/actions/git/ls.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package git
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
7+
"github.com/rsteube/carapace"
8+
)
9+
10+
type LsRemoteRefOption struct {
11+
Branches bool
12+
Tags bool
13+
}
14+
15+
// ActionLsRemoteRefs lists branches and tags for a remote url
16+
// gh-pages (da4528d0a57ad71417336f0e96fa65ece2fad45a)
17+
// master (3fbdef3c6a10094812a15cba8e825898b757dfb3)
18+
func ActionLsRemoteRefs(url string, opts LsRemoteRefOption) carapace.Action {
19+
return carapace.ActionExecCommand("git", "ls-remote", "--refs", "--tags", "--heads", url)(func(output []byte) carapace.Action {
20+
lines := strings.Split(string(output), "\n")
21+
22+
vals := make([]string, 0)
23+
for _, line := range lines[:len(lines)-1] {
24+
fields := strings.Fields(line)
25+
if opts.Branches && strings.HasPrefix(fields[1], "refs/heads/") {
26+
vals = append(vals, strings.TrimPrefix(fields[1], "refs/heads/"), fields[0])
27+
} else if opts.Tags && strings.HasPrefix(fields[1], "refs/tags/") {
28+
vals = append(vals, strings.TrimPrefix(fields[1], "refs/tags/"), fields[0])
29+
}
30+
}
31+
return carapace.ActionValuesDescribed(vals...)
32+
})
33+
}
34+
35+
// ActionRefFiles lists files of a reference
36+
// go.mod
37+
// pkg/
38+
func ActionRefFiles(ref string) carapace.Action {
39+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
40+
args := []string{"ls-tree", "--name-only", "--full-tree", ref}
41+
if dir := filepath.Dir(c.CallbackValue); dir != "." {
42+
args = append(args, dir+"/")
43+
}
44+
return carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
45+
lines := strings.Split(string(output), "\n")
46+
files := lines[:len(lines)-1]
47+
48+
args = append(args, "-d") // only directories
49+
return carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
50+
lines := strings.Split(string(output), "\n")
51+
directories := lines[:len(lines)-1]
52+
53+
filesA := carapace.ActionValues(files...).Invoke(c).Filter(directories)
54+
55+
for index, dir := range directories {
56+
directories[index] = dir + "/"
57+
}
58+
directoriesA := carapace.ActionValues(directories...).Invoke(c)
59+
60+
return filesA.Merge(directoriesA).ToA()
61+
})
62+
})
63+
})
64+
}

pkg/actions/git/lsremote.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)