From 1b378bd0b7723e9fbc5b6e1f2db994a886c6c0d5 Mon Sep 17 00:00:00 2001 From: sharan Date: Sat, 9 Oct 2021 16:35:48 +0530 Subject: [PATCH 1/2] add undo-commit command --- cmd/fix.go | 24 ++++++++++++++++++++++++ cmd/suggestion_tree.go | 4 +++- main.go | 5 +++-- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 cmd/fix.go diff --git a/cmd/fix.go b/cmd/fix.go new file mode 100644 index 0000000..92c6d1a --- /dev/null +++ b/cmd/fix.go @@ -0,0 +1,24 @@ +package cmd + +import ( + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +var undo_commitCmd = &cobra.Command{ + Use: "undo-commit", + Short: "Undo your previous commit if it is not yet pushed to repository", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + if IsAheadOfCurrent() { + err := execCommand("git", "reset", "--soft", "HEAD~1").Run() + if err != nil { + log.Debug().Err(err).Send() + } + } + }, +} + +func init() { + BitCmd.AddCommand(undo_commitCmd) +} diff --git a/cmd/suggestion_tree.go b/cmd/suggestion_tree.go index e1eb781..59fbfe8 100644 --- a/cmd/suggestion_tree.go +++ b/cmd/suggestion_tree.go @@ -1,11 +1,12 @@ package cmd import ( + "time" + "github.com/chriswalz/complete/v3" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/thoas/go-funk" - "time" ) func toAutoCLI(suggs []complete.Suggestion) func(prefix string) []complete.Suggestion { @@ -46,6 +47,7 @@ func CreateSuggestionMap(cmd *cobra.Command) (*complete.CompTree, map[string]*co st.Flags["version"] = &complete.CompTree{Desc: "Print bit and git version"} // add dynamic predictions and bit specific commands st.Sub["add"].Dynamic = toAutoCLI(gitAddSuggestions) + st.Sub["undo-commit"] = &complete.CompTree{Desc: "Lets you undo your commit"} st.Sub["checkout"].Dynamic = toAutoCLI(branchListSuggestions) st.Sub["co"].Dynamic = toAutoCLI(branchListSuggestions) st.Sub["info"] = &complete.CompTree{Desc: "Get general information about the status of your repository"} diff --git a/main.go b/main.go index 7c0e9a0..51184dd 100644 --- a/main.go +++ b/main.go @@ -17,10 +17,11 @@ package main import ( "fmt" + "os" + bitcmd "github.com/chriswalz/bit/cmd" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "os" ) // this should be overwritten at compile time @@ -61,7 +62,7 @@ func main() { } } - bitcliCmds := []string{"save", "sync", "help", "info", "release", "update", "pr", "complete", "gitmoji"} + bitcliCmds := []string{"save", "sync", "help", "info", "release", "update", "pr", "complete", "gitmoji", "undo-commit"} if len(argsWithoutProg) == 0 || bitcmd.Find(bitcliCmds, argsWithoutProg[0]) != -1 { bitcli() } else { From 7e42ea1983dcd7493607ccf75a98972fa23ca365 Mon Sep 17 00:00:00 2001 From: sharan Date: Sat, 23 Oct 2021 11:27:37 +0530 Subject: [PATCH 2/2] fixed the changes --- cmd/fix.go | 13 ++++++++++--- cmd/suggestion_tree.go | 6 +++++- main.go | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/fix.go b/cmd/fix.go index 92c6d1a..60825a8 100644 --- a/cmd/fix.go +++ b/cmd/fix.go @@ -5,10 +5,15 @@ import ( "github.com/spf13/cobra" ) +var fixCmd = &cobra.Command{ + Use: "fix sub-command", + Short: "For all the times you did something you really wish you didn't", + Args: cobra.NoArgs, +} + var undo_commitCmd = &cobra.Command{ Use: "undo-commit", - Short: "Undo your previous commit if it is not yet pushed to repository", - Args: cobra.NoArgs, + Short: "soft undos last commit if not pushed already", Run: func(cmd *cobra.Command, args []string) { if IsAheadOfCurrent() { err := execCommand("git", "reset", "--soft", "HEAD~1").Run() @@ -17,8 +22,10 @@ var undo_commitCmd = &cobra.Command{ } } }, + Args: cobra.NoArgs, } func init() { - BitCmd.AddCommand(undo_commitCmd) + BitCmd.AddCommand(fixCmd) + fixCmd.AddCommand(undo_commitCmd) } diff --git a/cmd/suggestion_tree.go b/cmd/suggestion_tree.go index 59fbfe8..77412db 100644 --- a/cmd/suggestion_tree.go +++ b/cmd/suggestion_tree.go @@ -47,7 +47,11 @@ func CreateSuggestionMap(cmd *cobra.Command) (*complete.CompTree, map[string]*co st.Flags["version"] = &complete.CompTree{Desc: "Print bit and git version"} // add dynamic predictions and bit specific commands st.Sub["add"].Dynamic = toAutoCLI(gitAddSuggestions) - st.Sub["undo-commit"] = &complete.CompTree{Desc: "Lets you undo your commit"} + st.Sub["fix"] = &complete.CompTree{ + Args: map[string]*complete.CompTree{ + "undo-commit": {Desc: "soft undos last commit if not pushed already"}, + }, + } st.Sub["checkout"].Dynamic = toAutoCLI(branchListSuggestions) st.Sub["co"].Dynamic = toAutoCLI(branchListSuggestions) st.Sub["info"] = &complete.CompTree{Desc: "Get general information about the status of your repository"} diff --git a/main.go b/main.go index 51184dd..c14f1ec 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func main() { } } - bitcliCmds := []string{"save", "sync", "help", "info", "release", "update", "pr", "complete", "gitmoji", "undo-commit"} + bitcliCmds := []string{"save", "sync", "help", "info", "release", "update", "pr", "complete", "gitmoji", "fix"} if len(argsWithoutProg) == 0 || bitcmd.Find(bitcliCmds, argsWithoutProg[0]) != -1 { bitcli() } else {