Skip to content

Commit

Permalink
fix(server): simplify git hooks invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent d2c91b3 commit 694a239
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 101 deletions.
106 changes: 7 additions & 99 deletions cmd/soft/hook.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bufio"
"fmt"
"os"
"path/filepath"
Expand All @@ -19,119 +18,28 @@ var (
hookCmd = &cobra.Command{
Use: "hook",
Short: "Run git server hooks",
Long: "Handles git server hooks. This includes pre-receive, update, and post-receive.",
Long: "Handles Soft Serve git server hooks.",
Hidden: true,
}

preReceiveCmd = &cobra.Command{
Use: "pre-receive",
Short: "Run git pre-receive hook",
RunE: func(cmd *cobra.Command, args []string) error {
c, s, err := commonInit()
if err != nil {
return err
}
defer c.Close() //nolint:errcheck
defer s.Close() //nolint:errcheck
in, err := s.StdinPipe()
if err != nil {
return err
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
in.Write([]byte(scanner.Text()))
in.Write([]byte("\n"))
}
in.Close() //nolint:errcheck
b, err := s.Output("hook pre-receive")
if err != nil {
return err
}
cmd.Print(string(b))
return nil
},
}

updateCmd = &cobra.Command{
Use: "update",
Short: "Run git update hook",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
refName := args[0]
oldSha := args[1]
newSha := args[2]
RunE: func(_ *cobra.Command, args []string) error {
c, s, err := commonInit()
if err != nil {
return err
}
defer c.Close() //nolint:errcheck
defer s.Close() //nolint:errcheck
b, err := s.Output(fmt.Sprintf("hook update %s %s %s", refName, oldSha, newSha))
if err != nil {
s.Stdin = os.Stdin
s.Stdout = os.Stdout
s.Stderr = os.Stderr
cmd := fmt.Sprintf("hook %s", strings.Join(args, " "))
if err := s.Run(cmd); err != nil {
return err
}
cmd.Print(string(b))
return nil
},
}

postReceiveCmd = &cobra.Command{
Use: "post-receive",
Short: "Run git post-receive hook",
RunE: func(cmd *cobra.Command, args []string) error {
c, s, err := commonInit()
if err != nil {
return err
}
defer c.Close() //nolint:errcheck
defer s.Close() //nolint:errcheck
in, err := s.StdinPipe()
if err != nil {
return err
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
in.Write([]byte(scanner.Text()))
in.Write([]byte("\n"))
}
in.Close() //nolint:errcheck
b, err := s.Output("hook post-receive")
if err != nil {
return err
}
cmd.Print(string(b))
return nil
},
}

postUpdateCmd = &cobra.Command{
Use: "post-update",
Short: "Run git post-update hook",
RunE: func(cmd *cobra.Command, args []string) error {
c, s, err := commonInit()
if err != nil {
return err
}
defer c.Close() //nolint:errcheck
defer s.Close() //nolint:errcheck
b, err := s.Output(fmt.Sprintf("hook post-update %s", strings.Join(args, " ")))
if err != nil {
return err
}
cmd.Print(string(b))
return nil
},
}
)

func init() {
hookCmd.AddCommand(
preReceiveCmd,
updateCmd,
postReceiveCmd,
postUpdateCmd,
)

hookCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "path to config file")
}

Expand Down
1 change: 0 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ func parseConfig(path string) (*Config, error) {
for _, key := range parseAuthKeys(cfg.InitialAdminKeys) {
ak := backend.MarshalAuthorizedKey(key)
pks = append(pks, ak)
log.Debugf("found initial admin key: %q", ak)
}

cfg.InitialAdminKeys = pks
Expand Down
2 changes: 1 addition & 1 deletion server/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var _ hooks.Hooks = (*Server)(nil)
//
// It implements Hooks.
func (*Server) PostReceive(stdin io.Reader, stdout io.Writer, stderr io.Writer, repo string, args []hooks.HookArg) {
io.WriteString(stdout, "Hello, world!\n")
log.WithPrefix("server.hooks").Debug("post-receive hook called", "repo", repo, "args", args)
}

Expand All @@ -35,6 +34,7 @@ func (*Server) Update(stdin io.Reader, stdout io.Writer, stderr io.Writer, repo
//
// It implements Hooks.
func (s *Server) PostUpdate(stdin io.Reader, stdout io.Writer, stderr io.Writer, repo string, args ...string) {
log.WithPrefix("server.hooks").Debug("post-update hook called", "repo", repo, "args", args)
rr, err := s.Config.Backend.Repository(repo)
if err != nil {
log.WithPrefix("server.hooks.post-update").Error("error getting repository", "repo", repo, "err", err)
Expand Down

0 comments on commit 694a239

Please sign in to comment.