all repos — cgit @ 44947bfcdc0d6e8c7d673bea0538cbf2a182f289

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