Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add undo-commit command #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions cmd/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/rs/zerolog/log"
"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: "soft undos last commit if not pushed already",
Run: func(cmd *cobra.Command, args []string) {
if IsAheadOfCurrent() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From lines 13-17 should be wrapped in this code

arg := args[0]
if (arg == "undo-commit") {
// undo commit code
}

err := execCommand("git", "reset", "--soft", "HEAD~1").Run()
if err != nil {
log.Debug().Err(err).Send()
}
}
},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this line you'll need Args: cobra.ExactArgs(1), to force the user to pass in a subcommand for bit fix

Copy link
Author

@rustiever rustiever Oct 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @chriswalz i added fix as command and undo-commit as sub command to fix command. Hence i added cobra.NoArgs. Pls check and test the code. Tell me if something wrong. I'm happy to take your suggestions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriswalz could you take look into this?

Args: cobra.NoArgs,
}

func init() {
BitCmd.AddCommand(fixCmd)
fixCmd.AddCommand(undo_commitCmd)
}
8 changes: 7 additions & 1 deletion cmd/suggestion_tree.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -46,6 +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["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"}
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", "fix"}
if len(argsWithoutProg) == 0 || bitcmd.Find(bitcliCmds, argsWithoutProg[0]) != -1 {
bitcli()
} else {
Expand Down