blob: dee83098c93a0113a9feb24b4aa364fc13cc1c40 [file] [log] [blame]
Lars Hjemli06fe0c22006-12-13 00:13:27 +01001/* 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;
Lars Hjemli61c3ca92007-05-08 22:40:59 +020017 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +020018 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +010019
Lars Hjemli61c3ca92007-05-08 22:40:59 +020020 name = xstrdup(pathname);
21 type = sha1_object_info(sha1, &size);
Lars Hjemlided93932007-05-11 12:12:48 +020022 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
Lars Hjemli61c3ca92007-05-08 22:40:59 +020023 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
24 name,
25 sha1_to_hex(sha1));
Lars Hjemli06fe0c22006-12-13 00:13:27 +010026 return 0;
27 }
Lars Hjemlia5304282006-12-17 23:55:53 +010028 html("<tr><td class='filemode'>");
29 html_filemode(mode);
Lars Hjemlided93932007-05-11 12:12:48 +020030 html("</td><td ");
Lars Hjemli61c3ca92007-05-08 22:40:59 +020031 if (S_ISDIRLNK(mode)) {
Lars Hjemlided93932007-05-11 12:12:48 +020032 htmlf("class='ls-mod'><a href='");
33 html_attr(fmt(cgit_repo->module_link,
34 name,
35 sha1_to_hex(sha1)));
Lars Hjemli61c3ca92007-05-08 22:40:59 +020036 } else if (S_ISDIR(mode)) {
Lars Hjemlided93932007-05-11 12:12:48 +020037 html("class='ls-dir'><a href='");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010038 html_attr(cgit_pageurl(cgit_query_repo, "tree",
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010039 fmt("id=%s&path=%s%s/",
40 sha1_to_hex(sha1),
41 cgit_query_path ? cgit_query_path : "",
42 pathname)));
Lars Hjemli06fe0c22006-12-13 00:13:27 +010043 } else {
Lars Hjemlided93932007-05-11 12:12:48 +020044 html("class='ls-blob'><a href='");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010045 html_attr(cgit_pageurl(cgit_query_repo, "view",
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010046 fmt("id=%s&path=%s%s", sha1_to_hex(sha1),
47 cgit_query_path ? cgit_query_path : "",
48 pathname)));
Lars Hjemli06fe0c22006-12-13 00:13:27 +010049 }
Lars Hjemlided93932007-05-11 12:12:48 +020050 htmlf("'>%s</a></div></td>", name);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010051 htmlf("<td class='filesize'>%li</td>", size);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010052 html("</tr>\n");
53 free(name);
54 return 0;
55}
56
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010057void cgit_print_tree(const char *hex, char *path)
Lars Hjemli06fe0c22006-12-13 00:13:27 +010058{
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
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010072 html_txt(path);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010073 html("<table class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +010074 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +010075 html("<th class='left'>Mode</th>");
76 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +010077 html("<th class='right'>Size</th>");
Lars Hjemlia5304282006-12-17 23:55:53 +010078 html("</tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010079 read_tree_recursive(tree, "", 0, 1, NULL, print_entry);
80 html("</table>\n");
81}