summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--.gitignore1
-rw-r--r--Makefile6
-rw-r--r--README54
-rw-r--r--cache.c86
-rw-r--r--cgit.c117
-rw-r--r--cgit.h47
-rw-r--r--config.c4
-rw-r--r--git.h60
-rw-r--r--html.c6
9 files changed, 353 insertions, 28 deletions
diff --git a/.gitignore b/.gitignore
index 4eaec97..c4c9ac3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3 +3,2 @@ cgit
*.o
+*~
diff --git a/Makefile b/Makefile
index 4e72b07..243f590 100644
--- a/Makefile
+++ b/Makefile
@@ -5,3 +5,5 @@ INSTALL_CSS = /var/www/htdocs/cgit.css
EXTLIBS = ../git/libgit.a ../git/xdiff/lib.a -lz -lcrypto
-OBJECTS = cgit.o config.o html.o
+OBJECTS = cgit.o config.o html.o cache.o
+
+CFLAGS += -Wall
@@ -17,3 +19,3 @@ clean:
cgit: $(OBJECTS)
- $(CC) -o cgit $(OBJECTS) $(EXTLIBS)
+ $(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
diff --git a/README b/README
new file mode 100644
index 0000000..5917c37
--- a/dev/null
+++ b/README
@@ -0,0 +1,54 @@
+Cache algorithm
+===============
+
+Cgit normally returns cached pages when invoked. If there is no cache file, or
+the cache file has expired, it is regenerated. Finally, the cache file is
+printed on stdout.
+
+When it is decided that a cache file needs to be regenerated, an attempt is
+made to create a corresponding lockfile. If this fails, the process gives up
+and uses the expired cache file instead.
+
+When there is no cache file for a request, an attempt is made to create a
+corresponding lockfile. If this fails, the process calls sched_yield(2) before
+restarting the request handling.
+
+In pseudocode:
+
+ name = generate_cache_name(request);
+top:
+ if (!exists(name)) {
+ if (lock_cache(name)) {
+ generate_cache(request, name);
+ unlock_cache(name);
+ } else {
+ sched_yield();
+ goto top;
+ }
+ } else if (expired(name)) {
+ if (lock_cache(name)) {
+ generate_cache(request, name);
+ unlock_cache(name);
+ }
+ }
+ print_file(name);
+
+
+The following options can be set in /etc/cgitrc to control cache behaviour:
+ cache-root: root directory for cache files
+ cache-root-ttl: TTL for the repo listing page
+ cache-repo-ttl: TTL for any repos summary page
+ cache-dynamic-ttl: TTL for pages with symbolic references (not SHA1)
+ cache-static-ttl: TTL for pages with sha1 references
+
+TTL is specified in minutes, -1 meaning "infinite caching".
+
+
+Naming of cache files
+---------------------
+Repository listing: <cachedir>/index.html
+Repository summary: <cachedir>/<repo>/index.html
+Repository subpage: <cachedir>/<repo>/<page>/<querystring>.html
+
+The corresponding lock files have a ".lock" suffix.
+
diff --git a/cache.c b/cache.c
new file mode 100644
index 0000000..1be1ea4
--- a/dev/null
+++ b/cache.c
@@ -0,0 +1,86 @@
+#include "cgit.h"
+
+const int NOLOCK = -1;
+
+int cache_lookup(struct cacheitem *item)
+{
+ if (!cgit_query_repo) {
+ item->name = xstrdup(fmt("%s/index.html", cgit_cache_root));
+ item->ttl = cgit_cache_root_ttl;
+ } else if (!cgit_query_page) {
+ item->name = xstrdup(fmt("%s/%s/index.html", cgit_cache_root,
+ cgit_query_repo));
+ item->ttl = cgit_cache_repo_ttl;
+ } else {
+ item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root,
+ cgit_query_repo, cgit_query_page,
+ cgit_querystring));
+ if (cgit_query_has_symref)
+ item->ttl = cgit_cache_dynamic_ttl;
+ else if (cgit_query_has_sha1)
+ item->ttl = cgit_cache_static_ttl;
+ else
+ item->ttl = cgit_cache_repo_ttl;
+ }
+ if (stat(item->name, &item->st)) {
+ item->st.st_mtime = 0;
+ return 0;
+ }
+ return 1;
+}
+
+int cache_create_dirs()
+{
+ char *path;
+
+ if (!cgit_query_repo)
+ return 0;
+
+ path = fmt("%s/%s", cgit_cache_root, cgit_query_repo);
+ if (mkdir(path, S_IRWXU) && errno!=EEXIST)
+ return 0;
+
+ if (cgit_query_page) {
+ path = fmt("%s/%s/%s", cgit_cache_root, cgit_query_repo,
+ cgit_query_page);
+ if (mkdir(path, S_IRWXU) && errno!=EEXIST)
+ return 0;
+ }
+ return 1;
+}
+
+int cache_lock(struct cacheitem *item)
+{
+ int ret;
+ char *lockfile = fmt("%s.lock", item->name);
+
+ top:
+ item->fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR);
+ if (item->fd == NOLOCK && errno == ENOENT && cache_create_dirs())
+ goto top;
+ if (item->fd == NOLOCK && errno == EEXIST) {
+ struct stat st;
+ time_t t;
+ if (stat(lockfile, &st))
+ return ret;
+ t = time(NULL);
+ if (t-st.st_mtime > cgit_cache_max_create_time &&
+ !unlink(lockfile))
+ goto top;
+ return 0;
+ }
+ return (item->fd > 0);
+}
+
+int cache_unlock(struct cacheitem *item)
+{
+ close(item->fd);
+ return (rename(fmt("%s.lock", item->name), item->name) == 0);
+}
+
+int cache_expired(struct cacheitem *item)
+{
+ if (item->ttl < 0)
+ return 0;
+ return item->st.st_mtime + item->ttl * 60 < time(NULL);
+}
diff --git a/cgit.c b/cgit.c
index 4c14f77..09c857c 100644
--- a/cgit.c
+++ b/cgit.c
@@ -12,4 +12,5 @@ static const char cgit_lib_error[] =
+int htmlfd = 0;
-char *cgit_root = "/var/git";
+char *cgit_root = "/usr/src/git";
char *cgit_root_title = "Git repository browser";
@@ -20,2 +21,10 @@ char *cgit_virtual_root = NULL;
+char *cgit_cache_root = "/var/cache/cgit";
+
+int cgit_cache_root_ttl = 5;
+int cgit_cache_repo_ttl = 5;
+int cgit_cache_dynamic_ttl = 5;
+int cgit_cache_static_ttl = -1;
+int cgit_cache_max_create_time = 5;
+
char *cgit_repo_name = NULL;
@@ -24,2 +33,6 @@ char *cgit_repo_owner = NULL;
+int cgit_query_has_symref = 0;
+int cgit_query_has_sha1 = 0;
+
+char *cgit_querystring = NULL;
char *cgit_query_repo = NULL;
@@ -27,2 +40,5 @@ char *cgit_query_page = NULL;
char *cgit_query_head = NULL;
+char *cgit_query_sha1 = NULL;
+
+struct cacheitem cacheitem;
@@ -30,3 +46,3 @@ int cgit_parse_query(char *txt, configfn fn)
{
- char *t = txt, *value = NULL, c;
+ char *t, *value = NULL, c;
@@ -35,2 +51,4 @@ int cgit_parse_query(char *txt, configfn fn)
+ t = txt = xstrdup(txt);
+
while((c=*t) != '\0') {
@@ -84,4 +102,9 @@ void cgit_querystring_cb(const char *name, const char *value)
cgit_query_page = xstrdup(value);
- else if (!strcmp(name, "h"))
+ else if (!strcmp(name, "h")) {
cgit_query_head = xstrdup(value);
+ cgit_query_has_symref = 1;
+ } else if (!strcmp(name, "id")) {
+ cgit_query_sha1 = xstrdup(value);
+ cgit_query_has_sha1 = 1;
+ }
}
@@ -138,2 +161,22 @@ static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
+/* Sun, 06 Nov 1994 08:49:37 GMT */
+static char *http_date(time_t t)
+{
+ static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
+ static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
+ struct tm *tm = gmtime(&t);
+ return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
+ tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+}
+
+static int ttl_seconds(int ttl)
+{
+ if (ttl<0)
+ return 60 * 60 * 24 * 365;
+ else
+ return ttl * 60;
+}
+
static void cgit_print_docstart(char *title)
@@ -141,2 +184,5 @@ static void cgit_print_docstart(char *title)
html("Content-Type: text/html; charset=utf-8\n");
+ htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime));
+ htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime +
+ ttl_seconds(cacheitem.ttl)));
html("\n");
@@ -177,2 +223,3 @@ static void cgit_print_repolist()
+ chdir(cgit_root);
cgit_print_docstart(cgit_root_title);
@@ -199,3 +246,3 @@ static void cgit_print_repolist()
cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
- name = fmt("%s/.git/info/cgit", de->d_name);
+ name = fmt("%s/info/cgit", de->d_name);
if (cgit_read_config(name, cgit_repo_config_cb))
@@ -293,3 +340,3 @@ static void cgit_print_commit_shortlog(struct commit *commit)
html("</td><td>");
- char *qry = fmt("h=%s", sha1_to_hex(commit->object.sha1));
+ char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1));
char *url = cgit_pageurl(cgit_query_repo, "view", qry);
@@ -373,4 +420,4 @@ static void cgit_print_repo_page()
{
- if (chdir(cgit_query_repo) ||
- cgit_read_config(".git/info/cgit", cgit_repo_config_cb)) {
+ if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
+ cgit_read_config("info/cgit", cgit_repo_config_cb)) {
char *title = fmt("%s - %s", cgit_root_title, "Bad request");
@@ -383,3 +430,3 @@ static void cgit_print_repo_page()
}
-
+ setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
@@ -392,3 +439,3 @@ static void cgit_print_repo_page()
} else if (!strcmp(cgit_query_page, "view")) {
- cgit_print_object(cgit_query_head);
+ cgit_print_object(cgit_query_sha1);
}
@@ -397,9 +444,6 @@ static void cgit_print_repo_page()
-int main(int argc, const char **argv)
+static void cgit_fill_cache(struct cacheitem *item)
{
- if (cgit_read_config("/etc/cgitrc", cgit_global_config_cb))
- die("Error reading config: %d %s", errno, strerror(errno));
-
- chdir(cgit_root);
- cgit_parse_query(getenv("QUERY_STRING"), cgit_querystring_cb);
+ htmlfd = item->fd;
+ item->st.st_mtime = time(NULL);
if (cgit_query_repo)
@@ -408,2 +452,45 @@ int main(int argc, const char **argv)
cgit_print_repolist();
+}
+
+static void cgit_refresh_cache(struct cacheitem *item)
+{
+ top:
+ if (!cache_lookup(item)) {
+ if (cache_lock(item)) {
+ cgit_fill_cache(item);
+ cache_unlock(item);
+ } else {
+ sched_yield();
+ goto top;
+ }
+ } else if (cache_expired(item)) {
+ if (cache_lock(item)) {
+ cgit_fill_cache(item);
+ cache_unlock(item);
+ }
+ }
+}
+
+static void cgit_print_cache(struct cacheitem *item)
+{
+ static char buf[4096];
+ ssize_t i;
+
+ int fd = open(item->name, O_RDONLY);
+ if (fd<0)
+ die("Unable to open cached file %s", item->name);
+
+ while((i=read(fd, buf, sizeof(buf))) > 0)
+ write(STDOUT_FILENO, buf, i);
+
+ close(fd);
+}
+
+int main(int argc, const char **argv)
+{
+ cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
+ cgit_querystring = xstrdup(getenv("QUERY_STRING"));
+ cgit_parse_query(cgit_querystring, cgit_querystring_cb);
+ cgit_refresh_cache(&cacheitem);
+ cgit_print_cache(&cacheitem);
return 0;
diff --git a/cgit.h b/cgit.h
index 19f7ba7..1e084d4 100644
--- a/cgit.h
+++ b/cgit.h
@@ -5,2 +5,42 @@
#include <openssl/sha.h>
+#include <ctype.h>
+#include <sched.h>
+
+typedef void (*configfn)(const char *name, const char *value);
+
+struct cacheitem {
+ char *name;
+ struct stat st;
+ int ttl;
+ int fd;
+};
+
+extern char *cgit_root;
+extern char *cgit_root_title;
+extern char *cgit_css;
+extern char *cgit_logo;
+extern char *cgit_logo_link;
+extern char *cgit_virtual_root;
+extern char *cgit_cache_root;
+
+extern int cgit_cache_root_ttl;
+extern int cgit_cache_repo_ttl;
+extern int cgit_cache_dynamic_ttl;
+extern int cgit_cache_static_ttl;
+extern int cgit_cache_max_create_time;
+
+extern char *cgit_repo_name;
+extern char *cgit_repo_desc;
+extern char *cgit_repo_owner;
+
+extern int cgit_query_has_symref;
+extern int cgit_query_has_sha1;
+
+extern char *cgit_querystring;
+extern char *cgit_query_repo;
+extern char *cgit_query_page;
+extern char *cgit_query_head;
+extern char *cgit_query_sha1;
+
+extern int htmlfd;
@@ -12,3 +52,2 @@ extern void html_txt(char *txt);
extern void html_attr(char *txt);
-
extern void html_link_open(char *url, char *title, char *class);
@@ -16,3 +55,2 @@ extern void html_link_close(void);
-typedef void (*configfn)(const char *name, const char *value);
@@ -20,2 +58,7 @@ extern int cgit_read_config(const char *filename, configfn fn);
+extern int cache_lookup(struct cacheitem *item);
+extern int cache_lock(struct cacheitem *item);
+extern int cache_unlock(struct cacheitem *item);
+extern int cache_expired(struct cacheitem *item);
+
#endif /* CGIT_H */
diff --git a/config.c b/config.c
index 858ab69..ee49b62 100644
--- a/config.c
+++ b/config.c
@@ -34,3 +34,3 @@ int read_config_line(FILE *f, char *line, const char **value, int bufsize)
}
- if (!isname && isblank(c))
+ if (!isname && isspace(c))
continue;
@@ -66,3 +66,3 @@ int cgit_read_config(const char *filename, configfn fn)
- while(len = read_config_line(f, line, &value, sizeof(line)))
+ while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
(*fn)(line, value);
diff --git a/git.h b/git.h
index 443f216..dfa3542 100644
--- a/git.h
+++ b/git.h
@@ -35,2 +35,22 @@
+/* On most systems <limits.h> would have given us this, but
+ * not on some systems (e.g. GNU/Hurd).
+ */
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
+#ifdef __GNUC__
+#define NORETURN __attribute__((__noreturn__))
+#else
+#define NORETURN
+#ifndef __attribute__
+#define __attribute__(x)
+#endif
+#endif
+
+
+extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
+
+
static inline char* xstrdup(const char *str)
@@ -110,2 +130,3 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len)
+extern int sha1_object_info(const unsigned char *, char *, unsigned long *);
@@ -113,2 +134,5 @@ extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned lon
+extern int get_sha1(const char *str, unsigned char *sha1);
+extern int get_sha1_hex(const char *hex, unsigned char *sha1);
+extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
@@ -185,2 +209,17 @@ struct commit {
+struct commit *lookup_commit(const unsigned char *sha1);
+struct commit *lookup_commit_reference(const unsigned char *sha1);
+struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
+ int quiet);
+
+int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
+int parse_commit(struct commit *item);
+
+struct commit_list * commit_list_insert(struct commit *item, struct commit_list **list_p);
+struct commit_list * insert_by_date(struct commit *item, struct commit_list **list);
+
+void free_commit_list(struct commit_list *list);
+
+void sort_by_date(struct commit_list **list);
+
/* Commit formats */
@@ -199,9 +238,5 @@ enum cmit_fmt {
+extern unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject, int relative_date);
-struct commit *lookup_commit(const unsigned char *sha1);
-struct commit *lookup_commit_reference(const unsigned char *sha1);
-struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
- int quiet);
-
typedef void (*topo_sort_set_fn_t)(struct commit*, void *data);
@@ -308,2 +343,12 @@ enum color_diff {
+/*
+ * from git:refs.g
+ */
+
+typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
+extern int head_ref(each_ref_fn, void *);
+extern int for_each_ref(each_ref_fn, void *);
+extern int for_each_tag_ref(each_ref_fn, void *);
+extern int for_each_branch_ref(each_ref_fn, void *);
+extern int for_each_remote_ref(each_ref_fn, void *);
@@ -393,2 +438,7 @@ struct rev_info {
+extern void init_revisions(struct rev_info *revs, const char *prefix);
+extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def);
+extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename);
+
+extern void prepare_revision_walk(struct rev_info *revs);
extern struct commit *get_revision(struct rev_info *revs);
diff --git a/html.c b/html.c
index 5780dc1..bf1490f 100644
--- a/html.c
+++ b/html.c
@@ -22,3 +22,3 @@ void html(const char *txt)
{
- fputs(txt, stdout);
+ write(htmlfd, txt, strlen(txt));
}
@@ -27,2 +27,3 @@ void htmlf(const char *format, ...)
{
+ static char buf[65536];
va_list args;
@@ -30,4 +31,5 @@ void htmlf(const char *format, ...)
va_start(args, format);
- vprintf(format, args);
+ vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
+ html(buf);
}