Skip to content

Commit 42a5aec

Browse files
authored
Merge pull request #2556 from carapace-sh/git-uid
git: added uids
2 parents f54bbaa + a5a19fd commit 42a5aec

File tree

10 files changed

+82
-33
lines changed

10 files changed

+82
-33
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/carapace-sh/carapace-bin
33
go 1.23.0
44

55
require (
6-
github.com/carapace-sh/carapace v1.3.2
6+
github.com/carapace-sh/carapace v1.3.3
77
github.com/carapace-sh/carapace-bridge v1.0.2
88
github.com/carapace-sh/carapace-shlex v1.0.1
99
github.com/carapace-sh/carapace-spec v1.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn
22
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
33
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
44
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
5-
github.com/carapace-sh/carapace v1.3.2 h1:EUJKuBVWKoUvvzxATGpnyIn5QFqvO8Vr3Z1Tx+EO+Uw=
6-
github.com/carapace-sh/carapace v1.3.2/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
5+
github.com/carapace-sh/carapace v1.3.3 h1:rxA1KHt6bZs0frgPz0JkknKQxtIKCZaY7TeAxuOWLQw=
6+
github.com/carapace-sh/carapace v1.3.3/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
77
github.com/carapace-sh/carapace-bridge v1.0.2 h1:q2yVrhpxjxA0p3ZcGHpjns99KE9lCrJLc3Zgaa7kMK4=
88
github.com/carapace-sh/carapace-bridge v1.0.2/go.mod h1:1tuz7tWpJeGMHa6Yvwlkb9QCNqxRD5VsS/4iZdK9ofQ=
99
github.com/carapace-sh/carapace-pflag v1.0.0 h1:uJMhl+vwEM/Eb0UdxZUuv4jo4rUAyPijkRGP5gfCuCE=

pkg/actions/tools/git/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func ActionLocalBranches() carapace.Action {
2424
return carapace.ActionExecCommand("git", "branch", "--format", "%(refname:short)\n%(subject)")(func(output []byte) carapace.Action {
2525
lines := strings.Split(string(output), "\n")
2626
return carapace.ActionValuesDescribed(lines[:len(lines)-1]...).Style(styles.Git.Branch)
27-
}).Tag("local branches")
27+
}).Tag("local branches").UidF(Uid("local-branch"))
2828
}
2929

3030
// ActionRemoteBranches completes remote branches
@@ -47,7 +47,7 @@ func ActionRemoteBranches(remote string) carapace.Action {
4747
}
4848
}
4949
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Branch)
50-
}).Tag("remote branches")
50+
}).Tag("remote branches").UidF(Uid("remote-branch"))
5151
}
5252

5353
// ActionRemoteBranchNames is like ActionRemoteBranches but skips the remote prefix

pkg/actions/tools/git/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ActionHeadCommits(limit int) carapace.Action {
3030
}
3131
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
3232
})
33-
}).Tag("head commits")
33+
}).Tag("head commits").UidF(Uid("ref"))
3434
}
3535

3636
// ActionRefCommits completes commits reachable by given ref

pkg/actions/tools/git/git.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,49 @@
11
// package git contains git related actions
22
package git
3+
4+
import (
5+
"fmt"
6+
"net/url"
7+
"path/filepath"
8+
9+
"github.com/carapace-sh/carapace/pkg/traverse"
10+
"github.com/carapace-sh/carapace/pkg/uid"
11+
)
12+
13+
// Uid TODO experimental
14+
func Uid(host string, opts ...string) func(s string, uc uid.Context) (*url.URL, error) {
15+
return func(s string, uc uid.Context) (*url.URL, error) {
16+
if length := len(opts); length%2 != 0 {
17+
return nil, fmt.Errorf("invalid amount of arguments [git.Uid]: %v", length)
18+
}
19+
20+
gitDir, err := traverse.GitDir(uc)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
workTree, err := traverse.GitWorkTree(uc)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
uid := &url.URL{
31+
Scheme: "git",
32+
Host: host,
33+
Path: s,
34+
}
35+
values := uid.Query()
36+
values.Add("GIT_WORK_TREE", workTree)
37+
if rel, err := filepath.Rel(workTree, gitDir); err != nil || rel != ".git" {
38+
values.Add("GIT_DIR", gitDir)
39+
}
40+
for i := 0; i < len(opts); i += 2 {
41+
if opts[i+1] != "" { // implicitly skip empty values
42+
values.Set(opts[i], opts[i+1])
43+
}
44+
}
45+
uid.RawQuery = values.Encode()
46+
47+
return uid, nil
48+
}
49+
}

pkg/actions/tools/git/ls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ func ActionRefFiles(ref string) carapace.Action {
101101
return filesA.Merge(directoriesA).Prefix(prefix).ToA().NoSpace().StyleF(style.ForPathExt)
102102
})
103103
})
104-
}).Tag("ref files")
104+
}).Tag("ref files").UidF(Uid("ref-file", "ref", ref))
105105
}

pkg/actions/tools/git/note.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func ActionNotes() carapace.Action {
2020
vals = append(vals, line[41:49], line[:8])
2121
}
2222
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Note)
23-
}).Tag("notes")
23+
}).Tag("notes").UidF(Uid("note")) // TODO ref?
2424
}
2525

2626
// ActionNotesMergeStrategies completes notes merge strategies

pkg/actions/tools/git/ref.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,31 @@ func ActionRefs(refOption RefOption) carapace.Action {
7272
return batch.ToA()
7373
}
7474

75-
index := max(strings.LastIndex(c.Value, "~"), strings.LastIndex(c.Value, "^"), strings.LastIndex(c.Value, "@"))
76-
switch c.Value[index] {
77-
case '^':
78-
return ActionRefParents(c.Value[:index]).Prefix(c.Value[:index+1])
79-
case '@':
80-
return carapace.Batch(
81-
time.ActionDateTime(time.DateTimeOpts{}),
82-
carapace.ActionValues("yesterday", "push", "upstream").Style(style.Blue).Suffix("}"),
83-
carapace.ActionMultiParts(".", func(c carapace.Context) carapace.Action {
84-
b := carapace.Batch()
85-
if len(c.Parts)%2 == 1 {
86-
b = append(b, carapace.ActionValues("year", "month", "week", "day", "hour", "second").Style(style.Blue).Suffix("."))
87-
}
88-
if len(c.Parts) > 0 && len(c.Parts)%2 == 0 {
89-
b = append(b, carapace.ActionValues("ago").Style(style.Blue).Suffix("}"))
90-
}
91-
return b.ToA()
92-
}),
93-
).ToA().Prefix(c.Value[:index+1] + "{")
94-
95-
default: // '~'
96-
return ActionRefCommits(c.Value[:index]).Prefix(c.Value[:index+1])
97-
}
75+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
76+
index := max(strings.LastIndex(c.Value, "~"), strings.LastIndex(c.Value, "^"), strings.LastIndex(c.Value, "@"))
77+
switch c.Value[index] {
78+
case '^':
79+
return ActionRefParents(c.Value[:index]).Prefix(c.Value[:index+1])
80+
case '@':
81+
return carapace.Batch(
82+
time.ActionDateTime(time.DateTimeOpts{}),
83+
carapace.ActionValues("yesterday", "push", "upstream").Style(style.Blue).Suffix("}"),
84+
carapace.ActionMultiParts(".", func(c carapace.Context) carapace.Action {
85+
b := carapace.Batch()
86+
if len(c.Parts)%2 == 1 {
87+
b = append(b, carapace.ActionValues("year", "month", "week", "day", "hour", "second").Style(style.Blue).Suffix("."))
88+
}
89+
if len(c.Parts) > 0 && len(c.Parts)%2 == 0 {
90+
b = append(b, carapace.ActionValues("ago").Style(style.Blue).Suffix("}"))
91+
}
92+
return b.ToA()
93+
}),
94+
).ToA().Prefix(c.Value[:index+1] + "{")
95+
96+
default: // '~'
97+
return ActionRefCommits(c.Value[:index]).Prefix(c.Value[:index+1])
98+
}
99+
}).UidF(Uid("ref"))
98100
})
99101
}
100102

pkg/actions/tools/git/stash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ func ActionStashes() carapace.Action {
2323
}
2424
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Stash)
2525
})
26-
}).Tag("stashes")
26+
}).Tag("stashes").UidF(Uid("stash"))
2727
}

pkg/actions/tools/git/tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ func ActionTags() carapace.Action {
2222
}
2323
}
2424
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Tag)
25-
}).Tag("tags")
25+
}).Tag("tags").UidF(Uid("tag"))
2626
}

0 commit comments

Comments
 (0)