all repos — cgit @ 52e605caf573fa20fdd4fbac5e1cc69b7740b1f5

a hyperfast web frontend for git written in c

cgit.c (view raw)

  1/* cgit.c: cgi for the git scm
  2 *
  3 * Copyright (C) 2006 Lars Hjemli
  4 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include "cgit.h"
 10
 11const char cgit_version[] = CGIT_VERSION;
 12
 13static void cgit_print_repo_page(struct cacheitem *item)
 14{
 15	if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || 
 16	    cgit_read_config("info/cgit", cgit_repo_config_cb)) {
 17		char *title = fmt("%s - %s", cgit_root_title, "Bad request");
 18		cgit_print_docstart(title, item);
 19		cgit_print_pageheader(title, 0);
 20		cgit_print_error(fmt("Unable to scan repository: %s",
 21				     strerror(errno)));
 22		cgit_print_docend();
 23		return;
 24	}
 25	setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
 26	char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
 27	int show_search = 0;
 28	if (cgit_query_page && !strcmp(cgit_query_page, "log"))
 29		show_search = 1;
 30	cgit_print_docstart(title, item);
 31	cgit_print_pageheader(title, show_search);
 32	if (!cgit_query_page) {
 33		cgit_print_summary();
 34	} else if (!strcmp(cgit_query_page, "log")) {
 35		cgit_print_log(cgit_query_head, cgit_query_ofs, 100, cgit_query_search);
 36	} else if (!strcmp(cgit_query_page, "tree")) {
 37		cgit_print_tree(cgit_query_sha1);
 38	} else if (!strcmp(cgit_query_page, "commit")) {
 39		cgit_print_commit(cgit_query_sha1);
 40	} else if (!strcmp(cgit_query_page, "view")) {
 41		cgit_print_view(cgit_query_sha1);
 42	} else if (!strcmp(cgit_query_page, "diff")) {
 43		cgit_print_diff(cgit_query_sha1, cgit_query_sha2);
 44	}
 45	cgit_print_docend();
 46}
 47
 48static void cgit_fill_cache(struct cacheitem *item)
 49{
 50	static char buf[PATH_MAX];
 51
 52	getcwd(buf, sizeof(buf));
 53	htmlfd = item->fd;
 54	item->st.st_mtime = time(NULL);
 55	if (cgit_query_repo)
 56		cgit_print_repo_page(item);
 57	else
 58		cgit_print_repolist(item);
 59	chdir(buf);
 60}
 61
 62static void cgit_check_cache(struct cacheitem *item)
 63{
 64	int i = 0;
 65
 66	cache_prepare(item);
 67 top:
 68	if (++i > cgit_max_lock_attempts) {
 69		die("cgit_refresh_cache: unable to lock %s: %s",
 70		    item->name, strerror(errno));
 71	}
 72	if (!cache_exist(item)) {
 73		if (!cache_lock(item)) {
 74			sleep(1);
 75			goto top;
 76		}
 77		if (!cache_exist(item)) {
 78			cgit_fill_cache(item);
 79			cache_unlock(item);
 80		} else {
 81			cache_cancel_lock(item);
 82		}
 83	} else if (cache_expired(item) && cache_lock(item)) {
 84		if (cache_expired(item)) {
 85			cgit_fill_cache(item);
 86			cache_unlock(item);
 87		} else {
 88			cache_cancel_lock(item);
 89		}
 90	}
 91}
 92
 93static void cgit_print_cache(struct cacheitem *item)
 94{
 95	static char buf[4096];
 96	ssize_t i;
 97
 98	int fd = open(item->name, O_RDONLY);
 99	if (fd<0)
100		die("Unable to open cached file %s", item->name);
101
102	while((i=read(fd, buf, sizeof(buf))) > 0)
103		write(STDOUT_FILENO, buf, i);
104
105	close(fd);
106}
107
108static void cgit_parse_args(int argc, const char **argv)
109{
110	int i;
111
112	for (i = 1; i < argc; i++) {
113		if (!strncmp(argv[i], "--root=", 7)) {
114			cgit_root = xstrdup(argv[i]+7);
115		}
116		if (!strncmp(argv[i], "--cache=", 8)) {
117			cgit_cache_root = xstrdup(argv[i]+8);
118		}
119		if (!strcmp(argv[i], "--nocache")) {
120			cgit_nocache = 1;
121		}
122		if (!strncmp(argv[i], "--query=", 8)) {
123			cgit_querystring = xstrdup(argv[i]+8);
124		}
125		if (!strncmp(argv[i], "--repo=", 7)) {
126			cgit_query_repo = xstrdup(argv[i]+7);
127		}
128		if (!strncmp(argv[i], "--page=", 7)) {
129			cgit_query_page = xstrdup(argv[i]+7);
130		}
131		if (!strncmp(argv[i], "--head=", 7)) {
132			cgit_query_head = xstrdup(argv[i]+7);
133			cgit_query_has_symref = 1;
134		}
135		if (!strncmp(argv[i], "--sha1=", 7)) {
136			cgit_query_sha1 = xstrdup(argv[i]+7);
137			cgit_query_has_sha1 = 1;
138		}
139		if (!strncmp(argv[i], "--ofs=", 6)) {
140			cgit_query_ofs = atoi(argv[i]+6);
141		}
142	}
143}
144
145int main(int argc, const char **argv)
146{
147	struct cacheitem item;
148
149	cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
150	if (getenv("QUERY_STRING"))
151		cgit_querystring = xstrdup(getenv("QUERY_STRING"));
152	cgit_parse_args(argc, argv);
153	cgit_parse_query(cgit_querystring, cgit_querystring_cb);
154
155	if (cgit_nocache) {
156		item.fd = STDOUT_FILENO;
157		cgit_fill_cache(&item);
158	} else {
159		cgit_check_cache(&item);
160		cgit_print_cache(&item);
161	}
162	return 0;
163}