blob: db63e13fe7e2b5d13d28f55c75345a79bd0422e7 [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
Lars Hjemli9fb53af2007-05-14 11:10:59 +020011char *curr_rev;
Lars Hjemli06fe0c22006-12-13 00:13:27 +010012
Lars Hjemli9fb53af2007-05-14 11:10:59 +020013static int print_entry(const unsigned char *sha1, const char *base,
14 int baselen, const char *pathname, unsigned int mode,
Lars Hjemli06fe0c22006-12-13 00:13:27 +010015 int stage)
16{
17 char *name;
Lars Hjemli61c3ca92007-05-08 22:40:59 +020018 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +020019 unsigned long size = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +010020
Lars Hjemli61c3ca92007-05-08 22:40:59 +020021 name = xstrdup(pathname);
22 type = sha1_object_info(sha1, &size);
Lars Hjemlided93932007-05-11 12:12:48 +020023 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
Lars Hjemli61c3ca92007-05-08 22:40:59 +020024 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
25 name,
26 sha1_to_hex(sha1));
Lars Hjemli06fe0c22006-12-13 00:13:27 +010027 return 0;
28 }
Lars Hjemlia5304282006-12-17 23:55:53 +010029 html("<tr><td class='filemode'>");
30 html_filemode(mode);
Lars Hjemlided93932007-05-11 12:12:48 +020031 html("</td><td ");
Lars Hjemli61c3ca92007-05-08 22:40:59 +020032 if (S_ISDIRLNK(mode)) {
Lars Hjemlided93932007-05-11 12:12:48 +020033 htmlf("class='ls-mod'><a href='");
34 html_attr(fmt(cgit_repo->module_link,
35 name,
36 sha1_to_hex(sha1)));
Lars Hjemli61c3ca92007-05-08 22:40:59 +020037 } else if (S_ISDIR(mode)) {
Lars Hjemlided93932007-05-11 12:12:48 +020038 html("class='ls-dir'><a href='");
Lars Hjemli9fb53af2007-05-14 11:10:59 +020039 html_attr(cgit_pageurl(cgit_query_repo, "tree",
40 fmt("h=%s&id=%s&path=%s%s/",
41 curr_rev,
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010042 sha1_to_hex(sha1),
43 cgit_query_path ? cgit_query_path : "",
44 pathname)));
Lars Hjemli06fe0c22006-12-13 00:13:27 +010045 } else {
Lars Hjemlided93932007-05-11 12:12:48 +020046 html("class='ls-blob'><a href='");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010047 html_attr(cgit_pageurl(cgit_query_repo, "view",
Lars Hjemli9fb53af2007-05-14 11:10:59 +020048 fmt("h=%s&id=%s&path=%s%s", curr_rev,
49 sha1_to_hex(sha1),
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010050 cgit_query_path ? cgit_query_path : "",
51 pathname)));
Lars Hjemli06fe0c22006-12-13 00:13:27 +010052 }
Lars Hjemlided93932007-05-11 12:12:48 +020053 htmlf("'>%s</a></div></td>", name);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010054 htmlf("<td class='filesize'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +020055
56 html("<td class='links'><a href='");
57 html_attr(cgit_pageurl(cgit_query_repo, "log",
58 fmt("h=%s&path=%s%s",
59 curr_rev,
60 cgit_query_path ? cgit_query_path : "",
61 pathname)));
62 html("'>history</a></td>");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010063 html("</tr>\n");
64 free(name);
65 return 0;
66}
67
Lars Hjemli9fb53af2007-05-14 11:10:59 +020068void cgit_print_tree(const char *rev, const char *hex, char *path)
Lars Hjemli06fe0c22006-12-13 00:13:27 +010069{
70 struct tree *tree;
71 unsigned char sha1[20];
72
Lars Hjemli9fb53af2007-05-14 11:10:59 +020073 curr_rev = xstrdup(rev);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010074 if (get_sha1_hex(hex, sha1)) {
75 cgit_print_error(fmt("Invalid object id: %s", hex));
76 return;
77 }
78 tree = parse_tree_indirect(sha1);
79 if (!tree) {
80 cgit_print_error(fmt("Not a tree object: %s", hex));
81 return;
82 }
83
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010084 html_txt(path);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010085 html("<table class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +010086 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +010087 html("<th class='left'>Mode</th>");
88 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +010089 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +020090 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +010091 html("</tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +010092 read_tree_recursive(tree, "", 0, 1, NULL, print_entry);
93 html("</table>\n");
94}