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