blob: d503bee9cc8b316c0ced1ad96f694bc480fa90f7 [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 Hjemliffc69732007-06-16 20:20:42 +020012char *match_path;
13int header = 0;
Lars Hjemli06fe0c22006-12-13 00:13:27 +010014
Lars Hjemliffc69732007-06-16 20:20:42 +020015static 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 }
Lars Hjemliffc69732007-06-16 20:20:42 +020050 html("</table>\n");
51}
52
53
54static int ls_item(const unsigned char *sha1, const char *base, int baselen,
55 const char *pathname, unsigned int mode, int stage)
Lars Hjemli06fe0c22006-12-13 00:13:27 +010056{
57 char *name;
Lars Hjemli44947bf2007-06-17 01:23:08 +020058 char *fullpath;
Lars Hjemli61c3ca92007-05-08 22:40:59 +020059 enum object_type type;
Lars Hjemlided93932007-05-11 12:12:48 +020060 unsigned long size = 0;
Lars Hjemliffc69732007-06-16 20:20:42 +020061 char *url, *qry;
Lars Hjemli06fe0c22006-12-13 00:13:27 +010062
Lars Hjemli61c3ca92007-05-08 22:40:59 +020063 name = xstrdup(pathname);
Lars Hjemli44947bf2007-06-17 01:23:08 +020064 fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "",
65 cgit_query_path ? "/" : "", name);
66
Lars Hjemli61c3ca92007-05-08 22:40:59 +020067 type = sha1_object_info(sha1, &size);
Lars Hjemlided93932007-05-11 12:12:48 +020068 if (type == OBJ_BAD && !S_ISDIRLNK(mode)) {
Lars Hjemli61c3ca92007-05-08 22:40:59 +020069 htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
70 name,
71 sha1_to_hex(sha1));
Lars Hjemli06fe0c22006-12-13 00:13:27 +010072 return 0;
73 }
Lars Hjemli44947bf2007-06-17 01:23:08 +020074
Lars Hjemli426032f2007-06-17 13:17:00 +020075 html("<tr><td class='ls-mode'>");
Lars Hjemlia5304282006-12-17 23:55:53 +010076 html_filemode(mode);
Lars Hjemli426032f2007-06-17 13:17:00 +020077 html("</td><td>");
Lars Hjemli61c3ca92007-05-08 22:40:59 +020078 if (S_ISDIRLNK(mode)) {
Lars Hjemli426032f2007-06-17 13:17:00 +020079 htmlf("<a class='ls-mod' href='");
Lars Hjemlided93932007-05-11 12:12:48 +020080 html_attr(fmt(cgit_repo->module_link,
81 name,
82 sha1_to_hex(sha1)));
Lars Hjemli44947bf2007-06-17 01:23:08 +020083 html("'>");
84 html_txt(name);
85 html("</a>");
Lars Hjemli61c3ca92007-05-08 22:40:59 +020086 } else if (S_ISDIR(mode)) {
Lars Hjemli426032f2007-06-17 13:17:00 +020087 cgit_tree_link(name, NULL, "ls-dir", cgit_query_head,
Lars Hjemli44947bf2007-06-17 01:23:08 +020088 curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010089 } else {
Lars Hjemli426032f2007-06-17 13:17:00 +020090 cgit_tree_link(name, NULL, "ls-blob", cgit_query_head,
Lars Hjemli44947bf2007-06-17 01:23:08 +020091 curr_rev, fullpath);
Lars Hjemli06fe0c22006-12-13 00:13:27 +010092 }
Lars Hjemli426032f2007-06-17 13:17:00 +020093 htmlf("</td><td class='ls-size'>%li</td>", size);
Lars Hjemli9fb53af2007-05-14 11:10:59 +020094
Lars Hjemli426032f2007-06-17 13:17:00 +020095 html("<td><a href='");
Lars Hjemliffc69732007-06-16 20:20:42 +020096 qry = fmt("h=%s&amp;path=%s%s%s", curr_rev,
97 cgit_query_path ? cgit_query_path : "",
98 cgit_query_path ? "/" : "", pathname);
99 url = cgit_pageurl(cgit_query_repo, "log", qry);
100 html_attr(url);
Lars Hjemli426032f2007-06-17 13:17:00 +0200101 html("' title='Log' class='button'>L</a></td>");
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100102 html("</tr>\n");
103 free(name);
104 return 0;
105}
106
Lars Hjemliffc69732007-06-16 20:20:42 +0200107static void ls_head()
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100108{
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100109 html("<table class='list'>\n");
Lars Hjemli777faf72007-01-28 00:39:26 +0100110 html("<tr class='nohover'>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100111 html("<th class='left'>Mode</th>");
112 html("<th class='left'>Name</th>");
Lars Hjemli9a8f8862006-12-16 00:19:56 +0100113 html("<th class='right'>Size</th>");
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200114 html("<th/>");
Lars Hjemlia5304282006-12-17 23:55:53 +0100115 html("</tr>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200116 header = 1;
117}
118
119static void ls_tail()
120{
121 if (!header)
122 return;
Lars Hjemli06fe0c22006-12-13 00:13:27 +0100123 html("</table>\n");
Lars Hjemliffc69732007-06-16 20:20:42 +0200124 header = 0;
125}
126
127static void ls_tree(const unsigned char *sha1, char *path)
128{
129 struct tree *tree;
130
131 tree = parse_tree_indirect(sha1);
132 if (!tree) {
133 cgit_print_error(fmt("Not a tree object: %s",
134 sha1_to_hex(sha1)));
135 return;
136 }
137
138 ls_head();
139 read_tree_recursive(tree, "", 0, 1, NULL, ls_item);
140 ls_tail();
141}
142
143
144static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
145 const char *pathname, unsigned mode, int stage)
146{
147 static int state;
148 static char buffer[PATH_MAX];
149 char *url;
150
151 if (state == 0) {
152 memcpy(buffer, base, baselen);
153 strcpy(buffer+baselen, pathname);
154 url = cgit_pageurl(cgit_query_repo, "tree",
155 fmt("h=%s&amp;path=%s", curr_rev, buffer));
Lars Hjemli44947bf2007-06-17 01:23:08 +0200156 html("/");
157 cgit_tree_link(xstrdup(pathname), NULL, NULL, cgit_query_head,
158 curr_rev, buffer);
Lars Hjemliffc69732007-06-16 20:20:42 +0200159
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 */
182void 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 Hjemli06fe0c22006-12-13 00:13:27 +0100214}