blob: 60f756089a082869bc7b96fb7c883a8650554f4b [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 Hjemli06fe0c22006-12-13 00:13:27 +010018 unsigned long size;
19
Lars Hjemli61c3ca92007-05-08 22:40:59 +020020 name = xstrdup(pathname);
21 type = sha1_object_info(sha1, &size);
22 if (type == OBJ_BAD) {
23 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);
30 html("</td><td>");
Lars Hjemli61c3ca92007-05-08 22:40:59 +020031 if (S_ISDIRLNK(mode)) {
32 htmlf("<div class='ls-dirlnk'>%s => submodule</div>", name);
33 } else if (S_ISDIR(mode)) {
Lars Hjemli06fe0c22006-12-13 00:13:27 +010034 html("<div class='ls-dir'><a href='");
35 html_attr(cgit_pageurl(cgit_query_repo, "tree",
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010036 fmt("id=%s&path=%s%s/",
37 sha1_to_hex(sha1),
38 cgit_query_path ? cgit_query_path : "",
39 pathname)));
Lars Hjemli61c3ca92007-05-08 22:40:59 +020040 htmlf("'>%s</a></div>", name);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010041 } else {
42 html("<div class='ls-blob'><a href='");
43 html_attr(cgit_pageurl(cgit_query_repo, "view",
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010044 fmt("id=%s&path=%s%s", sha1_to_hex(sha1),
45 cgit_query_path ? cgit_query_path : "",
46 pathname)));
Lars Hjemli61c3ca92007-05-08 22:40:59 +020047 htmlf("'>%s</a></div>", name);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010048 }
Lars Hjemli61c3ca92007-05-08 22:40:59 +020049 html("</div></td>");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010050 htmlf("<td class='filesize'>%li</td>", size);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010051 html("</tr>\n");
52 free(name);
53 return 0;
54}
55
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010056void cgit_print_tree(const char *hex, char *path)
Lars Hjemli06fe0c22006-12-13 00:13:27 +010057{
58 struct tree *tree;
59 unsigned char sha1[20];
60
61 if (get_sha1_hex(hex, sha1)) {
62 cgit_print_error(fmt("Invalid object id: %s", hex));
63 return;
64 }
65 tree = parse_tree_indirect(sha1);
66 if (!tree) {
67 cgit_print_error(fmt("Not a tree object: %s", hex));
68 return;
69 }
70
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010071 html_txt(path);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010072 html("<table class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +010073 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +010074 html("<th class='left'>Mode</th>");
75 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +010076 html("<th class='right'>Size</th>");
Lars Hjemlia5304282006-12-17 23:55:53 +010077 html("</tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010078 read_tree_recursive(tree, "", 0, 1, NULL, print_entry);
79 html("</table>\n");
80}