all repos — cgit @ 6bcda2f73da605d48279341b1905c1b234a39368

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 <ctype.h>
 10#include "cgit.h"
 11#include "ui-tree.h"
 12#include "html.h"
 13#include "ui-shared.h"
 14
 15struct walk_tree_context {
 16	char *curr_rev;
 17	char *match_path;
 18	int state;
 19};
 20
 21static void print_text_buffer(const char *name, char *buf, unsigned long size)
 22{
 23	unsigned long lineno, idx;
 24	const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
 25
 26	html("<table summary='blob content' class='blob'>\n");
 27
 28	if (ctx.cfg.enable_tree_linenumbers) {
 29		html("<tr><td class='linenumbers'><pre>");
 30		idx = 0;
 31		lineno = 0;
 32
 33		if (size) {
 34			htmlf(numberfmt, ++lineno);
 35			while (idx < size - 1) { // skip absolute last newline
 36				if (buf[idx] == '\n')
 37					htmlf(numberfmt, ++lineno);
 38				idx++;
 39			}
 40		}
 41		html("</pre></td>\n");
 42	}
 43	else {
 44		html("<tr>\n");
 45	}
 46
 47	if (ctx.repo->source_filter) {
 48		char *filter_arg = xstrdup(name);
 49		html("<td class='lines'><pre><code>");
 50		cgit_open_filter(ctx.repo->source_filter, filter_arg);
 51		html_raw(buf, size);
 52		cgit_close_filter(ctx.repo->source_filter);
 53		free(filter_arg);
 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("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("Error reading object %s", sha1_to_hex(sha1));
103		return;
104	}
105
106	htmlf("blob: %s (", sha1_to_hex(sha1));
107	cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
108		        rev, path);
109	html(")\n");
110
111	if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
112		htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
113				size / 1024, ctx.cfg.max_blob_size);
114		return;
115	}
116
117	if (buffer_is_binary(buf, size))
118		print_binary_buffer(buf, size);
119	else
120		print_text_buffer(basename, buf, size);
121}
122
123
124static int ls_item(const unsigned char *sha1, struct strbuf *base,
125		const char *pathname, unsigned mode, int stage, void *cbdata)
126{
127	struct walk_tree_context *walk_tree_ctx = cbdata;
128	char *name;
129	struct strbuf fullpath = STRBUF_INIT;
130	struct strbuf class = STRBUF_INIT;
131	enum object_type type;
132	unsigned long size = 0;
133
134	name = xstrdup(pathname);
135	strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
136		    ctx.qry.path ? "/" : "", name);
137
138	if (!S_ISGITLINK(mode)) {
139		type = sha1_object_info(sha1, &size);
140		if (type == OBJ_BAD) {
141			htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
142			      name,
143			      sha1_to_hex(sha1));
144			return 0;
145		}
146	}
147
148	html("<tr><td class='ls-mode'>");
149	cgit_print_filemode(mode);
150	html("</td><td>");
151	if (S_ISGITLINK(mode)) {
152		cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
153	} else if (S_ISDIR(mode)) {
154		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
155			       walk_tree_ctx->curr_rev, fullpath.buf);
156	} else {
157		char *ext = strrchr(name, '.');
158		strbuf_addstr(&class, "ls-blob");
159		if (ext)
160			strbuf_addf(&class, " %s", ext + 1);
161		cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
162			       walk_tree_ctx->curr_rev, fullpath.buf);
163	}
164	htmlf("</td><td class='ls-size'>%li</td>", size);
165
166	html("<td>");
167	cgit_log_link("log", NULL, "button", ctx.qry.head,
168		      walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
169		      ctx.qry.showmsg);
170	if (ctx.repo->max_stats)
171		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
172				fullpath.buf);
173	if (!S_ISGITLINK(mode))
174		cgit_plain_link("plain", NULL, "button", ctx.qry.head,
175				walk_tree_ctx->curr_rev, fullpath.buf);
176	html("</td></tr>\n");
177	free(name);
178	strbuf_release(&fullpath);
179	strbuf_release(&class);
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}
193
194static void ls_tail()
195{
196	html("</table>\n");
197}
198
199static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
200{
201	struct tree *tree;
202	struct pathspec paths = {
203		.nr = 0
204	};
205
206	tree = parse_tree_indirect(sha1);
207	if (!tree) {
208		cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
209		return;
210	}
211
212	ls_head();
213	read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
214	ls_tail();
215}
216
217
218static int walk_tree(const unsigned char *sha1, struct strbuf *base,
219		const char *pathname, unsigned mode, int stage, void *cbdata)
220{
221	struct walk_tree_context *walk_tree_ctx = cbdata;
222	static char buffer[PATH_MAX];
223
224	if (walk_tree_ctx->state == 0) {
225		memcpy(buffer, base->buf, base->len);
226		strcpy(buffer + base->len, pathname);
227		if (strcmp(walk_tree_ctx->match_path, buffer))
228			return READ_TREE_RECURSIVE;
229
230		if (S_ISDIR(mode)) {
231			walk_tree_ctx->state = 1;
232			ls_head();
233			return READ_TREE_RECURSIVE;
234		} else {
235			print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
236			return 0;
237		}
238	}
239	ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
240	return 0;
241}
242
243/*
244 * Show a tree or a blob
245 *   rev:  the commit pointing at the root tree object
246 *   path: path to tree or blob
247 */
248void cgit_print_tree(const char *rev, char *path)
249{
250	unsigned char sha1[20];
251	struct commit *commit;
252	struct pathspec_item path_items = {
253		.match = path,
254		.len = path ? strlen(path) : 0
255	};
256	struct pathspec paths = {
257		.nr = path ? 1 : 0,
258		.items = &path_items
259	};
260	struct walk_tree_context walk_tree_ctx = {
261		.match_path = path,
262		.state = 0
263	};
264
265	if (!rev)
266		rev = ctx.qry.head;
267
268	if (get_sha1(rev, sha1)) {
269		cgit_print_error("Invalid revision name: %s", rev);
270		return;
271	}
272	commit = lookup_commit_reference(sha1);
273	if (!commit || parse_commit(commit)) {
274		cgit_print_error("Invalid commit reference: %s", rev);
275		return;
276	}
277
278	walk_tree_ctx.curr_rev = xstrdup(rev);
279
280	if (path == NULL) {
281		ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
282		goto cleanup;
283	}
284
285	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
286	if (walk_tree_ctx.state == 1)
287		ls_tail();
288
289cleanup:
290	free(walk_tree_ctx.curr_rev);
291}