Skip to content

Commit

Permalink
git: relative refs
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 25, 2023
1 parent c48d350 commit b517edf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rsteube/carapace-bin

go 1.19
go 1.21

require (
github.com/pelletier/go-toml v1.9.5
Expand Down
55 changes: 32 additions & 23 deletions pkg/actions/tools/git/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,47 @@ func (o RefOption) Default() RefOption {
// v0.0.1 (last commit msg)
func ActionRefs(refOption RefOption) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
batch := carapace.Batch()
if !strings.ContainsAny(c.Value, "~^") {
batch := carapace.Batch()

if refOption.LocalBranches {
batch = append(batch, ActionLocalBranches())
}
if refOption.LocalBranches {
batch = append(batch, ActionLocalBranches())
}

if refOption.RemoteBranches {
batch = append(batch, ActionRemoteBranches(""))
}
if refOption.RemoteBranches {
batch = append(batch, ActionRemoteBranches(""))
}

if refOption.Commits > 0 {
batch = append(batch, ActionRecentCommits(refOption.Commits))
}
if refOption.Commits > 0 {
batch = append(batch, ActionRecentCommits(refOption.Commits))
}

if refOption.HeadCommits > 0 {
batch = append(batch, ActionHeadCommits(refOption.HeadCommits))
}
if refOption.HeadCommits > 0 {
batch = append(batch, ActionHeadCommits(refOption.HeadCommits))
}

if refOption.Tags {
batch = append(batch, ActionTags())
}
if refOption.Tags {
batch = append(batch, ActionTags())
}

if refOption.Stashes {
batch = append(batch, ActionStashes())
}
if refOption.Stashes {
batch = append(batch, ActionStashes())
}

if refOption.Notes {
batch = append(batch, ActionNotes())
}
if refOption.Notes {
batch = append(batch, ActionNotes())
}

return batch.ToA()
return batch.ToA()
}

index := max(strings.LastIndex(c.Value, "~"), strings.LastIndex(c.Value, "^"))
switch c.Value[index] {
case '^':
return ActionRefParents(c.Value[:index]).Prefix(c.Value[:index+1])
default: // '~'
return ActionRefCommits(c.Value[:index]).Prefix(c.Value[:index+1])
}
})
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/actions/tools/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@ func ActionHeadCommits(limit int) carapace.Action {
}).Tag("head commits")
}

// ActionRefCommits completes recent commits of given ref
//
// 00 (commit message)
// 01 (commit message)
func ActionRefCommits(ref string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
limit := 99 // TODO pass as argument
args := []string{"log", "--no-notes", "--first-parent", "--pretty=tformat:%h %<(64,trunc)%s", "--max-count", strconv.Itoa(limit), ref}
return carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
for index, line := range lines[:len(lines)-1] {
vals = append(vals, fmt.Sprintf("%0"+strconv.Itoa(len(strconv.Itoa(limit-1)))+"d", index), strings.TrimSpace(line[10:]))
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
})
}).Tag("ref commits")
}

// ActionRefParents completes parent of given ref
//
// 1 (7aca3ebc)
// 2 (4f4f9e93)
func ActionRefParents(ref string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{"log", "--no-notes", "--pretty=%p", "--max-count", "1", ref}
return carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
vals := make([]string, 0)
for index, field := range strings.Fields(string(output)) {
vals = append(vals, strconv.Itoa(index+1), field)
}
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
})
}).Tag("parent commits")
}

// ActionRecentCommits completes recent commits
//
// 123456A ((refname) commit message)
Expand Down

0 comments on commit b517edf

Please sign in to comment.