2006-12-11 16:48:03 +01:00
|
|
|
/* ui-repolist.c: functions for generating the repolist page
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Lars Hjemli
|
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2008-12-05 19:04:17 +01:00
|
|
|
/* This is needed for strcasestr to be defined by <string.h> */
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-05-22 23:25:25 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
2008-02-23 22:45:33 +01:00
|
|
|
#include "cgit.h"
|
|
|
|
#include "html.h"
|
2008-03-24 16:50:57 +01:00
|
|
|
#include "ui-shared.h"
|
2007-05-22 23:25:25 +02:00
|
|
|
|
|
|
|
time_t read_agefile(char *path)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
static char buf[64], buf2[64];
|
|
|
|
|
|
|
|
if (!(f = fopen(path, "r")))
|
|
|
|
return -1;
|
2009-03-07 01:06:24 +01:00
|
|
|
buf[0] = 0;
|
2008-11-14 09:29:22 +01:00
|
|
|
if (fgets(buf, sizeof(buf), f) == NULL)
|
|
|
|
return -1;
|
2007-05-22 23:25:25 +02:00
|
|
|
fclose(f);
|
|
|
|
if (parse_date(buf, buf2, sizeof(buf2)))
|
|
|
|
return strtoul(buf2, NULL, 10);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-29 13:33:02 +01:00
|
|
|
static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
|
2007-05-22 23:25:25 +02:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
struct stat s;
|
2008-11-29 16:46:37 +01:00
|
|
|
struct cgit_repo *r = (struct cgit_repo *)repo;
|
2007-05-22 23:25:25 +02:00
|
|
|
|
2008-11-29 16:46:37 +01:00
|
|
|
if (repo->mtime != -1) {
|
|
|
|
*mtime = repo->mtime;
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-16 13:07:13 +01:00
|
|
|
path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
|
2007-05-22 23:25:25 +02:00
|
|
|
if (stat(path, &s) == 0) {
|
2008-11-29 13:33:02 +01:00
|
|
|
*mtime = read_agefile(path);
|
2008-11-29 16:46:37 +01:00
|
|
|
r->mtime = *mtime;
|
2008-11-29 13:33:02 +01:00
|
|
|
return 1;
|
2007-05-22 23:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
|
2008-11-29 16:46:37 +01:00
|
|
|
if (stat(path, &s) == 0)
|
2008-11-29 13:33:02 +01:00
|
|
|
*mtime = s.st_mtime;
|
2008-11-29 16:46:37 +01:00
|
|
|
else
|
|
|
|
*mtime = 0;
|
|
|
|
|
|
|
|
r->mtime = *mtime;
|
|
|
|
return (r->mtime != 0);
|
2008-11-29 13:33:02 +01: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 23:25:25 +02:00
|
|
|
}
|
2006-12-11 16:48:03 +01:00
|
|
|
|
2008-04-13 11:57:10 +02:00
|
|
|
int is_match(struct cgit_repo *repo)
|
2006-12-11 16:48:03 +01:00
|
|
|
{
|
2008-04-13 11:57:10 +02:00
|
|
|
if (!ctx.qry.search)
|
|
|
|
return 1;
|
2008-04-13 12:42:27 +02:00
|
|
|
if (repo->url && strcasestr(repo->url, ctx.qry.search))
|
2008-04-13 11:57:10 +02:00
|
|
|
return 1;
|
2008-04-13 12:42:27 +02:00
|
|
|
if (repo->name && strcasestr(repo->name, ctx.qry.search))
|
2008-04-13 11:57:10 +02:00
|
|
|
return 1;
|
2008-04-13 12:42:27 +02:00
|
|
|
if (repo->desc && strcasestr(repo->desc, ctx.qry.search))
|
2008-04-13 11:57:10 +02:00
|
|
|
return 1;
|
2008-04-13 12:42:27 +02:00
|
|
|
if (repo->owner && strcasestr(repo->owner, ctx.qry.search))
|
2008-04-13 11:57:10 +02:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-11 16:48:03 +01:00
|
|
|
|
2008-09-14 20:18:10 +02: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 14:08:51 +01:00
|
|
|
void print_sort_header(const char *title, const char *sort)
|
|
|
|
{
|
|
|
|
htmlf("<th class='left'><a href='./?s=%s", sort);
|
|
|
|
if (ctx.qry.search) {
|
|
|
|
html("&q=");
|
|
|
|
html_url_arg(ctx.qry.search);
|
|
|
|
}
|
|
|
|
htmlf("'>%s</a></th>", title);
|
|
|
|
}
|
|
|
|
|
2008-04-13 11:57:10 +02:00
|
|
|
void print_header(int columns)
|
|
|
|
{
|
2008-11-29 14:08:51 +01: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 13:07:13 +01:00
|
|
|
if (ctx.cfg.enable_index_links)
|
2008-04-13 10:57:11 +02:00
|
|
|
html("<th class='left'>Links</th>");
|
2007-06-19 00:56:40 +02:00
|
|
|
html("</tr>\n");
|
2008-04-13 11:57:10 +02:00
|
|
|
}
|
2006-12-11 16:48:03 +01:00
|
|
|
|
2008-05-03 10:54:39 +02:00
|
|
|
|
|
|
|
void print_pager(int items, int pagelen, char *search)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
html("<div class='pager'>");
|
|
|
|
for(i = 0; i * pagelen < items; i++)
|
|
|
|
cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL,
|
|
|
|
search, i * pagelen);
|
|
|
|
html("</div>");
|
|
|
|
}
|
|
|
|
|
2008-11-29 14:08:51 +01:00
|
|
|
static int cmp(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
if (s1 && s2)
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
if (s1 && !s2)
|
|
|
|
return -1;
|
2008-11-29 14:27:35 +01:00
|
|
|
if (s2 && !s1)
|
|
|
|
return 1;
|
2008-11-29 14:08:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:25:35 +01:00
|
|
|
{
|
|
|
|
const struct cgit_repo *r1 = a;
|
|
|
|
const struct cgit_repo *r2 = b;
|
|
|
|
time_t t1, t2;
|
|
|
|
|
2008-11-29 13:33:02 +01:00
|
|
|
t1 = t2 = 0;
|
|
|
|
get_repo_modtime(r1, &t1);
|
|
|
|
get_repo_modtime(r2, &t2);
|
|
|
|
return t2 - t1;
|
2008-11-25 15:25:35 +01:00
|
|
|
}
|
|
|
|
|
2008-11-29 14:08:51 +01:00
|
|
|
struct sortcolumn {
|
|
|
|
const char *name;
|
|
|
|
int (*fn)(const void *a, const void *b);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sortcolumn sortcolumn[] = {
|
|
|
|
{"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 11:57:10 +02:00
|
|
|
void cgit_print_repolist()
|
|
|
|
{
|
|
|
|
int i, columns = 4, hits = 0, header = 0;
|
|
|
|
char *last_group = NULL;
|
2008-11-29 14:08:51 +01:00
|
|
|
int sorted = 0;
|
2008-04-13 11:57:10 +02: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-29 00:35:49 +02:00
|
|
|
if (ctx.cfg.index_header)
|
|
|
|
html_include(ctx.cfg.index_header);
|
|
|
|
|
2008-11-25 15:25:35 +01:00
|
|
|
if(ctx.qry.sort)
|
2008-11-29 14:08:51 +01:00
|
|
|
sorted = sort_repolist(ctx.qry.sort);
|
2008-11-25 15:25:35 +01:00
|
|
|
|
2008-04-13 11:57:10 +02:00
|
|
|
html("<table summary='repository list' class='list nowrap'>");
|
2007-02-03 15:02:55 +01:00
|
|
|
for (i=0; i<cgit_repolist.count; i++) {
|
2008-02-16 13:56:09 +01:00
|
|
|
ctx.repo = &cgit_repolist.repos[i];
|
2008-09-14 20:18:10 +02:00
|
|
|
if (!(is_match(ctx.repo) && is_in_url(ctx.repo)))
|
2008-04-13 11:57:10 +02:00
|
|
|
continue;
|
2008-05-03 10:54:39 +02:00
|
|
|
hits++;
|
|
|
|
if (hits <= ctx.qry.ofs)
|
|
|
|
continue;
|
|
|
|
if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
|
|
|
|
continue;
|
2008-04-13 11:57:10 +02:00
|
|
|
if (!header++)
|
|
|
|
print_header(columns);
|
2008-11-29 14:08:51 +01:00
|
|
|
if (!sorted &&
|
2008-11-25 15:25:35 +01:00
|
|
|
((last_group == NULL && ctx.repo->group != NULL) ||
|
2008-02-16 13:56:09 +01:00
|
|
|
(last_group != NULL && ctx.repo->group == NULL) ||
|
|
|
|
(last_group != NULL && ctx.repo->group != NULL &&
|
2008-11-25 15:25:35 +01:00
|
|
|
strcmp(ctx.repo->group, last_group)))) {
|
2007-06-19 00:56:40 +02:00
|
|
|
htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
|
|
|
|
columns);
|
2008-02-16 13:56:09 +01:00
|
|
|
html_txt(ctx.repo->group);
|
2007-05-18 22:48:22 +02:00
|
|
|
html("</td></tr>");
|
2008-02-16 13:56:09 +01:00
|
|
|
last_group = ctx.repo->group;
|
2007-05-18 22:48:22 +02:00
|
|
|
}
|
2007-05-21 00:14:28 +02:00
|
|
|
htmlf("<tr><td class='%s'>",
|
2008-11-29 14:08:51 +01:00
|
|
|
!sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
|
2008-10-05 17:16:36 +02:00
|
|
|
cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
|
2006-12-11 16:48:03 +01:00
|
|
|
html("</td><td>");
|
2008-04-14 22:23:48 +02:00
|
|
|
html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
|
2008-02-16 13:56:09 +01:00
|
|
|
html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
|
2008-04-14 22:23:48 +02:00
|
|
|
html_link_close();
|
2006-12-11 16:48:03 +01:00
|
|
|
html("</td><td>");
|
2008-02-16 13:56:09 +01:00
|
|
|
html_txt(ctx.repo->owner);
|
2007-05-16 02:12:06 +02:00
|
|
|
html("</td><td>");
|
2008-02-16 13:56:09 +01:00
|
|
|
print_modtime(ctx.repo);
|
2007-06-19 00:56:40 +02:00
|
|
|
html("</td>");
|
2008-02-16 13:07:13 +01:00
|
|
|
if (ctx.cfg.enable_index_links) {
|
2007-06-19 00:56:40 +02:00
|
|
|
html("<td>");
|
2008-10-05 17:16:36 +02:00
|
|
|
cgit_summary_link("summary", NULL, "button", NULL);
|
2007-11-03 10:42:37 +01:00
|
|
|
cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
|
2008-11-29 18:39:41 +01:00
|
|
|
0, NULL, NULL, ctx.qry.showmsg);
|
2007-06-19 00:56:40 +02:00
|
|
|
cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
|
|
|
|
html("</td>");
|
|
|
|
}
|
|
|
|
html("</tr>\n");
|
2006-12-11 16:48:03 +01:00
|
|
|
}
|
|
|
|
html("</table>");
|
2008-04-13 11:57:10 +02:00
|
|
|
if (!hits)
|
|
|
|
cgit_print_error("No repositories found");
|
2008-05-03 10:54:39 +02:00
|
|
|
else if (hits > ctx.cfg.max_repo_count)
|
|
|
|
print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search);
|
2006-12-11 16:48:03 +01:00
|
|
|
cgit_print_docend();
|
|
|
|
}
|
2008-04-29 01:06:30 +02:00
|
|
|
|
|
|
|
void cgit_print_site_readme()
|
|
|
|
{
|
|
|
|
if (ctx.cfg.root_readme)
|
|
|
|
html_include(ctx.cfg.root_readme);
|
|
|
|
}
|