|
| 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 | +} |
0 commit comments