all repos — cgit @ fab385ef5ca516dcda79df87eb926eccdda64a54

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 Lars Hjemli
  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 "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 =
 24		"<a class='no' id='n%1$d' name='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		html("<td class='lines'><pre><code>");
 49		ctx.repo->source_filter->argv[1] = xstrdup(name);
 50		cgit_open_filter(ctx.repo->source_filter);
 51		html_raw(buf, size);
 52		cgit_close_filter(ctx.repo->source_filter);
 53		free(ctx.repo->source_filter->argv[1]);
 54		ctx.repo->source_filter->argv[1] = NULL;
 55		html("</code></pre></td></tr></table>\n");
 56		return;
 57	}
 58
 59	html("<td class='lines'><pre><code>");
 60	html_txt(buf);
 61	html("</code></pre></td></tr></table>\n");
 62}
 63
 64#define ROWLEN 32
 65
 66static void print_binary_buffer(char *buf, unsigned long size)
 67{
 68	unsigned long ofs, idx;
 69	static char ascii[ROWLEN + 1];
 70
 71	html("<table summary='blob content' class='bin-blob'>\n");
 72	html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
 73	for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
 74		htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
 75		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 76			htmlf("%*s%02x",
 77			      idx == 16 ? 4 : 1, "",
 78			      buf[idx] & 0xff);
 79		html(" </td><td class='hex'>");
 80		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 81			ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
 82		ascii[idx] = '\0';
 83		html_txt(ascii);
 84		html("</td></tr>\n");
 85	}
 86	html("</table>\n");
 87}
 88
 89static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
 90{
 91	enum object_type type;
 92	char *buf;
 93	unsigned long size;
 94
 95	type = sha1_object_info(sha1, &size);
 96	if (type == OBJ_BAD) {
 97		cgit_print_error(fmt("Bad object name: %s",
 98				     sha1_to_hex(sha1)));
 99		return;
100	}
101
102	buf = read_sha1_file(sha1, &type, &size);
103	if (!buf) {
104		cgit_print_error(fmt("Error reading object %s",
105				     sha1_to_hex(sha1)));
106		return;
107	}
108
109	htmlf("blob: %s (", sha1_to_hex(sha1));
110	cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
111		        rev, path);
112	html(")\n");
113
114	if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
115		htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
116				size / 1024, ctx.cfg.max_blob_size);
117		return;
118	}
119
120	if (buffer_is_binary(buf, size))
121		print_binary_buffer(buf, size);
122	else
123		print_text_buffer(basename, buf, size);
124}
125
126
127static int ls_item(const unsigned char *sha1, const char *base, int baselen,
128		   const char *pathname, unsigned int mode, int stage,
129		   void *cbdata)
130{
131	struct walk_tree_context *walk_tree_ctx = cbdata;
132	char *name;
133	char *fullpath;
134	char *class;
135	enum object_type type;
136	unsigned long size = 0;
137
138	name = xstrdup(pathname);
139	fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
140		       ctx.qry.path ? "/" : "", name);
141
142	if (!S_ISGITLINK(mode)) {
143		type = sha1_object_info(sha1, &size);
144		if (type == OBJ_BAD) {
145			htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
146			      name,
147			      sha1_to_hex(sha1));
148			return 0;
149		}
150	}
151
152	html("<tr><td class='ls-mode'>");
153	cgit_print_filemode(mode);
154	html("</td><td>");
155	if (S_ISGITLINK(mode)) {
156		cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
157	} else if (S_ISDIR(mode)) {
158		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
159			       walk_tree_ctx->curr_rev, fullpath);
160	} else {
161		class = strrchr(name, '.');
162		if (class != NULL) {
163			class = fmt("ls-blob %s", class + 1);
164		} else
165			class = "ls-blob";
166		cgit_tree_link(name, NULL, class, ctx.qry.head,
167			       walk_tree_ctx->curr_rev, fullpath);
168	}
169	htmlf("</td><td class='ls-size'>%li</td>", size);
170
171	html("<td>");
172	cgit_log_link("log", NULL, "button", ctx.qry.head,
173		      walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL,
174		      ctx.qry.showmsg);
175	if (ctx.repo->max_stats)
176		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
177				fullpath);
178	if (!S_ISGITLINK(mode))
179		cgit_plain_link("plain", NULL, "button", ctx.qry.head,
180				walk_tree_ctx->curr_rev, fullpath);
181	html("</td></tr>\n");
182	free(name);
183	return 0;
184}
185
186static void ls_head()
187{
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()
198{
199	html("</table>\n");
200}
201
202static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_context *walk_tree_ctx)
203{
204	struct tree *tree;
205	struct pathspec paths = {
206		.nr = 0
207	};
208
209	tree = parse_tree_indirect(sha1);
210	if (!tree) {
211		cgit_print_error(fmt("Not a tree object: %s",
212				     sha1_to_hex(sha1)));
213		return;
214	}
215
216	ls_head();
217	read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx);
218	ls_tail();
219}
220
221
222static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
223		     const char *pathname, unsigned mode, int stage,
224		     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, baselen);
231		strcpy(buffer + baselen, 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			print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
241			return 0;
242		}
243	}
244	ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
245	return 0;
246}
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	walk_tree_ctx.curr_rev = xstrdup(rev);
275	if (get_sha1(rev, sha1)) {
276		cgit_print_error(fmt("Invalid revision name: %s", rev));
277		return;
278	}
279	commit = lookup_commit_reference(sha1);
280	if (!commit || parse_commit(commit)) {
281		cgit_print_error(fmt("Invalid commit reference: %s", rev));
282		return;
283	}
284
285	if (path == NULL) {
286		ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
287		return;
288	}
289
290	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
291	if (walk_tree_ctx.state == 1)
292		ls_tail();
293}