6588b6ddf90b51cd8cc55dbb729059126ffde99b
[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
85   struct commit *commit;
86   while ((commit = get_revision (&rev)))
87     {
88       if (!count--)
89         {
90           /* displayed query->count lines, finish */
91           html_list_append (cont,
92                             webgit_log_link (
93                                              query,
94                                              query->repo,
95                                              query->head,
96                                              sha1_to_hex(commit->object.sha1), 40,
97                                              query->count_def,
98                                              html("More..."))
99                             );
100           free(commit->buffer);
101           commit->buffer = NULL;
102           free_commit_list(commit->parents);
103           commit->parents = NULL;
104           break;
105         }
106
107       /* TODO: check if commit is one of the heads and display that */
108
109       struct tm tm;
110       webgit_object_commit_committer_date_parse (commit, &tm);
111
112       char pretty_date[256];
113       strftime (pretty_date, 255, "%c", &tm);
114
115       html_list_append (table,
116                         html (
117                               html (
118                                     html_tag ("tr"),
119                                     html(html_tag ("td"),
120                                          html_strndup (pretty_date, 254)
121                                          ),
122                                     html(html_tag ("td"),
123                                          webgit_object_link (query,
124                                                              query->repo,
125                                                              strlen (query->repo),
126                                                              sha1_to_hex (commit->object.sha1),
127                                                              40,
128                                                              NULL,
129                                                              NULL,
130                                                              webgit_object_commit_header_parse (commit)
131                                                              )
132                                          )
133                                     ),
134                               html (
135                                     html_tag ("tr"),
136                                     html(html_tag ("td"),
137                                          webgit_email_link (
138                                                             webgit_object_commit_author_name_parse (commit),
139                                                             webgit_object_commit_author_email_parse (commit)
140                                                             )
141                                          ),
142                                     html (html_tag ("td"),
143                                          html(
144                                               "Diff ",
145                                               " ",
146                                               webgit_object_link (query,
147                                                                   query->repo,
148                                                                   strlen (query->repo),
149                                                                   webgit_object_commit_tree_parse (commit),
150                                                                   40,
151                                                                   NULL,
152                                                                   NULL,
153                                                                   html ("Tree")
154                                                                   )
155                                               )
156                                           )
157                                     )
158                               )
159                         );
160
161       if (msg_flag)
162         html_list_append (table,
163                           html (
164                                 html_tag ("tr"),
165                                 html(html_tag ("td", html_attr ("colspan", "2")),
166                                      html(html_tag ("pre"),
167                                           webgit_object_commit_message_parse (commit)
168                                           ),
169                                      html (html_tag ("hr"))
170                                      )
171                                 )
172                           );
173
174
175 #if 0 /* traverse parents */
176       struct commit *parent;
177       while ((parent = pop_commit (&commit->parents)))
178         {
179         }
180 #endif
181
182       free(commit->buffer);
183       commit->buffer = NULL;
184       free_commit_list(commit->parents);
185       commit->parents = NULL;
186     }
187
188   return html (html( html_tag ("table", html_attr ("id", "log")), table), cont);
189 }
190
191
192 Html
193 webgit_log_content_action (struct webgit_query* query)
194 {
195   const char* object;
196   if (query->object)
197     object = query->object;
198   else if (query->head)
199     object = query->head;
200   else
201     object = "HEAD";
202
203   return webgit_log_table (query, object, query->count, 1);
204 }
205
206 /*
207 //      Local Variables:
208 //      mode: C
209 //      c-file-style: "gnu"
210 //      indent-tabs-mode: nil
211 //      End:
212 */