]> gitweb.ps.run Git - ps-cgit/blob - ui-refs.c
git: update to v2.15.1
[ps-cgit] / ui-refs.c
1 /* ui-refs.c: browse symbolic refs
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "ui-refs.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 static inline int cmp_age(int age1, int age2)
15 {
16         /* age1 and age2 are assumed to be non-negative */
17         return age2 - age1;
18 }
19
20 static 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
28 static 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
36 static 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
49 static 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
54         return cmp_age(get_ref_age(r1), get_ref_age(r2));
55 }
56
57 static 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>");
65         cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
66                       ctx.qry.showmsg, 0);
67         html("</td><td>");
68
69         if (ref->object->type == OBJ_COMMIT) {
70                 cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL);
71                 html("</td><td>");
72                 cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs");
73                 html_txt(info->author);
74                 cgit_close_filter(ctx.repo->email_filter);
75                 html("</td><td colspan='2'>");
76                 cgit_print_age(info->committer_date, info->committer_tz, -1);
77         } else {
78                 html("</td><td></td><td>");
79                 cgit_object_link(ref->object);
80         }
81         html("</td></tr>\n");
82         return 0;
83 }
84
85 static void print_tag_header(void)
86 {
87         html("<tr class='nohover'><th class='left'>Tag</th>"
88              "<th class='left'>Download</th>"
89              "<th class='left'>Author</th>"
90              "<th class='left' colspan='2'>Age</th></tr>\n");
91 }
92
93 static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
94 {
95         const struct cgit_snapshot_format* f;
96         const char *basename;
97         struct strbuf filename = STRBUF_INIT;
98         size_t prefixlen;
99
100         if (!ref || strlen(ref) < 1)
101                 return;
102
103         basename = cgit_repobasename(repo->url);
104         if (starts_with(ref, basename))
105                 strbuf_addstr(&filename, ref);
106         else
107                 cgit_compose_snapshot_prefix(&filename, basename, ref);
108         prefixlen = filename.len;
109         for (f = cgit_snapshot_formats; f->suffix; f++) {
110                 if (!(repo->snapshots & f->bit))
111                         continue;
112                 strbuf_setlen(&filename, prefixlen);
113                 strbuf_addstr(&filename, f->suffix);
114                 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
115                 html("&nbsp;&nbsp;");
116         }
117
118         strbuf_release(&filename);
119 }
120
121 static int print_tag(struct refinfo *ref)
122 {
123         struct tag *tag = NULL;
124         struct taginfo *info = NULL;
125         char *name = (char *)ref->refname;
126         struct object *obj = ref->object;
127
128         if (obj->type == OBJ_TAG) {
129                 tag = (struct tag *)obj;
130                 obj = tag->tagged;
131                 info = ref->tag;
132                 if (!info)
133                         return 1;
134         }
135
136         html("<tr><td>");
137         cgit_tag_link(name, NULL, NULL, name);
138         html("</td><td>");
139         if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
140                 print_tag_downloads(ctx.repo, name);
141         else
142                 cgit_object_link(obj);
143         html("</td><td>");
144         if (info) {
145                 if (info->tagger) {
146                         cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
147                         html_txt(info->tagger);
148                         cgit_close_filter(ctx.repo->email_filter);
149                 }
150         } else if (ref->object->type == OBJ_COMMIT) {
151                 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
152                 html_txt(ref->commit->author);
153                 cgit_close_filter(ctx.repo->email_filter);
154         }
155         html("</td><td colspan='2'>");
156         if (info) {
157                 if (info->tagger_date > 0)
158                         cgit_print_age(info->tagger_date, info->tagger_tz, -1);
159         } else if (ref->object->type == OBJ_COMMIT) {
160                 cgit_print_age(ref->commit->commit->date, 0, -1);
161         }
162         html("</td></tr>\n");
163
164         return 0;
165 }
166
167 static void print_refs_link(char *path)
168 {
169         html("<tr class='nohover'><td colspan='5'>");
170         cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
171         html("</td></tr>");
172 }
173
174 void cgit_print_branches(int maxcount)
175 {
176         struct reflist list;
177         int i;
178
179         html("<tr class='nohover'><th class='left'>Branch</th>"
180              "<th class='left'>Commit message</th>"
181              "<th class='left'>Author</th>"
182              "<th class='left' colspan='2'>Age</th></tr>\n");
183
184         list.refs = NULL;
185         list.alloc = list.count = 0;
186         for_each_branch_ref(cgit_refs_cb, &list);
187         if (ctx.repo->enable_remote_branches)
188                 for_each_remote_ref(cgit_refs_cb, &list);
189
190         if (maxcount == 0 || maxcount > list.count)
191                 maxcount = list.count;
192
193         qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
194         if (ctx.repo->branch_sort == 0)
195                 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
196
197         for (i = 0; i < maxcount; i++)
198                 print_branch(list.refs[i]);
199
200         if (maxcount < list.count)
201                 print_refs_link("heads");
202
203         cgit_free_reflist_inner(&list);
204 }
205
206 void cgit_print_tags(int maxcount)
207 {
208         struct reflist list;
209         int i;
210
211         list.refs = NULL;
212         list.alloc = list.count = 0;
213         for_each_tag_ref(cgit_refs_cb, &list);
214         if (list.count == 0)
215                 return;
216         qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
217         if (!maxcount)
218                 maxcount = list.count;
219         else if (maxcount > list.count)
220                 maxcount = list.count;
221         print_tag_header();
222         for (i = 0; i < maxcount; i++)
223                 print_tag(list.refs[i]);
224
225         if (maxcount < list.count)
226                 print_refs_link("tags");
227
228         cgit_free_reflist_inner(&list);
229 }
230
231 void cgit_print_refs(void)
232 {
233         cgit_print_layout_start();
234         html("<table class='list nowrap'>");
235
236         if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
237                 cgit_print_branches(0);
238         else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
239                 cgit_print_tags(0);
240         else {
241                 cgit_print_branches(0);
242                 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
243                 cgit_print_tags(0);
244         }
245         html("</table>");
246         cgit_print_layout_end();
247 }