]>
git.cameronkatri.com Git - cgit.git/blob - ui-repolist.c
1 /* ui-repolist.c: functions for generating the repolist page
3 * Copyright (C) 2006 Lars Hjemli
4 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
11 #include "ui-repolist.h"
13 #include "ui-shared.h"
16 static time_t read_agefile(char *path
)
23 if (readfile(path
, &buf
, &size
))
26 if (parse_date(buf
, buf2
, sizeof(buf2
)) > 0)
27 result
= strtoul(buf2
, NULL
, 10);
34 static int get_repo_modtime(const struct cgit_repo
*repo
, time_t *mtime
)
36 struct strbuf path
= STRBUF_INIT
;
38 struct cgit_repo
*r
= (struct cgit_repo
*)repo
;
40 if (repo
->mtime
!= -1) {
44 strbuf_addf(&path
, "%s/%s", repo
->path
, ctx
.cfg
.agefile
);
45 if (stat(path
.buf
, &s
) == 0) {
46 *mtime
= read_agefile(path
.buf
);
54 strbuf_addf(&path
, "%s/refs/heads/%s", repo
->path
,
55 repo
->defbranch
? repo
->defbranch
: "master");
56 if (stat(path
.buf
, &s
) == 0) {
63 strbuf_addf(&path
, "%s/%s", repo
->path
, "packed-refs");
64 if (stat(path
.buf
, &s
) == 0) {
73 strbuf_release(&path
);
74 return (r
->mtime
!= 0);
77 static void print_modtime(struct cgit_repo
*repo
)
80 if (get_repo_modtime(repo
, &t
))
81 cgit_print_age(t
, -1, NULL
);
84 static int is_match(struct cgit_repo
*repo
)
88 if (repo
->url
&& strcasestr(repo
->url
, ctx
.qry
.search
))
90 if (repo
->name
&& strcasestr(repo
->name
, ctx
.qry
.search
))
92 if (repo
->desc
&& strcasestr(repo
->desc
, ctx
.qry
.search
))
94 if (repo
->owner
&& strcasestr(repo
->owner
, ctx
.qry
.search
))
99 static int is_in_url(struct cgit_repo
*repo
)
103 if (repo
->url
&& !prefixcmp(repo
->url
, ctx
.qry
.url
))
108 static void print_sort_header(const char *title
, const char *sort
)
110 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort
);
111 if (ctx
.qry
.search
) {
113 html_url_arg(ctx
.qry
.search
);
115 htmlf("'>%s</a></th>", title
);
118 static void print_header()
120 html("<tr class='nohover'>");
121 print_sort_header("Name", "name");
122 print_sort_header("Description", "desc");
123 if (ctx
.cfg
.enable_index_owner
)
124 print_sort_header("Owner", "owner");
125 print_sort_header("Idle", "idle");
126 if (ctx
.cfg
.enable_index_links
)
127 html("<th class='left'>Links</th>");
132 static void print_pager(int items
, int pagelen
, char *search
, char *sort
)
136 html("<ul class='pager'>");
137 for (i
= 0, ofs
= 0; ofs
< items
; i
++, ofs
= i
* pagelen
) {
138 class = (ctx
.qry
.ofs
== ofs
) ? "current" : NULL
;
140 cgit_index_link(fmt("[%d]", i
+ 1), fmt("Page %d", i
+ 1),
141 class, search
, sort
, ofs
);
147 static int cmp(const char *s1
, const char *s2
)
150 if (ctx
.cfg
.case_sensitive_sort
)
151 return strcmp(s1
, s2
);
153 return strcasecmp(s1
, s2
);
162 static int sort_section(const void *a
, const void *b
)
164 const struct cgit_repo
*r1
= a
;
165 const struct cgit_repo
*r2
= b
;
169 result
= cmp(r1
->section
, r2
->section
);
171 if (!strcmp(ctx
.cfg
.repository_sort
, "age")) {
172 // get_repo_modtime caches the value in r->mtime, so we don't
173 // have to worry about inefficiencies here.
174 if (get_repo_modtime(r1
, &t
) && get_repo_modtime(r2
, &t
))
175 result
= r2
->mtime
- r1
->mtime
;
178 result
= cmp(r1
->name
, r2
->name
);
183 static int sort_name(const void *a
, const void *b
)
185 const struct cgit_repo
*r1
= a
;
186 const struct cgit_repo
*r2
= b
;
188 return cmp(r1
->name
, r2
->name
);
191 static int sort_desc(const void *a
, const void *b
)
193 const struct cgit_repo
*r1
= a
;
194 const struct cgit_repo
*r2
= b
;
196 return cmp(r1
->desc
, r2
->desc
);
199 static int sort_owner(const void *a
, const void *b
)
201 const struct cgit_repo
*r1
= a
;
202 const struct cgit_repo
*r2
= b
;
204 return cmp(r1
->owner
, r2
->owner
);
207 static int sort_idle(const void *a
, const void *b
)
209 const struct cgit_repo
*r1
= a
;
210 const struct cgit_repo
*r2
= b
;
214 get_repo_modtime(r1
, &t1
);
215 get_repo_modtime(r2
, &t2
);
221 int (*fn
)(const void *a
, const void *b
);
224 struct sortcolumn sortcolumn
[] = {
225 {"section", sort_section
},
228 {"owner", sort_owner
},
233 static int sort_repolist(char *field
)
235 struct sortcolumn
*column
;
237 for (column
= &sortcolumn
[0]; column
->name
; column
++) {
238 if (strcmp(field
, column
->name
))
240 qsort(cgit_repolist
.repos
, cgit_repolist
.count
,
241 sizeof(struct cgit_repo
), column
->fn
);
248 void cgit_print_repolist()
250 int i
, columns
= 3, hits
= 0, header
= 0;
251 char *last_section
= NULL
;
255 if (ctx
.cfg
.enable_index_links
)
257 if (ctx
.cfg
.enable_index_owner
)
260 ctx
.page
.title
= ctx
.cfg
.root_title
;
261 cgit_print_http_headers(&ctx
);
262 cgit_print_docstart(&ctx
);
263 cgit_print_pageheader(&ctx
);
265 if (ctx
.cfg
.index_header
)
266 html_include(ctx
.cfg
.index_header
);
269 sorted
= sort_repolist(ctx
.qry
.sort
);
270 else if (ctx
.cfg
.section_sort
)
271 sort_repolist("section");
273 html("<table summary='repository list' class='list nowrap'>");
274 for (i
= 0; i
< cgit_repolist
.count
; i
++) {
275 ctx
.repo
= &cgit_repolist
.repos
[i
];
276 if (!(is_match(ctx
.repo
) && is_in_url(ctx
.repo
)))
279 if (hits
<= ctx
.qry
.ofs
)
281 if (hits
> ctx
.qry
.ofs
+ ctx
.cfg
.max_repo_count
)
285 section
= ctx
.repo
->section
;
286 if (section
&& !strcmp(section
, ""))
289 ((last_section
== NULL
&& section
!= NULL
) ||
290 (last_section
!= NULL
&& section
== NULL
) ||
291 (last_section
!= NULL
&& section
!= NULL
&&
292 strcmp(section
, last_section
)))) {
293 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
297 last_section
= section
;
299 htmlf("<tr><td class='%s'>",
300 !sorted
&& section
? "sublevel-repo" : "toplevel-repo");
301 cgit_summary_link(ctx
.repo
->name
, ctx
.repo
->name
, NULL
, NULL
);
303 html_link_open(cgit_repourl(ctx
.repo
->url
), NULL
, NULL
);
304 html_ntxt(ctx
.cfg
.max_repodesc_len
, ctx
.repo
->desc
);
307 if (ctx
.cfg
.enable_index_owner
) {
308 html_txt(ctx
.repo
->owner
);
311 print_modtime(ctx
.repo
);
313 if (ctx
.cfg
.enable_index_links
) {
315 cgit_summary_link("summary", NULL
, "button", NULL
);
316 cgit_log_link("log", NULL
, "button", NULL
, NULL
, NULL
,
317 0, NULL
, NULL
, ctx
.qry
.showmsg
);
318 cgit_tree_link("tree", NULL
, "button", NULL
, NULL
, NULL
);
325 cgit_print_error("No repositories found");
326 else if (hits
> ctx
.cfg
.max_repo_count
)
327 print_pager(hits
, ctx
.cfg
.max_repo_count
, ctx
.qry
.search
, ctx
.qry
.sort
);
331 void cgit_print_site_readme()
333 if (!ctx
.cfg
.root_readme
)
335 if (ctx
.cfg
.about_filter
) {
336 ctx
.cfg
.about_filter
->argv
[1] = ctx
.cfg
.root_readme
;
337 cgit_open_filter(ctx
.cfg
.about_filter
);
339 html_include(ctx
.cfg
.root_readme
);
340 if (ctx
.cfg
.about_filter
) {
341 cgit_close_filter(ctx
.cfg
.about_filter
);
342 ctx
.cfg
.about_filter
->argv
[1] = NULL
;