blob: 456f610df41cef1e8b3ebdfd02fd44204a3461d4 [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
93static int print_tag(struct refinfo *ref)
94{
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +020095 struct tag *tag = NULL;
96 struct taginfo *info = NULL;
Lars Hjemli24d4bb32008-10-05 21:19:05 +020097 char *name = (char *)ref->refname;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +020098 struct object *obj = ref->object;
Lars Hjemlic5984a92008-03-24 16:38:47 +010099
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200100 if (obj->type == OBJ_TAG) {
101 tag = (struct tag *)obj;
102 obj = tag->tagged;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100103 info = ref->tag;
John Keeping198a4402015-10-08 23:23:59 +0100104 if (!info)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100105 return 1;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200106 }
107
108 html("<tr><td>");
John Keepingc422b9b2015-01-15 22:18:14 +0000109 cgit_tag_link(name, NULL, NULL, name);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200110 html("</td><td>");
111 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
John Keeping71d14d92018-03-31 15:11:05 +0100112 cgit_print_snapshot_links(ctx.repo, name, "&nbsp;&nbsp;");
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200113 else
114 cgit_object_link(obj);
115 html("</td><td>");
116 if (info) {
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100117 if (info->tagger) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100118 cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000119 html_txt(info->tagger);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100120 cgit_close_filter(ctx.repo->email_filter);
121 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200122 } else if (ref->object->type == OBJ_COMMIT) {
Jason A. Donenfeld786609b2014-01-13 16:24:40 +0100123 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
John Keepingd1a6ece2014-01-12 19:45:15 +0000124 html_txt(ref->commit->author);
Jason A. Donenfelda5e15532014-01-13 04:04:52 +0100125 cgit_close_filter(ctx.repo->email_filter);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200126 }
127 html("</td><td colspan='2'>");
128 if (info) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200129 if (info->tagger_date > 0)
John Keepingf2a901d2016-01-19 19:33:05 +0000130 cgit_print_age(info->tagger_date, info->tagger_tz, -1);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200131 } else if (ref->object->type == OBJ_COMMIT) {
John Keepingf2a901d2016-01-19 19:33:05 +0000132 cgit_print_age(ref->commit->commit->date, 0, -1);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100133 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200134 html("</td></tr>\n");
135
Lars Hjemlic5984a92008-03-24 16:38:47 +0100136 return 0;
137}
138
Christian Hesseccba7eb2019-01-02 17:25:01 +0100139static void print_refs_link(const char *path)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100140{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100141 html("<tr class='nohover'><td colspan='5'>");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100142 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
143 html("</td></tr>");
144}
145
146void cgit_print_branches(int maxcount)
147{
148 struct reflist list;
149 int i;
150
151 html("<tr class='nohover'><th class='left'>Branch</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200152 "<th class='left'>Commit message</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +0100153 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200154 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100155
156 list.refs = NULL;
157 list.alloc = list.count = 0;
158 for_each_branch_ref(cgit_refs_cb, &list);
Lars Hjemli41934a32009-11-07 19:10:58 +0100159 if (ctx.repo->enable_remote_branches)
160 for_each_remote_ref(cgit_refs_cb, &list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100161
162 if (maxcount == 0 || maxcount > list.count)
163 maxcount = list.count;
164
Jason A. Donenfeld389cc172013-04-08 16:57:12 +0200165 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
166 if (ctx.repo->branch_sort == 0)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100167 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100168
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500169 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100170 print_branch(list.refs[i]);
171
172 if (maxcount < list.count)
173 print_refs_link("heads");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100174
175 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100176}
177
178void cgit_print_tags(int maxcount)
179{
180 struct reflist list;
181 int i;
182
Lars Hjemlic5984a92008-03-24 16:38:47 +0100183 list.refs = NULL;
184 list.alloc = list.count = 0;
185 for_each_tag_ref(cgit_refs_cb, &list);
186 if (list.count == 0)
187 return;
188 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
189 if (!maxcount)
190 maxcount = list.count;
191 else if (maxcount > list.count)
192 maxcount = list.count;
193 print_tag_header();
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500194 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100195 print_tag(list.refs[i]);
196
197 if (maxcount < list.count)
198 print_refs_link("tags");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100199
200 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100201}
202
John Keepinge3d3fff2015-03-08 16:32:16 +0000203void cgit_print_refs(void)
Lars Hjemli7937d062007-10-27 10:36:53 +0200204{
John Keeping6d39dd12015-08-14 12:47:16 +0100205 cgit_print_layout_start();
Lars Hjemli7937d062007-10-27 10:36:53 +0200206 html("<table class='list nowrap'>");
207
Christian Hesse79c985e2014-05-29 17:35:46 +0200208 if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200209 cgit_print_branches(0);
Christian Hesse79c985e2014-05-29 17:35:46 +0200210 else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200211 cgit_print_tags(0);
212 else {
213 cgit_print_branches(0);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100214 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
Lars Hjemli7937d062007-10-27 10:36:53 +0200215 cgit_print_tags(0);
216 }
Lars Hjemli7937d062007-10-27 10:36:53 +0200217 html("</table>");
John Keeping6d39dd12015-08-14 12:47:16 +0100218 cgit_print_layout_end();
Lars Hjemli7937d062007-10-27 10:36:53 +0200219}