2006-12-11 15:48:03 +00:00
|
|
|
/* ui-repolist.c: functions for generating the repolist page
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Lars Hjemli
|
2012-07-12 18:00:40 +00:00
|
|
|
* Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
|
2006-12-11 15:48:03 +00:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2008-02-23 21:45:33 +00:00
|
|
|
#include "cgit.h"
|
|
|
|
#include "html.h"
|
2008-03-24 15:50:57 +00:00
|
|
|
#include "ui-shared.h"
|
2012-07-12 17:13:39 +00:00
|
|
|
#include <strings.h>
|
2007-05-22 21:25:25 +00:00
|
|
|
|
|
|
|
time_t read_agefile(char *path)
|
|
|
|
{
|
2009-08-18 15:17:41 +00:00
|
|
|
time_t result;
|
|
|
|
size_t size;
|
|
|
|
char *buf;
|
|
|
|
static char buf2[64];
|
2007-05-22 21:25:25 +00:00
|
|
|
|
2009-08-18 15:17:41 +00:00
|
|
|
if (readfile(path, &buf, &size))
|
2007-05-22 21:25:25 +00:00
|
|
|
return -1;
|
2009-08-18 15:17:41 +00:00
|
|
|
|
2011-05-23 21:10:37 +00:00
|
|
|
if (parse_date(buf, buf2, sizeof(buf2)) > 0)
|
2009-08-18 15:17:41 +00:00
|
|
|
result = strtoul(buf2, NULL, 10);
|
2007-05-22 21:25:25 +00:00
|
|
|
else
|
2009-08-18 15:17:41 +00:00
|
|
|
result = 0;
|
|
|
|
free(buf);
|
|
|
|
return result;
|
2007-05-22 21:25:25 +00:00
|
|
|
}
|
|
|
|
|
2008-11-29 12:33:02 +00:00
|
|
|
static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
|
2007-05-22 21:25:25 +00:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
struct stat s;
|
2008-11-29 15:46:37 +00:00
|
|
|
struct cgit_repo *r = (struct cgit_repo *)repo;
|
2007-05-22 21:25:25 +00:00
|
|
|
|
2008-11-29 15:46:37 +00:00
|
|
|
if (repo->mtime != -1) {
|
|
|
|
*mtime = repo->mtime;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-16 12:07:13 +00:00
|
|
|
path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
|
2007-05-22 21:25:25 +00:00
|
|
|
if (stat(path, &s) == 0) {
|
2008-11-29 12:33:02 +00:00
|
|
|
*mtime = read_agefile(path);
|
2008-11-29 15:46:37 +00:00
|
|
|
r->mtime = *mtime;
|
2008-11-29 12:33:02 +00:00
|
|
|
return 1;
|
2007-05-22 21:25:25 +00:00
|
|
|
}
|
|
|
|
|
2011-07-19 09:34:18 +00:00
|
|
|
path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
|
|
|
|
repo->defbranch : "master");
|
2011-05-13 21:09:34 +00:00
|
|
|
if (stat(path, &s) == 0) {
|
2008-11-29 12:33:02 +00:00
|
|
|
*mtime = s.st_mtime;
|
2011-05-13 21:09:34 +00:00
|
|
|
r->mtime = *mtime;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = fmt("%s/%s", repo->path, "packed-refs");
|
|
|
|
if (stat(path, &s) == 0) {
|
|
|
|
*mtime = s.st_mtime;
|
|
|
|
r->mtime = *mtime;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-11-29 15:46:37 +00:00
|
|
|
|
2011-05-13 21:09:34 +00:00
|
|
|
*mtime = 0;
|
2008-11-29 15:46:37 +00:00
|
|
|
r->mtime = *mtime;
|
|
|
|
return (r->mtime != 0);
|
2008-11-29 12:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_modtime(struct cgit_repo *repo)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
if (get_repo_modtime(repo, &t))
|
|
|
|
cgit_print_age(t, -1, NULL);
|
2007-05-22 21:25:25 +00:00
|
|
|
}
|
2006-12-11 15:48:03 +00:00
|
|
|
|
2008-04-13 09:57:10 +00:00
|
|
|
int is_match(struct cgit_repo *repo)
|
2006-12-11 15:48:03 +00:00
|
|
|
{
|
2008-04-13 09:57:10 +00:00
|
|
|
if (!ctx.qry.search)
|
|
|
|
return 1;
|
2008-04-13 10:42:27 +00:00
|
|
|
if (repo->url && strcasestr(repo->url, ctx.qry.search))
|
2008-04-13 09:57:10 +00:00
|
|
|
return 1;
|
2008-04-13 10:42:27 +00:00
|
|
|
if (repo->name && strcasestr(repo->name, ctx.qry.search))
|
2008-04-13 09:57:10 +00:00
|
|
|
return 1;
|
2008-04-13 10:42:27 +00:00
|
|
|
if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
|
2008-04-13 09:57:10 +00:00
|
|
|
return 1;
|
2008-04-13 10:42:27 +00:00
|
|
|
if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
|
2008-04-13 09:57:10 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-11 15:48:03 +00:00
|
|
|
|
2008-09-14 18:18:10 +00:00
|
|
|
int is_in_url(struct cgit_repo *repo)
|
|
|
|
{
|
|
|
|
if (!ctx.qry.url)
|
|
|
|
return 1;
|
|
|
|
if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-29 13:08:51 +00:00
|
|
|
void print_sort_header(const char *title, const char *sort)
|
|
|
|
{
|
2009-09-20 10:32:07 +00:00
|
|
|
htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort);
|
2008-11-29 13:08:51 +00:00
|
|
|
if (ctx.qry.search) {
|
|
|
|
html("&q=");
|
|
|
|
html_url_arg(ctx.qry.search);
|
|
|
|
}
|
|
|
|
htmlf("'>%s</a></th>", title);
|
|
|
|
}
|
|
|
|
|
2008-04-13 09:57:10 +00:00
|
|
|
void print_header(int columns)
|
|
|
|
{
|
2008-11-29 13:08:51 +00:00
|
|
|
html("<tr class='nohover'>");
|
|
|
|
print_sort_header("Name", "name");
|
|
|
|
print_sort_header("Description", "desc");
|
|
|
|
print_sort_header("Owner", "owner");
|
|
|
|
print_sort_header("Idle", "idle");
|
2008-02-16 12:07:13 +00:00
|
|
|
if (ctx.cfg.enable_index_links)
|
2008-04-13 08:57:11 +00:00
|
|
|
html("<th class='left'>Links</th>");
|
2007-06-18 22:56:40 +00:00
|
|
|
html("</tr>\n");
|
2008-04-13 09:57:10 +00:00
|
|
|
}
|
2006-12-11 15:48:03 +00:00
|
|
|
|
2008-05-03 08:54:39 +00:00
|
|
|
|
2011-07-31 00:44:05 +00:00
|
|
|
void print_pager(int items, int pagelen, char *search, char *sort)
|
2008-05-03 08:54:39 +00:00
|
|
|
{
|
2012-10-08 16:49:34 +00:00
|
|
|
int i, ofs;
|
|
|
|
char *class = NULL;
|
2008-05-03 08:54:39 +00:00
|
|
|
html("<div class='pager'>");
|
2012-10-08 16:49:34 +00:00
|
|
|
for(i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
|
|
|
|
class = (ctx.qry.ofs == ofs) ? "current" : NULL;
|
|
|
|
cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), class,
|
|
|
|
search, sort, ofs);
|
|
|
|
}
|
2008-05-03 08:54:39 +00:00
|
|
|
html("</div>");
|
|
|
|
}
|
|
|
|
|
2008-11-29 13:08:51 +00:00
|
|
|
static int cmp(const char *s1, const char *s2)
|
|
|
|
{
|
2012-07-12 17:13:39 +00:00
|
|
|
if (s1 && s2) {
|
|
|
|
if (ctx.cfg.case_sensitive_sort)
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
else
|
|
|
|
return strcasecmp(s1, s2);
|
|
|
|
}
|
2008-11-29 13:08:51 +00:00
|
|
|
if (s1 && !s2)
|
|
|
|
return -1;
|
2008-11-29 13:27:35 +00:00
|
|
|
if (s2 && !s1)
|
|
|
|
return 1;
|
2008-11-29 13:08:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-23 21:09:31 +00:00
|
|
|
static int sort_section(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct cgit_repo *r1 = a;
|
|
|
|
const struct cgit_repo *r2 = b;
|
|
|
|
int result;
|
2012-07-12 17:13:39 +00:00
|
|
|
time_t t;
|
2009-08-23 21:09:31 +00:00
|
|
|
|
|
|
|
result = cmp(r1->section, r2->section);
|
2012-07-12 17:13:39 +00:00
|
|
|
if (!result) {
|
|
|
|
if (!strcmp(ctx.cfg.section_sort, "age")) {
|
|
|
|
// get_repo_modtime caches the value in r->mtime, so we don't
|
|
|
|
// have to worry about inefficiencies here.
|
|
|
|
if (get_repo_modtime(r1, &t) && get_repo_modtime(r2, &t))
|
|
|
|
result = r2->mtime - r1->mtime;
|
|
|
|
}
|
|
|
|
if (!result)
|
|
|
|
result = cmp(r1->name, r2->name);
|
|
|
|
}
|
2009-08-23 21:09:31 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-11-29 13:08:51 +00:00
|
|
|
static int sort_name(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct cgit_repo *r1 = a;
|
|
|
|
const struct cgit_repo *r2 = b;
|
|
|
|
|
|
|
|
return cmp(r1->name, r2->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sort_desc(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct cgit_repo *r1 = a;
|
|
|
|
const struct cgit_repo *r2 = b;
|
|
|
|
|
|
|
|
return cmp(r1->desc, r2->desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sort_owner(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct cgit_repo *r1 = a;
|
|
|
|
const struct cgit_repo *r2 = b;
|
|
|
|
|
|
|
|
return cmp(r1->owner, r2->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sort_idle(const void *a, const void *b)
|
2008-11-25 14:25:35 +00:00
|
|
|
{
|
|
|
|
const struct cgit_repo *r1 = a;
|
|
|
|
const struct cgit_repo *r2 = b;
|
|
|
|
time_t t1, t2;
|
|
|
|
|
2008-11-29 12:33:02 +00:00
|
|
|
t1 = t2 = 0;
|
|
|
|
get_repo_modtime(r1, &t1);
|
|
|
|
get_repo_modtime(r2, &t2);
|
|
|
|
return t2 - t1;
|
2008-11-25 14:25:35 +00:00
|
|
|
}
|
|
|
|
|
2008-11-29 13:08:51 +00:00
|
|
|
struct sortcolumn {
|
|
|
|
const char *name;
|
|
|
|
int (*fn)(const void *a, const void *b);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sortcolumn sortcolumn[] = {
|
2009-08-23 21:09:31 +00:00
|
|
|
{"section", sort_section},
|
2008-11-29 13:08:51 +00:00
|
|
|
{"name", sort_name},
|
|
|
|
{"desc", sort_desc},
|
|
|
|
{"owner", sort_owner},
|
|
|
|
{"idle", sort_idle},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
int sort_repolist(char *field)
|
|
|
|
{
|
|
|
|
struct sortcolumn *column;
|
|
|
|
|
|
|
|
for (column = &sortcolumn[0]; column->name; column++) {
|
|
|
|
if (strcmp(field, column->name))
|
|
|
|
continue;
|
|
|
|
qsort(cgit_repolist.repos, cgit_repolist.count,
|
|
|
|
sizeof(struct cgit_repo), column->fn);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 09:57:10 +00:00
|
|
|
void cgit_print_repolist()
|
|
|
|
{
|
|
|
|
int i, columns = 4, hits = 0, header = 0;
|
2009-08-23 20:58:39 +00:00
|
|
|
char *last_section = NULL;
|
2009-08-24 05:42:03 +00:00
|
|
|
char *section;
|
2008-11-29 13:08:51 +00:00
|
|
|
int sorted = 0;
|
2008-04-13 09:57:10 +00:00
|
|
|
|
|
|
|
if (ctx.cfg.enable_index_links)
|
|
|
|
columns++;
|
|
|
|
|
|
|
|
ctx.page.title = ctx.cfg.root_title;
|
|
|
|
cgit_print_http_headers(&ctx);
|
|
|
|
cgit_print_docstart(&ctx);
|
|
|
|
cgit_print_pageheader(&ctx);
|
|
|
|
|
2008-04-28 22:35:49 +00:00
|
|
|
if (ctx.cfg.index_header)
|
|
|
|
html_include(ctx.cfg.index_header);
|
|
|
|
|
2008-11-25 14:25:35 +00:00
|
|
|
if(ctx.qry.sort)
|
2008-11-29 13:08:51 +00:00
|
|
|
sorted = sort_repolist(ctx.qry.sort);
|
2009-08-23 21:09:31 +00:00
|
|
|
else
|
|
|
|
sort_repolist("section");
|
2008-11-25 14:25:35 +00:00
|
|
|
|
2008-04-13 09:57:10 +00:00
|
|
|
html("<table summary='repository list' class='list nowrap'>");
|
2007-02-03 14:02:55 +00:00
|
|
|
for (i=0; i<cgit_repolist.count; i++) {
|
2008-02-16 12:56:09 +00:00
|
|
|
ctx.repo = &cgit_repolist.repos[i];
|
2008-09-14 18:18:10 +00:00
|
|
|
if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
|
2008-04-13 09:57:10 +00:00
|
|
|
continue;
|
2008-05-03 08:54:39 +00:00
|
|
|
hits++;
|
|
|
|
if (hits <= ctx.qry.ofs)
|
|
|
|
continue;
|
|
|
|
if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
|
|
|
|
continue;
|
2008-04-13 09:57:10 +00:00
|
|
|
if (!header++)
|
|
|
|
print_header(columns);
|
2009-08-24 05:42:03 +00:00
|
|
|
section = ctx.repo->section;
|
|
|
|
if (section && !strcmp(section, ""))
|
|
|
|
section = NULL;
|
2008-11-29 13:08:51 +00:00
|
|
|
if (!sorted &&
|
2009-08-24 05:42:03 +00:00
|
|
|
((last_section == NULL && section != NULL) ||
|
|
|
|
(last_section != NULL && section == NULL) ||
|
|
|
|
(last_section != NULL && section != NULL &&
|
|
|
|
strcmp(section, last_section)))) {
|
2009-08-23 20:58:39 +00:00
|
|
|
htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
|
2007-06-18 22:56:40 +00:00
|
|
|
columns);
|
2009-08-24 05:42:03 +00:00
|
|
|
html_txt(section);
|
2007-05-18 20:48:22 +00:00
|
|
|
html("</td></tr>");
|
2009-08-24 05:42:03 +00:00
|
|
|
last_section = section;
|
2007-05-18 20:48:22 +00:00
|
|
|
}
|
2007-05-20 22:14:28 +00:00
|
|
|
htmlf("<tr><td class='%s'>",
|
2009-08-24 05:42:03 +00:00
|
|
|
!sorted && section ? "sublevel-repo" : "toplevel-repo");
|
2008-10-05 15:16:36 +00:00
|
|
|
cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
|
2006-12-11 15:48:03 +00:00
|
|
|
html("</td><td>");
|
2008-04-14 20:23:48 +00:00
|
|
|
html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
|
2008-02-16 12:56:09 +00:00
|
|
|
html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
|
2008-04-14 20:23:48 +00:00
|
|
|
html_link_close();
|
2006-12-11 15:48:03 +00:00
|
|
|
html("</td><td>");
|
2008-02-16 12:56:09 +00:00
|
|
|
html_txt(ctx.repo->owner);
|
2007-05-16 00:12:06 +00:00
|
|
|
html("</td><td>");
|
2008-02-16 12:56:09 +00:00
|
|
|
print_modtime(ctx.repo);
|
2007-06-18 22:56:40 +00:00
|
|
|
html("</td>");
|
2008-02-16 12:07:13 +00:00
|
|
|
if (ctx.cfg.enable_index_links) {
|
2007-06-18 22:56:40 +00:00
|
|
|
html("<td>");
|
2008-10-05 15:16:36 +00:00
|
|
|
cgit_summary_link("summary", NULL, "button", NULL);
|
2007-11-03 09:42:37 +00:00
|
|
|
cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
|
2008-11-29 17:39:41 +00:00
|
|
|
0, NULL, NULL, ctx.qry.showmsg);
|
2007-06-18 22:56:40 +00:00
|
|
|
cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
|
|
|
|
html("</td>");
|
|
|
|
}
|
|
|
|
html("</tr>\n");
|
2006-12-11 15:48:03 +00:00
|
|
|
}
|
|
|
|
html("</table>");
|
2008-04-13 09:57:10 +00:00
|
|
|
if (!hits)
|
|
|
|
cgit_print_error("No repositories found");
|
2008-05-03 08:54:39 +00:00
|
|
|
else if (hits > ctx.cfg.max_repo_count)
|
2011-07-31 00:44:05 +00:00
|
|
|
print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
|
2006-12-11 15:48:03 +00:00
|
|
|
cgit_print_docend();
|
|
|
|
}
|
2008-04-28 23:06:30 +00:00
|
|
|
|
|
|
|
void cgit_print_site_readme()
|
|
|
|
{
|
2009-08-09 11:27:21 +00:00
|
|
|
if (!ctx.cfg.root_readme)
|
|
|
|
return;
|
|
|
|
if (ctx.cfg.about_filter)
|
2011-06-06 19:29:58 +00:00
|
|
|
cgit_open_filter(ctx.cfg.about_filter);
|
2009-08-09 11:27:21 +00:00
|
|
|
html_include(ctx.cfg.root_readme);
|
|
|
|
if (ctx.cfg.about_filter)
|
|
|
|
cgit_close_filter(ctx.cfg.about_filter);
|
2008-04-28 23:06:30 +00:00
|
|
|
}
|