all repos — cgit @ 198a4404b937033cf2cf2b054242df4e81ef3075

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_print_layout_start();
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, struct strbuf *base,
127		const char *pathname, unsigned mode, int stage, void *cbdata)
128{
129	struct walk_tree_context *walk_tree_ctx = cbdata;
130	char *name;
131	struct strbuf fullpath = STRBUF_INIT;
132	struct strbuf class = STRBUF_INIT;
133	enum object_type type;
134	unsigned long size = 0;
135
136	name = xstrdup(pathname);
137	strbuf_addf(&fullpath, "%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.buf, sha1_to_hex(sha1));
155	} else if (S_ISDIR(mode)) {
156		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
157			       walk_tree_ctx->curr_rev, fullpath.buf);
158	} else {
159		char *ext = strrchr(name, '.');
160		strbuf_addstr(&class, "ls-blob");
161		if (ext)
162			strbuf_addf(&class, " %s", ext + 1);
163		cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
164			       walk_tree_ctx->curr_rev, fullpath.buf);
165	}
166	htmlf("</td><td class='ls-size'>%li</td>", size);
167
168	html("<td>");
169	cgit_log_link("log", NULL, "button", ctx.qry.head,
170		      walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
171		      ctx.qry.showmsg, 0);
172	if (ctx.repo->max_stats)
173		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
174				fullpath.buf);
175	if (!S_ISGITLINK(mode))
176		cgit_plain_link("plain", NULL, "button", ctx.qry.head,
177				walk_tree_ctx->curr_rev, fullpath.buf);
178	html("</td></tr>\n");
179	free(name);
180	strbuf_release(&fullpath);
181	strbuf_release(&class);
182	return 0;
183}
184
185static void ls_head(void)
186{
187	cgit_print_layout_start();
188	html("<table summary='tree listing' class='list'>\n");
189	html("<tr class='nohover'>");
190	html("<th class='left'>Mode</th>");
191	html("<th class='left'>Name</th>");
192	html("<th class='right'>Size</th>");
193	html("<th/>");
194	html("</tr>\n");
195}
196
197static void ls_tail(void)
198{
199	html("</table>\n");
200	cgit_print_layout_end();
201}
202
203static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
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_page(404, "Not found",
213			"Not a tree object: %s", sha1_to_hex(sha1));
214		return;
215	}
216
217	ls_head();
218	read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
219	ls_tail();
220}
221
222
223static int walk_tree(const unsigned char *sha1, struct strbuf *base,
224		const char *pathname, unsigned mode, int stage, void *cbdata)
225{
226	struct walk_tree_context *walk_tree_ctx = cbdata;
227	static char buffer[PATH_MAX];
228
229	if (walk_tree_ctx->state == 0) {
230		memcpy(buffer, base->buf, base->len);
231		strcpy(buffer + base->len, pathname);
232		if (strcmp(walk_tree_ctx->match_path, buffer))
233			return READ_TREE_RECURSIVE;
234
235		if (S_ISDIR(mode)) {
236			walk_tree_ctx->state = 1;
237			ls_head();
238			return READ_TREE_RECURSIVE;
239		} else {
240			walk_tree_ctx->state = 2;
241			print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
242			return 0;
243		}
244	}
245	ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
246	return 0;
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	struct walk_tree_context walk_tree_ctx = {
267		.match_path = path,
268		.state = 0
269	};
270
271	if (!rev)
272		rev = ctx.qry.head;
273
274	if (get_sha1(rev, sha1)) {
275		cgit_print_error_page(404, "Not found",
276			"Invalid revision name: %s", rev);
277		return;
278	}
279	commit = lookup_commit_reference(sha1);
280	if (!commit || parse_commit(commit)) {
281		cgit_print_error_page(404, "Not found",
282			"Invalid commit reference: %s", rev);
283		return;
284	}
285
286	walk_tree_ctx.curr_rev = xstrdup(rev);
287
288	if (path == NULL) {
289		ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
290		goto cleanup;
291	}
292
293	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
294	if (walk_tree_ctx.state == 1)
295		ls_tail();
296	else if (walk_tree_ctx.state == 2)
297		cgit_print_layout_end();
298	else
299		cgit_print_error_page(404, "Not found", "Path not found");
300
301cleanup:
302	free(walk_tree_ctx.curr_rev);
303}