summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.h8
-rw-r--r--shared.c27
-rw-r--r--ui-shared.c17
-rw-r--r--ui-snapshot.c124
4 files changed, 90 insertions, 86 deletions
diff --git a/cgit.h b/cgit.h
index 295441b..e2d5126 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,285 +1,293 @@
1#ifndef CGIT_H 1#ifndef CGIT_H
2#define CGIT_H 2#define CGIT_H
3 3
4 4
5#include <git-compat-util.h> 5#include <git-compat-util.h>
6#include <cache.h> 6#include <cache.h>
7#include <grep.h> 7#include <grep.h>
8#include <object.h> 8#include <object.h>
9#include <tree.h> 9#include <tree.h>
10#include <commit.h> 10#include <commit.h>
11#include <tag.h> 11#include <tag.h>
12#include <diff.h> 12#include <diff.h>
13#include <diffcore.h> 13#include <diffcore.h>
14#include <refs.h> 14#include <refs.h>
15#include <revision.h> 15#include <revision.h>
16#include <log-tree.h> 16#include <log-tree.h>
17#include <archive.h> 17#include <archive.h>
18#include <xdiff/xdiff.h> 18#include <xdiff/xdiff.h>
19#include <utf8.h> 19#include <utf8.h>
20 20
21 21
22/* 22/*
23 * Dateformats used on misc. pages 23 * Dateformats used on misc. pages
24 */ 24 */
25#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" 25#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S"
26#define FMT_SHORTDATE "%Y-%m-%d" 26#define FMT_SHORTDATE "%Y-%m-%d"
27 27
28 28
29/* 29/*
30 * Limits used for relative dates 30 * Limits used for relative dates
31 */ 31 */
32#define TM_MIN 60 32#define TM_MIN 60
33#define TM_HOUR (TM_MIN * 60) 33#define TM_HOUR (TM_MIN * 60)
34#define TM_DAY (TM_HOUR * 24) 34#define TM_DAY (TM_HOUR * 24)
35#define TM_WEEK (TM_DAY * 7) 35#define TM_WEEK (TM_DAY * 7)
36#define TM_YEAR (TM_DAY * 365) 36#define TM_YEAR (TM_DAY * 365)
37#define TM_MONTH (TM_YEAR / 12.0) 37#define TM_MONTH (TM_YEAR / 12.0)
38 38
39 39
40/* 40/*
41 * Default encoding 41 * Default encoding
42 */ 42 */
43#define PAGE_ENCODING "UTF-8" 43#define PAGE_ENCODING "UTF-8"
44 44
45typedef void (*configfn)(const char *name, const char *value); 45typedef void (*configfn)(const char *name, const char *value);
46typedef void (*filepair_fn)(struct diff_filepair *pair); 46typedef void (*filepair_fn)(struct diff_filepair *pair);
47typedef void (*linediff_fn)(char *line, int len); 47typedef void (*linediff_fn)(char *line, int len);
48 48
49struct cacheitem { 49struct cacheitem {
50 char *name; 50 char *name;
51 struct stat st; 51 struct stat st;
52 int ttl; 52 int ttl;
53 int fd; 53 int fd;
54}; 54};
55 55
56struct cgit_repo { 56struct cgit_repo {
57 char *url; 57 char *url;
58 char *name; 58 char *name;
59 char *path; 59 char *path;
60 char *desc; 60 char *desc;
61 char *owner; 61 char *owner;
62 char *defbranch; 62 char *defbranch;
63 char *group; 63 char *group;
64 char *module_link; 64 char *module_link;
65 char *readme; 65 char *readme;
66 char *clone_url; 66 char *clone_url;
67 int snapshots; 67 int snapshots;
68 int enable_log_filecount; 68 int enable_log_filecount;
69 int enable_log_linecount; 69 int enable_log_linecount;
70}; 70};
71 71
72struct cgit_repolist { 72struct cgit_repolist {
73 int length; 73 int length;
74 int count; 74 int count;
75 struct cgit_repo *repos; 75 struct cgit_repo *repos;
76}; 76};
77 77
78struct commitinfo { 78struct commitinfo {
79 struct commit *commit; 79 struct commit *commit;
80 char *author; 80 char *author;
81 char *author_email; 81 char *author_email;
82 unsigned long author_date; 82 unsigned long author_date;
83 char *committer; 83 char *committer;
84 char *committer_email; 84 char *committer_email;
85 unsigned long committer_date; 85 unsigned long committer_date;
86 char *subject; 86 char *subject;
87 char *msg; 87 char *msg;
88 char *msg_encoding; 88 char *msg_encoding;
89}; 89};
90 90
91struct taginfo { 91struct taginfo {
92 char *tagger; 92 char *tagger;
93 char *tagger_email; 93 char *tagger_email;
94 int tagger_date; 94 int tagger_date;
95 char *msg; 95 char *msg;
96}; 96};
97 97
98struct refinfo { 98struct refinfo {
99 const char *refname; 99 const char *refname;
100 struct object *object; 100 struct object *object;
101 union { 101 union {
102 struct taginfo *tag; 102 struct taginfo *tag;
103 struct commitinfo *commit; 103 struct commitinfo *commit;
104 }; 104 };
105}; 105};
106 106
107struct reflist { 107struct reflist {
108 struct refinfo **refs; 108 struct refinfo **refs;
109 int alloc; 109 int alloc;
110 int count; 110 int count;
111}; 111};
112 112
113struct cgit_query { 113struct cgit_query {
114 int has_symref; 114 int has_symref;
115 int has_sha1; 115 int has_sha1;
116 char *raw; 116 char *raw;
117 char *repo; 117 char *repo;
118 char *page; 118 char *page;
119 char *search; 119 char *search;
120 char *grep; 120 char *grep;
121 char *head; 121 char *head;
122 char *sha1; 122 char *sha1;
123 char *sha2; 123 char *sha2;
124 char *path; 124 char *path;
125 char *name; 125 char *name;
126 int ofs; 126 int ofs;
127}; 127};
128 128
129struct cgit_config { 129struct cgit_config {
130 char *agefile; 130 char *agefile;
131 char *cache_root; 131 char *cache_root;
132 char *clone_prefix; 132 char *clone_prefix;
133 char *css; 133 char *css;
134 char *index_header; 134 char *index_header;
135 char *index_info; 135 char *index_info;
136 char *logo; 136 char *logo;
137 char *logo_link; 137 char *logo_link;
138 char *module_link; 138 char *module_link;
139 char *repo_group; 139 char *repo_group;
140 char *robots; 140 char *robots;
141 char *root_title; 141 char *root_title;
142 char *script_name; 142 char *script_name;
143 char *virtual_root; 143 char *virtual_root;
144 int cache_dynamic_ttl; 144 int cache_dynamic_ttl;
145 int cache_max_create_time; 145 int cache_max_create_time;
146 int cache_repo_ttl; 146 int cache_repo_ttl;
147 int cache_root_ttl; 147 int cache_root_ttl;
148 int cache_static_ttl; 148 int cache_static_ttl;
149 int enable_index_links; 149 int enable_index_links;
150 int enable_log_filecount; 150 int enable_log_filecount;
151 int enable_log_linecount; 151 int enable_log_linecount;
152 int max_commit_count; 152 int max_commit_count;
153 int max_lock_attempts; 153 int max_lock_attempts;
154 int max_msg_len; 154 int max_msg_len;
155 int max_repodesc_len; 155 int max_repodesc_len;
156 int nocache; 156 int nocache;
157 int renamelimit; 157 int renamelimit;
158 int snapshots; 158 int snapshots;
159 int summary_branches; 159 int summary_branches;
160 int summary_log; 160 int summary_log;
161 int summary_tags; 161 int summary_tags;
162}; 162};
163 163
164struct cgit_page { 164struct cgit_page {
165 time_t modified; 165 time_t modified;
166 time_t expires; 166 time_t expires;
167 char *mimetype; 167 char *mimetype;
168 char *charset; 168 char *charset;
169 char *filename; 169 char *filename;
170 char *title; 170 char *title;
171}; 171};
172 172
173struct cgit_context { 173struct cgit_context {
174 struct cgit_query qry; 174 struct cgit_query qry;
175 struct cgit_config cfg; 175 struct cgit_config cfg;
176 struct cgit_repo *repo; 176 struct cgit_repo *repo;
177 struct cgit_page page; 177 struct cgit_page page;
178}; 178};
179 179
180struct cgit_snapshot_format {
181 const char *suffix;
182 const char *mimetype;
183 write_archive_fn_t write_func;
184 int bit;
185};
186
180extern const char *cgit_version; 187extern const char *cgit_version;
181 188
182extern struct cgit_repolist cgit_repolist; 189extern struct cgit_repolist cgit_repolist;
183extern struct cgit_context ctx; 190extern struct cgit_context ctx;
191extern const struct cgit_snapshot_format cgit_snapshot_formats[];
184extern int cgit_cmd; 192extern int cgit_cmd;
185 193
186extern void cgit_prepare_context(struct cgit_context *ctx); 194extern void cgit_prepare_context(struct cgit_context *ctx);
187extern struct cgit_repo *cgit_get_repoinfo(const char *url); 195extern struct cgit_repo *cgit_get_repoinfo(const char *url);
188extern void cgit_global_config_cb(const char *name, const char *value); 196extern void cgit_global_config_cb(const char *name, const char *value);
189extern void cgit_repo_config_cb(const char *name, const char *value); 197extern void cgit_repo_config_cb(const char *name, const char *value);
190extern void cgit_querystring_cb(const char *name, const char *value); 198extern void cgit_querystring_cb(const char *name, const char *value);
191 199
192extern int chk_zero(int result, char *msg); 200extern int chk_zero(int result, char *msg);
193extern int chk_positive(int result, char *msg); 201extern int chk_positive(int result, char *msg);
194extern int chk_non_negative(int result, char *msg); 202extern int chk_non_negative(int result, char *msg);
195 203
196extern int hextoint(char c); 204extern int hextoint(char c);
197extern char *trim_end(const char *str, char c); 205extern char *trim_end(const char *str, char c);
198extern char *strlpart(char *txt, int maxlen); 206extern char *strlpart(char *txt, int maxlen);
199extern char *strrpart(char *txt, int maxlen); 207extern char *strrpart(char *txt, int maxlen);
200 208
201extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 209extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
202extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 210extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
203 int flags, void *cb_data); 211 int flags, void *cb_data);
204 212
205extern void *cgit_free_commitinfo(struct commitinfo *info); 213extern void *cgit_free_commitinfo(struct commitinfo *info);
206 214
207extern int cgit_diff_files(const unsigned char *old_sha1, 215extern int cgit_diff_files(const unsigned char *old_sha1,
208 const unsigned char *new_sha1, 216 const unsigned char *new_sha1,
209 linediff_fn fn); 217 linediff_fn fn);
210 218
211extern void cgit_diff_tree(const unsigned char *old_sha1, 219extern void cgit_diff_tree(const unsigned char *old_sha1,
212 const unsigned char *new_sha1, 220 const unsigned char *new_sha1,
213 filepair_fn fn, const char *prefix); 221 filepair_fn fn, const char *prefix);
214 222
215extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 223extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
216 224
217extern char *fmt(const char *format,...); 225extern char *fmt(const char *format,...);
218 226
219extern int cgit_read_config(const char *filename, configfn fn); 227extern int cgit_read_config(const char *filename, configfn fn);
220extern int cgit_parse_query(char *txt, configfn fn); 228extern int cgit_parse_query(char *txt, configfn fn);
221extern struct commitinfo *cgit_parse_commit(struct commit *commit); 229extern struct commitinfo *cgit_parse_commit(struct commit *commit);
222extern struct taginfo *cgit_parse_tag(struct tag *tag); 230extern struct taginfo *cgit_parse_tag(struct tag *tag);
223extern void cgit_parse_url(const char *url); 231extern void cgit_parse_url(const char *url);
224 232
225extern char *cache_safe_filename(const char *unsafe); 233extern char *cache_safe_filename(const char *unsafe);
226extern int cache_lock(struct cacheitem *item); 234extern int cache_lock(struct cacheitem *item);
227extern int cache_unlock(struct cacheitem *item); 235extern int cache_unlock(struct cacheitem *item);
228extern int cache_cancel_lock(struct cacheitem *item); 236extern int cache_cancel_lock(struct cacheitem *item);
229extern int cache_exist(struct cacheitem *item); 237extern int cache_exist(struct cacheitem *item);
230extern int cache_expired(struct cacheitem *item); 238extern int cache_expired(struct cacheitem *item);
231 239
232extern char *cgit_repourl(const char *reponame); 240extern char *cgit_repourl(const char *reponame);
233extern char *cgit_fileurl(const char *reponame, const char *pagename, 241extern char *cgit_fileurl(const char *reponame, const char *pagename,
234 const char *filename, const char *query); 242 const char *filename, const char *query);
235extern char *cgit_pageurl(const char *reponame, const char *pagename, 243extern char *cgit_pageurl(const char *reponame, const char *pagename,
236 const char *query); 244 const char *query);
237 245
238extern const char *cgit_repobasename(const char *reponame); 246extern const char *cgit_repobasename(const char *reponame);
239 247
240extern void cgit_tree_link(char *name, char *title, char *class, char *head, 248extern void cgit_tree_link(char *name, char *title, char *class, char *head,
241 char *rev, char *path); 249 char *rev, char *path);
242extern void cgit_log_link(char *name, char *title, char *class, char *head, 250extern void cgit_log_link(char *name, char *title, char *class, char *head,
243 char *rev, char *path, int ofs, char *grep, 251 char *rev, char *path, int ofs, char *grep,
244 char *pattern); 252 char *pattern);
245extern void cgit_commit_link(char *name, char *title, char *class, char *head, 253extern void cgit_commit_link(char *name, char *title, char *class, char *head,
246 char *rev); 254 char *rev);
247extern void cgit_refs_link(char *name, char *title, char *class, char *head, 255extern void cgit_refs_link(char *name, char *title, char *class, char *head,
248 char *rev, char *path); 256 char *rev, char *path);
249extern void cgit_snapshot_link(char *name, char *title, char *class, 257extern void cgit_snapshot_link(char *name, char *title, char *class,
250 char *head, char *rev, char *archivename); 258 char *head, char *rev, char *archivename);
251extern void cgit_diff_link(char *name, char *title, char *class, char *head, 259extern void cgit_diff_link(char *name, char *title, char *class, char *head,
252 char *new_rev, char *old_rev, char *path); 260 char *new_rev, char *old_rev, char *path);
253 261
254extern void cgit_object_link(struct object *obj); 262extern void cgit_object_link(struct object *obj);
255 263
256extern void cgit_print_error(char *msg); 264extern void cgit_print_error(char *msg);
257extern void cgit_print_date(time_t secs, char *format); 265extern void cgit_print_date(time_t secs, char *format);
258extern void cgit_print_age(time_t t, time_t max_relative, char *format); 266extern void cgit_print_age(time_t t, time_t max_relative, char *format);
259extern void cgit_print_http_headers(struct cgit_context *ctx); 267extern void cgit_print_http_headers(struct cgit_context *ctx);
260extern void cgit_print_docstart(struct cgit_context *ctx); 268extern void cgit_print_docstart(struct cgit_context *ctx);
261extern void cgit_print_docend(); 269extern void cgit_print_docend();
262extern void cgit_print_pageheader(struct cgit_context *ctx); 270extern void cgit_print_pageheader(struct cgit_context *ctx);
263extern void cgit_print_filemode(unsigned short mode); 271extern void cgit_print_filemode(unsigned short mode);
264extern void cgit_print_branches(int maxcount); 272extern void cgit_print_branches(int maxcount);
265extern void cgit_print_tags(int maxcount); 273extern void cgit_print_tags(int maxcount);
266 274
267extern void cgit_print_repolist(); 275extern void cgit_print_repolist();
268extern void cgit_print_summary(); 276extern void cgit_print_summary();
269extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, 277extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep,
270 char *pattern, char *path, int pager); 278 char *pattern, char *path, int pager);
271extern void cgit_print_blob(const char *hex, char *path); 279extern void cgit_print_blob(const char *hex, char *path);
272extern void cgit_print_tree(const char *rev, char *path); 280extern void cgit_print_tree(const char *rev, char *path);
273extern void cgit_print_commit(char *hex); 281extern void cgit_print_commit(char *hex);
274extern void cgit_print_refs(); 282extern void cgit_print_refs();
275extern void cgit_print_tag(char *revname); 283extern void cgit_print_tag(char *revname);
276extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix); 284extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix);
277extern void cgit_print_patch(char *hex); 285extern void cgit_print_patch(char *hex);
278extern void cgit_print_snapshot(const char *head, const char *hex, 286extern void cgit_print_snapshot(const char *head, const char *hex,
279 const char *prefix, const char *filename, 287 const char *prefix, const char *filename,
280 int snapshot); 288 int snapshot);
281extern void cgit_print_snapshot_links(const char *repo, const char *head, 289extern void cgit_print_snapshot_links(const char *repo, const char *head,
282 const char *hex, int snapshots); 290 const char *hex, int snapshots);
283extern int cgit_parse_snapshots_mask(const char *str); 291extern int cgit_parse_snapshots_mask(const char *str);
284 292
285#endif /* CGIT_H */ 293#endif /* CGIT_H */
diff --git a/shared.c b/shared.c
index 67eb67b..800c06a 100644
--- a/shared.c
+++ b/shared.c
@@ -1,481 +1,508 @@
1/* shared.c: global vars + some callback functions 1/* shared.c: global vars + some callback functions
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11struct cgit_repolist cgit_repolist; 11struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 12struct cgit_context ctx;
13int cgit_cmd; 13int cgit_cmd;
14 14
15const char *cgit_version = CGIT_VERSION; 15const char *cgit_version = CGIT_VERSION;
16 16
17void cgit_prepare_context(struct cgit_context *ctx) 17void cgit_prepare_context(struct cgit_context *ctx)
18{ 18{
19 memset(ctx, 0, sizeof(ctx)); 19 memset(ctx, 0, sizeof(ctx));
20 ctx->cfg.agefile = "info/web/last-modified"; 20 ctx->cfg.agefile = "info/web/last-modified";
21 ctx->cfg.cache_dynamic_ttl = 5; 21 ctx->cfg.cache_dynamic_ttl = 5;
22 ctx->cfg.cache_max_create_time = 5; 22 ctx->cfg.cache_max_create_time = 5;
23 ctx->cfg.cache_repo_ttl = 5; 23 ctx->cfg.cache_repo_ttl = 5;
24 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 24 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
25 ctx->cfg.cache_root_ttl = 5; 25 ctx->cfg.cache_root_ttl = 5;
26 ctx->cfg.cache_static_ttl = -1; 26 ctx->cfg.cache_static_ttl = -1;
27 ctx->cfg.css = "/cgit.css"; 27 ctx->cfg.css = "/cgit.css";
28 ctx->cfg.logo = "/git-logo.png"; 28 ctx->cfg.logo = "/git-logo.png";
29 ctx->cfg.max_commit_count = 50; 29 ctx->cfg.max_commit_count = 50;
30 ctx->cfg.max_lock_attempts = 5; 30 ctx->cfg.max_lock_attempts = 5;
31 ctx->cfg.max_msg_len = 60; 31 ctx->cfg.max_msg_len = 60;
32 ctx->cfg.max_repodesc_len = 60; 32 ctx->cfg.max_repodesc_len = 60;
33 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 33 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
34 ctx->cfg.renamelimit = -1; 34 ctx->cfg.renamelimit = -1;
35 ctx->cfg.robots = "index, nofollow"; 35 ctx->cfg.robots = "index, nofollow";
36 ctx->cfg.root_title = "Git repository browser"; 36 ctx->cfg.root_title = "Git repository browser";
37 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 37 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
38 ctx->page.mimetype = "text/html"; 38 ctx->page.mimetype = "text/html";
39 ctx->page.charset = PAGE_ENCODING; 39 ctx->page.charset = PAGE_ENCODING;
40 ctx->page.filename = NULL; 40 ctx->page.filename = NULL;
41} 41}
42 42
43int chk_zero(int result, char *msg) 43int chk_zero(int result, char *msg)
44{ 44{
45 if (result != 0) 45 if (result != 0)
46 die("%s: %s", msg, strerror(errno)); 46 die("%s: %s", msg, strerror(errno));
47 return result; 47 return result;
48} 48}
49 49
50int chk_positive(int result, char *msg) 50int chk_positive(int result, char *msg)
51{ 51{
52 if (result <= 0) 52 if (result <= 0)
53 die("%s: %s", msg, strerror(errno)); 53 die("%s: %s", msg, strerror(errno));
54 return result; 54 return result;
55} 55}
56 56
57int chk_non_negative(int result, char *msg) 57int chk_non_negative(int result, char *msg)
58{ 58{
59 if (result < 0) 59 if (result < 0)
60 die("%s: %s",msg, strerror(errno)); 60 die("%s: %s",msg, strerror(errno));
61 return result; 61 return result;
62} 62}
63 63
64struct cgit_repo *add_repo(const char *url) 64struct cgit_repo *add_repo(const char *url)
65{ 65{
66 struct cgit_repo *ret; 66 struct cgit_repo *ret;
67 67
68 if (++cgit_repolist.count > cgit_repolist.length) { 68 if (++cgit_repolist.count > cgit_repolist.length) {
69 if (cgit_repolist.length == 0) 69 if (cgit_repolist.length == 0)
70 cgit_repolist.length = 8; 70 cgit_repolist.length = 8;
71 else 71 else
72 cgit_repolist.length *= 2; 72 cgit_repolist.length *= 2;
73 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 73 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
74 cgit_repolist.length * 74 cgit_repolist.length *
75 sizeof(struct cgit_repo)); 75 sizeof(struct cgit_repo));
76 } 76 }
77 77
78 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 78 ret = &cgit_repolist.repos[cgit_repolist.count-1];
79 ret->url = trim_end(url, '/'); 79 ret->url = trim_end(url, '/');
80 ret->name = ret->url; 80 ret->name = ret->url;
81 ret->path = NULL; 81 ret->path = NULL;
82 ret->desc = "[no description]"; 82 ret->desc = "[no description]";
83 ret->owner = NULL; 83 ret->owner = NULL;
84 ret->group = ctx.cfg.repo_group; 84 ret->group = ctx.cfg.repo_group;
85 ret->defbranch = "master"; 85 ret->defbranch = "master";
86 ret->snapshots = ctx.cfg.snapshots; 86 ret->snapshots = ctx.cfg.snapshots;
87 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 87 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
88 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 88 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
89 ret->module_link = ctx.cfg.module_link; 89 ret->module_link = ctx.cfg.module_link;
90 ret->readme = NULL; 90 ret->readme = NULL;
91 return ret; 91 return ret;
92} 92}
93 93
94struct cgit_repo *cgit_get_repoinfo(const char *url) 94struct cgit_repo *cgit_get_repoinfo(const char *url)
95{ 95{
96 int i; 96 int i;
97 struct cgit_repo *repo; 97 struct cgit_repo *repo;
98 98
99 for (i=0; i<cgit_repolist.count; i++) { 99 for (i=0; i<cgit_repolist.count; i++) {
100 repo = &cgit_repolist.repos[i]; 100 repo = &cgit_repolist.repos[i];
101 if (!strcmp(repo->url, url)) 101 if (!strcmp(repo->url, url))
102 return repo; 102 return repo;
103 } 103 }
104 return NULL; 104 return NULL;
105} 105}
106 106
107void cgit_global_config_cb(const char *name, const char *value) 107void cgit_global_config_cb(const char *name, const char *value)
108{ 108{
109 if (!strcmp(name, "root-title")) 109 if (!strcmp(name, "root-title"))
110 ctx.cfg.root_title = xstrdup(value); 110 ctx.cfg.root_title = xstrdup(value);
111 else if (!strcmp(name, "css")) 111 else if (!strcmp(name, "css"))
112 ctx.cfg.css = xstrdup(value); 112 ctx.cfg.css = xstrdup(value);
113 else if (!strcmp(name, "logo")) 113 else if (!strcmp(name, "logo"))
114 ctx.cfg.logo = xstrdup(value); 114 ctx.cfg.logo = xstrdup(value);
115 else if (!strcmp(name, "index-header")) 115 else if (!strcmp(name, "index-header"))
116 ctx.cfg.index_header = xstrdup(value); 116 ctx.cfg.index_header = xstrdup(value);
117 else if (!strcmp(name, "index-info")) 117 else if (!strcmp(name, "index-info"))
118 ctx.cfg.index_info = xstrdup(value); 118 ctx.cfg.index_info = xstrdup(value);
119 else if (!strcmp(name, "logo-link")) 119 else if (!strcmp(name, "logo-link"))
120 ctx.cfg.logo_link = xstrdup(value); 120 ctx.cfg.logo_link = xstrdup(value);
121 else if (!strcmp(name, "module-link")) 121 else if (!strcmp(name, "module-link"))
122 ctx.cfg.module_link = xstrdup(value); 122 ctx.cfg.module_link = xstrdup(value);
123 else if (!strcmp(name, "virtual-root")) { 123 else if (!strcmp(name, "virtual-root")) {
124 ctx.cfg.virtual_root = trim_end(value, '/'); 124 ctx.cfg.virtual_root = trim_end(value, '/');
125 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 125 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
126 ctx.cfg.virtual_root = ""; 126 ctx.cfg.virtual_root = "";
127 } else if (!strcmp(name, "nocache")) 127 } else if (!strcmp(name, "nocache"))
128 ctx.cfg.nocache = atoi(value); 128 ctx.cfg.nocache = atoi(value);
129 else if (!strcmp(name, "snapshots")) 129 else if (!strcmp(name, "snapshots"))
130 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 130 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
131 else if (!strcmp(name, "enable-index-links")) 131 else if (!strcmp(name, "enable-index-links"))
132 ctx.cfg.enable_index_links = atoi(value); 132 ctx.cfg.enable_index_links = atoi(value);
133 else if (!strcmp(name, "enable-log-filecount")) 133 else if (!strcmp(name, "enable-log-filecount"))
134 ctx.cfg.enable_log_filecount = atoi(value); 134 ctx.cfg.enable_log_filecount = atoi(value);
135 else if (!strcmp(name, "enable-log-linecount")) 135 else if (!strcmp(name, "enable-log-linecount"))
136 ctx.cfg.enable_log_linecount = atoi(value); 136 ctx.cfg.enable_log_linecount = atoi(value);
137 else if (!strcmp(name, "cache-root")) 137 else if (!strcmp(name, "cache-root"))
138 ctx.cfg.cache_root = xstrdup(value); 138 ctx.cfg.cache_root = xstrdup(value);
139 else if (!strcmp(name, "cache-root-ttl")) 139 else if (!strcmp(name, "cache-root-ttl"))
140 ctx.cfg.cache_root_ttl = atoi(value); 140 ctx.cfg.cache_root_ttl = atoi(value);
141 else if (!strcmp(name, "cache-repo-ttl")) 141 else if (!strcmp(name, "cache-repo-ttl"))
142 ctx.cfg.cache_repo_ttl = atoi(value); 142 ctx.cfg.cache_repo_ttl = atoi(value);
143 else if (!strcmp(name, "cache-static-ttl")) 143 else if (!strcmp(name, "cache-static-ttl"))
144 ctx.cfg.cache_static_ttl = atoi(value); 144 ctx.cfg.cache_static_ttl = atoi(value);
145 else if (!strcmp(name, "cache-dynamic-ttl")) 145 else if (!strcmp(name, "cache-dynamic-ttl"))
146 ctx.cfg.cache_dynamic_ttl = atoi(value); 146 ctx.cfg.cache_dynamic_ttl = atoi(value);
147 else if (!strcmp(name, "max-message-length")) 147 else if (!strcmp(name, "max-message-length"))
148 ctx.cfg.max_msg_len = atoi(value); 148 ctx.cfg.max_msg_len = atoi(value);
149 else if (!strcmp(name, "max-repodesc-length")) 149 else if (!strcmp(name, "max-repodesc-length"))
150 ctx.cfg.max_repodesc_len = atoi(value); 150 ctx.cfg.max_repodesc_len = atoi(value);
151 else if (!strcmp(name, "max-commit-count")) 151 else if (!strcmp(name, "max-commit-count"))
152 ctx.cfg.max_commit_count = atoi(value); 152 ctx.cfg.max_commit_count = atoi(value);
153 else if (!strcmp(name, "summary-log")) 153 else if (!strcmp(name, "summary-log"))
154 ctx.cfg.summary_log = atoi(value); 154 ctx.cfg.summary_log = atoi(value);
155 else if (!strcmp(name, "summary-branches")) 155 else if (!strcmp(name, "summary-branches"))
156 ctx.cfg.summary_branches = atoi(value); 156 ctx.cfg.summary_branches = atoi(value);
157 else if (!strcmp(name, "summary-tags")) 157 else if (!strcmp(name, "summary-tags"))
158 ctx.cfg.summary_tags = atoi(value); 158 ctx.cfg.summary_tags = atoi(value);
159 else if (!strcmp(name, "agefile")) 159 else if (!strcmp(name, "agefile"))
160 ctx.cfg.agefile = xstrdup(value); 160 ctx.cfg.agefile = xstrdup(value);
161 else if (!strcmp(name, "renamelimit")) 161 else if (!strcmp(name, "renamelimit"))
162 ctx.cfg.renamelimit = atoi(value); 162 ctx.cfg.renamelimit = atoi(value);
163 else if (!strcmp(name, "robots")) 163 else if (!strcmp(name, "robots"))
164 ctx.cfg.robots = xstrdup(value); 164 ctx.cfg.robots = xstrdup(value);
165 else if (!strcmp(name, "clone-prefix")) 165 else if (!strcmp(name, "clone-prefix"))
166 ctx.cfg.clone_prefix = xstrdup(value); 166 ctx.cfg.clone_prefix = xstrdup(value);
167 else if (!strcmp(name, "repo.group")) 167 else if (!strcmp(name, "repo.group"))
168 ctx.cfg.repo_group = xstrdup(value); 168 ctx.cfg.repo_group = xstrdup(value);
169 else if (!strcmp(name, "repo.url")) 169 else if (!strcmp(name, "repo.url"))
170 ctx.repo = add_repo(value); 170 ctx.repo = add_repo(value);
171 else if (!strcmp(name, "repo.name")) 171 else if (!strcmp(name, "repo.name"))
172 ctx.repo->name = xstrdup(value); 172 ctx.repo->name = xstrdup(value);
173 else if (ctx.repo && !strcmp(name, "repo.path")) 173 else if (ctx.repo && !strcmp(name, "repo.path"))
174 ctx.repo->path = trim_end(value, '/'); 174 ctx.repo->path = trim_end(value, '/');
175 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 175 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
176 ctx.repo->clone_url = xstrdup(value); 176 ctx.repo->clone_url = xstrdup(value);
177 else if (ctx.repo && !strcmp(name, "repo.desc")) 177 else if (ctx.repo && !strcmp(name, "repo.desc"))
178 ctx.repo->desc = xstrdup(value); 178 ctx.repo->desc = xstrdup(value);
179 else if (ctx.repo && !strcmp(name, "repo.owner")) 179 else if (ctx.repo && !strcmp(name, "repo.owner"))
180 ctx.repo->owner = xstrdup(value); 180 ctx.repo->owner = xstrdup(value);
181 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 181 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
182 ctx.repo->defbranch = xstrdup(value); 182 ctx.repo->defbranch = xstrdup(value);
183 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 183 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
184 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 184 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
185 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 185 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
186 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 186 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
187 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 187 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
188 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 188 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
189 else if (ctx.repo && !strcmp(name, "repo.module-link")) 189 else if (ctx.repo && !strcmp(name, "repo.module-link"))
190 ctx.repo->module_link= xstrdup(value); 190 ctx.repo->module_link= xstrdup(value);
191 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 191 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
192 if (*value == '/') 192 if (*value == '/')
193 ctx.repo->readme = xstrdup(value); 193 ctx.repo->readme = xstrdup(value);
194 else 194 else
195 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 195 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
196 } else if (!strcmp(name, "include")) 196 } else if (!strcmp(name, "include"))
197 cgit_read_config(value, cgit_global_config_cb); 197 cgit_read_config(value, cgit_global_config_cb);
198} 198}
199 199
200void cgit_querystring_cb(const char *name, const char *value) 200void cgit_querystring_cb(const char *name, const char *value)
201{ 201{
202 if (!strcmp(name,"r")) { 202 if (!strcmp(name,"r")) {
203 ctx.qry.repo = xstrdup(value); 203 ctx.qry.repo = xstrdup(value);
204 ctx.repo = cgit_get_repoinfo(value); 204 ctx.repo = cgit_get_repoinfo(value);
205 } else if (!strcmp(name, "p")) { 205 } else if (!strcmp(name, "p")) {
206 ctx.qry.page = xstrdup(value); 206 ctx.qry.page = xstrdup(value);
207 } else if (!strcmp(name, "url")) { 207 } else if (!strcmp(name, "url")) {
208 cgit_parse_url(value); 208 cgit_parse_url(value);
209 } else if (!strcmp(name, "qt")) { 209 } else if (!strcmp(name, "qt")) {
210 ctx.qry.grep = xstrdup(value); 210 ctx.qry.grep = xstrdup(value);
211 } else if (!strcmp(name, "q")) { 211 } else if (!strcmp(name, "q")) {
212 ctx.qry.search = xstrdup(value); 212 ctx.qry.search = xstrdup(value);
213 } else if (!strcmp(name, "h")) { 213 } else if (!strcmp(name, "h")) {
214 ctx.qry.head = xstrdup(value); 214 ctx.qry.head = xstrdup(value);
215 ctx.qry.has_symref = 1; 215 ctx.qry.has_symref = 1;
216 } else if (!strcmp(name, "id")) { 216 } else if (!strcmp(name, "id")) {
217 ctx.qry.sha1 = xstrdup(value); 217 ctx.qry.sha1 = xstrdup(value);
218 ctx.qry.has_sha1 = 1; 218 ctx.qry.has_sha1 = 1;
219 } else if (!strcmp(name, "id2")) { 219 } else if (!strcmp(name, "id2")) {
220 ctx.qry.sha2 = xstrdup(value); 220 ctx.qry.sha2 = xstrdup(value);
221 ctx.qry.has_sha1 = 1; 221 ctx.qry.has_sha1 = 1;
222 } else if (!strcmp(name, "ofs")) { 222 } else if (!strcmp(name, "ofs")) {
223 ctx.qry.ofs = atoi(value); 223 ctx.qry.ofs = atoi(value);
224 } else if (!strcmp(name, "path")) { 224 } else if (!strcmp(name, "path")) {
225 ctx.qry.path = trim_end(value, '/'); 225 ctx.qry.path = trim_end(value, '/');
226 } else if (!strcmp(name, "name")) { 226 } else if (!strcmp(name, "name")) {
227 ctx.qry.name = xstrdup(value); 227 ctx.qry.name = xstrdup(value);
228 } 228 }
229} 229}
230 230
231void *cgit_free_commitinfo(struct commitinfo *info) 231void *cgit_free_commitinfo(struct commitinfo *info)
232{ 232{
233 free(info->author); 233 free(info->author);
234 free(info->author_email); 234 free(info->author_email);
235 free(info->committer); 235 free(info->committer);
236 free(info->committer_email); 236 free(info->committer_email);
237 free(info->subject); 237 free(info->subject);
238 free(info->msg); 238 free(info->msg);
239 free(info->msg_encoding); 239 free(info->msg_encoding);
240 free(info); 240 free(info);
241 return NULL; 241 return NULL;
242} 242}
243 243
244int hextoint(char c) 244int hextoint(char c)
245{ 245{
246 if (c >= 'a' && c <= 'f') 246 if (c >= 'a' && c <= 'f')
247 return 10 + c - 'a'; 247 return 10 + c - 'a';
248 else if (c >= 'A' && c <= 'F') 248 else if (c >= 'A' && c <= 'F')
249 return 10 + c - 'A'; 249 return 10 + c - 'A';
250 else if (c >= '0' && c <= '9') 250 else if (c >= '0' && c <= '9')
251 return c - '0'; 251 return c - '0';
252 else 252 else
253 return -1; 253 return -1;
254} 254}
255 255
256char *trim_end(const char *str, char c) 256char *trim_end(const char *str, char c)
257{ 257{
258 int len; 258 int len;
259 char *s, *t; 259 char *s, *t;
260 260
261 if (str == NULL) 261 if (str == NULL)
262 return NULL; 262 return NULL;
263 t = (char *)str; 263 t = (char *)str;
264 len = strlen(t); 264 len = strlen(t);
265 while(len > 0 && t[len - 1] == c) 265 while(len > 0 && t[len - 1] == c)
266 len--; 266 len--;
267 267
268 if (len == 0) 268 if (len == 0)
269 return NULL; 269 return NULL;
270 270
271 c = t[len]; 271 c = t[len];
272 t[len] = '\0'; 272 t[len] = '\0';
273 s = xstrdup(t); 273 s = xstrdup(t);
274 t[len] = c; 274 t[len] = c;
275 return s; 275 return s;
276} 276}
277 277
278char *strlpart(char *txt, int maxlen) 278char *strlpart(char *txt, int maxlen)
279{ 279{
280 char *result; 280 char *result;
281 281
282 if (!txt) 282 if (!txt)
283 return txt; 283 return txt;
284 284
285 if (strlen(txt) <= maxlen) 285 if (strlen(txt) <= maxlen)
286 return txt; 286 return txt;
287 result = xmalloc(maxlen + 1); 287 result = xmalloc(maxlen + 1);
288 memcpy(result, txt, maxlen - 3); 288 memcpy(result, txt, maxlen - 3);
289 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; 289 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
290 result[maxlen] = '\0'; 290 result[maxlen] = '\0';
291 return result; 291 return result;
292} 292}
293 293
294char *strrpart(char *txt, int maxlen) 294char *strrpart(char *txt, int maxlen)
295{ 295{
296 char *result; 296 char *result;
297 297
298 if (!txt) 298 if (!txt)
299 return txt; 299 return txt;
300 300
301 if (strlen(txt) <= maxlen) 301 if (strlen(txt) <= maxlen)
302 return txt; 302 return txt;
303 result = xmalloc(maxlen + 1); 303 result = xmalloc(maxlen + 1);
304 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); 304 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
305 result[0] = result[1] = result[2] = '.'; 305 result[0] = result[1] = result[2] = '.';
306 return result; 306 return result;
307} 307}
308 308
309void cgit_add_ref(struct reflist *list, struct refinfo *ref) 309void cgit_add_ref(struct reflist *list, struct refinfo *ref)
310{ 310{
311 size_t size; 311 size_t size;
312 312
313 if (list->count >= list->alloc) { 313 if (list->count >= list->alloc) {
314 list->alloc += (list->alloc ? list->alloc : 4); 314 list->alloc += (list->alloc ? list->alloc : 4);
315 size = list->alloc * sizeof(struct refinfo *); 315 size = list->alloc * sizeof(struct refinfo *);
316 list->refs = xrealloc(list->refs, size); 316 list->refs = xrealloc(list->refs, size);
317 } 317 }
318 list->refs[list->count++] = ref; 318 list->refs[list->count++] = ref;
319} 319}
320 320
321struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) 321struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
322{ 322{
323 struct refinfo *ref; 323 struct refinfo *ref;
324 324
325 ref = xmalloc(sizeof (struct refinfo)); 325 ref = xmalloc(sizeof (struct refinfo));
326 ref->refname = xstrdup(refname); 326 ref->refname = xstrdup(refname);
327 ref->object = parse_object(sha1); 327 ref->object = parse_object(sha1);
328 switch (ref->object->type) { 328 switch (ref->object->type) {
329 case OBJ_TAG: 329 case OBJ_TAG:
330 ref->tag = cgit_parse_tag((struct tag *)ref->object); 330 ref->tag = cgit_parse_tag((struct tag *)ref->object);
331 break; 331 break;
332 case OBJ_COMMIT: 332 case OBJ_COMMIT:
333 ref->commit = cgit_parse_commit((struct commit *)ref->object); 333 ref->commit = cgit_parse_commit((struct commit *)ref->object);
334 break; 334 break;
335 } 335 }
336 return ref; 336 return ref;
337} 337}
338 338
339int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, 339int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
340 void *cb_data) 340 void *cb_data)
341{ 341{
342 struct reflist *list = (struct reflist *)cb_data; 342 struct reflist *list = (struct reflist *)cb_data;
343 struct refinfo *info = cgit_mk_refinfo(refname, sha1); 343 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
344 344
345 if (info) 345 if (info)
346 cgit_add_ref(list, info); 346 cgit_add_ref(list, info);
347 return 0; 347 return 0;
348} 348}
349 349
350void cgit_diff_tree_cb(struct diff_queue_struct *q, 350void cgit_diff_tree_cb(struct diff_queue_struct *q,
351 struct diff_options *options, void *data) 351 struct diff_options *options, void *data)
352{ 352{
353 int i; 353 int i;
354 354
355 for (i = 0; i < q->nr; i++) { 355 for (i = 0; i < q->nr; i++) {
356 if (q->queue[i]->status == 'U') 356 if (q->queue[i]->status == 'U')
357 continue; 357 continue;
358 ((filepair_fn)data)(q->queue[i]); 358 ((filepair_fn)data)(q->queue[i]);
359 } 359 }
360} 360}
361 361
362static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 362static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
363{ 363{
364 enum object_type type; 364 enum object_type type;
365 365
366 if (is_null_sha1(sha1)) { 366 if (is_null_sha1(sha1)) {
367 file->ptr = (char *)""; 367 file->ptr = (char *)"";
368 file->size = 0; 368 file->size = 0;
369 } else { 369 } else {
370 file->ptr = read_sha1_file(sha1, &type, 370 file->ptr = read_sha1_file(sha1, &type,
371 (unsigned long *)&file->size); 371 (unsigned long *)&file->size);
372 } 372 }
373 return 1; 373 return 1;
374} 374}
375 375
376/* 376/*
377 * Receive diff-buffers from xdiff and concatenate them as 377 * Receive diff-buffers from xdiff and concatenate them as
378 * needed across multiple callbacks. 378 * needed across multiple callbacks.
379 * 379 *
380 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 380 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
381 * ripped from git and modified to use globals instead of 381 * ripped from git and modified to use globals instead of
382 * a special callback-struct. 382 * a special callback-struct.
383 */ 383 */
384char *diffbuf = NULL; 384char *diffbuf = NULL;
385int buflen = 0; 385int buflen = 0;
386 386
387int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) 387int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
388{ 388{
389 int i; 389 int i;
390 390
391 for (i = 0; i < nbuf; i++) { 391 for (i = 0; i < nbuf; i++) {
392 if (mb[i].ptr[mb[i].size-1] != '\n') { 392 if (mb[i].ptr[mb[i].size-1] != '\n') {
393 /* Incomplete line */ 393 /* Incomplete line */
394 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 394 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
395 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 395 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
396 buflen += mb[i].size; 396 buflen += mb[i].size;
397 continue; 397 continue;
398 } 398 }
399 399
400 /* we have a complete line */ 400 /* we have a complete line */
401 if (!diffbuf) { 401 if (!diffbuf) {
402 ((linediff_fn)priv)(mb[i].ptr, mb[i].size); 402 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
403 continue; 403 continue;
404 } 404 }
405 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 405 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
406 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 406 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
407 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); 407 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
408 free(diffbuf); 408 free(diffbuf);
409 diffbuf = NULL; 409 diffbuf = NULL;
410 buflen = 0; 410 buflen = 0;
411 } 411 }
412 if (diffbuf) { 412 if (diffbuf) {
413 ((linediff_fn)priv)(diffbuf, buflen); 413 ((linediff_fn)priv)(diffbuf, buflen);
414 free(diffbuf); 414 free(diffbuf);
415 diffbuf = NULL; 415 diffbuf = NULL;
416 buflen = 0; 416 buflen = 0;
417 } 417 }
418 return 0; 418 return 0;
419} 419}
420 420
421int cgit_diff_files(const unsigned char *old_sha1, 421int cgit_diff_files(const unsigned char *old_sha1,
422 const unsigned char *new_sha1, 422 const unsigned char *new_sha1,
423 linediff_fn fn) 423 linediff_fn fn)
424{ 424{
425 mmfile_t file1, file2; 425 mmfile_t file1, file2;
426 xpparam_t diff_params; 426 xpparam_t diff_params;
427 xdemitconf_t emit_params; 427 xdemitconf_t emit_params;
428 xdemitcb_t emit_cb; 428 xdemitcb_t emit_cb;
429 429
430 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) 430 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
431 return 1; 431 return 1;
432 432
433 diff_params.flags = XDF_NEED_MINIMAL; 433 diff_params.flags = XDF_NEED_MINIMAL;
434 emit_params.ctxlen = 3; 434 emit_params.ctxlen = 3;
435 emit_params.flags = XDL_EMIT_FUNCNAMES; 435 emit_params.flags = XDL_EMIT_FUNCNAMES;
436 emit_params.find_func = NULL; 436 emit_params.find_func = NULL;
437 emit_cb.outf = filediff_cb; 437 emit_cb.outf = filediff_cb;
438 emit_cb.priv = fn; 438 emit_cb.priv = fn;
439 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); 439 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
440 return 0; 440 return 0;
441} 441}
442 442
443void cgit_diff_tree(const unsigned char *old_sha1, 443void cgit_diff_tree(const unsigned char *old_sha1,
444 const unsigned char *new_sha1, 444 const unsigned char *new_sha1,
445 filepair_fn fn, const char *prefix) 445 filepair_fn fn, const char *prefix)
446{ 446{
447 struct diff_options opt; 447 struct diff_options opt;
448 int ret; 448 int ret;
449 int prefixlen; 449 int prefixlen;
450 450
451 diff_setup(&opt); 451 diff_setup(&opt);
452 opt.output_format = DIFF_FORMAT_CALLBACK; 452 opt.output_format = DIFF_FORMAT_CALLBACK;
453 opt.detect_rename = 1; 453 opt.detect_rename = 1;
454 opt.rename_limit = ctx.cfg.renamelimit; 454 opt.rename_limit = ctx.cfg.renamelimit;
455 DIFF_OPT_SET(&opt, RECURSIVE); 455 DIFF_OPT_SET(&opt, RECURSIVE);
456 opt.format_callback = cgit_diff_tree_cb; 456 opt.format_callback = cgit_diff_tree_cb;
457 opt.format_callback_data = fn; 457 opt.format_callback_data = fn;
458 if (prefix) { 458 if (prefix) {
459 opt.nr_paths = 1; 459 opt.nr_paths = 1;
460 opt.paths = &prefix; 460 opt.paths = &prefix;
461 prefixlen = strlen(prefix); 461 prefixlen = strlen(prefix);
462 opt.pathlens = &prefixlen; 462 opt.pathlens = &prefixlen;
463 } 463 }
464 diff_setup_done(&opt); 464 diff_setup_done(&opt);
465 465
466 if (old_sha1 && !is_null_sha1(old_sha1)) 466 if (old_sha1 && !is_null_sha1(old_sha1))
467 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); 467 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
468 else 468 else
469 ret = diff_root_tree_sha1(new_sha1, "", &opt); 469 ret = diff_root_tree_sha1(new_sha1, "", &opt);
470 diffcore_std(&opt); 470 diffcore_std(&opt);
471 diff_flush(&opt); 471 diff_flush(&opt);
472} 472}
473 473
474void cgit_diff_commit(struct commit *commit, filepair_fn fn) 474void cgit_diff_commit(struct commit *commit, filepair_fn fn)
475{ 475{
476 unsigned char *old_sha1 = NULL; 476 unsigned char *old_sha1 = NULL;
477 477
478 if (commit->parents) 478 if (commit->parents)
479 old_sha1 = commit->parents->item->object.sha1; 479 old_sha1 = commit->parents->item->object.sha1;
480 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); 480 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
481} 481}
482
483int cgit_parse_snapshots_mask(const char *str)
484{
485 const struct cgit_snapshot_format *f;
486 static const char *delim = " \t,:/|;";
487 int tl, sl, rv = 0;
488
489 /* favor legacy setting */
490 if(atoi(str))
491 return 1;
492 for(;;) {
493 str += strspn(str,delim);
494 tl = strcspn(str,delim);
495 if (!tl)
496 break;
497 for (f = cgit_snapshot_formats; f->suffix; f++) {
498 sl = strlen(f->suffix);
499 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
500 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
501 rv |= f->bit;
502 break;
503 }
504 }
505 str += tl;
506 }
507 return rv;
508}
diff --git a/ui-shared.c b/ui-shared.c
index 2596023..aa65988 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,576 +1,591 @@
1/* ui-shared.c: common web output functions 1/* ui-shared.c: common web output functions
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "html.h" 10#include "html.h"
11 11
12const char cgit_doctype[] = 12const char cgit_doctype[] =
13"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 13"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
14" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 14" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
15 15
16static char *http_date(time_t t) 16static char *http_date(time_t t)
17{ 17{
18 static char day[][4] = 18 static char day[][4] =
19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
20 static char month[][4] = 20 static char month[][4] =
21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
23 struct tm *tm = gmtime(&t); 23 struct tm *tm = gmtime(&t);
24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
25 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 25 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
26 tm->tm_hour, tm->tm_min, tm->tm_sec); 26 tm->tm_hour, tm->tm_min, tm->tm_sec);
27} 27}
28 28
29void cgit_print_error(char *msg) 29void cgit_print_error(char *msg)
30{ 30{
31 html("<div class='error'>"); 31 html("<div class='error'>");
32 html_txt(msg); 32 html_txt(msg);
33 html("</div>\n"); 33 html("</div>\n");
34} 34}
35 35
36char *cgit_rooturl() 36char *cgit_rooturl()
37{ 37{
38 if (ctx.cfg.virtual_root) 38 if (ctx.cfg.virtual_root)
39 return fmt("%s/", ctx.cfg.virtual_root); 39 return fmt("%s/", ctx.cfg.virtual_root);
40 else 40 else
41 return ctx.cfg.script_name; 41 return ctx.cfg.script_name;
42} 42}
43 43
44char *cgit_repourl(const char *reponame) 44char *cgit_repourl(const char *reponame)
45{ 45{
46 if (ctx.cfg.virtual_root) { 46 if (ctx.cfg.virtual_root) {
47 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); 47 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
48 } else { 48 } else {
49 return fmt("?r=%s", reponame); 49 return fmt("?r=%s", reponame);
50 } 50 }
51} 51}
52 52
53char *cgit_fileurl(const char *reponame, const char *pagename, 53char *cgit_fileurl(const char *reponame, const char *pagename,
54 const char *filename, const char *query) 54 const char *filename, const char *query)
55{ 55{
56 char *tmp; 56 char *tmp;
57 char *delim; 57 char *delim;
58 58
59 if (ctx.cfg.virtual_root) { 59 if (ctx.cfg.virtual_root) {
60 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, 60 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
61 pagename, (filename ? filename:"")); 61 pagename, (filename ? filename:""));
62 delim = "?"; 62 delim = "?";
63 } else { 63 } else {
64 tmp = fmt("?url=%s/%s/%s", reponame, pagename, 64 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
65 (filename ? filename : "")); 65 (filename ? filename : ""));
66 delim = "&"; 66 delim = "&";
67 } 67 }
68 if (query) 68 if (query)
69 tmp = fmt("%s%s%s", tmp, delim, query); 69 tmp = fmt("%s%s%s", tmp, delim, query);
70 return tmp; 70 return tmp;
71} 71}
72 72
73char *cgit_pageurl(const char *reponame, const char *pagename, 73char *cgit_pageurl(const char *reponame, const char *pagename,
74 const char *query) 74 const char *query)
75{ 75{
76 return cgit_fileurl(reponame,pagename,0,query); 76 return cgit_fileurl(reponame,pagename,0,query);
77} 77}
78 78
79const char *cgit_repobasename(const char *reponame) 79const char *cgit_repobasename(const char *reponame)
80{ 80{
81 /* I assume we don't need to store more than one repo basename */ 81 /* I assume we don't need to store more than one repo basename */
82 static char rvbuf[1024]; 82 static char rvbuf[1024];
83 int p; 83 int p;
84 const char *rv; 84 const char *rv;
85 strncpy(rvbuf,reponame,sizeof(rvbuf)); 85 strncpy(rvbuf,reponame,sizeof(rvbuf));
86 if(rvbuf[sizeof(rvbuf)-1]) 86 if(rvbuf[sizeof(rvbuf)-1])
87 die("cgit_repobasename: truncated repository name '%s'", reponame); 87 die("cgit_repobasename: truncated repository name '%s'", reponame);
88 p = strlen(rvbuf)-1; 88 p = strlen(rvbuf)-1;
89 /* strip trailing slashes */ 89 /* strip trailing slashes */
90 while(p && rvbuf[p]=='/') rvbuf[p--]=0; 90 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
91 /* strip trailing .git */ 91 /* strip trailing .git */
92 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 92 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
93 p -= 3; rvbuf[p--] = 0; 93 p -= 3; rvbuf[p--] = 0;
94 } 94 }
95 /* strip more trailing slashes if any */ 95 /* strip more trailing slashes if any */
96 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 96 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
97 /* find last slash in the remaining string */ 97 /* find last slash in the remaining string */
98 rv = strrchr(rvbuf,'/'); 98 rv = strrchr(rvbuf,'/');
99 if(rv) 99 if(rv)
100 return ++rv; 100 return ++rv;
101 return rvbuf; 101 return rvbuf;
102} 102}
103 103
104char *cgit_currurl() 104char *cgit_currurl()
105{ 105{
106 if (!ctx.cfg.virtual_root) 106 if (!ctx.cfg.virtual_root)
107 return ctx.cfg.script_name; 107 return ctx.cfg.script_name;
108 else if (ctx.qry.page) 108 else if (ctx.qry.page)
109 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); 109 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
110 else if (ctx.qry.repo) 110 else if (ctx.qry.repo)
111 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); 111 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
112 else 112 else
113 return fmt("%s/", ctx.cfg.virtual_root); 113 return fmt("%s/", ctx.cfg.virtual_root);
114} 114}
115 115
116static char *repolink(char *title, char *class, char *page, char *head, 116static char *repolink(char *title, char *class, char *page, char *head,
117 char *path) 117 char *path)
118{ 118{
119 char *delim = "?"; 119 char *delim = "?";
120 120
121 html("<a"); 121 html("<a");
122 if (title) { 122 if (title) {
123 html(" title='"); 123 html(" title='");
124 html_attr(title); 124 html_attr(title);
125 html("'"); 125 html("'");
126 } 126 }
127 if (class) { 127 if (class) {
128 html(" class='"); 128 html(" class='");
129 html_attr(class); 129 html_attr(class);
130 html("'"); 130 html("'");
131 } 131 }
132 html(" href='"); 132 html(" href='");
133 if (ctx.cfg.virtual_root) { 133 if (ctx.cfg.virtual_root) {
134 html_attr(ctx.cfg.virtual_root); 134 html_attr(ctx.cfg.virtual_root);
135 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 135 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
136 html("/"); 136 html("/");
137 html_attr(ctx.repo->url); 137 html_attr(ctx.repo->url);
138 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 138 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
139 html("/"); 139 html("/");
140 if (page) { 140 if (page) {
141 html(page); 141 html(page);
142 html("/"); 142 html("/");
143 if (path) 143 if (path)
144 html_attr(path); 144 html_attr(path);
145 } 145 }
146 } else { 146 } else {
147 html(ctx.cfg.script_name); 147 html(ctx.cfg.script_name);
148 html("?url="); 148 html("?url=");
149 html_attr(ctx.repo->url); 149 html_attr(ctx.repo->url);
150 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 150 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
151 html("/"); 151 html("/");
152 if (page) { 152 if (page) {
153 html(page); 153 html(page);
154 html("/"); 154 html("/");
155 if (path) 155 if (path)
156 html_attr(path); 156 html_attr(path);
157 } 157 }
158 delim = "&amp;"; 158 delim = "&amp;";
159 } 159 }
160 if (head && strcmp(head, ctx.repo->defbranch)) { 160 if (head && strcmp(head, ctx.repo->defbranch)) {
161 html(delim); 161 html(delim);
162 html("h="); 162 html("h=");
163 html_attr(head); 163 html_attr(head);
164 delim = "&amp;"; 164 delim = "&amp;";
165 } 165 }
166 return fmt("%s", delim); 166 return fmt("%s", delim);
167} 167}
168 168
169static void reporevlink(char *page, char *name, char *title, char *class, 169static void reporevlink(char *page, char *name, char *title, char *class,
170 char *head, char *rev, char *path) 170 char *head, char *rev, char *path)
171{ 171{
172 char *delim; 172 char *delim;
173 173
174 delim = repolink(title, class, page, head, path); 174 delim = repolink(title, class, page, head, path);
175 if (rev && strcmp(rev, ctx.qry.head)) { 175 if (rev && strcmp(rev, ctx.qry.head)) {
176 html(delim); 176 html(delim);
177 html("id="); 177 html("id=");
178 html_attr(rev); 178 html_attr(rev);
179 } 179 }
180 html("'>"); 180 html("'>");
181 html_txt(name); 181 html_txt(name);
182 html("</a>"); 182 html("</a>");
183} 183}
184 184
185void cgit_tree_link(char *name, char *title, char *class, char *head, 185void cgit_tree_link(char *name, char *title, char *class, char *head,
186 char *rev, char *path) 186 char *rev, char *path)
187{ 187{
188 reporevlink("tree", name, title, class, head, rev, path); 188 reporevlink("tree", name, title, class, head, rev, path);
189} 189}
190 190
191void cgit_log_link(char *name, char *title, char *class, char *head, 191void cgit_log_link(char *name, char *title, char *class, char *head,
192 char *rev, char *path, int ofs, char *grep, char *pattern) 192 char *rev, char *path, int ofs, char *grep, char *pattern)
193{ 193{
194 char *delim; 194 char *delim;
195 195
196 delim = repolink(title, class, "log", head, path); 196 delim = repolink(title, class, "log", head, path);
197 if (rev && strcmp(rev, ctx.qry.head)) { 197 if (rev && strcmp(rev, ctx.qry.head)) {
198 html(delim); 198 html(delim);
199 html("id="); 199 html("id=");
200 html_attr(rev); 200 html_attr(rev);
201 delim = "&"; 201 delim = "&";
202 } 202 }
203 if (grep && pattern) { 203 if (grep && pattern) {
204 html(delim); 204 html(delim);
205 html("qt="); 205 html("qt=");
206 html_attr(grep); 206 html_attr(grep);
207 delim = "&"; 207 delim = "&";
208 html(delim); 208 html(delim);
209 html("q="); 209 html("q=");
210 html_attr(pattern); 210 html_attr(pattern);
211 } 211 }
212 if (ofs > 0) { 212 if (ofs > 0) {
213 html(delim); 213 html(delim);
214 html("ofs="); 214 html("ofs=");
215 htmlf("%d", ofs); 215 htmlf("%d", ofs);
216 } 216 }
217 html("'>"); 217 html("'>");
218 html_txt(name); 218 html_txt(name);
219 html("</a>"); 219 html("</a>");
220} 220}
221 221
222void cgit_commit_link(char *name, char *title, char *class, char *head, 222void cgit_commit_link(char *name, char *title, char *class, char *head,
223 char *rev) 223 char *rev)
224{ 224{
225 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { 225 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
226 name[ctx.cfg.max_msg_len] = '\0'; 226 name[ctx.cfg.max_msg_len] = '\0';
227 name[ctx.cfg.max_msg_len - 1] = '.'; 227 name[ctx.cfg.max_msg_len - 1] = '.';
228 name[ctx.cfg.max_msg_len - 2] = '.'; 228 name[ctx.cfg.max_msg_len - 2] = '.';
229 name[ctx.cfg.max_msg_len - 3] = '.'; 229 name[ctx.cfg.max_msg_len - 3] = '.';
230 } 230 }
231 reporevlink("commit", name, title, class, head, rev, NULL); 231 reporevlink("commit", name, title, class, head, rev, NULL);
232} 232}
233 233
234void cgit_refs_link(char *name, char *title, char *class, char *head, 234void cgit_refs_link(char *name, char *title, char *class, char *head,
235 char *rev, char *path) 235 char *rev, char *path)
236{ 236{
237 reporevlink("refs", name, title, class, head, rev, path); 237 reporevlink("refs", name, title, class, head, rev, path);
238} 238}
239 239
240void cgit_snapshot_link(char *name, char *title, char *class, char *head, 240void cgit_snapshot_link(char *name, char *title, char *class, char *head,
241 char *rev, char *archivename) 241 char *rev, char *archivename)
242{ 242{
243 reporevlink("snapshot", name, title, class, head, rev, archivename); 243 reporevlink("snapshot", name, title, class, head, rev, archivename);
244} 244}
245 245
246void cgit_diff_link(char *name, char *title, char *class, char *head, 246void cgit_diff_link(char *name, char *title, char *class, char *head,
247 char *new_rev, char *old_rev, char *path) 247 char *new_rev, char *old_rev, char *path)
248{ 248{
249 char *delim; 249 char *delim;
250 250
251 delim = repolink(title, class, "diff", head, path); 251 delim = repolink(title, class, "diff", head, path);
252 if (new_rev && strcmp(new_rev, ctx.qry.head)) { 252 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
253 html(delim); 253 html(delim);
254 html("id="); 254 html("id=");
255 html_attr(new_rev); 255 html_attr(new_rev);
256 delim = "&amp;"; 256 delim = "&amp;";
257 } 257 }
258 if (old_rev) { 258 if (old_rev) {
259 html(delim); 259 html(delim);
260 html("id2="); 260 html("id2=");
261 html_attr(old_rev); 261 html_attr(old_rev);
262 } 262 }
263 html("'>"); 263 html("'>");
264 html_txt(name); 264 html_txt(name);
265 html("</a>"); 265 html("</a>");
266} 266}
267 267
268void cgit_patch_link(char *name, char *title, char *class, char *head, 268void cgit_patch_link(char *name, char *title, char *class, char *head,
269 char *rev) 269 char *rev)
270{ 270{
271 reporevlink("patch", name, title, class, head, rev, NULL); 271 reporevlink("patch", name, title, class, head, rev, NULL);
272} 272}
273 273
274void cgit_object_link(struct object *obj) 274void cgit_object_link(struct object *obj)
275{ 275{
276 char *page, *arg, *url; 276 char *page, *arg, *url;
277 277
278 if (obj->type == OBJ_COMMIT) { 278 if (obj->type == OBJ_COMMIT) {
279 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 279 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
280 ctx.qry.head, sha1_to_hex(obj->sha1)); 280 ctx.qry.head, sha1_to_hex(obj->sha1));
281 return; 281 return;
282 } else if (obj->type == OBJ_TREE) { 282 } else if (obj->type == OBJ_TREE) {
283 page = "tree"; 283 page = "tree";
284 arg = "id"; 284 arg = "id";
285 } else if (obj->type == OBJ_TAG) { 285 } else if (obj->type == OBJ_TAG) {
286 page = "tag"; 286 page = "tag";
287 arg = "id"; 287 arg = "id";
288 } else { 288 } else {
289 page = "blob"; 289 page = "blob";
290 arg = "id"; 290 arg = "id";
291 } 291 }
292 292
293 url = cgit_pageurl(ctx.qry.repo, page, 293 url = cgit_pageurl(ctx.qry.repo, page,
294 fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); 294 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
295 html_link_open(url, NULL, NULL); 295 html_link_open(url, NULL, NULL);
296 htmlf("%s %s", typename(obj->type), 296 htmlf("%s %s", typename(obj->type),
297 sha1_to_hex(obj->sha1)); 297 sha1_to_hex(obj->sha1));
298 html_link_close(); 298 html_link_close();
299} 299}
300 300
301void cgit_print_date(time_t secs, char *format) 301void cgit_print_date(time_t secs, char *format)
302{ 302{
303 char buf[64]; 303 char buf[64];
304 struct tm *time; 304 struct tm *time;
305 305
306 if (!secs) 306 if (!secs)
307 return; 307 return;
308 time = gmtime(&secs); 308 time = gmtime(&secs);
309 strftime(buf, sizeof(buf)-1, format, time); 309 strftime(buf, sizeof(buf)-1, format, time);
310 html_txt(buf); 310 html_txt(buf);
311} 311}
312 312
313void cgit_print_age(time_t t, time_t max_relative, char *format) 313void cgit_print_age(time_t t, time_t max_relative, char *format)
314{ 314{
315 time_t now, secs; 315 time_t now, secs;
316 316
317 if (!t) 317 if (!t)
318 return; 318 return;
319 time(&now); 319 time(&now);
320 secs = now - t; 320 secs = now - t;
321 321
322 if (secs > max_relative && max_relative >= 0) { 322 if (secs > max_relative && max_relative >= 0) {
323 cgit_print_date(t, format); 323 cgit_print_date(t, format);
324 return; 324 return;
325 } 325 }
326 326
327 if (secs < TM_HOUR * 2) { 327 if (secs < TM_HOUR * 2) {
328 htmlf("<span class='age-mins'>%.0f min.</span>", 328 htmlf("<span class='age-mins'>%.0f min.</span>",
329 secs * 1.0 / TM_MIN); 329 secs * 1.0 / TM_MIN);
330 return; 330 return;
331 } 331 }
332 if (secs < TM_DAY * 2) { 332 if (secs < TM_DAY * 2) {
333 htmlf("<span class='age-hours'>%.0f hours</span>", 333 htmlf("<span class='age-hours'>%.0f hours</span>",
334 secs * 1.0 / TM_HOUR); 334 secs * 1.0 / TM_HOUR);
335 return; 335 return;
336 } 336 }
337 if (secs < TM_WEEK * 2) { 337 if (secs < TM_WEEK * 2) {
338 htmlf("<span class='age-days'>%.0f days</span>", 338 htmlf("<span class='age-days'>%.0f days</span>",
339 secs * 1.0 / TM_DAY); 339 secs * 1.0 / TM_DAY);
340 return; 340 return;
341 } 341 }
342 if (secs < TM_MONTH * 2) { 342 if (secs < TM_MONTH * 2) {
343 htmlf("<span class='age-weeks'>%.0f weeks</span>", 343 htmlf("<span class='age-weeks'>%.0f weeks</span>",
344 secs * 1.0 / TM_WEEK); 344 secs * 1.0 / TM_WEEK);
345 return; 345 return;
346 } 346 }
347 if (secs < TM_YEAR * 2) { 347 if (secs < TM_YEAR * 2) {
348 htmlf("<span class='age-months'>%.0f months</span>", 348 htmlf("<span class='age-months'>%.0f months</span>",
349 secs * 1.0 / TM_MONTH); 349 secs * 1.0 / TM_MONTH);
350 return; 350 return;
351 } 351 }
352 htmlf("<span class='age-years'>%.0f years</span>", 352 htmlf("<span class='age-years'>%.0f years</span>",
353 secs * 1.0 / TM_YEAR); 353 secs * 1.0 / TM_YEAR);
354} 354}
355 355
356void cgit_print_http_headers(struct cgit_context *ctx) 356void cgit_print_http_headers(struct cgit_context *ctx)
357{ 357{
358 if (ctx->page.mimetype && ctx->page.charset) 358 if (ctx->page.mimetype && ctx->page.charset)
359 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, 359 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
360 ctx->page.charset); 360 ctx->page.charset);
361 else if (ctx->page.mimetype) 361 else if (ctx->page.mimetype)
362 htmlf("Content-Type: %s\n", ctx->page.mimetype); 362 htmlf("Content-Type: %s\n", ctx->page.mimetype);
363 if (ctx->page.filename) 363 if (ctx->page.filename)
364 htmlf("Content-Disposition: inline; filename=\"%s\"\n", 364 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
365 ctx->page.filename); 365 ctx->page.filename);
366 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); 366 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
367 htmlf("Expires: %s\n", http_date(ctx->page.expires)); 367 htmlf("Expires: %s\n", http_date(ctx->page.expires));
368 html("\n"); 368 html("\n");
369} 369}
370 370
371void cgit_print_docstart(struct cgit_context *ctx) 371void cgit_print_docstart(struct cgit_context *ctx)
372{ 372{
373 html(cgit_doctype); 373 html(cgit_doctype);
374 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 374 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
375 html("<head>\n"); 375 html("<head>\n");
376 html("<title>"); 376 html("<title>");
377 html_txt(ctx->page.title); 377 html_txt(ctx->page.title);
378 html("</title>\n"); 378 html("</title>\n");
379 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 379 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
380 if (ctx->cfg.robots && *ctx->cfg.robots) 380 if (ctx->cfg.robots && *ctx->cfg.robots)
381 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 381 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
382 html("<link rel='stylesheet' type='text/css' href='"); 382 html("<link rel='stylesheet' type='text/css' href='");
383 html_attr(ctx->cfg.css); 383 html_attr(ctx->cfg.css);
384 html("'/>\n"); 384 html("'/>\n");
385 html("</head>\n"); 385 html("</head>\n");
386 html("<body>\n"); 386 html("<body>\n");
387} 387}
388 388
389void cgit_print_docend() 389void cgit_print_docend()
390{ 390{
391 html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); 391 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
392} 392}
393 393
394int print_branch_option(const char *refname, const unsigned char *sha1, 394int print_branch_option(const char *refname, const unsigned char *sha1,
395 int flags, void *cb_data) 395 int flags, void *cb_data)
396{ 396{
397 char *name = (char *)refname; 397 char *name = (char *)refname;
398 html_option(name, name, ctx.qry.head); 398 html_option(name, name, ctx.qry.head);
399 return 0; 399 return 0;
400} 400}
401 401
402int print_archive_ref(const char *refname, const unsigned char *sha1, 402int print_archive_ref(const char *refname, const unsigned char *sha1,
403 int flags, void *cb_data) 403 int flags, void *cb_data)
404{ 404{
405 struct tag *tag; 405 struct tag *tag;
406 struct taginfo *info; 406 struct taginfo *info;
407 struct object *obj; 407 struct object *obj;
408 char buf[256], *url; 408 char buf[256], *url;
409 unsigned char fileid[20]; 409 unsigned char fileid[20];
410 int *header = (int *)cb_data; 410 int *header = (int *)cb_data;
411 411
412 if (prefixcmp(refname, "refs/archives")) 412 if (prefixcmp(refname, "refs/archives"))
413 return 0; 413 return 0;
414 strncpy(buf, refname+14, sizeof(buf)); 414 strncpy(buf, refname+14, sizeof(buf));
415 obj = parse_object(sha1); 415 obj = parse_object(sha1);
416 if (!obj) 416 if (!obj)
417 return 1; 417 return 1;
418 if (obj->type == OBJ_TAG) { 418 if (obj->type == OBJ_TAG) {
419 tag = lookup_tag(sha1); 419 tag = lookup_tag(sha1);
420 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 420 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
421 return 0; 421 return 0;
422 hashcpy(fileid, tag->tagged->sha1); 422 hashcpy(fileid, tag->tagged->sha1);
423 } else if (obj->type != OBJ_BLOB) { 423 } else if (obj->type != OBJ_BLOB) {
424 return 0; 424 return 0;
425 } else { 425 } else {
426 hashcpy(fileid, sha1); 426 hashcpy(fileid, sha1);
427 } 427 }
428 if (!*header) { 428 if (!*header) {
429 html("<h1>download</h1>\n"); 429 html("<h1>download</h1>\n");
430 *header = 1; 430 *header = 1;
431 } 431 }
432 url = cgit_pageurl(ctx.qry.repo, "blob", 432 url = cgit_pageurl(ctx.qry.repo, "blob",
433 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 433 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
434 buf)); 434 buf));
435 html_link_open(url, NULL, "menu"); 435 html_link_open(url, NULL, "menu");
436 html_txt(strlpart(buf, 20)); 436 html_txt(strlpart(buf, 20));
437 html_link_close(); 437 html_link_close();
438 return 0; 438 return 0;
439} 439}
440 440
441void add_hidden_formfields(int incl_head, int incl_search, char *page) 441void add_hidden_formfields(int incl_head, int incl_search, char *page)
442{ 442{
443 char *url; 443 char *url;
444 444
445 if (!ctx.cfg.virtual_root) { 445 if (!ctx.cfg.virtual_root) {
446 url = fmt("%s/%s", ctx.qry.repo, page); 446 url = fmt("%s/%s", ctx.qry.repo, page);
447 if (ctx.qry.path) 447 if (ctx.qry.path)
448 url = fmt("%s/%s", url, ctx.qry.path); 448 url = fmt("%s/%s", url, ctx.qry.path);
449 html_hidden("url", url); 449 html_hidden("url", url);
450 } 450 }
451 451
452 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) 452 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
453 html_hidden("h", ctx.qry.head); 453 html_hidden("h", ctx.qry.head);
454 454
455 if (ctx.qry.sha1) 455 if (ctx.qry.sha1)
456 html_hidden("id", ctx.qry.sha1); 456 html_hidden("id", ctx.qry.sha1);
457 if (ctx.qry.sha2) 457 if (ctx.qry.sha2)
458 html_hidden("id2", ctx.qry.sha2); 458 html_hidden("id2", ctx.qry.sha2);
459 459
460 if (incl_search) { 460 if (incl_search) {
461 if (ctx.qry.grep) 461 if (ctx.qry.grep)
462 html_hidden("qt", ctx.qry.grep); 462 html_hidden("qt", ctx.qry.grep);
463 if (ctx.qry.search) 463 if (ctx.qry.search)
464 html_hidden("q", ctx.qry.search); 464 html_hidden("q", ctx.qry.search);
465 } 465 }
466} 466}
467 467
468void cgit_print_pageheader(struct cgit_context *ctx) 468void cgit_print_pageheader(struct cgit_context *ctx)
469{ 469{
470 static const char *default_info = "This is cgit, a fast webinterface for git repositories"; 470 static const char *default_info = "This is cgit, a fast webinterface for git repositories";
471 int header = 0; 471 int header = 0;
472 char *url; 472 char *url;
473 473
474 html("<table id='layout' summary=''>\n"); 474 html("<table id='layout' summary=''>\n");
475 html("<tr><td id='sidebar'>\n"); 475 html("<tr><td id='sidebar'>\n");
476 html("<table class='sidebar' cellspacing='0' summary=''>\n"); 476 html("<table class='sidebar' cellspacing='0' summary=''>\n");
477 html("<tr><td class='sidebar'>\n<a href='"); 477 html("<tr><td class='sidebar'>\n<a href='");
478 html_attr(cgit_rooturl()); 478 html_attr(cgit_rooturl());
479 htmlf("'><img src='%s' alt='cgit'/></a>\n", 479 htmlf("'><img src='%s' alt='cgit'/></a>\n",
480 ctx->cfg.logo); 480 ctx->cfg.logo);
481 html("</td></tr>\n<tr><td class='sidebar'>\n"); 481 html("</td></tr>\n<tr><td class='sidebar'>\n");
482 if (ctx->repo) { 482 if (ctx->repo) {
483 html("<h1 class='first'>"); 483 html("<h1 class='first'>");
484 html_txt(strrpart(ctx->repo->name, 20)); 484 html_txt(strrpart(ctx->repo->name, 20));
485 html("</h1>\n"); 485 html("</h1>\n");
486 html_txt(ctx->repo->desc); 486 html_txt(ctx->repo->desc);
487 if (ctx->repo->owner) { 487 if (ctx->repo->owner) {
488 html("<h1>owner</h1>\n"); 488 html("<h1>owner</h1>\n");
489 html_txt(ctx->repo->owner); 489 html_txt(ctx->repo->owner);
490 } 490 }
491 html("<h1>navigate</h1>\n"); 491 html("<h1>navigate</h1>\n");
492 reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head, 492 reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head,
493 NULL, NULL); 493 NULL, NULL);
494 cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL, 494 cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL,
495 0, NULL, NULL); 495 0, NULL, NULL);
496 cgit_tree_link("tree", NULL, "menu", ctx->qry.head, 496 cgit_tree_link("tree", NULL, "menu", ctx->qry.head,
497 ctx->qry.sha1, NULL); 497 ctx->qry.sha1, NULL);
498 cgit_commit_link("commit", NULL, "menu", ctx->qry.head, 498 cgit_commit_link("commit", NULL, "menu", ctx->qry.head,
499 ctx->qry.sha1); 499 ctx->qry.sha1);
500 cgit_diff_link("diff", NULL, "menu", ctx->qry.head, 500 cgit_diff_link("diff", NULL, "menu", ctx->qry.head,
501 ctx->qry.sha1, ctx->qry.sha2, NULL); 501 ctx->qry.sha1, ctx->qry.sha2, NULL);
502 cgit_patch_link("patch", NULL, "menu", ctx->qry.head, 502 cgit_patch_link("patch", NULL, "menu", ctx->qry.head,
503 ctx->qry.sha1); 503 ctx->qry.sha1);
504 504
505 for_each_ref(print_archive_ref, &header); 505 for_each_ref(print_archive_ref, &header);
506 506
507 if (ctx->repo->clone_url || ctx->cfg.clone_prefix) { 507 if (ctx->repo->clone_url || ctx->cfg.clone_prefix) {
508 html("<h1>clone</h1>\n"); 508 html("<h1>clone</h1>\n");
509 if (ctx->repo->clone_url) 509 if (ctx->repo->clone_url)
510 url = ctx->repo->clone_url; 510 url = ctx->repo->clone_url;
511 else 511 else
512 url = fmt("%s%s", ctx->cfg.clone_prefix, 512 url = fmt("%s%s", ctx->cfg.clone_prefix,
513 ctx->repo->url); 513 ctx->repo->url);
514 html("<a class='menu' href='"); 514 html("<a class='menu' href='");
515 html_attr(url); 515 html_attr(url);
516 html("' title='"); 516 html("' title='");
517 html_attr(url); 517 html_attr(url);
518 html("'>\n"); 518 html("'>\n");
519 html_txt(strrpart(url, 20)); 519 html_txt(strrpart(url, 20));
520 html("</a>\n"); 520 html("</a>\n");
521 } 521 }
522 522
523 html("<h1>branch</h1>\n"); 523 html("<h1>branch</h1>\n");
524 html("<form method='get' action=''>\n"); 524 html("<form method='get' action=''>\n");
525 add_hidden_formfields(0, 1, ctx->qry.page); 525 add_hidden_formfields(0, 1, ctx->qry.page);
526 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); 526 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
527 html("<select name='h' onchange='this.form.submit();'>\n"); 527 html("<select name='h' onchange='this.form.submit();'>\n");
528 for_each_branch_ref(print_branch_option, ctx->qry.head); 528 for_each_branch_ref(print_branch_option, ctx->qry.head);
529 html("</select>\n"); 529 html("</select>\n");
530 // html("</td><td>"); 530 // html("</td><td>");
531 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); 531 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
532 // html("</td></tr></table>"); 532 // html("</td></tr></table>");
533 html("</form>\n"); 533 html("</form>\n");
534 534
535 html("<h1>search</h1>\n"); 535 html("<h1>search</h1>\n");
536 html("<form method='get' action='"); 536 html("<form method='get' action='");
537 if (ctx->cfg.virtual_root) 537 if (ctx->cfg.virtual_root)
538 html_attr(cgit_fileurl(ctx->qry.repo, "log", 538 html_attr(cgit_fileurl(ctx->qry.repo, "log",
539 ctx->qry.path, NULL)); 539 ctx->qry.path, NULL));
540 html("'>\n"); 540 html("'>\n");
541 add_hidden_formfields(1, 0, "log"); 541 add_hidden_formfields(1, 0, "log");
542 html("<select name='qt'>\n"); 542 html("<select name='qt'>\n");
543 html_option("grep", "log msg", ctx->qry.grep); 543 html_option("grep", "log msg", ctx->qry.grep);
544 html_option("author", "author", ctx->qry.grep); 544 html_option("author", "author", ctx->qry.grep);
545 html_option("committer", "committer", ctx->qry.grep); 545 html_option("committer", "committer", ctx->qry.grep);
546 html("</select>\n"); 546 html("</select>\n");
547 html("<input class='txt' type='text' name='q' value='"); 547 html("<input class='txt' type='text' name='q' value='");
548 html_attr(ctx->qry.search); 548 html_attr(ctx->qry.search);
549 html("'/>\n"); 549 html("'/>\n");
550 html("</form>\n"); 550 html("</form>\n");
551 } else { 551 } else {
552 if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info)) 552 if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info))
553 html(default_info); 553 html(default_info);
554 } 554 }
555 555
556 html("</td></tr></table></td>\n"); 556 html("</td></tr></table></td>\n");
557 557
558 html("<td id='content'>\n"); 558 html("<td id='content'>\n");
559} 559}
560 560
561void cgit_print_filemode(unsigned short mode) 561void cgit_print_filemode(unsigned short mode)
562{ 562{
563 if (S_ISDIR(mode)) 563 if (S_ISDIR(mode))
564 html("d"); 564 html("d");
565 else if (S_ISLNK(mode)) 565 else if (S_ISLNK(mode))
566 html("l"); 566 html("l");
567 else if (S_ISGITLINK(mode)) 567 else if (S_ISGITLINK(mode))
568 html("m"); 568 html("m");
569 else 569 else
570 html("-"); 570 html("-");
571 html_fileperm(mode >> 6); 571 html_fileperm(mode >> 6);
572 html_fileperm(mode >> 3); 572 html_fileperm(mode >> 3);
573 html_fileperm(mode); 573 html_fileperm(mode);
574} 574}
575 575
576/* vim:set sw=8: */ 576void cgit_print_snapshot_links(const char *repo, const char *head,
577 const char *hex, int snapshots)
578{
579 const struct cgit_snapshot_format* f;
580 char *filename;
581
582 for (f = cgit_snapshot_formats; f->suffix; f++) {
583 if (!(snapshots & f->bit))
584 continue;
585 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
586 f->suffix);
587 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
588 (char *)hex, filename);
589 html("<br/>");
590 }
591}
diff --git a/ui-snapshot.c b/ui-snapshot.c
index c741469..512fcd2 100644
--- a/ui-snapshot.c
+++ b/ui-snapshot.c
@@ -1,159 +1,113 @@
1/* ui-snapshot.c: generate snapshot of a commit 1/* ui-snapshot.c: generate snapshot of a commit
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "html.h" 10#include "html.h"
11 11
12static int write_compressed_tar_archive(struct archiver_args *args,const char *filter) 12static int write_compressed_tar_archive(struct archiver_args *args,const char *filter)
13{ 13{
14 int rw[2]; 14 int rw[2];
15 pid_t gzpid; 15 pid_t gzpid;
16 int stdout2; 16 int stdout2;
17 int status; 17 int status;
18 int rv; 18 int rv;
19 19
20 stdout2 = chk_non_negative(dup(STDIN_FILENO), "Preserving STDOUT before compressing"); 20 stdout2 = chk_non_negative(dup(STDIN_FILENO), "Preserving STDOUT before compressing");
21 chk_zero(pipe(rw), "Opening pipe from compressor subprocess"); 21 chk_zero(pipe(rw), "Opening pipe from compressor subprocess");
22 gzpid = chk_non_negative(fork(), "Forking compressor subprocess"); 22 gzpid = chk_non_negative(fork(), "Forking compressor subprocess");
23 if(gzpid==0) { 23 if(gzpid==0) {
24 /* child */ 24 /* child */
25 chk_zero(close(rw[1]), "Closing write end of pipe in child"); 25 chk_zero(close(rw[1]), "Closing write end of pipe in child");
26 chk_zero(close(STDIN_FILENO), "Closing STDIN"); 26 chk_zero(close(STDIN_FILENO), "Closing STDIN");
27 chk_non_negative(dup2(rw[0],STDIN_FILENO), "Redirecting compressor input to stdin"); 27 chk_non_negative(dup2(rw[0],STDIN_FILENO), "Redirecting compressor input to stdin");
28 execlp(filter,filter,NULL); 28 execlp(filter,filter,NULL);
29 _exit(-1); 29 _exit(-1);
30 } 30 }
31 /* parent */ 31 /* parent */
32 chk_zero(close(rw[0]), "Closing read end of pipe"); 32 chk_zero(close(rw[0]), "Closing read end of pipe");
33 chk_non_negative(dup2(rw[1],STDOUT_FILENO), "Redirecting output to compressor"); 33 chk_non_negative(dup2(rw[1],STDOUT_FILENO), "Redirecting output to compressor");
34 34
35 rv = write_tar_archive(args); 35 rv = write_tar_archive(args);
36 36
37 chk_zero(close(STDOUT_FILENO), "Closing STDOUT redirected to compressor"); 37 chk_zero(close(STDOUT_FILENO), "Closing STDOUT redirected to compressor");
38 chk_non_negative(dup2(stdout2,STDOUT_FILENO), "Restoring uncompressed STDOUT"); 38 chk_non_negative(dup2(stdout2,STDOUT_FILENO), "Restoring uncompressed STDOUT");
39 chk_zero(close(stdout2), "Closing uncompressed STDOUT"); 39 chk_zero(close(stdout2), "Closing uncompressed STDOUT");
40 chk_zero(close(rw[1]), "Closing write end of pipe in parent"); 40 chk_zero(close(rw[1]), "Closing write end of pipe in parent");
41 chk_positive(waitpid(gzpid,&status,0), "Waiting on compressor process"); 41 chk_positive(waitpid(gzpid,&status,0), "Waiting on compressor process");
42 if(! ( WIFEXITED(status) && WEXITSTATUS(status)==0 ) ) 42 if(! ( WIFEXITED(status) && WEXITSTATUS(status)==0 ) )
43 cgit_print_error("Failed to compress archive"); 43 cgit_print_error("Failed to compress archive");
44 44
45 return rv; 45 return rv;
46} 46}
47 47
48static int write_tar_gzip_archive(struct archiver_args *args) 48static int write_tar_gzip_archive(struct archiver_args *args)
49{ 49{
50 return write_compressed_tar_archive(args,"gzip"); 50 return write_compressed_tar_archive(args,"gzip");
51} 51}
52 52
53static int write_tar_bzip2_archive(struct archiver_args *args) 53static int write_tar_bzip2_archive(struct archiver_args *args)
54{ 54{
55 return write_compressed_tar_archive(args,"bzip2"); 55 return write_compressed_tar_archive(args,"bzip2");
56} 56}
57 57
58static const struct snapshot_archive_t { 58const struct cgit_snapshot_format cgit_snapshot_formats[] = {
59 const char *suffix;
60 const char *mimetype;
61 write_archive_fn_t write_func;
62 int bit;
63 }snapshot_archives[] = {
64 { ".zip", "application/x-zip", write_zip_archive, 0x1 }, 59 { ".zip", "application/x-zip", write_zip_archive, 0x1 },
65 { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, 60 { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 },
66 { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, 61 { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 },
67 { ".tar", "application/x-tar", write_tar_archive, 0x8 } 62 { ".tar", "application/x-tar", write_tar_archive, 0x8 },
63 {}
68}; 64};
69 65
70#define snapshot_archives_len (sizeof(snapshot_archives) / sizeof(*snapshot_archives)) 66static int make_snapshot(const struct cgit_snapshot_format *format,
71 67 const char *hex, const char *prefix,
72void cgit_print_snapshot(const char *head, const char *hex, const char *prefix, 68 const char *filename)
73 const char *filename, int snapshots)
74{ 69{
75 const struct snapshot_archive_t* sat;
76 struct archiver_args args; 70 struct archiver_args args;
77 struct commit *commit; 71 struct commit *commit;
78 unsigned char sha1[20]; 72 unsigned char sha1[20];
79 int f, sl, fnl = strlen(filename);
80 73
81 for(f=0; f<snapshot_archives_len; f++) { 74 if(get_sha1(hex, sha1)) {
82 sat = &snapshot_archives[f]; 75 cgit_print_error(fmt("Bad object id: %s", hex));
83 if(!(snapshots & sat->bit)) 76 return 1;
84 continue;
85 sl = strlen(sat->suffix);
86 if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix))
87 continue;
88 if (!hex)
89 hex = head;
90 if(get_sha1(hex, sha1)) {
91 cgit_print_error(fmt("Bad object id: %s", hex));
92 return;
93 }
94 commit = lookup_commit_reference(sha1);
95 if(!commit) {
96 cgit_print_error(fmt("Not a commit reference: %s", hex));
97 return;;
98 }
99 memset(&args,0,sizeof(args));
100 args.base = fmt("%s/", prefix);
101 args.tree = commit->tree;
102 args.time = commit->date;
103 ctx.page.mimetype = xstrdup(sat->mimetype);
104 ctx.page.filename = xstrdup(filename);
105 cgit_print_http_headers(&ctx);
106 (*sat->write_func)(&args);
107 return;
108 } 77 }
109 cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); 78 commit = lookup_commit_reference(sha1);
110} 79 if(!commit) {
111 80 cgit_print_error(fmt("Not a commit reference: %s", hex));
112void cgit_print_snapshot_links(const char *repo, const char *head, 81 return 1;
113 const char *hex, int snapshots)
114{
115 const struct snapshot_archive_t* sat;
116 char *filename;
117 int f;
118
119 for(f=0; f<snapshot_archives_len; f++) {
120 sat = &snapshot_archives[f];
121 if(!(snapshots & sat->bit))
122 continue;
123 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
124 sat->suffix);
125 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
126 (char *)hex, filename);
127 html("<br/>");
128 } 82 }
83 memset(&args, 0, sizeof(args));
84 args.base = fmt("%s/", prefix);
85 args.tree = commit->tree;
86 args.time = commit->date;
87 ctx.page.mimetype = xstrdup(format->mimetype);
88 ctx.page.filename = xstrdup(filename);
89 cgit_print_http_headers(&ctx);
90 format->write_func(&args);
91 return 0;
129} 92}
130 93
131int cgit_parse_snapshots_mask(const char *str) 94void cgit_print_snapshot(const char *head, const char *hex, const char *prefix,
95 const char *filename, int snapshots)
132{ 96{
133 const struct snapshot_archive_t* sat; 97 const struct cgit_snapshot_format* f;
134 static const char *delim = " \t,:/|;"; 98 int sl, fnl;
135 int f, tl, sl, rv = 0; 99
136 100 fnl = strlen(filename);
137 /* favor legacy setting */ 101 if (!hex)
138 if(atoi(str)) 102 hex = head;
139 return 1; 103 for (f = cgit_snapshot_formats; f->suffix; f++) {
140 for(;;) { 104 if (!(snapshots & f->bit))
141 str += strspn(str,delim); 105 continue;
142 tl = strcspn(str,delim); 106 sl = strlen(f->suffix);
143 if(!tl) 107 if(fnl < sl || strcmp(&filename[fnl-sl], f->suffix))
144 break; 108 continue;
145 for(f=0; f<snapshot_archives_len; f++) { 109 make_snapshot(f, hex, prefix, filename);
146 sat = &snapshot_archives[f]; 110 return;
147 sl = strlen(sat->suffix);
148 if((tl == sl && !strncmp(sat->suffix, str, tl)) ||
149 (tl == sl-1 && !strncmp(sat->suffix+1, str, tl-1))) {
150 rv |= sat->bit;
151 break;
152 }
153 }
154 str += tl;
155 } 111 }
156 return rv; 112 cgit_print_error(fmt("Unsupported snapshot format: %s", filename));
157} 113}
158
159/* vim:set sw=8: */