first object ui for commits
[webgit] / src / object.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 "object.h"
23
24 #define SHA1_HEADER <openssl/sha.h>
25 #include "git/git-compat-util.h"
26 #include "git/object.h"
27
28
29 Html
30 ctgit_object_link (struct ctgit_query* query, struct ctgit_repo_info* n, const char* object, Html text)
31 {
32   return html (
33                html_tag ("a",
34                          html_attr ("href", html_fmt ("%s?repo=%s&object=%s",
35                                                       query->request->script_name,
36                                                       n->name,
37                                                       object)
38                                     )
39                          ),
40                text
41                );
42 }
43
44
45
46 static Html
47 ctgit_object_commit_menu_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
48 {
49   return html ("TODO: commit-object sidebar");
50 }
51
52 static Html
53 ctgit_object_commit_content_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
54 {
55   Html tree = html_list ();
56   Html parents = html_list ();
57   Html author_text = html_list ();
58   Html author = html_list ();
59   Html committer = html_list ();
60
61   const char* author_beg = NULL;
62   const char* author_end = NULL;
63
64   const char* i = buf;
65   while (i && i < buf+size)
66     {
67       if (*i == '\n')
68         {
69           while (*i == '\n')
70             ++i;
71           break; /* message */
72         }
73       if (!strncmp (i, "tree ", 5))
74         {
75           html_list_append (tree, html (
76                                            "Tree: ",
77                                            html (
78                                                  html_tag ("a",
79                                                            html_attr ("href", html_fmt ("%s?repo=%s&object=%.40s",
80                                                                                         query->request->script_name,
81                                                                                         query->repo,
82                                                                                         i+5)
83                                                                       )
84                                                            ),
85                                                  html_fmt ("%.40s", i + 5)
86                                                  )
87                                            )
88                             );
89
90           (i = strchr (i, '\n')) && ++i;
91           continue;
92         }
93       else if (!strncmp (i, "parent ", 7))
94         {
95           html_list_append (parents, html (
96                                            "Parent: ",
97                                            html (
98                                                  html_tag ("a",
99                                                            html_attr ("href", html_fmt ("%s?repo=%s&object=%.40s",
100                                                                                         query->request->script_name,
101                                                                                         query->repo,
102                                                                                         i+7)
103                                                                       )
104                                                            ),
105                                                  html_fmt ("%.40s", i + 7)
106                                                  )
107                                            )
108                             );
109           (i = strchr (i, '\n')) && ++i;
110           continue;
111         }
112       else if (!strncmp (i, "author ", 7))
113         {
114           char* email_beg = strchr (i, '<');
115           char* email_end = strchr (email_beg, '>');
116
117           author_beg = i+7;
118           author_end = strchr (author_beg, '\n');
119
120           Html name = html_strndup (i+7, email_beg - i - 8);
121           Html email = html_strndup (email_beg + 1, email_end - email_beg - 1);
122
123           struct tm tm;
124           strptime (email_end + 2, "%s %Z", &tm);
125           char pretty_date[256];
126           strftime (pretty_date, 255, "%c", &tm);
127
128           html_list_append (author_text, "Author");
129
130           html_list_append (author, html (
131                                           html ( author_text ), /*BUG: libcwa bug, must be wraped in html()*/
132                                           html (
133                                                 html_tag("a",
134                                                          html_attr ("href",
135                                                                     html ("mailto:", email))
136                                                          ),
137                                                 name
138                                                 ),
139                                           html_strndup (pretty_date, 255)
140                                           )
141                             );
142           (i = strchr (i, '\n')) && ++i;
143           continue;
144         }
145       else if (!strncmp (i, "committer ", 10))
146         {
147           char* email_beg = strchr (i, '<');
148           char* email_end = strchr (email_beg, '>');
149
150           if (author_beg && author_end && strncmp (i + 10, author_beg, author_end - author_beg))
151             {
152               Html name = html_strndup (i+10, email_beg - i - 11);
153               Html email = html_strndup (email_beg + 1, email_end - email_beg - 1);
154
155               struct tm tm;
156               strptime (email_end + 2, "%s %Z", &tm);
157               char pretty_date[256];
158               strftime (pretty_date, 255, "%c", &tm);
159
160               html_list_append (committer, html ("Committer: ",
161                                                  html(
162                                                       html_tag("a",
163                                                                html_attr("href",
164                                                                          html ("mailto:", email))
165                                                                ),
166                                                       name
167                                                       ),
168                                                  html_strndup (pretty_date, 255)
169                                                  )
170                                 );
171             }
172           else
173             html_list_append (author_text, "and Committer");
174
175           (i = strchr (i, '\n')) && ++i;
176           continue;
177         }
178       ++i;
179     }
180
181   html_list_append (author_text, ": ");
182
183
184
185   Html headline = html_list ();
186   Html message = html_list ();
187
188   if (i)
189     {
190       const char* message_beg = strchr (i, '\n');
191
192       if (message_beg)
193         {
194           html_list_append (headline,
195                             html_strndup (i, message_beg - i)
196                             );
197
198           while (*message_beg == '\n')
199             ++message_beg;
200
201           html_list_append (message,
202                             html_strndup (message_beg, SIZE_MAX)
203                             );
204         }
205       else
206         {
207           html_list_append (headline,
208                             html_strndup (i, SIZE_MAX)
209                             );
210           html_list_append (message,
211                             html_strndup (i, SIZE_MAX)
212                             );
213         }
214     }
215
216   /* diffstat */
217
218
219   return html (
220                html (html_tag ("h3"), headline),
221                html (html_tag ("div"), tree), html_nl (),
222                html (html_tag ("div"), parents), html_nl (),
223                html (html_tag ("div"), author), html_nl (),
224                html (html_tag ("div"), committer), html_nl (),
225                html (html_tag ("pre"), message)
226                );
227 }
228
229 Html
230 ctgit_object_commit_action (struct ctgit_query* query, unsigned char* sha1)
231 {
232   void* buf;
233   unsigned long size;
234
235   buf = read_object_with_reference (sha1, "commit", &size, NULL);
236
237   return html(
238               html(html_tag("div"), ctgit_object_commit_menu_action (query, sha1, buf, size)), html_nl (),
239               html(html_tag("div"), ctgit_object_commit_content_action (query, sha1, buf, size)), html_nl ()
240               );
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254 static Html
255 ctgit_object_tree_menu_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
256 {
257   return html ("TODO: tree-object sidebar");
258 }
259
260 static Html
261 ctgit_object_tree_content_action (struct ctgit_query* query, unsigned char* sha1, void* buf, unsigned long size)
262 {
263
264   return html_fmt ("tree content %s", buf);
265 }
266
267 Html
268 ctgit_object_tree_action (struct ctgit_query* query, unsigned char* sha1)
269 {
270   unsigned long size;
271   void* buf;
272   buf = read_object_with_reference (sha1, "tree", &size, NULL);
273
274   return html(
275               html(html_tag("div"), ctgit_object_tree_menu_action (query, sha1, buf, size)), html_nl (),
276               html(html_tag("div"), ctgit_object_tree_content_action (query, sha1, buf, size)), html_nl ()
277               );
278 }
279
280
281
282
283 /*pretty printer for sourcecode*/
284 //Html
285 //ctgit_object_blob_dispatch (struct ctgit_query* query, const char *sha1);