blob: 312a7ee2c7a59df7a9bbb85daf4dd993e274432f [file] [log] [blame]
Lars Hjemli74620f12006-12-11 16:48:03 +01001/* ui-repolist.c: functions for generating the repolist page
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli57f6a8b2007-05-22 23:25:25 +02009#include <time.h>
10
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "cgit.h"
12#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010013#include "ui-shared.h"
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020014
15time_t read_agefile(char *path)
16{
17 FILE *f;
18 static char buf[64], buf2[64];
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020019
20 if (!(f = fopen(path, "r")))
21 return -1;
Lars Hjemlic7d14022008-11-14 09:29:22 +010022 if (fgets(buf, sizeof(buf), f) == NULL)
23 return -1;
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020024 fclose(f);
25 if (parse_date(buf, buf2, sizeof(buf2)))
26 return strtoul(buf2, NULL, 10);
27 else
28 return 0;
29}
30
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010031static void print_modtime(struct cgit_repo *repo)
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020032{
33 char *path;
34 struct stat s;
35
Lars Hjemlib228d4f2008-02-16 13:07:13 +010036 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
Lars Hjemli57f6a8b2007-05-22 23:25:25 +020037 if (stat(path, &s) == 0) {
38 cgit_print_age(read_agefile(path), -1, NULL);
39 return;
40 }
41
42 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
43 if (stat(path, &s) != 0)
44 return;
45 cgit_print_age(s.st_mtime, -1, NULL);
46}
Lars Hjemli74620f12006-12-11 16:48:03 +010047
Lars Hjemli536b0542008-04-13 11:57:10 +020048int is_match(struct cgit_repo *repo)
Lars Hjemli74620f12006-12-11 16:48:03 +010049{
Lars Hjemli536b0542008-04-13 11:57:10 +020050 if (!ctx.qry.search)
51 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020052 if (repo->url && strcasestr(repo->url, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020053 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020054 if (repo->name && strcasestr(repo->name, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020055 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020056 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020057 return 1;
Lars Hjemli28d781f2008-04-13 12:42:27 +020058 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
Lars Hjemli536b0542008-04-13 11:57:10 +020059 return 1;
60 return 0;
61}
Lars Hjemli74620f12006-12-11 16:48:03 +010062
Lars Hjemlidd80ef52008-09-14 20:18:10 +020063int is_in_url(struct cgit_repo *repo)
64{
65 if (!ctx.qry.url)
66 return 1;
67 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
68 return 1;
69 return 0;
70}
71
Lars Hjemli536b0542008-04-13 11:57:10 +020072void print_header(int columns)
73{
Lars Hjemli777faf72007-01-28 00:39:26 +010074 html("<tr class='nohover'>"
Lars Hjemlic43f1242006-12-22 01:44:32 +010075 "<th class='left'>Name</th>"
76 "<th class='left'>Description</th>"
Lars Hjemli8f6f8c32007-05-16 02:12:06 +020077 "<th class='left'>Owner</th>"
Benjamin Closed71c0c72008-11-25 06:25:35 -080078 "<th class='left'><a href=\"?s=1\">Idle</a></th>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +010079 if (ctx.cfg.enable_index_links)
Lars Hjemli931fc6d2008-04-13 10:57:11 +020080 html("<th class='left'>Links</th>");
Lars Hjemli0d05bca2007-06-19 00:56:40 +020081 html("</tr>\n");
Lars Hjemli536b0542008-04-13 11:57:10 +020082}
Lars Hjemli74620f12006-12-11 16:48:03 +010083
Lars Hjemlic6078b82008-05-03 10:54:39 +020084
85void print_pager(int items, int pagelen, char *search)
86{
87 int i;
88 html("<div class='pager'>");
89 for(i = 0; i * pagelen < items; i++)
90 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
91 search, i * pagelen);
92 html("</div>");
93}
94
Benjamin Closed71c0c72008-11-25 06:25:35 -080095static int cgit_reposort_modtime(const void *a, const void *b)
96{
97 const struct cgit_repo *r1 = a;
98 const struct cgit_repo *r2 = b;
99 char *path;
100 struct stat s;
101 time_t t1, t2;
102 path = fmt("%s/%s", r1->path, ctx.cfg.agefile);
103 if (stat(path, &s) == 0) {
104 t1 = read_agefile(path);
105 } else {
106 path = fmt("%s/refs/heads/%s", r1->path, r1->defbranch);
107 if (stat(path, &s) != 0)
108 return 0;
109 t1 =s.st_mtime;
110 }
111
112 path = fmt("%s/%s", r2->path, ctx.cfg.agefile);
113 if (stat(path, &s) == 0) {
114 t2 = read_agefile(path);
115 } else {
116 path = fmt("%s/refs/heads/%s", r2->path, r2->defbranch);
117 if (stat(path, &s) != 0)
118 return 0;
119 t2 =s.st_mtime;
120 }
121 return t2-t1;
122}
123
Lars Hjemli536b0542008-04-13 11:57:10 +0200124void cgit_print_repolist()
125{
126 int i, columns = 4, hits = 0, header = 0;
127 char *last_group = NULL;
128
129 if (ctx.cfg.enable_index_links)
130 columns++;
131
132 ctx.page.title = ctx.cfg.root_title;
133 cgit_print_http_headers(&ctx);
134 cgit_print_docstart(&ctx);
135 cgit_print_pageheader(&ctx);
136
Lars Hjemli80628172008-04-29 00:35:49 +0200137 if (ctx.cfg.index_header)
138 html_include(ctx.cfg.index_header);
139
Benjamin Closed71c0c72008-11-25 06:25:35 -0800140 if(ctx.qry.sort)
141 qsort(cgit_repolist.repos,cgit_repolist.count,sizeof(struct cgit_repo),cgit_reposort_modtime);
142
Lars Hjemli536b0542008-04-13 11:57:10 +0200143 html("<table summary='repository list' class='list nowrap'>");
Lars Hjemlice1c7332007-02-03 15:02:55 +0100144 for (i=0; i<cgit_repolist.count; i++) {
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100145 ctx.repo = &cgit_repolist.repos[i];
Lars Hjemlidd80ef52008-09-14 20:18:10 +0200146 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
Lars Hjemli536b0542008-04-13 11:57:10 +0200147 continue;
Lars Hjemlic6078b82008-05-03 10:54:39 +0200148 hits++;
149 if (hits <= ctx.qry.ofs)
150 continue;
151 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
152 continue;
Lars Hjemli536b0542008-04-13 11:57:10 +0200153 if (!header++)
154 print_header(columns);
Benjamin Closed71c0c72008-11-25 06:25:35 -0800155 if (!ctx.qry.sort &&
156 ((last_group == NULL && ctx.repo->group != NULL) ||
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100157 (last_group != NULL && ctx.repo->group == NULL) ||
158 (last_group != NULL && ctx.repo->group != NULL &&
Benjamin Closed71c0c72008-11-25 06:25:35 -0800159 strcmp(ctx.repo->group, last_group)))) {
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200160 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
161 columns);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100162 html_txt(ctx.repo->group);
Lars Hjemli5877c492007-05-18 22:48:22 +0200163 html("</td></tr>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100164 last_group = ctx.repo->group;
Lars Hjemli5877c492007-05-18 22:48:22 +0200165 }
Lars Hjemli0b8b6a32007-05-21 00:14:28 +0200166 htmlf("<tr><td class='%s'>",
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100167 ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200168 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
Lars Hjemli74620f12006-12-11 16:48:03 +0100169 html("</td><td>");
Lars Hjemlie9a70422008-04-14 22:23:48 +0200170 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100171 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
Lars Hjemlie9a70422008-04-14 22:23:48 +0200172 html_link_close();
Lars Hjemli74620f12006-12-11 16:48:03 +0100173 html("</td><td>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100174 html_txt(ctx.repo->owner);
Lars Hjemli8f6f8c32007-05-16 02:12:06 +0200175 html("</td><td>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100176 print_modtime(ctx.repo);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200177 html("</td>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100178 if (ctx.cfg.enable_index_links) {
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200179 html("<td>");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200180 cgit_summary_link("summary", NULL, "button", NULL);
Lars Hjemli51140312007-11-03 10:42:37 +0100181 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
182 0, NULL, NULL);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200183 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
184 html("</td>");
185 }
186 html("</tr>\n");
Lars Hjemli74620f12006-12-11 16:48:03 +0100187 }
Lars Hjemli74620f12006-12-11 16:48:03 +0100188 html("</table>");
Lars Hjemli536b0542008-04-13 11:57:10 +0200189 if (!hits)
190 cgit_print_error("No repositories found");
Lars Hjemlic6078b82008-05-03 10:54:39 +0200191 else if (hits > ctx.cfg.max_repo_count)
192 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
Lars Hjemli74620f12006-12-11 16:48:03 +0100193 cgit_print_docend();
194}
Lars Hjemlic6431a72008-04-29 01:06:30 +0200195
196void cgit_print_site_readme()
197{
198 if (ctx.cfg.root_readme)
199 html_include(ctx.cfg.root_readme);
200}