webgit_tree_action() dereferences an treeish to the tree object
[webgit] / src / actions.c
1 /*
2     cehtehs git web frontend
3
4   Copyright (C)
5     2007,               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 "actions.h"
23 #include "summary.h"
24 #include "object.h"
25 #include "log.h"
26 #include "repo.h"
27
28 #include "cwa.h"
29
30 /* todo configure this */
31 #define SHA1_HEADER <openssl/sha.h>
32 #include "git/cache.h"
33
34 #include <stdio.h>
35
36
37 static Html
38 webgit_main_menu_action (struct webgit_query* query)
39 {
40   return html ("TODO: main menu sidebar");
41 }
42
43 static Html
44 webgit_main_content_action (struct webgit_query* query)
45 {
46   Html table = html_list ();
47
48   html_list_append (table, html(
49                           html(html_tag ("tr"),
50                                html(html_tag ("th"),"Name"),
51                                html(html_tag ("th"),"Description"),
52                                html(html_tag ("th"),"Owner"),
53                                html(html_tag ("th"),"Last Change"),
54                                html(html_tag ("th"),"Author"),
55                                html(html_tag ("th"),"Committer"),
56                                html(html_tag ("th"),"Action")
57                                ),
58                           html_nl()
59                           )
60               );
61
62   LLIST_FOREACH (&query->repos, node)
63     {
64       struct webgit_repo_info* n = (struct webgit_repo_info*)node;
65       html_list_append (table, html (
66                                      html_tag ("tr"),
67                                      html(html_tag ("td"), webgit_summary_link (query, n, html (n->name))),
68                                      html(html_tag ("td"), n->description),
69                                      html(html_tag ("td"), n->owner),
70                                      html(html_tag ("td"),
71                                           webgit_object_link (query,
72                                                               n->name, strlen(n->name),
73                                                               n->last_commit, 40,
74                                                               webgit_pretty_age (n->age))
75                                           ),
76                                      html(html_tag ("td"),
77                                           webgit_email_link (n->last_author_name, n->last_author_email)
78                                           ),
79                                      html(html_tag ("td"),
80                                           webgit_email_link (n->last_committer_name, n->last_committer_email)
81                                           ),
82                                      html(html_tag ("td"),
83                                           html(
84                                                webgit_log_link (query, n, n->last_head, html("Log")),
85                                                " ",
86                                                //webgit_diff_link (query, n, n->last_head, html("Diff")),
87                                                //" ",
88                                                webgit_object_link (query,
89                                                                   n->name, strlen(n->name),
90                                                                   n->last_tree, 40,
91                                                                   html("Tree"))
92                                                )
93                                           )
94                                      ),
95                         html_nl()
96                         );
97     }
98
99   return html (html_tag ("table", html_attr ("border", "1")), table);
100 }
101
102 static Html
103 webgit_main_action (struct webgit_query* query)
104 {
105   return html(
106               html(html_tag("div"), webgit_main_menu_action (query)), html_nl (),
107               html(html_tag("div"), webgit_main_content_action (query)), html_nl ()
108               );
109 }
110
111 static Html
112 webgit_summary_action (struct webgit_query* query)
113 {
114   return html("summary");
115 }
116
117 static Html
118 webgit_log_action (struct webgit_query* query)
119 {
120   return html("log");
121 }
122
123 static Html
124 webgit_diff_action (struct webgit_query* query)
125 {
126   return html("diff");
127 }
128
129 static Html
130 webgit_object_action (struct webgit_query* query)
131 {
132   LLIST_FOREACH (&query->repos, node)
133     {
134       struct webgit_repo_info* ri = (struct webgit_repo_info*) node;
135       if (!strcmp (ri->name, query->repo))
136         {
137           chdir (ri->path);
138           break;
139         }
140     }
141
142   setup_git_directory ();
143
144   unsigned char sha1[20];
145   if (get_sha1(query->object, sha1))
146     return html("error: unknown object");
147
148   switch (sha1_object_info(sha1, NULL))
149     {
150     case OBJ_COMMIT:
151       if (!query->deref_to_tree)
152         return webgit_object_commit_action (query, sha1);
153       /* else fallthrough */
154     case OBJ_TREE:
155       return webgit_object_tree_action (query, sha1);
156     case OBJ_BLOB:
157       return webgit_object_blob_action (query, sha1);
158     case OBJ_TAG:
159       return html ("its a tag");
160
161       break;
162     default:
163       return html ("error: unknown object type");
164       break;
165     }
166 }
167
168
169 /*
170   treeish dereferenced to tree
171 */
172 static Html
173 webgit_tree_action (struct webgit_query* query)
174 {
175   query->deref_to_tree = 1;
176   return webgit_object_action (query);
177 }
178
179
180 static Html
181 webgit_raw_action (struct webgit_query* query)
182 {
183   return html("raw");
184 }
185
186 static Html
187 webgit_tag_action (struct webgit_query* query)
188 {
189   return html("tag");
190 }
191
192 static Html
193 webgit_edit_action (struct webgit_query* query)
194 {
195   return html("edit");
196 }
197
198 static Html
199 webgit_commit_action (struct webgit_query* query)
200 {
201   return html("commit");
202 }
203
204
205 Html
206 webgit_action_dispatch (struct webgit_query* query)
207 {
208 #define WEBGIT_ACTION(name, _)                   \
209 {                                               \
210  #name,                                         \
211  webgit_##name##_action,                         \
212 },
213   struct action_table{
214     char* name;
215     Html (*cb)(struct webgit_query* query);
216   } cmds[] = {WEBGIT_ACTIONS {"", NULL}};
217 #undef WEBGIT_ACTION
218
219   for (struct action_table* j = cmds; j->cb; ++j)
220     {
221       if (!strcmp (j->name, query->action))
222         return j->cb (query);
223     }
224   return html("error, no such action");
225 }
226
227