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"
|
2013-04-06 10:37:59 +00:00
|
|
|
#include "ui-repolist.h"
|
2008-02-23 21:45:33 +00:00
|
|
|
#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
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static time_t read_agefile(char *path)
|
2007-05-22 21:25:25 +00:00
|
|
|
{
|
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
|
|
|
{
|
2013-04-06 09:28:57 +00:00
|
|
|
struct strbuf path = STRBUF_INIT;
|
2007-05-22 21:25:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-04-06 09:28:57 +00:00
|
|
|
strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
|
|
|
|
if (stat(path.buf, &s) == 0) {
|
|
|
|
*mtime = read_agefile(path.buf);
|
2012-10-09 11:09:58 +00:00
|
|
|
if (*mtime) {
|
|
|
|
r->mtime = *mtime;
|
2013-04-06 09:28:57 +00:00
|
|
|
goto end;
|
2012-10-09 11:09:58 +00:00
|
|
|
}
|
2007-05-22 21:25:25 +00:00
|
|
|
}
|
|
|
|
|
2013-04-06 09:28:57 +00:00
|
|
|
strbuf_reset(&path);
|
|
|
|
strbuf_addf(&path, "%s/refs/heads/%s", repo->path,
|
|
|
|
repo->defbranch ? repo->defbranch : "master");
|
|
|
|
if (stat(path.buf, &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;
|
2013-04-06 09:28:57 +00:00
|
|
|
goto end;
|
2011-05-13 21:09:34 +00:00
|
|
|
}
|
|
|
|
|
2013-04-06 09:28:57 +00:00
|
|
|
strbuf_reset(&path);
|
|
|
|
strbuf_addf(&path, "%s/%s", repo->path, "packed-refs");
|
|
|
|
if (stat(path.buf, &s) == 0) {
|
2011-05-13 21:09:34 +00:00
|
|
|
*mtime = s.st_mtime;
|
|
|
|
r->mtime = *mtime;
|
2013-04-06 09:28:57 +00:00
|
|
|
goto end;
|
2011-05-13 21:09:34 +00:00
|
|
|
}
|
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;
|
2013-04-06 09:28:57 +00:00
|
|
|
end:
|
|
|
|
strbuf_release(&path);
|
2008-11-29 15:46:37 +00:00
|
|
|
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
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static 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
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static int is_in_url(struct cgit_repo *repo)
|
2008-09-14 18:18:10 +00:00
|
|
|
{
|
|
|
|
if (!ctx.qry.url)
|
|
|
|
return 1;
|
|
|
|
if (repo->url && !prefixcmp(repo->url, ctx.qry.url))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static void print_sort_header(const char *title, const char *sort)
|
2008-11-29 13:08:51 +00:00
|
|
|
{
|
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) {
|
2012-10-09 18:45:58 +00:00
|
|
|
html("&q=");
|
2008-11-29 13:08:51 +00:00
|
|
|
html_url_arg(ctx.qry.search);
|
|
|
|
}
|
|
|
|
htmlf("'>%s</a></th>", title);
|
|
|
|
}
|
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static void print_header()
|
2008-04-13 09:57:10 +00:00
|
|
|
{
|
2008-11-29 13:08:51 +00:00
|
|
|
html("<tr class='nohover'>");
|
|
|
|
print_sort_header("Name", "name");
|
|
|
|
print_sort_header("Description", "desc");
|
2013-02-01 09:59:13 +00:00
|
|
|
if (ctx.cfg.enable_index_owner)
|
|
|
|
print_sort_header("Owner", "owner");
|
2008-11-29 13:08:51 +00:00
|
|
|
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
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static 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;
|
2013-03-07 07:56:22 +00:00
|
|
|
html("<ul class='pager'>");
|
2013-03-04 04:21:33 +00:00
|
|
|
for (i = 0, ofs = 0; ofs < items; i++, ofs = i * pagelen) {
|
2012-10-08 16:49:34 +00:00
|
|
|
class = (ctx.qry.ofs == ofs) ? "current" : NULL;
|
2013-03-07 07:56:22 +00:00
|
|
|
html("<li>");
|
2013-03-03 15:04:29 +00:00
|
|
|
cgit_index_link(fmt("[%d]", i + 1), fmt("Page %d", i + 1),
|
|
|
|
class, search, sort, ofs);
|
2013-03-07 07:56:22 +00:00
|
|
|
html("</li>");
|
2012-10-08 16:49:34 +00:00
|
|
|
}
|
2013-03-07 07:56:22 +00:00
|
|
|
html("</ul>");
|
2008-05-03 08:54:39 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2012-10-16 21:32:40 +00:00
|
|
|
if (!strcmp(ctx.cfg.repository_sort, "age")) {
|
2012-07-12 17:13:39 +00:00
|
|
|
// 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}
|
|
|
|
};
|
|
|
|
|
2013-03-04 07:52:33 +00:00
|
|
|
static int sort_repolist(char *field)
|
2008-11-29 13:08:51 +00:00
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2013-02-01 09:59:13 +00:00
|
|
|
int i, columns = 3, 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)
|
2013-02-01 09:59:13 +00:00
|
|
|
++columns;
|
|
|
|
if (ctx.cfg.enable_index_owner)
|
|
|
|
++columns;
|
2008-04-13 09:57:10 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-03-04 04:21:33 +00:00
|
|
|
if (ctx.qry.sort)
|
2008-11-29 13:08:51 +00:00
|
|
|
sorted = sort_repolist(ctx.qry.sort);
|
2012-10-09 18:53:29 +00:00
|
|
|
else if (ctx.cfg.section_sort)
|
2009-08-23 21:09:31 +00:00
|
|
|
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'>");
|
2013-03-03 15:04:29 +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++)
|
2013-02-01 09:59:13 +00:00
|
|
|
print_header();
|
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>");
|
2013-02-01 09:59:13 +00:00
|
|
|
if (ctx.cfg.enable_index_owner) {
|
|
|
|
html_txt(ctx.repo->owner);
|
|
|
|
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;
|
2013-05-25 12:50:19 +00:00
|
|
|
if (ctx.cfg.about_filter) {
|
|
|
|
ctx.cfg.about_filter->argv[1] = ctx.cfg.root_readme;
|
2011-06-06 19:29:58 +00:00
|
|
|
cgit_open_filter(ctx.cfg.about_filter);
|
2013-05-25 12:50:19 +00:00
|
|
|
}
|
2009-08-09 11:27:21 +00:00
|
|
|
html_include(ctx.cfg.root_readme);
|
2013-05-25 12:50:19 +00:00
|
|
|
if (ctx.cfg.about_filter) {
|
2009-08-09 11:27:21 +00:00
|
|
|
cgit_close_filter(ctx.cfg.about_filter);
|
2013-05-25 12:50:19 +00:00
|
|
|
ctx.cfg.about_filter->argv[1] = NULL;
|
|
|
|
}
|
2008-04-28 23:06:30 +00:00
|
|
|
}
|