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