adjusting table css
[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
8   modify it under the terms of the GNU General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (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 General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "object.h"
23
24 /*
25   Display trees
26 */
27
28 static Html
29 webgit_object_tree_menu_action (struct webgit_repo_info* repo, unsigned char* sha1, struct tree *tree)
30 {
31   (void) repo;
32   (void) sha1;
33   (void) tree;
34   return html ("TODO: tree-object sidebar");
35 }
36
37 static const char*
38 pretty_mode (unsigned mode)
39 {
40   if (S_ISGITLINK (mode))
41     return "m---------";
42   else if (S_ISDIR (mode & S_IFMT))
43     return "drwxr-xr-x";
44   else if (S_ISLNK (mode))
45     return "lrwxrwxrwx";
46   else if (S_ISREG (mode))
47     {
48       if (mode & S_IXUSR)
49         return "-rwxr-xr-x";
50       else
51         return "-rw-r--r--";
52     }
53   else
54     return "----------";
55 }
56
57 /* callback has no user-data pointer! */
58 static struct webgit_query* query_in_flight = NULL;
59 static Html tree_in_flight = NULL;
60
61 static int
62 webgit_html_tree (const unsigned char *sha1, const char *base, int baselen,
63                   const char *name, unsigned mode, int stage)
64 {
65   (void) stage;
66   char pathname[PATH_MAX];
67
68   snprintf (pathname, PATH_MAX-1, "%.*s%s%s", baselen, base, baselen ? "/": "", name);
69
70   if (S_ISGITLINK(mode))
71     {
72       html_list_append (tree_in_flight, html (
73                                               html (
74                                                     html_tag ("tr"),
75                                                     html (html_tag ("td"), pretty_mode (mode)),
76                                                     html (html_tag ("td"),
77                                                           webgit_repo_link (query_in_flight,
78                                                                             query_in_flight->repo,
79                                                                             strlen (query_in_flight->repo),
80                                                                             name, strlen (pathname),
81                                                                             sha1_to_hex (sha1), 40,
82                                                                             "tree",
83                                                                             html_strndup (pathname, SIZE_MAX))
84                                                           ),
85                                                     html (html_tag ("td"), "history summary")
86                                                     ),
87                                               html_nl ()
88                                               )
89                         );
90     }
91   else if (S_ISDIR(mode))
92     {
93       html_list_append (tree_in_flight, html (
94                                               html (html_tag ("tr"),
95                                                     html (html_tag ("td"), pretty_mode (mode)),
96                                                     html (html_tag ("td"),
97                                                           webgit_object_link (query_in_flight,
98                                                                               query_in_flight->repo,
99                                                                               strlen (query_in_flight->repo),
100                                                                               sha1_to_hex (sha1), 40,
101                                                                               pathname,
102                                                                               NULL,
103                                                                               html_strndup (name, SIZE_MAX))
104                                                           ),
105                                                   html (html_tag ("td"), "history snap")
106                                                   ),
107                                              html_nl ()
108                                              )
109                         );
110     }
111   else
112     {
113       html_list_append (tree_in_flight, html (
114                                               html (html_tag ("tr"),
115                                                     html (html_tag ("td"), pretty_mode (mode)),
116                                                     html (html_tag ("td"),
117                                                           webgit_object_link (query_in_flight,
118                                                                               query_in_flight->repo,
119                                                                               strlen (query_in_flight->repo),
120                                                                               sha1_to_hex (sha1), 40,
121                                                                               pathname,
122                                                                               NULL,
123                                                                               html_strndup (name, SIZE_MAX))
124                                                           ),
125                                                     html (html_tag ("td"),
126                                                           "history ",
127                                                           webgit_object_link (query_in_flight,
128                                                                               query_in_flight->repo,
129                                                                               strlen (query_in_flight->repo),
130                                                                               sha1_to_hex (sha1), 40,
131                                                                               pathname,
132                                                                               "raw",
133                                                                               html ("raw")),
134                                                           " edit")
135                                                     ),
136                                               html_nl()
137                                               )
138                         );
139     }
140
141   return 0;
142 }
143
144
145 static Html
146 webgit_object_tree_content_action (struct webgit_repo_info* repo, unsigned char* sha1, struct tree *tree)
147 {
148   (void) sha1;
149   query_in_flight = repo->query;
150   tree_in_flight = html_list ();
151
152   read_tree_recursive (tree, repo->query->path,
153                        repo->query->path ? strlen (repo->query->path) : 0, 0, NULL, webgit_html_tree);
154
155   return html (html_tag ("table", html_attr ("id", "object-tree")), tree_in_flight);
156 }
157
158 Html
159 webgit_object_tree_action (struct webgit_repo_info* repo, unsigned char* sha1)
160 {
161   struct tree *tree;
162
163   tree = parse_tree_indirect (sha1);
164   if (!tree)
165     die("not a tree object");
166
167   return html(
168               html(html_tag("div"), webgit_object_tree_menu_action (repo, sha1, tree)), html_nl (),
169               html(html_tag("div"), webgit_object_tree_content_action (repo, sha1, tree)), html_nl ()
170               );
171 }
172