Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 1 | /* 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 Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 11 | char *curr_rev; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 12 | char *match_path; |
| 13 | int header = 0; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 14 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 15 | static void print_object(const unsigned char *sha1, char *path) |
| 16 | { |
| 17 | enum object_type type; |
| 18 | unsigned char *buf; |
| 19 | unsigned long size, lineno, start, idx; |
| 20 | |
| 21 | type = sha1_object_info(sha1, &size); |
| 22 | if (type == OBJ_BAD) { |
| 23 | cgit_print_error(fmt("Bad object name: %s", |
| 24 | sha1_to_hex(sha1))); |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | buf = read_sha1_file(sha1, &type, &size); |
| 29 | if (!buf) { |
| 30 | cgit_print_error(fmt("Error reading object %s", |
| 31 | sha1_to_hex(sha1))); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | html("<table class='blob'>\n"); |
| 36 | idx = 0; |
| 37 | start = 0; |
| 38 | lineno = 0; |
| 39 | while(idx < size) { |
| 40 | if (buf[idx] == '\n') { |
| 41 | buf[idx] = '\0'; |
| 42 | htmlf("<tr><td class='no'>%d</td><td class='txt'>", |
| 43 | ++lineno); |
| 44 | html_txt(buf + start); |
| 45 | html("</td></tr>\n"); |
| 46 | start = idx + 1; |
| 47 | } |
| 48 | idx++; |
| 49 | } |
| 50 | html("\n</td></tr>\n"); |
| 51 | html("</table>\n"); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static int ls_item(const unsigned char *sha1, const char *base, int baselen, |
| 56 | const char *pathname, unsigned int mode, int stage) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 57 | { |
| 58 | char *name; |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 59 | enum object_type type; |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 60 | unsigned long size = 0; |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 61 | char *url, *qry; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 62 | |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 63 | name = xstrdup(pathname); |
| 64 | type = sha1_object_info(sha1, &size); |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 65 | if (type == OBJ_BAD && !S_ISDIRLNK(mode)) { |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 66 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
| 67 | name, |
| 68 | sha1_to_hex(sha1)); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 69 | return 0; |
| 70 | } |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 71 | qry = fmt("h=%s&path=%s%s%s", curr_rev, |
| 72 | cgit_query_path ? cgit_query_path : "", |
| 73 | cgit_query_path ? "/" : "", pathname); |
| 74 | url = cgit_pageurl(cgit_query_repo, "tree", qry); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 75 | html("<tr><td class='filemode'>"); |
| 76 | html_filemode(mode); |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 77 | html("</td><td "); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 78 | if (S_ISDIRLNK(mode)) { |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 79 | htmlf("class='ls-mod'><a href='"); |
| 80 | html_attr(fmt(cgit_repo->module_link, |
| 81 | name, |
| 82 | sha1_to_hex(sha1))); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame] | 83 | } else if (S_ISDIR(mode)) { |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 84 | html("class='ls-dir'><a href='"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 85 | html_attr(url); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 86 | } else { |
Lars Hjemli | ded9393 | 2007-05-11 12:12:48 +0200 | [diff] [blame] | 87 | html("class='ls-blob'><a href='"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 88 | html_attr(url); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 89 | } |
Ondrej Jirman | 0928d88 | 2007-05-26 01:14:25 +0200 | [diff] [blame] | 90 | htmlf("'>%s</a></td>", name); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 91 | htmlf("<td class='filesize'>%li</td>", size); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 92 | |
| 93 | html("<td class='links'><a href='"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 94 | qry = fmt("h=%s&path=%s%s%s", curr_rev, |
| 95 | cgit_query_path ? cgit_query_path : "", |
| 96 | cgit_query_path ? "/" : "", pathname); |
| 97 | url = cgit_pageurl(cgit_query_repo, "log", qry); |
| 98 | html_attr(url); |
| 99 | html("' class='button'>H</a></td>"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 100 | html("</tr>\n"); |
| 101 | free(name); |
| 102 | return 0; |
| 103 | } |
| 104 | |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 105 | static void ls_head() |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 106 | { |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 107 | html("<table class='list'>\n"); |
Lars Hjemli | 777faf7 | 2007-01-28 00:39:26 +0100 | [diff] [blame] | 108 | html("<tr class='nohover'>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 109 | html("<th class='left'>Mode</th>"); |
| 110 | html("<th class='left'>Name</th>"); |
Lars Hjemli | 9a8f886 | 2006-12-16 00:19:56 +0100 | [diff] [blame] | 111 | html("<th class='right'>Size</th>"); |
Lars Hjemli | 9fb53af | 2007-05-14 11:10:59 +0200 | [diff] [blame] | 112 | html("<th/>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 113 | html("</tr>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 114 | header = 1; |
| 115 | } |
| 116 | |
| 117 | static void ls_tail() |
| 118 | { |
| 119 | if (!header) |
| 120 | return; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 121 | html("</table>\n"); |
Lars Hjemli | ffc6973 | 2007-06-16 20:20:42 +0200 | [diff] [blame^] | 122 | header = 0; |
| 123 | } |
| 124 | |
| 125 | static void ls_tree(const unsigned char *sha1, char *path) |
| 126 | { |
| 127 | struct tree *tree; |
| 128 | |
| 129 | tree = parse_tree_indirect(sha1); |
| 130 | if (!tree) { |
| 131 | cgit_print_error(fmt("Not a tree object: %s", |
| 132 | sha1_to_hex(sha1))); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | ls_head(); |
| 137 | read_tree_recursive(tree, "", 0, 1, NULL, ls_item); |
| 138 | ls_tail(); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
| 143 | const char *pathname, unsigned mode, int stage) |
| 144 | { |
| 145 | static int state; |
| 146 | static char buffer[PATH_MAX]; |
| 147 | char *url; |
| 148 | |
| 149 | if (state == 0) { |
| 150 | memcpy(buffer, base, baselen); |
| 151 | strcpy(buffer+baselen, pathname); |
| 152 | url = cgit_pageurl(cgit_query_repo, "tree", |
| 153 | fmt("h=%s&path=%s", curr_rev, buffer)); |
| 154 | htmlf(" / <a href='"); |
| 155 | html_attr(url); |
| 156 | html("'>"); |
| 157 | html_txt(xstrdup(pathname)); |
| 158 | html("</a>"); |
| 159 | |
| 160 | if (strcmp(match_path, buffer)) |
| 161 | return READ_TREE_RECURSIVE; |
| 162 | |
| 163 | if (S_ISDIR(mode)) { |
| 164 | state = 1; |
| 165 | ls_head(); |
| 166 | return READ_TREE_RECURSIVE; |
| 167 | } else { |
| 168 | print_object(sha1, buffer); |
| 169 | return 0; |
| 170 | } |
| 171 | } |
| 172 | ls_item(sha1, base, baselen, pathname, mode, stage); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* |
| 178 | * Show a tree or a blob |
| 179 | * rev: the commit pointing at the root tree object |
| 180 | * path: path to tree or blob |
| 181 | */ |
| 182 | void cgit_print_tree(const char *rev, char *path) |
| 183 | { |
| 184 | unsigned char sha1[20]; |
| 185 | struct commit *commit; |
| 186 | const char *paths[] = {path, NULL}; |
| 187 | |
| 188 | if (!rev) |
| 189 | rev = cgit_query_head; |
| 190 | |
| 191 | curr_rev = xstrdup(rev); |
| 192 | if (get_sha1(rev, sha1)) { |
| 193 | cgit_print_error(fmt("Invalid revision name: %s", rev)); |
| 194 | return; |
| 195 | } |
| 196 | commit = lookup_commit_reference(sha1); |
| 197 | if (!commit || parse_commit(commit)) { |
| 198 | cgit_print_error(fmt("Invalid commit reference: %s", rev)); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | html("path: <a href='"); |
| 203 | html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("h=%s", rev))); |
| 204 | html("'>root</a>"); |
| 205 | |
| 206 | if (path == NULL) { |
| 207 | ls_tree(commit->tree->object.sha1, NULL); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | match_path = path; |
| 212 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree); |
| 213 | ls_tail(); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 214 | } |