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)
59 enum object_type type;
60 unsigned long size = 0;
62 name = xstrdup(pathname);
63 fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "",
64 cgit_query_path ? "/" : "", name);
66 type = sha1_object_info(sha1, &size);
67 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
68 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
74 html("<tr><td class='ls-mode'>");
77 if (S_ISDIRLNK(mode)) {
78 htmlf("<a class='ls-mod' href='");
79 html_attr(fmt(cgit_repo->module_link,
85 } else if (S_ISDIR(mode)) {
86 cgit_tree_link(name, NULL, "ls-dir", cgit_query_head,
89 cgit_tree_link(name, NULL, "ls-blob", cgit_query_head,
92 htmlf("</td><td class='ls-size'>%li</td>", size);
95 cgit_log_link("L", "Log", "button", cgit_query_head, curr_rev,
102 static void ls_head()
104 html("<table class='list'>\n");
105 html("<tr class='nohover'>");
106 html("<th class='left'>Mode</th>");
107 html("<th class='left'>Name</th>");
108 html("<th class='right'>Size</th>");
114 static void ls_tail()
122 static void ls_tree(const unsigned char *sha1, char *path)
126 tree = parse_tree_indirect(sha1);
128 cgit_print_error(fmt("Not a tree object: %s",
134 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
139 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
140 const char *pathname, unsigned mode, int stage)
143 static char buffer[PATH_MAX];
147 memcpy(buffer, base, baselen);
148 strcpy(buffer+baselen, pathname);
149 url = cgit_pageurl(cgit_query_repo, "tree",
150 fmt("h=%s&path=%s", curr_rev, buffer));
152 cgit_tree_link(xstrdup(pathname), NULL, NULL, cgit_query_head,
155 if (strcmp(match_path, buffer))
156 return READ_TREE_RECURSIVE;
161 return READ_TREE_RECURSIVE;
163 print_object(sha1, buffer);
167 ls_item(sha1, base, baselen, pathname, mode, stage);
173 * Show a tree or a blob
174 * rev: the commit pointing at the root tree object
175 * path: path to tree or blob
177 void cgit_print_tree(const char *rev, char *path)
179 unsigned char sha1[20];
180 struct commit *commit;
181 const char *paths[] = {path, NULL};
184 rev = cgit_query_head;
186 curr_rev = xstrdup(rev);
187 if (get_sha1(rev, sha1)) {
188 cgit_print_error(fmt("Invalid revision name: %s", rev));
191 commit = lookup_commit_reference(sha1);
192 if (!commit || parse_commit(commit)) {
193 cgit_print_error(fmt("Invalid commit reference: %s", rev));
197 html("path: <a href='");
198 html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev)));
202 ls_tree(commit->tree->object.sha1, NULL);
207 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);