blob: 75f2789fc1653b76509f2ff3f8a9120051dd30e6 [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
Jason A. Donenfeld49306112014-02-26 16:57:15 +010014static inline int cmp_age(int age1, int age2)
Lars Hjemlic5984a92008-03-24 16:38:47 +010015{
Jason A. Donenfeld49306112014-02-26 16:57:15 +010016 /* age1 and age2 are assumed to be non-negative */
17 return age2 - age1;
Lars Hjemlic5984a92008-03-24 16:38:47 +010018}
19
20static int cmp_ref_name(const void *a, const void *b)
21{
22 struct refinfo *r1 = *(struct refinfo **)a;
23 struct refinfo *r2 = *(struct refinfo **)b;
24
25 return strcmp(r1->refname, r2->refname);
26}
27
28static int cmp_branch_age(const void *a, const void *b)
29{
30 struct refinfo *r1 = *(struct refinfo **)a;
31 struct refinfo *r2 = *(struct refinfo **)b;
32
33 return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
34}
35
Lars Hjemli3687be22010-08-03 22:06:21 +020036static int get_ref_age(struct refinfo *ref)
37{
38 if (!ref->object)
39 return 0;
40 switch (ref->object->type) {
41 case OBJ_TAG:
42 return ref->tag ? ref->tag->tagger_date : 0;
43 case OBJ_COMMIT:
44 return ref->commit ? ref->commit->committer_date : 0;
45 }
46 return 0;
47}
48
Lars Hjemlic5984a92008-03-24 16:38:47 +010049static int cmp_tag_age(const void *a, const void *b)
50{
51 struct refinfo *r1 = *(struct refinfo **)a;
52 struct refinfo *r2 = *(struct refinfo **)b;
53
Lars Hjemli3687be22010-08-03 22:06:21 +020054 return cmp_age(get_ref_age(r1), get_ref_age(r2));
Lars Hjemlic5984a92008-03-24 16:38:47 +010055}
56
57static int print_branch(struct refinfo *ref)
58{
59 struct commitinfo *info = ref->commit;
60 char *name = (char *)ref->refname;
61
62 if (!info)
63 return 1;
64 html("<tr><td>");
Lars Hjemli0274b572008-11-29 18:39:41 +010065 cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
John Keeping30304d82015-08-12 15:55:28 +010066 ctx.qry.showmsg, 0);
Lars Hjemlic5984a92008-03-24 16:38:47 +010067 html("</td><td>");
68
69 if (ref->object->type == OBJ_COMMIT) {
John Keepingeeddb5b2014-10-05 10:59:02 +010070 cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +010071 html("</td><td>");
Jason A. Donenfeld786609b2014-01-13 16:24:40 +010072 cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs");
Lars Hjemlic5984a92008-03-24 16:38:47 +010073 html_txt(info->author);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +010074 cgit_close_filter(ctx.repo->email_filter);
Lars Hjemli5764fe92008-04-14 22:13:38 +020075 html("</td><td colspan='2'>");
John Keepingf2a901d2016-01-19 19:33:05 +000076 cgit_print_age(info->committer_date, info->committer_tz, -1);
Lars Hjemlic5984a92008-03-24 16:38:47 +010077 } else {
78 html("</td><td></td><td>");
79 cgit_object_link(ref->object);
80 }
81 html("</td></tr>\n");
82 return 0;
83}
84
John Keepinge3d3fff2015-03-08 16:32:16 +000085static void print_tag_header(void)
Lars Hjemlic5984a92008-03-24 16:38:47 +010086{
87 html("<tr class='nohover'><th class='left'>Tag</th>"
Lars Hjemli65962682008-12-01 21:56:07 +010088 "<th class='left'>Download</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +010089 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +020090 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +010091}
92
Lars Hjemli65962682008-12-01 21:56:07 +010093static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
94{
95 const struct cgit_snapshot_format* f;
Lars Hjemli65962682008-12-01 21:56:07 +010096 const char *basename;
Lukas Fleischer9984e7a2016-05-24 18:15:18 +020097 struct strbuf filename = STRBUF_INIT;
98 size_t prefixlen;
Lars Hjemli65962682008-12-01 21:56:07 +010099
Christian Hessee6749642014-02-20 20:48:45 +0100100 if (!ref || strlen(ref) < 1)
Lars Hjemli65962682008-12-01 21:56:07 +0100101 return;
102
103 basename = cgit_repobasename(repo->url);
Lukas Fleischer9984e7a2016-05-24 18:15:18 +0200104 if (starts_with(ref, basename))
105 strbuf_addstr(&filename, ref);
106 else
107 cgit_compose_snapshot_prefix(&filename, basename, ref);
108 prefixlen = filename.len;
Lars Hjemli65962682008-12-01 21:56:07 +0100109 for (f = cgit_snapshot_formats; f->suffix; f++) {
110 if (!(repo->snapshots & f->bit))
111 continue;
Lukas Fleischer9984e7a2016-05-24 18:15:18 +0200112 strbuf_setlen(&filename, prefixlen);
113 strbuf_addstr(&filename, f->suffix);
John Keepingfb3655d2013-04-06 10:28:57 +0100114 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
Lars Hjemli65962682008-12-01 21:56:07 +0100115 html("&nbsp;&nbsp;");
116 }
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100117
John Keepingfb3655d2013-04-06 10:28:57 +0100118 strbuf_release(&filename);
Lars Hjemli65962682008-12-01 21:56:07 +0100119}
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200120
Lars Hjemlic5984a92008-03-24 16:38:47 +0100121static int print_tag(struct refinfo *ref)
122{
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200123 struct tag *tag = NULL;
124 struct taginfo *info = NULL;
Lars Hjemli24d4bb32008-10-05 21:19:05 +0200125 char *name = (char *)ref->refname;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200126 struct object *obj = ref->object;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100127
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200128 if (obj->type == OBJ_TAG) {
129 tag = (struct tag *)obj;
130 obj = tag->tagged;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100131 info = ref->tag;
John Keeping198a4402015-10-08 23:23:59 +0100132 if (!info)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100133 return 1;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200134 }
135
136 html("<tr><td>");
John Keepingc422b9b2015-01-15 22:18:14 +0000137 cgit_tag_link(name, NULL, NULL, name);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200138 html("</td><td>");
139 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
140 print_tag_downloads(ctx.repo, name);
141 else
142 cgit_object_link(obj);
143 html("</td><td>");
144 if (info) {
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100145 if (info->tagger) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100146 cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000147 html_txt(info->tagger);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100148 cgit_close_filter(ctx.repo->email_filter);
149 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200150 } else if (ref->object->type == OBJ_COMMIT) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100151 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000152 html_txt(ref->commit->author);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100153 cgit_close_filter(ctx.repo->email_filter);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200154 }
155 html("</td><td colspan='2'>");
156 if (info) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200157 if (info->tagger_date > 0)
John Keepingf2a901d2016-01-19 19:33:05 +0000158 cgit_print_age(info->tagger_date, info->tagger_tz, -1);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200159 } else if (ref->object->type == OBJ_COMMIT) {
John Keepingf2a901d2016-01-19 19:33:05 +0000160 cgit_print_age(ref->commit->commit->date, 0, -1);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100161 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200162 html("</td></tr>\n");
163
Lars Hjemlic5984a92008-03-24 16:38:47 +0100164 return 0;
165}
166
167static void print_refs_link(char *path)
168{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100169 html("<tr class='nohover'><td colspan='5'>");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100170 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
171 html("</td></tr>");
172}
173
174void cgit_print_branches(int maxcount)
175{
176 struct reflist list;
177 int i;
178
179 html("<tr class='nohover'><th class='left'>Branch</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200180 "<th class='left'>Commit message</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +0100181 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200182 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100183
184 list.refs = NULL;
185 list.alloc = list.count = 0;
186 for_each_branch_ref(cgit_refs_cb, &list);
Lars Hjemli41934a32009-11-07 19:10:58 +0100187 if (ctx.repo->enable_remote_branches)
188 for_each_remote_ref(cgit_refs_cb, &list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100189
190 if (maxcount == 0 || maxcount > list.count)
191 maxcount = list.count;
192
Jason A. Donenfeld389cc172013-04-08 16:57:12 +0200193 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
194 if (ctx.repo->branch_sort == 0)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100195 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100196
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500197 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100198 print_branch(list.refs[i]);
199
200 if (maxcount < list.count)
201 print_refs_link("heads");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100202
203 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100204}
205
206void cgit_print_tags(int maxcount)
207{
208 struct reflist list;
209 int i;
210
Lars Hjemlic5984a92008-03-24 16:38:47 +0100211 list.refs = NULL;
212 list.alloc = list.count = 0;
213 for_each_tag_ref(cgit_refs_cb, &list);
214 if (list.count == 0)
215 return;
216 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
217 if (!maxcount)
218 maxcount = list.count;
219 else if (maxcount > list.count)
220 maxcount = list.count;
221 print_tag_header();
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500222 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100223 print_tag(list.refs[i]);
224
225 if (maxcount < list.count)
226 print_refs_link("tags");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100227
228 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100229}
230
John Keepinge3d3fff2015-03-08 16:32:16 +0000231void cgit_print_refs(void)
Lars Hjemli7937d062007-10-27 10:36:53 +0200232{
John Keeping6d39dd12015-08-14 12:47:16 +0100233 cgit_print_layout_start();
Lars Hjemli7937d062007-10-27 10:36:53 +0200234 html("<table class='list nowrap'>");
235
Christian Hesse79c985e2014-05-29 17:35:46 +0200236 if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200237 cgit_print_branches(0);
Christian Hesse79c985e2014-05-29 17:35:46 +0200238 else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200239 cgit_print_tags(0);
240 else {
241 cgit_print_branches(0);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100242 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
Lars Hjemli7937d062007-10-27 10:36:53 +0200243 cgit_print_tags(0);
244 }
Lars Hjemli7937d062007-10-27 10:36:53 +0200245 html("</table>");
John Keeping6d39dd12015-08-14 12:47:16 +0100246 cgit_print_layout_end();
Lars Hjemli7937d062007-10-27 10:36:53 +0200247}