summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (show whitespace changes)
-rw-r--r--cgit.c50
1 files changed, 33 insertions, 17 deletions
diff --git a/cgit.c b/cgit.c
index e6b4526..d0f6905 100644
--- a/cgit.c
+++ b/cgit.c
@@ -7,13 +7,16 @@
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cmd.h"
10 11
11static int cgit_prepare_cache(struct cacheitem *item) 12static int cgit_prepare_cache(struct cacheitem *item)
12{ 13{
13 if (!ctx.repo && ctx.qry.repo) { 14 if (!ctx.repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); 15 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
15 cgit_print_docstart(title, item); 16 "Bad request");
16 cgit_print_pageheader(title, 0); 17 cgit_print_http_headers(&ctx);
18 cgit_print_docstart(&ctx);
19 cgit_print_pageheader(&ctx);
17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); 20 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 21 cgit_print_docend();
19 return 0; 22 return 0;
@@ -80,7 +83,7 @@ char *find_default_branch(struct cgit_repo *repo)
80 83
81static void cgit_print_repo_page(struct cacheitem *item) 84static void cgit_print_repo_page(struct cacheitem *item)
82{ 85{
83 char *title, *tmp; 86 char *tmp;
84 int show_search; 87 int show_search;
85 unsigned char sha1[20]; 88 unsigned char sha1[20];
86 int nongit = 0; 89 int nongit = 0;
@@ -88,17 +91,19 @@ static void cgit_print_repo_page(struct cacheitem *item)
88 setenv("GIT_DIR", ctx.repo->path, 1); 91 setenv("GIT_DIR", ctx.repo->path, 1);
89 setup_git_directory_gently(&nongit); 92 setup_git_directory_gently(&nongit);
90 if (nongit) { 93 if (nongit) {
91 title = fmt("%s - %s", ctx.cfg.root_title, "config error"); 94 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
95 "config error");
92 tmp = fmt("Not a git repository: '%s'", ctx.repo->path); 96 tmp = fmt("Not a git repository: '%s'", ctx.repo->path);
93 ctx.repo = NULL; 97 ctx.repo = NULL;
94 cgit_print_docstart(title, item); 98 cgit_print_http_headers(&ctx);
95 cgit_print_pageheader(title, 0); 99 cgit_print_docstart(&ctx);
100 cgit_print_pageheader(&ctx);
96 cgit_print_error(tmp); 101 cgit_print_error(tmp);
97 cgit_print_docend(); 102 cgit_print_docend();
98 return; 103 return;
99 } 104 }
100 105
101 title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); 106 ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc);
102 show_search = 0; 107 show_search = 0;
103 108
104 if (!ctx.qry.head) { 109 if (!ctx.qry.head) {
@@ -107,8 +112,9 @@ static void cgit_print_repo_page(struct cacheitem *item)
107 } 112 }
108 113
109 if (!ctx.qry.head) { 114 if (!ctx.qry.head) {
110 cgit_print_docstart(title, item); 115 cgit_print_http_headers(&ctx);
111 cgit_print_pageheader(title, 0); 116 cgit_print_docstart(&ctx);
117 cgit_print_pageheader(&ctx);
112 cgit_print_error("Repository seems to be empty"); 118 cgit_print_error("Repository seems to be empty");
113 cgit_print_docend(); 119 cgit_print_docend();
114 return; 120 return;
@@ -117,8 +123,9 @@ static void cgit_print_repo_page(struct cacheitem *item)
117 if (get_sha1(ctx.qry.head, sha1)) { 123 if (get_sha1(ctx.qry.head, sha1)) {
118 tmp = xstrdup(ctx.qry.head); 124 tmp = xstrdup(ctx.qry.head);
119 ctx.qry.head = ctx.repo->defbranch; 125 ctx.qry.head = ctx.repo->defbranch;
120 cgit_print_docstart(title, item); 126 cgit_print_http_headers(&ctx);
121 cgit_print_pageheader(title, 0); 127 cgit_print_docstart(&ctx);
128 cgit_print_pageheader(&ctx);
122 cgit_print_error(fmt("Invalid branch: %s", tmp)); 129 cgit_print_error(fmt("Invalid branch: %s", tmp));
123 cgit_print_docend(); 130 cgit_print_docend();
124 return; 131 return;
@@ -143,15 +150,16 @@ static void cgit_print_repo_page(struct cacheitem *item)
143 } 150 }
144 151
145 show_search = (cgit_cmd == CMD_LOG); 152 show_search = (cgit_cmd == CMD_LOG);
146 cgit_print_docstart(title, item); 153 cgit_print_http_headers(&ctx);
154 cgit_print_docstart(&ctx);
147 if (!cgit_cmd) { 155 if (!cgit_cmd) {
148 cgit_print_pageheader("summary", show_search); 156 cgit_print_pageheader(&ctx);
149 cgit_print_summary(); 157 cgit_print_summary();
150 cgit_print_docend(); 158 cgit_print_docend();
151 return; 159 return;
152 } 160 }
153 161
154 cgit_print_pageheader(ctx.qry.page, show_search); 162 cgit_print_pageheader(&ctx);
155 163
156 switch(cgit_cmd) { 164 switch(cgit_cmd) {
157 case CMD_LOG: 165 case CMD_LOG:
@@ -180,12 +188,18 @@ static void cgit_print_repo_page(struct cacheitem *item)
180 cgit_print_docend(); 188 cgit_print_docend();
181} 189}
182 190
191static long ttl_seconds(long ttl)
192{
193 if (ttl<0)
194 return 60 * 60 * 24 * 365;
195 else
196 return ttl * 60;
197}
198
183static void cgit_fill_cache(struct cacheitem *item, int use_cache) 199static void cgit_fill_cache(struct cacheitem *item, int use_cache)
184{ 200{
185 int stdout2; 201 int stdout2;
186 202
187 item->st.st_mtime = time(NULL);
188
189 if (use_cache) { 203 if (use_cache) {
190 stdout2 = chk_positive(dup(STDOUT_FILENO), 204 stdout2 = chk_positive(dup(STDOUT_FILENO),
191 "Preserving STDOUT"); 205 "Preserving STDOUT");
@@ -193,6 +207,8 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
193 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 207 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
194 } 208 }
195 209
210 ctx.page.modified = time(NULL);
211 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
196 if (ctx.repo) 212 if (ctx.repo)
197 cgit_print_repo_page(item); 213 cgit_print_repo_page(item);
198 else 214 else