ui-repolist.c (view raw)
1/* 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
9#include <time.h>
10
11#include "cgit.h"
12#include "html.h"
13#include "ui-shared.h"
14
15time_t read_agefile(char *path)
16{
17 FILE *f;
18 static char buf[64], buf2[64];
19
20 if (!(f = fopen(path, "r")))
21 return -1;
22 if (fgets(buf, sizeof(buf), f) == NULL)
23 return -1;
24 fclose(f);
25 if (parse_date(buf, buf2, sizeof(buf2)))
26 return strtoul(buf2, NULL, 10);
27 else
28 return 0;
29}
30
31static void print_modtime(struct cgit_repo *repo)
32{
33 char *path;
34 struct stat s;
35
36 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
37 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}
47
48int is_match(struct cgit_repo *repo)
49{
50 if (!ctx.qry.search)
51 return 1;
52 if (repo->url && strcasestr(repo->url, ctx.qry.search))
53 return 1;
54 if (repo->name && strcasestr(repo->name, ctx.qry.search))
55 return 1;
56 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
57 return 1;
58 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
59 return 1;
60 return 0;
61}
62
63int 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
72void print_header(int columns)
73{
74 html("<tr class='nohover'>"
75 "<th class='left'>Name</th>"
76 "<th class='left'>Description</th>"
77 "<th class='left'>Owner</th>"
78 "<th class='left'>Idle</th>");
79 if (ctx.cfg.enable_index_links)
80 html("<th class='left'>Links</th>");
81 html("</tr>\n");
82}
83
84
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
95void 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
108 if (ctx.cfg.index_header)
109 html_include(ctx.cfg.index_header);
110
111 html("<table summary='repository list' class='list nowrap'>");
112 for (i=0; i<cgit_repolist.count; i++) {
113 ctx.repo = &cgit_repolist.repos[i];
114 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
115 continue;
116 hits++;
117 if (hits <= ctx.qry.ofs)
118 continue;
119 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
120 continue;
121 if (!header++)
122 print_header(columns);
123 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))) {
127 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
128 columns);
129 html_txt(ctx.repo->group);
130 html("</td></tr>");
131 last_group = ctx.repo->group;
132 }
133 htmlf("<tr><td class='%s'>",
134 ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
135 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
136 html("</td><td>");
137 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
138 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
139 html_link_close();
140 html("</td><td>");
141 html_txt(ctx.repo->owner);
142 html("</td><td>");
143 print_modtime(ctx.repo);
144 html("</td>");
145 if (ctx.cfg.enable_index_links) {
146 html("<td>");
147 cgit_summary_link("summary", NULL, "button", NULL);
148 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
149 0, NULL, NULL, ctx.qry.showmsg);
150 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
151 html("</td>");
152 }
153 html("</tr>\n");
154 }
155 html("</table>");
156 if (!hits)
157 cgit_print_error("No repositories found");
158 else if (hits > ctx.cfg.max_repo_count)
159 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
160 cgit_print_docend();
161}
162
163void cgit_print_site_readme()
164{
165 if (ctx.cfg.root_readme)
166 html_include(ctx.cfg.root_readme);
167}