all repos — cgit @ 5b1f42ffeec7e315fc4fcff5145e5b0adca30715

a hyperfast web frontend for git written in c

ui-refs.c (view raw)

  1/* ui-refs.c: browse symbolic refs
  2 *
  3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
  4 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include "cgit.h"
 10#include "ui-refs.h"
 11#include "html.h"
 12#include "ui-shared.h"
 13
 14static inline int cmp_age(int age1, int age2)
 15{
 16	/* age1 and age2 are assumed to be non-negative */
 17	return age2 - age1;
 18}
 19
 20static int cmp_ref_name(const void *a, const void *b)
 21{
 22	struct refinfo *r1 = *(struct refinfo **)a;
 23	struct refinfo *r2 = *(struct refinfo **)b;
 24
 25	return strcmp(r1->refname, r2->refname);
 26}
 27
 28static int cmp_branch_age(const void *a, const void *b)
 29{
 30	struct refinfo *r1 = *(struct refinfo **)a;
 31	struct refinfo *r2 = *(struct refinfo **)b;
 32
 33	return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
 34}
 35
 36static int get_ref_age(struct refinfo *ref)
 37{
 38	if (!ref->object)
 39		return 0;
 40	switch (ref->object->type) {
 41	case OBJ_TAG:
 42		return ref->tag ? ref->tag->tagger_date : 0;
 43	case OBJ_COMMIT:
 44		return ref->commit ? ref->commit->committer_date : 0;
 45	}
 46	return 0;
 47}
 48
 49static int cmp_tag_age(const void *a, const void *b)
 50{
 51	struct refinfo *r1 = *(struct refinfo **)a;
 52	struct refinfo *r2 = *(struct refinfo **)b;
 53
 54	return cmp_age(get_ref_age(r1), get_ref_age(r2));
 55}
 56
 57static int print_branch(struct refinfo *ref)
 58{
 59	struct commitinfo *info = ref->commit;
 60	char *name = (char *)ref->refname;
 61
 62	if (!info)
 63		return 1;
 64	html("<tr><td>");
 65	cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
 66		      ctx.qry.showmsg, 0);
 67	html("</td><td>");
 68
 69	if (ref->object->type == OBJ_COMMIT) {
 70		cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL);
 71		html("</td><td>");
 72		cgit_open_filter(ctx.repo->email_filter, info->author_email, "refs");
 73		html_txt(info->author);
 74		cgit_close_filter(ctx.repo->email_filter);
 75		html("</td><td colspan='2'>");
 76		cgit_print_age(info->committer_date, info->committer_tz, -1);
 77	} else {
 78		html("</td><td></td><td>");
 79		cgit_object_link(ref->object);
 80	}
 81	html("</td></tr>\n");
 82	return 0;
 83}
 84
 85static void print_tag_header(void)
 86{
 87	html("<tr class='nohover'><th class='left'>Tag</th>"
 88	     "<th class='left'>Download</th>"
 89	     "<th class='left'>Author</th>"
 90	     "<th class='left' colspan='2'>Age</th></tr>\n");
 91}
 92
 93static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
 94{
 95	const struct cgit_snapshot_format* f;
 96	const char *basename;
 97	struct strbuf filename = STRBUF_INIT;
 98	size_t prefixlen;
 99
100	basename = cgit_snapshot_prefix(repo);
101	if (starts_with(ref, basename))
102		strbuf_addstr(&filename, ref);
103	else
104		cgit_compose_snapshot_prefix(&filename, basename, ref);
105	prefixlen = filename.len;
106	for (f = cgit_snapshot_formats; f->suffix; f++) {
107		if (!(repo->snapshots & f->bit))
108			continue;
109		strbuf_setlen(&filename, prefixlen);
110		strbuf_addstr(&filename, f->suffix);
111		cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
112		html("&nbsp;&nbsp;");
113	}
114
115	strbuf_release(&filename);
116}
117
118static int print_tag(struct refinfo *ref)
119{
120	struct tag *tag = NULL;
121	struct taginfo *info = NULL;
122	char *name = (char *)ref->refname;
123	struct object *obj = ref->object;
124
125	if (obj->type == OBJ_TAG) {
126		tag = (struct tag *)obj;
127		obj = tag->tagged;
128		info = ref->tag;
129		if (!info)
130			return 1;
131	}
132
133	html("<tr><td>");
134	cgit_tag_link(name, NULL, NULL, name);
135	html("</td><td>");
136	if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT))
137		print_tag_downloads(ctx.repo, name);
138	else
139		cgit_object_link(obj);
140	html("</td><td>");
141	if (info) {
142		if (info->tagger) {
143			cgit_open_filter(ctx.repo->email_filter, info->tagger_email, "refs");
144			html_txt(info->tagger);
145			cgit_close_filter(ctx.repo->email_filter);
146		}
147	} else if (ref->object->type == OBJ_COMMIT) {
148		cgit_open_filter(ctx.repo->email_filter, ref->commit->author_email, "refs");
149		html_txt(ref->commit->author);
150		cgit_close_filter(ctx.repo->email_filter);
151	}
152	html("</td><td colspan='2'>");
153	if (info) {
154		if (info->tagger_date > 0)
155			cgit_print_age(info->tagger_date, info->tagger_tz, -1);
156	} else if (ref->object->type == OBJ_COMMIT) {
157		cgit_print_age(ref->commit->commit->date, 0, -1);
158	}
159	html("</td></tr>\n");
160
161	return 0;
162}
163
164static void print_refs_link(char *path)
165{
166	html("<tr class='nohover'><td colspan='5'>");
167	cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
168	html("</td></tr>");
169}
170
171void 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'>Commit message</th>"
178	     "<th class='left'>Author</th>"
179	     "<th class='left' colspan='2'>Age</th></tr>\n");
180
181	list.refs = NULL;
182	list.alloc = list.count = 0;
183	for_each_branch_ref(cgit_refs_cb, &list);
184	if (ctx.repo->enable_remote_branches)
185		for_each_remote_ref(cgit_refs_cb, &list);
186
187	if (maxcount == 0 || maxcount > list.count)
188		maxcount = list.count;
189
190	qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
191	if (ctx.repo->branch_sort == 0)
192		qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
193
194	for (i = 0; i < maxcount; i++)
195		print_branch(list.refs[i]);
196
197	if (maxcount < list.count)
198		print_refs_link("heads");
199
200	cgit_free_reflist_inner(&list);
201}
202
203void cgit_print_tags(int maxcount)
204{
205	struct reflist list;
206	int i;
207
208	list.refs = NULL;
209	list.alloc = list.count = 0;
210	for_each_tag_ref(cgit_refs_cb, &list);
211	if (list.count == 0)
212		return;
213	qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
214	if (!maxcount)
215		maxcount = list.count;
216	else if (maxcount > list.count)
217		maxcount = list.count;
218	print_tag_header();
219	for (i = 0; i < maxcount; i++)
220		print_tag(list.refs[i]);
221
222	if (maxcount < list.count)
223		print_refs_link("tags");
224
225	cgit_free_reflist_inner(&list);
226}
227
228void cgit_print_refs(void)
229{
230	cgit_print_layout_start();
231	html("<table class='list nowrap'>");
232
233	if (ctx.qry.path && starts_with(ctx.qry.path, "heads"))
234		cgit_print_branches(0);
235	else if (ctx.qry.path && starts_with(ctx.qry.path, "tags"))
236		cgit_print_tags(0);
237	else {
238		cgit_print_branches(0);
239		html("<tr class='nohover'><td colspan='5'>&nbsp;</td></tr>");
240		cgit_print_tags(0);
241	}
242	html("</table>");
243	cgit_print_layout_end();
244}