From f401f0be89cf5ca29278cc836cdb50faca4613aa Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 07 Mar 2025 15:15:29 +0800 Subject: [PATCH] hooks: Send/process environment variables starting with GIT_ --- git_hooks_client/git_hooks_client.c | 32 ++++++++++++++++++++++++++++++++ git_hooks_handle.go | 29 +++++++++++++++++++++++++++++ diff --git a/git_hooks_client/git_hooks_client.c b/git_hooks_client/git_hooks_client.c index fc49d915c8f9db46a760b71659c45a7f3c7c88f4..bdc6dc24f397ed940ba93a1aa008113ab085560c 100644 --- a/git_hooks_client/git_hooks_client.c +++ b/git_hooks_client/git_hooks_client.c @@ -154,6 +154,38 @@ } } /* + * Then send all environment variables that begin with "GIT_" + */ + extern char **environ; + for (char **env = environ; *env != NULL; env++) { + if (strncmp(*env, "GIT_", 4) == 0) { + unsigned long len = strlen(*env) + 1; + bytes_sent = send(sock, *env, len, 0); + if (bytes_sent == -1) { + perror("send env"); + close(sock); + exit(EXIT_FAILURE); + } else if ((unsigned long)bytes_sent == len) { + } else { + dprintf(STDERR_FILENO, "send returned unexpected value on internal socket\n"); + close(sock); + exit(EXIT_FAILURE); + } + } + } + bytes_sent = send(sock, "", 1, 0); + if (bytes_sent == -1) { + perror("send env terminator"); + close(sock); + exit(EXIT_FAILURE); + } else if (bytes_sent == 1) { + } else { + dprintf(STDERR_FILENO, "send returned unexpected value on internal socket\n"); + close(sock); + exit(EXIT_FAILURE); + } + + /* * Now we can start splicing data from stdin to the UNIX domain socket. * The format is irrelevant and depends on the hook being called. All we * do is pass it to the socket for it to handle. diff --git a/git_hooks_handle.go b/git_hooks_handle.go index 063eb50900fca7b0db2913350b363322af9833fa..69fe0aa6f6aca0f0ba924b194b51b3d5be321f5e 100644 --- a/git_hooks_handle.go +++ b/git_hooks_handle.go @@ -108,6 +108,35 @@ } args = append(args, arg.String()) } + git_env := make(map[string]string) + for { + var env_line bytes.Buffer + for { + b := make([]byte, 1) + n, err := conn.Read(b) + if err != nil || n != 1 { + wf_error(ssh_stderr, "Failed to read environment variable: %v", err) + return 1 + } + if b[0] == 0 { + break + } + env_line.WriteByte(b[0]) + } + if env_line.Len() == 0 { + break + } + kv := env_line.String() + parts := strings.SplitN(kv, "=", 2) + if len(parts) < 2 { + wf_error(ssh_stderr, "Invalid environment variable line: %v", kv) + return 1 + } + git_env[parts[0]] = parts[1] + } + + fmt.Printf("%#v\n", git_env) + var stdin bytes.Buffer if _, err = io.Copy(&stdin, conn); err != nil { wf_error(conn, "Failed to read to the stdin buffer: %v", err) -- 2.48.1