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