all repos — cgit @ caca860ba79fe9f6bc387f64ddb57ac0db1fac33

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 "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 =
 25		"<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
 26
 27	html("<table summary='blob content' class='blob'>\n");
 28
 29	if (ctx.cfg.enable_tree_linenumbers) {
 30		html("<tr><td class='linenumbers'><pre>");
 31		idx = 0;
 32		lineno = 0;
 33
 34		if (size) {
 35			htmlf(numberfmt, ++lineno);
 36			while (idx < size - 1) { // skip absolute last newline
 37				if (buf[idx] == '\n')
 38					htmlf(numberfmt, ++lineno);
 39				idx++;
 40			}
 41		}
 42		html("</pre></td>\n");
 43	}
 44	else {
 45		html("<tr>\n");
 46	}
 47
 48	if (ctx.repo->source_filter) {
 49		html("<td class='lines'><pre><code>");
 50		ctx.repo->source_filter->argv[1] = xstrdup(name);
 51		cgit_open_filter(ctx.repo->source_filter);
 52		html_raw(buf, size);
 53		cgit_close_filter(ctx.repo->source_filter);
 54		free(ctx.repo->source_filter->argv[1]);
 55		ctx.repo->source_filter->argv[1] = NULL;
 56		html("</code></pre></td></tr></table>\n");
 57		return;
 58	}
 59
 60	html("<td class='lines'><pre><code>");
 61	html_txt(buf);
 62	html("</code></pre></td></tr></table>\n");
 63}
 64
 65#define ROWLEN 32
 66
 67static void print_binary_buffer(char *buf, unsigned long size)
 68{
 69	unsigned long ofs, idx;
 70	static char ascii[ROWLEN + 1];
 71
 72	html("<table summary='blob content' class='bin-blob'>\n");
 73	html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
 74	for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
 75		htmlf("<tr><td class='right'>%04lx</td><td class='hex'>", ofs);
 76		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 77			htmlf("%*s%02x",
 78			      idx == 16 ? 4 : 1, "",
 79			      buf[idx] & 0xff);
 80		html(" </td><td class='hex'>");
 81		for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
 82			ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
 83		ascii[idx] = '\0';
 84		html_txt(ascii);
 85		html("</td></tr>\n");
 86	}
 87	html("</table>\n");
 88}
 89
 90static void print_object(const unsigned char *sha1, char *path, const char *basename, const char *rev)
 91{
 92	enum object_type type;
 93	char *buf;
 94	unsigned long size;
 95
 96	type = sha1_object_info(sha1, &size);
 97	if (type == OBJ_BAD) {
 98		cgit_print_error(fmt("Bad object name: %s",
 99				     sha1_to_hex(sha1)));
100		return;
101	}
102
103	buf = read_sha1_file(sha1, &type, &size);
104	if (!buf) {
105		cgit_print_error(fmt("Error reading object %s",
106				     sha1_to_hex(sha1)));
107		return;
108	}
109
110	htmlf("blob: %s (", sha1_to_hex(sha1));
111	cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
112		        rev, path);
113	html(")\n");
114
115	if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
116		htmlf("<div class='error'>blob size (%ldKB) exceeds display size limit (%dKB).</div>",
117				size / 1024, ctx.cfg.max_blob_size);
118		return;
119	}
120
121	if (buffer_is_binary(buf, size))
122		print_binary_buffer(buf, size);
123	else
124		print_text_buffer(basename, buf, size);
125}
126
127
128static int ls_item(const unsigned char *sha1, const char *base, int baselen,
129		   const char *pathname, unsigned int mode, int stage,
130		   void *cbdata)
131{
132	struct walk_tree_context *walk_tree_ctx = cbdata;
133	char *name;
134	char *fullpath;
135	char *class;
136	enum object_type type;
137	unsigned long size = 0;
138
139	name = xstrdup(pathname);
140	fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
141		       ctx.qry.path ? "/" : "", name);
142
143	if (!S_ISGITLINK(mode)) {
144		type = sha1_object_info(sha1, &size);
145		if (type == OBJ_BAD) {
146			htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
147			      name,
148			      sha1_to_hex(sha1));
149			return 0;
150		}
151	}
152
153	html("<tr><td class='ls-mode'>");
154	cgit_print_filemode(mode);
155	html("</td><td>");
156	if (S_ISGITLINK(mode)) {
157		cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
158	} else if (S_ISDIR(mode)) {
159		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
160			       walk_tree_ctx->curr_rev, fullpath);
161	} else {
162		class = strrchr(name, '.');
163		if (class != NULL) {
164			class = fmt("ls-blob %s", class + 1);
165		} else
166			class = "ls-blob";
167		cgit_tree_link(name, NULL, class, ctx.qry.head,
168			       walk_tree_ctx->curr_rev, fullpath);
169	}
170	htmlf("</td><td class='ls-size'>%li</td>", size);
171
172	html("<td>");
173	cgit_log_link("log", NULL, "button", ctx.qry.head,
174		      walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL,
175		      ctx.qry.showmsg);
176	if (ctx.repo->max_stats)
177		cgit_stats_link("stats", NULL, "button", ctx.qry.head,
178				fullpath);
179	if (!S_ISGITLINK(mode))
180		cgit_plain_link("plain", NULL, "button", ctx.qry.head,
181				walk_tree_ctx->curr_rev, fullpath);
182	html("</td></tr>\n");
183	free(name);
184	return 0;
185}
186
187static void ls_head()
188{
189	html("<table summary='tree listing' class='list'>\n");
190	html("<tr class='nohover'>");
191	html("<th class='left'>Mode</th>");
192	html("<th class='left'>Name</th>");
193	html("<th class='right'>Size</th>");
194	html("<th/>");
195	html("</tr>\n");
196}
197
198static void ls_tail()
199{
200	html("</table>\n");
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(fmt("Not a tree object: %s",
213				     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, const char *base, int baselen,
224		     const char *pathname, unsigned mode, int stage,
225		     void *cbdata)
226{
227	struct walk_tree_context *walk_tree_ctx = cbdata;
228	static char buffer[PATH_MAX];
229
230	if (walk_tree_ctx->state == 0) {
231		memcpy(buffer, base, baselen);
232		strcpy(buffer + baselen, pathname);
233		if (strcmp(walk_tree_ctx->match_path, buffer))
234			return READ_TREE_RECURSIVE;
235
236		if (S_ISDIR(mode)) {
237			walk_tree_ctx->state = 1;
238			ls_head();
239			return READ_TREE_RECURSIVE;
240		} else {
241			print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev);
242			return 0;
243		}
244	}
245	ls_item(sha1, base, baselen, pathname, mode, stage, walk_tree_ctx);
246	return 0;
247}
248
249
250/*
251 * Show a tree or a blob
252 *   rev:  the commit pointing at the root tree object
253 *   path: path to tree or blob
254 */
255void cgit_print_tree(const char *rev, char *path)
256{
257	unsigned char sha1[20];
258	struct commit *commit;
259	struct pathspec_item path_items = {
260		.match = path,
261		.len = path ? strlen(path) : 0
262	};
263	struct pathspec paths = {
264		.nr = path ? 1 : 0,
265		.items = &path_items
266	};
267	struct walk_tree_context walk_tree_ctx = {
268		.match_path = path,
269		.state = 0
270	};
271
272	if (!rev)
273		rev = ctx.qry.head;
274
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	walk_tree_ctx.curr_rev = xstrdup(rev);
286
287	if (path == NULL) {
288		ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
289		goto cleanup;
290	}
291
292	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
293	if (walk_tree_ctx.state == 1)
294		ls_tail();
295
296cleanup:
297	free(walk_tree_ctx.curr_rev);
298}