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