blob: 20c91e3c2401d7da4d6fa3c634623ef60f71e5fe [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>");
80 html_txt(info->author);
Lars Hjemli5764fe92008-04-14 22:13:38 +020081 html("</td><td colspan='2'>");
82 cgit_print_age(info->commit->date, -1, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +010083 } else {
84 html("</td><td></td><td>");
85 cgit_object_link(ref->object);
86 }
87 html("</td></tr>\n");
88 return 0;
89}
90
91static void print_tag_header()
92{
93 html("<tr class='nohover'><th class='left'>Tag</th>"
Lars Hjemli65962682008-12-01 21:56:07 +010094 "<th class='left'>Download</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +010095 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +020096 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +010097}
98
Lars Hjemli65962682008-12-01 21:56:07 +010099static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
100{
101 const struct cgit_snapshot_format* f;
John Keepingfb3655d2013-04-06 10:28:57 +0100102 struct strbuf filename = STRBUF_INIT;
Lars Hjemli65962682008-12-01 21:56:07 +0100103 const char *basename;
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100104 int free_ref = 0;
Lars Hjemli65962682008-12-01 21:56:07 +0100105
106 if (!ref || strlen(ref) < 2)
107 return;
108
109 basename = cgit_repobasename(repo->url);
110 if (prefixcmp(ref, basename) != 0) {
111 if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
112 ref++;
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100113 if (isdigit(ref[0])) {
John Keepingfb3655d2013-04-06 10:28:57 +0100114 ref = fmtalloc("%s-%s", basename, ref);
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100115 free_ref = 1;
116 }
Lars Hjemli65962682008-12-01 21:56:07 +0100117 }
118
119 for (f = cgit_snapshot_formats; f->suffix; f++) {
120 if (!(repo->snapshots & f->bit))
121 continue;
John Keepingfb3655d2013-04-06 10:28:57 +0100122 strbuf_reset(&filename);
123 strbuf_addf(&filename, "%s%s", ref, f->suffix);
124 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
Lars Hjemli65962682008-12-01 21:56:07 +0100125 html("&nbsp;&nbsp;");
126 }
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100127
128 if (free_ref)
129 free((char *)ref);
John Keepingfb3655d2013-04-06 10:28:57 +0100130 strbuf_release(&filename);
Lars Hjemli65962682008-12-01 21:56:07 +0100131}
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200132
Lars Hjemlic5984a92008-03-24 16:38:47 +0100133static int print_tag(struct refinfo *ref)
134{
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200135 struct tag *tag = NULL;
136 struct taginfo *info = NULL;
Lars Hjemli24d4bb32008-10-05 21:19:05 +0200137 char *name = (char *)ref->refname;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200138 struct object *obj = ref->object;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100139
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200140 if (obj->type == OBJ_TAG) {
141 tag = (struct tag *)obj;
142 obj = tag->tagged;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100143 info = ref->tag;
144 if (!tag || !info)
145 return 1;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200146 }
147
148 html("<tr><td>");
149 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
150 html("</td><td>");
151 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
152 print_tag_downloads(ctx.repo, name);
153 else
154 cgit_object_link(obj);
155 html("</td><td>");
156 if (info) {
Lars Hjemlic5984a92008-03-24 16:38:47 +0100157 if (info->tagger)
158 html(info->tagger);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200159 } else if (ref->object->type == OBJ_COMMIT) {
160 html(ref->commit->author);
161 }
162 html("</td><td colspan='2'>");
163 if (info) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200164 if (info->tagger_date > 0)
165 cgit_print_age(info->tagger_date, -1, NULL);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200166 } else if (ref->object->type == OBJ_COMMIT) {
167 cgit_print_age(ref->commit->commit->date, -1, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100168 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200169 html("</td></tr>\n");
170
Lars Hjemlic5984a92008-03-24 16:38:47 +0100171 return 0;
172}
173
174static void print_refs_link(char *path)
175{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100176 html("<tr class='nohover'><td colspan='5'>");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100177 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
178 html("</td></tr>");
179}
180
181void cgit_print_branches(int maxcount)
182{
183 struct reflist list;
184 int i;
185
186 html("<tr class='nohover'><th class='left'>Branch</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200187 "<th class='left'>Commit message</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +0100188 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200189 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100190
191 list.refs = NULL;
192 list.alloc = list.count = 0;
193 for_each_branch_ref(cgit_refs_cb, &list);
Lars Hjemli41934a32009-11-07 19:10:58 +0100194 if (ctx.repo->enable_remote_branches)
195 for_each_remote_ref(cgit_refs_cb, &list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100196
197 if (maxcount == 0 || maxcount > list.count)
198 maxcount = list.count;
199
Jason A. Donenfeld389cc172013-04-08 16:57:12 +0200200 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
201 if (ctx.repo->branch_sort == 0)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100202 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100203
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500204 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100205 print_branch(list.refs[i]);
206
207 if (maxcount < list.count)
208 print_refs_link("heads");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100209
210 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100211}
212
213void cgit_print_tags(int maxcount)
214{
215 struct reflist list;
216 int i;
217
Lars Hjemlic5984a92008-03-24 16:38:47 +0100218 list.refs = NULL;
219 list.alloc = list.count = 0;
220 for_each_tag_ref(cgit_refs_cb, &list);
221 if (list.count == 0)
222 return;
223 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
224 if (!maxcount)
225 maxcount = list.count;
226 else if (maxcount > list.count)
227 maxcount = list.count;
228 print_tag_header();
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500229 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100230 print_tag(list.refs[i]);
231
232 if (maxcount < list.count)
233 print_refs_link("tags");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100234
235 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100236}
237
Lars Hjemli7937d062007-10-27 10:36:53 +0200238void cgit_print_refs()
239{
240
241 html("<table class='list nowrap'>");
242
Lukas Fleischer36bdb212014-01-10 12:44:35 +0100243 if (ctx.qry.path && !prefixcmp(ctx.qry.path, "heads"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200244 cgit_print_branches(0);
Lukas Fleischer36bdb212014-01-10 12:44:35 +0100245 else if (ctx.qry.path && !prefixcmp(ctx.qry.path, "tags"))
Lars Hjemli7937d062007-10-27 10:36:53 +0200246 cgit_print_tags(0);
247 else {
248 cgit_print_branches(0);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100249 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
Lars Hjemli7937d062007-10-27 10:36:53 +0200250 cgit_print_tags(0);
251 }
Lars Hjemli7937d062007-10-27 10:36:53 +0200252 html("</table>");
253}