1 /* ui-tree.c: functions for tree output
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
15 static void print_object(const unsigned char *sha1, char *path)
17 enum object_type type;
19 unsigned long size, lineno, start, idx;
21 type = sha1_object_info(sha1, &size);
22 if (type == OBJ_BAD) {
23 cgit_print_error(fmt("Bad object name: %s",
28 buf = read_sha1_file(sha1, &type, &size);
30 cgit_print_error(fmt("Error reading object %s",
35 html("<table class='blob'>\n");
40 if (buf[idx] == '\n') {
42 htmlf("<tr><td class='no'>%d</td><td class='txt'>",
44 html_txt(buf + start);
50 html("\n</td></tr>\n");
55 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
56 const char *pathname, unsigned int mode, int stage)
59 enum object_type type;
60 unsigned long size = 0;
63 name = xstrdup(pathname);
64 type = sha1_object_info(sha1, &size);
65 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
66 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
71 qry = fmt("h=%s&path=%s%s%s", curr_rev,
72 cgit_query_path ? cgit_query_path : "",
73 cgit_query_path ? "/" : "", pathname);
74 url = cgit_pageurl(cgit_query_repo, "tree", qry);
75 html("<tr><td class='filemode'>");
78 if (S_ISDIRLNK(mode)) {
79 htmlf("class='ls-mod'><a href='");
80 html_attr(fmt(cgit_repo->module_link,
83 } else if (S_ISDIR(mode)) {
84 html("class='ls-dir'><a href='");
87 html("class='ls-blob'><a href='");
90 htmlf("'>%s</a></td>", name);
91 htmlf("<td class='filesize'>%li</td>", size);
93 html("<td class='links'><a href='");
94 qry = fmt("h=%s&path=%s%s%s", curr_rev,
95 cgit_query_path ? cgit_query_path : "",
96 cgit_query_path ? "/" : "", pathname);
97 url = cgit_pageurl(cgit_query_repo, "log", qry);
99 html("' class='button'>H</a></td>");
105 static void ls_head()
107 html("<table class='list'>\n");
108 html("<tr class='nohover'>");
109 html("<th class='left'>Mode</th>");
110 html("<th class='left'>Name</th>");
111 html("<th class='right'>Size</th>");
117 static void ls_tail()
125 static void ls_tree(const unsigned char *sha1, char *path)
129 tree = parse_tree_indirect(sha1);
131 cgit_print_error(fmt("Not a tree object: %s",
137 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
142 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
143 const char *pathname, unsigned mode, int stage)
146 static char buffer[PATH_MAX];
150 memcpy(buffer, base, baselen);
151 strcpy(buffer+baselen, pathname);
152 url = cgit_pageurl(cgit_query_repo, "tree",
153 fmt("h=%s&path=%s", curr_rev, buffer));
154 htmlf(" / <a href='");
157 html_txt(xstrdup(pathname));
160 if (strcmp(match_path, buffer))
161 return READ_TREE_RECURSIVE;
166 return READ_TREE_RECURSIVE;
168 print_object(sha1, buffer);
172 ls_item(sha1, base, baselen, pathname, mode, stage);
178 * Show a tree or a blob
179 * rev: the commit pointing at the root tree object
180 * path: path to tree or blob
182 void cgit_print_tree(const char *rev, char *path)
184 unsigned char sha1[20];
185 struct commit *commit;
186 const char *paths[] = {path, NULL};
189 rev = cgit_query_head;
191 curr_rev = xstrdup(rev);
192 if (get_sha1(rev, sha1)) {
193 cgit_print_error(fmt("Invalid revision name: %s", rev));
196 commit = lookup_commit_reference(sha1);
197 if (!commit || parse_commit(commit)) {
198 cgit_print_error(fmt("Invalid commit reference: %s", rev));
202 html("path: <a href='");
203 html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev)));
207 ls_tree(commit->tree->object.sha1, NULL);
212 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);