Skip to content

Commit

Permalink
Merge pull request #48 from JunNishimura/#41
Browse files Browse the repository at this point in the history
add rev-parse
  • Loading branch information
JunNishimura authored May 21, 2023
2 parents ae68213 + c0be038 commit c42acdf
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/revParse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

func revParse(refNames ...string) error {
for _, refName := range refNames {
var refPath string
if strings.ToLower(refName) == "head" {
refPath = filepath.Join(client.RootGoitPath, "refs", "heads", string(client.Head))
} else {
refPath = filepath.Join(client.RootGoitPath, "refs", "heads", refName)
}
hashBytes, err := ioutil.ReadFile(refPath)
if err != nil {
return fmt.Errorf(`fatal: ambiguous argument '%s': unknown revision or path not in the working tree`, refName)
}
hashString := string(hashBytes)
fmt.Println(hashString)
}
return nil
}

// revParseCmd represents the revParse command
var revParseCmd = &cobra.Command{
Use: "rev-parse",
Short: "pick out and massage parameters",
Long: "pick out and massage parameters",
PreRunE: func(cmd *cobra.Command, args []string) error {
if client.RootGoitPath == "" {
return ErrGoitNotInitialized
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := revParse(args...); err != nil {
return err
}

return nil
},
}

func init() {
rootCmd.AddCommand(revParseCmd)
}

0 comments on commit c42acdf

Please sign in to comment.