all repos — cgit @ 9337c7ee83221d48d02c3c7b5c9dcbaca23ad92f

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-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-tree.h"
 11#include "html.h"
 12#include "ui-shared.h"
 13
 14struct walk_tree_context {
 15	char *curr_rev;
 16	char *match_path;
 17	int state;
 18};
 19
 20static void print_text_buffer(const char *name, char *buf, unsigned long size)
 21{
 22	unsigned long lineno, idx;
 23	const char *numberfmt = "<a id='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		char *filter_arg = xstrdup(name);
 48		html("<td class='lines'><pre><code>");
 49		cgit_open_filter(ctx.repo->source_filter, filter_arg);
 50		html_raw(buf, size);
 51		cgit_close_filter(ctx.repo->source_filter);
 52		free(filter_arg);
 53		html("</code></pre></td></tr></table>\n");
 54		return;
 55	}
 56
 57	html("<td class='lines'><pre><code>");
 58	html_txt(buf);
 59	html("</code></pre></td></tr></table>\n");
 60}
 61
 62#define ROWLEN 32
 63
 64static void print_binary_buffer(char *buf, unsigned long size)
 65{
 66	unsigned long ofs, idx;
 67	static char ascii[ROWLEN + 1];
 68
 69	html("<table summary='blob content' class='bin-blob'>\n");
 70	html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
 71	for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
 72		htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
 73		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 74			htmlf("%*s%02x",
 75			      idx == 16 ? 4 : 1, "",
 76			      buf[idx] & 0xff);
 77		html(" </td><td class='hex'>");
 78		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 79			ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
 80		ascii[idx] = '\0';
 81		html_txt(ascii);
 82		html("</td></tr>\n");
 83	}
 84	html("</table>\n");
 85}
 86
 87static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
 88{
 89	enum object_type type;
 90	char *buf;
 91	unsigned long size;
 92
 93	type = sha1_object_info(sha1, &size);
 94	if (type == OBJ_BAD) {
 95		cgit_print_error_page(404, "Not found",
 96			"Bad object name: %s", sha1_to_hex(sha1));
 97		return;
 98	}
 99
100	buf = read_sha1_file(sha1, &type, &size);
101	if (!buf) {
102		cgit_print_error_page(500, "Internal server error",
103			"Error reading object %s", sha1_to_hex(sha1));
104		return;
105	}
106
107	cgit_set_title_from_path(path);
108
109	cgit_print_layout_start();
110	htmlf("blob: %s (", sha1_to_hex(sha1));
111	cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
112		        rev, path);
113	html(")\n");
114
115	if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
116		htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
117				size / 1024, ctx.cfg.max_blob_size);
118		return;
119	}
120
121	if (buffer_is_binary(buf, size))
122		print_binary_buffer(buf, size);
123	else
124		print_text_buffer(basename, buf, size);
125}
126
127struct single_tree_ctx {
128	struct strbuf *path;
129	struct object_id oid;
130	char *name;
131	size_t count;
132};
133
134static int single_tree_cb(const unsigned char *sha1, struct strbuf *base,
135			  const char *pathname, unsigned mode, int stage,
136			  void *cbdata)
137{
138	struct single_tree_ctx *ctx = cbdata;
139
140	if (++ctx->count > 1)
141		return -1;
142
143	if (!S_ISDIR(mode)) {
144		ctx->count = 2;
145		return -1;
146	}
147
148	ctx->name = xstrdup(pathname);
149	hashcpy(ctx->oid.hash, sha1);
150	strbuf_addf(ctx->path, "/%s", pathname);
151	return 0;
152}
153
154static void write_tree_link(const unsigned char *sha1, char *name,
155			    char *rev, struct strbuf *fullpath)
156{
157	size_t initial_length = fullpath->len;
158	struct tree *tree;
159	struct single_tree_ctx tree_ctx = {
160		.path = fullpath,
161		.count = 1,
162	};
163	struct pathspec paths = {
164		.nr = 0
165	};
166
167	hashcpy(tree_ctx.oid.hash, sha1);
168
169	while (tree_ctx.count == 1) {
170		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head, rev,
171			       fullpath->buf);
172
173		tree = lookup_tree(&tree_ctx.oid);
174		if (!tree)
175			return;
176
177		free(tree_ctx.name);
178		tree_ctx.name = NULL;
179		tree_ctx.count = 0;
180
181		read_tree_recursive(tree, "", 0, 1, &paths, single_tree_cb,
182				    &tree_ctx);
183
184		if (tree_ctx.count != 1)
185			break;
186
187		html(" / ");
188		name = tree_ctx.name;
189	}
190
191	strbuf_setlen(fullpath, initial_length);
192}
193
194static int ls_item(const unsigned char *sha1, struct strbuf *base,
195		const char *pathname, unsigned mode, int stage, void *cbdata)
196{
197	struct walk_tree_context *walk_tree_ctx = cbdata;
198	char *name;
199	struct strbuf fullpath = STRBUF_INIT;
200	struct strbuf class = STRBUF_INIT;
201	enum object_type type;
202	unsigned long size = 0;
203
204	name = xstrdup(pathname);
205	strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
206		    ctx.qry.path ? "/" : "", name);
207
208	if (!S_ISGITLINK(mode)) {
209		type = sha1_object_info(sha1, &size);
210		if (type == OBJ_BAD) {
211			htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
212			      name,
213			      sha1_to_hex(sha1));
214			free(name);
215			return 0;
216		}
217	}
218
219	html("<tr><td class='ls-mode'>");
220	cgit_print_filemode(mode);
221	html("</td><td>");
222	if (S_ISGITLINK(mode)) {
223		cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
224	} else if (S_ISDIR(mode)) {
225		write_tree_link(sha1, name, walk_tree_ctx->curr_rev,
226				&fullpath);
227	} else {
228		char *ext = strrchr(name, '.');
229		strbuf_addstr(&class, "ls-blob");
230		if (ext)
231			strbuf_addf(&class, " %s", ext + 1);
232		cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
233			       walk_tree_ctx->curr_rev, fullpath.buf);
234	}
235	htmlf("</td><td class='ls-size'>%li</td>", size);
236
237	html("<td>");
238	cgit_log_link("log", NULL, "button", ctx.qry.head,
239		      walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
240		      ctx.qry.showmsg, 0);
241	if (ctx.repo->max_stats)
242		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
243				fullpath.buf);
244	if (!S_ISGITLINK(mode))
245		cgit_plain_link("plain", NULL, "button", ctx.qry.head,
246				walk_tree_ctx->curr_rev, fullpath.buf);
247	html("</td></tr>\n");
248	free(name);
249	strbuf_release(&fullpath);
250	strbuf_release(&class);
251	return 0;
252}
253
254static void ls_head(void)
255{
256	cgit_print_layout_start();
257	html("<table summary='tree listing' class='list'>\n");
258	html("<tr class='nohover'>");
259	html("<th class='left'>Mode</th>");
260	html("<th class='left'>Name</th>");
261	html("<th class='right'>Size</th>");
262	html("<th/>");
263	html("</tr>\n");
264}
265
266static void ls_tail(void)
267{
268	html("</table>\n");
269	cgit_print_layout_end();
270}
271
272static void ls_tree(const struct object_id *oid, char *path, struct walk_tree_context *walk_tree_ctx)
273{
274	struct tree *tree;
275	struct pathspec paths = {
276		.nr = 0
277	};
278
279	tree = parse_tree_indirect(oid);
280	if (!tree) {
281		cgit_print_error_page(404, "Not found",
282			"Not a tree object: %s", sha1_to_hex(oid->hash));
283		return;
284	}
285
286	ls_head();
287	read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
288	ls_tail();
289}
290
291
292static int walk_tree(const unsigned char *sha1, struct strbuf *base,
293		const char *pathname, unsigned mode, int stage, void *cbdata)
294{
295	struct walk_tree_context *walk_tree_ctx = cbdata;
296
297	if (walk_tree_ctx->state == 0) {
298		struct strbuf buffer = STRBUF_INIT;
299
300		strbuf_addbuf(&buffer, base);
301		strbuf_addstr(&buffer, pathname);
302		if (strcmp(walk_tree_ctx->match_path, buffer.buf))
303			return READ_TREE_RECURSIVE;
304
305		if (S_ISDIR(mode)) {
306			walk_tree_ctx->state = 1;
307			cgit_set_title_from_path(buffer.buf);
308			strbuf_release(&buffer);
309			ls_head();
310			return READ_TREE_RECURSIVE;
311		} else {
312			walk_tree_ctx->state = 2;
313			print_object(sha1, buffer.buf, pathname, walk_tree_ctx->curr_rev);
314			strbuf_release(&buffer);
315			return 0;
316		}
317	}
318	ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
319	return 0;
320}
321
322/*
323 * Show a tree or a blob
324 *   rev:  the commit pointing at the root tree object
325 *   path: path to tree or blob
326 */
327void cgit_print_tree(const char *rev, char *path)
328{
329	struct object_id oid;
330	struct commit *commit;
331	struct pathspec_item path_items = {
332		.match = path,
333		.len = path ? strlen(path) : 0
334	};
335	struct pathspec paths = {
336		.nr = path ? 1 : 0,
337		.items = &path_items
338	};
339	struct walk_tree_context walk_tree_ctx = {
340		.match_path = path,
341		.state = 0
342	};
343
344	if (!rev)
345		rev = ctx.qry.head;
346
347	if (get_oid(rev, &oid)) {
348		cgit_print_error_page(404, "Not found",
349			"Invalid revision name: %s", rev);
350		return;
351	}
352	commit = lookup_commit_reference(&oid);
353	if (!commit || parse_commit(commit)) {
354		cgit_print_error_page(404, "Not found",
355			"Invalid commit reference: %s", rev);
356		return;
357	}
358
359	walk_tree_ctx.curr_rev = xstrdup(rev);
360
361	if (path == NULL) {
362		ls_tree(&commit->tree->object.oid, NULL, &walk_tree_ctx);
363		goto cleanup;
364	}
365
366	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
367	if (walk_tree_ctx.state == 1)
368		ls_tail();
369	else if (walk_tree_ctx.state == 2)
370		cgit_print_layout_end();
371	else
372		cgit_print_error_page(404, "Not found", "Path not found");
373
374cleanup:
375	free(walk_tree_ctx.curr_rev);
376}