all repos — cgit @ 892c5441f4af2f53a3b1906c4d7e458829e0cf0a

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("Bad object name: %s", sha1_to_hex(sha1));
 96		return;
 97	}
 98
 99	buf = read_sha1_file(sha1, &type, &size);
100	if (!buf) {
101		cgit_print_error("Error reading object %s", sha1_to_hex(sha1));
102		return;
103	}
104
105	htmlf("blob: %s (", sha1_to_hex(sha1));
106	cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
107		        rev, path);
108	html(")\n");
109
110	if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
111		htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
112				size / 1024, ctx.cfg.max_blob_size);
113		return;
114	}
115
116	if (buffer_is_binary(buf, size))
117		print_binary_buffer(buf, size);
118	else
119		print_text_buffer(basename, buf, size);
120}
121
122
123static int ls_item(const unsigned char *sha1, struct strbuf *base,
124		const char *pathname, unsigned mode, int stage, void *cbdata)
125{
126	struct walk_tree_context *walk_tree_ctx = cbdata;
127	char *name;
128	struct strbuf fullpath = STRBUF_INIT;
129	struct strbuf class = STRBUF_INIT;
130	enum object_type type;
131	unsigned long size = 0;
132
133	name = xstrdup(pathname);
134	strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
135		    ctx.qry.path ? "/" : "", name);
136
137	if (!S_ISGITLINK(mode)) {
138		type = sha1_object_info(sha1, &size);
139		if (type == OBJ_BAD) {
140			htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
141			      name,
142			      sha1_to_hex(sha1));
143			return 0;
144		}
145	}
146
147	html("<tr><td class='ls-mode'>");
148	cgit_print_filemode(mode);
149	html("</td><td>");
150	if (S_ISGITLINK(mode)) {
151		cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
152	} else if (S_ISDIR(mode)) {
153		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
154			       walk_tree_ctx->curr_rev, fullpath.buf);
155	} else {
156		char *ext = strrchr(name, '.');
157		strbuf_addstr(&class, "ls-blob");
158		if (ext)
159			strbuf_addf(&class, " %s", ext + 1);
160		cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
161			       walk_tree_ctx->curr_rev, fullpath.buf);
162	}
163	htmlf("</td><td class='ls-size'>%li</td>", size);
164
165	html("<td>");
166	cgit_log_link("log", NULL, "button", ctx.qry.head,
167		      walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
168		      ctx.qry.showmsg, 0);
169	if (ctx.repo->max_stats)
170		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
171				fullpath.buf);
172	if (!S_ISGITLINK(mode))
173		cgit_plain_link("plain", NULL, "button", ctx.qry.head,
174				walk_tree_ctx->curr_rev, fullpath.buf);
175	html("</td></tr>\n");
176	free(name);
177	strbuf_release(&fullpath);
178	strbuf_release(&class);
179	return 0;
180}
181
182static void ls_head(void)
183{
184	html("<table summary='tree listing' class='list'>\n");
185	html("<tr class='nohover'>");
186	html("<th class='left'>Mode</th>");
187	html("<th class='left'>Name</th>");
188	html("<th class='right'>Size</th>");
189	html("<th/>");
190	html("</tr>\n");
191}
192
193static void ls_tail(void)
194{
195	html("</table>\n");
196}
197
198static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
199{
200	struct tree *tree;
201	struct pathspec paths = {
202		.nr = 0
203	};
204
205	tree = parse_tree_indirect(sha1);
206	if (!tree) {
207		cgit_print_error("Not a tree object: %s", sha1_to_hex(sha1));
208		return;
209	}
210
211	ls_head();
212	read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
213	ls_tail();
214}
215
216
217static int walk_tree(const unsigned char *sha1, struct strbuf *base,
218		const char *pathname, unsigned mode, int stage, void *cbdata)
219{
220	struct walk_tree_context *walk_tree_ctx = cbdata;
221	static char buffer[PATH_MAX];
222
223	if (walk_tree_ctx->state == 0) {
224		memcpy(buffer, base->buf, base->len);
225		strcpy(buffer + base->len, pathname);
226		if (strcmp(walk_tree_ctx->match_path, buffer))
227			return READ_TREE_RECURSIVE;
228
229		if (S_ISDIR(mode)) {
230			walk_tree_ctx->state = 1;
231			ls_head();
232			return READ_TREE_RECURSIVE;
233		} else {
234			print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
235			return 0;
236		}
237	}
238	ls_item(sha1, base, pathname, mode, stage, walk_tree_ctx);
239	return 0;
240}
241
242/*
243 * Show a tree or a blob
244 *   rev:  the commit pointing at the root tree object
245 *   path: path to tree or blob
246 */
247void cgit_print_tree(const char *rev, char *path)
248{
249	unsigned char sha1[20];
250	struct commit *commit;
251	struct pathspec_item path_items = {
252		.match = path,
253		.len = path ? strlen(path) : 0
254	};
255	struct pathspec paths = {
256		.nr = path ? 1 : 0,
257		.items = &path_items
258	};
259	struct walk_tree_context walk_tree_ctx = {
260		.match_path = path,
261		.state = 0
262	};
263
264	if (!rev)
265		rev = ctx.qry.head;
266
267	if (get_sha1(rev, sha1)) {
268		cgit_print_error("Invalid revision name: %s", rev);
269		return;
270	}
271	commit = lookup_commit_reference(sha1);
272	if (!commit || parse_commit(commit)) {
273		cgit_print_error("Invalid commit reference: %s", rev);
274		return;
275	}
276
277	walk_tree_ctx.curr_rev = xstrdup(rev);
278
279	if (path == NULL) {
280		ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
281		goto cleanup;
282	}
283
284	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
285	if (walk_tree_ctx.state == 1)
286		ls_tail();
287
288cleanup:
289	free(walk_tree_ctx.curr_rev);
290}