Skip to content

Commit

Permalink
chore(cmd): add git hook management feature.
Browse files Browse the repository at this point in the history
- Add a new `hookCmd` command to install and uninstall git hooks
- Remove `log` library import from `cmd/config.go`
- Replace `log.Fatal` calls with `errors.New` returns in `cmd/config.go`
- Add a `hookPath` function to get the git hook path
- Add a new `prepare-commit-msg` file to the `hook/templates` directory.
  • Loading branch information
appleboy committed Mar 5, 2023
1 parent df7f69a commit 7afbe1a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(commitCmd)
rootCmd.AddCommand(hookCmd)

// hide completion command
rootCmd.CompletionOptions.HiddenDefaultCmd = true
Expand Down Expand Up @@ -83,7 +84,6 @@ func initConfig() {

func Execute(ctx context.Context) {
if _, err := rootCmd.ExecuteContextC(ctx); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
11 changes: 6 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"errors"
"fmt"
"log"
"strings"

"github.com/appleboy/com/array"
Expand All @@ -27,19 +27,20 @@ var configCmd = &cobra.Command{
Use: "config",
Short: "Add openai config (openai.api_key, openai.model ...)",
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if args[0] != "set" {
log.Fatal("config set key value. ex: config set openai.api_key sk-...")
return errors.New("config set key value. ex: config set openai.api_key sk-...")
}

if !array.InSlice(args[1], availableKeys) {
log.Fatal("available key list:", strings.Join(availableKeys, ", "))
return errors.New("available key list: " + strings.Join(availableKeys, ", "))
}

viper.Set(args[1], args[2])
if err := viper.WriteConfig(); err != nil {
log.Fatal(err)
return err
}
fmt.Println("you can see the config file:", viper.ConfigFileUsed())
return nil
},
}
29 changes: 29 additions & 0 deletions cmd/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"errors"

"github.com/appleboy/CodeGPT/hook"

"github.com/spf13/cobra"
)

var hookCmd = &cobra.Command{
Use: "hook",
Short: "install/uninstall git prepare-commit-msg hook",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if args[0] != "install" && args[0] != "uninstall" {
return errors.New("only support install or uninstall command")
}

switch args[0] {
case "install":
return hook.Install()
case "uninstall":
return hook.Uninstall()
}

return nil
},
}
19 changes: 19 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func diffFiles() *exec.Cmd {
)
}

func hookPath() *exec.Cmd {
args := []string{
"rev-parse",
"--git-path",
"hooks",
}

return exec.Command(
"git",
args...,
)
}

// Diff compares the differences between two sets of data.
// It returns a string representing the differences and an error.
// If there are no differences, it returns an empty string and an error.
Expand All @@ -74,3 +87,9 @@ func Diff() (string, error) {

return string(output), nil
}

// Hook to show git hook path
func Hook() (string, error) {
output, err := hookPath().Output()
return string(output), err
}
3 changes: 3 additions & 0 deletions hook/templates/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

codegpt commit --file $1

0 comments on commit 7afbe1a

Please sign in to comment.