]>
git.cameronkatri.com Git - cgit.git/blob - ui-repolist.c
1 /* ui-repolist.c: functions for generating the repolist page
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
10 #include "ui-repolist.h"
12 #include "ui-shared.h"
14 static time_t read_agefile(char *path
)
19 struct strbuf date_buf
= STRBUF_INIT
;
21 if (readfile(path
, &buf
, &size
)) {
26 if (parse_date(buf
, &date_buf
) == 0)
27 result
= strtoul(date_buf
.buf
, NULL
, 10);
31 strbuf_release(&date_buf
);
35 static int get_repo_modtime(const struct cgit_repo
*repo
, time_t *mtime
)
37 struct strbuf path
= STRBUF_INIT
;
39 struct cgit_repo
*r
= (struct cgit_repo
*)repo
;
41 if (repo
->mtime
!= -1) {
45 strbuf_addf(&path
, "%s/%s", repo
->path
, ctx
.cfg
.agefile
);
46 if (stat(path
.buf
, &s
) == 0) {
47 *mtime
= read_agefile(path
.buf
);
55 strbuf_addf(&path
, "%s/refs/heads/%s", repo
->path
,
56 repo
->defbranch
? repo
->defbranch
: "master");
57 if (stat(path
.buf
, &s
) == 0) {
64 strbuf_addf(&path
, "%s/%s", repo
->path
, "packed-refs");
65 if (stat(path
.buf
, &s
) == 0) {
74 strbuf_release(&path
);
75 return (r
->mtime
!= 0);
78 static void print_modtime(struct cgit_repo
*repo
)
81 if (get_repo_modtime(repo
, &t
))
82 cgit_print_age(t
, 0, -1);
85 static int is_match(struct cgit_repo
*repo
)
89 if (repo
->url
&& strcasestr(repo
->url
, ctx
.qry
.search
))
91 if (repo
->name
&& strcasestr(repo
->name
, ctx
.qry
.search
))
93 if (repo
->desc
&& strcasestr(repo
->desc
, ctx
.qry
.search
))
95 if (repo
->owner
&& strcasestr(repo
->owner
, ctx
.qry
.search
))
100 static int is_in_url(struct cgit_repo
*repo
)
104 if (repo
->url
&& starts_with(repo
->url
, ctx
.qry
.url
))
109 static int is_visible(struct cgit_repo
*repo
)
111 if (repo
->hide
|| repo
->ignore
)
113 if (!(is_match(repo
) && is_in_url(repo
)))
118 static int any_repos_visible(void)
122 for (i
= 0; i
< cgit_repolist
.count
; i
++) {
123 if (is_visible(&cgit_repolist
.repos
[i
]))
129 static void print_sort_header(const char *title
, const char *sort
)
131 char *currenturl
= cgit_currenturl();
132 html("<th class='left'><a href='");
133 html_attr(currenturl
);
134 htmlf("?s=%s", sort
);
135 if (ctx
.qry
.search
) {
137 html_url_arg(ctx
.qry
.search
);
139 htmlf("'>%s</a></th>", title
);
143 static void print_header(void)
145 html("<tr class='nohover'>");
146 print_sort_header("Name", "name");
147 print_sort_header("Description", "desc");
148 if (ctx
.cfg
.enable_index_owner
)
149 print_sort_header("Owner", "owner");
150 print_sort_header("Idle", "idle");
151 if (ctx
.cfg
.enable_index_links
)
152 html("<th class='left'>Links</th>");
157 static void print_pager(int items
, int pagelen
, char *search
, char *sort
)
161 html("<ul class='pager'>");
162 for (i
= 0, ofs
= 0; ofs
< items
; i
++, ofs
= i
* pagelen
) {
163 class = (ctx
.qry
.ofs
== ofs
) ? "current" : NULL
;
165 cgit_index_link(fmt("[%d]", i
+ 1), fmt("Page %d", i
+ 1),
166 class, search
, sort
, ofs
, 0);
172 static int cmp(const char *s1
, const char *s2
)
175 if (ctx
.cfg
.case_sensitive_sort
)
176 return strcmp(s1
, s2
);
178 return strcasecmp(s1
, s2
);
187 static int sort_name(const void *a
, const void *b
)
189 const struct cgit_repo
*r1
= a
;
190 const struct cgit_repo
*r2
= b
;
192 return cmp(r1
->name
, r2
->name
);
195 static int sort_desc(const void *a
, const void *b
)
197 const struct cgit_repo
*r1
= a
;
198 const struct cgit_repo
*r2
= b
;
200 return cmp(r1
->desc
, r2
->desc
);
203 static int sort_owner(const void *a
, const void *b
)
205 const struct cgit_repo
*r1
= a
;
206 const struct cgit_repo
*r2
= b
;
208 return cmp(r1
->owner
, r2
->owner
);
211 static int sort_idle(const void *a
, const void *b
)
213 const struct cgit_repo
*r1
= a
;
214 const struct cgit_repo
*r2
= b
;
218 get_repo_modtime(r1
, &t1
);
219 get_repo_modtime(r2
, &t2
);
223 static int sort_section(const void *a
, const void *b
)
225 const struct cgit_repo
*r1
= a
;
226 const struct cgit_repo
*r2
= b
;
229 result
= cmp(r1
->section
, r2
->section
);
231 if (!strcmp(ctx
.cfg
.repository_sort
, "age"))
232 result
= sort_idle(r1
, r2
);
234 result
= cmp(r1
->name
, r2
->name
);
241 int (*fn
)(const void *a
, const void *b
);
244 static const struct sortcolumn sortcolumn
[] = {
245 {"section", sort_section
},
248 {"owner", sort_owner
},
253 static int sort_repolist(char *field
)
255 const struct sortcolumn
*column
;
257 for (column
= &sortcolumn
[0]; column
->name
; column
++) {
258 if (strcmp(field
, column
->name
))
260 qsort(cgit_repolist
.repos
, cgit_repolist
.count
,
261 sizeof(struct cgit_repo
), column
->fn
);
268 void cgit_print_repolist(void)
270 int i
, columns
= 3, hits
= 0, header
= 0;
271 char *last_section
= NULL
;
276 if (!any_repos_visible()) {
277 cgit_print_error_page(404, "Not found", "No repositories found");
281 if (ctx
.cfg
.enable_index_links
)
283 if (ctx
.cfg
.enable_index_owner
)
286 ctx
.page
.title
= ctx
.cfg
.root_title
;
287 cgit_print_http_headers();
288 cgit_print_docstart();
289 cgit_print_pageheader();
291 if (ctx
.cfg
.index_header
)
292 html_include(ctx
.cfg
.index_header
);
295 sorted
= sort_repolist(ctx
.qry
.sort
);
296 else if (ctx
.cfg
.section_sort
)
297 sort_repolist("section");
299 html("<table summary='repository list' class='list nowrap'>");
300 for (i
= 0; i
< cgit_repolist
.count
; i
++) {
301 ctx
.repo
= &cgit_repolist
.repos
[i
];
302 if (!is_visible(ctx
.repo
))
305 if (hits
<= ctx
.qry
.ofs
)
307 if (hits
> ctx
.qry
.ofs
+ ctx
.cfg
.max_repo_count
)
311 section
= ctx
.repo
->section
;
312 if (section
&& !strcmp(section
, ""))
315 ((last_section
== NULL
&& section
!= NULL
) ||
316 (last_section
!= NULL
&& section
== NULL
) ||
317 (last_section
!= NULL
&& section
!= NULL
&&
318 strcmp(section
, last_section
)))) {
319 htmlf("<tr class='nohover-highlight'><td colspan='%d' class='reposection'>",
323 last_section
= section
;
325 htmlf("<tr><td class='%s'>",
326 !sorted
&& section
? "sublevel-repo" : "toplevel-repo");
327 cgit_summary_link(ctx
.repo
->name
, ctx
.repo
->name
, NULL
, NULL
);
329 repourl
= cgit_repourl(ctx
.repo
->url
);
330 html_link_open(repourl
, NULL
, NULL
);
332 if (html_ntxt(ctx
.repo
->desc
, ctx
.cfg
.max_repodesc_len
) < 0)
336 if (ctx
.cfg
.enable_index_owner
) {
337 if (ctx
.repo
->owner_filter
) {
338 cgit_open_filter(ctx
.repo
->owner_filter
);
339 html_txt(ctx
.repo
->owner
);
340 cgit_close_filter(ctx
.repo
->owner_filter
);
342 char *currenturl
= cgit_currenturl();
344 html_attr(currenturl
);
346 html_url_arg(ctx
.repo
->owner
);
348 html_txt(ctx
.repo
->owner
);
354 print_modtime(ctx
.repo
);
356 if (ctx
.cfg
.enable_index_links
) {
358 cgit_summary_link("summary", NULL
, "button", NULL
);
359 cgit_log_link("log", NULL
, "button", NULL
, NULL
, NULL
,
360 0, NULL
, NULL
, ctx
.qry
.showmsg
, 0);
361 cgit_tree_link("tree", NULL
, "button", NULL
, NULL
, NULL
);
367 if (hits
> ctx
.cfg
.max_repo_count
)
368 print_pager(hits
, ctx
.cfg
.max_repo_count
, ctx
.qry
.search
, ctx
.qry
.sort
);
372 void cgit_print_site_readme(void)
374 cgit_print_layout_start();
375 if (!ctx
.cfg
.root_readme
)
377 cgit_open_filter(ctx
.cfg
.about_filter
, ctx
.cfg
.root_readme
);
378 html_include(ctx
.cfg
.root_readme
);
379 cgit_close_filter(ctx
.cfg
.about_filter
);
381 cgit_print_layout_end();