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 int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
36 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
37 if (stat(path, &s) == 0) {
38 *mtime = read_agefile(path);
42 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
43 if (stat(path, &s) == 0) {
50 static void print_modtime(struct cgit_repo *repo)
53 if (get_repo_modtime(repo, &t))
54 cgit_print_age(t, -1, NULL);
57 int is_match(struct cgit_repo *repo)
61 if (repo->url && strcasestr(repo->url, ctx.qry.search))
63 if (repo->name && strcasestr(repo->name, ctx.qry.search))
65 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
67 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
72 int is_in_url(struct cgit_repo *repo)
76 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
81 void print_header(int columns)
83 html("<tr class='nohover'>"
84 "<th class='left'>Name</th>"
85 "<th class='left'>Description</th>"
86 "<th class='left'>Owner</th>"
87 "<th class='left'><a href=\"?s=1\">Idle</a></th>");
88 if (ctx.cfg.enable_index_links)
89 html("<th class='left'>Links</th>");
94 void print_pager(int items, int pagelen, char *search)
97 html("<div class='pager'>");
98 for(i = 0; i * pagelen < items; i++)
99 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
100 search, i * pagelen);
104 static int cgit_reposort_modtime(const void *a, const void *b)
106 const struct cgit_repo *r1 = a;
107 const struct cgit_repo *r2 = b;
111 get_repo_modtime(r1, &t1);
112 get_repo_modtime(r2, &t2);
116 void cgit_print_repolist()
118 int i, columns = 4, hits = 0, header = 0;
119 char *last_group = NULL;
121 if (ctx.cfg.enable_index_links)
124 ctx.page.title = ctx.cfg.root_title;
125 cgit_print_http_headers(&ctx);
126 cgit_print_docstart(&ctx);
127 cgit_print_pageheader(&ctx);
129 if (ctx.cfg.index_header)
130 html_include(ctx.cfg.index_header);
133 qsort(cgit_repolist.repos,cgit_repolist.count,sizeof(struct cgit_repo),cgit_reposort_modtime);
135 html("<table summary='repository list' class='list nowrap'>");
136 for (i=0; i<cgit_repolist.count; i++) {
137 ctx.repo = &cgit_repolist.repos[i];
138 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
141 if (hits <= ctx.qry.ofs)
143 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
146 print_header(columns);
148 ((last_group == NULL && ctx.repo->group != NULL) ||
149 (last_group != NULL && ctx.repo->group == NULL) ||
150 (last_group != NULL && ctx.repo->group != NULL &&
151 strcmp(ctx.repo->group, last_group)))) {
152 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
154 html_txt(ctx.repo->group);
156 last_group = ctx.repo->group;
158 htmlf("<tr><td class='%s'>",
159 ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
160 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
162 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
163 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
166 html_txt(ctx.repo->owner);
168 print_modtime(ctx.repo);
170 if (ctx.cfg.enable_index_links) {
172 cgit_summary_link("summary", NULL, "button", NULL);
173 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
175 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
182 cgit_print_error("No repositories found");
183 else if (hits > ctx.cfg.max_repo_count)
184 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
188 void cgit_print_site_readme()
190 if (ctx.cfg.root_readme)
191 html_include(ctx.cfg.root_readme);