blob: 74064789ecdd281bd411472b20730501d1c52182 [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}
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200130
Lars Hjemlic5984a92008-03-24 16:38:47 +0100131static int print_tag(struct refinfo *ref)
132{
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200133 struct tag *tag = NULL;
134 struct taginfo *info = NULL;
Lars Hjemli24d4bb32008-10-05 21:19:05 +0200135 char *name = (char *)ref->refname;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200136 struct object *obj = ref->object;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100137
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200138 if (obj->type == OBJ_TAG) {
139 tag = (struct tag *)obj;
140 obj = tag->tagged;
Lars Hjemlic5984a92008-03-24 16:38:47 +0100141 info = ref->tag;
142 if (!tag || !info)
143 return 1;
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200144 }
145
146 html("<tr><td>");
147 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
148 html("</td><td>");
149 if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
150 print_tag_downloads(ctx.repo, name);
151 else
152 cgit_object_link(obj);
153 html("</td><td>");
154 if (info) {
Lars Hjemlic5984a92008-03-24 16:38:47 +0100155 if (info->tagger)
156 html(info->tagger);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200157 } else if (ref->object->type == OBJ_COMMIT) {
158 html(ref->commit->author);
159 }
160 html("</td><td colspan='2'>");
161 if (info) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200162 if (info->tagger_date > 0)
163 cgit_print_age(info->tagger_date, -1, NULL);
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200164 } else if (ref->object->type == OBJ_COMMIT) {
165 cgit_print_age(ref->commit->commit->date, -1, NULL);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100166 }
Lukas Fleischer4b4a62d2013-04-06 23:39:08 +0200167 html("</td></tr>\n");
168
Lars Hjemlic5984a92008-03-24 16:38:47 +0100169 return 0;
170}
171
172static void print_refs_link(char *path)
173{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +0100174 html("<tr class='nohover'><td colspan='5'>");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100175 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
176 html("</td></tr>");
177}
178
179void cgit_print_branches(int maxcount)
180{
181 struct reflist list;
182 int i;
183
184 html("<tr class='nohover'><th class='left'>Branch</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200185 "<th class='left'>Commit message</th>"
Lars Hjemlic5984a92008-03-24 16:38:47 +0100186 "<th class='left'>Author</th>"
Lars Hjemli5764fe92008-04-14 22:13:38 +0200187 "<th class='left' colspan='2'>Age</th></tr>\n");
Lars Hjemlic5984a92008-03-24 16:38:47 +0100188
189 list.refs = NULL;
190 list.alloc = list.count = 0;
191 for_each_branch_ref(cgit_refs_cb, &list);
Lars Hjemli41934a32009-11-07 19:10:58 +0100192 if (ctx.repo->enable_remote_branches)
193 for_each_remote_ref(cgit_refs_cb, &list);
Lars Hjemlic5984a92008-03-24 16:38:47 +0100194
195 if (maxcount == 0 || maxcount > list.count)
196 maxcount = list.count;
197
198 if (maxcount < list.count) {
199 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
200 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
201 }
202
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
Lars Hjemli7937d062007-10-27 10:36:53 +0200237void cgit_print_refs()
238{
239
240 html("<table class='list nowrap'>");
241
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100242 if (ctx.qry.path && !strncmp(ctx.qry.path, "heads", 5))
Lars Hjemli7937d062007-10-27 10:36:53 +0200243 cgit_print_branches(0);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100244 else if (ctx.qry.path && !strncmp(ctx.qry.path, "tags", 4))
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>");
252}