aboutsummaryrefslogtreecommitdiffstats
path: root/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared.c')
-rw-r--r--shared.c51
1 files changed, 36 insertions, 15 deletions
diff --git a/shared.c b/shared.c
index 7ec2e19..be2ae59 100644
--- a/shared.c
+++ b/shared.c
@@ -7,6 +7,8 @@
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include <stdio.h>
11#include <linux/limits.h>
10 12
11struct cgit_repolist cgit_repolist; 13struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 14struct cgit_context ctx;
@@ -100,23 +102,15 @@ void *cgit_free_commitinfo(struct commitinfo *info)
100char *trim_end(const char *str, char c) 102char *trim_end(const char *str, char c)
101{ 103{
102 int len; 104 int len;
103 char *s, *t;
104 105
105 if (str == NULL) 106 if (str == NULL)
106 return NULL; 107 return NULL;
107 t = (char *)str; 108 len = strlen(str);
108 len = strlen(t); 109 while(len > 0 && str[len - 1] == c)
109 while(len > 0 && t[len - 1] == c)
110 len--; 110 len--;
111
112 if (len == 0) 111 if (len == 0)
113 return NULL; 112 return NULL;
114 113 return xstrndup(str, len);
115 c = t[len];
116 t[len] = '\0';
117 s = xstrdup(t);
118 t[len] = c;
119 return s;
120} 114}
121 115
122char *strlpart(char *txt, int maxlen) 116char *strlpart(char *txt, int maxlen)
@@ -311,7 +305,6 @@ void cgit_diff_tree(const unsigned char *old_sha1,
311 filepair_fn fn, const char *prefix, int ignorews) 305 filepair_fn fn, const char *prefix, int ignorews)
312{ 306{
313 struct diff_options opt; 307 struct diff_options opt;
314 int ret;
315 int prefixlen; 308 int prefixlen;
316 309
317 diff_setup(&opt); 310 diff_setup(&opt);
@@ -332,9 +325,9 @@ void cgit_diff_tree(const unsigned char *old_sha1,
332 diff_setup_done(&opt); 325 diff_setup_done(&opt);
333 326
334 if (old_sha1 && !is_null_sha1(old_sha1)) 327 if (old_sha1 && !is_null_sha1(old_sha1))
335 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); 328 diff_tree_sha1(old_sha1, new_sha1, "", &opt);
336 else 329 else
337 ret = diff_root_tree_sha1(new_sha1, "", &opt); 330 diff_root_tree_sha1(new_sha1, "", &opt);
338 diffcore_std(&opt); 331 diffcore_std(&opt);
339 diff_flush(&opt); 332 diff_flush(&opt);
340} 333}
@@ -376,7 +369,33 @@ int cgit_parse_snapshots_mask(const char *str)
376 return rv; 369 return rv;
377} 370}
378 371
379int cgit_open_filter(struct cgit_filter *filter) 372typedef struct {
373 char * name;
374 char * value;
375} cgit_env_var;
376
377static void prepare_env(struct cgit_repo * repo) {
378 cgit_env_var env_vars[] = {
379 { .name = "CGIT_REPO_URL", .value = repo->url },
380 { .name = "CGIT_REPO_NAME", .value = repo->name },
381 { .name = "CGIT_REPO_PATH", .value = repo->path },
382 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
383 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
384 { .name = "CGIT_REPO_SECTION", .value = repo->section },
385 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
386 };
387 int env_var_count = ARRAY_SIZE(env_vars);
388 cgit_env_var *p, *q;
389 static char *warn = "cgit warning: failed to set env: %s=%s\n";
390
391 p = env_vars;
392 q = p + env_var_count;
393 for (; p < q; p++)
394 if (setenv(p->name, p->value, 1))
395 fprintf(stderr, warn, p->name, p->value);
396}
397
398int cgit_open_filter(struct cgit_filter *filter, struct cgit_repo * repo)
380{ 399{
381 400
382 filter->old_stdout = chk_positive(dup(STDOUT_FILENO), 401 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
@@ -387,6 +406,8 @@ int cgit_open_filter(struct cgit_filter *filter)
387 close(filter->pipe_fh[1]); 406 close(filter->pipe_fh[1]);
388 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO), 407 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
389 "Unable to use pipe as STDIN"); 408 "Unable to use pipe as STDIN");
409 if (repo)
410 prepare_env(repo);
390 execvp(filter->cmd, filter->argv); 411 execvp(filter->cmd, filter->argv);
391 die("Unable to exec subprocess %s: %s (%d)", filter->cmd, 412 die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
392 strerror(errno), errno); 413 strerror(errno), errno);