]> gitweb.ps.run Git - ps-cgit/blob - ui-tree.c
Merge branch 'lh/mimetypes'
[ps-cgit] / ui-tree.c
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 <ctype.h>
10 #include "cgit.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
14 char *curr_rev;
15 char *match_path;
16 int header = 0;
17
18 static void print_text_buffer(char *buf, unsigned long size)
19 {
20         unsigned long lineno, idx;
21         const char *numberfmt =
22                 "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";
23
24         html("<table summary='blob content' class='blob'>\n");
25         html("<tr><td class='linenumbers'><pre>");
26         idx = 0;
27         lineno = 0;
28
29         if (size) {
30                 htmlf(numberfmt, ++lineno);
31                 while(idx < size - 1) { // skip absolute last newline
32                         if (buf[idx] == '\n')
33                                 htmlf(numberfmt, ++lineno);
34                         idx++;
35                 }
36         }
37         html("</pre></td>\n");
38         html("<td class='lines'><pre><code>");
39         html_txt(buf);
40         html("</code></pre></td></tr></table>\n");
41 }
42
43 #define ROWLEN 32
44
45 static void print_binary_buffer(char *buf, unsigned long size)
46 {
47         unsigned long ofs, idx;
48         static char ascii[ROWLEN + 1];
49
50         html("<table summary='blob content' class='bin-blob'>\n");
51         html("<tr><th>ofs</th><th>hex dump</th><th>ascii</th></tr>");
52         for (ofs = 0; ofs < size; ofs += ROWLEN, buf += ROWLEN) {
53                 htmlf("<tr><td class='right'>%04x</td><td class='hex'>", ofs);
54                 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
55                         htmlf("%*s%02x",
56                               idx == 16 ? 4 : 1, "",
57                               buf[idx] & 0xff);
58                 html(" </td><td class='hex'>");
59                 for (idx = 0; idx < ROWLEN && ofs + idx < size; idx++)
60                         ascii[idx] = isgraph(buf[idx]) ? buf[idx] : '.';
61                 ascii[idx] = '\0';
62                 html_txt(ascii);
63                 html("</td></tr>\n");
64         }
65         html("</table>\n");
66 }
67
68 static void print_object(const unsigned char *sha1, char *path)
69 {
70         enum object_type type;
71         char *buf;
72         unsigned long size;
73
74         type = sha1_object_info(sha1, &size);
75         if (type == OBJ_BAD) {
76                 cgit_print_error(fmt("Bad object name: %s",
77                                      sha1_to_hex(sha1)));
78                 return;
79         }
80
81         buf = read_sha1_file(sha1, &type, &size);
82         if (!buf) {
83                 cgit_print_error(fmt("Error reading object %s",
84                                      sha1_to_hex(sha1)));
85                 return;
86         }
87
88         html(" (");
89         cgit_plain_link("plain", NULL, NULL, ctx.qry.head,
90                         curr_rev, path);
91         htmlf(")<br/>blob: %s\n", sha1_to_hex(sha1));
92
93         if (buffer_is_binary(buf, size))
94                 print_binary_buffer(buf, size);
95         else
96                 print_text_buffer(buf, size);
97 }
98
99
100 static int ls_item(const unsigned char *sha1, const char *base, int baselen,
101                    const char *pathname, unsigned int mode, int stage,
102                    void *cbdata)
103 {
104         char *name;
105         char *fullpath;
106         char *class;
107         enum object_type type;
108         unsigned long size = 0;
109
110         name = xstrdup(pathname);
111         fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
112                        ctx.qry.path ? "/" : "", name);
113
114         if (!S_ISGITLINK(mode)) {
115                 type = sha1_object_info(sha1, &size);
116                 if (type == OBJ_BAD) {
117                         htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>",
118                               name,
119                               sha1_to_hex(sha1));
120                         return 0;
121                 }
122         }
123
124         html("<tr><td class='ls-mode'>");
125         cgit_print_filemode(mode);
126         html("</td><td>");
127         if (S_ISGITLINK(mode)) {
128                 htmlf("<a class='ls-mod' href='");
129                 html_attr(fmt(ctx.repo->module_link,
130                               name,
131                               sha1_to_hex(sha1)));
132                 html("'>");
133                 html_txt(name);
134                 html("</a>");
135         } else if (S_ISDIR(mode)) {
136                 cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
137                                curr_rev, fullpath);
138         } else {
139                 class = strrchr(name, '.');
140                 if (class != NULL) {
141                         class = fmt("ls-blob %s", class + 1);
142                 } else
143                         class = "ls-blob";
144                 cgit_tree_link(name, NULL, class, ctx.qry.head,
145                                curr_rev, fullpath);
146         }
147         htmlf("</td><td class='ls-size'>%li</td>", size);
148
149         html("<td>");
150         cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev,
151                       fullpath, 0, NULL, NULL, ctx.qry.showmsg);
152         if (ctx.repo->max_stats)
153                 cgit_stats_link("stats", NULL, "button", ctx.qry.head,
154                                 fullpath);
155         html("</td></tr>\n");
156         free(name);
157         return 0;
158 }
159
160 static void ls_head()
161 {
162         html("<table summary='tree listing' class='list'>\n");
163         html("<tr class='nohover'>");
164         html("<th class='left'>Mode</th>");
165         html("<th class='left'>Name</th>");
166         html("<th class='right'>Size</th>");
167         html("<th/>");
168         html("</tr>\n");
169         header = 1;
170 }
171
172 static void ls_tail()
173 {
174         if (!header)
175                 return;
176         html("</table>\n");
177         header = 0;
178 }
179
180 static void ls_tree(const unsigned char *sha1, char *path)
181 {
182         struct tree *tree;
183
184         tree = parse_tree_indirect(sha1);
185         if (!tree) {
186                 cgit_print_error(fmt("Not a tree object: %s",
187                                      sha1_to_hex(sha1)));
188                 return;
189         }
190
191         ls_head();
192         read_tree_recursive(tree, "", 0, 1, NULL, ls_item, NULL);
193         ls_tail();
194 }
195
196
197 static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
198                      const char *pathname, unsigned mode, int stage,
199                      void *cbdata)
200 {
201         static int state;
202         static char buffer[PATH_MAX];
203         char *url;
204
205         if (state == 0) {
206                 memcpy(buffer, base, baselen);
207                 strcpy(buffer+baselen, pathname);
208                 url = cgit_pageurl(ctx.qry.repo, "tree",
209                                    fmt("h=%s&amp;path=%s", curr_rev, buffer));
210                 html("/");
211                 cgit_tree_link(xstrdup(pathname), NULL, NULL, ctx.qry.head,
212                                curr_rev, buffer);
213
214                 if (strcmp(match_path, buffer))
215                         return READ_TREE_RECURSIVE;
216
217                 if (S_ISDIR(mode)) {
218                         state = 1;
219                         ls_head();
220                         return READ_TREE_RECURSIVE;
221                 } else {
222                         print_object(sha1, buffer);
223                         return 0;
224                 }
225         }
226         ls_item(sha1, base, baselen, pathname, mode, stage, NULL);
227         return 0;
228 }
229
230
231 /*
232  * Show a tree or a blob
233  *   rev:  the commit pointing at the root tree object
234  *   path: path to tree or blob
235  */
236 void cgit_print_tree(const char *rev, char *path)
237 {
238         unsigned char sha1[20];
239         struct commit *commit;
240         const char *paths[] = {path, NULL};
241
242         if (!rev)
243                 rev = ctx.qry.head;
244
245         curr_rev = xstrdup(rev);
246         if (get_sha1(rev, sha1)) {
247                 cgit_print_error(fmt("Invalid revision name: %s", rev));
248                 return;
249         }
250         commit = lookup_commit_reference(sha1);
251         if (!commit || parse_commit(commit)) {
252                 cgit_print_error(fmt("Invalid commit reference: %s", rev));
253                 return;
254         }
255
256         html("path: <a href='");
257         html_attr(cgit_pageurl(ctx.qry.repo, "tree", fmt("h=%s", rev)));
258         html("'>root</a>");
259
260         if (path == NULL) {
261                 ls_tree(commit->tree->object.sha1, NULL);
262                 return;
263         }
264
265         match_path = path;
266         read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
267         ls_tail();
268 }