Skip to content

Commit

Permalink
git: added uids
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 18, 2024
1 parent 8e5167c commit 3cdd8be
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 31 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/carapace-sh/carapace-bin
go 1.23.0

require (
github.com/carapace-sh/carapace v1.3.1
github.com/carapace-sh/carapace v1.3.3
github.com/carapace-sh/carapace-bridge v1.0.2
github.com/carapace-sh/carapace-shlex v1.0.1
github.com/carapace-sh/carapace-spec v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMU
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/carapace-sh/carapace v1.3.1 h1:F9rQIYRjpukCpFdKZSPNC+Byb8n6wPhfy0WD9RcYSNU=
github.com/carapace-sh/carapace v1.3.1/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
github.com/carapace-sh/carapace v1.3.3 h1:rxA1KHt6bZs0frgPz0JkknKQxtIKCZaY7TeAxuOWLQw=
github.com/carapace-sh/carapace v1.3.3/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
github.com/carapace-sh/carapace-bridge v1.0.2 h1:q2yVrhpxjxA0p3ZcGHpjns99KE9lCrJLc3Zgaa7kMK4=
github.com/carapace-sh/carapace-bridge v1.0.2/go.mod h1:1tuz7tWpJeGMHa6Yvwlkb9QCNqxRD5VsS/4iZdK9ofQ=
github.com/carapace-sh/carapace-pflag v1.0.0 h1:uJMhl+vwEM/Eb0UdxZUuv4jo4rUAyPijkRGP5gfCuCE=
Expand Down
4 changes: 2 additions & 2 deletions pkg/actions/tools/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ActionLocalBranches() carapace.Action {
return carapace.ActionExecCommand("git", "branch", "--format", "%(refname:short)\n%(subject)")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValuesDescribed(lines[:len(lines)-1]...).Style(styles.Git.Branch)
}).Tag("local branches")
}).Tag("local branches").UidF(Uid("local-branch"))
}

// ActionRemoteBranches completes remote branches
Expand All @@ -47,7 +47,7 @@ func ActionRemoteBranches(remote string) carapace.Action {
}
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Branch)
}).Tag("remote branches")
}).Tag("remote branches").UidF(Uid("remote-branch"))
}

// ActionRemoteBranchNames is like ActionRemoteBranches but skips the remote prefix
Expand Down
2 changes: 1 addition & 1 deletion pkg/actions/tools/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ActionHeadCommits(limit int) carapace.Action {
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
})
}).Tag("head commits")
}).Tag("head commits").UidF(Uid("ref"))
}

// ActionRefCommits completes commits reachable by given ref
Expand Down
47 changes: 47 additions & 0 deletions pkg/actions/tools/git/git.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
// package git contains git related actions
package git

import (
"fmt"
"net/url"
"path/filepath"

"github.com/carapace-sh/carapace/pkg/traverse"
"github.com/carapace-sh/carapace/pkg/uid"
)

// Uid TODO experimental
func Uid(host string, opts ...string) func(s string, uc uid.Context) (*url.URL, error) {
return func(s string, uc uid.Context) (*url.URL, error) {
if length := len(opts); length%2 != 0 {
return nil, fmt.Errorf("invalid amount of arguments [git.Uid]: %v", length)
}

gitDir, err := traverse.GitDir(uc)
if err != nil {
return nil, err
}

workTree, err := traverse.GitWorkTree(uc)
if err != nil {
return nil, err
}

uid := &url.URL{
Scheme: "git",
Host: host,
Path: s,
}
values := uid.Query()
values.Add("GIT_WORK_TREE", workTree)
if rel, err := filepath.Rel(workTree, gitDir); err != nil || rel != ".git" {
values.Add("GIT_DIR", gitDir)
}
for i := 0; i < len(opts); i += 2 {
if opts[i+1] != "" { // implicitly skip empty values
values.Set(opts[i], opts[i+1])
}
}
uid.RawQuery = values.Encode()

return uid, nil
}
}
2 changes: 1 addition & 1 deletion pkg/actions/tools/git/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ func ActionRefFiles(ref string) carapace.Action {
return filesA.Merge(directoriesA).Prefix(prefix).ToA().NoSpace().StyleF(style.ForPathExt)
})
})
}).Tag("ref files")
}).Tag("ref files").UidF(Uid("ref-file", "ref", ref))
}
2 changes: 1 addition & 1 deletion pkg/actions/tools/git/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ActionNotes() carapace.Action {
vals = append(vals, line[41:49], line[:8])
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Note)
}).Tag("notes")
}).Tag("notes").UidF(Uid("note")) // TODO ref?
}

// ActionNotesMergeStrategies completes notes merge strategies
Expand Down
48 changes: 25 additions & 23 deletions pkg/actions/tools/git/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,31 @@ func ActionRefs(refOption RefOption) carapace.Action {
return batch.ToA()
}

index := max(strings.LastIndex(c.Value, "~"), strings.LastIndex(c.Value, "^"), strings.LastIndex(c.Value, "@"))
switch c.Value[index] {
case '^':
return ActionRefParents(c.Value[:index]).Prefix(c.Value[:index+1])
case '@':
return carapace.Batch(
time.ActionDateTime(time.DateTimeOpts{}),
carapace.ActionValues("yesterday", "push", "upstream").Style(style.Blue).Suffix("}"),
carapace.ActionMultiParts(".", func(c carapace.Context) carapace.Action {
b := carapace.Batch()
if len(c.Parts)%2 == 1 {
b = append(b, carapace.ActionValues("year", "month", "week", "day", "hour", "second").Style(style.Blue).Suffix("."))
}
if len(c.Parts) > 0 && len(c.Parts)%2 == 0 {
b = append(b, carapace.ActionValues("ago").Style(style.Blue).Suffix("}"))
}
return b.ToA()
}),
).ToA().Prefix(c.Value[:index+1] + "{")

default: // '~'
return ActionRefCommits(c.Value[:index]).Prefix(c.Value[:index+1])
}
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
index := max(strings.LastIndex(c.Value, "~"), strings.LastIndex(c.Value, "^"), strings.LastIndex(c.Value, "@"))
switch c.Value[index] {
case '^':
return ActionRefParents(c.Value[:index]).Prefix(c.Value[:index+1])
case '@':
return carapace.Batch(
time.ActionDateTime(time.DateTimeOpts{}),
carapace.ActionValues("yesterday", "push", "upstream").Style(style.Blue).Suffix("}"),
carapace.ActionMultiParts(".", func(c carapace.Context) carapace.Action {
b := carapace.Batch()
if len(c.Parts)%2 == 1 {
b = append(b, carapace.ActionValues("year", "month", "week", "day", "hour", "second").Style(style.Blue).Suffix("."))
}
if len(c.Parts) > 0 && len(c.Parts)%2 == 0 {
b = append(b, carapace.ActionValues("ago").Style(style.Blue).Suffix("}"))
}
return b.ToA()
}),
).ToA().Prefix(c.Value[:index+1] + "{")

default: // '~'
return ActionRefCommits(c.Value[:index]).Prefix(c.Value[:index+1])
}
}).UidF(Uid("ref"))
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/actions/tools/git/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func ActionStashes() carapace.Action {
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Stash)
})
}).Tag("stashes")
}).Tag("stashes").UidF(Uid("stash"))
}
2 changes: 1 addition & 1 deletion pkg/actions/tools/git/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ func ActionTags() carapace.Action {
}
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Tag)
}).Tag("tags")
}).Tag("tags").UidF(Uid("tag"))
}

0 comments on commit 3cdd8be

Please sign in to comment.