all repos — cgit @ v0.4

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
11
12static int print_entry(const unsigned char *sha1, const char *base, 
13		       int baselen, const char *pathname, unsigned int mode, 
14		       int stage)
15{
16	char *name;
17	enum object_type type;
18	unsigned long size = 0;
19
20	name = xstrdup(pathname);
21	type = sha1_object_info(sha1, &size);
22	if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
23		htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
24		      name,
25		      sha1_to_hex(sha1));
26		return 0;
27	}
28	html("<tr><td class='filemode'>");
29	html_filemode(mode);
30	html("</td><td ");
31	if (S_ISDIRLNK(mode)) {
32		htmlf("class='ls-mod'><a href='");
33		html_attr(fmt(cgit_repo->module_link,
34			      name,
35			      sha1_to_hex(sha1)));
36	} else if (S_ISDIR(mode)) {
37		html("class='ls-dir'><a href='");
38		html_attr(cgit_pageurl(cgit_query_repo, "tree", 
39				       fmt("id=%s&path=%s%s/", 
40					   sha1_to_hex(sha1),
41					   cgit_query_path ? cgit_query_path : "",
42					   pathname)));
43	} else {
44		html("class='ls-blob'><a href='");
45		html_attr(cgit_pageurl(cgit_query_repo, "view",
46				      fmt("id=%s&path=%s%s", sha1_to_hex(sha1),
47					  cgit_query_path ? cgit_query_path : "",
48					  pathname)));
49	}
50	htmlf("'>%s</a></div></td>", name);
51	htmlf("<td class='filesize'>%li</td>", size);
52	html("</tr>\n");
53	free(name);
54	return 0;
55}
56
57void cgit_print_tree(const char *hex, char *path)
58{
59	struct tree *tree;
60	unsigned char sha1[20];
61
62	if (get_sha1_hex(hex, sha1)) {
63		cgit_print_error(fmt("Invalid object id: %s", hex));
64		return;
65	}
66	tree = parse_tree_indirect(sha1);
67	if (!tree) {
68		cgit_print_error(fmt("Not a tree object: %s", hex));
69		return;
70	}
71
72	html_txt(path);
73	html("<table class='list'>\n");
74	html("<tr class='nohover'>");
75	html("<th class='left'>Mode</th>");
76	html("<th class='left'>Name</th>");
77	html("<th class='right'>Size</th>");
78	html("</tr>\n");
79	read_tree_recursive(tree, "", 0, 1, NULL, print_entry);
80	html("</table>\n");
81}