09567524700e631b1a9b371ebbc577c3b2108091

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

Message

fix(hooks): log invalid hook input instead of returning error to the client

This change logs invalid hook input instead of returning an error to the client
in git hooks. This is done to prevent the client from receiving an error message
when the hook input is invalid.

Diff

 1diff --git a/cmd/soft/hook/hook.go b/cmd/soft/hook/hook.go
 2index b12938af10e66b8741347d3f9e69a3908e7e3daa..e9b3f1f5c1ff6c3ebf40cbf3f5d5160c53ed8953 100644
 3--- a/cmd/soft/hook/hook.go
 4+++ b/cmd/soft/hook/hook.go
 5@@ -65,6 +65,8 @@ var (
 6 		// This is set in the server before invoking git-receive-pack/git-upload-pack
 7 		repoName := os.Getenv("SOFT_SERVE_REPO_NAME")
 8 
 9+		logger := log.FromContext(ctx).With("repo", repoName)
10+
11 		stdin := cmd.InOrStdin()
12 		stdout := cmd.OutOrStdout()
13 		stderr := cmd.ErrOrStderr()
14@@ -83,7 +85,8 @@ var (
15 				buf.WriteByte('\n')
16 				fields := strings.Fields(scanner.Text())
17 				if len(fields) != 3 {
18-					return fmt.Errorf("invalid hook input: %s", scanner.Text())
19+					logger.Error(fmt.Sprintf("invalid %s hook input", cmdName), "input", scanner.Text())
20+					continue
21 				}
22 				opts = append(opts, hooks.HookArg{
23 					OldSha:  fields[0],
24@@ -100,7 +103,8 @@ var (
25 			}
26 		case hooks.UpdateHook:
27 			if len(args) != 3 {
28-				return fmt.Errorf("invalid update hook input: %s", args)
29+				logger.Error("invalid update hook input", "input", args)
30+				break
31 			}
32 
33 			hks.Update(ctx, stdout, stderr, repoName, hooks.HookArg{
34@@ -116,7 +120,7 @@ var (
35 		if stat, err := os.Stat(customHookPath); err == nil && !stat.IsDir() && stat.Mode()&0o111 != 0 {
36 			// If the custom hook is executable, run it
37 			if err := runCommand(ctx, &buf, stdout, stderr, customHookPath, args...); err != nil {
38-				return fmt.Errorf("failed to run custom hook: %w", err)
39+				logger.Error("failed to run custom hook", "err", err)
40 			}
41 		}
42