blob: cb57d8d13aa182f244e479207b656de82ae13a9c [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",
Ondrej Jirman1a63cfc2007-05-26 01:15:10 +020040 fmt("h=%s&amp;id=%s&amp;path=%s%s/",
Lars Hjemli9fb53af2007-05-14 11:10:59 +020041 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",
Ondrej Jirman1a63cfc2007-05-26 01:15:10 +020048 fmt("h=%s&amp;id=%s&amp;path=%s%s", curr_rev,
Lars Hjemli9fb53af2007-05-14 11:10:59 +020049 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 }
Ondrej Jirman0928d882007-05-26 01:14:25 +020053 htmlf("'>%s</a></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",
Ondrej Jirman1a63cfc2007-05-26 01:15:10 +020058 fmt("h=%s&amp;path=%s%s",
Lars Hjemli9fb53af2007-05-14 11:10:59 +020059 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];
Lars Hjemli97de8f02007-05-16 01:38:02 +020072 struct commit *commit;
Lars Hjemli06fe0c22006-12-13 00:13:27 +010073
Lars Hjemli9fb53af2007-05-14 11:10:59 +020074 curr_rev = xstrdup(rev);
Lars Hjemli97de8f02007-05-16 01:38:02 +020075 get_sha1(rev, sha1);
76 commit = lookup_commit_reference(sha1);
77 if (!commit || parse_commit(commit)) {
78 cgit_print_error(fmt("Invalid head: %s", rev));
79 return;
80 }
81 if (!hex)
82 hex = sha1_to_hex(commit->tree->object.sha1);
83
Lars Hjemli06fe0c22006-12-13 00:13:27 +010084 if (get_sha1_hex(hex, sha1)) {
85 cgit_print_error(fmt("Invalid object id: %s", hex));
86 return;
87 }
88 tree = parse_tree_indirect(sha1);
89 if (!tree) {
90 cgit_print_error(fmt("Not a tree object: %s", hex));
91 return;
92 }
93
Lars Hjemli5cd2bf72007-01-12 00:46:17 +010094 html_txt(path);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010095 html("<table class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +010096 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +010097 html("<th class='left'>Mode</th>");
98 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +010099 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200100 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100101 html("</tr>\n");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100102 read_tree_recursive(tree, "", 0, 1, NULL, print_entry);
103 html("</table>\n");
104}