blob: 0399e6b9ebc05f87e6f1bffc1281965d26015ad3 [file] [log] [blame]
Lars Hjemli7937d062007-10-27 10:36:53 +02001/* ui-refs.c: browse symbolic refs
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
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;
102 char *filename;
103 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])) {
Lars Hjemli65962682008-12-01 21:56:07 +0100114 ref = xstrdup(fmt("%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;
122 filename = fmt("%s%s", ref, f->suffix);
123 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
124 html("&nbsp;&nbsp;");
125 }
Lukas Fleischerfab385e2013-03-04 13:25:34 +0100126
127 if (free_ref)
128 free((char *)ref);
Lars Hjemli65962682008-12-01 21:56:07 +0100129}
Lars Hjemlic5984a92008-03-24 16:38:47 +0100130static int print_tag(struct refinfo *ref)
131{
132 struct tag *tag;
133 struct taginfo *info;
Lars Hjemli24d4bb32008-10-05 21:19:05 +0200134 char *name = (char *)ref->refname;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100135
136 if (ref->object->type == OBJ_TAG) {
137 tag = (struct tag *)ref->object;
138 info = ref->tag;
139 if (!tag || !info)
140 return 1;
141 html("<tr><td>");
Lars Hjemli24d4bb32008-10-05 21:19:05 +0200142 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100143 html("</td><td>");
Lars Hjemli65962682008-12-01 21:56:07 +0100144 if (ctx.repo->snapshots && (tag->tagged->type == OBJ_COMMIT))
145 print_tag_downloads(ctx.repo, name);
146 else
147 cgit_object_link(tag->tagged);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100148 html("</td><td>");
149 if (info->tagger)
150 html(info->tagger);
Lars Hjemli5764fe92008-04-14 22:13:38 +0200151 html("</td><td colspan='2'>");
152 if (info->tagger_date > 0)
153 cgit_print_age(info->tagger_date, -1, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100154 html("</td></tr>\n");
155 } else {
Lars Hjemlic5984a92008-03-24 16:38:47 +0100156 html("<tr><td>");
Robin Redeker372b4042009-01-11 16:25:01 +0100157 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
Lars Hjemli5764fe92008-04-14 22:13:38 +0200158 html("</td><td>");
Robin Redeker5164be32009-01-10 12:44:08 +0100159 if (ctx.repo->snapshots && (ref->object->type == OBJ_COMMIT))
Lars Hjemli65962682008-12-01 21:56:07 +0100160 print_tag_downloads(ctx.repo, name);
161 else
162 cgit_object_link(ref->object);
Stefan Bühler1bbe04c2009-08-16 19:35:18 +0200163 html("</td><td>");
164 if (ref->object->type == OBJ_COMMIT)
165 html(ref->commit->author);
166 html("</td><td colspan='2'>");
167 if (ref->object->type == OBJ_COMMIT)
168 cgit_print_age(ref->commit->commit->date, -1, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100169 html("</td></tr>\n");
170 }
171 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
200 if (maxcount < list.count) {
201 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
202 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
203 }
204
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500205 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100206 print_branch(list.refs[i]);
207
208 if (maxcount < list.count)
209 print_refs_link("heads");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100210
211 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100212}
213
214void cgit_print_tags(int maxcount)
215{
216 struct reflist list;
217 int i;
218
Lars Hjemlic5984a92008-03-24 16:38:47 +0100219 list.refs = NULL;
220 list.alloc = list.count = 0;
221 for_each_tag_ref(cgit_refs_cb, &list);
222 if (list.count == 0)
223 return;
224 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
225 if (!maxcount)
226 maxcount = list.count;
227 else if (maxcount > list.count)
228 maxcount = list.count;
229 print_tag_header();
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500230 for (i = 0; i < maxcount; i++)
Lars Hjemlic5984a92008-03-24 16:38:47 +0100231 print_tag(list.refs[i]);
232
233 if (maxcount < list.count)
234 print_refs_link("tags");
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100235
236 cgit_free_reflist_inner(&list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100237}
238
Lars Hjemli7937d062007-10-27 10:36:53 +0200239void cgit_print_refs()
240{
241
242 html("<table class='list nowrap'>");
243
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100244 if (ctx.qry.path && !strncmp(ctx.qry.path, "heads", 5))
Lars Hjemli7937d062007-10-27 10:36:53 +0200245 cgit_print_branches(0);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100246 else if (ctx.qry.path && !strncmp(ctx.qry.path, "tags", 4))
Lars Hjemli7937d062007-10-27 10:36:53 +0200247 cgit_print_tags(0);
248 else {
249 cgit_print_branches(0);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100250 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
Lars Hjemli7937d062007-10-27 10:36:53 +0200251 cgit_print_tags(0);
252 }
Lars Hjemli7937d062007-10-27 10:36:53 +0200253 html("</table>");
254}