github.com/erikjuhani/git-gong@v0.0.0-20220213141213-6b9fa82d4e7c/cmd/undo.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/erikjuhani/git-gong/gong"
     5  	"github.com/spf13/cobra"
     6  )
     7  
     8  func init() {
     9  	rootCmd.AddCommand(undoCmd)
    10  }
    11  
    12  // TODO: When command history has been implemented undo the last command instead of last commit.
    13  // The last command called will be reversed.
    14  var undoCmd = &cobra.Command{
    15  	Use:   "undo",
    16  	Short: "Undo undoes the last command of the user.",
    17  	Long: `If user for example has made a mistake commit gong commit -m "mistake"
    18  		the undo, undoes the commit command and sets the repository to a prior state.
    19  	`,
    20  	Run: func(cmd *cobra.Command, args []string) {
    21  		repo, err := gong.Open()
    22  		if err != nil {
    23  			cmd.PrintErr(err)
    24  			return
    25  		}
    26  		defer gong.Free(repo)
    27  
    28  		commit, err := repo.UndoLastCommit()
    29  		if err != nil {
    30  			cmd.PrintErr(err)
    31  			return
    32  		}
    33  		defer gong.Free(commit)
    34  
    35  		cmd.Printf("undo last commit %s", commit.ID.String())
    36  	},
    37  }