author | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 13:27:35 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 13:27:35 (UTC) |
commit | 54272e60965ec6a98b49cbf67d72a4b1f5adc55b (patch) (unidiff) | |
tree | 183bc1876d0c53b879627bb9fd66f2978d41929e | |
parent | f250c1ca2ea7f35d65f639e42e8b8f0657515e5d (diff) | |
download | cgit-54272e60965ec6a98b49cbf67d72a4b1f5adc55b.zip cgit-54272e60965ec6a98b49cbf67d72a4b1f5adc55b.tar.gz cgit-54272e60965ec6a98b49cbf67d72a4b1f5adc55b.tar.bz2 |
ui-repolist: sort null values last
When sorting on e.g. owner, it's not interesting to get all repos
without owner at the top of the list.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | ui-repolist.c | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/ui-repolist.c b/ui-repolist.c index 0de328b..cf27cb3 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -1,266 +1,266 @@ | |||
1 | /* ui-repolist.c: functions for generating the repolist page | 1 | /* ui-repolist.c: functions for generating the repolist page |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include <time.h> | 9 | #include <time.h> |
10 | 10 | ||
11 | #include "cgit.h" | 11 | #include "cgit.h" |
12 | #include "html.h" | 12 | #include "html.h" |
13 | #include "ui-shared.h" | 13 | #include "ui-shared.h" |
14 | 14 | ||
15 | time_t read_agefile(char *path) | 15 | time_t read_agefile(char *path) |
16 | { | 16 | { |
17 | FILE *f; | 17 | FILE *f; |
18 | static char buf[64], buf2[64]; | 18 | static char buf[64], buf2[64]; |
19 | 19 | ||
20 | if (!(f = fopen(path, "r"))) | 20 | if (!(f = fopen(path, "r"))) |
21 | return -1; | 21 | return -1; |
22 | if (fgets(buf, sizeof(buf), f) == NULL) | 22 | if (fgets(buf, sizeof(buf), f) == NULL) |
23 | return -1; | 23 | return -1; |
24 | fclose(f); | 24 | fclose(f); |
25 | if (parse_date(buf, buf2, sizeof(buf2))) | 25 | if (parse_date(buf, buf2, sizeof(buf2))) |
26 | return strtoul(buf2, NULL, 10); | 26 | return strtoul(buf2, NULL, 10); |
27 | else | 27 | else |
28 | return 0; | 28 | return 0; |
29 | } | 29 | } |
30 | 30 | ||
31 | static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) | 31 | static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) |
32 | { | 32 | { |
33 | char *path; | 33 | char *path; |
34 | struct stat s; | 34 | struct stat s; |
35 | 35 | ||
36 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); | 36 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); |
37 | if (stat(path, &s) == 0) { | 37 | if (stat(path, &s) == 0) { |
38 | *mtime = read_agefile(path); | 38 | *mtime = read_agefile(path); |
39 | return 1; | 39 | return 1; |
40 | } | 40 | } |
41 | 41 | ||
42 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); | 42 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); |
43 | if (stat(path, &s) == 0) { | 43 | if (stat(path, &s) == 0) { |
44 | *mtime = s.st_mtime; | 44 | *mtime = s.st_mtime; |
45 | return 1; | 45 | return 1; |
46 | } | 46 | } |
47 | return 0; | 47 | return 0; |
48 | } | 48 | } |
49 | 49 | ||
50 | static void print_modtime(struct cgit_repo *repo) | 50 | static void print_modtime(struct cgit_repo *repo) |
51 | { | 51 | { |
52 | time_t t; | 52 | time_t t; |
53 | if (get_repo_modtime(repo, &t)) | 53 | if (get_repo_modtime(repo, &t)) |
54 | cgit_print_age(t, -1, NULL); | 54 | cgit_print_age(t, -1, NULL); |
55 | } | 55 | } |
56 | 56 | ||
57 | int is_match(struct cgit_repo *repo) | 57 | int is_match(struct cgit_repo *repo) |
58 | { | 58 | { |
59 | if (!ctx.qry.search) | 59 | if (!ctx.qry.search) |
60 | return 1; | 60 | return 1; |
61 | if (repo->url && strcasestr(repo->url, ctx.qry.search)) | 61 | if (repo->url && strcasestr(repo->url, ctx.qry.search)) |
62 | return 1; | 62 | return 1; |
63 | if (repo->name && strcasestr(repo->name, ctx.qry.search)) | 63 | if (repo->name && strcasestr(repo->name, ctx.qry.search)) |
64 | return 1; | 64 | return 1; |
65 | if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) | 65 | if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) |
66 | return 1; | 66 | return 1; |
67 | if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) | 67 | if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) |
68 | return 1; | 68 | return 1; |
69 | return 0; | 69 | return 0; |
70 | } | 70 | } |
71 | 71 | ||
72 | int is_in_url(struct cgit_repo *repo) | 72 | int is_in_url(struct cgit_repo *repo) |
73 | { | 73 | { |
74 | if (!ctx.qry.url) | 74 | if (!ctx.qry.url) |
75 | return 1; | 75 | return 1; |
76 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) | 76 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) |
77 | return 1; | 77 | return 1; |
78 | return 0; | 78 | return 0; |
79 | } | 79 | } |
80 | 80 | ||
81 | void print_sort_header(const char *title, const char *sort) | 81 | void print_sort_header(const char *title, const char *sort) |
82 | { | 82 | { |
83 | htmlf("<th class='left'><a href='./?s=%s", sort); | 83 | htmlf("<th class='left'><a href='./?s=%s", sort); |
84 | if (ctx.qry.search) { | 84 | if (ctx.qry.search) { |
85 | html("&q="); | 85 | html("&q="); |
86 | html_url_arg(ctx.qry.search); | 86 | html_url_arg(ctx.qry.search); |
87 | } | 87 | } |
88 | htmlf("'>%s</a></th>", title); | 88 | htmlf("'>%s</a></th>", title); |
89 | } | 89 | } |
90 | 90 | ||
91 | void print_header(int columns) | 91 | void print_header(int columns) |
92 | { | 92 | { |
93 | html("<tr class='nohover'>"); | 93 | html("<tr class='nohover'>"); |
94 | print_sort_header("Name", "name"); | 94 | print_sort_header("Name", "name"); |
95 | print_sort_header("Description", "desc"); | 95 | print_sort_header("Description", "desc"); |
96 | print_sort_header("Owner", "owner"); | 96 | print_sort_header("Owner", "owner"); |
97 | print_sort_header("Idle", "idle"); | 97 | print_sort_header("Idle", "idle"); |
98 | if (ctx.cfg.enable_index_links) | 98 | if (ctx.cfg.enable_index_links) |
99 | html("<th class='left'>Links</th>"); | 99 | html("<th class='left'>Links</th>"); |
100 | html("</tr>\n"); | 100 | html("</tr>\n"); |
101 | } | 101 | } |
102 | 102 | ||
103 | 103 | ||
104 | void print_pager(int items, int pagelen, char *search) | 104 | void print_pager(int items, int pagelen, char *search) |
105 | { | 105 | { |
106 | int i; | 106 | int i; |
107 | html("<div class='pager'>"); | 107 | html("<div class='pager'>"); |
108 | for(i = 0; i * pagelen < items; i++) | 108 | for(i = 0; i * pagelen < items; i++) |
109 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, | 109 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, |
110 | search, i * pagelen); | 110 | search, i * pagelen); |
111 | html("</div>"); | 111 | html("</div>"); |
112 | } | 112 | } |
113 | 113 | ||
114 | static int cmp(const char *s1, const char *s2) | 114 | static int cmp(const char *s1, const char *s2) |
115 | { | 115 | { |
116 | if (s1 && s2) | 116 | if (s1 && s2) |
117 | return strcmp(s1, s2); | 117 | return strcmp(s1, s2); |
118 | if (s1 && !s2) | 118 | if (s1 && !s2) |
119 | return 1; | ||
120 | if (s2 && !s1) | ||
121 | return -1; | 119 | return -1; |
120 | if (s2 && !s1) | ||
121 | return 1; | ||
122 | return 0; | 122 | return 0; |
123 | } | 123 | } |
124 | 124 | ||
125 | static int sort_name(const void *a, const void *b) | 125 | static int sort_name(const void *a, const void *b) |
126 | { | 126 | { |
127 | const struct cgit_repo *r1 = a; | 127 | const struct cgit_repo *r1 = a; |
128 | const struct cgit_repo *r2 = b; | 128 | const struct cgit_repo *r2 = b; |
129 | 129 | ||
130 | return cmp(r1->name, r2->name); | 130 | return cmp(r1->name, r2->name); |
131 | } | 131 | } |
132 | 132 | ||
133 | static int sort_desc(const void *a, const void *b) | 133 | static int sort_desc(const void *a, const void *b) |
134 | { | 134 | { |
135 | const struct cgit_repo *r1 = a; | 135 | const struct cgit_repo *r1 = a; |
136 | const struct cgit_repo *r2 = b; | 136 | const struct cgit_repo *r2 = b; |
137 | 137 | ||
138 | return cmp(r1->desc, r2->desc); | 138 | return cmp(r1->desc, r2->desc); |
139 | } | 139 | } |
140 | 140 | ||
141 | static int sort_owner(const void *a, const void *b) | 141 | static int sort_owner(const void *a, const void *b) |
142 | { | 142 | { |
143 | const struct cgit_repo *r1 = a; | 143 | const struct cgit_repo *r1 = a; |
144 | const struct cgit_repo *r2 = b; | 144 | const struct cgit_repo *r2 = b; |
145 | 145 | ||
146 | return cmp(r1->owner, r2->owner); | 146 | return cmp(r1->owner, r2->owner); |
147 | } | 147 | } |
148 | 148 | ||
149 | static int sort_idle(const void *a, const void *b) | 149 | static int sort_idle(const void *a, const void *b) |
150 | { | 150 | { |
151 | const struct cgit_repo *r1 = a; | 151 | const struct cgit_repo *r1 = a; |
152 | const struct cgit_repo *r2 = b; | 152 | const struct cgit_repo *r2 = b; |
153 | time_t t1, t2; | 153 | time_t t1, t2; |
154 | 154 | ||
155 | t1 = t2 = 0; | 155 | t1 = t2 = 0; |
156 | get_repo_modtime(r1, &t1); | 156 | get_repo_modtime(r1, &t1); |
157 | get_repo_modtime(r2, &t2); | 157 | get_repo_modtime(r2, &t2); |
158 | return t2 - t1; | 158 | return t2 - t1; |
159 | } | 159 | } |
160 | 160 | ||
161 | struct sortcolumn { | 161 | struct sortcolumn { |
162 | const char *name; | 162 | const char *name; |
163 | int (*fn)(const void *a, const void *b); | 163 | int (*fn)(const void *a, const void *b); |
164 | }; | 164 | }; |
165 | 165 | ||
166 | struct sortcolumn sortcolumn[] = { | 166 | struct sortcolumn sortcolumn[] = { |
167 | {"name", sort_name}, | 167 | {"name", sort_name}, |
168 | {"desc", sort_desc}, | 168 | {"desc", sort_desc}, |
169 | {"owner", sort_owner}, | 169 | {"owner", sort_owner}, |
170 | {"idle", sort_idle}, | 170 | {"idle", sort_idle}, |
171 | {NULL, NULL} | 171 | {NULL, NULL} |
172 | }; | 172 | }; |
173 | 173 | ||
174 | int sort_repolist(char *field) | 174 | int sort_repolist(char *field) |
175 | { | 175 | { |
176 | struct sortcolumn *column; | 176 | struct sortcolumn *column; |
177 | 177 | ||
178 | for (column = &sortcolumn[0]; column->name; column++) { | 178 | for (column = &sortcolumn[0]; column->name; column++) { |
179 | if (strcmp(field, column->name)) | 179 | if (strcmp(field, column->name)) |
180 | continue; | 180 | continue; |
181 | qsort(cgit_repolist.repos, cgit_repolist.count, | 181 | qsort(cgit_repolist.repos, cgit_repolist.count, |
182 | sizeof(struct cgit_repo), column->fn); | 182 | sizeof(struct cgit_repo), column->fn); |
183 | return 1; | 183 | return 1; |
184 | } | 184 | } |
185 | return 0; | 185 | return 0; |
186 | } | 186 | } |
187 | 187 | ||
188 | 188 | ||
189 | void cgit_print_repolist() | 189 | void cgit_print_repolist() |
190 | { | 190 | { |
191 | int i, columns = 4, hits = 0, header = 0; | 191 | int i, columns = 4, hits = 0, header = 0; |
192 | char *last_group = NULL; | 192 | char *last_group = NULL; |
193 | int sorted = 0; | 193 | int sorted = 0; |
194 | 194 | ||
195 | if (ctx.cfg.enable_index_links) | 195 | if (ctx.cfg.enable_index_links) |
196 | columns++; | 196 | columns++; |
197 | 197 | ||
198 | ctx.page.title = ctx.cfg.root_title; | 198 | ctx.page.title = ctx.cfg.root_title; |
199 | cgit_print_http_headers(&ctx); | 199 | cgit_print_http_headers(&ctx); |
200 | cgit_print_docstart(&ctx); | 200 | cgit_print_docstart(&ctx); |
201 | cgit_print_pageheader(&ctx); | 201 | cgit_print_pageheader(&ctx); |
202 | 202 | ||
203 | if (ctx.cfg.index_header) | 203 | if (ctx.cfg.index_header) |
204 | html_include(ctx.cfg.index_header); | 204 | html_include(ctx.cfg.index_header); |
205 | 205 | ||
206 | if(ctx.qry.sort) | 206 | if(ctx.qry.sort) |
207 | sorted = sort_repolist(ctx.qry.sort); | 207 | sorted = sort_repolist(ctx.qry.sort); |
208 | 208 | ||
209 | html("<table summary='repository list' class='list nowrap'>"); | 209 | html("<table summary='repository list' class='list nowrap'>"); |
210 | for (i=0; i<cgit_repolist.count; i++) { | 210 | for (i=0; i<cgit_repolist.count; i++) { |
211 | ctx.repo = &cgit_repolist.repos[i]; | 211 | ctx.repo = &cgit_repolist.repos[i]; |
212 | if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) | 212 | if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) |
213 | continue; | 213 | continue; |
214 | hits++; | 214 | hits++; |
215 | if (hits <= ctx.qry.ofs) | 215 | if (hits <= ctx.qry.ofs) |
216 | continue; | 216 | continue; |
217 | if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) | 217 | if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) |
218 | continue; | 218 | continue; |
219 | if (!header++) | 219 | if (!header++) |
220 | print_header(columns); | 220 | print_header(columns); |
221 | if (!sorted && | 221 | if (!sorted && |
222 | ((last_group == NULL && ctx.repo->group != NULL) || | 222 | ((last_group == NULL && ctx.repo->group != NULL) || |
223 | (last_group != NULL && ctx.repo->group == NULL) || | 223 | (last_group != NULL && ctx.repo->group == NULL) || |
224 | (last_group != NULL && ctx.repo->group != NULL && | 224 | (last_group != NULL && ctx.repo->group != NULL && |
225 | strcmp(ctx.repo->group, last_group)))) { | 225 | strcmp(ctx.repo->group, last_group)))) { |
226 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", | 226 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", |
227 | columns); | 227 | columns); |
228 | html_txt(ctx.repo->group); | 228 | html_txt(ctx.repo->group); |
229 | html("</td></tr>"); | 229 | html("</td></tr>"); |
230 | last_group = ctx.repo->group; | 230 | last_group = ctx.repo->group; |
231 | } | 231 | } |
232 | htmlf("<tr><td class='%s'>", | 232 | htmlf("<tr><td class='%s'>", |
233 | !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); | 233 | !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); |
234 | cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); | 234 | cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); |
235 | html("</td><td>"); | 235 | html("</td><td>"); |
236 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); | 236 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); |
237 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); | 237 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); |
238 | html_link_close(); | 238 | html_link_close(); |
239 | html("</td><td>"); | 239 | html("</td><td>"); |
240 | html_txt(ctx.repo->owner); | 240 | html_txt(ctx.repo->owner); |
241 | html("</td><td>"); | 241 | html("</td><td>"); |
242 | print_modtime(ctx.repo); | 242 | print_modtime(ctx.repo); |
243 | html("</td>"); | 243 | html("</td>"); |
244 | if (ctx.cfg.enable_index_links) { | 244 | if (ctx.cfg.enable_index_links) { |
245 | html("<td>"); | 245 | html("<td>"); |
246 | cgit_summary_link("summary", NULL, "button", NULL); | 246 | cgit_summary_link("summary", NULL, "button", NULL); |
247 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, | 247 | cgit_log_link("log", NULL, "button", NULL, NULL, NULL, |
248 | 0, NULL, NULL); | 248 | 0, NULL, NULL); |
249 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); | 249 | cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); |
250 | html("</td>"); | 250 | html("</td>"); |
251 | } | 251 | } |
252 | html("</tr>\n"); | 252 | html("</tr>\n"); |
253 | } | 253 | } |
254 | html("</table>"); | 254 | html("</table>"); |
255 | if (!hits) | 255 | if (!hits) |
256 | cgit_print_error("No repositories found"); | 256 | cgit_print_error("No repositories found"); |
257 | else if (hits > ctx.cfg.max_repo_count) | 257 | else if (hits > ctx.cfg.max_repo_count) |
258 | print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search); | 258 | print_pager(hits, ctx.cfg.max_repo_count, ctx.qry.search); |
259 | cgit_print_docend(); | 259 | cgit_print_docend(); |
260 | } | 260 | } |
261 | 261 | ||
262 | void cgit_print_site_readme() | 262 | void cgit_print_site_readme() |
263 | { | 263 | { |
264 | if (ctx.cfg.root_readme) | 264 | if (ctx.cfg.root_readme) |
265 | html_include(ctx.cfg.root_readme); | 265 | html_include(ctx.cfg.root_readme); |
266 | } | 266 | } |