about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLars Hjemli2007-02-08 13:53:13 +0100
committerLars Hjemli2007-02-08 13:58:58 +0100
commitab2ab95f09994560f62fd631f07d3b6e3577aa6e (patch)
tree846763c1bcb78bd27dc37c99e5f6d703ca5ab179
parentcgit v0.2 (diff)
downloadcgit-ab2ab95f09994560f62fd631f07d3b6e3577aa6e.tar.gz
cgit-ab2ab95f09994560f62fd631f07d3b6e3577aa6e.zip
Add support for snapshots
Make a link from the commit viewer to a snapshot of the corresponding tree.

Currently only zip-format is supported.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-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
@@ -6,7 +6,8 @@ gitsrc = ../git
6CACHE_ROOT = /var/cache/cgit 6CACHE_ROOT = /var/cache/cgit
7EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto 7EXTLIBS = $(gitsrc)/libgit.a $(gitsrc)/xdiff/lib.a -lz -lcrypto
8OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ 8OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \
9 ui-summary.o ui-log.o ui-view.c ui-tree.c ui-commit.c ui-diff.o 9 ui-summary.o ui-log.o ui-view.c ui-tree.c ui-commit.c ui-diff.o \
10 ui-snapshot.o
10 11
11CFLAGS += -Wall 12CFLAGS += -Wall
12 13
diff --git a/cgit.c b/cgit.c index 0fa203c..2795ecc 100644 --- a/cgit.c +++ b/cgit.c
@@ -78,6 +78,13 @@ static void cgit_print_repo_page(struct cacheitem *item)
78 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 78 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
79 show_search = 0; 79 show_search = 0;
80 setenv("GIT_DIR", cgit_repo->path, 1); 80 setenv("GIT_DIR", cgit_repo->path, 1);
81
82 if (cgit_query_page && !strcmp(cgit_query_page, "snapshot")) {
83 cgit_print_snapshot(item, cgit_query_sha1, "zip",
84 cgit_repo->url, cgit_query_name);
85 return;
86 }
87
81 if (cgit_query_page && !strcmp(cgit_query_page, "log")) 88 if (cgit_query_page && !strcmp(cgit_query_page, "log"))
82 show_search = 1; 89 show_search = 1;
83 cgit_print_docstart(title, item); 90 cgit_print_docstart(title, item);
@@ -85,7 +92,8 @@ static void cgit_print_repo_page(struct cacheitem *item)
85 if (!cgit_query_page) { 92 if (!cgit_query_page) {
86 cgit_print_summary(); 93 cgit_print_summary();
87 } else if (!strcmp(cgit_query_page, "log")) { 94 } else if (!strcmp(cgit_query_page, "log")) {
88 cgit_print_log(cgit_query_head, cgit_query_ofs, 100, cgit_query_search); 95 cgit_print_log(cgit_query_head, cgit_query_ofs, 100,
96 cgit_query_search);
89 } else if (!strcmp(cgit_query_page, "tree")) { 97 } else if (!strcmp(cgit_query_page, "tree")) {
90 cgit_print_tree(cgit_query_sha1, cgit_query_path); 98 cgit_print_tree(cgit_query_sha1, cgit_query_path);
91 } else if (!strcmp(cgit_query_page, "commit")) { 99 } else if (!strcmp(cgit_query_page, "commit")) {
@@ -94,21 +102,39 @@ static void cgit_print_repo_page(struct cacheitem *item)
94 cgit_print_view(cgit_query_sha1); 102 cgit_print_view(cgit_query_sha1);
95 } else if (!strcmp(cgit_query_page, "diff")) { 103 } else if (!strcmp(cgit_query_page, "diff")) {
96 cgit_print_diff(cgit_query_sha1, cgit_query_sha2); 104 cgit_print_diff(cgit_query_sha1, cgit_query_sha2);
105 } else {
106 cgit_print_error("Invalid request");
97 } 107 }
98 cgit_print_docend(); 108 cgit_print_docend();
99} 109}
100 110
101static void cgit_fill_cache(struct cacheitem *item) 111static void cgit_fill_cache(struct cacheitem *item, int use_cache)
102{ 112{
103 static char buf[PATH_MAX]; 113 static char buf[PATH_MAX];
114 int stdout2;
104 115
105 getcwd(buf, sizeof(buf)); 116 getcwd(buf, sizeof(buf));
106 htmlfd = item->fd;
107 item->st.st_mtime = time(NULL); 117 item->st.st_mtime = time(NULL);
118
119 if (use_cache) {
120 stdout2 = chk_positive(dup(STDOUT_FILENO),
121 "Preserving STDOUT");
122 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
123 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
124 }
125
108 if (cgit_query_repo) 126 if (cgit_query_repo)
109 cgit_print_repo_page(item); 127 cgit_print_repo_page(item);
110 else 128 else
111 cgit_print_repolist(item); 129 cgit_print_repolist(item);
130
131 if (use_cache) {
132 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
133 chk_positive(dup2(stdout2, STDOUT_FILENO),
134 "Restoring original STDOUT");
135 chk_zero(close(stdout2), "Closing temporary STDOUT");
136 }
137
112 chdir(buf); 138 chdir(buf);
113} 139}
114 140
@@ -127,14 +153,14 @@ static void cgit_check_cache(struct cacheitem *item)
127 goto top; 153 goto top;
128 } 154 }
129 if (!cache_exist(item)) { 155 if (!cache_exist(item)) {
130 cgit_fill_cache(item); 156 cgit_fill_cache(item, 1);
131 cache_unlock(item); 157 cache_unlock(item);
132 } else { 158 } else {
133 cache_cancel_lock(item); 159 cache_cancel_lock(item);
134 } 160 }
135 } else if (cache_expired(item) && cache_lock(item)) { 161 } else if (cache_expired(item) && cache_lock(item)) {
136 if (cache_expired(item)) { 162 if (cache_expired(item)) {
137 cgit_fill_cache(item); 163 cgit_fill_cache(item, 1);
138 cache_unlock(item); 164 cache_unlock(item);
139 } else { 165 } else {
140 cache_cancel_lock(item); 166 cache_cancel_lock(item);
@@ -209,8 +235,7 @@ int main(int argc, const char **argv)
209 if (!cgit_prepare_cache(&item)) 235 if (!cgit_prepare_cache(&item))
210 return 0; 236 return 0;
211 if (cgit_nocache) { 237 if (cgit_nocache) {
212 item.fd = STDOUT_FILENO; 238 cgit_fill_cache(&item, 0);
213 cgit_fill_cache(&item);
214 } else { 239 } else {
215 cgit_check_cache(&item); 240 cgit_check_cache(&item);
216 cgit_print_cache(&item); 241 cgit_print_cache(&item);
diff --git a/cgit.h b/cgit.h index 2a3cd5a..03c2fdb 100644 --- a/cgit.h +++ b/cgit.h
@@ -85,6 +85,7 @@ extern char *cgit_query_head;
85extern char *cgit_query_sha1; 85extern char *cgit_query_sha1;
86extern char *cgit_query_sha2; 86extern char *cgit_query_sha2;
87extern char *cgit_query_path; 87extern char *cgit_query_path;
88extern char *cgit_query_name;
88extern int cgit_query_ofs; 89extern int cgit_query_ofs;
89 90
90extern int htmlfd; 91extern int htmlfd;
@@ -93,6 +94,9 @@ extern void cgit_global_config_cb(const char *name, const char *value);
93extern void cgit_repo_config_cb(const char *name, const char *value); 94extern void cgit_repo_config_cb(const char *name, const char *value);
94extern void cgit_querystring_cb(const char *name, const char *value); 95extern void cgit_querystring_cb(const char *name, const char *value);
95 96
97extern int chk_zero(int result, char *msg);
98extern int chk_positive(int result, char *msg);
99
96extern int hextoint(char c); 100extern int hextoint(char c);
97 101
98extern void *cgit_free_commitinfo(struct commitinfo *info); 102extern void *cgit_free_commitinfo(struct commitinfo *info);
@@ -130,6 +134,9 @@ extern void cgit_print_date(unsigned long secs);
130extern void cgit_print_docstart(char *title, struct cacheitem *item); 134extern void cgit_print_docstart(char *title, struct cacheitem *item);
131extern void cgit_print_docend(); 135extern void cgit_print_docend();
132extern void cgit_print_pageheader(char *title, int show_search); 136extern void cgit_print_pageheader(char *title, int show_search);
137extern void cgit_print_snapshot_start(const char *mimetype,
138 const char *filename,
139 struct cacheitem *item);
133 140
134extern void cgit_print_repolist(struct cacheitem *item); 141extern void cgit_print_repolist(struct cacheitem *item);
135extern void cgit_print_summary(); 142extern void cgit_print_summary();
@@ -138,5 +145,8 @@ extern void cgit_print_view(const char *hex);
138extern void cgit_print_tree(const char *hex, char *path); 145extern void cgit_print_tree(const char *hex, char *path);
139extern void cgit_print_commit(const char *hex); 146extern void cgit_print_commit(const char *hex);
140extern void cgit_print_diff(const char *old_hex, const char *new_hex); 147extern void cgit_print_diff(const char *old_hex, const char *new_hex);
148extern void cgit_print_snapshot(struct cacheitem *item, const char *hex,
149 const char *format, const char *prefix,
150 const char *filename);
141 151
142#endif /* CGIT_H */ 152#endif /* CGIT_H */
diff --git a/git.h b/git.h index eca48d5..a1d1c4b 100644 --- a/git.h +++ b/git.h
@@ -669,4 +669,31 @@ int log_tree_commit(struct rev_info *, struct commit *);
669 669
670 670
671 671
672/* from git:archive.h */
673
674struct archiver_args {
675 const char *base;
676 struct tree *tree;
677 const unsigned char *commit_sha1;
678 time_t time;
679 const char **pathspec;
680 unsigned int verbose : 1;
681 void *extra;
682};
683
684typedef int (*write_archive_fn_t)(struct archiver_args *);
685
686typedef void *(*parse_extra_args_fn_t)(int argc, const char **argv);
687
688struct archiver {
689 const char *name;
690 struct archiver_args args;
691 write_archive_fn_t write_archive;
692 parse_extra_args_fn_t parse_extra;
693};
694
695extern int write_tar_archive(struct archiver_args *);
696extern int write_zip_archive(struct archiver_args *);
697extern void *parse_extra_zip_args(int argc, const char **argv);
698
672#endif /* GIT_H */ 699#endif /* GIT_H */
diff --git a/shared.c b/shared.c index 6fd70a8..5757d0c 100644 --- a/shared.c +++ b/shared.c
@@ -44,10 +44,25 @@ char *cgit_query_search = NULL;
44char *cgit_query_sha1 = NULL; 44char *cgit_query_sha1 = NULL;
45char *cgit_query_sha2 = NULL; 45char *cgit_query_sha2 = NULL;
46char *cgit_query_path = NULL; 46char *cgit_query_path = NULL;
47char *cgit_query_name = NULL;
47int cgit_query_ofs = 0; 48int cgit_query_ofs = 0;
48 49
49int htmlfd = 0; 50int htmlfd = 0;
50 51
52int chk_zero(int result, char *msg)
53{
54 if (result != 0)
55 die("%s: %s", msg, strerror(errno));
56 return result;
57}
58
59int chk_positive(int result, char *msg)
60{
61 if (result <= 0)
62 die("%s: %s", msg, strerror(errno));
63 return result;
64}
65
51struct repoinfo *add_repo(const char *url) 66struct repoinfo *add_repo(const char *url)
52{ 67{
53 struct repoinfo *ret; 68 struct repoinfo *ret;
@@ -140,6 +155,8 @@ void cgit_querystring_cb(const char *name, const char *value)
140 cgit_query_ofs = atoi(value); 155 cgit_query_ofs = atoi(value);
141 } else if (!strcmp(name, "path")) { 156 } else if (!strcmp(name, "path")) {
142 cgit_query_path = xstrdup(value); 157 cgit_query_path = xstrdup(value);
158 } else if (!strcmp(name, "name")) {
159 cgit_query_name = xstrdup(value);
143 } 160 }
144} 161}
145 162
diff --git a/ui-commit.c b/ui-commit.c index 73fa104..de3f2cf 100644 --- a/ui-commit.c +++ b/ui-commit.c
@@ -128,6 +128,7 @@ void cgit_print_commit(const char *hex)
128 struct commit_list *p; 128 struct commit_list *p;
129 unsigned char sha1[20]; 129 unsigned char sha1[20];
130 char *query; 130 char *query;
131 char *filename;
131 132
132 if (get_sha1(hex, sha1)) { 133 if (get_sha1(hex, sha1)) {
133 cgit_print_error(fmt("Bad object id: %s", hex)); 134 cgit_print_error(fmt("Bad object id: %s", hex));
@@ -168,6 +169,12 @@ void cgit_print_commit(const char *hex)
168 htmlf("'>%s</a></td></tr>\n", 169 htmlf("'>%s</a></td></tr>\n",
169 sha1_to_hex(p->item->object.sha1)); 170 sha1_to_hex(p->item->object.sha1));
170 } 171 }
172 htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='");
173 filename = fmt("%s-%s.zip", cgit_query_repo, hex);
174 html_attr(cgit_pageurl(cgit_query_repo, "snapshot",
175 fmt("id=%s&name=%s", hex, filename)));
176 htmlf("'>%s</a></td></tr>", filename);
177
171 html("</table>\n"); 178 html("</table>\n");
172 html("<div class='commit-subject'>"); 179 html("<div class='commit-subject'>");
173 html_txt(info->subject); 180 html_txt(info->subject);
diff --git a/ui-shared.c b/ui-shared.c index 3322561..172499c 100644 --- a/ui-shared.c +++ b/ui-shared.c
@@ -144,3 +144,14 @@ void cgit_print_pageheader(char *title, int show_search)
144 html("</a>"); 144 html("</a>");
145 html("</td></tr><tr><td id='content'>"); 145 html("</td></tr><tr><td id='content'>");
146} 146}
147
148void cgit_print_snapshot_start(const char *mimetype, const char *filename,
149 struct cacheitem *item)
150{
151 htmlf("Content-Type: %s\n", mimetype);
152 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
153 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
154 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
155 ttl_seconds(item->ttl)));
156 html("\n");
157}
diff --git a/ui-snapshot.c b/ui-snapshot.c new file mode 100644 index 0000000..2257d6b --- /dev/null +++ b/ui-snapshot.c
@@ -0,0 +1,47 @@
1/* ui-snapshot.c: generate snapshot of a commit
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
11static void cgit_print_zip(struct cacheitem *item, const char *hex,
12 const char *prefix, const char *filename)
13{
14 struct archiver_args args;
15 struct commit *commit;
16 unsigned char sha1[20];
17
18 if (get_sha1(hex, sha1)) {
19 cgit_print_error(fmt("Bad object id: %s", hex));
20 return;
21 }
22 commit = lookup_commit_reference(sha1);
23
24 if (!commit) {
25 cgit_print_error(fmt("Not a commit reference: %s", hex));
26 return;
27 }
28
29 memset(&args, 0, sizeof(args));
30 args.base = fmt("%s/", prefix);
31 args.tree = commit->tree;
32
33 cgit_print_snapshot_start("application/x-zip", filename, item);
34 write_zip_archive(&args);
35}
36
37
38void cgit_print_snapshot(struct cacheitem *item, const char *hex,
39 const char *format, const char *prefix,
40 const char *filename)
41{
42 if (!strcmp(format, "zip"))
43 cgit_print_zip(item, hex, prefix, filename);
44 else
45 cgit_print_error(fmt("Unsupported snapshot format: %s",
46 format));
47}