blob: 58331404b7e45482fd3162e6c5f624c89b4c7959 [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>"
Lars Hjemli0d05bca2007-06-19 00:56:40 +020078 "<th class='left'>Idle</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
Lars Hjemli536b0542008-04-13 11:57:10 +020095void cgit_print_repolist()
96{
97 int i, columns = 4, hits = 0, header = 0;
98 char *last_group = NULL;
99
100 if (ctx.cfg.enable_index_links)
101 columns++;
102
103 ctx.page.title = ctx.cfg.root_title;
104 cgit_print_http_headers(&ctx);
105 cgit_print_docstart(&ctx);
106 cgit_print_pageheader(&ctx);
107
Lars Hjemli80628172008-04-29 00:35:49 +0200108 if (ctx.cfg.index_header)
109 html_include(ctx.cfg.index_header);
110
Lars Hjemli536b0542008-04-13 11:57:10 +0200111 html("<table summary='repository list' class='list nowrap'>");
Lars Hjemlice1c7332007-02-03 15:02:55 +0100112 for (i=0; i<cgit_repolist.count; i++) {
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100113 ctx.repo = &cgit_repolist.repos[i];
Lars Hjemlidd80ef52008-09-14 20:18:10 +0200114 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
Lars Hjemli536b0542008-04-13 11:57:10 +0200115 continue;
Lars Hjemlic6078b82008-05-03 10:54:39 +0200116 hits++;
117 if (hits <= ctx.qry.ofs)
118 continue;
119 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
120 continue;
Lars Hjemli536b0542008-04-13 11:57:10 +0200121 if (!header++)
122 print_header(columns);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100123 if ((last_group == NULL && ctx.repo->group != NULL) ||
124 (last_group != NULL && ctx.repo->group == NULL) ||
125 (last_group != NULL && ctx.repo->group != NULL &&
126 strcmp(ctx.repo->group, last_group))) {
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200127 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
128 columns);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100129 html_txt(ctx.repo->group);
Lars Hjemli5877c492007-05-18 22:48:22 +0200130 html("</td></tr>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100131 last_group = ctx.repo->group;
Lars Hjemli5877c492007-05-18 22:48:22 +0200132 }
Lars Hjemli0b8b6a32007-05-21 00:14:28 +0200133 htmlf("<tr><td class='%s'>",
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100134 ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200135 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
Lars Hjemli74620f12006-12-11 16:48:03 +0100136 html("</td><td>");
Lars Hjemlie9a70422008-04-14 22:23:48 +0200137 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100138 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
Lars Hjemlie9a70422008-04-14 22:23:48 +0200139 html_link_close();
Lars Hjemli74620f12006-12-11 16:48:03 +0100140 html("</td><td>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100141 html_txt(ctx.repo->owner);
Lars Hjemli8f6f8c32007-05-16 02:12:06 +0200142 html("</td><td>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100143 print_modtime(ctx.repo);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200144 html("</td>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100145 if (ctx.cfg.enable_index_links) {
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200146 html("<td>");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200147 cgit_summary_link("summary", NULL, "button", NULL);
Lars Hjemli51140312007-11-03 10:42:37 +0100148 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
Lars Hjemli0274b572008-11-29 18:39:41 +0100149 0, NULL, NULL, ctx.qry.showmsg);
Lars Hjemli0d05bca2007-06-19 00:56:40 +0200150 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
151 html("</td>");
152 }
153 html("</tr>\n");
Lars Hjemli74620f12006-12-11 16:48:03 +0100154 }
Lars Hjemli74620f12006-12-11 16:48:03 +0100155 html("</table>");
Lars Hjemli536b0542008-04-13 11:57:10 +0200156 if (!hits)
157 cgit_print_error("No repositories found");
Lars Hjemlic6078b82008-05-03 10:54:39 +0200158 else if (hits > ctx.cfg.max_repo_count)
159 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
Lars Hjemli74620f12006-12-11 16:48:03 +0100160 cgit_print_docend();
161}
Lars Hjemlic6431a72008-04-29 01:06:30 +0200162
163void cgit_print_site_readme()
164{
165 if (ctx.cfg.root_readme)
166 html_include(ctx.cfg.root_readme);
167}