Sketch menu contents
[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 (html_tag ("div"),
34                "Project or webgit logo<br />",
35                "diff against.. <br />",
36                "history <br />",
37                "snapshot <br />",
38                "Back to parent tree <br />",
39                "link to project summary <br />",
40                "link to main page <br />"
41                );
42 }
43
44 static const char*
45 pretty_mode (unsigned mode)
46 {
47   if (S_ISGITLINK (mode))
48     return "m---------";
49   else if (S_ISDIR (mode & S_IFMT))
50     return "drwxr-xr-x";
51   else if (S_ISLNK (mode))
52     return "lrwxrwxrwx";
53   else if (S_ISREG (mode))
54     {
55       if (mode & S_IXUSR)
56         return "-rwxr-xr-x";
57       else
58         return "-rw-r--r--";
59     }
60   else
61     return "----------";
62 }
63
64 /* callback has no user-data pointer! */
65 static struct webgit_query* query_in_flight = NULL;
66 static Html tree_in_flight = NULL;
67
68 static int
69 webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
70                   const char *name, unsigned mode, int stage)
71 {
72   (void) stage;
73   char pathname[PATH_MAX];
74
75   snprintf (pathname, PATH_MAX-1, "%.*s%s%s", baselen, base, baselen ? "/": "", name);
76
77   if (S_ISGITLINK(mode))
78     {
79       html_list_append (tree_in_flight, html (
80                                               html (
81                                                     html_tag ("tr"),
82                                                     html (html_tag ("td"), pretty_mode (mode)),
83                                                     html (html_tag ("td"),
84                                                           webgit_repo_link (query_in_flight,
85                                                                             query_in_flight->repo,
86                                                                             strlen (query_in_flight->repo),
87                                                                             name, strlen (pathname),
88                                                                             sha1_to_hex (sha1), 40,
89                                                                             "tree",
90                                                                             html_strndup (pathname, SIZE_MAX))
91                                                           ),
92                                                     html (html_tag ("td"), "history summary")
93                                                     ),
94                                               html_nl ()
95                                               )
96                         );
97     }
98   else if (S_ISDIR(mode))
99     {
100       html_list_append (tree_in_flight, html (
101                                               html (html_tag ("tr"),
102                                                     html (html_tag ("td"), pretty_mode (mode)),
103                                                     html (html_tag ("td"),
104                                                           webgit_object_link (query_in_flight,
105                                                                               query_in_flight->repo,
106                                                                               strlen (query_in_flight->repo),
107                                                                               sha1_to_hex (sha1), 40,
108                                                                               pathname,
109                                                                               NULL,
110                                                                               html_strndup (name, SIZE_MAX))
111                                                           ),
112                                                   html (html_tag ("td"), "history snap")
113                                                   ),
114                                              html_nl ()
115                                              )
116                         );
117     }
118   else
119     {
120       html_list_append (tree_in_flight, html (
121                                               html (html_tag ("tr"),
122                                                     html (html_tag ("td"), pretty_mode (mode)),
123                                                     html (html_tag ("td"),
124                                                           webgit_object_link (query_in_flight,
125                                                                               query_in_flight->repo,
126                                                                               strlen (query_in_flight->repo),
127                                                                               sha1_to_hex (sha1), 40,
128                                                                               pathname,
129                                                                               NULL,
130                                                                               html_strndup (name, SIZE_MAX))
131                                                           ),
132                                                     html (html_tag ("td"),
133                                                           "history ",
134                                                           webgit_object_link (query_in_flight,
135                                                                               query_in_flight->repo,
136                                                                               strlen (query_in_flight->repo),
137                                                                               sha1_to_hex (sha1), 40,
138                                                                               pathname,
139                                                                               "raw",
140                                                                               html ("raw")),
141                                                           " edit")
142                                                     ),
143                                               html_nl()
144                                               )
145                         );
146     }
147
148   return 0;
149 }
150
151
152 static Html
153 webgit_object_tree_content_action (struct webgit_repo_info* repo, unsigned char* sha1, struct tree *tree)
154 {
155   (void) sha1;
156   query_in_flight = repo->query;
157   tree_in_flight = html_list ();
158
159   read_tree_recursive (tree, repo->query->path,
160                        repo->query->path ? strlen (repo->query->path) : 0, 0, NULL, webgit_html_tree);
161
162   return html (html_tag ("table", html_attr ("id", "object-tree")), tree_in_flight);
163 }
164
165 Html
166 webgit_object_tree_action (struct webgit_repo_info* repo, unsigned char* sha1)
167 {
168   struct tree *tree;
169
170   tree = parse_tree_indirect (sha1);
171   if (!tree)
172     die("not a tree object");
173
174   return html(
175               html(html_tag("div", html_attr("id", "sub-menu")), webgit_object_tree_menu_action (repo, sha1, tree)), html_nl (),
176               html(html_tag("div", html_attr("id", "content")), webgit_object_tree_content_action (repo, sha1, tree)), html_nl ()
177               );
178 }
179
180
181 /*
182 //      Local Variables:
183 //      mode: C
184 //      c-file-style: "gnu"
185 //      indent-tabs-mode: nil
186 //      End:
187 */