all repos — cgit @ fa4dfee5489d8c829da92637dd84e8650439f313

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 "cgit.h"
 10#include "html.h"
 11#include "ui-shared.h"
 12
 13char *curr_rev;
 14char *match_path;
 15int header = 0;
 16
 17static void print_object(const unsigned char *sha1, char *path)
 18{
 19	enum object_type type;
 20	char *buf;
 21	unsigned long size, lineno, start, idx;
 22	const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>";
 23
 24	type = sha1_object_info(sha1, &size);
 25	if (type == OBJ_BAD) {
 26		cgit_print_error(fmt("Bad object name: %s",
 27				     sha1_to_hex(sha1)));
 28		return;
 29	}
 30
 31	buf = read_sha1_file(sha1, &type, &size);
 32	if (!buf) {
 33		cgit_print_error(fmt("Error reading object %s",
 34				     sha1_to_hex(sha1)));
 35		return;
 36	}
 37
 38	html(" blob: <a href='");
 39	html_attr(cgit_pageurl(ctx.qry.repo, "blob",
 40			       fmt("id=%s&path=%s", sha1_to_hex(sha1), path)));
 41	htmlf("'>%s</a>",sha1_to_hex(sha1));
 42
 43	html("<table summary='blob content' class='blob'>\n");
 44	idx = 0;
 45	start = 0;
 46	lineno = 0;
 47	while(idx < size) {
 48		if (buf[idx] == '\n') {
 49			buf[idx] = '\0';
 50			htmlf(linefmt, ++lineno);
 51			html_txt(buf + start);
 52			html("</td></tr>\n");
 53			start = idx + 1;
 54		}
 55		idx++;
 56	}
 57	htmlf(linefmt, ++lineno);
 58	html_txt(buf + start);
 59	html("</td></tr>\n");
 60	html("</table>\n");
 61}
 62
 63
 64static int ls_item(const unsigned char *sha1, const char *base, int baselen,
 65		   const char *pathname, unsigned int mode, int stage)
 66{
 67	char *name;
 68	char *fullpath;
 69	enum object_type type;
 70	unsigned long size = 0;
 71
 72	name = xstrdup(pathname);
 73	fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
 74		       ctx.qry.path ? "/" : "", name);
 75
 76	type = sha1_object_info(sha1, &size);
 77	if (type == OBJ_BAD && !S_ISGITLINK(mode)) {
 78		htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
 79		      name,
 80		      sha1_to_hex(sha1));
 81		return 0;
 82	}
 83
 84	html("<tr><td class='ls-mode'>");
 85	cgit_print_filemode(mode);
 86	html("</td><td>");
 87	if (S_ISGITLINK(mode)) {
 88		htmlf("<a class='ls-mod' href='");
 89		html_attr(fmt(ctx.repo->module_link,
 90			      name,
 91			      sha1_to_hex(sha1)));
 92		html("'>");
 93		html_txt(name);
 94		html("</a>");
 95	} else if (S_ISDIR(mode)) {
 96		cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
 97			       curr_rev, fullpath);
 98	} else {
 99		cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
100			       curr_rev, fullpath);
101	}
102	htmlf("</td><td class='ls-size'>%li</td>", size);
103
104	html("<td>");
105	cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
106		      fullpath, 0, NULL, NULL);
107	html("</td></tr>\n");
108	free(name);
109	return 0;
110}
111
112static void ls_head()
113{
114	html("<table summary='tree listing' class='list'>\n");
115	html("<tr class='nohover'>");
116	html("<th class='left'>Mode</th>");
117	html("<th class='left'>Name</th>");
118	html("<th class='right'>Size</th>");
119	html("<th/>");
120	html("</tr>\n");
121	header = 1;
122}
123
124static void ls_tail()
125{
126	if (!header)
127		return;
128	html("</table>\n");
129	header = 0;
130}
131
132static void ls_tree(const unsigned char *sha1, char *path)
133{
134	struct tree *tree;
135
136	tree = parse_tree_indirect(sha1);
137	if (!tree) {
138		cgit_print_error(fmt("Not a tree object: %s",
139				     sha1_to_hex(sha1)));
140		return;
141	}
142
143	ls_head();
144	read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
145	ls_tail();
146}
147
148
149static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
150		     const char *pathname, unsigned mode, int stage)
151{
152	static int state;
153	static char buffer[PATH_MAX];
154	char *url;
155
156	if (state == 0) {
157		memcpy(buffer, base, baselen);
158		strcpy(buffer+baselen, pathname);
159		url = cgit_pageurl(ctx.qry.repo, "tree",
160				   fmt("h=%s&amp;path=%s", curr_rev, buffer));
161		html("/");
162		cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
163			       curr_rev, buffer);
164
165		if (strcmp(match_path, buffer))
166			return READ_TREE_RECURSIVE;
167
168		if (S_ISDIR(mode)) {
169			state = 1;
170			ls_head();
171			return READ_TREE_RECURSIVE;
172		} else {
173			print_object(sha1, buffer);
174			return 0;
175		}
176	}
177	ls_item(sha1, base, baselen, pathname, mode, stage);
178	return 0;
179}
180
181
182/*
183 * Show a tree or a blob
184 *   rev:  the commit pointing at the root tree object
185 *   path: path to tree or blob
186 */
187void cgit_print_tree(const char *rev, char *path)
188{
189	unsigned char sha1[20];
190	struct commit *commit;
191	const char *paths[] = {path, NULL};
192
193	if (!rev)
194		rev = ctx.qry.head;
195
196	curr_rev = xstrdup(rev);
197	if (get_sha1(rev, sha1)) {
198		cgit_print_error(fmt("Invalid revision name: %s", rev));
199		return;
200	}
201	commit = lookup_commit_reference(sha1);
202	if (!commit || parse_commit(commit)) {
203		cgit_print_error(fmt("Invalid commit reference: %s", rev));
204		return;
205	}
206
207	html("path: <a href='");
208	html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
209	html("'>root</a>");
210
211	if (path == NULL) {
212		ls_tree(commit->tree->object.sha1, NULL);
213		return;
214	}
215
216	match_path = path;
217	read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);
218	ls_tail();
219}