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);
54 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
55 const char *pathname, unsigned int mode, int stage)
58 enum object_type type;
59 unsigned long size = 0;
62 name = xstrdup(pathname);
63 type = sha1_object_info(sha1, &size);
64 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
65 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
70 qry = fmt("h=%s&path=%s%s%s", curr_rev,
71 cgit_query_path ? cgit_query_path : "",
72 cgit_query_path ? "/" : "", pathname);
73 url = cgit_pageurl(cgit_query_repo, "tree", qry);
74 html("<tr><td class='filemode'>");
77 if (S_ISDIRLNK(mode)) {
78 htmlf("class='ls-mod'><a href='");
79 html_attr(fmt(cgit_repo->module_link,
82 } else if (S_ISDIR(mode)) {
83 html("class='ls-dir'><a href='");
86 html("class='ls-blob'><a href='");
89 htmlf("'>%s</a></td>", name);
90 htmlf("<td class='filesize'>%li</td>", size);
92 html("<td class='links'><a href='");
93 qry = fmt("h=%s&path=%s%s%s", curr_rev,
94 cgit_query_path ? cgit_query_path : "",
95 cgit_query_path ? "/" : "", pathname);
96 url = cgit_pageurl(cgit_query_repo, "log", qry);
98 html("' class='button'>H</a></td>");
104 static void ls_head()
106 html("<table class='list'>\n");
107 html("<tr class='nohover'>");
108 html("<th class='left'>Mode</th>");
109 html("<th class='left'>Name</th>");
110 html("<th class='right'>Size</th>");
116 static void ls_tail()
124 static void ls_tree(const unsigned char *sha1, char *path)
128 tree = parse_tree_indirect(sha1);
130 cgit_print_error(fmt("Not a tree object: %s",
136 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
141 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
142 const char *pathname, unsigned mode, int stage)
145 static char buffer[PATH_MAX];
149 memcpy(buffer, base, baselen);
150 strcpy(buffer+baselen, pathname);
151 url = cgit_pageurl(cgit_query_repo, "tree",
152 fmt("h=%s&path=%s", curr_rev, buffer));
153 htmlf(" / <a href='");
156 html_txt(xstrdup(pathname));
159 if (strcmp(match_path, buffer))
160 return READ_TREE_RECURSIVE;
165 return READ_TREE_RECURSIVE;
167 print_object(sha1, buffer);
171 ls_item(sha1, base, baselen, pathname, mode, stage);
177 * Show a tree or a blob
178 * rev: the commit pointing at the root tree object
179 * path: path to tree or blob
181 void cgit_print_tree(const char *rev, char *path)
183 unsigned char sha1[20];
184 struct commit *commit;
185 const char *paths[] = {path, NULL};
188 rev = cgit_query_head;
190 curr_rev = xstrdup(rev);
191 if (get_sha1(rev, sha1)) {
192 cgit_print_error(fmt("Invalid revision name: %s", rev));
195 commit = lookup_commit_reference(sha1);
196 if (!commit || parse_commit(commit)) {
197 cgit_print_error(fmt("Invalid commit reference: %s", rev));
201 html("path: <a href='");
202 html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev)));
206 ls_tree(commit->tree->object.sha1, NULL);
211 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);