all repos — cgit @ fd90d2826a979f3844312718ad130880cf19d52c

a hyperfast web frontend for git written in c

ui-summary.c (view raw)

  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
 11static int header;
 12
 13static 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
 27static 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
 35static 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
 43static 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
 51static 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, NULL, NULL);
 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
 76static 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
 85static 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
123static void print_refs_link(char *path)
124{
125	html("<tr class='nohover'><td colspan='4'>");
126	cgit_refs_link("[...]", NULL, NULL, cgit_query_head, NULL, path);
127	html("</td></tr>");
128}
129
130void cgit_print_branches(int maxcount)
131{
132	struct reflist list;
133	int i;
134
135	html("<tr class='nohover'><th class='left'>Branch</th>"
136	     "<th class='left'>Idle</th>"
137	     "<th class='left'>Author</th>"
138	     "<th class='left'>Head commit</th></tr>\n");
139
140	list.refs = NULL;
141	list.alloc = list.count = 0;
142	for_each_branch_ref(cgit_refs_cb, &list);
143
144	if (maxcount == 0 || maxcount > list.count)
145		maxcount = list.count;
146
147	if (maxcount < list.count) {
148		qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
149		qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
150	}
151
152	for(i=0; i<maxcount; i++)
153		print_branch(list.refs[i]);
154
155	if (maxcount < list.count)
156		print_refs_link("heads");
157}
158
159void cgit_print_tags(int maxcount)
160{
161	struct reflist list;
162	int i;
163
164	header = 0;
165	list.refs = NULL;
166	list.alloc = list.count = 0;
167	for_each_tag_ref(cgit_refs_cb, &list);
168	if (list.count == 0)
169		return;
170	qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
171	if (!maxcount)
172		maxcount = list.count;
173	else if (maxcount > list.count)
174		maxcount = list.count;
175	print_tag_header();
176	for(i=0; i<maxcount; i++)
177		print_tag(list.refs[i]);
178
179	if (maxcount < list.count)
180		print_refs_link("tags");
181}
182
183void cgit_print_summary()
184{
185	if (cgit_repo->readme) {
186		html("<div id='summary'>");
187		html_include(cgit_repo->readme);
188		html("</div>");
189	}
190	if (cgit_summary_log > 0)
191		cgit_print_log(cgit_query_head, 0, cgit_summary_log, NULL,
192			       NULL, NULL, 0);
193	html("<table summary='repository info' class='list nowrap'>");
194	if (cgit_summary_log > 0)
195		html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
196	cgit_print_branches(cgit_summary_branches);
197	html("<tr class='nohover'><td colspan='4'>&nbsp;</td></tr>");
198	cgit_print_tags(cgit_summary_tags);
199	html("</table>");
200}