Skip to content

Commit c323d88

Browse files
authored
Merge pull request #3229 from carapace-sh/git-heads
git: added special heads
2 parents e8cd3d6 + 58f9db2 commit c323d88

File tree

10 files changed

+84
-58
lines changed

10 files changed

+84
-58
lines changed

completers/common/git_completer/cmd/commit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ func init() {
6969
carapace.Gen(commitCmd).FlagCompletion(carapace.ActionMap{
7070
"cleanup": git.ActionCleanupModes(),
7171
"file": carapace.ActionFiles(),
72-
"fixup": git.ActionRefs(git.RefOption{HeadCommits: true}),
72+
"fixup": git.ActionRefs(git.RefOption{}.Default()),
7373
"gpg-sign": os.ActionGpgKeyIds(),
7474
"pathspec-from-file": carapace.ActionFiles(),
75-
"reedit-message": git.ActionRefs(git.RefOption{HeadCommits: true}),
76-
"reuse-message": git.ActionRefs(git.RefOption{HeadCommits: true}),
77-
"squash": git.ActionRefs(git.RefOption{HeadCommits: true}),
75+
"reedit-message": git.ActionRefs(git.RefOption{}.Default()),
76+
"reuse-message": git.ActionRefs(git.RefOption{}.Default()),
77+
"squash": git.ActionRefs(git.RefOption{}.Default()),
7878
"template": carapace.ActionFiles(),
7979
"trailer": carapace.ActionMultiPartsN(":", 2, func(c carapace.Context) carapace.Action {
8080
switch len(c.Parts) {

completers/common/git_completer/cmd/mergeTree.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ func init() {
2525
rootCmd.AddCommand(mergeTreeCmd)
2626

2727
carapace.Gen(mergeTreeCmd).FlagCompletion(carapace.ActionMap{
28-
"merge-base": git.ActionRefs(git.RefOption{HeadCommits: true}),
28+
"merge-base": git.ActionRefs(git.RefOption{}.Default()),
2929
})
3030

3131
carapace.Gen(mergeTreeCmd).PositionalCompletion(
32-
git.ActionRefs(git.RefOption{LocalBranches: true, RemoteBranches: true, Tags: true}),
33-
git.ActionRefs(git.RefOption{LocalBranches: true, RemoteBranches: true, Tags: true}),
32+
git.ActionRefs(git.RefOption{}.Default()),
33+
git.ActionRefs(git.RefOption{}.Default()),
3434
)
3535
}

completers/common/git_completer/cmd/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func init() {
7575
case 0:
7676
return git.ActionRefs(git.RefOption{
7777
LocalBranches: true,
78-
HeadCommits: true,
78+
Heads: true,
7979
Tags: true,
8080
}).NoSpace()
8181
default:

completers/common/git_completer/cmd/subtree_push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func init() {
3131
case 0:
3232
return git.ActionRefs(git.RefOption{
3333
LocalBranches: true,
34-
HeadCommits: true,
34+
Heads: true,
3535
Tags: true,
3636
}).NoSpace()
3737
default:

completers/common/git_completer/cmd/tag.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ func init() {
4747
carapace.Gen(tagCmd).FlagCompletion(carapace.ActionMap{
4848
"cleanup": git.ActionCleanupModes(),
4949
"color": git.ActionColorModes(),
50-
"contains": git.ActionRefs(git.RefOption{HeadCommits: true}),
50+
"contains": git.ActionRefs(git.RefOption{}.Default()),
5151
"file": carapace.ActionFiles(),
5252
"local-user": os.ActionGpgKeyIds(),
53-
"merged": git.ActionRefs(git.RefOption{HeadCommits: true}),
54-
"no-contains": git.ActionRefs(git.RefOption{HeadCommits: true}),
55-
"no-merged": git.ActionRefs(git.RefOption{HeadCommits: true}),
56-
"points-at": git.ActionRefs(git.RefOption{LocalBranches: true, RemoteBranches: true, HeadCommits: true}),
53+
"merged": git.ActionRefs(git.RefOption{}.Default()),
54+
"no-contains": git.ActionRefs(git.RefOption{}.Default()),
55+
"no-merged": git.ActionRefs(git.RefOption{}.Default()),
56+
"points-at": git.ActionRefs(git.RefOption{}.Default()),
5757
})
5858

5959
carapace.Gen(tagCmd).PositionalAnyCompletion(
@@ -67,7 +67,7 @@ func init() {
6767
case 0:
6868
return git.ActionRefs(git.RefOption{Tags: true})
6969
case 1:
70-
return git.ActionRefs(git.RefOption{LocalBranches: true, HeadCommits: true})
70+
return git.ActionRefs(git.RefOption{}.Default())
7171
default:
7272
return carapace.ActionValues()
7373
}

pkg/actions/tools/git/commit.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,6 @@ import (
99
"github.com/carapace-sh/carapace-bin/pkg/styles"
1010
)
1111

12-
// ActionHeadCommits completes recent head commits
13-
//
14-
// HEAD (commit message)
15-
// HEAD~1 (commit message)
16-
func ActionHeadCommits(limit int) carapace.Action {
17-
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
18-
args := []string{"log", "--no-notes", "--first-parent", "--pretty=tformat:%h %<(64,trunc)%s", "--max-count", strconv.Itoa(limit)}
19-
return carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
20-
lines := strings.Split(string(output), "\n")
21-
22-
vals := make([]string, 0)
23-
for index, line := range lines[:len(lines)-1] {
24-
switch index {
25-
case 0:
26-
vals = append(vals, "HEAD", strings.TrimSpace(line[10:]))
27-
default:
28-
vals = append(vals, "HEAD~"+fmt.Sprintf("%0"+strconv.Itoa(len(strconv.Itoa(limit-1)))+"d", index), strings.TrimSpace(line[10:]))
29-
}
30-
}
31-
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
32-
})
33-
}).Tag("head commits").UidF(Uid("ref"))
34-
}
35-
3612
// ActionRefCommits completes commits reachable by given ref
3713
//
3814
// 00 (commit message)

pkg/actions/tools/git/head.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package git
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/styles"
6+
"strings"
7+
)
8+
9+
// ActionHeads completes heads
10+
//
11+
// FETCH_HEAD (message)
12+
// ORIG_HEAD (message)
13+
func ActionHeads() carapace.Action {
14+
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
15+
refs := []string{
16+
// https://git-scm.com/docs/gitrevisions#_specifying_revisions
17+
"HEAD", // names the commit on which you based the changes in the working tree.
18+
"FETCH_HEAD", // records the branch which you fetched from a remote repository with your last git fetch invocation.
19+
"ORIG_HEAD", // is created by commands that move your HEAD in a drastic way (git am, git merge, git rebase, git reset), to record the position of the HEAD before their operation, so that you can easily change the tip of the branch back to the state before you ran them.
20+
"MERGE_HEAD", // records the commit(s) which you are merging into your branch when you run git merge.
21+
"REBASE_HEAD", // during a rebase, records the commit at which the operation is currently stopped, either because of conflicts or an edit command in an interactive rebase.
22+
"REVERT_HEAD", // records the commit which you are reverting when you run git revert.
23+
"CHERRY_PICK_HEAD", // records the commit which you are cherry-picking when you run git cherry-pick.
24+
"BISECT_HEAD", // records the current commit to be tested when you run git bisect --no-checkout.
25+
"AUTO_MERGE", // records a tree object corresponding to the state the ort merge strategy wrote to the working tree when a merge operation resulted in conflicts.
26+
}
27+
28+
if output, err := c.Command("git", "remote").Output(); err == nil {
29+
lines := strings.Split(string(output), "\n")
30+
for _, remote := range lines[:len(lines)-1] {
31+
refs = append(refs, remote+"/HEAD") // head commit of remotes default branch
32+
}
33+
}
34+
35+
batch := carapace.Batch()
36+
for _, ref := range refs {
37+
args := []string{"log", "--no-notes", "--pretty=tformat:%<(64,trunc)%s", "--max-count", "1", ref}
38+
batch = append(batch, carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
39+
lines := strings.Split(string(output), "\n")
40+
41+
vals := make([]string, 0)
42+
for _, line := range lines[:len(lines)-1] {
43+
vals = append(vals, ref, strings.TrimSpace(line))
44+
}
45+
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Head)
46+
}).Suppress("unknown revision"))
47+
}
48+
return batch.ToA().Tag("heads").UidF(Uid("ref"))
49+
})
50+
}

pkg/actions/tools/git/ref.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func rootDir(c carapace.Context) (string, error) {
1919
type RefOption struct {
2020
LocalBranches bool
2121
RemoteBranches bool
22-
HeadCommits bool
22+
Heads bool
2323
Tags bool
2424
Stashes bool
2525
Notes bool
@@ -28,7 +28,7 @@ type RefOption struct {
2828
func (o RefOption) Default() RefOption {
2929
o.LocalBranches = true
3030
o.RemoteBranches = true
31-
o.HeadCommits = true
31+
o.Heads = true
3232
o.Tags = true
3333
o.Stashes = true
3434
o.Notes = false
@@ -53,8 +53,8 @@ func ActionRefs(refOption RefOption) carapace.Action {
5353
batch = append(batch, ActionRemoteBranches(""))
5454
}
5555

56-
if refOption.HeadCommits {
57-
batch = append(batch, ActionHeadCommits(1))
56+
if refOption.Heads {
57+
batch = append(batch, ActionHeads())
5858
}
5959

6060
if refOption.Tags {

pkg/actions/tools/jj/commit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func ActionHeadCommits(limit int) carapace.Action {
2121
for index, line := range lines[:len(lines)-1] {
2222
vals = append(vals, "@"+strings.Repeat("-", index), line)
2323
}
24-
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
24+
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Head)
2525
}).Tag("head commits")
2626
}
2727

@@ -39,7 +39,7 @@ func ActionPrevCommits(limit int) carapace.Action {
3939
splitted := strings.SplitN(line, "\t", 2)
4040
vals = append(vals, fmt.Sprintf(format, index), splitted[1])
4141
}
42-
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
42+
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Head)
4343
}).Tag("previous commits")
4444
}
4545

@@ -54,7 +54,7 @@ func ActionNextCommits(limit int) carapace.Action {
5454
splitted := strings.SplitN(line, "\t", 2)
5555
vals = append(vals, fmt.Sprintf(format, len(lines)-3-index), splitted[1])
5656
}
57-
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.HeadCommit)
57+
return carapace.ActionValuesDescribed(vals...).Style(styles.Git.Head)
5858
}).Tag("previous commits")
5959
}
6060

pkg/styles/git.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ package styles
33
import "github.com/carapace-sh/carapace/pkg/style"
44

55
var Git = struct {
6-
Branch string `description:"git branches"`
7-
Commit string `description:"git commits"`
8-
HeadCommit string `description:"git HEAD~ commits"`
9-
Note string `description:"git notes"`
10-
Stash string `description:"git stashes"`
11-
Tag string `description:"git tags"`
6+
Branch string `description:"git branches"`
7+
Commit string `description:"git commits"`
8+
Head string `description:"git heads"`
9+
Note string `description:"git notes"`
10+
Stash string `description:"git stashes"`
11+
Tag string `description:"git tags"`
1212
}{
13-
Branch: style.Blue,
14-
Commit: style.Default,
15-
HeadCommit: style.Bold,
16-
Note: style.Cyan,
17-
Stash: style.Green,
18-
Tag: style.Yellow,
13+
Branch: style.Blue,
14+
Commit: style.Default,
15+
Head: style.Bold,
16+
Note: style.Cyan,
17+
Stash: style.Green,
18+
Tag: style.Yellow,
1919
}
2020

2121
func init() {

0 commit comments

Comments
 (0)