blob: e8e308e58d5848c721febefdf45326f76e31ff7e [file] [log] [blame]
Lars Hjemli7937d062007-10-27 10:36:53 +02001/* ui-refs.c: browse symbolic refs
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli7937d062007-10-27 10:36:53 +02004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-refs.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemli7937d062007-10-27 10:36:53 +020013
Lars Hjemlic5984a92008-03-24 16:38:47 +010014static int cmp_age(int age1, int age2)
15{
16 if (age1 != 0 && age2 != 0)
17 return age2 - age1;
18
19 if (age1 == 0 && age2 == 0)
20 return 0;
21
22 if (age1 == 0)
23 return +1;
24
25 return -1;
26}
27
28static int cmp_ref_name(const void *a, const void *b)
29{
30 struct refinfo *r1 = *(struct refinfo **)a;
31 struct refinfo *r2 = *(struct refinfo **)b;
32
33 return strcmp(r1->refname, r2->refname);
34}
35
36static int cmp_branch_age(const void *a, const void *b)
37{
38 struct refinfo *r1 = *(struct refinfo **)a;
39 struct refinfo *r2 = *(struct refinfo **)b;
40
41 return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
42}
43
Lars Hjemli3687be22010-08-03 22:06:21 +020044static int get_ref_age(struct refinfo *ref)
45{
46 if (!ref->object)
47 return 0;
48 switch (ref->object->type) {
49 case OBJ_TAG:
50 return ref->tag ? ref->tag->tagger_date : 0;
51 case OBJ_COMMIT:
52 return ref->commit ? ref->commit->committer_date : 0;
53 }
54 return 0;
55}
56
Lars Hjemlic5984a92008-03-24 16:38:47 +010057static int cmp_tag_age(const void *a, const void *b)
58{
59 struct refinfo *r1 = *(struct refinfo **)a;
60 struct refinfo *r2 = *(struct refinfo **)b;
61
Lars Hjemli3687be22010-08-03 22:06:21 +020062 return cmp_age(get_ref_age(r1), get_ref_age(r2));
Lars Hjemlic5984a92008-03-24 16:38:47 +010063}
64
65static int print_branch(struct refinfo *ref)
66{
67 struct commitinfo *info = ref->commit;
68 char *name = (char *)ref->refname;
69
70 if (!info)
71 return 1;
72 html("<tr><td>");
Lars Hjemli0274b572008-11-29 18:39:41 +010073 cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
74 ctx.qry.showmsg);
Lars Hjemlic5984a92008-03-24 16:38:47 +010075 html("</td><td>");
76
77 if (ref->object->type == OBJ_COMMIT) {
Johan Herland685872b2010-06-10 01:09:35 +020078 cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL, 0);
Lars Hjemlic5984a92008-03-24 16:38:47 +010079 html("</td><td>");
Jason A. Donenfeld786609b2014-01-13 16:24:40 +010080 cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs");
Lars Hjemlic5984a92008-03-24 16:38:47 +010081 html_txt(info->author);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +010082 cgit_close_filter(ctx.repo->email_filter);
Lars Hjemli5764fe92008-04-14 22:13:38 +020083 html("</td><td colspan='2'>");
84 cgit_print_age(info->commit->date, -1, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +010085 } else {
86 html("</td><td></td><td>");
87 cgit_object_link(ref->object);
88 }
89 html("</td></tr>\n");
90 return 0;
91}
92
93static void print_tag_header()
94{
95 html("<tr class='nohover'><th class='left'>Tag</th>"
Lars Hjemli65962682008-12-01 21:56:07 +010096 "<th class='left'>Download</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +010097 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +020098 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +010099}
100
Lars Hjemli65962682008-12-01 21:56:07 +0100101static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
102{
103 const struct cgit_snapshot_format* f;
John Keepingfb3655d2013-04-06 10:28:57 +0100104 struct strbuf filename = STRBUF_INIT;
Lars Hjemli65962682008-12-01 21:56:07 +0100105 const char *basename;
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100106 int free_ref = 0;
Lars Hjemli65962682008-12-01 21:56:07 +0100107
Christian Hessee6749642014-02-20 20:48:45 +0100108 if (!ref || strlen(ref) < 1)
Lars Hjemli65962682008-12-01 21:56:07 +0100109 return;
110
111 basename = cgit_repobasename(repo->url);
112 if (prefixcmp(ref, basename) != 0) {
113 if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
114 ref++;
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100115 if (isdigit(ref[0])) {
John Keepingfb3655d2013-04-06 10:28:57 +0100116 ref = fmtalloc("%s-%s", basename, ref);
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100117 free_ref = 1;
118 }
Lars Hjemli65962682008-12-01 21:56:07 +0100119 }
120
121 for (f = cgit_snapshot_formats; f->suffix; f++) {
122 if (!(repo->snapshots & f->bit))
123 continue;
John Keepingfb3655d2013-04-06 10:28:57 +0100124 strbuf_reset(&filename);
125 strbuf_addf(&filename, "%s%s", ref, f->suffix);
126 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
Lars Hjemli65962682008-12-01 21:56:07 +0100127 html("&nbsp;&nbsp;");
128 }
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100129
130 if (free_ref)
131 free((char *)ref);
John Keepingfb3655d2013-04-06 10:28:57 +0100132 strbuf_release(&filename);
Lars Hjemli65962682008-12-01 21:56:07 +0100133}
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200134
Lars Hjemlic5984a92008-03-24 16:38:47 +0100135static int print_tag(struct refinfo *ref)
136{
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200137 struct tag *tag = NULL;
138 struct taginfo *info = NULL;
Lars Hjemli24d4bb32008-10-05 21:19:05 +0200139 char *name = (char *)ref->refname;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200140 struct object *obj = ref->object;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100141
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200142 if (obj->type == OBJ_TAG) {
143 tag = (struct tag *)obj;
144 obj = tag->tagged;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100145 info = ref->tag;
146 if (!tag || !info)
147 return 1;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200148 }
149
150 html("<tr><td>");
151 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
152 html("</td><td>");
153 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
154 print_tag_downloads(ctx.repo, name);
155 else
156 cgit_object_link(obj);
157 html("</td><td>");
158 if (info) {
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100159 if (info->tagger) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100160 cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000161 html_txt(info->tagger);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100162 cgit_close_filter(ctx.repo->email_filter);
163 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200164 } else if (ref->object->type == OBJ_COMMIT) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100165 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000166 html_txt(ref->commit->author);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100167 cgit_close_filter(ctx.repo->email_filter);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200168 }
169 html("</td><td colspan='2'>");
170 if (info) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200171 if (info->tagger_date > 0)
172 cgit_print_age(info->tagger_date, -1, NULL);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200173 } else if (ref->object->type == OBJ_COMMIT) {
174 cgit_print_age(ref->commit->commit->date, -1, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100175 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200176 html("</td></tr>\n");
177
Lars Hjemlic5984a92008-03-24 16:38:47 +0100178 return 0;
179}
180
181static void print_refs_link(char *path)
182{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100183 html("<tr class='nohover'><td colspan='5'>");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100184 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
185 html("</td></tr>");
186}
187
188void cgit_print_branches(int maxcount)
189{
190 struct reflist list;
191 int i;
192
193 html("<tr class='nohover'><th class='left'>Branch</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200194 "<th class='left'>Commit message</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +0100195 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200196 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100197
198 list.refs = NULL;
199 list.alloc = list.count = 0;
200 for_each_branch_ref(cgit_refs_cb, &list);
Lars Hjemli41934a32009-11-07 19:10:58 +0100201 if (ctx.repo->enable_remote_branches)
202 for_each_remote_ref(cgit_refs_cb, &list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100203
204 if (maxcount == 0 || maxcount > list.count)
205 maxcount = list.count;
206
Jason A. Donenfeld389cc172013-04-08 16:57:12 +0200207 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
208 if (ctx.repo->branch_sort == 0)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100209 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100210
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500211 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100212 print_branch(list.refs[i]);
213
214 if (maxcount < list.count)
215 print_refs_link("heads");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100216
217 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100218}
219
220void cgit_print_tags(int maxcount)
221{
222 struct reflist list;
223 int i;
224
Lars Hjemlic5984a92008-03-24 16:38:47 +0100225 list.refs = NULL;
226 list.alloc = list.count = 0;
227 for_each_tag_ref(cgit_refs_cb, &list);
228 if (list.count == 0)
229 return;
230 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
231 if (!maxcount)
232 maxcount = list.count;
233 else if (maxcount > list.count)
234 maxcount = list.count;
235 print_tag_header();
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500236 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100237 print_tag(list.refs[i]);
238
239 if (maxcount < list.count)
240 print_refs_link("tags");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100241
242 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100243}
244
Lars Hjemli7937d062007-10-27 10:36:53 +0200245void cgit_print_refs()
246{
247
248 html("<table class='list nowrap'>");
249
Lukas Fleischer36bdb212014-01-10 12:44:35 +0100250 if (ctx.qry.path && !prefixcmp(ctx.qry.path, "heads"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200251 cgit_print_branches(0);
Lukas Fleischer36bdb212014-01-10 12:44:35 +0100252 else if (ctx.qry.path && !prefixcmp(ctx.qry.path, "tags"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200253 cgit_print_tags(0);
254 else {
255 cgit_print_branches(0);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100256 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
Lars Hjemli7937d062007-10-27 10:36:53 +0200257 cgit_print_tags(0);
258 }
Lars Hjemli7937d062007-10-27 10:36:53 +0200259 html("</table>");
260}