]>
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
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 /* This is needed for strcasestr to be defined by <string.h> */
17 #include "ui-shared.h"
19 time_t read_agefile(char *path
)
26 if (readfile(path
, &buf
, &size
))
29 if (parse_date(buf
, buf2
, sizeof(buf2
)))
30 result
= strtoul(buf2
, NULL
, 10);
37 static int get_repo_modtime(const struct cgit_repo
*repo
, time_t *mtime
)
41 struct cgit_repo
*r
= (struct cgit_repo
*)repo
;
43 if (repo
->mtime
!= -1) {
47 path
= fmt("%s/%s", repo
->path
, ctx
.cfg
.agefile
);
48 if (stat(path
, &s
) == 0) {
49 *mtime
= read_agefile(path
);
54 path
= fmt("%s/refs/heads/%s", repo
->path
, repo
->defbranch
);
55 if (stat(path
, &s
) == 0)
61 return (r
->mtime
!= 0);
64 static void print_modtime(struct cgit_repo
*repo
)
67 if (get_repo_modtime(repo
, &t
))
68 cgit_print_age(t
, -1, NULL
);
71 int is_match(struct cgit_repo
*repo
)
75 if (repo
->url
&& strcasestr(repo
->url
, ctx
.qry
.search
))
77 if (repo
->name
&& strcasestr(repo
->name
, ctx
.qry
.search
))
79 if (repo
->desc
&& strcasestr(repo
->desc
, ctx
.qry
.search
))
81 if (repo
->owner
&& strcasestr(repo
->owner
, ctx
.qry
.search
))
86 int is_in_url(struct cgit_repo
*repo
)
90 if (repo
->url
&& !prefixcmp(repo
->url
, ctx
.qry
.url
))
95 void print_sort_header(const char *title
, const char *sort
)
97 htmlf("<th class='left'><a href='%s?s=%s", cgit_rooturl(), sort
);
100 html_url_arg(ctx
.qry
.search
);
102 htmlf("'>%s</a></th>", title
);
105 void print_header(int columns
)
107 html("<tr class='nohover'>");
108 print_sort_header("Name", "name");
109 print_sort_header("Description", "desc");
110 print_sort_header("Owner", "owner");
111 print_sort_header("Idle", "idle");
112 if (ctx
.cfg
.enable_index_links
)
113 html("<th class='left'>Links</th>");
118 void print_pager(int items
, int pagelen
, char *search
)
121 html("<div class='pager'>");
122 for(i
= 0; i
* pagelen
< items
; i
++)
123 cgit_index_link(fmt("[%d]", i
+1), fmt("Page %d", i
+1), NULL
,
124 search
, i
* pagelen
);
128 static int cmp(const char *s1
, const char *s2
)
131 return strcmp(s1
, s2
);
139 static int sort_section(const void *a
, const void *b
)
141 const struct cgit_repo
*r1
= a
;
142 const struct cgit_repo
*r2
= b
;
145 result
= cmp(r1
->section
, r2
->section
);
147 result
= cmp(r1
->name
, r2
->name
);
151 static int sort_name(const void *a
, const void *b
)
153 const struct cgit_repo
*r1
= a
;
154 const struct cgit_repo
*r2
= b
;
156 return cmp(r1
->name
, r2
->name
);
159 static int sort_desc(const void *a
, const void *b
)
161 const struct cgit_repo
*r1
= a
;
162 const struct cgit_repo
*r2
= b
;
164 return cmp(r1
->desc
, r2
->desc
);
167 static int sort_owner(const void *a
, const void *b
)
169 const struct cgit_repo
*r1
= a
;
170 const struct cgit_repo
*r2
= b
;
172 return cmp(r1
->owner
, r2
->owner
);
175 static int sort_idle(const void *a
, const void *b
)
177 const struct cgit_repo
*r1
= a
;
178 const struct cgit_repo
*r2
= b
;
182 get_repo_modtime(r1
, &t1
);
183 get_repo_modtime(r2
, &t2
);
189 int (*fn
)(const void *a
, const void *b
);
192 struct sortcolumn sortcolumn
[] = {
193 {"section", sort_section
},
196 {"owner", sort_owner
},
201 int sort_repolist(char *field
)
203 struct sortcolumn
*column
;
205 for (column
= &sortcolumn
[0]; column
->name
; column
++) {
206 if (strcmp(field
, column
->name
))
208 qsort(cgit_repolist
.repos
, cgit_repolist
.count
,
209 sizeof(struct cgit_repo
), column
->fn
);
216 void cgit_print_repolist()
218 int i
, columns
= 4, hits
= 0, header
= 0;
219 char *last_section
= NULL
;
223 if (ctx
.cfg
.enable_index_links
)
226 ctx
.page
.title
= ctx
.cfg
.root_title
;
227 cgit_print_http_headers(&ctx
);
228 cgit_print_docstart(&ctx
);
229 cgit_print_pageheader(&ctx
);
231 if (ctx
.cfg
.index_header
)
232 html_include(ctx
.cfg
.index_header
);
235 sorted
= sort_repolist(ctx
.qry
.sort
);
237 sort_repolist("section");
239 html("<table summary='repository list' class='list nowrap'>");
240 for (i
=0; i
<cgit_repolist
.count
; i
++) {
241 ctx
.repo
= &cgit_repolist
.repos
[i
];
242 if (!(is_match(ctx
.repo
) && is_in_url(ctx
.repo
)))
245 if (hits
<= ctx
.qry
.ofs
)
247 if (hits
> ctx
.qry
.ofs
+ ctx
.cfg
.max_repo_count
)
250 print_header(columns
);
251 section
= ctx
.repo
->section
;
252 if (section
&& !strcmp(section
, ""))
255 ((last_section
== NULL
&& section
!= NULL
) ||
256 (last_section
!= NULL
&& section
== NULL
) ||
257 (last_section
!= NULL
&& section
!= NULL
&&
258 strcmp(section
, last_section
)))) {
259 htmlf("<tr class='nohover'><td colspan='%d' class='reposection'>",
263 last_section
= section
;
265 htmlf("<tr><td class='%s'>",
266 !sorted
&& section
? "sublevel-repo" : "toplevel-repo");
267 cgit_summary_link(ctx
.repo
->name
, ctx
.repo
->name
, NULL
, NULL
);
269 html_link_open(cgit_repourl(ctx
.repo
->url
), NULL
, NULL
);
270 html_ntxt(ctx
.cfg
.max_repodesc_len
, ctx
.repo
->desc
);
273 html_txt(ctx
.repo
->owner
);
275 print_modtime(ctx
.repo
);
277 if (ctx
.cfg
.enable_index_links
) {
279 cgit_summary_link("summary", NULL
, "button", NULL
);
280 cgit_log_link("log", NULL
, "button", NULL
, NULL
, NULL
,
281 0, NULL
, NULL
, ctx
.qry
.showmsg
);
282 cgit_tree_link("tree", NULL
, "button", NULL
, NULL
, NULL
);
289 cgit_print_error("No repositories found");
290 else if (hits
> ctx
.cfg
.max_repo_count
)
291 print_pager(hits
, ctx
.cfg
.max_repo_count
, ctx
.qry
.search
);
295 void cgit_print_site_readme()
297 if (!ctx
.cfg
.root_readme
)
299 if (ctx
.cfg
.about_filter
)
300 cgit_open_filter(ctx
.cfg
.about_filter
);
301 html_include(ctx
.cfg
.root_readme
);
302 if (ctx
.cfg
.about_filter
)
303 cgit_close_filter(ctx
.cfg
.about_filter
);