From 1e4e273139513801b1e5318c9d8799752d72949f Mon Sep 17 00:00:00 2001 From: rsteube Date: Sun, 25 Oct 2020 20:16:31 +0100 Subject: [PATCH] added git stash --- completers/git_completer/cmd/action/action.go | 18 +++++++++++ .../cmd/{stash_generated.go => stash.go} | 4 +-- completers/git_completer/cmd/stash_branch.go | 24 ++++++++++++++ completers/git_completer/cmd/stash_clear.go | 18 +++++++++++ completers/git_completer/cmd/stash_drop.go | 22 +++++++++++++ completers/git_completer/cmd/stash_list.go | 18 +++++++++++ completers/git_completer/cmd/stash_pop.go | 24 ++++++++++++++ completers/git_completer/cmd/stash_push.go | 31 +++++++++++++++++++ completers/git_completer/cmd/stash_show.go | 21 +++++++++++++ 9 files changed, 177 insertions(+), 3 deletions(-) rename completers/git_completer/cmd/{stash_generated.go => stash.go} (79%) create mode 100644 completers/git_completer/cmd/stash_branch.go create mode 100644 completers/git_completer/cmd/stash_clear.go create mode 100644 completers/git_completer/cmd/stash_drop.go create mode 100644 completers/git_completer/cmd/stash_list.go create mode 100644 completers/git_completer/cmd/stash_pop.go create mode 100644 completers/git_completer/cmd/stash_push.go create mode 100644 completers/git_completer/cmd/stash_show.go diff --git a/completers/git_completer/cmd/action/action.go b/completers/git_completer/cmd/action/action.go index dafa55eac7..e7fc71ced8 100644 --- a/completers/git_completer/cmd/action/action.go +++ b/completers/git_completer/cmd/action/action.go @@ -305,3 +305,21 @@ func ActionMergeStrategyOptions(strategy string) carapace.Action { return carapace.ActionValues() } } + +func ActionStashes() carapace.Action { + return carapace.ActionCallback(func(args []string) carapace.Action { + if output, err := exec.Command("git", "stash", "list").Output(); err != nil { + return carapace.ActionValues(err.Error()) + } else { + lines := strings.Split(string(output), "\n") + vals := make([]string, (len(lines)-1)*2) + + for index, line := range lines[:len(lines)-1] { + splitted := strings.SplitN(line, ": ", 2) + vals[index*2] = splitted[0] + vals[(index*2)+1] = splitted[1] + } + return carapace.ActionValuesDescribed(vals...) + } + }) +} diff --git a/completers/git_completer/cmd/stash_generated.go b/completers/git_completer/cmd/stash.go similarity index 79% rename from completers/git_completer/cmd/stash_generated.go rename to completers/git_completer/cmd/stash.go index fd69ddae78..ad5733b740 100644 --- a/completers/git_completer/cmd/stash_generated.go +++ b/completers/git_completer/cmd/stash.go @@ -7,11 +7,9 @@ import ( var stashCmd = &cobra.Command{ Use: "stash", Short: "Stash the changes in a dirty working directory away", - Run: func(cmd *cobra.Command, args []string) { - }, + Run: func(cmd *cobra.Command, args []string) {}, } func init() { - rootCmd.AddCommand(stashCmd) } diff --git a/completers/git_completer/cmd/stash_branch.go b/completers/git_completer/cmd/stash_branch.go new file mode 100644 index 0000000000..c49bff5329 --- /dev/null +++ b/completers/git_completer/cmd/stash_branch.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/completers/git_completer/cmd/action" + "github.com/spf13/cobra" +) + +var stash_branchCmd = &cobra.Command{ + Use: "branch", + Short: "create and check out a new branch with the stashes changes", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(stash_branchCmd).Standalone() + + stashCmd.AddCommand(stash_branchCmd) + + carapace.Gen(stash_branchCmd).PositionalCompletion( + action.ActionRefs(action.RefOption{LocalBranches: true}), + action.ActionStashes(), + ) +} diff --git a/completers/git_completer/cmd/stash_clear.go b/completers/git_completer/cmd/stash_clear.go new file mode 100644 index 0000000000..f059dce0f1 --- /dev/null +++ b/completers/git_completer/cmd/stash_clear.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var stash_clearCmd = &cobra.Command{ + Use: "clear", + Short: "remove all the stash entries", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(stash_clearCmd).Standalone() + + stashCmd.AddCommand(stash_clearCmd) +} diff --git a/completers/git_completer/cmd/stash_drop.go b/completers/git_completer/cmd/stash_drop.go new file mode 100644 index 0000000000..a2154d9d72 --- /dev/null +++ b/completers/git_completer/cmd/stash_drop.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/completers/git_completer/cmd/action" + "github.com/spf13/cobra" +) + +var stash_dropCmd = &cobra.Command{ + Use: "drop", + Short: "remove a single stash entry", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(stash_dropCmd).Standalone() + stash_dropCmd.Flags().BoolP("quiet", "q", false, "suppress feedback messages") + + stashCmd.AddCommand(stash_dropCmd) + + carapace.Gen(stash_dropCmd).PositionalCompletion(action.ActionStashes()) +} diff --git a/completers/git_completer/cmd/stash_list.go b/completers/git_completer/cmd/stash_list.go new file mode 100644 index 0000000000..0b83fea064 --- /dev/null +++ b/completers/git_completer/cmd/stash_list.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var stash_listCmd = &cobra.Command{ + Use: "list", + Short: "list the stash entries that you currently have", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(stash_listCmd).Standalone() + + stashCmd.AddCommand(stash_listCmd) +} diff --git a/completers/git_completer/cmd/stash_pop.go b/completers/git_completer/cmd/stash_pop.go new file mode 100644 index 0000000000..37415626e7 --- /dev/null +++ b/completers/git_completer/cmd/stash_pop.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/completers/git_completer/cmd/action" + "github.com/spf13/cobra" +) + +var stash_popCmd = &cobra.Command{ + Use: "pop", + Aliases: []string{"apply"}, + Short: "remove a single stashed state", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(stash_popCmd).Standalone() + stash_popCmd.Flags().Bool("index", false, "try to reinstate index changes as well") + stash_popCmd.Flags().BoolP("quiet", "q", false, "suppress feedback messages") + + stashCmd.AddCommand(stash_popCmd) + + carapace.Gen(stash_popCmd).PositionalCompletion(action.ActionStashes()) +} diff --git a/completers/git_completer/cmd/stash_push.go b/completers/git_completer/cmd/stash_push.go new file mode 100644 index 0000000000..0a09b49524 --- /dev/null +++ b/completers/git_completer/cmd/stash_push.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/spf13/cobra" +) + +var stash_pushCmd = &cobra.Command{ + Use: "push", + Short: "save your local modifications to a new stash", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(stash_pushCmd).Standalone() + + stash_pushCmd.Flags().BoolP("all", "a", false, "also stash ignored and untracked") + stash_pushCmd.Flags().BoolP("include-untracked", "u", false, "also stash untracked") + stash_pushCmd.Flags().BoolP("keep-index", "k", false, "keep changed added to index") + stash_pushCmd.Flags().StringP("message", "m", "", "set description") + stash_pushCmd.Flags().Bool("no-keep-index", false, "also apply to index") + stash_pushCmd.Flags().BoolP("patch", "p", false, "interactively select hunks between HEAD and working tree") + stash_pushCmd.Flags().Bool("pathspec-file-nul", false, "pathspec elemts are seperated by NUL") + stash_pushCmd.Flags().String("pathspec-from-file", "", "read pathspec from file") + stash_pushCmd.Flags().BoolP("quiet", "q", false, "suppress feedback messages") + stashCmd.AddCommand(stash_pushCmd) + + carapace.Gen(stash_pushCmd).FlagCompletion(carapace.ActionMap{ + "pathspec-from-file": carapace.ActionFiles(""), + }) +} diff --git a/completers/git_completer/cmd/stash_show.go b/completers/git_completer/cmd/stash_show.go new file mode 100644 index 0000000000..091ad2bba6 --- /dev/null +++ b/completers/git_completer/cmd/stash_show.go @@ -0,0 +1,21 @@ +package cmd + +import ( + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/completers/git_completer/cmd/action" + "github.com/spf13/cobra" +) + +var stash_showCmd = &cobra.Command{ + Use: "show", + Short: "show the changes recorded in the stash entry", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(stash_showCmd).Standalone() + + stashCmd.AddCommand(stash_showCmd) + + carapace.Gen(stash_showCmd).PositionalCompletion(action.ActionStashes()) +}