694a2395f39173a74465d5169ec98d826955cc88

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

fix(server): simplify git hooks invocation

Diff

This diff is truncated to protect this page.

  1diff --git a/cmd/soft/hook.go b/cmd/soft/hook.go
  2index 99a26d86f37463e5ae1dab05afc624aa30516996..de16e3e89e0c14e8e040d7b1140375ca4f04dd4a 100644
  3--- a/cmd/soft/hook.go
  4+++ b/cmd/soft/hook.go
  5@@ -1,7 +1,6 @@
  6 package main
  7 
  8 import (
  9-	"bufio"
 10 	"fmt"
 11 	"os"
 12 	"path/filepath"
 13@@ -19,119 +18,28 @@ var (
 14 	hookCmd = &cobra.Command{
 15 		Use:    "hook",
 16 		Short:  "Run git server hooks",
 17-		Long:   "Handles git server hooks. This includes pre-receive, update, and post-receive.",
 18+		Long:   "Handles Soft Serve git server hooks.",
 19 		Hidden: true,
 20-	}
 21-
 22-	preReceiveCmd = &cobra.Command{
 23-		Use:   "pre-receive",
 24-		Short: "Run git pre-receive hook",
 25-		RunE: func(cmd *cobra.Command, args []string) error {
 26-			c, s, err := commonInit()
 27-			if err != nil {
 28-				return err
 29-			}
 30-			defer c.Close() //nolint:errcheck
 31-			defer s.Close() //nolint:errcheck
 32-			in, err := s.StdinPipe()
 33-			if err != nil {
 34-				return err
 35-			}
 36-			scanner := bufio.NewScanner(os.Stdin)
 37-			for scanner.Scan() {
 38-				in.Write([]byte(scanner.Text()))
 39-				in.Write([]byte("\n"))
 40-			}
 41-			in.Close() //nolint:errcheck
 42-			b, err := s.Output("hook pre-receive")
 43-			if err != nil {
 44-				return err
 45-			}
 46-			cmd.Print(string(b))
 47-			return nil
 48-		},
 49-	}
 50-
 51-	updateCmd = &cobra.Command{
 52-		Use:   "update",
 53-		Short: "Run git update hook",
 54-		Args:  cobra.ExactArgs(3),
 55-		RunE: func(cmd *cobra.Command, args []string) error {
 56-			refName := args[0]
 57-			oldSha := args[1]
 58-			newSha := args[2]
 59+		RunE: func(_ *cobra.Command, args []string) error {
 60 			c, s, err := commonInit()
 61 			if err != nil {
 62 				return err
 63 			}
 64 			defer c.Close() //nolint:errcheck
 65 			defer s.Close() //nolint:errcheck
 66-			b, err := s.Output(fmt.Sprintf("hook update %s %s %s", refName, oldSha, newSha))
 67-			if err != nil {
 68+			s.Stdin = os.Stdin
 69+			s.Stdout = os.Stdout
 70+			s.Stderr = os.Stderr
 71+			cmd := fmt.Sprintf("hook %s", strings.Join(args, " "))
 72+			if err := s.Run(cmd); err != nil {
 73 				return err
 74 			}
 75-			cmd.Print(string(b))
 76-			return nil
 77-		},
 78-	}
 79-
 80-	postReceiveCmd = &cobra.Command{
 81-		Use:   "post-receive",
 82-		Short: "Run git post-receive hook",
 83-		RunE: func(cmd *cobra.Command, args []string) error {
 84-			c, s, err := commonInit()
 85-			if err != nil {
 86-				return err
 87-			}
 88-			defer c.Close() //nolint:errcheck
 89-			defer s.Close() //nolint:errcheck
 90-			in, err := s.StdinPipe()
 91-			if err != nil {
 92-				return err
 93-			}
 94-			scanner := bufio.NewScanner(os.Stdin)
 95-			for scanner.Scan() {
 96-				in.Write([]byte(scanner.Text()))
 97-				in.Write([]byte("\n"))
 98-			}
 99-			in.Close() //nolint:errcheck
100-			b, err := s.Output("hook post-receive")
101-			if err != nil {
102-				return err
103-			}
104-			cmd.Print(string(b))
105diff --git a/server/config/config.go b/server/config/config.go
106index 978fccc722013374c9c2fdb7b901609fbc5c811a..ddbb0e566d647d05899f239c18f26d64c4f174ff 100644
107--- a/server/config/config.go
108+++ b/server/config/config.go
109@@ -175,7 +175,6 @@ func parseConfig(path string) (*Config, error) {
110 	for _, key := range parseAuthKeys(cfg.InitialAdminKeys) {
111 		ak := backend.MarshalAuthorizedKey(key)
112 		pks = append(pks, ak)
113-		log.Debugf("found initial admin key: %q", ak)
114 	}
115 
116 	cfg.InitialAdminKeys = pks
117diff --git a/server/hooks.go b/server/hooks.go
118index 5258901c06585fc0cfff8b543f830d0a5338c692..17cc4c83e108cd1f24e4e1f72c1b413d95dd294b 100644
119--- a/server/hooks.go
120+++ b/server/hooks.go
121@@ -13,7 +13,6 @@ var _ hooks.Hooks = (*Server)(nil)
122 //
123 // It implements Hooks.
124 func (*Server) PostReceive(stdin io.Reader, stdout io.Writer, stderr io.Writer, repo string, args []hooks.HookArg) {
125-	io.WriteString(stdout, "Hello, world!\n")
126 	log.WithPrefix("server.hooks").Debug("post-receive hook called", "repo", repo, "args", args)
127 }
128 
129@@ -35,6 +34,7 @@ func (*Server) Update(stdin io.Reader, stdout io.Writer, stderr io.Writer, repo
130 //
131 // It implements Hooks.
132 func (s *Server) PostUpdate(stdin io.Reader, stdout io.Writer, stderr io.Writer, repo string, args ...string) {
133+	log.WithPrefix("server.hooks").Debug("post-update hook called", "repo", repo, "args", args)
134 	rr, err := s.Config.Backend.Repository(repo)
135 	if err != nil {
136 		log.WithPrefix("server.hooks.post-update").Error("error getting repository", "repo", repo, "err", err)