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