1 /* ui-repolist.c: functions for generating the repolist page
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
13 #include "ui-shared.h"
15 time_t read_agefile(char *path)
18 static char buf[64], buf2[64];
20 if (!(f = fopen(path, "r")))
22 if (fgets(buf, sizeof(buf), f) == NULL)
25 if (parse_date(buf, buf2, sizeof(buf2)))
26 return strtoul(buf2, NULL, 10);
31 static void print_modtime(struct cgit_repo *repo)
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);
42 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
43 if (stat(path, &s) != 0)
45 cgit_print_age(s.st_mtime, -1, NULL);
48 int is_match(struct cgit_repo *repo)
52 if (repo->url && strcasestr(repo->url, ctx.qry.search))
54 if (repo->name && strcasestr(repo->name, ctx.qry.search))
56 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
58 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
63 int is_in_url(struct cgit_repo *repo)
67 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
72 void print_header(int columns)
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'><a href=\"?s=1\">Idle</a></th>");
79 if (ctx.cfg.enable_index_links)
80 html("<th class='left'>Links</th>");
85 void print_pager(int items, int pagelen, char *search)
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,
95 static int cgit_reposort_modtime(const void *a, const void *b)
97 const struct cgit_repo *r1 = a;
98 const struct cgit_repo *r2 = b;
102 path = fmt("%s/%s", r1->path, ctx.cfg.agefile);
103 if (stat(path, &s) == 0) {
104 t1 = read_agefile(path);
106 path = fmt("%s/refs/heads/%s", r1->path, r1->defbranch);
107 if (stat(path, &s) != 0)
112 path = fmt("%s/%s", r2->path, ctx.cfg.agefile);
113 if (stat(path, &s) == 0) {
114 t2 = read_agefile(path);
116 path = fmt("%s/refs/heads/%s", r2->path, r2->defbranch);
117 if (stat(path, &s) != 0)
124 void cgit_print_repolist()
126 int i, columns = 4, hits = 0, header = 0;
127 char *last_group = NULL;
129 if (ctx.cfg.enable_index_links)
132 ctx.page.title = ctx.cfg.root_title;
133 cgit_print_http_headers(&ctx);
134 cgit_print_docstart(&ctx);
135 cgit_print_pageheader(&ctx);
137 if (ctx.cfg.index_header)
138 html_include(ctx.cfg.index_header);
141 qsort(cgit_repolist.repos,cgit_repolist.count,sizeof(struct cgit_repo),cgit_reposort_modtime);
143 html("<table summary='repository list' class='list nowrap'>");
144 for (i=0; i<cgit_repolist.count; i++) {
145 ctx.repo = &cgit_repolist.repos[i];
146 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
149 if (hits <= ctx.qry.ofs)
151 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
154 print_header(columns);
156 ((last_group == NULL && ctx.repo->group != NULL) ||
157 (last_group != NULL && ctx.repo->group == NULL) ||
158 (last_group != NULL && ctx.repo->group != NULL &&
159 strcmp(ctx.repo->group, last_group)))) {
160 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
162 html_txt(ctx.repo->group);
164 last_group = ctx.repo->group;
166 htmlf("<tr><td class='%s'>",
167 ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
168 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
170 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
171 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
174 html_txt(ctx.repo->owner);
176 print_modtime(ctx.repo);
178 if (ctx.cfg.enable_index_links) {
180 cgit_summary_link("summary", NULL, "button", NULL);
181 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
183 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
190 cgit_print_error("No repositories found");
191 else if (hits > ctx.cfg.max_repo_count)
192 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
196 void cgit_print_site_readme()
198 if (ctx.cfg.root_readme)
199 html_include(ctx.cfg.root_readme);