Skip to content

Commit

Permalink
added git branch subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 7, 2020
1 parent b37e5a5 commit ab4a596
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 45 deletions.
88 changes: 88 additions & 0 deletions completers/git_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,55 @@ func ActionRemotes() carapace.Action {
})
}

// TODO combine with ActionRemoteBranches
func ActionLocalBranches() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("git", "branch", "--format", "%(refname)").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
refs := make([]string, 0)
for _, line := range strings.Split(string(output), "\n") {
if len(line) > 12 {
refs = append(refs, line[11:])
}
}
return carapace.ActionValues(refs...)
}
})
}

func ActionRemoteBranches() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("git", "branch", "--remote", "--format", "%(refname)").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
refs := make([]string, 0)
for _, line := range strings.Split(string(output), "\n") {
if len(line) > 14 {
refs = append(refs, line[13:])
}
}
return carapace.ActionValues(refs...)
}
})
}

func ActionCommits() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
if output, err := exec.Command("git", "log", "--pretty=tformat:%H %<(64,trunc)%s", "--all", "--max-count", "1000").Output(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
refs := make([]string, 0)
for _, line := range strings.Split(string(output), "\n") {
if len(line) > 14 {
refs = append(refs, line[:40], strings.TrimSpace(line[43:]))
}
}
return carapace.ActionValuesDescribed(refs...)
}
})
}

// TODO multiparts action to complete step by step
func ActionUnstagedChanges() carapace.Action {
return carapace.ActionCallback(func(args []string) carapace.Action {
Expand Down Expand Up @@ -55,3 +104,42 @@ func ActionUnstagedChanges() carapace.Action {
}
})
}

func ActionFieldNames() carapace.Action {
return carapace.ActionValuesDescribed(
"authordate", "the date component of the author header-field",
"authoremail", "the email component of the author header-field",
"authorname", "the name component of the author header-field",
"author", "the author header-field",
"body", "the body of the message",
"committerdate", "the date component of the committer header-field",
"committeremail", "the email component of the committer header-field",
"committername", "the name component of the committer header-field",
"committer", "the committer header-field",
"contents", "complete message",
"creatordate", "the date component of the creator header-field",
"creator", "the creator header-field",
"deltabase", "object name of the delta base of the object",
"HEAD", "* if HEAD matches ref or space otherwise",
"numparent", "number of parent objects",
"objectname", "object name (SHA-1)",
"objectsize", "the size of the object",
"object", "the object header-field",
"objecttype", "the type of the object",
"parent", "the parent header-field",
"push", "name of a local ref which represents the @{push} location for the displayed ref",
"refname", "name of the ref",
"subject", "the subject of the message",
"symref", "the ref which the given symbolic ref refers to",
"taggerdate", "the date component of the tagger header-field",
"taggeremail", "the email component of the tagger header-field",
"taggername", "the name component of the tagger header-field",
"tagger", "the tagger header-field",
"tag", "the tag header-field",
"trailers", "structured information in commit messages",
"tree", "the tree header-field",
"type", "the type header-field",
"upstream", "name of a local ref which can be considered “upstream” from the displayed ref",
"version:refname", "sort by versions",
)
}
99 changes: 99 additions & 0 deletions completers/git_completer/cmd/branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/git_completer/cmd/action"
"github.com/spf13/cobra"
)

var branchCmd = &cobra.Command{
Use: "branch",
Short: "List, create, or delete branches",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(branchCmd).Standalone()

branchCmd.Flags().BoolS("D", "D", false, "Shortcut for --delete --force.")
branchCmd.Flags().BoolS("M", "M", false, "Shortcut for --move --force.")
branchCmd.Flags().BoolS("C", "C", false, "Shortcut for --copy --force.")
branchCmd.Flags().String("abbrev", "", "Alter the sha1s minimum display length in the output listing.")
branchCmd.Flags().BoolP("all", "a", false, "List both remote-tracking branches and local branches.")
branchCmd.Flags().String("color", "", "Color branches to highlight current, local, and remote-tracking branches.")
branchCmd.Flags().String("contains", "", "Only list branches which contain the specified commit (HEAD if not specified).")
branchCmd.Flags().BoolP("copy", "c", false, "Copy a branch and the corresponding reflog.")
branchCmd.Flags().Bool("create-reflog", false, "Create the branchs reflog.")
branchCmd.Flags().BoolP("delete", "d", false, "Delete a branch.")
branchCmd.Flags().Bool("edit-description", false, "Open an editor and edit the text to explain what the branch is for.")
branchCmd.Flags().BoolP("force", "f", false, "Reset <branchname> to <startpoint>, even if <branchname> exists already.")
branchCmd.Flags().String("format", "", "A string that interpolates %(fieldname) from a branch ref being shown and the object it points at.")
branchCmd.Flags().BoolP("ignore-case", "i", false, "Sorting and filtering branches are case insensitive.")
branchCmd.Flags().BoolP("list", "l", false, "List branches.")
branchCmd.Flags().String("merged", "", "Only list branches whose tips are reachable from the specified commit (HEAD if not specified).")
branchCmd.Flags().BoolP("move", "m", false, "Move/rename a branch and the corresponding reflog.")
branchCmd.Flags().Bool("no-abbrev", false, "Display the full sha1s in the output listing rather than abbreviating them.")
branchCmd.Flags().Bool("no-color", false, "Turn off branch colors, even when the configuration file gives the default to color output.")
branchCmd.Flags().String("no-contains", "", "Only list branches which dont contain the specified commit (HEAD if not specified).")
branchCmd.Flags().String("no-merged", "", "Only list branches whose tips are not reachable from the specified commit (HEAD if not specified).")
branchCmd.Flags().Bool("no-track", false, "Do not set up upstream configuration, even if the branch.autoSetupMerge configuration variable is true.")
branchCmd.Flags().String("points-at", "", "Only list branches of the given object.")
branchCmd.Flags().BoolP("quiet", "q", false, "Be more quiet when creating or deleting a branch, suppressing non-error messages.")
branchCmd.Flags().BoolP("remotes", "r", false, "List or delete (if used with -d) the remote-tracking branches.")
branchCmd.Flags().String("set-upstream-to", "", "Set up <branchname>s tracking information so <upstream> is considered <branchname>s upstream branch.")
branchCmd.Flags().Bool("show-current", false, "Print the name of the current branch.")
branchCmd.Flags().String("sort", "", "Sort based on the key given.")
branchCmd.Flags().BoolP("track", "t", false, "When creating a new branch, set up branch.<name>.remote and branch.<name>.merge configuration entries to mark the start-point branch as upstream from the new branch.")
branchCmd.Flags().Bool("unset-upstream", false, "Remove the upstream information for <branchname>.")
rootCmd.AddCommand(branchCmd)

carapace.Gen(branchCmd).FlagCompletion(carapace.ActionMap{
"color": carapace.ActionValues("always", "auto", "never"),
"contains": action.ActionCommits(),
"D": action.ActionLocalBranches(),
"delete": action.ActionLocalBranches(),
"merged": action.ActionCommits(),
"no-contains": action.ActionCommits(),
"no-merged": action.ActionCommits(),
"points-at": action.ActionRemoteBranches(),
"set-upstream-to": action.ActionRemoteBranches(),
"sort": action.ActionFieldNames(),
})

carapace.Gen(branchCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(args []string) carapace.Action {
switch len(args) {
case 0:
if branchCmd.Flag("set-upstream-to").Changed ||
branchCmd.Flag("track").Changed ||
branchCmd.Flag("no-track").Changed ||
branchCmd.Flag("unset-upstream").Changed ||
branchCmd.Flag("M").Changed ||
branchCmd.Flag("C").Changed ||
branchCmd.Flag("D").Changed ||
branchCmd.Flag("move").Changed ||
branchCmd.Flag("copy").Changed ||
branchCmd.Flag("delete").Changed ||
branchCmd.Flag("edit-description").Changed {
return action.ActionLocalBranches()
}
case 1:
if branchCmd.Flag("M").Changed ||
branchCmd.Flag("C").Changed ||
branchCmd.Flag("D").Changed ||
branchCmd.Flag("move").Changed ||
branchCmd.Flag("copy").Changed ||
branchCmd.Flag("delete").Changed {
return action.ActionLocalBranches()
}
default:
if branchCmd.Flag("D").Changed ||
branchCmd.Flag("delete").Changed {
return action.ActionLocalBranches()
}
}
return carapace.ActionValues()
}),
)

}
45 changes: 0 additions & 45 deletions completers/git_completer/cmd/branch_generated.go

This file was deleted.

0 comments on commit ab4a596

Please sign in to comment.