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

Ayman Bagabas created

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.

Change summary

cmd/soft/hook/hook.go | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

Detailed changes

cmd/soft/hook/hook.go 🔗

@@ -65,6 +65,8 @@ var (
 		// This is set in the server before invoking git-receive-pack/git-upload-pack
 		repoName := os.Getenv("SOFT_SERVE_REPO_NAME")
 
+		logger := log.FromContext(ctx).With("repo", repoName)
+
 		stdin := cmd.InOrStdin()
 		stdout := cmd.OutOrStdout()
 		stderr := cmd.ErrOrStderr()
@@ -83,7 +85,8 @@ var (
 				buf.WriteByte('\n')
 				fields := strings.Fields(scanner.Text())
 				if len(fields) != 3 {
-					return fmt.Errorf("invalid hook input: %s", scanner.Text())
+					logger.Error(fmt.Sprintf("invalid %s hook input", cmdName), "input", scanner.Text())
+					continue
 				}
 				opts = append(opts, hooks.HookArg{
 					OldSha:  fields[0],
@@ -100,7 +103,8 @@ var (
 			}
 		case hooks.UpdateHook:
 			if len(args) != 3 {
-				return fmt.Errorf("invalid update hook input: %s", args)
+				logger.Error("invalid update hook input", "input", args)
+				break
 			}
 
 			hks.Update(ctx, stdout, stderr, repoName, hooks.HookArg{
@@ -116,7 +120,7 @@ var (
 		if stat, err := os.Stat(customHookPath); err == nil && !stat.IsDir() && stat.Mode()&0o111 != 0 {
 			// If the custom hook is executable, run it
 			if err := runCommand(ctx, &buf, stdout, stderr, customHookPath, args...); err != nil {
-				return fmt.Errorf("failed to run custom hook: %w", err)
+				logger.Error("failed to run custom hook", "err", err)
 			}
 		}