]> gitweb.ps.run Git - ps-cgit/blob - ui-refs.c
git: update to v2.46.0
[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 #define USE_THE_REPOSITORY_VARIABLE
10
11 #include "cgit.h"
12 #include "ui-refs.h"
13 #include "html.h"
14 #include "ui-shared.h"
15
16 static inline int cmp_age(int age1, int age2)
17 {
18         /* age1 and age2 are assumed to be non-negative */
19         return age2 - age1;
20 }
21
22 static int cmp_ref_name(const void *a, const void *b)
23 {
24         struct refinfo *r1 = *(struct refinfo **)a;
25         struct refinfo *r2 = *(struct refinfo **)b;
26
27         return strcmp(r1->refname, r2->refname);
28 }
29
30 static int cmp_branch_age(const void *a, const void *b)
31 {
32         struct refinfo *r1 = *(struct refinfo **)a;
33         struct refinfo *r2 = *(struct refinfo **)b;
34
35         return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
36 }
37
38 static int get_ref_age(struct refinfo *ref)
39 {
40         if (!ref->object)
41                 return 0;
42         switch (ref->object->type) {
43         case OBJ_TAG:
44                 return ref->tag ? ref->tag->tagger_date : 0;
45         case OBJ_COMMIT:
46                 return ref->commit ? ref->commit->committer_date : 0;
47         }
48         return 0;
49 }
50
51 static int cmp_tag_age(const void *a, const void *b)
52 {
53         struct refinfo *r1 = *(struct refinfo **)a;
54         struct refinfo *r2 = *(struct refinfo **)b;
55
56         return cmp_age(get_ref_age(r1), get_ref_age(r2));
57 }
58
59 static int print_branch(struct refinfo *ref)
60 {
61         struct commitinfo *info = ref->commit;
62         char *name = (char *)ref->refname;
63
64         if (!info)
65                 return 1;
66         html("<tr><td>");
67         cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
68                       ctx.qry.showmsg, 0);
69         html("</td><td>");
70
71         if (ref->object->type == OBJ_COMMIT) {
72                 cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL);
73                 html("</td><td>");
74                 cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs");
75                 html_txt(info->author);
76                 cgit_close_filter(ctx.repo->email_filter);
77                 html("</td><td colspan='2'>");
78                 cgit_print_age(info->committer_date, info->committer_tz, -1);
79         } else {
80                 html("</td><td></td><td>");
81                 cgit_object_link(ref->object);
82         }
83         html("</td></tr>\n");
84         return 0;
85 }
86
87 static void print_tag_header(void)
88 {
89         html("<tr class='nohover'><th class='left'>Tag</th>"
90              "<th class='left'>Download</th>"
91              "<th class='left'>Author</th>"
92              "<th class='left' colspan='2'>Age</th></tr>\n");
93 }
94
95 static int print_tag(struct refinfo *ref)
96 {
97         struct tag *tag = NULL;
98         struct taginfo *info = NULL;
99         char *name = (char *)ref->refname;
100         struct object *obj = ref->object;
101
102         if (obj->type == OBJ_TAG) {
103                 tag = (struct tag *)obj;
104                 obj = tag->tagged;
105                 info = ref->tag;
106                 if (!info)
107                         return 1;
108         }
109
110         html("<tr><td>");
111         cgit_tag_link(name, NULL, NULL, name);
112         html("</td><td>");
113         if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
114                 cgit_print_snapshot_links(ctx.repo, name, "&nbsp;&nbsp;");
115         else
116                 cgit_object_link(obj);
117         html("</td><td>");
118         if (info) {
119                 if (info->tagger) {
120                         cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
121                         html_txt(info->tagger);
122                         cgit_close_filter(ctx.repo->email_filter);
123                 }
124         } else if (ref->object->type == OBJ_COMMIT) {
125                 cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
126                 html_txt(ref->commit->author);
127                 cgit_close_filter(ctx.repo->email_filter);
128         }
129         html("</td><td colspan='2'>");
130         if (info) {
131                 if (info->tagger_date > 0)
132                         cgit_print_age(info->tagger_date, info->tagger_tz, -1);
133         } else if (ref->object->type == OBJ_COMMIT) {
134                 cgit_print_age(ref->commit->commit->date, 0, -1);
135         }
136         html("</td></tr>\n");
137
138         return 0;
139 }
140
141 static void print_refs_link(const char *path)
142 {
143         html("<tr class='nohover'><td colspan='5'>");
144         cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
145         html("</td></tr>");
146 }
147
148 void cgit_print_branches(int maxcount)
149 {
150         struct reflist list;
151         int i;
152
153         html("<tr class='nohover'><th class='left'>Branch</th>"
154              "<th class='left'>Commit message</th>"
155              "<th class='left'>Author</th>"
156              "<th class='left' colspan='2'>Age</th></tr>\n");
157
158         list.refs = NULL;
159         list.alloc = list.count = 0;
160         refs_for_each_branch_ref(get_main_ref_store(the_repository),
161                                  cgit_refs_cb, &list);
162         if (ctx.repo->enable_remote_branches)
163                 refs_for_each_remote_ref(get_main_ref_store(the_repository),
164                                          cgit_refs_cb, &list);
165
166         if (maxcount == 0 || maxcount > list.count)
167                 maxcount = list.count;
168
169         qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
170         if (ctx.repo->branch_sort == 0)
171                 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
172
173         for (i = 0; i < maxcount; i++)
174                 print_branch(list.refs[i]);
175
176         if (maxcount < list.count)
177                 print_refs_link("heads");
178
179         cgit_free_reflist_inner(&list);
180 }
181
182 void cgit_print_tags(int maxcount)
183 {
184         struct reflist list;
185         int i;
186
187         list.refs = NULL;
188         list.alloc = list.count = 0;
189         refs_for_each_tag_ref(get_main_ref_store(the_repository),
190                               cgit_refs_cb, &list);
191         if (list.count == 0)
192                 return;
193         qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
194         if (!maxcount)
195                 maxcount = list.count;
196         else if (maxcount > list.count)
197                 maxcount = list.count;
198         print_tag_header();
199         for (i = 0; i < maxcount; i++)
200                 print_tag(list.refs[i]);
201
202         if (maxcount < list.count)
203                 print_refs_link("tags");
204
205         cgit_free_reflist_inner(&list);
206 }
207
208 void cgit_print_refs(void)
209 {
210         cgit_print_layout_start();
211         html("<table class='list nowrap'>");
212
213         if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
214                 cgit_print_branches(0);
215         else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
216                 cgit_print_tags(0);
217         else {
218                 cgit_print_branches(0);
219                 html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
220                 cgit_print_tags(0);
221         }
222         html("</table>");
223         cgit_print_layout_end();
224 }