add odd/even class for table coloring
[webgit] / src / log.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 "log.h"
22 #include "object.h"
23
24 #define SHA1_HEADER <openssl/sha.h>
25 #include "git/cache.h"
26 #include "git/commit.h"
27 #include "git/diff.h"
28 #include "git/revision.h"
29
30 Html
31 webgit_log_link (struct webgit_query* query,
32                  const char* repo,
33                  const char* head,
34                  const char* object, int object_len,
35                  int count,
36                  Html text)
37 {
38   return html (
39                html_tag ("a",
40                          html_attr ("href", html(
41                                                  html_fmt ("%s?repo=%s&action=log&head=%s",
42                                                            query->request->script_name,
43                                                            repo,
44                                                            head?head:"master"),
45                                                  object ? html_fmt("&object=%.*s",
46                                                                    object_len, object)
47                                                  : html (),
48                                                  count > 0 ? html_fmt("&count=%d",
49                                                                       count)
50                                                  : html ()
51                                                  )
52                                     )
53                          ),
54                text
55                );
56 }
57
58
59 Html
60 webgit_log_menu_action (struct webgit_query* query)
61 {
62   (void) query;
63   return html (html_tag("div", html_attr("id", "sub-menu")), "TODO: log sidebar");
64 }
65
66
67 Html
68 webgit_log_table (struct webgit_query* query, const char* object, int count, int msg_flag)
69 {
70   struct rev_info rev;
71   const char* argv[] = {NULL, object, NULL};
72   int argc = 2;
73
74   init_revisions (&rev, NULL);
75   rev.verbose_header = 1;
76   rev.topo_order = 1;
77
78   setup_revisions (argc, argv, &rev, NULL);
79
80   prepare_revision_walk(&rev);
81
82   Html table = html_list ();
83   Html cont = html_list ();
84   int row = 0;
85
86   struct commit *commit;
87   while ((commit = get_revision (&rev)))
88     {
89       if (!count--)
90         {
91           /* displayed query->count lines, finish */
92           html_list_append (cont,
93                             webgit_log_link (
94                                              query,
95                                              query->repo,
96                                              query->head,
97                                              sha1_to_hex(commit->object.sha1), 40,
98                                              query->count_def,
99                                              html("More..."))
100                             );
101           free(commit->buffer);
102           commit->buffer = NULL;
103           free_commit_list(commit->parents);
104           commit->parents = NULL;
105           break;
106         }
107
108       /* TODO: check if commit is one of the heads and display that */
109
110       struct tm tm;
111       webgit_object_commit_committer_date_parse (commit, &tm);
112
113       char pretty_date[256];
114       strftime (pretty_date, 255, "%c", &tm);
115
116       ++row;
117
118       html_list_append (table,
119                         html (
120                               html (
121                                     html_tag ("tr",
122                                               html_attr ("class", (row & 1) ? "odd" : "even")
123                                               ),
124                                     html(html_tag ("td"),
125                                          html_strndup (pretty_date, 254)
126                                          ),
127                                     html(html_tag ("td"),
128                                          webgit_object_link (query,
129                                                              query->repo,
130                                                              strlen (query->repo),
131                                                              sha1_to_hex (commit->object.sha1),
132                                                              40,
133                                                              NULL,
134                                                              NULL,
135                                                              webgit_object_commit_header_parse (commit)
136                                                              )
137                                          )
138                                     ),
139                               html (
140                                     html_tag ("tr",
141                                               html_attr ("class", (row & 1) ? "odd" : "even")
142                                               ),
143                                     html(html_tag ("td"),
144                                          webgit_email_link (
145                                                             webgit_object_commit_author_name_parse (commit),
146                                                             webgit_object_commit_author_email_parse (commit)
147                                                             )
148                                          ),
149                                     html (html_tag ("td"),
150                                          html(
151                                               "Diff ",
152                                               " ",
153                                               webgit_object_link (query,
154                                                                   query->repo,
155                                                                   strlen (query->repo),
156                                                                   webgit_object_commit_tree_parse (commit),
157                                                                   40,
158                                                                   NULL,
159                                                                   NULL,
160                                                                   html ("Tree")
161                                                                   )
162                                               )
163                                           )
164                                     )
165                               )
166                         );
167
168       if (msg_flag)
169         html_list_append (table,
170                           html (
171                                 html_tag ("tr",
172                                           html_attr ("class", (row & 1) ? "odd" : "even")
173                                           ),
174                                 html(html_tag ("td", html_attr ("colspan", "2")),
175                                      html(html_tag ("pre"),
176                                           webgit_object_commit_message_parse (commit)
177                                           ),
178                                      html (html_tag ("hr"))
179                                      )
180                                 )
181                           );
182
183
184 #if 0 /* traverse parents */
185       struct commit *parent;
186       while ((parent = pop_commit (&commit->parents)))
187         {
188         }
189 #endif
190
191       free(commit->buffer);
192       commit->buffer = NULL;
193       free_commit_list(commit->parents);
194       commit->parents = NULL;
195     }
196
197   return html (html( html_tag ("table", html_attr ("id", "log")), table), cont);
198 }
199
200
201 Html
202 webgit_log_content_action (struct webgit_query* query)
203 {
204   const char* object;
205   if (query->object)
206     object = query->object;
207   else if (query->head)
208     object = query->head;
209   else
210     object = "HEAD";
211
212   return webgit_log_table (query, object, query->count, 1);
213 }
214
215 /*
216 //      Local Variables:
217 //      mode: C
218 //      c-file-style: "gnu"
219 //      indent-tabs-mode: nil
220 //      End:
221 */