add odd/even class for table coloring
[webgit] / src / branch.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 "branch.h"
22 #include "object.h"
23 #include "age.h"
24 #include "log.h"
25
26 #include <cwa.h>
27
28 #define SHA1_HEADER <openssl/sha.h>
29 #include "git/cache.h"
30 #include "git/commit.h"
31 #include "git/refs.h"
32
33 struct branch_context
34 {
35   struct webgit_repo_info* repo;
36   Html table;
37   Html cont;
38   const char* start;
39   int count;
40   int row;
41 };
42
43 Html
44 webgit_branch_link (struct webgit_repo_info* repo, const char* branch, int count, Html text)
45 {
46   return html (
47                html_tag ("a",
48                          html_attr ("href", html(
49                                                  html_fmt ("%s?repo=%s&action=branch",
50                                                            repo->query->request->script_name,
51                                                            repo->name),
52                                                  branch ? html_fmt("&ref=%s", branch) : html (),
53                                                  count > 0 ? html_fmt("&count=%d",
54                                                                       count) : html ()
55                                                  )
56                                     )
57                          ),
58                text
59                );
60
61 }
62
63 static int
64 show_branch_ref (const char *refname, const unsigned char *sha1, int flags, void *data)
65 {
66   (void) flags;
67
68   struct branch_context* ctx = (struct branch_context*) data;
69   struct webgit_repo_info* repo = ctx->repo;
70
71   if (ctx->start)
72     {
73       if (!strcmp (ctx->start, refname))
74         ctx->start = NULL;
75       else
76         return 0;
77     }
78
79   if (!ctx->count--)
80     {
81       /* displayed query->count lines, finish */
82       html_list_append (ctx->cont,
83                         webgit_branch_link (
84                                             repo, refname,
85                                             repo->query->count,
86                                             html ("More..."))
87                         );
88       return 1;
89     }
90
91   struct commit* commit = lookup_commit_reference (sha1);
92
93   unsigned long date =
94     webgit_object_commit_committer_date_parse (commit, NULL);
95
96   html_list_append (ctx->table,
97                     html (
98                           html_tag ("tr",
99                                     html_attr ("class", (++ctx->row & 1) ? "odd" : "even")
100                                     ),
101                           html (
102                                 html_tag ("td"),
103                                 webgit_log_link (repo->query,
104                                                  repo->name,
105                                                  refname,
106                                                  NULL, 0,
107                                                  repo->query->count,
108                                                  html ((char*)refname)
109                                                  )
110                                 ),
111                           html (
112                                 html_tag ("td"),
113                                 webgit_object_link (repo->query,
114                                                     repo->name, strlen(repo->name),
115                                                     sha1_to_hex (sha1), 40,
116                                                     NULL,
117                                                     NULL,
118                                                     webgit_pretty_age (repo->query->now - date))
119                                 ),
120                           html (
121                                 html_tag ("td"),
122                                 webgit_email_link (
123                                                    webgit_object_commit_committer_name_parse (commit),
124                                                    webgit_object_commit_committer_email_parse (commit)
125                                                    )
126                                 ),
127                           html (
128                                 html_tag ("td"),
129                                 webgit_object_link (repo->query,
130                                                     repo->name, strlen(repo->name),
131                                                     webgit_object_commit_tree_parse (commit), 40,
132                                                     NULL,
133                                                     NULL,
134                                                     html ("Tree")
135                                                     ),
136                                 html (" Snapshot")
137                                 )
138                           )
139                     );
140
141   free(commit->buffer);
142   commit->buffer = NULL;
143   free_commit_list(commit->parents);
144   commit->parents = NULL;
145
146   return 0;
147 }
148
149
150 Html
151 webgit_branch_table (struct webgit_repo_info* repo, const char* head, int count)
152 {
153   struct branch_context ctx;
154   ctx.repo = repo;
155   ctx.table = html_list ();
156   ctx.cont = html_list ();
157   ctx.count = count;
158   ctx.start = head;
159   ctx.row = 0;
160
161   for_each_branch_ref (show_branch_ref, &ctx);
162
163   return html (html( html_tag ("table", html_attr ("id", "branch-context")), ctx.table), ctx.cont);
164 }
165
166 /*
167 //      Local Variables:
168 //      mode: C
169 //      c-file-style: "gnu"
170 //      indent-tabs-mode: nil
171 //      End:
172 */