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;
20 const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>";
22 type = sha1_object_info(sha1, &size);
23 if (type == OBJ_BAD) {
24 cgit_print_error(fmt("Bad object name: %s",
29 buf = read_sha1_file(sha1, &type, &size);
31 cgit_print_error(fmt("Error reading object %s",
36 html(" blob: <a href='");
37 html_attr(cgit_pageurl(ctx.qry.repo, "blob", fmt("id=%s", sha1_to_hex(sha1))));
38 htmlf("'>%s</a>",sha1_to_hex(sha1));
40 html("<table summary='blob content' class='blob'>\n");
45 if (buf[idx] == '\n') {
47 htmlf(linefmt, ++lineno);
48 html_txt(buf + start);
54 htmlf(linefmt, ++lineno);
55 html_txt(buf + start);
61 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
62 const char *pathname, unsigned int mode, int stage)
66 enum object_type type;
67 unsigned long size = 0;
69 name = xstrdup(pathname);
70 fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
71 ctx.qry.path ? "/" : "", name);
73 type = sha1_object_info(sha1, &size);
74 if (type == OBJ_BAD && !S_ISGITLINK(mode)) {
75 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
81 html("<tr><td class='ls-mode'>");
84 if (S_ISGITLINK(mode)) {
85 htmlf("<a class='ls-mod' href='");
86 html_attr(fmt(cgit_repo->module_link,
92 } else if (S_ISDIR(mode)) {
93 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
96 cgit_tree_link(name, NULL, "ls-blob", ctx.qry.head,
99 htmlf("</td><td class='ls-size'>%li</td>", size);
102 cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
103 fullpath, 0, NULL, NULL);
104 html("</td></tr>\n");
109 static void ls_head()
111 html("<table summary='tree listing' 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>");
121 static void ls_tail()
129 static void ls_tree(const unsigned char *sha1, char *path)
133 tree = parse_tree_indirect(sha1);
135 cgit_print_error(fmt("Not a tree object: %s",
141 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
146 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
147 const char *pathname, unsigned mode, int stage)
150 static char buffer[PATH_MAX];
154 memcpy(buffer, base, baselen);
155 strcpy(buffer+baselen, pathname);
156 url = cgit_pageurl(ctx.qry.repo, "tree",
157 fmt("h=%s&path=%s", curr_rev, buffer));
159 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
162 if (strcmp(match_path, buffer))
163 return READ_TREE_RECURSIVE;
168 return READ_TREE_RECURSIVE;
170 print_object(sha1, buffer);
174 ls_item(sha1, base, baselen, pathname, mode, stage);
180 * Show a tree or a blob
181 * rev: the commit pointing at the root tree object
182 * path: path to tree or blob
184 void cgit_print_tree(const char *rev, char *path)
186 unsigned char sha1[20];
187 struct commit *commit;
188 const char *paths[] = {path, NULL};
193 curr_rev = xstrdup(rev);
194 if (get_sha1(rev, sha1)) {
195 cgit_print_error(fmt("Invalid revision name: %s", rev));
198 commit = lookup_commit_reference(sha1);
199 if (!commit || parse_commit(commit)) {
200 cgit_print_error(fmt("Invalid commit reference: %s", rev));
204 html("path: <a href='");
205 html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
209 ls_tree(commit->tree->object.sha1, NULL);
214 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);