Skip to content

Commit

Permalink
add soft reset (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 17, 2023
1 parent 814716e commit ffd3384
Showing 1 changed file with 62 additions and 19 deletions.
81 changes: 62 additions & 19 deletions cmd/reset.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,83 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"

"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
)

var (
isSoft bool
isMixed bool
isHard bool
resetRegexp = regexp.MustCompile(`HEAD@\{\d\}`)
)

// resetCmd represents the reset command
var resetCmd = &cobra.Command{
Use: "reset",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("reset called")
Short: "reset current HEAD to the specified state",
Long: "reset current HEAD to the specified state",
PreRunE: func(cmd *cobra.Command, args []string) error {
if client.RootGoitPath == "" {
return ErrGoitNotInitialized
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// flag validation
if isSoft || isHard {
isMixed = false
}
if !((isSoft && !isMixed && !isHard) ||
(!isSoft && isMixed && !isHard) ||
(!isSoft && !isMixed && isHard)) {
return errors.New("invalid flags")
}

// args validation
if !(len(args) == 1 && resetRegexp.MatchString(args[0])) {
return errors.New("only one argument is acceptible. argument format is 'HEAD@{number}'")
}

if isSoft {
// get log record
reflog, err := store.NewReflog(client.RootGoitPath, client.Head, client.Refs)
if err != nil {
return fmt.Errorf("fail to initialize reflog: %w", err)
}
sp := strings.Split(args[0], "HEAD@")[1]
headNum, err := strconv.Atoi(sp[1 : len(sp)-1])
if err != nil {
return fmt.Errorf("fail to convert number '%s': %w", args[0], err)
}
logRecord, err := reflog.GetRecord(headNum)
if err != nil {
return fmt.Errorf("fail to get log record: %w", err)
}

// reset Head
if err := client.Head.Reset(client.RootGoitPath, client.Refs, logRecord.Hash); err != nil {
return fmt.Errorf("fail to reset HEAD: %w", err)
}
}

return nil
},
}

func init() {
rootCmd.AddCommand(resetCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// resetCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// resetCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
resetCmd.Flags().BoolVar(&isSoft, "soft", false, "reset HEAD")
resetCmd.Flags().BoolVar(&isMixed, "mixed", true, "reset HEAD and index")
resetCmd.Flags().BoolVar(&isHard, "hard", false, "reset HEAD, index and working tree")
}

0 comments on commit ffd3384

Please sign in to comment.