blob: 5b4530e5410fd159534e03051ce86471c0992bf3 [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;
John Keepingfb3655d2013-04-06 10:28:57 +010096 struct strbuf filename = STRBUF_INIT;
Lars Hjemli65962682008-12-01 21:56:07 +010097 const char *basename;
Lukas Fleischerfab385e2013-03-04 13:25:34 +010098 int free_ref = 0;
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);
Christian Hesse79c985e2014-05-29 17:35:46 +0200104 if (!starts_with(ref, basename)) {
Lars Hjemli65962682008-12-01 21:56:07 +0100105 if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
106 ref++;
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100107 if (isdigit(ref[0])) {
John Keepingfb3655d2013-04-06 10:28:57 +0100108 ref = fmtalloc("%s-%s", basename, ref);
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100109 free_ref = 1;
110 }
Lars Hjemli65962682008-12-01 21:56:07 +0100111 }
112
113 for (f = cgit_snapshot_formats; f->suffix; f++) {
114 if (!(repo->snapshots & f->bit))
115 continue;
John Keepingfb3655d2013-04-06 10:28:57 +0100116 strbuf_reset(&filename);
117 strbuf_addf(&filename, "%s%s", ref, f->suffix);
118 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
Lars Hjemli65962682008-12-01 21:56:07 +0100119 html("&nbsp;&nbsp;");
120 }
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100121
122 if (free_ref)
123 free((char *)ref);
John Keepingfb3655d2013-04-06 10:28:57 +0100124 strbuf_release(&filename);
Lars Hjemli65962682008-12-01 21:56:07 +0100125}
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200126
Lars Hjemlic5984a92008-03-24 16:38:47 +0100127static int print_tag(struct refinfo *ref)
128{
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200129 struct tag *tag = NULL;
130 struct taginfo *info = NULL;
Lars Hjemli24d4bb32008-10-05 21:19:05 +0200131 char *name = (char *)ref->refname;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200132 struct object *obj = ref->object;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100133
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200134 if (obj->type == OBJ_TAG) {
135 tag = (struct tag *)obj;
136 obj = tag->tagged;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100137 info = ref->tag;
John Keeping198a4402015-10-08 23:23:59 +0100138 if (!info)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100139 return 1;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200140 }
141
142 html("<tr><td>");
John Keepingc422b9b2015-01-15 22:18:14 +0000143 cgit_tag_link(name, NULL, NULL, name);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200144 html("</td><td>");
145 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
146 print_tag_downloads(ctx.repo, name);
147 else
148 cgit_object_link(obj);
149 html("</td><td>");
150 if (info) {
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100151 if (info->tagger) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100152 cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000153 html_txt(info->tagger);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100154 cgit_close_filter(ctx.repo->email_filter);
155 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200156 } else if (ref->object->type == OBJ_COMMIT) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100157 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000158 html_txt(ref->commit->author);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100159 cgit_close_filter(ctx.repo->email_filter);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200160 }
161 html("</td><td colspan='2'>");
162 if (info) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200163 if (info->tagger_date > 0)
John Keepingf2a901d2016-01-19 19:33:05 +0000164 cgit_print_age(info->tagger_date, info->tagger_tz, -1);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200165 } else if (ref->object->type == OBJ_COMMIT) {
John Keepingf2a901d2016-01-19 19:33:05 +0000166 cgit_print_age(ref->commit->commit->date, 0, -1);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100167 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200168 html("</td></tr>\n");
169
Lars Hjemlic5984a92008-03-24 16:38:47 +0100170 return 0;
171}
172
173static void print_refs_link(char *path)
174{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100175 html("<tr class='nohover'><td colspan='5'>");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100176 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
177 html("</td></tr>");
178}
179
180void cgit_print_branches(int maxcount)
181{
182 struct reflist list;
183 int i;
184
185 html("<tr class='nohover'><th class='left'>Branch</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200186 "<th class='left'>Commit message</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +0100187 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200188 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100189
190 list.refs = NULL;
191 list.alloc = list.count = 0;
192 for_each_branch_ref(cgit_refs_cb, &list);
Lars Hjemli41934a32009-11-07 19:10:58 +0100193 if (ctx.repo->enable_remote_branches)
194 for_each_remote_ref(cgit_refs_cb, &list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100195
196 if (maxcount == 0 || maxcount > list.count)
197 maxcount = list.count;
198
Jason A. Donenfeld389cc172013-04-08 16:57:12 +0200199 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
200 if (ctx.repo->branch_sort == 0)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100201 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100202
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500203 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100204 print_branch(list.refs[i]);
205
206 if (maxcount < list.count)
207 print_refs_link("heads");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100208
209 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100210}
211
212void cgit_print_tags(int maxcount)
213{
214 struct reflist list;
215 int i;
216
Lars Hjemlic5984a92008-03-24 16:38:47 +0100217 list.refs = NULL;
218 list.alloc = list.count = 0;
219 for_each_tag_ref(cgit_refs_cb, &list);
220 if (list.count == 0)
221 return;
222 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
223 if (!maxcount)
224 maxcount = list.count;
225 else if (maxcount > list.count)
226 maxcount = list.count;
227 print_tag_header();
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500228 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100229 print_tag(list.refs[i]);
230
231 if (maxcount < list.count)
232 print_refs_link("tags");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100233
234 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100235}
236
John Keepinge3d3fff2015-03-08 16:32:16 +0000237void cgit_print_refs(void)
Lars Hjemli7937d062007-10-27 10:36:53 +0200238{
John Keeping6d39dd12015-08-14 12:47:16 +0100239 cgit_print_layout_start();
Lars Hjemli7937d062007-10-27 10:36:53 +0200240 html("<table class='list nowrap'>");
241
Christian Hesse79c985e2014-05-29 17:35:46 +0200242 if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200243 cgit_print_branches(0);
Christian Hesse79c985e2014-05-29 17:35:46 +0200244 else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200245 cgit_print_tags(0);
246 else {
247 cgit_print_branches(0);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100248 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
Lars Hjemli7937d062007-10-27 10:36:53 +0200249 cgit_print_tags(0);
250 }
Lars Hjemli7937d062007-10-27 10:36:53 +0200251 html("</table>");
John Keeping6d39dd12015-08-14 12:47:16 +0100252 cgit_print_layout_end();
Lars Hjemli7937d062007-10-27 10:36:53 +0200253}