summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile3
-rw-r--r--cgit.c39
-rw-r--r--cgit.h10
-rw-r--r--git.h27
-rw-r--r--shared.c17
-rw-r--r--ui-commit.c7
-rw-r--r--ui-shared.c11
-rw-r--r--ui-snapshot.c47
8 files changed, 153 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index d826f97..b1c3a4e 100644
--- a/Makefile
+++ b/Makefile
@@ -1,21 +1,22 @@
CGIT_VERSION = 0.2
prefix = /var/www/htdocs/cgit
gitsrc = ../git
CACHE_ROOT = /var/cache/cgit
EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto
OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \
- ui-summary.o ui-log.o ui-view.c ui-tree.c ui-commit.c ui-diff.o
+ ui-summary.o ui-log.o ui-view.c ui-tree.c ui-commit.c ui-diff.o \
+ ui-snapshot.o
CFLAGS += -Wall
ifdef DEBUG
CFLAGS += -g
endif
all: cgit
install: all clean-cache
mkdir -p $(prefix)
install cgit $(prefix)/cgit.cgi
diff --git a/cgit.c b/cgit.c
index 0fa203c..2795ecc 100644
--- a/cgit.c
+++ b/cgit.c
@@ -69,81 +69,107 @@ static void cgit_print_repo_page(struct cacheitem *item)
title = fmt("%s - %s", cgit_root_title, "Bad request");
cgit_print_docstart(title, item);
cgit_print_pageheader(title, 0);
cgit_print_error(fmt("Unable to scan repository: %s",
strerror(errno)));
cgit_print_docend();
return;
}
title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
show_search = 0;
setenv("GIT_DIR", cgit_repo->path, 1);
+
+ if (cgit_query_page && !strcmp(cgit_query_page, "snapshot")) {
+ cgit_print_snapshot(item, cgit_query_sha1, "zip",
+ cgit_repo->url, cgit_query_name);
+ return;
+ }
+
if (cgit_query_page && !strcmp(cgit_query_page, "log"))
show_search = 1;
cgit_print_docstart(title, item);
cgit_print_pageheader(title, show_search);
if (!cgit_query_page) {
cgit_print_summary();
} else if (!strcmp(cgit_query_page, "log")) {
- cgit_print_log(cgit_query_head, cgit_query_ofs, 100, cgit_query_search);
+ cgit_print_log(cgit_query_head, cgit_query_ofs, 100,
+ cgit_query_search);
} else if (!strcmp(cgit_query_page, "tree")) {
cgit_print_tree(cgit_query_sha1, cgit_query_path);
} else if (!strcmp(cgit_query_page, "commit")) {
cgit_print_commit(cgit_query_sha1);
} else if (!strcmp(cgit_query_page, "view")) {
cgit_print_view(cgit_query_sha1);
} else if (!strcmp(cgit_query_page, "diff")) {
cgit_print_diff(cgit_query_sha1, cgit_query_sha2);
+ } else {
+ cgit_print_error("Invalid request");
}
cgit_print_docend();
}
-static void cgit_fill_cache(struct cacheitem *item)
+static void cgit_fill_cache(struct cacheitem *item, int use_cache)
{
static char buf[PATH_MAX];
+ int stdout2;
getcwd(buf, sizeof(buf));
- htmlfd = item->fd;
item->st.st_mtime = time(NULL);
+
+ if (use_cache) {
+ stdout2 = chk_positive(dup(STDOUT_FILENO),
+ "Preserving STDOUT");
+ chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
+ chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
+ }
+
if (cgit_query_repo)
cgit_print_repo_page(item);
else
cgit_print_repolist(item);
+
+ if (use_cache) {
+ chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
+ chk_positive(dup2(stdout2, STDOUT_FILENO),
+ "Restoring original STDOUT");
+ chk_zero(close(stdout2), "Closing temporary STDOUT");
+ }
+
chdir(buf);
}
static void cgit_check_cache(struct cacheitem *item)
{
int i = 0;
top:
if (++i > cgit_max_lock_attempts) {
die("cgit_refresh_cache: unable to lock %s: %s",
item->name, strerror(errno));
}
if (!cache_exist(item)) {
if (!cache_lock(item)) {
sleep(1);
goto top;
}
if (!cache_exist(item)) {
- cgit_fill_cache(item);
+ cgit_fill_cache(item, 1);
cache_unlock(item);
} else {
cache_cancel_lock(item);
}
} else if (cache_expired(item) && cache_lock(item)) {
if (cache_expired(item)) {
- cgit_fill_cache(item);
+ cgit_fill_cache(item, 1);
cache_unlock(item);
} else {
cache_cancel_lock(item);
}
}
}
static void cgit_print_cache(struct cacheitem *item)
{
static char buf[4096];
ssize_t i;
@@ -200,20 +226,19 @@ int main(int argc, const char **argv)
cgit_repolist.length = 0;
cgit_repolist.count = 0;
cgit_repolist.repos = NULL;
cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
if (getenv("QUERY_STRING"))
cgit_querystring = xstrdup(getenv("QUERY_STRING"));
cgit_parse_args(argc, argv);
cgit_parse_query(cgit_querystring, cgit_querystring_cb);
if (!cgit_prepare_cache(&item))
return 0;
if (cgit_nocache) {
- item.fd = STDOUT_FILENO;
- cgit_fill_cache(&item);
+ cgit_fill_cache(&item, 0);
} else {
cgit_check_cache(&item);
cgit_print_cache(&item);
}
return 0;
}
diff --git a/cgit.h b/cgit.h
index 2a3cd5a..03c2fdb 100644
--- a/cgit.h
+++ b/cgit.h
@@ -76,32 +76,36 @@ 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_search;
extern char *cgit_query_head;
extern char *cgit_query_sha1;
extern char *cgit_query_sha2;
extern char *cgit_query_path;
+extern char *cgit_query_name;
extern int cgit_query_ofs;
extern int htmlfd;
extern void cgit_global_config_cb(const char *name, const char *value);
extern void cgit_repo_config_cb(const char *name, const char *value);
extern void cgit_querystring_cb(const char *name, const char *value);
+extern int chk_zero(int result, char *msg);
+extern int chk_positive(int result, char *msg);
+
extern int hextoint(char c);
extern void *cgit_free_commitinfo(struct commitinfo *info);
extern char *fmt(const char *format,...);
extern void html(const char *txt);
extern void htmlf(const char *format,...);
extern void html_txt(char *txt);
extern void html_ntxt(int len, char *txt);
extern void html_attr(char *txt);
extern void html_hidden(char *name, char *value);
@@ -121,22 +125,28 @@ extern int cache_cancel_lock(struct cacheitem *item);
extern int cache_exist(struct cacheitem *item);
extern int cache_expired(struct cacheitem *item);
extern char *cgit_repourl(const char *reponame);
extern char *cgit_pageurl(const char *reponame, const char *pagename,
const char *query);
extern void cgit_print_error(char *msg);
extern void cgit_print_date(unsigned long secs);
extern void cgit_print_docstart(char *title, struct cacheitem *item);
extern void cgit_print_docend();
extern void cgit_print_pageheader(char *title, int show_search);
+extern void cgit_print_snapshot_start(const char *mimetype,
+ const char *filename,
+ struct cacheitem *item);
extern void cgit_print_repolist(struct cacheitem *item);
extern void cgit_print_summary();
extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep);
extern void cgit_print_view(const char *hex);
extern void cgit_print_tree(const char *hex, char *path);
extern void cgit_print_commit(const char *hex);
extern void cgit_print_diff(const char *old_hex, const char *new_hex);
+extern void cgit_print_snapshot(struct cacheitem *item, const char *hex,
+ const char *format, const char *prefix,
+ const char *filename);
#endif /* CGIT_H */
diff --git a/git.h b/git.h
index eca48d5..a1d1c4b 100644
--- a/git.h
+++ b/git.h
@@ -660,13 +660,40 @@ extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,
extern void prepare_revision_walk(struct rev_info *revs);
extern struct commit *get_revision(struct rev_info *revs);
/* from git:log-tree.h */
int log_tree_commit(struct rev_info *, struct commit *);
+/* from git:archive.h */
+
+struct archiver_args {
+ const char *base;
+ struct tree *tree;
+ const unsigned char *commit_sha1;
+ time_t time;
+ const char **pathspec;
+ unsigned int verbose : 1;
+ void *extra;
+};
+
+typedef int (*write_archive_fn_t)(struct archiver_args *);
+
+typedef void *(*parse_extra_args_fn_t)(int argc, const char **argv);
+
+struct archiver {
+ const char *name;
+ struct archiver_args args;
+ write_archive_fn_t write_archive;
+ parse_extra_args_fn_t parse_extra;
+};
+
+extern int write_tar_archive(struct archiver_args *);
+extern int write_zip_archive(struct archiver_args *);
+extern void *parse_extra_zip_args(int argc, const char **argv);
+
#endif /* GIT_H */
diff --git a/shared.c b/shared.c
index 6fd70a8..5757d0c 100644
--- a/shared.c
+++ b/shared.c
@@ -35,28 +35,43 @@ 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;
char *cgit_query_page = NULL;
char *cgit_query_head = NULL;
char *cgit_query_search = NULL;
char *cgit_query_sha1 = NULL;
char *cgit_query_sha2 = NULL;
char *cgit_query_path = NULL;
+char *cgit_query_name = NULL;
int cgit_query_ofs = 0;
int htmlfd = 0;
+int chk_zero(int result, char *msg)
+{
+ if (result != 0)
+ die("%s: %s", msg, strerror(errno));
+ return result;
+}
+
+int chk_positive(int result, char *msg)
+{
+ if (result <= 0)
+ die("%s: %s", msg, strerror(errno));
+ return result;
+}
+
struct repoinfo *add_repo(const char *url)
{
struct repoinfo *ret;
if (++cgit_repolist.count > cgit_repolist.length) {
if (cgit_repolist.length == 0)
cgit_repolist.length = 8;
else
cgit_repolist.length *= 2;
cgit_repolist.repos = xrealloc(cgit_repolist.repos,
cgit_repolist.length *
sizeof(struct repoinfo));
@@ -131,24 +146,26 @@ void cgit_querystring_cb(const char *name, const char *value)
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;
} else if (!strcmp(name, "id2")) {
cgit_query_sha2 = xstrdup(value);
cgit_query_has_sha1 = 1;
} else if (!strcmp(name, "ofs")) {
cgit_query_ofs = atoi(value);
} else if (!strcmp(name, "path")) {
cgit_query_path = xstrdup(value);
+ } else if (!strcmp(name, "name")) {
+ cgit_query_name = xstrdup(value);
}
}
void *cgit_free_commitinfo(struct commitinfo *info)
{
free(info->author);
free(info->author_email);
free(info->committer);
free(info->committer_email);
free(info->subject);
free(info);
return NULL;
diff --git a/ui-commit.c b/ui-commit.c
index 73fa104..de3f2cf 100644
--- a/ui-commit.c
+++ b/ui-commit.c
@@ -119,24 +119,25 @@ void cgit_diffstat(struct commit *commit)
diffcore_std(&opt);
diff_flush(&opt);
}
void cgit_print_commit(const char *hex)
{
struct commit *commit;
struct commitinfo *info;
struct commit_list *p;
unsigned char sha1[20];
char *query;
+ char *filename;
if (get_sha1(hex, sha1)) {
cgit_print_error(fmt("Bad object id: %s", hex));
return;
}
commit = lookup_commit_reference(sha1);
if (!commit) {
cgit_print_error(fmt("Bad commit reference: %s", hex));
return;
}
info = cgit_parse_commit(commit);
@@ -159,24 +160,30 @@ void cgit_print_commit(const char *hex)
query = fmt("id=%s", sha1_to_hex(commit->tree->object.sha1));
html_attr(cgit_pageurl(cgit_query_repo, "tree", query));
htmlf("'>%s</a></td></tr>\n", sha1_to_hex(commit->tree->object.sha1));
for (p = commit->parents; p ; p = p->next) {
html("<tr><th>parent</th>"
"<td colspan='2' class='sha1'>"
"<a href='");
query = fmt("id=%s", sha1_to_hex(p->item->object.sha1));
html_attr(cgit_pageurl(cgit_query_repo, "commit", query));
htmlf("'>%s</a></td></tr>\n",
sha1_to_hex(p->item->object.sha1));
}
+ htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='");
+ filename = fmt("%s-%s.zip", cgit_query_repo, hex);
+ html_attr(cgit_pageurl(cgit_query_repo, "snapshot",
+ fmt("id=%s&name=%s", hex, filename)));
+ htmlf("'>%s</a></td></tr>", filename);
+
html("</table>\n");
html("<div class='commit-subject'>");
html_txt(info->subject);
html("</div>");
html("<div class='commit-msg'>");
html_txt(info->msg);
html("</div>");
html("<table class='diffstat'>");
html("<tr><th colspan='3'>Affected files</tr>\n");
cgit_diffstat(commit);
htmlf("<tr><td colspan='3' class='summary'>"
"%d file%s changed</td></tr>\n", files, files > 1 ? "s" : "");
diff --git a/ui-shared.c b/ui-shared.c
index 3322561..172499c 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -135,12 +135,23 @@ void cgit_print_pageheader(char *title, int show_search)
html_hidden("id2", cgit_query_sha2);
html("<input type='text' name='q' value='");
html_attr(cgit_query_search);
html("'/></form>");
}
if (cgit_query_repo)
htmlf("<a href='%s'>", cgit_repourl(cgit_query_repo));
html_txt(title);
if (cgit_query_repo)
html("</a>");
html("</td></tr><tr><td id='content'>");
}
+
+void cgit_print_snapshot_start(const char *mimetype, const char *filename,
+ struct cacheitem *item)
+{
+ htmlf("Content-Type: %s\n", mimetype);
+ htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
+ htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
+ htmlf("Expires: %s\n", http_date(item->st.st_mtime +
+ ttl_seconds(item->ttl)));
+ html("\n");
+}
diff --git a/ui-snapshot.c b/ui-snapshot.c
new file mode 100644
index 0000000..2257d6b
--- a/dev/null
+++ b/ui-snapshot.c
@@ -0,0 +1,47 @@
+/* ui-snapshot.c: generate snapshot of a commit
+ *
+ * Copyright (C) 2006 Lars Hjemli
+ *
+ * Licensed under GNU General Public License v2
+ * (see COPYING for full license text)
+ */
+
+#include "cgit.h"
+
+static void cgit_print_zip(struct cacheitem *item, const char *hex,
+ const char *prefix, const char *filename)
+{
+ struct archiver_args args;
+ struct commit *commit;
+ unsigned char sha1[20];
+
+ if (get_sha1(hex, sha1)) {
+ cgit_print_error(fmt("Bad object id: %s", hex));
+ return;
+ }
+ commit = lookup_commit_reference(sha1);
+
+ if (!commit) {
+ cgit_print_error(fmt("Not a commit reference: %s", hex));
+ return;
+ }
+
+ memset(&args, 0, sizeof(args));
+ args.base = fmt("%s/", prefix);
+ args.tree = commit->tree;
+
+ cgit_print_snapshot_start("application/x-zip", filename, item);
+ write_zip_archive(&args);
+}
+
+
+void cgit_print_snapshot(struct cacheitem *item, const char *hex,
+ const char *format, const char *prefix,
+ const char *filename)
+{
+ if (!strcmp(format, "zip"))
+ cgit_print_zip(item, hex, prefix, filename);
+ else
+ cgit_print_error(fmt("Unsupported snapshot format: %s",
+ format));
+}