]> gitweb.ps.run Git - ps-cgit/blob - ui-summary.c
ui-diff: close td/tr/table properly
[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 cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
14                                 int flags, void *cb_data)
15 {
16         struct commit *commit;
17         struct commitinfo *info;
18         char buf[256], *url;
19
20         strncpy(buf, refname, sizeof(buf));
21         commit = lookup_commit(sha1);
22         // object is not really parsed at this point, because of some fallout
23         // from previous calls to git functions in cgit_print_log()
24         commit->object.parsed = 0;
25         if (commit && !parse_commit(commit)){
26                 info = cgit_parse_commit(commit);
27                 html("<tr><td>");
28                 url = cgit_pageurl(cgit_query_repo, "log",
29                                    fmt("h=%s", refname));
30                 html_link_open(url, NULL, NULL);
31                 html_txt(buf);
32                 html_link_close();
33                 html("</td><td>");
34                 cgit_print_age(commit->date, -1, NULL);
35                 html("</td><td>");
36                 html_txt(info->author);
37                 html("</td><td>");
38                 url = cgit_pageurl(cgit_query_repo, "commit",
39                                    fmt("h=%s", sha1_to_hex(sha1)));
40                 html_link_open(url, NULL, NULL);
41                 html_ntxt(cgit_max_msg_len, info->subject);
42                 html_link_close();
43                 html("</td></tr>\n");
44                 cgit_free_commitinfo(info);
45         } else {
46                 html("<tr><td>");
47                 html_txt(buf);
48                 html("</td><td colspan='3'>");
49                 htmlf("*** bad ref %s ***", sha1_to_hex(sha1));
50                 html("</td></tr>\n");
51         }
52         return 0;
53 }
54
55
56 static void cgit_print_object_ref(struct object *obj)
57 {
58         char *page, *arg, *url;
59
60         if (obj->type == OBJ_COMMIT) {
61                 page = "commit";
62                 arg = "h";
63         } else if (obj->type == OBJ_TREE) {
64                 page = "tree";
65                 arg = "id";
66         } else {
67                 page = "view";
68                 arg = "id";
69         }
70
71         url = cgit_pageurl(cgit_query_repo, page,
72                            fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
73         html_link_open(url, NULL, NULL);
74         htmlf("%s %s", typename(obj->type),
75               sha1_to_hex(obj->sha1));
76         html_link_close();
77 }
78
79 static void print_tag_header()
80 {
81         html("<tr class='nohover'><th class='left'>Tag</th>"
82              "<th class='left'>Age</th>"
83              "<th class='left'>Author</th>"
84              "<th class='left'>Reference</th></tr>\n");
85         header = 1;
86 }
87
88 static int cgit_print_tag_cb(const char *refname, const unsigned char *sha1,
89                                 int flags, void *cb_data)
90 {
91         struct tag *tag;
92         struct taginfo *info;
93         struct object *obj;
94         char buf[256], *url;
95
96         strncpy(buf, refname, sizeof(buf));
97         obj = parse_object(sha1);
98         if (!obj)
99                 return 1;
100         if (obj->type == OBJ_TAG) {
101                 tag = lookup_tag(sha1);
102                 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
103                         return 2;
104                 if (!header)
105                         print_tag_header();
106                 html("<tr><td>");
107                 url = cgit_pageurl(cgit_query_repo, "view",
108                                    fmt("id=%s", sha1_to_hex(sha1)));
109                 html_link_open(url, NULL, NULL);
110                 html_txt(buf);
111                 html_link_close();
112                 html("</td><td>");
113                 if (info->tagger_date > 0)
114                         cgit_print_age(info->tagger_date, -1, NULL);
115                 html("</td><td>");
116                 if (info->tagger)
117                         html(info->tagger);
118                 html("</td><td>");
119                 cgit_print_object_ref(tag->tagged);
120                 html("</td></tr>\n");
121         } else {
122                 if (!header)
123                         print_tag_header();
124                 html("<tr><td>");
125                 html_txt(buf);
126                 html("</td><td colspan='2'/><td>");
127                 cgit_print_object_ref(obj);
128                 html("</td></tr>\n");
129         }
130         return 0;
131 }
132
133 static int cgit_print_archive_cb(const char *refname, const unsigned char *sha1,
134                                  int flags, void *cb_data)
135 {
136         struct tag *tag;
137         struct taginfo *info;
138         struct object *obj;
139         char buf[256], *url;
140         unsigned char fileid[20];
141
142         if (prefixcmp(refname, "refs/archives"))
143                 return 0;
144         strncpy(buf, refname+14, sizeof(buf));
145         obj = parse_object(sha1);
146         if (!obj)
147                 return 1;
148         if (obj->type == OBJ_TAG) {
149                 tag = lookup_tag(sha1);
150                 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
151                         return 0;
152                 hashcpy(fileid, tag->tagged->sha1);
153         } else if (obj->type != OBJ_BLOB) {
154                 return 0;
155         } else {
156                 hashcpy(fileid, sha1);
157         }
158         if (!header) {
159                 html("<table id='downloads'>");
160                 html("<tr><th>Downloads</th></tr>");
161                 header = 1;
162         }
163         html("<tr><td>");
164         url = cgit_pageurl(cgit_query_repo, "blob",
165                            fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
166                                buf));
167         html_link_open(url, NULL, NULL);
168         html_txt(buf);
169         html_link_close();
170         html("</td></tr>");
171         return 0;
172 }
173
174 static void cgit_print_branches()
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         for_each_branch_ref(cgit_print_branch_cb, NULL);
181 }
182
183 static void cgit_print_tags()
184 {
185         header = 0;
186         for_each_tag_ref(cgit_print_tag_cb, NULL);
187 }
188
189 static void cgit_print_archives()
190 {
191         header = 0;
192         for_each_ref(cgit_print_archive_cb, NULL);
193         if (header)
194                 html("</table>");
195 }
196
197 void cgit_print_summary()
198 {
199         html("<div id='summary'>");
200         cgit_print_archives();
201         html("<h2>");
202         html_txt(cgit_repo->name);
203         html(" - ");
204         html_txt(cgit_repo->desc);
205         html("</h2>");
206         if (cgit_repo->readme)
207                 html_include(cgit_repo->readme);
208         html("</div>");
209         if (cgit_summary_log > 0)
210                 cgit_print_log(cgit_query_head, 0, cgit_summary_log, NULL, NULL, 0);
211         html("<table class='list nowrap'>");
212         if (cgit_summary_log > 0)
213                 html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
214         cgit_print_branches();
215         html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
216         cgit_print_tags();
217         html("</table>");
218 }