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