all repos — cgit @ 2159414a3945f059a7e52a4900ffcd1ca665e102

a hyperfast web frontend for git written in c

shared.c (view raw)

  1/* shared.c: global vars + some callback functions
  2 *
  3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
  4 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include "cgit.h"
 10#include <stdio.h>
 11
 12struct cgit_repolist cgit_repolist;
 13struct cgit_context ctx;
 14
 15int chk_zero(int result, char *msg)
 16{
 17	if (result != 0)
 18		die_errno("%s", msg);
 19	return result;
 20}
 21
 22int chk_positive(int result, char *msg)
 23{
 24	if (result <= 0)
 25		die_errno("%s", msg);
 26	return result;
 27}
 28
 29int chk_non_negative(int result, char *msg)
 30{
 31	if (result < 0)
 32		die_errno("%s", msg);
 33	return result;
 34}
 35
 36char *cgit_default_repo_desc = "[no description]";
 37struct cgit_repo *cgit_add_repo(const char *url)
 38{
 39	struct cgit_repo *ret;
 40
 41	if (++cgit_repolist.count > cgit_repolist.length) {
 42		if (cgit_repolist.length == 0)
 43			cgit_repolist.length = 8;
 44		else
 45			cgit_repolist.length *= 2;
 46		cgit_repolist.repos = xrealloc(cgit_repolist.repos,
 47					       cgit_repolist.length *
 48					       sizeof(struct cgit_repo));
 49	}
 50
 51	ret = &cgit_repolist.repos[cgit_repolist.count-1];
 52	memset(ret, 0, sizeof(struct cgit_repo));
 53	ret->url = trim_end(url, '/');
 54	ret->name = ret->url;
 55	ret->path = NULL;
 56	ret->desc = cgit_default_repo_desc;
 57	ret->owner = NULL;
 58	ret->section = ctx.cfg.section;
 59	ret->snapshots = ctx.cfg.snapshots;
 60	ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
 61	ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
 62	ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
 63	ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
 64	ret->enable_subject_links = ctx.cfg.enable_subject_links;
 65	ret->max_stats = ctx.cfg.max_stats;
 66	ret->branch_sort = ctx.cfg.branch_sort;
 67	ret->commit_sort = ctx.cfg.commit_sort;
 68	ret->module_link = ctx.cfg.module_link;
 69	ret->readme = ctx.cfg.readme;
 70	ret->mtime = -1;
 71	ret->about_filter = ctx.cfg.about_filter;
 72	ret->commit_filter = ctx.cfg.commit_filter;
 73	ret->source_filter = ctx.cfg.source_filter;
 74	ret->email_filter = ctx.cfg.email_filter;
 75	ret->owner_filter = ctx.cfg.owner_filter;
 76	ret->clone_url = ctx.cfg.clone_url;
 77	ret->submodules.strdup_strings = 1;
 78	return ret;
 79}
 80
 81struct cgit_repo *cgit_get_repoinfo(const char *url)
 82{
 83	int i;
 84	struct cgit_repo *repo;
 85
 86	for (i = 0; i < cgit_repolist.count; i++) {
 87		repo = &cgit_repolist.repos[i];
 88		if (!strcmp(repo->url, url))
 89			return repo;
 90	}
 91	return NULL;
 92}
 93
 94void *cgit_free_commitinfo(struct commitinfo *info)
 95{
 96	free(info->author);
 97	free(info->author_email);
 98	free(info->committer);
 99	free(info->committer_email);
100	free(info->subject);
101	free(info->msg);
102	free(info->msg_encoding);
103	free(info);
104	return NULL;
105}
106
107char *trim_end(const char *str, char c)
108{
109	int len;
110
111	if (str == NULL)
112		return NULL;
113	len = strlen(str);
114	while (len > 0 && str[len - 1] == c)
115		len--;
116	if (len == 0)
117		return NULL;
118	return xstrndup(str, len);
119}
120
121char *ensure_end(const char *str, char c)
122{
123	size_t len = strlen(str);
124	char *result;
125
126	if (len && str[len - 1] == c)
127		return xstrndup(str, len);
128
129	result = xmalloc(len + 2);
130	memcpy(result, str, len);
131	result[len] = '/';
132	result[len + 1] = '\0';
133	return result;
134}
135
136void strbuf_ensure_end(struct strbuf *sb, char c)
137{
138	if (!sb->len || sb->buf[sb->len - 1] != c)
139		strbuf_addch(sb, c);
140}
141
142char *strlpart(char *txt, int maxlen)
143{
144	char *result;
145
146	if (!txt)
147		return txt;
148
149	if (strlen(txt) <= maxlen)
150		return txt;
151	result = xmalloc(maxlen + 1);
152	memcpy(result, txt, maxlen - 3);
153	result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
154	result[maxlen] = '\0';
155	return result;
156}
157
158char *strrpart(char *txt, int maxlen)
159{
160	char *result;
161
162	if (!txt)
163		return txt;
164
165	if (strlen(txt) <= maxlen)
166		return txt;
167	result = xmalloc(maxlen + 1);
168	memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
169	result[0] = result[1] = result[2] = '.';
170	return result;
171}
172
173void cgit_add_ref(struct reflist *list, struct refinfo *ref)
174{
175	size_t size;
176
177	if (list->count >= list->alloc) {
178		list->alloc += (list->alloc ? list->alloc : 4);
179		size = list->alloc * sizeof(struct refinfo *);
180		list->refs = xrealloc(list->refs, size);
181	}
182	list->refs[list->count++] = ref;
183}
184
185static struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
186{
187	struct refinfo *ref;
188
189	ref = xmalloc(sizeof (struct refinfo));
190	ref->refname = xstrdup(refname);
191	ref->object = parse_object(sha1);
192	switch (ref->object->type) {
193	case OBJ_TAG:
194		ref->tag = cgit_parse_tag((struct tag *)ref->object);
195		break;
196	case OBJ_COMMIT:
197		ref->commit = cgit_parse_commit((struct commit *)ref->object);
198		break;
199	}
200	return ref;
201}
202
203static void cgit_free_taginfo(struct taginfo *tag)
204{
205	if (tag->tagger)
206		free(tag->tagger);
207	if (tag->tagger_email)
208		free(tag->tagger_email);
209	if (tag->msg)
210		free(tag->msg);
211	free(tag);
212}
213
214static void cgit_free_refinfo(struct refinfo *ref)
215{
216	if (ref->refname)
217		free((char *)ref->refname);
218	switch (ref->object->type) {
219	case OBJ_TAG:
220		cgit_free_taginfo(ref->tag);
221		break;
222	case OBJ_COMMIT:
223		cgit_free_commitinfo(ref->commit);
224		break;
225	}
226	free(ref);
227}
228
229void cgit_free_reflist_inner(struct reflist *list)
230{
231	int i;
232
233	for (i = 0; i < list->count; i++) {
234		cgit_free_refinfo(list->refs[i]);
235	}
236	free(list->refs);
237}
238
239int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
240		  void *cb_data)
241{
242	struct reflist *list = (struct reflist *)cb_data;
243	struct refinfo *info = cgit_mk_refinfo(refname, sha1);
244
245	if (info)
246		cgit_add_ref(list, info);
247	return 0;
248}
249
250static void cgit_diff_tree_cb(struct diff_queue_struct *q,
251			      struct diff_options *options, void *data)
252{
253	int i;
254
255	for (i = 0; i < q->nr; i++) {
256		if (q->queue[i]->status == 'U')
257			continue;
258		((filepair_fn)data)(q->queue[i]);
259	}
260}
261
262static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
263{
264	enum object_type type;
265
266	if (is_null_sha1(sha1)) {
267		file->ptr = (char *)"";
268		file->size = 0;
269	} else {
270		file->ptr = read_sha1_file(sha1, &type,
271		                           (unsigned long *)&file->size);
272	}
273	return 1;
274}
275
276/*
277 * Receive diff-buffers from xdiff and concatenate them as
278 * needed across multiple callbacks.
279 *
280 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
281 * ripped from git and modified to use globals instead of
282 * a special callback-struct.
283 */
284char *diffbuf = NULL;
285int buflen = 0;
286
287static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
288{
289	int i;
290
291	for (i = 0; i < nbuf; i++) {
292		if (mb[i].ptr[mb[i].size-1] != '\n') {
293			/* Incomplete line */
294			diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
295			memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
296			buflen += mb[i].size;
297			continue;
298		}
299
300		/* we have a complete line */
301		if (!diffbuf) {
302			((linediff_fn)priv)(mb[i].ptr, mb[i].size);
303			continue;
304		}
305		diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
306		memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
307		((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
308		free(diffbuf);
309		diffbuf = NULL;
310		buflen = 0;
311	}
312	if (diffbuf) {
313		((linediff_fn)priv)(diffbuf, buflen);
314		free(diffbuf);
315		diffbuf = NULL;
316		buflen = 0;
317	}
318	return 0;
319}
320
321int cgit_diff_files(const unsigned char *old_sha1,
322		    const unsigned char *new_sha1, unsigned long *old_size,
323		    unsigned long *new_size, int *binary, int context,
324		    int ignorews, linediff_fn fn)
325{
326	mmfile_t file1, file2;
327	xpparam_t diff_params;
328	xdemitconf_t emit_params;
329	xdemitcb_t emit_cb;
330
331	if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
332		return 1;
333
334	*old_size = file1.size;
335	*new_size = file2.size;
336
337	if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
338	    (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
339		*binary = 1;
340		if (file1.size)
341			free(file1.ptr);
342		if (file2.size)
343			free(file2.ptr);
344		return 0;
345	}
346
347	memset(&diff_params, 0, sizeof(diff_params));
348	memset(&emit_params, 0, sizeof(emit_params));
349	memset(&emit_cb, 0, sizeof(emit_cb));
350	diff_params.flags = XDF_NEED_MINIMAL;
351	if (ignorews)
352		diff_params.flags |= XDF_IGNORE_WHITESPACE;
353	emit_params.ctxlen = context > 0 ? context : 3;
354	emit_params.flags = XDL_EMIT_FUNCNAMES;
355	emit_cb.outf = filediff_cb;
356	emit_cb.priv = fn;
357	xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
358	if (file1.size)
359		free(file1.ptr);
360	if (file2.size)
361		free(file2.ptr);
362	return 0;
363}
364
365void cgit_diff_tree(const unsigned char *old_sha1,
366		    const unsigned char *new_sha1,
367		    filepair_fn fn, const char *prefix, int ignorews)
368{
369	struct diff_options opt;
370	struct pathspec_item item;
371
372	memset(&item, 0, sizeof(item));
373	diff_setup(&opt);
374	opt.output_format = DIFF_FORMAT_CALLBACK;
375	opt.detect_rename = 1;
376	opt.rename_limit = ctx.cfg.renamelimit;
377	DIFF_OPT_SET(&opt, RECURSIVE);
378	if (ignorews)
379		DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
380	opt.format_callback = cgit_diff_tree_cb;
381	opt.format_callback_data = fn;
382	if (prefix) {
383		item.match = prefix;
384		item.len = strlen(prefix);
385		opt.pathspec.nr = 1;
386		opt.pathspec.items = &item;
387	}
388	diff_setup_done(&opt);
389
390	if (old_sha1 && !is_null_sha1(old_sha1))
391		diff_tree_sha1(old_sha1, new_sha1, "", &opt);
392	else
393		diff_root_tree_sha1(new_sha1, "", &opt);
394	diffcore_std(&opt);
395	diff_flush(&opt);
396}
397
398void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
399{
400	unsigned char *old_sha1 = NULL;
401
402	if (commit->parents)
403		old_sha1 = commit->parents->item->object.sha1;
404	cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
405		       ctx.qry.ignorews);
406}
407
408int cgit_parse_snapshots_mask(const char *str)
409{
410	struct string_list tokens = STRING_LIST_INIT_DUP;
411	struct string_list_item *item;
412	const struct cgit_snapshot_format *f;
413	int rv = 0;
414
415	/* favor legacy setting */
416	if (atoi(str))
417		return 1;
418
419	string_list_split(&tokens, str, ' ', -1);
420	string_list_remove_empty_items(&tokens, 0);
421
422	for_each_string_list_item(item, &tokens) {
423		for (f = cgit_snapshot_formats; f->suffix; f++) {
424			if (!strcmp(item->string, f->suffix) ||
425			    !strcmp(item->string, f->suffix + 1)) {
426				rv |= f->bit;
427				break;
428			}
429		}
430	}
431
432	string_list_clear(&tokens, 0);
433	return rv;
434}
435
436typedef struct {
437	char * name;
438	char * value;
439} cgit_env_var;
440
441void cgit_prepare_repo_env(struct cgit_repo * repo)
442{
443	cgit_env_var env_vars[] = {
444		{ .name = "CGIT_REPO_URL", .value = repo->url },
445		{ .name = "CGIT_REPO_NAME", .value = repo->name },
446		{ .name = "CGIT_REPO_PATH", .value = repo->path },
447		{ .name = "CGIT_REPO_OWNER", .value = repo->owner },
448		{ .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
449		{ .name = "CGIT_REPO_SECTION", .value = repo->section },
450		{ .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
451	};
452	int env_var_count = ARRAY_SIZE(env_vars);
453	cgit_env_var *p, *q;
454	static char *warn = "cgit warning: failed to set env: %s=%s\n";
455
456	p = env_vars;
457	q = p + env_var_count;
458	for (; p < q; p++)
459		if (p->value && setenv(p->name, p->value, 1))
460			fprintf(stderr, warn, p->name, p->value);
461}
462
463/* Read the content of the specified file into a newly allocated buffer,
464 * zeroterminate the buffer and return 0 on success, errno otherwise.
465 */
466int readfile(const char *path, char **buf, size_t *size)
467{
468	int fd, e;
469	struct stat st;
470
471	fd = open(path, O_RDONLY);
472	if (fd == -1)
473		return errno;
474	if (fstat(fd, &st)) {
475		e = errno;
476		close(fd);
477		return e;
478	}
479	if (!S_ISREG(st.st_mode)) {
480		close(fd);
481		return EISDIR;
482	}
483	*buf = xmalloc(st.st_size + 1);
484	*size = read_in_full(fd, *buf, st.st_size);
485	e = errno;
486	(*buf)[*size] = '\0';
487	close(fd);
488	return (*size == st.st_size ? 0 : e);
489}
490
491static int is_token_char(char c)
492{
493	return isalnum(c) || c == '_';
494}
495
496/* Replace name with getenv(name), return pointer to zero-terminating char
497 */
498static char *expand_macro(char *name, int maxlength)
499{
500	char *value;
501	int len;
502
503	len = 0;
504	value = getenv(name);
505	if (value) {
506		len = strlen(value);
507		if (len > maxlength)
508			len = maxlength;
509		strncpy(name, value, len);
510	}
511	return name + len;
512}
513
514#define EXPBUFSIZE (1024 * 8)
515
516/* Replace all tokens prefixed by '$' in the specified text with the
517 * value of the named environment variable.
518 * NB: the return value is a static buffer, i.e. it must be strdup'd
519 * by the caller.
520 */
521char *expand_macros(const char *txt)
522{
523	static char result[EXPBUFSIZE];
524	char *p, *start;
525	int len;
526
527	p = result;
528	start = NULL;
529	while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
530		*p = *txt;
531		if (start) {
532			if (!is_token_char(*txt)) {
533				if (p - start > 0) {
534					*p = '\0';
535					len = result + EXPBUFSIZE - start - 1;
536					p = expand_macro(start, len) - 1;
537				}
538				start = NULL;
539				txt--;
540			}
541			p++;
542			txt++;
543			continue;
544		}
545		if (*txt == '$') {
546			start = p;
547			txt++;
548			continue;
549		}
550		p++;
551		txt++;
552	}
553	*p = '\0';
554	if (start && p - start > 0) {
555		len = result + EXPBUFSIZE - start - 1;
556		p = expand_macro(start, len);
557		*p = '\0';
558	}
559	return result;
560}