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