435f8df896df1eeed7ea5c12290a3f65f966bdf4
[webgit] / src / object_tree.c
1 /*
2     cehtehs git web frontend
3
4   Copyright (C)
5     2007, 2008,         Christian Thaeter <ct@pipapo.org>
6
7   This program is free software: you can redistribute it and/or modify
8   it under the terms of the GNU Affero General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU Affero General Public License for more details.
16
17   You should have received a copy of the GNU Affero General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "object.h"
22
23 /*
24   Display trees
25 */
26
27 static Html
28 webgit_object_tree_menu_action (struct webgit_repo_info* repo, unsigned char* sha1, struct tree *tree)
29 {
30   (void) repo;
31   (void) sha1;
32   (void) tree;
33   return html ("TODO: tree-object sidebar");
34 }
35
36 static const char*
37 pretty_mode (unsigned mode)
38 {
39   if (S_ISGITLINK (mode))
40     return "m---------";
41   else if (S_ISDIR (mode & S_IFMT))
42     return "drwxr-xr-x";
43   else if (S_ISLNK (mode))
44     return "lrwxrwxrwx";
45   else if (S_ISREG (mode))
46     {
47       if (mode & S_IXUSR)
48         return "-rwxr-xr-x";
49       else
50         return "-rw-r--r--";
51     }
52   else
53     return "----------";
54 }
55
56 /* callback has no user-data pointer! */
57 static struct webgit_query* query_in_flight = NULL;
58 static Html tree_in_flight = NULL;
59
60 static int
61 webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
62                   const char *name, unsigned mode, int stage)
63 {
64   (void) stage;
65   char pathname[PATH_MAX];
66
67   snprintf (pathname, PATH_MAX-1, "%.*s%s%s", baselen, base, baselen ? "/": "", name);
68
69   if (S_ISGITLINK(mode))
70     {
71       html_list_append (tree_in_flight, html (
72                                               html (
73                                                     html_tag ("tr"),
74                                                     html (html_tag ("td"), pretty_mode (mode)),
75                                                     html (html_tag ("td"),
76                                                           webgit_repo_link (query_in_flight,
77                                                                             query_in_flight->repo,
78                                                                             strlen (query_in_flight->repo),
79                                                                             name, strlen (pathname),
80                                                                             sha1_to_hex (sha1), 40,
81                                                                             "tree",
82                                                                             html_strndup (pathname, SIZE_MAX))
83                                                           ),
84                                                     html (html_tag ("td"), "history summary")
85                                                     ),
86                                               html_nl ()
87                                               )
88                         );
89     }
90   else if (S_ISDIR(mode))
91     {
92       html_list_append (tree_in_flight, html (
93                                               html (html_tag ("tr"),
94                                                     html (html_tag ("td"), pretty_mode (mode)),
95                                                     html (html_tag ("td"),
96                                                           webgit_object_link (query_in_flight,
97                                                                               query_in_flight->repo,
98                                                                               strlen (query_in_flight->repo),
99                                                                               sha1_to_hex (sha1), 40,
100                                                                               pathname,
101                                                                               NULL,
102                                                                               html_strndup (name, SIZE_MAX))
103                                                           ),
104                                                   html (html_tag ("td"), "history snap")
105                                                   ),
106                                              html_nl ()
107                                              )
108                         );
109     }
110   else
111     {
112       html_list_append (tree_in_flight, html (
113                                               html (html_tag ("tr"),
114                                                     html (html_tag ("td"), pretty_mode (mode)),
115                                                     html (html_tag ("td"),
116                                                           webgit_object_link (query_in_flight,
117                                                                               query_in_flight->repo,
118                                                                               strlen (query_in_flight->repo),
119                                                                               sha1_to_hex (sha1), 40,
120                                                                               pathname,
121                                                                               NULL,
122                                                                               html_strndup (name, SIZE_MAX))
123                                                           ),
124                                                     html (html_tag ("td"),
125                                                           "history ",
126                                                           webgit_object_link (query_in_flight,
127                                                                               query_in_flight->repo,
128                                                                               strlen (query_in_flight->repo),
129                                                                               sha1_to_hex (sha1), 40,
130                                                                               pathname,
131                                                                               "raw",
132                                                                               html ("raw")),
133                                                           " edit")
134                                                     ),
135                                               html_nl()
136                                               )
137                         );
138     }
139
140   return 0;
141 }
142
143
144 static Html
145 webgit_object_tree_content_action (struct webgit_repo_info* repo, unsigned char* sha1, struct tree *tree)
146 {
147   (void) sha1;
148   query_in_flight = repo->query;
149   tree_in_flight = html_list ();
150
151   read_tree_recursive (tree, repo->query->path,
152                        repo->query->path ? strlen (repo->query->path) : 0, 0, NULL, webgit_html_tree);
153
154   return html (html_tag ("table", html_attr ("id", "object-tree")), tree_in_flight);
155 }
156
157 Html
158 webgit_object_tree_action (struct webgit_repo_info* repo, unsigned char* sha1)
159 {
160   struct tree *tree;
161
162   tree = parse_tree_indirect (sha1);
163   if (!tree)
164     die("not a tree object");
165
166   return html(
167               html(html_tag("div", html_attr("id", "sub-menu")), webgit_object_tree_menu_action (repo, sha1, tree)), html_nl (),
168               html(html_tag("div", html_attr("id", "content")), webgit_object_tree_content_action (repo, sha1, tree)), html_nl ()
169               );
170 }
171
172
173 /*
174 //      Local Variables:
175 //      mode: C
176 //      c-file-style: "gnu"
177 //      indent-tabs-mode: nil
178 //      End:
179 */