all repos — cgit @ edf5aac3db5780478c304cf027d23d29b83151cf

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