1 /* shared.c: global vars + some callback functions
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include <linux/limits.h>
13 struct cgit_repolist cgit_repolist
;
14 struct cgit_context ctx
;
16 int chk_zero(int result
, char *msg
)
19 die("%s: %s", msg
, strerror(errno
));
23 int chk_positive(int result
, char *msg
)
26 die("%s: %s", msg
, strerror(errno
));
30 int chk_non_negative(int result
, char *msg
)
33 die("%s: %s",msg
, strerror(errno
));
37 struct cgit_repo
*cgit_add_repo(const char *url
)
39 struct cgit_repo
*ret
;
41 if (++cgit_repolist
.count
> cgit_repolist
.length
) {
42 if (cgit_repolist
.length
== 0)
43 cgit_repolist
.length
= 8;
45 cgit_repolist
.length
*= 2;
46 cgit_repolist
.repos
= xrealloc(cgit_repolist
.repos
,
47 cgit_repolist
.length
*
48 sizeof(struct cgit_repo
));
51 ret
= &cgit_repolist
.repos
[cgit_repolist
.count
-1];
52 memset(ret
, 0, sizeof(struct cgit_repo
));
53 ret
->url
= trim_end(url
, '/');
56 ret
->desc
= "[no description]";
58 ret
->section
= ctx
.cfg
.section
;
59 ret
->defbranch
= "master";
60 ret
->snapshots
= ctx
.cfg
.snapshots
;
61 ret
->enable_commit_graph
= ctx
.cfg
.enable_commit_graph
;
62 ret
->enable_log_filecount
= ctx
.cfg
.enable_log_filecount
;
63 ret
->enable_log_linecount
= ctx
.cfg
.enable_log_linecount
;
64 ret
->enable_remote_branches
= ctx
.cfg
.enable_remote_branches
;
65 ret
->enable_subject_links
= ctx
.cfg
.enable_subject_links
;
66 ret
->max_stats
= ctx
.cfg
.max_stats
;
67 ret
->module_link
= ctx
.cfg
.module_link
;
68 ret
->readme
= ctx
.cfg
.readme
;
70 ret
->about_filter
= ctx
.cfg
.about_filter
;
71 ret
->commit_filter
= ctx
.cfg
.commit_filter
;
72 ret
->source_filter
= ctx
.cfg
.source_filter
;
73 ret
->clone_url
= ctx
.cfg
.clone_url
;
77 struct cgit_repo
*cgit_get_repoinfo(const char *url
)
80 struct cgit_repo
*repo
;
82 for (i
=0; i
<cgit_repolist
.count
; i
++) {
83 repo
= &cgit_repolist
.repos
[i
];
84 if (!strcmp(repo
->url
, url
))
90 void *cgit_free_commitinfo(struct commitinfo
*info
)
93 free(info
->author_email
);
94 free(info
->committer
);
95 free(info
->committer_email
);
98 free(info
->msg_encoding
);
103 char *trim_end(const char *str
, char c
)
110 while(len
> 0 && str
[len
- 1] == c
)
114 return xstrndup(str
, len
);
117 char *strlpart(char *txt
, int maxlen
)
124 if (strlen(txt
) <= maxlen
)
126 result
= xmalloc(maxlen
+ 1);
127 memcpy(result
, txt
, maxlen
- 3);
128 result
[maxlen
-1] = result
[maxlen
-2] = result
[maxlen
-3] = '.';
129 result
[maxlen
] = '\0';
133 char *strrpart(char *txt
, int maxlen
)
140 if (strlen(txt
) <= maxlen
)
142 result
= xmalloc(maxlen
+ 1);
143 memcpy(result
+ 3, txt
+ strlen(txt
) - maxlen
+ 4, maxlen
- 3);
144 result
[0] = result
[1] = result
[2] = '.';
148 void cgit_add_ref(struct reflist
*list
, struct refinfo
*ref
)
152 if (list
->count
>= list
->alloc
) {
153 list
->alloc
+= (list
->alloc
? list
->alloc
: 4);
154 size
= list
->alloc
* sizeof(struct refinfo
*);
155 list
->refs
= xrealloc(list
->refs
, size
);
157 list
->refs
[list
->count
++] = ref
;
160 struct refinfo
*cgit_mk_refinfo(const char *refname
, const unsigned char *sha1
)
164 ref
= xmalloc(sizeof (struct refinfo
));
165 ref
->refname
= xstrdup(refname
);
166 ref
->object
= parse_object(sha1
);
167 switch (ref
->object
->type
) {
169 ref
->tag
= cgit_parse_tag((struct tag
*)ref
->object
);
172 ref
->commit
= cgit_parse_commit((struct commit
*)ref
->object
);
178 int cgit_refs_cb(const char *refname
, const unsigned char *sha1
, int flags
,
181 struct reflist
*list
= (struct reflist
*)cb_data
;
182 struct refinfo
*info
= cgit_mk_refinfo(refname
, sha1
);
185 cgit_add_ref(list
, info
);
189 void cgit_diff_tree_cb(struct diff_queue_struct
*q
,
190 struct diff_options
*options
, void *data
)
194 for (i
= 0; i
< q
->nr
; i
++) {
195 if (q
->queue
[i
]->status
== 'U')
197 ((filepair_fn
)data
)(q
->queue
[i
]);
201 static int load_mmfile(mmfile_t
*file
, const unsigned char *sha1
)
203 enum object_type type
;
205 if (is_null_sha1(sha1
)) {
206 file
->ptr
= (char *)"
";
209 file->ptr = read_sha1_file(sha1, &type,
210 (unsigned long *)&file->size);
216 * Receive diff-buffers from xdiff and concatenate them as
217 * needed across multiple callbacks.
219 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
220 * ripped from git and modified to use globals instead of
221 * a special callback-struct.
223 char *diffbuf = NULL;
226 int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
230 for (i = 0; i < nbuf; i++) {
231 if (mb[i].ptr[mb[i].size-1] != '\n') {
232 /* Incomplete line */
233 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
234 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
235 buflen += mb[i].size;
239 /* we have a complete line */
241 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
244 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
245 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
246 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
252 ((linediff_fn)priv)(diffbuf, buflen);
260 int cgit_diff_files(const unsigned char *old_sha1,
261 const unsigned char *new_sha1, unsigned long *old_size,
262 unsigned long *new_size, int *binary, int context,
263 int ignorews, linediff_fn fn)
265 mmfile_t file1, file2;
266 xpparam_t diff_params;
267 xdemitconf_t emit_params;
270 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
273 *old_size = file1.size;
274 *new_size = file2.size;
276 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
277 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
286 memset(&diff_params, 0, sizeof(diff_params));
287 memset(&emit_params, 0, sizeof(emit_params));
288 memset(&emit_cb, 0, sizeof(emit_cb));
289 diff_params.flags = XDF_NEED_MINIMAL;
291 diff_params.flags |= XDF_IGNORE_WHITESPACE;
292 emit_params.ctxlen = context > 0 ? context : 3;
293 emit_params.flags = XDL_EMIT_FUNCNAMES;
294 emit_cb.outf = filediff_cb;
296 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
304 void cgit_diff_tree(const unsigned char *old_sha1,
305 const unsigned char *new_sha1,
306 filepair_fn fn, const char *prefix, int ignorews)
308 struct diff_options opt;
312 opt.output_format = DIFF_FORMAT_CALLBACK;
313 opt.detect_rename = 1;
314 opt.rename_limit = ctx.cfg.renamelimit;
315 DIFF_OPT_SET(&opt, RECURSIVE);
317 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
318 opt.format_callback = cgit_diff_tree_cb;
319 opt.format_callback_data = fn;
323 prefixlen = strlen(prefix);
324 opt.pathlens = &prefixlen;
326 diff_setup_done(&opt);
328 if (old_sha1 && !is_null_sha1(old_sha1))
329 diff_tree_sha1(old_sha1, new_sha1, "", &opt);
331 diff_root_tree_sha1(new_sha1, "", &opt);
336 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
338 unsigned char *old_sha1 = NULL;
341 old_sha1 = commit->parents->item->object.sha1;
342 cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
346 int cgit_parse_snapshots_mask(const char *str)
348 const struct cgit_snapshot_format *f;
349 static const char *delim = " \t,:/|;";
352 /* favor legacy setting */
356 str += strspn(str,delim);
357 tl = strcspn(str,delim);
360 for (f = cgit_snapshot_formats; f->suffix; f++) {
361 sl = strlen(f->suffix);
362 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
363 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
378 void cgit_prepare_repo_env(struct cgit_repo * repo)
380 cgit_env_var env_vars[] = {
381 { .name = "CGIT_REPO_URL
", .value = repo->url },
382 { .name = "CGIT_REPO_NAME
", .value = repo->name },
383 { .name = "CGIT_REPO_PATH
", .value = repo->path },
384 { .name = "CGIT_REPO_OWNER
", .value = repo->owner },
385 { .name = "CGIT_REPO_DEFBRANCH
", .value = repo->defbranch },
386 { .name = "CGIT_REPO_SECTION
", .value = repo->section },
387 { .name = "CGIT_REPO_CLONE_URL
", .value = repo->clone_url }
389 int env_var_count = ARRAY_SIZE(env_vars);
391 static char *warn = "cgit warning
: failed to set env
: %s
=%s
\n";
394 q = p + env_var_count;
396 if (setenv(p->name, p->value, 1))
397 fprintf(stderr, warn, p->name, p->value);
400 int cgit_open_filter(struct cgit_filter *filter)
403 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
404 "Unable to duplicate STDOUT
");
405 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess
");
406 filter->pid = chk_non_negative(fork(), "Unable to create subprocess
");
407 if (filter->pid == 0) {
408 close(filter->pipe_fh[1]);
409 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
410 "Unable to use pipe as STDIN
");
411 execvp(filter->cmd, filter->argv);
412 die("Unable to exec subprocess %s
: %s
(%d)"
, filter
->cmd
,
413 strerror(errno
), errno
);
415 close(filter
->pipe_fh
[0]);
416 chk_non_negative(dup2(filter
->pipe_fh
[1], STDOUT_FILENO
),
417 "Unable to use pipe as STDOUT");
418 close(filter
->pipe_fh
[1]);
422 int cgit_close_filter(struct cgit_filter
*filter
)
424 chk_non_negative(dup2(filter
->old_stdout
, STDOUT_FILENO
),
425 "Unable to restore STDOUT");
426 close(filter
->old_stdout
);
429 waitpid(filter
->pid
, &filter
->exitstatus
, 0);
430 if (WIFEXITED(filter
->exitstatus
) && !WEXITSTATUS(filter
->exitstatus
))
432 die("Subprocess %s exited abnormally", filter
->cmd
);
435 /* Read the content of the specified file into a newly allocated buffer,
436 * zeroterminate the buffer and return 0 on success, errno otherwise.
438 int readfile(const char *path
, char **buf
, size_t *size
)
443 fd
= open(path
, O_RDONLY
);
446 if (fstat(fd
, &st
)) {
451 if (!S_ISREG(st
.st_mode
)) {
455 *buf
= xmalloc(st
.st_size
+ 1);
456 *size
= read_in_full(fd
, *buf
, st
.st_size
);
458 (*buf
)[*size
] = '\0';
460 return (*size
== st
.st_size
? 0 : e
);
463 int is_token_char(char c
)
465 return isalnum(c
) || c
== '_';
468 /* Replace name with getenv(name), return pointer to zero-terminating char
470 char *expand_macro(char *name
, int maxlength
)
476 value
= getenv(name
);
481 strncpy(name
, value
, len
);
486 #define EXPBUFSIZE (1024 * 8)
488 /* Replace all tokens prefixed by '$' in the specified text with the
489 * value of the named environment variable.
490 * NB: the return value is a static buffer, i.e. it must be strdup'd
493 char *expand_macros(const char *txt
)
495 static char result
[EXPBUFSIZE
];
501 while (p
< result
+ EXPBUFSIZE
- 1 && txt
&& *txt
) {
504 if (!is_token_char(*txt
)) {
507 len
= result
+ EXPBUFSIZE
- start
- 1;
508 p
= expand_macro(start
, len
) - 1;
526 if (start
&& p
- start
> 0) {
527 len
= result
+ EXPBUFSIZE
- start
- 1;
528 p
= expand_macro(start
, len
);