all repos — cgit @ f405d0bf75427c627778027e9900359335d6774e

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