-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add rev-parse
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |