]> gitweb.ps.run Git - ps-cgit/blob - ui-summary.c
Make print_branch() handle refs not pointing at commits
[ps-cgit] / ui-summary.c
1 /* ui-summary.c: functions for generating repo summary page
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"
10
11 static int header;
12
13 static int cmp_age(int age1, int age2)
14 {
15         if (age1 != 0 && age2 != 0)
16                 return age2 - age1;
17
18         if (age1 == 0 && age2 == 0)
19                 return 0;
20
21         if (age1 == 0)
22                 return +1;
23
24         return -1;
25 }
26
27 static int cmp_ref_name(const void *a, const void *b)
28 {
29         struct refinfo *r1 = *(struct refinfo **)a;
30         struct refinfo *r2 = *(struct refinfo **)b;
31
32         return strcmp(r1->refname, r2->refname);
33 }
34
35 static int cmp_branch_age(const void *a, const void *b)
36 {
37         struct refinfo *r1 = *(struct refinfo **)a;
38         struct refinfo *r2 = *(struct refinfo **)b;
39
40         return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
41 }
42
43 static int cmp_tag_age(const void *a, const void *b)
44 {
45         struct refinfo *r1 = *(struct refinfo **)a;
46         struct refinfo *r2 = *(struct refinfo **)b;
47
48         return cmp_age(r1->tag->tagger_date, r2->tag->tagger_date);
49 }
50
51 static int print_branch(struct refinfo *ref)
52 {
53         struct commitinfo *info = ref->commit;
54         char *name = (char *)ref->refname;
55
56         if (!info)
57                 return 1;
58         html("<tr><td>");
59         cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0);
60         html("</td><td>");
61
62         if (ref->object->type == OBJ_COMMIT) {
63                 cgit_print_age(info->commit->date, -1, NULL);
64                 html("</td><td>");
65                 html_txt(info->author);
66                 html("</td><td>");
67                 cgit_commit_link(info->subject, NULL, NULL, name, NULL);
68         } else {
69                 html("</td><td></td><td>");
70                 cgit_object_link(ref->object);
71         }
72         html("</td></tr>\n");
73         return 0;
74 }
75
76 static void print_tag_header()
77 {
78         html("<tr class='nohover'><th class='left'>Tag</th>"
79              "<th class='left'>Age</th>"
80              "<th class='left'>Author</th>"
81              "<th class='left'>Reference</th></tr>\n");
82         header = 1;
83 }
84
85 static int print_tag(struct refinfo *ref)
86 {
87         struct tag *tag;
88         struct taginfo *info;
89         char *url, *name = (char *)ref->refname;
90
91         if (ref->object->type == OBJ_TAG) {
92                 tag = (struct tag *)ref->object;
93                 info = ref->tag;
94                 if (!tag || !info)
95                         return 1;
96                 html("<tr><td>");
97                 url = cgit_pageurl(cgit_query_repo, "tag",
98                                    fmt("id=%s", name));
99                 html_link_open(url, NULL, NULL);
100                 html_txt(name);
101                 html_link_close();
102                 html("</td><td>");
103                 if (info->tagger_date > 0)
104                         cgit_print_age(info->tagger_date, -1, NULL);
105                 html("</td><td>");
106                 if (info->tagger)
107                         html(info->tagger);
108                 html("</td><td>");
109                 cgit_object_link(tag->tagged);
110                 html("</td></tr>\n");
111         } else {
112                 if (!header)
113                         print_tag_header();
114                 html("<tr><td>");
115                 html_txt(name);
116                 html("</td><td colspan='2'/><td>");
117                 cgit_object_link(ref->object);
118                 html("</td></tr>\n");
119         }
120         return 0;
121 }
122
123 static int cgit_print_archive_cb(const char *refname, const unsigned char *sha1,
124                                  int flags, void *cb_data)
125 {
126         struct tag *tag;
127         struct taginfo *info;
128         struct object *obj;
129         char buf[256], *url;
130         unsigned char fileid[20];
131
132         if (prefixcmp(refname, "refs/archives"))
133                 return 0;
134         strncpy(buf, refname+14, sizeof(buf));
135         obj = parse_object(sha1);
136         if (!obj)
137                 return 1;
138         if (obj->type == OBJ_TAG) {
139                 tag = lookup_tag(sha1);
140                 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
141                         return 0;
142                 hashcpy(fileid, tag->tagged->sha1);
143         } else if (obj->type != OBJ_BLOB) {
144                 return 0;
145         } else {
146                 hashcpy(fileid, sha1);
147         }
148         if (!header) {
149                 html("<table id='downloads'>");
150                 html("<tr><th>Downloads</th></tr>");
151                 header = 1;
152         }
153         html("<tr><td>");
154         url = cgit_pageurl(cgit_query_repo, "blob",
155                            fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
156                                buf));
157         html_link_open(url, NULL, NULL);
158         html_txt(buf);
159         html_link_close();
160         html("</td></tr>");
161         return 0;
162 }
163
164 static void print_refs_link(char *path)
165 {
166         html("<tr class='nohover'><td colspan='4'>");
167         cgit_refs_link("[...]", NULL, NULL, cgit_query_head, NULL, path);
168         html("</td></tr>");
169 }
170
171 void cgit_print_branches(int maxcount)
172 {
173         struct reflist list;
174         int i;
175
176         html("<tr class='nohover'><th class='left'>Branch</th>"
177              "<th class='left'>Idle</th>"
178              "<th class='left'>Author</th>"
179              "<th class='left'>Head commit</th></tr>\n");
180
181         list.refs = NULL;
182         list.alloc = list.count = 0;
183         for_each_branch_ref(cgit_refs_cb, &list);
184
185         if (maxcount == 0 || maxcount > list.count)
186                 maxcount = list.count;
187
188         if (maxcount < list.count) {
189                 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
190                 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
191         }
192
193         for(i=0; i<maxcount; i++)
194                 print_branch(list.refs[i]);
195
196         if (maxcount < list.count)
197                 print_refs_link("heads");
198 }
199
200 void cgit_print_tags(int maxcount)
201 {
202         struct reflist list;
203         int i;
204
205         header = 0;
206         list.refs = NULL;
207         list.alloc = list.count = 0;
208         for_each_tag_ref(cgit_refs_cb, &list);
209         if (list.count == 0)
210                 return;
211         qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
212         if (!maxcount)
213                 maxcount = list.count;
214         else if (maxcount > list.count)
215                 maxcount = list.count;
216         print_tag_header();
217         for(i=0; i<maxcount; i++)
218                 print_tag(list.refs[i]);
219
220         if (maxcount < list.count)
221                 print_refs_link("tags");
222 }
223
224 static void cgit_print_archives()
225 {
226         header = 0;
227         for_each_ref(cgit_print_archive_cb, NULL);
228         if (header)
229                 html("</table>");
230 }
231
232 void cgit_print_summary()
233 {
234         html("<div id='summary'>");
235         cgit_print_archives();
236         html("<h2>");
237         html_txt(cgit_repo->name);
238         html(" - ");
239         html_txt(cgit_repo->desc);
240         html("</h2>");
241         if (cgit_repo->readme)
242                 html_include(cgit_repo->readme);
243         html("</div>");
244         if (cgit_summary_log > 0)
245                 cgit_print_log(cgit_query_head, 0, cgit_summary_log, NULL, NULL, NULL, 0);
246         html("<table class='list nowrap'>");
247         if (cgit_summary_log > 0)
248                 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
249         cgit_print_branches(cgit_summary_branches);
250         html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
251         cgit_print_tags(cgit_summary_tags);
252         html("</table>");
253 }