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)
9 /* This is needed for strcasestr to be defined by <string.h> */
17 #include "ui-shared.h"
19 time_t read_agefile(char *path)
22 static char buf[64], buf2[64];
24 if (!(f = fopen(path, "r")))
26 if (fgets(buf, sizeof(buf), f) == NULL)
29 if (parse_date(buf, buf2, sizeof(buf2)))
30 return strtoul(buf2, NULL, 10);
35 static void print_modtime(struct cgit_repo *repo)
40 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
41 if (stat(path, &s) == 0) {
42 cgit_print_age(read_agefile(path), -1, NULL);
46 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
47 if (stat(path, &s) != 0)
49 cgit_print_age(s.st_mtime, -1, NULL);
52 int is_match(struct cgit_repo *repo)
56 if (repo->url && strcasestr(repo->url, ctx.qry.search))
58 if (repo->name && strcasestr(repo->name, ctx.qry.search))
60 if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
62 if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
67 int is_in_url(struct cgit_repo *repo)
71 if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
76 void print_header(int columns)
78 html("<tr class='nohover'>"
79 "<th class='left'>Name</th>"
80 "<th class='left'>Description</th>"
81 "<th class='left'>Owner</th>"
82 "<th class='left'>Idle</th>");
83 if (ctx.cfg.enable_index_links)
84 html("<th class='left'>Links</th>");
89 void print_pager(int items, int pagelen, char *search)
92 html("<div class='pager'>");
93 for(i = 0; i * pagelen < items; i++)
94 cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
99 void cgit_print_repolist()
101 int i, columns = 4, hits = 0, header = 0;
102 char *last_group = NULL;
104 if (ctx.cfg.enable_index_links)
107 ctx.page.title = ctx.cfg.root_title;
108 cgit_print_http_headers(&ctx);
109 cgit_print_docstart(&ctx);
110 cgit_print_pageheader(&ctx);
112 if (ctx.cfg.index_header)
113 html_include(ctx.cfg.index_header);
115 html("<table summary='repository list' class='list nowrap'>");
116 for (i=0; i<cgit_repolist.count; i++) {
117 ctx.repo = &cgit_repolist.repos[i];
118 if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
121 if (hits <= ctx.qry.ofs)
123 if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
126 print_header(columns);
127 if ((last_group == NULL && ctx.repo->group != NULL) ||
128 (last_group != NULL && ctx.repo->group == NULL) ||
129 (last_group != NULL && ctx.repo->group != NULL &&
130 strcmp(ctx.repo->group, last_group))) {
131 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
133 html_txt(ctx.repo->group);
135 last_group = ctx.repo->group;
137 htmlf("<tr><td class='%s'>",
138 ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
139 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
141 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
142 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
145 html_txt(ctx.repo->owner);
147 print_modtime(ctx.repo);
149 if (ctx.cfg.enable_index_links) {
151 cgit_summary_link("summary", NULL, "button", NULL);
152 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
154 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
161 cgit_print_error("No repositories found");
162 else if (hits > ctx.cfg.max_repo_count)
163 print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
167 void cgit_print_site_readme()
169 if (ctx.cfg.root_readme)
170 html_include(ctx.cfg.root_readme);