summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--ui-repolist.c2
-rw-r--r--ui-shared.h1
2 files changed, 2 insertions, 1 deletions
diff --git a/ui-repolist.c b/ui-repolist.c
index 3ef2e99..0a0b6ca 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -1,289 +1,289 @@
1/* ui-repolist.c: functions for generating the repolist page 1/* ui-repolist.c: functions for generating the repolist page
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/* This is needed for strcasestr to be defined by <string.h> */ 9/* This is needed for strcasestr to be defined by <string.h> */
10#define _GNU_SOURCE 1 10#define _GNU_SOURCE 1
11#include <string.h> 11#include <string.h>
12 12
13#include <time.h> 13#include <time.h>
14 14
15#include "cgit.h" 15#include "cgit.h"
16#include "html.h" 16#include "html.h"
17#include "ui-shared.h" 17#include "ui-shared.h"
18 18
19time_t read_agefile(char *path) 19time_t read_agefile(char *path)
20{ 20{
21 time_t result; 21 time_t result;
22 size_t size; 22 size_t size;
23 char *buf; 23 char *buf;
24 static char buf2[64]; 24 static char buf2[64];
25 25
26 if (readfile(path, &buf, &size)) 26 if (readfile(path, &buf, &size))
27 return -1; 27 return -1;
28 28
29 if (parse_date(buf, buf2, sizeof(buf2))) 29 if (parse_date(buf, buf2, sizeof(buf2)))
30 result = strtoul(buf2, NULL, 10); 30 result = strtoul(buf2, NULL, 10);
31 else 31 else
32 result = 0; 32 result = 0;
33 free(buf); 33 free(buf);
34 return result; 34 return result;
35} 35}
36 36
37static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) 37static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
38{ 38{
39 char *path; 39 char *path;
40 struct stat s; 40 struct stat s;
41 struct cgit_repo *r = (struct cgit_repo *)repo; 41 struct cgit_repo *r = (struct cgit_repo *)repo;
42 42
43 if (repo->mtime != -1) { 43 if (repo->mtime != -1) {
44 *mtime = repo->mtime; 44 *mtime = repo->mtime;
45 return 1; 45 return 1;
46 } 46 }
47 path = fmt("%s/%s", repo->path, ctx.cfg.agefile); 47 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
48 if (stat(path, &s) == 0) { 48 if (stat(path, &s) == 0) {
49 *mtime = read_agefile(path); 49 *mtime = read_agefile(path);
50 r->mtime = *mtime; 50 r->mtime = *mtime;
51 return 1; 51 return 1;
52 } 52 }
53 53
54 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); 54 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
55 if (stat(path, &s) == 0) 55 if (stat(path, &s) == 0)
56 *mtime = s.st_mtime; 56 *mtime = s.st_mtime;
57 else 57 else
58 *mtime = 0; 58 *mtime = 0;
59 59
60 r->mtime = *mtime; 60 r->mtime = *mtime;
61 return (r->mtime != 0); 61 return (r->mtime != 0);
62} 62}
63 63
64static void print_modtime(struct cgit_repo *repo) 64static void print_modtime(struct cgit_repo *repo)
65{ 65{
66 time_t t; 66 time_t t;
67 if (get_repo_modtime(repo, &t)) 67 if (get_repo_modtime(repo, &t))
68 cgit_print_age(t, -1, NULL); 68 cgit_print_age(t, -1, NULL);
69} 69}
70 70
71int is_match(struct cgit_repo *repo) 71int is_match(struct cgit_repo *repo)
72{ 72{
73 if (!ctx.qry.search) 73 if (!ctx.qry.search)
74 return 1; 74 return 1;
75 if (repo->url && strcasestr(repo->url, ctx.qry.search)) 75 if (repo->url && strcasestr(repo->url, ctx.qry.search))
76 return 1; 76 return 1;
77 if (repo->name && strcasestr(repo->name, ctx.qry.search)) 77 if (repo->name && strcasestr(repo->name, ctx.qry.search))
78 return 1; 78 return 1;
79 if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) 79 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
80 return 1; 80 return 1;
81 if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) 81 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
82 return 1; 82 return 1;
83 return 0; 83 return 0;
84} 84}
85 85
86int is_in_url(struct cgit_repo *repo) 86int is_in_url(struct cgit_repo *repo)
87{ 87{
88 if (!ctx.qry.url) 88 if (!ctx.qry.url)
89 return 1; 89 return 1;
90 if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) 90 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
91 return 1; 91 return 1;
92 return 0; 92 return 0;
93} 93}
94 94
95void print_sort_header(const char *title, const char *sort) 95void print_sort_header(const char *title, const char *sort)
96{ 96{
97 htmlf("<th class='left'><a href='./?s=%s", sort); 97 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
98 if (ctx.qry.search) { 98 if (ctx.qry.search) {
99 html("&q="); 99 html("&q=");
100 html_url_arg(ctx.qry.search); 100 html_url_arg(ctx.qry.search);
101 } 101 }
102 htmlf("'>%s</a></th>", title); 102 htmlf("'>%s</a></th>", title);
103} 103}
104 104
105void print_header(int columns) 105void print_header(int columns)
106{ 106{
107 html("<tr class='nohover'>"); 107 html("<tr class='nohover'>");
108 print_sort_header("Name", "name"); 108 print_sort_header("Name", "name");
109 print_sort_header("Description", "desc"); 109 print_sort_header("Description", "desc");
110 print_sort_header("Owner", "owner"); 110 print_sort_header("Owner", "owner");
111 print_sort_header("Idle", "idle"); 111 print_sort_header("Idle", "idle");
112 if (ctx.cfg.enable_index_links) 112 if (ctx.cfg.enable_index_links)
113 html("<th class='left'>Links</th>"); 113 html("<th class='left'>Links</th>");
114 html("</tr>\n"); 114 html("</tr>\n");
115} 115}
116 116
117 117
118void print_pager(int items, int pagelen, char *search) 118void print_pager(int items, int pagelen, char *search)
119{ 119{
120 int i; 120 int i;
121 html("<div class='pager'>"); 121 html("<div class='pager'>");
122 for(i = 0; i * pagelen < items; i++) 122 for(i = 0; i * pagelen < items; i++)
123 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, 123 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
124 search, i * pagelen); 124 search, i * pagelen);
125 html("</div>"); 125 html("</div>");
126} 126}
127 127
128static int cmp(const char *s1, const char *s2) 128static int cmp(const char *s1, const char *s2)
129{ 129{
130 if (s1 && s2) 130 if (s1 && s2)
131 return strcmp(s1, s2); 131 return strcmp(s1, s2);
132 if (s1 && !s2) 132 if (s1 && !s2)
133 return -1; 133 return -1;
134 if (s2 && !s1) 134 if (s2 && !s1)
135 return 1; 135 return 1;
136 return 0; 136 return 0;
137} 137}
138 138
139static int sort_section(const void *a, const void *b) 139static int sort_section(const void *a, const void *b)
140{ 140{
141 const struct cgit_repo *r1 = a; 141 const struct cgit_repo *r1 = a;
142 const struct cgit_repo *r2 = b; 142 const struct cgit_repo *r2 = b;
143 int result; 143 int result;
144 144
145 result = cmp(r1->section, r2->section); 145 result = cmp(r1->section, r2->section);
146 if (!result) 146 if (!result)
147 result = cmp(r1->name, r2->name); 147 result = cmp(r1->name, r2->name);
148 return result; 148 return result;
149} 149}
150 150
151static int sort_name(const void *a, const void *b) 151static int sort_name(const void *a, const void *b)
152{ 152{
153 const struct cgit_repo *r1 = a; 153 const struct cgit_repo *r1 = a;
154 const struct cgit_repo *r2 = b; 154 const struct cgit_repo *r2 = b;
155 155
156 return cmp(r1->name, r2->name); 156 return cmp(r1->name, r2->name);
157} 157}
158 158
159static int sort_desc(const void *a, const void *b) 159static int sort_desc(const void *a, const void *b)
160{ 160{
161 const struct cgit_repo *r1 = a; 161 const struct cgit_repo *r1 = a;
162 const struct cgit_repo *r2 = b; 162 const struct cgit_repo *r2 = b;
163 163
164 return cmp(r1->desc, r2->desc); 164 return cmp(r1->desc, r2->desc);
165} 165}
166 166
167static int sort_owner(const void *a, const void *b) 167static int sort_owner(const void *a, const void *b)
168{ 168{
169 const struct cgit_repo *r1 = a; 169 const struct cgit_repo *r1 = a;
170 const struct cgit_repo *r2 = b; 170 const struct cgit_repo *r2 = b;
171 171
172 return cmp(r1->owner, r2->owner); 172 return cmp(r1->owner, r2->owner);
173} 173}
174 174
175static int sort_idle(const void *a, const void *b) 175static int sort_idle(const void *a, const void *b)
176{ 176{
177 const struct cgit_repo *r1 = a; 177 const struct cgit_repo *r1 = a;
178 const struct cgit_repo *r2 = b; 178 const struct cgit_repo *r2 = b;
179 time_t t1, t2; 179 time_t t1, t2;
180 180
181 t1 = t2 = 0; 181 t1 = t2 = 0;
182 get_repo_modtime(r1, &t1); 182 get_repo_modtime(r1, &t1);
183 get_repo_modtime(r2, &t2); 183 get_repo_modtime(r2, &t2);
184 return t2 - t1; 184 return t2 - t1;
185} 185}
186 186
187struct sortcolumn { 187struct sortcolumn {
188 const char *name; 188 const char *name;
189 int (*fn)(const void *a, const void *b); 189 int (*fn)(const void *a, const void *b);
190}; 190};
191 191
192struct sortcolumn sortcolumn[] = { 192struct sortcolumn sortcolumn[] = {
193 {"section", sort_section}, 193 {"section", sort_section},
194 {"name", sort_name}, 194 {"name", sort_name},
195 {"desc", sort_desc}, 195 {"desc", sort_desc},
196 {"owner", sort_owner}, 196 {"owner", sort_owner},
197 {"idle", sort_idle}, 197 {"idle", sort_idle},
198 {NULL, NULL} 198 {NULL, NULL}
199}; 199};
200 200
201int sort_repolist(char *field) 201int sort_repolist(char *field)
202{ 202{
203 struct sortcolumn *column; 203 struct sortcolumn *column;
204 204
205 for (column = &sortcolumn[0]; column->name; column++) { 205 for (column = &sortcolumn[0]; column->name; column++) {
206 if (strcmp(field, column->name)) 206 if (strcmp(field, column->name))
207 continue; 207 continue;
208 qsort(cgit_repolist.repos, cgit_repolist.count, 208 qsort(cgit_repolist.repos, cgit_repolist.count,
209 sizeof(struct cgit_repo), column->fn); 209 sizeof(struct cgit_repo), column->fn);
210 return 1; 210 return 1;
211 } 211 }
212 return 0; 212 return 0;
213} 213}
214 214
215 215
216void cgit_print_repolist() 216void cgit_print_repolist()
217{ 217{
218 int i, columns = 4, hits = 0, header = 0; 218 int i, columns = 4, hits = 0, header = 0;
219 char *last_section = NULL; 219 char *last_section = NULL;
220 char *section; 220 char *section;
221 int sorted = 0; 221 int sorted = 0;
222 222
223 if (ctx.cfg.enable_index_links) 223 if (ctx.cfg.enable_index_links)
224 columns++; 224 columns++;
225 225
226 ctx.page.title = ctx.cfg.root_title; 226 ctx.page.title = ctx.cfg.root_title;
227 cgit_print_http_headers(&ctx); 227 cgit_print_http_headers(&ctx);
228 cgit_print_docstart(&ctx); 228 cgit_print_docstart(&ctx);
229 cgit_print_pageheader(&ctx); 229 cgit_print_pageheader(&ctx);
230 230
231 if (ctx.cfg.index_header) 231 if (ctx.cfg.index_header)
232 html_include(ctx.cfg.index_header); 232 html_include(ctx.cfg.index_header);
233 233
234 if(ctx.qry.sort) 234 if(ctx.qry.sort)
235 sorted = sort_repolist(ctx.qry.sort); 235 sorted = sort_repolist(ctx.qry.sort);
236 else 236 else
237 sort_repolist("section"); 237 sort_repolist("section");
238 238
239 html("<table summary='repository list' class='list nowrap'>"); 239 html("<table summary='repository list' class='list nowrap'>");
240 for (i=0; i<cgit_repolist.count; i++) { 240 for (i=0; i<cgit_repolist.count; i++) {
241 ctx.repo = &cgit_repolist.repos[i]; 241 ctx.repo = &cgit_repolist.repos[i];
242 if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) 242 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
243 continue; 243 continue;
244 hits++; 244 hits++;
245 if (hits <= ctx.qry.ofs) 245 if (hits <= ctx.qry.ofs)
246 continue; 246 continue;
247 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) 247 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
248 continue; 248 continue;
249 if (!header++) 249 if (!header++)
250 print_header(columns); 250 print_header(columns);
251 section = ctx.repo->section; 251 section = ctx.repo->section;
252 if (section && !strcmp(section, "")) 252 if (section && !strcmp(section, ""))
253 section = NULL; 253 section = NULL;
254 if (!sorted && 254 if (!sorted &&
255 ((last_section == NULL && section != NULL) || 255 ((last_section == NULL && section != NULL) ||
256 (last_section != NULL && section == NULL) || 256 (last_section != NULL && section == NULL) ||
257 (last_section != NULL && section != NULL && 257 (last_section != NULL && section != NULL &&
258 strcmp(section, last_section)))) { 258 strcmp(section, last_section)))) {
259 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>", 259 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
260 columns); 260 columns);
261 html_txt(section); 261 html_txt(section);
262 html("</td></tr>"); 262 html("</td></tr>");
263 last_section = section; 263 last_section = section;
264 } 264 }
265 htmlf("<tr><td class='%s'>", 265 htmlf("<tr><td class='%s'>",
266 !sorted && section ? "sublevel-repo" : "toplevel-repo"); 266 !sorted && section ? "sublevel-repo" : "toplevel-repo");
267 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); 267 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
268 html("</td><td>"); 268 html("</td><td>");
269 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); 269 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
270 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); 270 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
271 html_link_close(); 271 html_link_close();
272 html("</td><td>"); 272 html("</td><td>");
273 html_txt(ctx.repo->owner); 273 html_txt(ctx.repo->owner);
274 html("</td><td>"); 274 html("</td><td>");
275 print_modtime(ctx.repo); 275 print_modtime(ctx.repo);
276 html("</td>"); 276 html("</td>");
277 if (ctx.cfg.enable_index_links) { 277 if (ctx.cfg.enable_index_links) {
278 html("<td>"); 278 html("<td>");
279 cgit_summary_link("summary", NULL, "button", NULL); 279 cgit_summary_link("summary", NULL, "button", NULL);
280 cgit_log_link("log", NULL, "button", NULL, NULL, NULL, 280 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
281 0, NULL, NULL, ctx.qry.showmsg); 281 0, NULL, NULL, ctx.qry.showmsg);
282 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); 282 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
283 html("</td>"); 283 html("</td>");
284 } 284 }
285 html("</tr>\n"); 285 html("</tr>\n");
286 } 286 }
287 html("</table>"); 287 html("</table>");
288 if (!hits) 288 if (!hits)
289 cgit_print_error("No repositories found"); 289 cgit_print_error("No repositories found");
diff --git a/ui-shared.h b/ui-shared.h
index bff4826..b12aa89 100644
--- a/ui-shared.h
+++ b/ui-shared.h
@@ -1,50 +1,51 @@
1#ifndef UI_SHARED_H 1#ifndef UI_SHARED_H
2#define UI_SHARED_H 2#define UI_SHARED_H
3 3
4extern char *cgit_httpscheme(); 4extern char *cgit_httpscheme();
5extern char *cgit_hosturl(); 5extern char *cgit_hosturl();
6extern char *cgit_rooturl();
6extern char *cgit_repourl(const char *reponame); 7extern char *cgit_repourl(const char *reponame);
7extern char *cgit_fileurl(const char *reponame, const char *pagename, 8extern char *cgit_fileurl(const char *reponame, const char *pagename,
8 const char *filename, const char *query); 9 const char *filename, const char *query);
9extern char *cgit_pageurl(const char *reponame, const char *pagename, 10extern char *cgit_pageurl(const char *reponame, const char *pagename,
10 const char *query); 11 const char *query);
11 12
12extern void cgit_index_link(char *name, char *title, char *class, 13extern void cgit_index_link(char *name, char *title, char *class,
13 char *pattern, int ofs); 14 char *pattern, int ofs);
14extern void cgit_summary_link(char *name, char *title, char *class, char *head); 15extern void cgit_summary_link(char *name, char *title, char *class, char *head);
15extern void cgit_tag_link(char *name, char *title, char *class, char *head, 16extern void cgit_tag_link(char *name, char *title, char *class, char *head,
16 char *rev); 17 char *rev);
17extern void cgit_tree_link(char *name, char *title, char *class, char *head, 18extern void cgit_tree_link(char *name, char *title, char *class, char *head,
18 char *rev, char *path); 19 char *rev, char *path);
19extern void cgit_plain_link(char *name, char *title, char *class, char *head, 20extern void cgit_plain_link(char *name, char *title, char *class, char *head,
20 char *rev, char *path); 21 char *rev, char *path);
21extern void cgit_log_link(char *name, char *title, char *class, char *head, 22extern void cgit_log_link(char *name, char *title, char *class, char *head,
22 char *rev, char *path, int ofs, char *grep, 23 char *rev, char *path, int ofs, char *grep,
23 char *pattern, int showmsg); 24 char *pattern, int showmsg);
24extern void cgit_commit_link(char *name, char *title, char *class, char *head, 25extern void cgit_commit_link(char *name, char *title, char *class, char *head,
25 char *rev); 26 char *rev);
26extern void cgit_patch_link(char *name, char *title, char *class, char *head, 27extern void cgit_patch_link(char *name, char *title, char *class, char *head,
27 char *rev); 28 char *rev);
28extern void cgit_refs_link(char *name, char *title, char *class, char *head, 29extern void cgit_refs_link(char *name, char *title, char *class, char *head,
29 char *rev, char *path); 30 char *rev, char *path);
30extern void cgit_snapshot_link(char *name, char *title, char *class, 31extern void cgit_snapshot_link(char *name, char *title, char *class,
31 char *head, char *rev, char *archivename); 32 char *head, char *rev, char *archivename);
32extern void cgit_diff_link(char *name, char *title, char *class, char *head, 33extern void cgit_diff_link(char *name, char *title, char *class, char *head,
33 char *new_rev, char *old_rev, char *path); 34 char *new_rev, char *old_rev, char *path);
34extern void cgit_stats_link(char *name, char *title, char *class, char *head, 35extern void cgit_stats_link(char *name, char *title, char *class, char *head,
35 char *path); 36 char *path);
36extern void cgit_object_link(struct object *obj); 37extern void cgit_object_link(struct object *obj);
37 38
38extern void cgit_print_error(char *msg); 39extern void cgit_print_error(char *msg);
39extern void cgit_print_date(time_t secs, char *format, int local_time); 40extern void cgit_print_date(time_t secs, char *format, int local_time);
40extern void cgit_print_age(time_t t, time_t max_relative, char *format); 41extern void cgit_print_age(time_t t, time_t max_relative, char *format);
41extern void cgit_print_http_headers(struct cgit_context *ctx); 42extern void cgit_print_http_headers(struct cgit_context *ctx);
42extern void cgit_print_docstart(struct cgit_context *ctx); 43extern void cgit_print_docstart(struct cgit_context *ctx);
43extern void cgit_print_docend(); 44extern void cgit_print_docend();
44extern void cgit_print_pageheader(struct cgit_context *ctx); 45extern void cgit_print_pageheader(struct cgit_context *ctx);
45extern void cgit_print_filemode(unsigned short mode); 46extern void cgit_print_filemode(unsigned short mode);
46extern void cgit_print_snapshot_links(const char *repo, const char *head, 47extern void cgit_print_snapshot_links(const char *repo, const char *head,
47 const char *hex, int snapshots); 48 const char *hex, int snapshots);
48extern void cgit_add_hidden_formfields(int incl_head, int incl_search, 49extern void cgit_add_hidden_formfields(int incl_head, int incl_search,
49 char *page); 50 char *page);
50#endif /* UI_SHARED_H */ 51#endif /* UI_SHARED_H */