git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[CGit] [PATCH v2 5/6] cgit_open_filter: hand down repo configuration to script

From
Ferry Huberts <mailings@hupie.com>
Date
Mar 4, 2011, 00:31 UTC
Message-ID
<1299198695-23215-6-git-send-email-mailings@hupie.com>
In-Reply-To
<1299198695-23215-1-git-send-email-mailings@hupie.com>
From: Ferry Huberts <ferry.huberts@pelagic.nl>
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
---
 shared.c |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 107 insertions(+), 1 deletions(-)
diff --git a/shared.c b/shared.c
index 49128ad..5f09a63 100644
--- a/shared.c
+++ b/shared.c
@@ -7,6 +7,8 @@
  */
 
 #include "cgit.h"
+#include <stdio.h>
+#include <linux/limits.h>
 
 struct cgit_repolist cgit_repolist;
 struct cgit_context ctx;
@@ -375,6 +377,100 @@ int cgit_parse_snapshots_mask(const char *str)
 	return rv;
 }
 
+#define ENV_VARS			7
+#define ENV_SIZE_PER_VAR	(PATH_MAX + 64)
+#define ENV_SIZE			(ENV_VARS * ENV_SIZE_PER_VAR)
+
+typedef struct {
+	char * vars[ENV_VARS + 1];
+	char buffer[ENV_SIZE];
+} env_struct;
+
+static env_struct * prepare_env(struct cgit_repo * repo) {
+	long buffer_space = sizeof(env_struct);
+	env_struct * env = malloc(buffer_space);
+	int var_index = 0;
+	char * buffer_var_index;
+	unsigned int chars_printed;
+
+	if (!env)
+		return NULL;
+
+	/**
+	 * CGIT_REPO_URL
+	 * CGIT_REPO_NAME
+	 * CGIT_REPO_PATH
+	 * CGIT_REPO_OWNER
+	 * CGIT_REPO_DEFBRANCH
+	 * CGIT_REPO_SECTION
+	 * CGIT_REPO_CLONE_URL
+	 */
+
+	buffer_var_index = &env->buffer[0];
+
+	while (var_index < ENV_VARS) {
+		chars_printed = 0;
+		switch (var_index) {
+		case 0:
+			chars_printed = snprintf(buffer_var_index, buffer_space,
+					"CGIT_REPO_URL=%s", (repo->url) ? repo->url : "");
+			break;
+
+		case 1:
+			chars_printed = snprintf(buffer_var_index, buffer_space,
+					"CGIT_REPO_NAME=%s", (repo->name) ? repo->name : "");
+			break;
+
+		case 2:
+			chars_printed = snprintf(buffer_var_index, buffer_space,
+					"CGIT_REPO_PATH=%s", (repo->path) ? repo->path : "");
+			break;
+
+		case 3:
+			chars_printed = snprintf(buffer_var_index, buffer_space,
+					"CGIT_REPO_OWNER=%s", (repo->owner) ? repo->owner : "");
+			break;
+
+		case 4:
+			chars_printed = snprintf(buffer_var_index, buffer_space,
+					"CGIT_REPO_DEFBRANCH=%s",
+					(repo->defbranch) ? repo->defbranch : "");
+			break;
+
+		case 5:
+			chars_printed = snprintf(buffer_var_index, buffer_space,
+					"CGIT_REPO_SECTION=%s", (repo->section) ? repo->section
+							: "");
+			break;
+
+		case 6:
+			chars_printed = snprintf(buffer_var_index, buffer_space,
+					"CGIT_REPO_CLONE_URL=%s",
+					(repo->clone_url) ? repo->clone_url : "");
+			break;
+
+		default:
+			break;
+		}
+
+		if (chars_printed > buffer_space) {
+			free(env);
+			return NULL;
+		}
+
+		env->vars[var_index] = buffer_var_index;
+		buffer_var_index[chars_printed] = '\0';
+		buffer_var_index += chars_printed + 1;
+		buffer_space -= chars_printed;
+
+		var_index++;
+	}
+	env->vars[var_index] = NULL;
+
+	return env;
+}
+
+
 int cgit_open_filter(struct cgit_filter *filter, struct cgit_repo * repo)
 {
 
@@ -383,10 +479,20 @@ int cgit_open_filter(struct cgit_filter *filter, struct cgit_repo * repo)
 	chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
 	filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
 	if (filter->pid == 0) {
+		env_struct * env = NULL;
+
 		close(filter->pipe_fh[1]);
 		chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
 			"Unable to use pipe as STDIN");
-		execvp(filter->cmd, filter->argv);
+
+		if (repo)
+			env = prepare_env(repo);
+
+		execve(filter->cmd, filter->argv, (env) ? &env->vars[0] : NULL);
+
+		if (env)
+			free(env);
+
 		die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
 			strerror(errno), errno);
 	}
-- 
1.7.4
Previous: Ferry HubertsNext: Ferry Huberts
Message 6 of 7 in “Communicate the repo name to the filter scripts”
  1. Ferry HubertsMar 4, 2011
  2. [CGit] [PATCH v2 1/6] source_filter: fix a memory leakFerry Huberts, Mar 4, 2011
  3. [CGit] [PATCH v2 2/6] new_filter: correctly initialise all arguments for a new filterFerry Huberts, Mar 4, 2011
  4. [CGit] [PATCH v2 3/6] new_filter: determine extra_args from filter typeFerry Huberts, Mar 4, 2011
  5. [CGit] [PATCH v2 4/6] cgit_open_filter: also take the repo as a parameterFerry Huberts, Mar 4, 2011
  6. [CGit] [PATCH v2 5/6] cgit_open_filter: hand down repo configuration to scriptFerry Huberts, Mar 4, 2011
  7. [CGit] [PATCH v2 6/6] filters: document environment variables in filter scriptsFerry Huberts, Mar 4, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.