all repos — cgit @ c4b8db3f0973bc63d60806d197b55dc2901870f0

a hyperfast web frontend for git written in c

ui-tree.c (view raw)

  1/* ui-tree.c: functions for tree output
  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 <ctype.h>
 10#include "cgit.h"
 11#include "html.h"
 12#include "ui-shared.h"
 13
 14char *curr_rev;
 15char *match_path;
 16int header = 0;
 17static int state;
 18
 19static void print_text_buffer(const char *name, char *buf, unsigned long size)
 20{
 21	unsigned long lineno, idx;
 22	const char *numberfmt =
 23		"<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
 24
 25	html("<table summary='blob content' class='blob'>\n");
 26
 27	if (ctx.cfg.enable_tree_linenumbers) {
 28		html("<tr><td class='linenumbers'><pre>");
 29		idx = 0;
 30		lineno = 0;
 31
 32		if (size) {
 33			htmlf(numberfmt, ++lineno);
 34			while (idx < size - 1) { // skip absolute last newline
 35				if (buf[idx] == '\n')
 36					htmlf(numberfmt, ++lineno);
 37				idx++;
 38			}
 39		}
 40		html("</pre></td>\n");
 41	}
 42	else {
 43		html("<tr>\n");
 44	}
 45
 46	if (ctx.repo->source_filter) {
 47		html("<td class='lines'><pre><code>");
 48		ctx.repo->source_filter->argv[1] = xstrdup(name);
 49		cgit_open_filter(ctx.repo->source_filter);
 50		html_raw(buf, size);
 51		cgit_close_filter(ctx.repo->source_filter);
 52		free(ctx.repo->source_filter->argv[1]);
 53		ctx.repo->source_filter->argv[1] = NULL;
 54		html("</code></pre></td></tr></table>\n");
 55		return;
 56	}
 57
 58	html("<td class='lines'><pre><code>");
 59	html_txt(buf);
 60	html("</code></pre></td></tr></table>\n");
 61}
 62
 63#define ROWLEN 32
 64
 65static void print_binary_buffer(char *buf, unsigned long size)
 66{
 67	unsigned long ofs, idx;
 68	static char ascii[ROWLEN + 1];
 69
 70	html("<table summary='blob content' class='bin-blob'>\n");
 71	html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
 72	for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
 73		htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
 74		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 75			htmlf("%*s%02x",
 76			      idx == 16 ? 4 : 1, "",
 77			      buf[idx] & 0xff);
 78		html(" </td><td class='hex'>");
 79		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 80			ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
 81		ascii[idx] = '\0';
 82		html_txt(ascii);
 83		html("</td></tr>\n");
 84	}
 85	html("</table>\n");
 86}
 87
 88static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
 89{
 90	enum object_type type;
 91	char *buf;
 92	unsigned long size;
 93
 94	type = sha1_object_info(sha1, &size);
 95	if (type == OBJ_BAD) {
 96		cgit_print_error(fmt("Bad object name: %s",
 97				     sha1_to_hex(sha1)));
 98		return;
 99	}
100
101	buf = read_sha1_file(sha1, &type, &size);
102	if (!buf) {
103		cgit_print_error(fmt("Error reading object %s",
104				     sha1_to_hex(sha1)));
105		return;
106	}
107
108	htmlf("blob: %s (", sha1_to_hex(sha1));
109	cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
110		        rev, path);
111	html(")\n");
112
113	if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
114		htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
115				size / 1024, ctx.cfg.max_blob_size);
116		return;
117	}
118
119	if (buffer_is_binary(buf, size))
120		print_binary_buffer(buf, size);
121	else
122		print_text_buffer(basename, buf, size);
123}
124
125
126static int ls_item(const unsigned char *sha1, const char *base, int baselen,
127		   const char *pathname, unsigned int mode, int stage,
128		   void *cbdata)
129{
130	char *name;
131	char *fullpath;
132	char *class;
133	enum object_type type;
134	unsigned long size = 0;
135
136	name = xstrdup(pathname);
137	fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
138		       ctx.qry.path ? "/" : "", name);
139
140	if (!S_ISGITLINK(mode)) {
141		type = sha1_object_info(sha1, &size);
142		if (type == OBJ_BAD) {
143			htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
144			      name,
145			      sha1_to_hex(sha1));
146			return 0;
147		}
148	}
149
150	html("<tr><td class='ls-mode'>");
151	cgit_print_filemode(mode);
152	html("</td><td>");
153	if (S_ISGITLINK(mode)) {
154		cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
155	} else if (S_ISDIR(mode)) {
156		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
157			       curr_rev, fullpath);
158	} else {
159		class = strrchr(name, '.');
160		if (class != NULL) {
161			class = fmt("ls-blob %s", class + 1);
162		} else
163			class = "ls-blob";
164		cgit_tree_link(name, NULL, class, ctx.qry.head,
165			       curr_rev, fullpath);
166	}
167	htmlf("</td><td class='ls-size'>%li</td>", size);
168
169	html("<td>");
170	cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
171		      fullpath, 0, NULL, NULL, ctx.qry.showmsg);
172	if (ctx.repo->max_stats)
173		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
174				fullpath);
175	if (!S_ISGITLINK(mode))
176		cgit_plain_link("plain", NULL, "button", ctx.qry.head, curr_rev,
177				fullpath);
178	html("</td></tr>\n");
179	free(name);
180	return 0;
181}
182
183static void ls_head()
184{
185	html("<table summary='tree listing' class='list'>\n");
186	html("<tr class='nohover'>");
187	html("<th class='left'>Mode</th>");
188	html("<th class='left'>Name</th>");
189	html("<th class='right'>Size</th>");
190	html("<th/>");
191	html("</tr>\n");
192	header = 1;
193}
194
195static void ls_tail()
196{
197	if (!header)
198		return;
199	html("</table>\n");
200	header = 0;
201}
202
203static void ls_tree(const unsigned char *sha1, char *path)
204{
205	struct tree *tree;
206	struct pathspec paths = {
207		.nr = 0
208	};
209
210	tree = parse_tree_indirect(sha1);
211	if (!tree) {
212		cgit_print_error(fmt("Not a tree object: %s",
213				     sha1_to_hex(sha1)));
214		return;
215	}
216
217	ls_head();
218	read_tree_recursive(tree, "", 0, 1, &paths, ls_item, NULL);
219	ls_tail();
220}
221
222
223static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
224		     const char *pathname, unsigned mode, int stage,
225		     void *cbdata)
226{
227	static char buffer[PATH_MAX];
228
229	if (state == 0) {
230		memcpy(buffer, base, baselen);
231		strcpy(buffer + baselen, pathname);
232		if (strcmp(match_path, buffer))
233			return READ_TREE_RECURSIVE;
234
235		if (S_ISDIR(mode)) {
236			state = 1;
237			ls_head();
238			return READ_TREE_RECURSIVE;
239		} else {
240			print_object(sha1, buffer, pathname, curr_rev);
241			return 0;
242		}
243	}
244	ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
245	return 0;
246}
247
248
249/*
250 * Show a tree or a blob
251 *   rev:  the commit pointing at the root tree object
252 *   path: path to tree or blob
253 */
254void cgit_print_tree(const char *rev, char *path)
255{
256	unsigned char sha1[20];
257	struct commit *commit;
258	struct pathspec_item path_items = {
259		.match = path,
260		.len = path ? strlen(path) : 0
261	};
262	struct pathspec paths = {
263		.nr = path ? 1 : 0,
264		.items = &path_items
265	};
266
267	if (!rev)
268		rev = ctx.qry.head;
269
270	curr_rev = xstrdup(rev);
271	if (get_sha1(rev, sha1)) {
272		cgit_print_error(fmt("Invalid revision name: %s", rev));
273		return;
274	}
275	commit = lookup_commit_reference(sha1);
276	if (!commit || parse_commit(commit)) {
277		cgit_print_error(fmt("Invalid commit reference: %s", rev));
278		return;
279	}
280
281	if (path == NULL) {
282		ls_tree(commit->tree->object.sha1, NULL);
283		return;
284	}
285
286	match_path = path;
287	state = 0;
288	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, NULL);
289	ls_tail();
290}