Blame


1 2c251c14 2020-01-15 tracey /*
2 9460dac0 2020-01-15 tracey * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 2c251c14 2020-01-15 tracey * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 2c251c14 2020-01-15 tracey *
5 2c251c14 2020-01-15 tracey * Permission to use, copy, modify, and distribute this software for any
6 2c251c14 2020-01-15 tracey * purpose with or without fee is hereby granted, provided that the above
7 2c251c14 2020-01-15 tracey * copyright notice and this permission notice appear in all copies.
8 2c251c14 2020-01-15 tracey *
9 2c251c14 2020-01-15 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2c251c14 2020-01-15 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2c251c14 2020-01-15 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2c251c14 2020-01-15 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2c251c14 2020-01-15 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2c251c14 2020-01-15 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2c251c14 2020-01-15 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 2c251c14 2020-01-15 tracey */
17 2c251c14 2020-01-15 tracey
18 2c251c14 2020-01-15 tracey #include <sys/queue.h>
19 2c251c14 2020-01-15 tracey #include <sys/stat.h>
20 2c251c14 2020-01-15 tracey #include <sys/types.h>
21 2c251c14 2020-01-15 tracey
22 017d6da3 2020-01-29 tracey #include <ctype.h>
23 2c251c14 2020-01-15 tracey #include <dirent.h>
24 2c251c14 2020-01-15 tracey #include <err.h>
25 c25c2314 2020-01-28 stsp #include <errno.h>
26 474370cb 2020-01-15 tracey #include <regex.h>
27 2c251c14 2020-01-15 tracey #include <stdarg.h>
28 2c251c14 2020-01-15 tracey #include <stdint.h>
29 2c251c14 2020-01-15 tracey #include <stdio.h>
30 2c251c14 2020-01-15 tracey #include <stdlib.h>
31 2c251c14 2020-01-15 tracey #include <string.h>
32 2c251c14 2020-01-15 tracey #include <unistd.h>
33 2c251c14 2020-01-15 tracey
34 2c251c14 2020-01-15 tracey #include <got_object.h>
35 2c251c14 2020-01-15 tracey #include <got_reference.h>
36 2c251c14 2020-01-15 tracey #include <got_repository.h>
37 2c251c14 2020-01-15 tracey #include <got_path.h>
38 2c251c14 2020-01-15 tracey #include <got_cancel.h>
39 2c251c14 2020-01-15 tracey #include <got_worktree.h>
40 2c251c14 2020-01-15 tracey #include <got_diff.h>
41 2c251c14 2020-01-15 tracey #include <got_commit_graph.h>
42 2c251c14 2020-01-15 tracey #include <got_blame.h>
43 2c251c14 2020-01-15 tracey #include <got_privsep.h>
44 2c251c14 2020-01-15 tracey #include <got_opentemp.h>
45 2c251c14 2020-01-15 tracey
46 2c251c14 2020-01-15 tracey #include <kcgi.h>
47 2c251c14 2020-01-15 tracey #include <kcgihtml.h>
48 2c251c14 2020-01-15 tracey
49 2c251c14 2020-01-15 tracey #include "gotweb.h"
50 2c251c14 2020-01-15 tracey
51 2c251c14 2020-01-15 tracey #ifndef nitems
52 2c251c14 2020-01-15 tracey #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 2c251c14 2020-01-15 tracey #endif
54 2c251c14 2020-01-15 tracey
55 54415d85 2020-01-15 tracey struct gw_trans {
56 f2f46662 2020-01-23 tracey TAILQ_HEAD(headers, gw_header) gw_headers;
57 f2f46662 2020-01-23 tracey TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 deac617c 2020-02-14 tracey struct got_repository *repo;
59 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir;
60 2c251c14 2020-01-15 tracey struct gotweb_conf *gw_conf;
61 2c251c14 2020-01-15 tracey struct ktemplate *gw_tmpl;
62 2c251c14 2020-01-15 tracey struct khtmlreq *gw_html_req;
63 2c251c14 2020-01-15 tracey struct kreq *gw_req;
64 f1200fe3 2020-02-13 tracey const struct got_error *error;
65 f652ec0c 2020-02-13 stsp const char *repo_name;
66 2c251c14 2020-01-15 tracey char *repo_path;
67 56997cd3 2020-02-14 tracey char *commit_id;
68 55e46400 2020-02-18 tracey char *next_id;
69 55e46400 2020-02-18 tracey char *next_prev_id;
70 55e46400 2020-02-18 tracey char *prev_id;
71 55e46400 2020-02-18 tracey char *prev_prev_id;
72 4d6abe2b 2020-02-13 stsp const char *repo_file;
73 ec46ccd7 2020-01-15 tracey char *repo_folder;
74 742ba378 2020-02-14 stsp const char *headref;
75 2c251c14 2020-01-15 tracey unsigned int action;
76 2c251c14 2020-01-15 tracey unsigned int page;
77 2c251c14 2020-01-15 tracey unsigned int repos_total;
78 387a29ba 2020-01-15 tracey enum kmime mime;
79 2c251c14 2020-01-15 tracey };
80 2c251c14 2020-01-15 tracey
81 f2f46662 2020-01-23 tracey struct gw_header {
82 f2f46662 2020-01-23 tracey TAILQ_ENTRY(gw_header) entry;
83 f2f46662 2020-01-23 tracey struct got_reflist_head refs;
84 f2f46662 2020-01-23 tracey char *path;
85 f2f46662 2020-01-23 tracey
86 f2f46662 2020-01-23 tracey char *refs_str;
87 f2f46662 2020-01-23 tracey char *commit_id; /* id_str1 */
88 f2f46662 2020-01-23 tracey char *parent_id; /* id_str2 */
89 f2f46662 2020-01-23 tracey char *tree_id;
90 4e8d6e3e 2020-02-14 tracey char *author;
91 4e8d6e3e 2020-02-14 tracey char *committer;
92 f2f46662 2020-01-23 tracey char *commit_msg;
93 f2f46662 2020-01-23 tracey time_t committer_time;
94 2c251c14 2020-01-15 tracey };
95 2c251c14 2020-01-15 tracey
96 2c251c14 2020-01-15 tracey struct gw_dir {
97 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
98 2c251c14 2020-01-15 tracey char *name;
99 2c251c14 2020-01-15 tracey char *owner;
100 2c251c14 2020-01-15 tracey char *description;
101 2c251c14 2020-01-15 tracey char *url;
102 2c251c14 2020-01-15 tracey char *age;
103 2c251c14 2020-01-15 tracey char *path;
104 2c251c14 2020-01-15 tracey };
105 2c251c14 2020-01-15 tracey
106 f2f46662 2020-01-23 tracey enum gw_key {
107 f2f46662 2020-01-23 tracey KEY_ACTION,
108 f2f46662 2020-01-23 tracey KEY_COMMIT_ID,
109 f2f46662 2020-01-23 tracey KEY_FILE,
110 f2f46662 2020-01-23 tracey KEY_FOLDER,
111 f2f46662 2020-01-23 tracey KEY_HEADREF,
112 f2f46662 2020-01-23 tracey KEY_PAGE,
113 f2f46662 2020-01-23 tracey KEY_PATH,
114 55e46400 2020-02-18 tracey KEY_PREV_ID,
115 55e46400 2020-02-18 tracey KEY_PREV_PREV_ID,
116 f2f46662 2020-01-23 tracey KEY__ZMAX
117 f2f46662 2020-01-23 tracey };
118 f2f46662 2020-01-23 tracey
119 54415d85 2020-01-15 tracey enum gw_tmpl {
120 f2f46662 2020-01-23 tracey TEMPL_CONTENT,
121 2c251c14 2020-01-15 tracey TEMPL_HEAD,
122 2c251c14 2020-01-15 tracey TEMPL_HEADER,
123 f2f46662 2020-01-23 tracey TEMPL_SEARCH,
124 2c251c14 2020-01-15 tracey TEMPL_SITEPATH,
125 2c251c14 2020-01-15 tracey TEMPL_SITEOWNER,
126 2c251c14 2020-01-15 tracey TEMPL_TITLE,
127 2c251c14 2020-01-15 tracey TEMPL__MAX
128 2c251c14 2020-01-15 tracey };
129 2c251c14 2020-01-15 tracey
130 54415d85 2020-01-15 tracey enum gw_ref_tm {
131 387a29ba 2020-01-15 tracey TM_DIFF,
132 387a29ba 2020-01-15 tracey TM_LONG,
133 387a29ba 2020-01-15 tracey };
134 387a29ba 2020-01-15 tracey
135 54415d85 2020-01-15 tracey enum gw_tags {
136 87f9ebf5 2020-01-15 tracey TAGBRIEF,
137 87f9ebf5 2020-01-15 tracey TAGFULL,
138 87f9ebf5 2020-01-15 tracey };
139 87f9ebf5 2020-01-15 tracey
140 54415d85 2020-01-15 tracey static const char *const gw_templs[TEMPL__MAX] = {
141 f2f46662 2020-01-23 tracey "content",
142 2c251c14 2020-01-15 tracey "head",
143 2c251c14 2020-01-15 tracey "header",
144 f2f46662 2020-01-23 tracey "search",
145 2c251c14 2020-01-15 tracey "sitepath",
146 2c251c14 2020-01-15 tracey "siteowner",
147 2c251c14 2020-01-15 tracey "title",
148 2c251c14 2020-01-15 tracey };
149 2c251c14 2020-01-15 tracey
150 ec46ccd7 2020-01-15 tracey static const struct kvalid gw_keys[KEY__ZMAX] = {
151 2c251c14 2020-01-15 tracey { kvalid_stringne, "action" },
152 2c251c14 2020-01-15 tracey { kvalid_stringne, "commit" },
153 2c251c14 2020-01-15 tracey { kvalid_stringne, "file" },
154 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "folder" },
155 8087c3c5 2020-01-15 tracey { kvalid_stringne, "headref" },
156 ec46ccd7 2020-01-15 tracey { kvalid_int, "page" },
157 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "path" },
158 55e46400 2020-02-18 tracey { kvalid_stringne, "prev" },
159 55e46400 2020-02-18 tracey { kvalid_stringne, "prev_prev" },
160 2c251c14 2020-01-15 tracey };
161 2c251c14 2020-01-15 tracey
162 f2f46662 2020-01-23 tracey static struct gw_header *gw_init_header(void);
163 2c251c14 2020-01-15 tracey
164 b0a1bc86 2020-02-14 stsp static void gw_free_header(struct gw_header *);
165 00f13350 2020-02-10 tracey
166 00f13350 2020-02-10 tracey static int gw_template(size_t, void *);
167 00f13350 2020-02-10 tracey
168 f1200fe3 2020-02-13 tracey static const struct got_error *gw_error(struct gw_trans *);
169 f1200fe3 2020-02-13 tracey static const struct got_error *gw_init_gw_dir(struct gw_dir **, const char *);
170 185ba3ba 2020-02-04 tracey static const struct got_error *gw_get_repo_description(char **,
171 185ba3ba 2020-02-04 tracey struct gw_trans *, char *);
172 9a1cc63f 2020-02-03 stsp static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
173 2c251c14 2020-01-15 tracey char *);
174 55ccf528 2020-02-03 stsp static const struct got_error *gw_get_time_str(char **, time_t, int);
175 d126c1b7 2020-01-29 stsp static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
176 2011c142 2020-02-14 stsp char *, const char *, int);
177 9a9d12ca 2020-02-06 tracey static const struct got_error *gw_output_file_blame(struct gw_trans *);
178 e0857cfe 2020-02-06 tracey static const struct got_error *gw_output_blob_buf(struct gw_trans *);
179 626b25b6 2020-02-06 tracey static const struct got_error *gw_output_repo_tree(struct gw_trans *);
180 38b240fb 2020-02-06 tracey static const struct got_error *gw_output_diff(struct gw_trans *,
181 f2f46662 2020-01-23 tracey struct gw_header *);
182 38b240fb 2020-02-06 tracey static const struct got_error *gw_output_repo_tags(struct gw_trans *,
183 4ff7391f 2020-01-28 tracey struct gw_header *, int, int);
184 9eed6f10 2020-02-08 tracey static const struct got_error *gw_output_repo_heads(struct gw_trans *);
185 4ae179a2 2020-02-08 tracey static const struct got_error *gw_output_site_link(struct gw_trans *);
186 185ba3ba 2020-02-04 tracey static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
187 185ba3ba 2020-02-04 tracey char *);
188 f7cc9480 2020-02-05 tracey static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
189 2c251c14 2020-01-15 tracey
190 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
191 21a55cc7 2020-02-04 tracey char*);
192 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
193 f7cc9480 2020-02-05 tracey char*);
194 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_author_header(struct gw_trans *,
195 21a55cc7 2020-02-04 tracey const char *);
196 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_age_header(struct gw_trans *,
197 21a55cc7 2020-02-04 tracey const char *);
198 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_committer_header(struct gw_trans *,
199 21a55cc7 2020-02-04 tracey const char *);
200 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
201 f7cc9480 2020-02-05 tracey char *);
202 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
203 00f13350 2020-02-10 tracey static const struct got_error *gw_display_open(struct gw_trans *, enum khttp,
204 2c251c14 2020-01-15 tracey enum kmime);
205 00f13350 2020-02-10 tracey static const struct got_error *gw_display_index(struct gw_trans *);
206 00f13350 2020-02-10 tracey static const struct got_error *gw_get_header(struct gw_trans *,
207 f2f46662 2020-01-23 tracey struct gw_header *, int);
208 00f13350 2020-02-10 tracey static const struct got_error *gw_get_commits(struct gw_trans *,
209 3c245a0e 2020-02-14 tracey struct gw_header *, int,
210 3c245a0e 2020-02-14 tracey struct got_object_id *);
211 00f13350 2020-02-10 tracey static const struct got_error *gw_get_commit(struct gw_trans *,
212 4e8d6e3e 2020-02-14 tracey struct gw_header *,
213 4e8d6e3e 2020-02-14 tracey struct got_commit_object *,
214 3c245a0e 2020-02-14 tracey struct got_object_id *);
215 d4159696 2020-02-13 stsp static const struct got_error *gw_apply_unveil(const char *);
216 00f13350 2020-02-10 tracey static const struct got_error *gw_blame_cb(void *, int, int,
217 ec46ccd7 2020-01-15 tracey struct got_object_id *);
218 00f13350 2020-02-10 tracey static const struct got_error *gw_load_got_paths(struct gw_trans *);
219 00f13350 2020-02-10 tracey static const struct got_error *gw_load_got_path(struct gw_trans *,
220 2c251c14 2020-01-15 tracey struct gw_dir *);
221 00f13350 2020-02-10 tracey static const struct got_error *gw_parse_querystring(struct gw_trans *);
222 00f13350 2020-02-10 tracey static const struct got_error *gw_blame(struct gw_trans *);
223 00f13350 2020-02-10 tracey static const struct got_error *gw_blob(struct gw_trans *);
224 00f13350 2020-02-10 tracey static const struct got_error *gw_diff(struct gw_trans *);
225 00f13350 2020-02-10 tracey static const struct got_error *gw_index(struct gw_trans *);
226 00f13350 2020-02-10 tracey static const struct got_error *gw_commits(struct gw_trans *);
227 00f13350 2020-02-10 tracey static const struct got_error *gw_briefs(struct gw_trans *);
228 00f13350 2020-02-10 tracey static const struct got_error *gw_summary(struct gw_trans *);
229 00f13350 2020-02-10 tracey static const struct got_error *gw_tree(struct gw_trans *);
230 00f13350 2020-02-10 tracey static const struct got_error *gw_tag(struct gw_trans *);
231 2c251c14 2020-01-15 tracey
232 2c251c14 2020-01-15 tracey struct gw_query_action {
233 2c251c14 2020-01-15 tracey unsigned int func_id;
234 2c251c14 2020-01-15 tracey const char *func_name;
235 54415d85 2020-01-15 tracey const struct got_error *(*func_main)(struct gw_trans *);
236 2c251c14 2020-01-15 tracey char *template;
237 2c251c14 2020-01-15 tracey };
238 2c251c14 2020-01-15 tracey
239 2c251c14 2020-01-15 tracey enum gw_query_actions {
240 2c251c14 2020-01-15 tracey GW_BLAME,
241 e46f587c 2020-01-29 tracey GW_BLOB,
242 f2f46662 2020-01-23 tracey GW_BRIEFS,
243 f2f46662 2020-01-23 tracey GW_COMMITS,
244 f2f46662 2020-01-23 tracey GW_DIFF,
245 2c251c14 2020-01-15 tracey GW_ERR,
246 2c251c14 2020-01-15 tracey GW_INDEX,
247 2c251c14 2020-01-15 tracey GW_SUMMARY,
248 4ff7391f 2020-01-28 tracey GW_TAG,
249 b772de24 2020-01-15 tracey GW_TREE,
250 2c251c14 2020-01-15 tracey };
251 2c251c14 2020-01-15 tracey
252 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
253 f2f46662 2020-01-23 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
254 017d6da3 2020-01-29 tracey { GW_BLOB, "blob", NULL, NULL },
255 f2f46662 2020-01-23 tracey { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
256 f2f46662 2020-01-23 tracey { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
257 f2f46662 2020-01-23 tracey { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
258 f1200fe3 2020-02-13 tracey { GW_ERR, "error", gw_error, "gw_tmpl/err.tmpl" },
259 f2f46662 2020-01-23 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
260 f2f46662 2020-01-23 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
261 4ff7391f 2020-01-28 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
262 f2f46662 2020-01-23 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
263 2c251c14 2020-01-15 tracey };
264 5d1296de 2020-02-13 stsp
265 5d1296de 2020-02-13 stsp static const char *
266 5d1296de 2020-02-13 stsp gw_get_action_name(struct gw_trans *gw_trans)
267 5d1296de 2020-02-13 stsp {
268 5d1296de 2020-02-13 stsp return gw_query_funcs[gw_trans->action].func_name;
269 5d1296de 2020-02-13 stsp }
270 c25c2314 2020-01-28 stsp
271 c25c2314 2020-01-28 stsp static const struct got_error *
272 c25c2314 2020-01-28 stsp gw_kcgi_error(enum kcgi_err kerr)
273 c25c2314 2020-01-28 stsp {
274 c25c2314 2020-01-28 stsp if (kerr == KCGI_OK)
275 c25c2314 2020-01-28 stsp return NULL;
276 c25c2314 2020-01-28 stsp
277 c25c2314 2020-01-28 stsp if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
278 c25c2314 2020-01-28 stsp return got_error(GOT_ERR_CANCELLED);
279 c25c2314 2020-01-28 stsp
280 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENOMEM)
281 f7cc9480 2020-02-05 tracey return got_error_set_errno(ENOMEM,
282 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
283 2c251c14 2020-01-15 tracey
284 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENFILE)
285 f7cc9480 2020-02-05 tracey return got_error_set_errno(ENFILE,
286 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
287 c25c2314 2020-01-28 stsp
288 c25c2314 2020-01-28 stsp if (kerr == KCGI_EAGAIN)
289 f7cc9480 2020-02-05 tracey return got_error_set_errno(EAGAIN,
290 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
291 c25c2314 2020-01-28 stsp
292 c25c2314 2020-01-28 stsp if (kerr == KCGI_FORM)
293 f7cc9480 2020-02-05 tracey return got_error_msg(GOT_ERR_IO,
294 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
295 c25c2314 2020-01-28 stsp
296 7afec891 2020-02-06 stsp return got_error_from_errno(kcgi_strerror(kerr));
297 c25c2314 2020-01-28 stsp }
298 c25c2314 2020-01-28 stsp
299 2c251c14 2020-01-15 tracey static const struct got_error *
300 d4159696 2020-02-13 stsp gw_apply_unveil(const char *repo_path)
301 2c251c14 2020-01-15 tracey {
302 2c251c14 2020-01-15 tracey const struct got_error *err;
303 2c251c14 2020-01-15 tracey
304 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
305 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
306 2c251c14 2020-01-15 tracey
307 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
308 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 2c251c14 2020-01-15 tracey
310 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
311 2c251c14 2020-01-15 tracey if (err != NULL)
312 2c251c14 2020-01-15 tracey return err;
313 2c251c14 2020-01-15 tracey
314 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
315 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
316 65b95fb2 2020-01-15 tracey
317 32cd4d18 2020-02-03 stsp return NULL;
318 5894d523 2020-02-03 stsp }
319 5894d523 2020-02-03 stsp
320 5894d523 2020-02-03 stsp static int
321 e0857cfe 2020-02-06 tracey isbinary(const uint8_t *buf, size_t n)
322 5894d523 2020-02-03 stsp {
323 e0857cfe 2020-02-06 tracey size_t i;
324 e0857cfe 2020-02-06 tracey
325 e0857cfe 2020-02-06 tracey for (i = 0; i < n; i++)
326 e0857cfe 2020-02-06 tracey if (buf[i] == 0)
327 e0857cfe 2020-02-06 tracey return 1;
328 e0857cfe 2020-02-06 tracey return 0;
329 32cd4d18 2020-02-03 stsp }
330 5894d523 2020-02-03 stsp
331 32cd4d18 2020-02-03 stsp static const struct got_error *
332 54415d85 2020-01-15 tracey gw_blame(struct gw_trans *gw_trans)
333 2c251c14 2020-01-15 tracey {
334 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
335 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
336 78a9dab5 2020-02-07 tracey char *age = NULL;
337 1b33afc0 2020-02-14 tracey enum kcgi_err kerr = KCGI_OK;
338 2c251c14 2020-01-15 tracey
339 6bee13de 2020-01-16 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
340 f2f46662 2020-01-23 tracey NULL) == -1)
341 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
342 6bee13de 2020-01-16 tracey
343 f2f46662 2020-01-23 tracey if ((header = gw_init_header()) == NULL)
344 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
345 f2f46662 2020-01-23 tracey
346 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
347 ec46ccd7 2020-01-15 tracey if (error)
348 5ddf0079 2020-01-29 stsp goto done;
349 ec46ccd7 2020-01-15 tracey
350 1b33afc0 2020-02-14 tracey /* check querystring */
351 1b33afc0 2020-02-14 tracey if (gw_trans->repo_file == NULL) {
352 1b33afc0 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
353 1b33afc0 2020-02-14 tracey "file required in querystring");
354 1b33afc0 2020-02-14 tracey goto done;
355 1b33afc0 2020-02-14 tracey }
356 1b33afc0 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
357 1b33afc0 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
358 1b33afc0 2020-02-14 tracey "commit required in querystring");
359 1b33afc0 2020-02-14 tracey goto done;
360 1b33afc0 2020-02-14 tracey }
361 1b33afc0 2020-02-14 tracey
362 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
363 f2f46662 2020-01-23 tracey if (error)
364 5ddf0079 2020-01-29 stsp goto done;
365 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
366 9a9d12ca 2020-02-06 tracey "blame_header_wrapper", KATTR__MAX);
367 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
368 9a9d12ca 2020-02-06 tracey goto done;
369 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
370 9a9d12ca 2020-02-06 tracey "blame_header", KATTR__MAX);
371 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
372 9a9d12ca 2020-02-06 tracey goto done;
373 9a9d12ca 2020-02-06 tracey error = gw_get_time_str(&age, header->committer_time,
374 9a9d12ca 2020-02-06 tracey TM_LONG);
375 a81f3554 2020-02-03 stsp if (error)
376 a81f3554 2020-02-03 stsp goto done;
377 9a9d12ca 2020-02-06 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
378 55ccf528 2020-02-03 stsp if (error)
379 55ccf528 2020-02-03 stsp goto done;
380 9a9d12ca 2020-02-06 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
381 9a9d12ca 2020-02-06 tracey if (error)
382 6d9fc692 2020-01-28 stsp goto done;
383 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
384 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
385 6d9fc692 2020-01-28 stsp goto done;
386 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
387 9a9d12ca 2020-02-06 tracey "dotted_line", KATTR__MAX);
388 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
389 9a9d12ca 2020-02-06 tracey goto done;
390 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
391 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
392 9a9d12ca 2020-02-06 tracey goto done;
393 9a9d12ca 2020-02-06 tracey
394 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
395 9a9d12ca 2020-02-06 tracey "blame", KATTR__MAX);
396 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
397 9a9d12ca 2020-02-06 tracey goto done;
398 9a9d12ca 2020-02-06 tracey error = gw_output_file_blame(gw_trans);
399 9a9d12ca 2020-02-06 tracey if (error)
400 9a9d12ca 2020-02-06 tracey goto done;
401 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
402 6d9fc692 2020-01-28 stsp done:
403 b0a1bc86 2020-02-14 stsp gw_free_header(header);
404 9a9d12ca 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
405 9a9d12ca 2020-02-06 tracey error = gw_kcgi_error(kerr);
406 2c251c14 2020-01-15 tracey return error;
407 2c251c14 2020-01-15 tracey }
408 2c251c14 2020-01-15 tracey
409 2c251c14 2020-01-15 tracey static const struct got_error *
410 e46f587c 2020-01-29 tracey gw_blob(struct gw_trans *gw_trans)
411 e46f587c 2020-01-29 tracey {
412 3b7fdcf4 2020-02-14 tracey const struct got_error *error = NULL, *err = NULL;
413 e46f587c 2020-01-29 tracey struct gw_header *header = NULL;
414 3b7fdcf4 2020-02-14 tracey enum kcgi_err kerr = KCGI_OK;
415 e46f587c 2020-01-29 tracey
416 e46f587c 2020-01-29 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
417 e46f587c 2020-01-29 tracey NULL) == -1)
418 e46f587c 2020-01-29 tracey return got_error_from_errno("pledge");
419 e46f587c 2020-01-29 tracey
420 e46f587c 2020-01-29 tracey if ((header = gw_init_header()) == NULL)
421 e46f587c 2020-01-29 tracey return got_error_from_errno("malloc");
422 e46f587c 2020-01-29 tracey
423 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
424 e46f587c 2020-01-29 tracey if (error)
425 e46f587c 2020-01-29 tracey goto done;
426 e46f587c 2020-01-29 tracey
427 3b7fdcf4 2020-02-14 tracey /* check querystring */
428 3b7fdcf4 2020-02-14 tracey if (gw_trans->repo_file == NULL) {
429 3b7fdcf4 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
430 3b7fdcf4 2020-02-14 tracey "file required in querystring");
431 3b7fdcf4 2020-02-14 tracey goto done;
432 3b7fdcf4 2020-02-14 tracey }
433 3b7fdcf4 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
434 3b7fdcf4 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
435 3b7fdcf4 2020-02-14 tracey "commit required in querystring");
436 3b7fdcf4 2020-02-14 tracey goto done;
437 3b7fdcf4 2020-02-14 tracey }
438 e46f587c 2020-01-29 tracey error = gw_get_header(gw_trans, header, 1);
439 e46f587c 2020-01-29 tracey if (error)
440 e46f587c 2020-01-29 tracey goto done;
441 e46f587c 2020-01-29 tracey
442 e0857cfe 2020-02-06 tracey error = gw_output_blob_buf(gw_trans);
443 e46f587c 2020-01-29 tracey done:
444 3b7fdcf4 2020-02-14 tracey
445 3b7fdcf4 2020-02-14 tracey if (error) {
446 3b7fdcf4 2020-02-14 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
447 3b7fdcf4 2020-02-14 tracey err = gw_display_index(gw_trans);
448 3b7fdcf4 2020-02-14 tracey if (err) {
449 3b7fdcf4 2020-02-14 tracey error = err;
450 3b7fdcf4 2020-02-14 tracey goto errored;
451 3b7fdcf4 2020-02-14 tracey }
452 3b7fdcf4 2020-02-14 tracey kerr = khttp_puts(gw_trans->gw_req, error->msg);
453 3b7fdcf4 2020-02-14 tracey }
454 3b7fdcf4 2020-02-14 tracey errored:
455 b0a1bc86 2020-02-14 stsp gw_free_header(header);
456 3b7fdcf4 2020-02-14 tracey if (error == NULL && kerr != KCGI_OK)
457 3b7fdcf4 2020-02-14 tracey error = gw_kcgi_error(kerr);
458 e46f587c 2020-01-29 tracey return error;
459 e46f587c 2020-01-29 tracey }
460 e46f587c 2020-01-29 tracey
461 e46f587c 2020-01-29 tracey static const struct got_error *
462 f2f46662 2020-01-23 tracey gw_diff(struct gw_trans *gw_trans)
463 2c251c14 2020-01-15 tracey {
464 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
465 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
466 78a9dab5 2020-02-07 tracey char *age = NULL;
467 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
468 2c251c14 2020-01-15 tracey
469 f2f46662 2020-01-23 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
470 f2f46662 2020-01-23 tracey NULL) == -1)
471 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
472 6bee13de 2020-01-16 tracey
473 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
474 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
475 f2f46662 2020-01-23 tracey
476 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
477 8087c3c5 2020-01-15 tracey if (error)
478 390d412c 2020-01-28 stsp goto done;
479 8087c3c5 2020-01-15 tracey
480 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
481 f2f46662 2020-01-23 tracey if (error)
482 55ccf528 2020-02-03 stsp goto done;
483 f7cc9480 2020-02-05 tracey
484 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
485 f7cc9480 2020-02-05 tracey "diff_header_wrapper", KATTR__MAX);
486 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
487 f7cc9480 2020-02-05 tracey goto done;
488 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
489 f7cc9480 2020-02-05 tracey "diff_header", KATTR__MAX);
490 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
491 f7cc9480 2020-02-05 tracey goto done;
492 f7cc9480 2020-02-05 tracey error = gw_gen_diff_header(gw_trans, header->parent_id,
493 f7cc9480 2020-02-05 tracey header->commit_id);
494 f7cc9480 2020-02-05 tracey if (error)
495 f7cc9480 2020-02-05 tracey goto done;
496 f7cc9480 2020-02-05 tracey error = gw_gen_commit_header(gw_trans, header->commit_id,
497 f7cc9480 2020-02-05 tracey header->refs_str);
498 f7cc9480 2020-02-05 tracey if (error)
499 f7cc9480 2020-02-05 tracey goto done;
500 f7cc9480 2020-02-05 tracey error = gw_gen_tree_header(gw_trans, header->tree_id);
501 f7cc9480 2020-02-05 tracey if (error)
502 f7cc9480 2020-02-05 tracey goto done;
503 f7cc9480 2020-02-05 tracey error = gw_gen_author_header(gw_trans, header->author);
504 f7cc9480 2020-02-05 tracey if (error)
505 f7cc9480 2020-02-05 tracey goto done;
506 f7cc9480 2020-02-05 tracey error = gw_gen_committer_header(gw_trans, header->author);
507 f7cc9480 2020-02-05 tracey if (error)
508 f7cc9480 2020-02-05 tracey goto done;
509 f7cc9480 2020-02-05 tracey error = gw_get_time_str(&age, header->committer_time,
510 f7cc9480 2020-02-05 tracey TM_LONG);
511 f7cc9480 2020-02-05 tracey if (error)
512 f7cc9480 2020-02-05 tracey goto done;
513 f7cc9480 2020-02-05 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
514 070fee27 2020-02-03 stsp if (error)
515 070fee27 2020-02-03 stsp goto done;
516 f7cc9480 2020-02-05 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
517 f7cc9480 2020-02-05 tracey if (error)
518 390d412c 2020-01-28 stsp goto done;
519 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
520 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
521 390d412c 2020-01-28 stsp goto done;
522 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
523 f7cc9480 2020-02-05 tracey "dotted_line", KATTR__MAX);
524 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
525 f7cc9480 2020-02-05 tracey goto done;
526 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
527 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
528 f7cc9480 2020-02-05 tracey goto done;
529 87f9ebf5 2020-01-15 tracey
530 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
531 f7cc9480 2020-02-05 tracey "diff", KATTR__MAX);
532 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
533 f7cc9480 2020-02-05 tracey goto done;
534 38b240fb 2020-02-06 tracey error = gw_output_diff(gw_trans, header);
535 f7cc9480 2020-02-05 tracey if (error)
536 f7cc9480 2020-02-05 tracey goto done;
537 f7cc9480 2020-02-05 tracey
538 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
539 390d412c 2020-01-28 stsp done:
540 b0a1bc86 2020-02-14 stsp gw_free_header(header);
541 55ccf528 2020-02-03 stsp free(age);
542 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
543 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
544 2204c934 2020-01-15 tracey return error;
545 2204c934 2020-01-15 tracey }
546 2204c934 2020-01-15 tracey
547 2204c934 2020-01-15 tracey static const struct got_error *
548 54415d85 2020-01-15 tracey gw_index(struct gw_trans *gw_trans)
549 2c251c14 2020-01-15 tracey {
550 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
551 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
552 b3f1f953 2020-02-09 tracey char *href_next = NULL, *href_prev = NULL, *href_summary = NULL;
553 b3f1f953 2020-02-09 tracey char *href_briefs = NULL, *href_commits = NULL, *href_tree = NULL;
554 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
555 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
556 2c251c14 2020-01-15 tracey
557 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
558 6bee13de 2020-01-16 tracey NULL) == -1) {
559 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
560 6bee13de 2020-01-16 tracey return error;
561 6bee13de 2020-01-16 tracey }
562 6bee13de 2020-01-16 tracey
563 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path);
564 46b9c89b 2020-01-15 tracey if (error)
565 46b9c89b 2020-01-15 tracey return error;
566 46b9c89b 2020-01-15 tracey
567 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
568 46b9c89b 2020-01-15 tracey if (error)
569 2c251c14 2020-01-15 tracey return error;
570 2c251c14 2020-01-15 tracey
571 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
572 b3f1f953 2020-02-09 tracey "index_header", KATTR__MAX);
573 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
574 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
575 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
576 b3f1f953 2020-02-09 tracey "index_header_project", KATTR__MAX);
577 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
578 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
579 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Project");
580 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
581 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
582 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
583 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
584 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
585 2c251c14 2020-01-15 tracey
586 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_description) {
587 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
588 b3f1f953 2020-02-09 tracey "index_header_description", KATTR__MAX);
589 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
590 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
591 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Description");
592 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
593 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
594 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
595 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
596 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
597 b3f1f953 2020-02-09 tracey }
598 b3f1f953 2020-02-09 tracey
599 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
600 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
601 b3f1f953 2020-02-09 tracey "index_header_owner", KATTR__MAX);
602 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
603 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
604 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Owner");
605 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
606 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
607 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
608 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
609 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
610 b3f1f953 2020-02-09 tracey }
611 b3f1f953 2020-02-09 tracey
612 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_age) {
613 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
614 b3f1f953 2020-02-09 tracey "index_header_age", KATTR__MAX);
615 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
616 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
617 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Last Change");
618 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
619 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
620 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
621 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
622 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
623 b3f1f953 2020-02-09 tracey }
624 b3f1f953 2020-02-09 tracey
625 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
626 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
627 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
628 b3f1f953 2020-02-09 tracey
629 b3f1f953 2020-02-09 tracey if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
630 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
631 b3f1f953 2020-02-09 tracey "index_wrapper", KATTR__MAX);
632 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
633 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
634 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
635 b3f1f953 2020-02-09 tracey "No repositories found in ");
636 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
637 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
638 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
639 b3f1f953 2020-02-09 tracey gw_trans->gw_conf->got_repos_path);
640 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
641 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
642 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
643 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
644 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
645 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
646 b3f1f953 2020-02-09 tracey "dotted_line", KATTR__MAX);
647 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
648 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
649 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
650 b3f1f953 2020-02-09 tracey if (kerr != KCGI_OK)
651 b3f1f953 2020-02-09 tracey return gw_kcgi_error(kerr);
652 c25c2314 2020-01-28 stsp return error;
653 bce5dac1 2020-01-28 stsp }
654 bce5dac1 2020-01-28 stsp
655 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
656 2c251c14 2020-01-15 tracey dir_c++;
657 2c251c14 2020-01-15 tracey
658 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
659 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
660 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
661 2c251c14 2020-01-15 tracey prev_disp++;
662 2c251c14 2020-01-15 tracey continue;
663 2c251c14 2020-01-15 tracey }
664 2c251c14 2020-01-15 tracey
665 2c251c14 2020-01-15 tracey prev_disp++;
666 f2f46662 2020-01-23 tracey
667 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
668 b3f1f953 2020-02-09 tracey "index_wrapper", KATTR__MAX);
669 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
670 a02d5f81 2020-02-13 stsp goto done;
671 b3f1f953 2020-02-09 tracey
672 b3f1f953 2020-02-09 tracey if (asprintf(&href_summary, "?path=%s&action=summary",
673 a02d5f81 2020-02-13 stsp gw_dir->name) == -1) {
674 a02d5f81 2020-02-13 stsp error = got_error_from_errno("asprintf");
675 a02d5f81 2020-02-13 stsp goto done;
676 a02d5f81 2020-02-13 stsp }
677 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
678 b3f1f953 2020-02-09 tracey "index_project", KATTR__MAX);
679 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
680 b3f1f953 2020-02-09 tracey goto done;
681 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
682 b3f1f953 2020-02-09 tracey href_summary, KATTR__MAX);
683 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
684 b3f1f953 2020-02-09 tracey goto done;
685 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_dir->name);
686 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
687 b3f1f953 2020-02-09 tracey goto done;
688 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
689 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
690 b3f1f953 2020-02-09 tracey goto done;
691 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_description) {
692 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
693 b3f1f953 2020-02-09 tracey KATTR_ID, "index_project_description", KATTR__MAX);
694 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
695 b3f1f953 2020-02-09 tracey goto done;
696 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
697 b3f1f953 2020-02-09 tracey gw_dir->description ? gw_dir->description : "");
698 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
699 b3f1f953 2020-02-09 tracey goto done;
700 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
701 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
702 b3f1f953 2020-02-09 tracey goto done;
703 b3f1f953 2020-02-09 tracey }
704 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
705 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
706 b3f1f953 2020-02-09 tracey KATTR_ID, "index_project_owner", KATTR__MAX);
707 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
708 b3f1f953 2020-02-09 tracey goto done;
709 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
710 b3f1f953 2020-02-09 tracey gw_dir->owner ? gw_dir->owner : "");
711 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
712 b3f1f953 2020-02-09 tracey goto done;
713 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
714 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
715 b3f1f953 2020-02-09 tracey goto done;
716 b3f1f953 2020-02-09 tracey }
717 b3f1f953 2020-02-09 tracey if (gw_trans->gw_conf->got_show_repo_age) {
718 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
719 b3f1f953 2020-02-09 tracey KATTR_ID, "index_project_age", KATTR__MAX);
720 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
721 b3f1f953 2020-02-09 tracey goto done;
722 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req,
723 b3f1f953 2020-02-09 tracey gw_dir->age ? gw_dir->age : "");
724 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
725 b3f1f953 2020-02-09 tracey goto done;
726 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
727 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
728 b3f1f953 2020-02-09 tracey goto done;
729 b3f1f953 2020-02-09 tracey }
730 b3f1f953 2020-02-09 tracey
731 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
732 b3f1f953 2020-02-09 tracey "navs_wrapper", KATTR__MAX);
733 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
734 b3f1f953 2020-02-09 tracey goto done;
735 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
736 b3f1f953 2020-02-09 tracey "navs", KATTR__MAX);
737 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
738 b3f1f953 2020-02-09 tracey goto done;
739 b3f1f953 2020-02-09 tracey
740 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
741 b3f1f953 2020-02-09 tracey href_summary, KATTR__MAX);
742 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
743 b3f1f953 2020-02-09 tracey goto done;
744 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "summary");
745 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
746 b3f1f953 2020-02-09 tracey goto done;
747 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
748 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
749 b3f1f953 2020-02-09 tracey goto done;
750 2c251c14 2020-01-15 tracey
751 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
752 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
753 b3f1f953 2020-02-09 tracey goto done;
754 b3f1f953 2020-02-09 tracey
755 b3f1f953 2020-02-09 tracey if (asprintf(&href_briefs, "?path=%s&action=briefs",
756 b3f1f953 2020-02-09 tracey gw_dir->name) == -1) {
757 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
758 b3f1f953 2020-02-09 tracey goto done;
759 b3f1f953 2020-02-09 tracey }
760 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
761 b3f1f953 2020-02-09 tracey href_briefs, KATTR__MAX);
762 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
763 b3f1f953 2020-02-09 tracey goto done;
764 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
765 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
766 b3f1f953 2020-02-09 tracey goto done;
767 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
768 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
769 b3f1f953 2020-02-09 tracey error = gw_kcgi_error(kerr);
770 b3f1f953 2020-02-09 tracey
771 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
772 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
773 b3f1f953 2020-02-09 tracey goto done;
774 b3f1f953 2020-02-09 tracey
775 b3f1f953 2020-02-09 tracey if (asprintf(&href_commits, "?path=%s&action=commits",
776 b3f1f953 2020-02-09 tracey gw_dir->name) == -1) {
777 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
778 b3f1f953 2020-02-09 tracey goto done;
779 b3f1f953 2020-02-09 tracey }
780 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
781 b3f1f953 2020-02-09 tracey href_commits, KATTR__MAX);
782 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
783 b3f1f953 2020-02-09 tracey goto done;
784 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commits");
785 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
786 b3f1f953 2020-02-09 tracey goto done;
787 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
788 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
789 b3f1f953 2020-02-09 tracey goto done;
790 b3f1f953 2020-02-09 tracey
791 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
792 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
793 b3f1f953 2020-02-09 tracey goto done;
794 b3f1f953 2020-02-09 tracey
795 b3f1f953 2020-02-09 tracey if (asprintf(&href_tree, "?path=%s&action=tree",
796 b3f1f953 2020-02-09 tracey gw_dir->name) == -1) {
797 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
798 b3f1f953 2020-02-09 tracey goto done;
799 b3f1f953 2020-02-09 tracey }
800 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
801 b3f1f953 2020-02-09 tracey href_tree, KATTR__MAX);
802 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
803 b3f1f953 2020-02-09 tracey goto done;
804 b3f1f953 2020-02-09 tracey kerr = khtml_puts(gw_trans->gw_html_req, "tree");
805 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
806 b3f1f953 2020-02-09 tracey goto done;
807 b3f1f953 2020-02-09 tracey
808 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
809 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
810 b3f1f953 2020-02-09 tracey goto done;
811 b3f1f953 2020-02-09 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
812 b3f1f953 2020-02-09 tracey "dotted_line", KATTR__MAX);
813 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
814 b3f1f953 2020-02-09 tracey goto done;
815 b3f1f953 2020-02-09 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
816 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
817 b3f1f953 2020-02-09 tracey goto done;
818 b3f1f953 2020-02-09 tracey
819 b3f1f953 2020-02-09 tracey free(href_summary);
820 b3f1f953 2020-02-09 tracey href_summary = NULL;
821 b3f1f953 2020-02-09 tracey free(href_briefs);
822 b3f1f953 2020-02-09 tracey href_briefs = NULL;
823 b3f1f953 2020-02-09 tracey free(href_commits);
824 b3f1f953 2020-02-09 tracey href_commits = NULL;
825 b3f1f953 2020-02-09 tracey free(href_tree);
826 b3f1f953 2020-02-09 tracey href_tree = NULL;
827 b3f1f953 2020-02-09 tracey
828 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
829 2c251c14 2020-01-15 tracey continue;
830 2c251c14 2020-01-15 tracey
831 c25c2314 2020-01-28 stsp if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
832 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
833 63ee0dca 2020-02-08 tracey KATTR_ID, "np_wrapper", KATTR__MAX);
834 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
835 b3f1f953 2020-02-09 tracey goto done;
836 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
837 63ee0dca 2020-02-08 tracey KATTR_ID, "nav_prev", KATTR__MAX);
838 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
839 b3f1f953 2020-02-09 tracey goto done;
840 c25c2314 2020-01-28 stsp } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
841 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
842 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
843 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
844 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
845 63ee0dca 2020-02-08 tracey KATTR_ID, "np_wrapper", KATTR__MAX);
846 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
847 b3f1f953 2020-02-09 tracey goto done;
848 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
849 63ee0dca 2020-02-08 tracey KATTR_ID, "nav_prev", KATTR__MAX);
850 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
851 b3f1f953 2020-02-09 tracey goto done;
852 c25c2314 2020-01-28 stsp }
853 2c251c14 2020-01-15 tracey
854 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
855 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
856 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
857 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
858 63ee0dca 2020-02-08 tracey if (asprintf(&href_prev, "?page=%d",
859 b3f1f953 2020-02-09 tracey gw_trans->page - 1) == -1) {
860 b3f1f953 2020-02-09 tracey error = got_error_from_errno("asprintf");
861 b3f1f953 2020-02-09 tracey goto done;
862 b3f1f953 2020-02-09 tracey }
863 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
864 63ee0dca 2020-02-08 tracey KATTR_HREF, href_prev, KATTR__MAX);
865 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
866 565d9dcb 2020-02-09 tracey goto done;
867 63ee0dca 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
868 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
869 b3f1f953 2020-02-09 tracey goto done;
870 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
871 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
872 b3f1f953 2020-02-09 tracey goto done;
873 2c251c14 2020-01-15 tracey }
874 2c251c14 2020-01-15 tracey
875 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
876 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
877 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
878 2c251c14 2020-01-15 tracey
879 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
880 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
881 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
882 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
883 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
884 63ee0dca 2020-02-08 tracey KATTR_ID, "nav_next", KATTR__MAX);
885 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
886 b3f1f953 2020-02-09 tracey goto done;
887 63ee0dca 2020-02-08 tracey if (asprintf(&href_next, "?page=%d",
888 b3f1f953 2020-02-09 tracey gw_trans->page + 1) == -1) {
889 b3f1f953 2020-02-09 tracey error = got_error_from_errno("calloc");
890 b3f1f953 2020-02-09 tracey goto done;
891 b3f1f953 2020-02-09 tracey }
892 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
893 63ee0dca 2020-02-08 tracey KATTR_HREF, href_next, KATTR__MAX);
894 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
895 b3f1f953 2020-02-09 tracey goto done;
896 63ee0dca 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Next");
897 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
898 b3f1f953 2020-02-09 tracey goto done;
899 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
900 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
901 b3f1f953 2020-02-09 tracey goto done;
902 2c251c14 2020-01-15 tracey next_disp = 0;
903 2c251c14 2020-01-15 tracey break;
904 2c251c14 2020-01-15 tracey }
905 2c251c14 2020-01-15 tracey
906 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
907 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
908 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
909 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
910 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
911 565d9dcb 2020-02-09 tracey if (kerr != KCGI_OK)
912 b3f1f953 2020-02-09 tracey goto done;
913 c25c2314 2020-01-28 stsp }
914 2c251c14 2020-01-15 tracey next_disp++;
915 2c251c14 2020-01-15 tracey }
916 b3f1f953 2020-02-09 tracey done:
917 00f13350 2020-02-10 tracey free(href_prev);
918 00f13350 2020-02-10 tracey free(href_next);
919 b3f1f953 2020-02-09 tracey free(href_summary);
920 b3f1f953 2020-02-09 tracey free(href_briefs);
921 b3f1f953 2020-02-09 tracey free(href_commits);
922 b3f1f953 2020-02-09 tracey free(href_tree);
923 565d9dcb 2020-02-09 tracey if (error == NULL && kerr != KCGI_OK)
924 565d9dcb 2020-02-09 tracey error = gw_kcgi_error(kerr);
925 2c251c14 2020-01-15 tracey return error;
926 2c251c14 2020-01-15 tracey }
927 2c251c14 2020-01-15 tracey
928 2c251c14 2020-01-15 tracey static const struct got_error *
929 f2f46662 2020-01-23 tracey gw_commits(struct gw_trans *gw_trans)
930 2c251c14 2020-01-15 tracey {
931 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
932 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
933 55e46400 2020-02-18 tracey char *age = NULL, *href_diff = NULL, *href_blob = NULL;
934 55e46400 2020-02-18 tracey char *href_prev = NULL, *href_next = NULL;
935 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
936 6bee13de 2020-01-16 tracey
937 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
938 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
939 f2f46662 2020-01-23 tracey
940 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
941 6bee13de 2020-01-16 tracey NULL) == -1) {
942 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
943 5ddf0079 2020-01-29 stsp goto done;
944 6bee13de 2020-01-16 tracey }
945 8087c3c5 2020-01-15 tracey
946 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
947 8087c3c5 2020-01-15 tracey if (error)
948 5ddf0079 2020-01-29 stsp goto done;
949 2c251c14 2020-01-15 tracey
950 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
951 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
952 18da9978 2020-01-29 stsp if (error)
953 18da9978 2020-01-29 stsp goto done;
954 4ceb8155 2020-01-15 tracey
955 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
956 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
957 21a55cc7 2020-02-04 tracey "commits_line_wrapper", KATTR__MAX);
958 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
959 18da9978 2020-01-29 stsp goto done;
960 21a55cc7 2020-02-04 tracey error = gw_gen_commit_header(gw_trans, n_header->commit_id,
961 21a55cc7 2020-02-04 tracey n_header->refs_str);
962 f7cc9480 2020-02-05 tracey if (error)
963 f7cc9480 2020-02-05 tracey goto done;
964 21a55cc7 2020-02-04 tracey error = gw_gen_author_header(gw_trans, n_header->author);
965 21a55cc7 2020-02-04 tracey if (error)
966 21a55cc7 2020-02-04 tracey goto done;
967 21a55cc7 2020-02-04 tracey error = gw_gen_committer_header(gw_trans, n_header->author);
968 21a55cc7 2020-02-04 tracey if (error)
969 21a55cc7 2020-02-04 tracey goto done;
970 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, n_header->committer_time,
971 55ccf528 2020-02-03 stsp TM_LONG);
972 55ccf528 2020-02-03 stsp if (error)
973 55ccf528 2020-02-03 stsp goto done;
974 21a55cc7 2020-02-04 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
975 21a55cc7 2020-02-04 tracey if (error)
976 55ccf528 2020-02-03 stsp goto done;
977 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
978 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
979 21a55cc7 2020-02-04 tracey goto done;
980 21a55cc7 2020-02-04 tracey
981 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
982 21a55cc7 2020-02-04 tracey "dotted_line", KATTR__MAX);
983 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
984 21a55cc7 2020-02-04 tracey goto done;
985 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
986 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
987 21a55cc7 2020-02-04 tracey goto done;
988 21a55cc7 2020-02-04 tracey
989 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
990 21a55cc7 2020-02-04 tracey "commit", KATTR__MAX);
991 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
992 21a55cc7 2020-02-04 tracey goto done;
993 78a9dab5 2020-02-07 tracey kerr = khttp_puts(gw_trans->gw_req, n_header->commit_msg);
994 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
995 21a55cc7 2020-02-04 tracey goto done;
996 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
997 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
998 21a55cc7 2020-02-04 tracey goto done;
999 21a55cc7 2020-02-04 tracey
1000 21a55cc7 2020-02-04 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1001 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
1002 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
1003 18da9978 2020-01-29 stsp goto done;
1004 18da9978 2020-01-29 stsp }
1005 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1006 21a55cc7 2020-02-04 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
1007 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1008 21a55cc7 2020-02-04 tracey goto done;
1009 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1010 21a55cc7 2020-02-04 tracey KATTR_ID, "navs", KATTR__MAX);
1011 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1012 21a55cc7 2020-02-04 tracey goto done;
1013 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1014 21a55cc7 2020-02-04 tracey KATTR_HREF, href_diff, KATTR__MAX);
1015 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1016 21a55cc7 2020-02-04 tracey goto done;
1017 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1018 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1019 21a55cc7 2020-02-04 tracey goto done;
1020 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1021 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1022 21a55cc7 2020-02-04 tracey goto done;
1023 21a55cc7 2020-02-04 tracey
1024 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1025 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1026 21a55cc7 2020-02-04 tracey goto done;
1027 21a55cc7 2020-02-04 tracey
1028 626b25b6 2020-02-06 tracey if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1029 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
1030 21a55cc7 2020-02-04 tracey error = got_error_from_errno("asprintf");
1031 21a55cc7 2020-02-04 tracey goto done;
1032 21a55cc7 2020-02-04 tracey }
1033 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1034 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
1035 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1036 21a55cc7 2020-02-04 tracey goto done;
1037 21a55cc7 2020-02-04 tracey khtml_puts(gw_trans->gw_html_req, "tree");
1038 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1040 21a55cc7 2020-02-04 tracey goto done;
1041 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1042 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1043 21a55cc7 2020-02-04 tracey goto done;
1044 21a55cc7 2020-02-04 tracey
1045 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1046 21a55cc7 2020-02-04 tracey "solid_line", KATTR__MAX);
1047 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1048 21a55cc7 2020-02-04 tracey goto done;
1049 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1050 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1051 21a55cc7 2020-02-04 tracey goto done;
1052 21a55cc7 2020-02-04 tracey
1053 55ccf528 2020-02-03 stsp free(age);
1054 55ccf528 2020-02-03 stsp age = NULL;
1055 55e46400 2020-02-18 tracey }
1056 55e46400 2020-02-18 tracey
1057 55e46400 2020-02-18 tracey if (gw_trans->next_id || gw_trans->page > 0) {
1058 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1059 55e46400 2020-02-18 tracey KATTR_ID, "np_wrapper", KATTR__MAX);
1060 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1061 55e46400 2020-02-18 tracey goto done;
1062 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1063 55e46400 2020-02-18 tracey KATTR_ID, "nav_prev", KATTR__MAX);
1064 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1065 55e46400 2020-02-18 tracey goto done;
1066 f2f46662 2020-01-23 tracey }
1067 55e46400 2020-02-18 tracey
1068 55e46400 2020-02-18 tracey if (gw_trans->page > 0) {
1069 55e46400 2020-02-18 tracey if (asprintf(&href_prev,
1070 55e46400 2020-02-18 tracey "?path=%s&page=%d&action=commits&commit=%s&prev=%s",
1071 55e46400 2020-02-18 tracey gw_trans->repo_name, gw_trans->page - 1,
1072 55e46400 2020-02-18 tracey gw_trans->prev_id ? gw_trans->prev_id : "",
1073 55e46400 2020-02-18 tracey gw_trans->prev_prev_id ?
1074 55e46400 2020-02-18 tracey gw_trans->prev_prev_id : "") == -1) {
1075 55e46400 2020-02-18 tracey error = got_error_from_errno("asprintf");
1076 55e46400 2020-02-18 tracey goto done;
1077 55e46400 2020-02-18 tracey }
1078 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1079 55e46400 2020-02-18 tracey KATTR_HREF, href_prev, KATTR__MAX);
1080 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1081 55e46400 2020-02-18 tracey goto done;
1082 55e46400 2020-02-18 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1083 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1084 55e46400 2020-02-18 tracey goto done;
1085 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1086 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1087 55e46400 2020-02-18 tracey goto done;
1088 55e46400 2020-02-18 tracey }
1089 55e46400 2020-02-18 tracey
1090 55e46400 2020-02-18 tracey if (gw_trans->next_id || gw_trans->page > 0) {
1091 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1092 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1093 55e46400 2020-02-18 tracey return gw_kcgi_error(kerr);
1094 55e46400 2020-02-18 tracey }
1095 55e46400 2020-02-18 tracey
1096 55e46400 2020-02-18 tracey if (gw_trans->next_id) {
1097 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1098 55e46400 2020-02-18 tracey KATTR_ID, "nav_next", KATTR__MAX);
1099 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1100 55e46400 2020-02-18 tracey goto done;
1101 55e46400 2020-02-18 tracey if (asprintf(&href_next,
1102 55e46400 2020-02-18 tracey "?path=%s&page=%d&action=commits" \
1103 55e46400 2020-02-18 tracey "&commit=%s&prev=%s&prev_prev=%s",
1104 55e46400 2020-02-18 tracey gw_trans->repo_name, gw_trans->page + 1,
1105 55e46400 2020-02-18 tracey gw_trans->next_id,
1106 55e46400 2020-02-18 tracey gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1107 55e46400 2020-02-18 tracey gw_trans->prev_id ?
1108 55e46400 2020-02-18 tracey gw_trans->prev_id : "") == -1) {
1109 55e46400 2020-02-18 tracey error = got_error_from_errno("calloc");
1110 55e46400 2020-02-18 tracey goto done;
1111 55e46400 2020-02-18 tracey }
1112 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1113 55e46400 2020-02-18 tracey KATTR_HREF, href_next, KATTR__MAX);
1114 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1115 55e46400 2020-02-18 tracey goto done;
1116 55e46400 2020-02-18 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1117 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1118 55e46400 2020-02-18 tracey goto done;
1119 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1120 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1121 55e46400 2020-02-18 tracey goto done;
1122 55e46400 2020-02-18 tracey }
1123 55e46400 2020-02-18 tracey
1124 55e46400 2020-02-18 tracey if (gw_trans->next_id || gw_trans->page > 0) {
1125 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1126 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1127 55e46400 2020-02-18 tracey goto done;
1128 55e46400 2020-02-18 tracey }
1129 18da9978 2020-01-29 stsp done:
1130 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1131 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1132 b0a1bc86 2020-02-14 stsp gw_free_header(n_header);
1133 55ccf528 2020-02-03 stsp free(age);
1134 55e46400 2020-02-18 tracey free(href_next);
1135 55e46400 2020-02-18 tracey free(href_prev);
1136 21a55cc7 2020-02-04 tracey free(href_diff);
1137 626b25b6 2020-02-06 tracey free(href_blob);
1138 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1139 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1140 2c251c14 2020-01-15 tracey return error;
1141 2c251c14 2020-01-15 tracey }
1142 2c251c14 2020-01-15 tracey
1143 2c251c14 2020-01-15 tracey static const struct got_error *
1144 f2f46662 2020-01-23 tracey gw_briefs(struct gw_trans *gw_trans)
1145 2c251c14 2020-01-15 tracey {
1146 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1147 bd275801 2020-02-03 tracey struct gw_header *header = NULL, *n_header = NULL;
1148 55e46400 2020-02-18 tracey char *age = NULL, *href_diff = NULL, *href_blob = NULL;
1149 55e46400 2020-02-18 tracey char *href_prev = NULL, *href_next = NULL;
1150 bd275801 2020-02-03 tracey char *newline, *smallerthan;
1151 ef72fe2c 2020-02-04 stsp enum kcgi_err kerr = KCGI_OK;
1152 17a96b9f 2020-01-15 tracey
1153 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
1154 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
1155 f2f46662 2020-01-23 tracey
1156 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
1157 6bee13de 2020-01-16 tracey NULL) == -1) {
1158 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
1159 5ddf0079 2020-01-29 stsp goto done;
1160 6bee13de 2020-01-16 tracey }
1161 6bee13de 2020-01-16 tracey
1162 85993e64 2020-02-17 tracey if (gw_trans->action != GW_SUMMARY) {
1163 85993e64 2020-02-17 tracey error = gw_apply_unveil(gw_trans->gw_dir->path);
1164 85993e64 2020-02-17 tracey if (error)
1165 85993e64 2020-02-17 tracey goto done;
1166 85993e64 2020-02-17 tracey }
1167 9d84e7dd 2020-01-15 tracey
1168 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_SUMMARY)
1169 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
1170 f2f46662 2020-01-23 tracey else
1171 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
1172 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
1173 c25c2314 2020-01-28 stsp if (error)
1174 0e00e8f4 2020-01-29 stsp goto done;
1175 c25c2314 2020-01-28 stsp
1176 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
1177 bd275801 2020-02-03 tracey error = gw_get_time_str(&age, n_header->committer_time,
1178 bd275801 2020-02-03 tracey TM_DIFF);
1179 bd275801 2020-02-03 tracey if (error)
1180 bd275801 2020-02-03 tracey goto done;
1181 bd275801 2020-02-03 tracey
1182 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1183 bd275801 2020-02-03 tracey KATTR_ID, "briefs_wrapper", KATTR__MAX);
1184 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1185 bd275801 2020-02-03 tracey goto done;
1186 bd275801 2020-02-03 tracey
1187 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1188 bd275801 2020-02-03 tracey KATTR_ID, "briefs_age", KATTR__MAX);
1189 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1190 bd275801 2020-02-03 tracey goto done;
1191 53e81e48 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
1192 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1193 bd275801 2020-02-03 tracey goto done;
1194 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1195 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1196 bd275801 2020-02-03 tracey goto done;
1197 bd275801 2020-02-03 tracey
1198 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1199 bd275801 2020-02-03 tracey KATTR_ID, "briefs_author", KATTR__MAX);
1200 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1201 bd275801 2020-02-03 tracey goto done;
1202 bd275801 2020-02-03 tracey smallerthan = strchr(n_header->author, '<');
1203 bd275801 2020-02-03 tracey if (smallerthan)
1204 bd275801 2020-02-03 tracey *smallerthan = '\0';
1205 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
1206 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1207 bd275801 2020-02-03 tracey goto done;
1208 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1209 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1210 bd275801 2020-02-03 tracey goto done;
1211 bd275801 2020-02-03 tracey
1212 52f8346c 2020-02-03 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
1213 52f8346c 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
1214 52f8346c 2020-02-03 tracey error = got_error_from_errno("asprintf");
1215 52f8346c 2020-02-03 tracey goto done;
1216 52f8346c 2020-02-03 tracey }
1217 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1218 bd275801 2020-02-03 tracey KATTR_ID, "briefs_log", KATTR__MAX);
1219 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1220 bd275801 2020-02-03 tracey goto done;
1221 f2f46662 2020-01-23 tracey newline = strchr(n_header->commit_msg, '\n');
1222 f2f46662 2020-01-23 tracey if (newline)
1223 f2f46662 2020-01-23 tracey *newline = '\0';
1224 52f8346c 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1225 52f8346c 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
1226 52f8346c 2020-02-03 tracey if (kerr != KCGI_OK)
1227 52f8346c 2020-02-03 tracey goto done;
1228 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
1229 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1230 55ccf528 2020-02-03 stsp goto done;
1231 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1232 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1233 bf93a5c0 2020-02-15 tracey goto done;
1234 bf93a5c0 2020-02-15 tracey
1235 bf93a5c0 2020-02-15 tracey if (n_header->refs_str) {
1236 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req, " ");
1237 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1238 bf93a5c0 2020-02-15 tracey goto done;
1239 bf93a5c0 2020-02-15 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
1240 bf93a5c0 2020-02-15 tracey KATTR_ID, "refs_str", KATTR__MAX);
1241 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1242 bf93a5c0 2020-02-15 tracey goto done;
1243 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req, "(");
1244 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1245 bf93a5c0 2020-02-15 tracey goto done;
1246 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1247 bf93a5c0 2020-02-15 tracey n_header->refs_str);
1248 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1249 bf93a5c0 2020-02-15 tracey goto done;
1250 bf93a5c0 2020-02-15 tracey kerr = khtml_puts(gw_trans->gw_html_req, ")");
1251 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1252 bf93a5c0 2020-02-15 tracey goto done;
1253 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1254 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
1255 bf93a5c0 2020-02-15 tracey goto done;
1256 bf93a5c0 2020-02-15 tracey }
1257 bf93a5c0 2020-02-15 tracey
1258 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1259 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1260 bd275801 2020-02-03 tracey goto done;
1261 bd275801 2020-02-03 tracey
1262 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1263 bd275801 2020-02-03 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
1264 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1265 bd275801 2020-02-03 tracey goto done;
1266 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1267 bd275801 2020-02-03 tracey KATTR_ID, "navs", KATTR__MAX);
1268 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1269 bd275801 2020-02-03 tracey goto done;
1270 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1271 bd275801 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
1272 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1273 070fee27 2020-02-03 stsp goto done;
1274 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "diff");
1275 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1276 bd275801 2020-02-03 tracey goto done;
1277 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1278 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1279 bd275801 2020-02-03 tracey goto done;
1280 bd275801 2020-02-03 tracey
1281 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
1282 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1283 bd275801 2020-02-03 tracey goto done;
1284 bd275801 2020-02-03 tracey
1285 626b25b6 2020-02-06 tracey if (asprintf(&href_blob, "?path=%s&action=tree&commit=%s",
1286 bd275801 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
1287 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
1288 0e00e8f4 2020-01-29 stsp goto done;
1289 0e00e8f4 2020-01-29 stsp }
1290 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1291 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
1292 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1293 bd275801 2020-02-03 tracey goto done;
1294 bd275801 2020-02-03 tracey khtml_puts(gw_trans->gw_html_req, "tree");
1295 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1296 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1297 bd275801 2020-02-03 tracey goto done;
1298 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1299 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1300 bd275801 2020-02-03 tracey goto done;
1301 bd275801 2020-02-03 tracey
1302 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1303 bd275801 2020-02-03 tracey KATTR_ID, "dotted_line", KATTR__MAX);
1304 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1305 bd275801 2020-02-03 tracey goto done;
1306 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1307 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1308 bd275801 2020-02-03 tracey goto done;
1309 bd275801 2020-02-03 tracey
1310 55ccf528 2020-02-03 stsp free(age);
1311 55ccf528 2020-02-03 stsp age = NULL;
1312 bd275801 2020-02-03 tracey free(href_diff);
1313 bd275801 2020-02-03 tracey href_diff = NULL;
1314 626b25b6 2020-02-06 tracey free(href_blob);
1315 626b25b6 2020-02-06 tracey href_blob = NULL;
1316 55e46400 2020-02-18 tracey }
1317 55e46400 2020-02-18 tracey
1318 55e46400 2020-02-18 tracey if (gw_trans->next_id || gw_trans->page > 0) {
1319 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1320 55e46400 2020-02-18 tracey KATTR_ID, "np_wrapper", KATTR__MAX);
1321 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1322 55e46400 2020-02-18 tracey goto done;
1323 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1324 55e46400 2020-02-18 tracey KATTR_ID, "nav_prev", KATTR__MAX);
1325 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1326 55e46400 2020-02-18 tracey goto done;
1327 55e46400 2020-02-18 tracey }
1328 55e46400 2020-02-18 tracey
1329 55e46400 2020-02-18 tracey if (gw_trans->page > 0) {
1330 55e46400 2020-02-18 tracey if (asprintf(&href_prev,
1331 55e46400 2020-02-18 tracey "?path=%s&page=%d&action=briefs&commit=%s&prev=%s",
1332 55e46400 2020-02-18 tracey gw_trans->repo_name, gw_trans->page - 1,
1333 55e46400 2020-02-18 tracey gw_trans->prev_id ? gw_trans->prev_id : "",
1334 55e46400 2020-02-18 tracey gw_trans->prev_prev_id ?
1335 55e46400 2020-02-18 tracey gw_trans->prev_prev_id : "") == -1) {
1336 55e46400 2020-02-18 tracey error = got_error_from_errno("asprintf");
1337 55e46400 2020-02-18 tracey goto done;
1338 55e46400 2020-02-18 tracey }
1339 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1340 55e46400 2020-02-18 tracey KATTR_HREF, href_prev, KATTR__MAX);
1341 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1342 55e46400 2020-02-18 tracey goto done;
1343 55e46400 2020-02-18 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Previous");
1344 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1345 55e46400 2020-02-18 tracey goto done;
1346 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1347 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1348 55e46400 2020-02-18 tracey goto done;
1349 9d84e7dd 2020-01-15 tracey }
1350 55e46400 2020-02-18 tracey
1351 55e46400 2020-02-18 tracey if (gw_trans->next_id || gw_trans->page > 0) {
1352 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1353 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1354 55e46400 2020-02-18 tracey return gw_kcgi_error(kerr);
1355 55e46400 2020-02-18 tracey }
1356 55e46400 2020-02-18 tracey
1357 55e46400 2020-02-18 tracey if (gw_trans->next_id) {
1358 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1359 55e46400 2020-02-18 tracey KATTR_ID, "nav_next", KATTR__MAX);
1360 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1361 55e46400 2020-02-18 tracey goto done;
1362 55e46400 2020-02-18 tracey if (asprintf(&href_next,
1363 55e46400 2020-02-18 tracey "?path=%s&page=%d&action=briefs" \
1364 55e46400 2020-02-18 tracey "&commit=%s&prev=%s&prev_prev=%s",
1365 55e46400 2020-02-18 tracey gw_trans->repo_name, gw_trans->page + 1,
1366 55e46400 2020-02-18 tracey gw_trans->next_id,
1367 55e46400 2020-02-18 tracey gw_trans->next_prev_id ? gw_trans->next_prev_id : "",
1368 55e46400 2020-02-18 tracey gw_trans->prev_id ?
1369 55e46400 2020-02-18 tracey gw_trans->prev_id : "") == -1) {
1370 55e46400 2020-02-18 tracey error = got_error_from_errno("calloc");
1371 55e46400 2020-02-18 tracey goto done;
1372 55e46400 2020-02-18 tracey }
1373 55e46400 2020-02-18 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1374 55e46400 2020-02-18 tracey KATTR_HREF, href_next, KATTR__MAX);
1375 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1376 55e46400 2020-02-18 tracey goto done;
1377 55e46400 2020-02-18 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Next");
1378 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1379 55e46400 2020-02-18 tracey goto done;
1380 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1381 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1382 55e46400 2020-02-18 tracey goto done;
1383 55e46400 2020-02-18 tracey }
1384 55e46400 2020-02-18 tracey
1385 55e46400 2020-02-18 tracey if (gw_trans->next_id || gw_trans->page > 0) {
1386 55e46400 2020-02-18 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1387 55e46400 2020-02-18 tracey if (kerr != KCGI_OK)
1388 55e46400 2020-02-18 tracey goto done;
1389 55e46400 2020-02-18 tracey }
1390 0e00e8f4 2020-01-29 stsp done:
1391 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1392 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
1393 b0a1bc86 2020-02-14 stsp gw_free_header(n_header);
1394 55ccf528 2020-02-03 stsp free(age);
1395 55e46400 2020-02-18 tracey free(href_next);
1396 55e46400 2020-02-18 tracey free(href_prev);
1397 bd275801 2020-02-03 tracey free(href_diff);
1398 626b25b6 2020-02-06 tracey free(href_blob);
1399 ef72fe2c 2020-02-04 stsp if (error == NULL && kerr != KCGI_OK)
1400 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1401 2c251c14 2020-01-15 tracey return error;
1402 2c251c14 2020-01-15 tracey }
1403 2c251c14 2020-01-15 tracey
1404 2c251c14 2020-01-15 tracey static const struct got_error *
1405 54415d85 2020-01-15 tracey gw_summary(struct gw_trans *gw_trans)
1406 387a29ba 2020-01-15 tracey {
1407 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
1408 9eed6f10 2020-02-08 tracey char *age = NULL;
1409 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1410 387a29ba 2020-01-15 tracey
1411 f4df82f9 2020-01-29 stsp if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1412 f4df82f9 2020-01-29 stsp return got_error_from_errno("pledge");
1413 6bee13de 2020-01-16 tracey
1414 85993e64 2020-02-17 tracey error = gw_apply_unveil(gw_trans->gw_dir->path);
1415 85993e64 2020-02-17 tracey if (error)
1416 85993e64 2020-02-17 tracey goto done;
1417 46b9c89b 2020-01-15 tracey
1418 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1419 783ec107 2020-02-03 tracey "summary_wrapper", KATTR__MAX);
1420 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1421 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1422 c25c2314 2020-01-28 stsp
1423 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_description &&
1424 783ec107 2020-02-03 tracey gw_trans->gw_dir->description != NULL &&
1425 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1426 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1427 783ec107 2020-02-03 tracey KATTR_ID, "description_title", KATTR__MAX);
1428 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1429 783ec107 2020-02-03 tracey goto done;
1430 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1431 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1432 783ec107 2020-02-03 tracey goto done;
1433 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1434 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1435 783ec107 2020-02-03 tracey goto done;
1436 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1437 783ec107 2020-02-03 tracey KATTR_ID, "description", KATTR__MAX);
1438 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1439 783ec107 2020-02-03 tracey goto done;
1440 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1441 783ec107 2020-02-03 tracey gw_trans->gw_dir->description);
1442 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1443 783ec107 2020-02-03 tracey goto done;
1444 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1445 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1446 783ec107 2020-02-03 tracey goto done;
1447 46b9c89b 2020-01-15 tracey }
1448 46b9c89b 2020-01-15 tracey
1449 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner &&
1450 a942aa37 2020-02-10 tracey gw_trans->gw_dir->owner != NULL &&
1451 a942aa37 2020-02-10 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
1452 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1453 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner_title", KATTR__MAX);
1454 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1455 9a1cc63f 2020-02-03 stsp goto done;
1456 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1457 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1458 9a1cc63f 2020-02-03 stsp goto done;
1459 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1460 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1461 783ec107 2020-02-03 tracey goto done;
1462 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1463 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner", KATTR__MAX);
1464 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1465 783ec107 2020-02-03 tracey goto done;
1466 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1467 783ec107 2020-02-03 tracey gw_trans->gw_dir->owner);
1468 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1469 783ec107 2020-02-03 tracey goto done;
1470 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1471 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1472 783ec107 2020-02-03 tracey goto done;
1473 46b9c89b 2020-01-15 tracey }
1474 46b9c89b 2020-01-15 tracey
1475 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
1476 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1477 2011c142 2020-02-14 stsp NULL, TM_LONG);
1478 d126c1b7 2020-01-29 stsp if (error)
1479 20f34652 2020-01-29 stsp goto done;
1480 55ccf528 2020-02-03 stsp if (age != NULL) {
1481 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1482 783ec107 2020-02-03 tracey KATTR_ID, "last_change_title", KATTR__MAX);
1483 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1484 20f34652 2020-01-29 stsp goto done;
1485 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1486 783ec107 2020-02-03 tracey "Last Change: ");
1487 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1488 20f34652 2020-01-29 stsp goto done;
1489 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1490 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1491 20f34652 2020-01-29 stsp goto done;
1492 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1493 783ec107 2020-02-03 tracey KATTR_ID, "last_change", KATTR__MAX);
1494 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1495 20f34652 2020-01-29 stsp goto done;
1496 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, age);
1497 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1498 783ec107 2020-02-03 tracey goto done;
1499 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1500 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1501 783ec107 2020-02-03 tracey goto done;
1502 46b9c89b 2020-01-15 tracey }
1503 46b9c89b 2020-01-15 tracey }
1504 783ec107 2020-02-03 tracey
1505 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1506 783ec107 2020-02-03 tracey gw_trans->gw_dir->url != NULL &&
1507 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1508 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1509 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl_title", KATTR__MAX);
1510 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1511 783ec107 2020-02-03 tracey goto done;
1512 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1513 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1514 783ec107 2020-02-03 tracey goto done;
1515 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1516 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1517 783ec107 2020-02-03 tracey goto done;
1518 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1519 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl", KATTR__MAX);
1520 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1521 783ec107 2020-02-03 tracey goto done;
1522 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1523 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1524 21a55cc7 2020-02-04 tracey goto done;
1525 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1526 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1527 783ec107 2020-02-03 tracey goto done;
1528 783ec107 2020-02-03 tracey }
1529 783ec107 2020-02-03 tracey
1530 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1531 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1532 bd275801 2020-02-03 tracey goto done;
1533 bd275801 2020-02-03 tracey
1534 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1535 bd275801 2020-02-03 tracey "briefs_title_wrapper", KATTR__MAX);
1536 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1537 bd275801 2020-02-03 tracey goto done;
1538 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1539 bd275801 2020-02-03 tracey "briefs_title", KATTR__MAX);
1540 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1541 20f34652 2020-01-29 stsp goto done;
1542 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1543 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1544 bd275801 2020-02-03 tracey goto done;
1545 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1546 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1547 bd275801 2020-02-03 tracey goto done;
1548 f2f46662 2020-01-23 tracey error = gw_briefs(gw_trans);
1549 f2f46662 2020-01-23 tracey if (error)
1550 20f34652 2020-01-29 stsp goto done;
1551 f2f46662 2020-01-23 tracey
1552 38b240fb 2020-02-06 tracey error = gw_output_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP,
1553 6eccd105 2020-02-03 stsp TAGBRIEF);
1554 6eccd105 2020-02-03 stsp if (error)
1555 6eccd105 2020-02-03 stsp goto done;
1556 8d4d2453 2020-01-15 tracey
1557 9eed6f10 2020-02-08 tracey error = gw_output_repo_heads(gw_trans);
1558 20f34652 2020-01-29 stsp done:
1559 20f34652 2020-01-29 stsp free(age);
1560 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1561 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1562 2204c934 2020-01-15 tracey return error;
1563 2204c934 2020-01-15 tracey }
1564 2204c934 2020-01-15 tracey
1565 2204c934 2020-01-15 tracey static const struct got_error *
1566 f2f46662 2020-01-23 tracey gw_tree(struct gw_trans *gw_trans)
1567 b772de24 2020-01-15 tracey {
1568 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
1569 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
1570 f2f46662 2020-01-23 tracey char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1571 53e81e48 2020-02-13 tracey char *age = NULL;
1572 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1573 6bee13de 2020-01-16 tracey
1574 f2f46662 2020-01-23 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1575 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
1576 b772de24 2020-01-15 tracey
1577 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
1578 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
1579 f2f46662 2020-01-23 tracey
1580 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
1581 b772de24 2020-01-15 tracey if (error)
1582 5ddf0079 2020-01-29 stsp goto done;
1583 b772de24 2020-01-15 tracey
1584 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
1585 f2f46662 2020-01-23 tracey if (error)
1586 42281175 2020-01-29 stsp goto done;
1587 b772de24 2020-01-15 tracey
1588 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1589 626b25b6 2020-02-06 tracey "tree_header_wrapper", KATTR__MAX);
1590 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1591 55ccf528 2020-02-03 stsp goto done;
1592 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1593 626b25b6 2020-02-06 tracey "tree_header", KATTR__MAX);
1594 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1595 55ccf528 2020-02-03 stsp goto done;
1596 626b25b6 2020-02-06 tracey error = gw_gen_tree_header(gw_trans, header->tree_id);
1597 626b25b6 2020-02-06 tracey if (error)
1598 626b25b6 2020-02-06 tracey goto done;
1599 626b25b6 2020-02-06 tracey error = gw_get_time_str(&age, header->committer_time,
1600 626b25b6 2020-02-06 tracey TM_LONG);
1601 626b25b6 2020-02-06 tracey if (error)
1602 626b25b6 2020-02-06 tracey goto done;
1603 626b25b6 2020-02-06 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
1604 070fee27 2020-02-03 stsp if (error)
1605 070fee27 2020-02-03 stsp goto done;
1606 626b25b6 2020-02-06 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1607 626b25b6 2020-02-06 tracey if (error)
1608 42281175 2020-01-29 stsp goto done;
1609 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1610 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1611 42281175 2020-01-29 stsp goto done;
1612 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1613 626b25b6 2020-02-06 tracey "dotted_line", KATTR__MAX);
1614 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1615 626b25b6 2020-02-06 tracey goto done;
1616 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1617 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1618 626b25b6 2020-02-06 tracey goto done;
1619 626b25b6 2020-02-06 tracey
1620 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1621 626b25b6 2020-02-06 tracey "tree", KATTR__MAX);
1622 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
1623 626b25b6 2020-02-06 tracey goto done;
1624 626b25b6 2020-02-06 tracey error = gw_output_repo_tree(gw_trans);
1625 626b25b6 2020-02-06 tracey if (error)
1626 626b25b6 2020-02-06 tracey goto done;
1627 626b25b6 2020-02-06 tracey
1628 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1629 4ff7391f 2020-01-28 tracey done:
1630 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1631 f2f46662 2020-01-23 tracey free(tree_html_disp);
1632 f2f46662 2020-01-23 tracey free(tree_html);
1633 f2f46662 2020-01-23 tracey free(tree);
1634 55ccf528 2020-02-03 stsp free(age);
1635 626b25b6 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
1636 626b25b6 2020-02-06 tracey error = gw_kcgi_error(kerr);
1637 4ff7391f 2020-01-28 tracey return error;
1638 4ff7391f 2020-01-28 tracey }
1639 4ff7391f 2020-01-28 tracey
1640 4ff7391f 2020-01-28 tracey static const struct got_error *
1641 4ff7391f 2020-01-28 tracey gw_tag(struct gw_trans *gw_trans)
1642 4ff7391f 2020-01-28 tracey {
1643 4ff7391f 2020-01-28 tracey const struct got_error *error = NULL;
1644 4ff7391f 2020-01-28 tracey struct gw_header *header = NULL;
1645 4ff7391f 2020-01-28 tracey enum kcgi_err kerr;
1646 4ff7391f 2020-01-28 tracey
1647 4ff7391f 2020-01-28 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1648 4ff7391f 2020-01-28 tracey return got_error_from_errno("pledge");
1649 4ff7391f 2020-01-28 tracey
1650 4ff7391f 2020-01-28 tracey if ((header = gw_init_header()) == NULL)
1651 4ff7391f 2020-01-28 tracey return got_error_from_errno("malloc");
1652 4ff7391f 2020-01-28 tracey
1653 d4159696 2020-02-13 stsp error = gw_apply_unveil(gw_trans->gw_dir->path);
1654 4ff7391f 2020-01-28 tracey if (error)
1655 5ddf0079 2020-01-29 stsp goto done;
1656 4ff7391f 2020-01-28 tracey
1657 9f33591a 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
1658 9f33591a 2020-02-14 tracey error = got_error_msg(GOT_ERR_QUERYSTRING,
1659 9f33591a 2020-02-14 tracey "commit required in querystring");
1660 9f33591a 2020-02-14 tracey goto done;
1661 9f33591a 2020-02-14 tracey }
1662 9f33591a 2020-02-14 tracey
1663 4ff7391f 2020-01-28 tracey error = gw_get_header(gw_trans, header, 1);
1664 4ff7391f 2020-01-28 tracey if (error)
1665 470cd826 2020-01-29 stsp goto done;
1666 4ff7391f 2020-01-28 tracey
1667 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1668 38b240fb 2020-02-06 tracey "tag_header_wrapper", KATTR__MAX);
1669 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1670 38b240fb 2020-02-06 tracey goto done;
1671 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1672 38b240fb 2020-02-06 tracey "tag_header", KATTR__MAX);
1673 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1674 38b240fb 2020-02-06 tracey goto done;
1675 38b240fb 2020-02-06 tracey error = gw_gen_commit_header(gw_trans, header->commit_id,
1676 38b240fb 2020-02-06 tracey header->refs_str);
1677 6eccd105 2020-02-03 stsp if (error)
1678 6eccd105 2020-02-03 stsp goto done;
1679 38b240fb 2020-02-06 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
1680 38b240fb 2020-02-06 tracey if (error)
1681 470cd826 2020-01-29 stsp goto done;
1682 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1683 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1684 470cd826 2020-01-29 stsp goto done;
1685 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1686 38b240fb 2020-02-06 tracey "dotted_line", KATTR__MAX);
1687 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1688 38b240fb 2020-02-06 tracey goto done;
1689 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1690 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
1691 38b240fb 2020-02-06 tracey goto done;
1692 4ff7391f 2020-01-28 tracey
1693 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1694 38b240fb 2020-02-06 tracey "tree", KATTR__MAX);
1695 4ff7391f 2020-01-28 tracey if (kerr != KCGI_OK)
1696 38b240fb 2020-02-06 tracey goto done;
1697 38b240fb 2020-02-06 tracey
1698 38b240fb 2020-02-06 tracey error = gw_output_repo_tags(gw_trans, header, 1, TAGFULL);
1699 38b240fb 2020-02-06 tracey if (error)
1700 38b240fb 2020-02-06 tracey goto done;
1701 38b240fb 2020-02-06 tracey
1702 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1703 4ff7391f 2020-01-28 tracey done:
1704 b0a1bc86 2020-02-14 stsp gw_free_header(header);
1705 38b240fb 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
1706 38b240fb 2020-02-06 tracey error = gw_kcgi_error(kerr);
1707 2c251c14 2020-01-15 tracey return error;
1708 2c251c14 2020-01-15 tracey }
1709 2c251c14 2020-01-15 tracey
1710 2c251c14 2020-01-15 tracey static const struct got_error *
1711 54415d85 2020-01-15 tracey gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1712 2c251c14 2020-01-15 tracey {
1713 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1714 2c251c14 2020-01-15 tracey DIR *dt;
1715 2c251c14 2020-01-15 tracey char *dir_test;
1716 4ceb8155 2020-01-15 tracey int opened = 0;
1717 2c251c14 2020-01-15 tracey
1718 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1719 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1720 88d59c36 2020-01-29 stsp GOTWEB_GIT_DIR) == -1)
1721 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1722 2c251c14 2020-01-15 tracey
1723 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1724 2c251c14 2020-01-15 tracey if (dt == NULL) {
1725 2c251c14 2020-01-15 tracey free(dir_test);
1726 2c251c14 2020-01-15 tracey } else {
1727 d1635ae4 2020-02-13 tracey gw_dir->path = strdup(dir_test);
1728 d1635ae4 2020-02-13 tracey if (gw_dir->path == NULL) {
1729 d1635ae4 2020-02-13 tracey opened = 1;
1730 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
1731 a942aa37 2020-02-10 tracey goto errored;
1732 d1635ae4 2020-02-13 tracey }
1733 d1635ae4 2020-02-13 tracey opened = 1;
1734 2c251c14 2020-01-15 tracey goto done;
1735 2c251c14 2020-01-15 tracey }
1736 2c251c14 2020-01-15 tracey
1737 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1738 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1739 d81f9039 2020-02-13 stsp GOTWEB_GOT_DIR) == -1) {
1740 d81f9039 2020-02-13 stsp dir_test = NULL;
1741 d81f9039 2020-02-13 stsp error = got_error_from_errno("asprintf");
1742 d81f9039 2020-02-13 stsp goto errored;
1743 d81f9039 2020-02-13 stsp }
1744 2c251c14 2020-01-15 tracey
1745 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1746 2c251c14 2020-01-15 tracey if (dt == NULL)
1747 2c251c14 2020-01-15 tracey free(dir_test);
1748 2c251c14 2020-01-15 tracey else {
1749 4ceb8155 2020-01-15 tracey opened = 1;
1750 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
1751 2c251c14 2020-01-15 tracey goto errored;
1752 2c251c14 2020-01-15 tracey }
1753 2c251c14 2020-01-15 tracey
1754 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s",
1755 d81f9039 2020-02-13 stsp gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1) {
1756 d81f9039 2020-02-13 stsp error = got_error_from_errno("asprintf");
1757 d81f9039 2020-02-13 stsp dir_test = NULL;
1758 d81f9039 2020-02-13 stsp goto errored;
1759 d81f9039 2020-02-13 stsp }
1760 2c251c14 2020-01-15 tracey
1761 d1635ae4 2020-02-13 tracey gw_dir->path = strdup(dir_test);
1762 d1635ae4 2020-02-13 tracey if (gw_dir->path == NULL) {
1763 a942aa37 2020-02-10 tracey opened = 1;
1764 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
1765 a942aa37 2020-02-10 tracey goto errored;
1766 a942aa37 2020-02-10 tracey }
1767 cc18a904 2020-02-13 tracey
1768 cc18a904 2020-02-13 tracey dt = opendir(dir_test);
1769 cc18a904 2020-02-13 tracey if (dt == NULL) {
1770 d90bcdd6 2020-02-17 stsp error = got_error_path(gw_dir->name, GOT_ERR_NOT_GIT_REPO);
1771 cc18a904 2020-02-13 tracey goto errored;
1772 7ecc73af 2020-02-13 tracey } else
1773 7ecc73af 2020-02-13 tracey opened = 1;
1774 2c251c14 2020-01-15 tracey done:
1775 32cd4d18 2020-02-03 stsp error = gw_get_repo_description(&gw_dir->description, gw_trans,
1776 2c251c14 2020-01-15 tracey gw_dir->path);
1777 32cd4d18 2020-02-03 stsp if (error)
1778 32cd4d18 2020-02-03 stsp goto errored;
1779 9a1cc63f 2020-02-03 stsp error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1780 9a1cc63f 2020-02-03 stsp if (error)
1781 9a1cc63f 2020-02-03 stsp goto errored;
1782 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1783 2011c142 2020-02-14 stsp NULL, TM_DIFF);
1784 d126c1b7 2020-01-29 stsp if (error)
1785 d126c1b7 2020-01-29 stsp goto errored;
1786 59d3c40e 2020-02-03 stsp error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1787 2c251c14 2020-01-15 tracey errored:
1788 2c251c14 2020-01-15 tracey free(dir_test);
1789 2c251c14 2020-01-15 tracey if (opened)
1790 795c5bd7 2020-02-17 tracey if (dt && closedir(dt) == -1 && error == NULL)
1791 e227880d 2020-02-17 tracey error = got_error_from_errno("closedir");
1792 2c251c14 2020-01-15 tracey return error;
1793 2c251c14 2020-01-15 tracey }
1794 2c251c14 2020-01-15 tracey
1795 2c251c14 2020-01-15 tracey static const struct got_error *
1796 54415d85 2020-01-15 tracey gw_load_got_paths(struct gw_trans *gw_trans)
1797 2c251c14 2020-01-15 tracey {
1798 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1799 2c251c14 2020-01-15 tracey DIR *d;
1800 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
1801 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1802 2c251c14 2020-01-15 tracey struct stat st;
1803 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
1804 2c251c14 2020-01-15 tracey
1805 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
1806 2c251c14 2020-01-15 tracey if (d == NULL) {
1807 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
1808 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1809 2c251c14 2020-01-15 tracey return error;
1810 2c251c14 2020-01-15 tracey }
1811 2c251c14 2020-01-15 tracey
1812 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1813 2c251c14 2020-01-15 tracey alphasort);
1814 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
1815 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
1816 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1817 8182bd34 2020-02-17 tracey goto done;
1818 2c251c14 2020-01-15 tracey }
1819 2c251c14 2020-01-15 tracey
1820 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
1821 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
1822 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1823 2c251c14 2020-01-15 tracey break; /* account for parent and self */
1824 2c251c14 2020-01-15 tracey
1825 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1826 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
1827 2c251c14 2020-01-15 tracey continue;
1828 2c251c14 2020-01-15 tracey
1829 4619e1f2 2020-02-13 stsp error = gw_init_gw_dir(&gw_dir, sd_dent[d_i]->d_name);
1830 4619e1f2 2020-02-13 stsp if (error)
1831 8182bd34 2020-02-17 tracey goto done;
1832 2c251c14 2020-01-15 tracey
1833 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
1834 7bf16821 2020-02-07 stsp if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1835 7bf16821 2020-02-07 stsp error = NULL;
1836 2c251c14 2020-01-15 tracey continue;
1837 7bf16821 2020-02-07 stsp }
1838 2c251c14 2020-01-15 tracey else if (error)
1839 8182bd34 2020-02-17 tracey goto done;
1840 2c251c14 2020-01-15 tracey
1841 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1842 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
1843 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1844 2c251c14 2020-01-15 tracey entry);
1845 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
1846 2c251c14 2020-01-15 tracey }
1847 2c251c14 2020-01-15 tracey }
1848 8182bd34 2020-02-17 tracey done:
1849 e227880d 2020-02-17 tracey if (d && closedir(d) == -1 && error == NULL)
1850 e227880d 2020-02-17 tracey error = got_error_from_errno("closedir");
1851 2c251c14 2020-01-15 tracey return error;
1852 2c251c14 2020-01-15 tracey }
1853 2c251c14 2020-01-15 tracey
1854 2c251c14 2020-01-15 tracey static const struct got_error *
1855 54415d85 2020-01-15 tracey gw_parse_querystring(struct gw_trans *gw_trans)
1856 2c251c14 2020-01-15 tracey {
1857 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1858 2c251c14 2020-01-15 tracey struct kpair *p;
1859 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
1860 2c251c14 2020-01-15 tracey unsigned int i;
1861 2c251c14 2020-01-15 tracey
1862 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
1863 8f214b62 2020-02-17 stsp return got_error(GOT_ERR_QUERYSTRING);
1864 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1865 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
1866 f652ec0c 2020-02-13 stsp gw_trans->repo_name = p->parsed.s;
1867 2c251c14 2020-01-15 tracey
1868 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_path, "%s/%s",
1869 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1870 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1871 2c251c14 2020-01-15 tracey
1872 2c251c14 2020-01-15 tracey /* get action and set function */
1873 f1200fe3 2020-02-13 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
1874 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
1875 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
1876 f1200fe3 2020-02-13 tracey if (action->func_name == NULL)
1877 2c251c14 2020-01-15 tracey continue;
1878 f1200fe3 2020-02-13 tracey if (strcmp(action->func_name,
1879 f1200fe3 2020-02-13 tracey p->parsed.s) == 0) {
1880 f1200fe3 2020-02-13 tracey gw_trans->action = i;
1881 f1200fe3 2020-02-13 tracey break;
1882 f1200fe3 2020-02-13 tracey }
1883 f1200fe3 2020-02-13 tracey }
1884 6f6f771f 2020-02-13 tracey }
1885 6f6f771f 2020-02-13 tracey if (gw_trans->action == -1) {
1886 6f6f771f 2020-02-13 tracey gw_trans->action = GW_ERR;
1887 8f214b62 2020-02-17 stsp gw_trans->error = got_error_msg(GOT_ERR_QUERYSTRING,
1888 8f214b62 2020-02-17 stsp p != NULL ? "bad action in querystring" :
1889 8f214b62 2020-02-17 stsp "no action in querystring");
1890 cc18a904 2020-02-13 tracey return error;
1891 f1200fe3 2020-02-13 tracey }
1892 ec46ccd7 2020-01-15 tracey
1893 26dc3004 2020-02-13 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
1894 56997cd3 2020-02-14 tracey if (asprintf(&gw_trans->commit_id, "%s",
1895 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1896 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1897 26dc3004 2020-02-13 stsp }
1898 2c251c14 2020-01-15 tracey
1899 4d6abe2b 2020-02-13 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1900 4d6abe2b 2020-02-13 stsp gw_trans->repo_file = p->parsed.s;
1901 8087c3c5 2020-01-15 tracey
1902 26dc3004 2020-02-13 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER])) {
1903 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_folder, "%s",
1904 55e46400 2020-02-18 tracey p->parsed.s) == -1)
1905 55e46400 2020-02-18 tracey return got_error_from_errno("asprintf");
1906 55e46400 2020-02-18 tracey }
1907 55e46400 2020-02-18 tracey
1908 55e46400 2020-02-18 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_ID])) {
1909 55e46400 2020-02-18 tracey if (asprintf(&gw_trans->prev_id, "%s",
1910 55e46400 2020-02-18 tracey p->parsed.s) == -1)
1911 55e46400 2020-02-18 tracey return got_error_from_errno("asprintf");
1912 55e46400 2020-02-18 tracey }
1913 55e46400 2020-02-18 tracey
1914 55e46400 2020-02-18 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PREV_PREV_ID])) {
1915 55e46400 2020-02-18 tracey if (asprintf(&gw_trans->prev_prev_id, "%s",
1916 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1917 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1918 26dc3004 2020-02-13 stsp }
1919 ec46ccd7 2020-01-15 tracey
1920 742ba378 2020-02-14 stsp if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1921 742ba378 2020-02-14 stsp gw_trans->headref = p->parsed.s;
1922 2c251c14 2020-01-15 tracey
1923 4619e1f2 2020-02-13 stsp error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
1924 4619e1f2 2020-02-13 stsp if (error)
1925 4619e1f2 2020-02-13 stsp return error;
1926 46b9c89b 2020-01-15 tracey
1927 cc18a904 2020-02-13 tracey gw_trans->error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1928 2c251c14 2020-01-15 tracey } else
1929 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1930 2c251c14 2020-01-15 tracey
1931 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1932 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1933 2c251c14 2020-01-15 tracey
1934 2c251c14 2020-01-15 tracey return error;
1935 2c251c14 2020-01-15 tracey }
1936 2c251c14 2020-01-15 tracey
1937 4619e1f2 2020-02-13 stsp static const struct got_error *
1938 f652ec0c 2020-02-13 stsp gw_init_gw_dir(struct gw_dir **gw_dir, const char *dir)
1939 4619e1f2 2020-02-13 stsp {
1940 4619e1f2 2020-02-13 stsp const struct got_error *error;
1941 2c251c14 2020-01-15 tracey
1942 4619e1f2 2020-02-13 stsp *gw_dir = malloc(sizeof(**gw_dir));
1943 4619e1f2 2020-02-13 stsp if (*gw_dir == NULL)
1944 4619e1f2 2020-02-13 stsp return got_error_from_errno("malloc");
1945 2c251c14 2020-01-15 tracey
1946 4619e1f2 2020-02-13 stsp if (asprintf(&(*gw_dir)->name, "%s", dir) == -1) {
1947 4619e1f2 2020-02-13 stsp error = got_error_from_errno("asprintf");
1948 4619e1f2 2020-02-13 stsp free(*gw_dir);
1949 4619e1f2 2020-02-13 stsp *gw_dir = NULL;
1950 2c251c14 2020-01-15 tracey return NULL;
1951 4619e1f2 2020-02-13 stsp }
1952 2c251c14 2020-01-15 tracey
1953 4619e1f2 2020-02-13 stsp return NULL;
1954 474370cb 2020-01-15 tracey }
1955 474370cb 2020-01-15 tracey
1956 c25c2314 2020-01-28 stsp static const struct got_error *
1957 54415d85 2020-01-15 tracey gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1958 2c251c14 2020-01-15 tracey {
1959 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1960 c25c2314 2020-01-28 stsp
1961 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1962 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1963 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1964 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1965 2c251c14 2020-01-15 tracey khttps[code]);
1966 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1967 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1968 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1969 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1970 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1971 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1972 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1973 017d6da3 2020-01-29 tracey "nosniff");
1974 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1975 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1976 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1977 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1978 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1979 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1980 017d6da3 2020-01-29 tracey "1; mode=block");
1981 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1982 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1983 c25c2314 2020-01-28 stsp
1984 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1985 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req,
1986 017d6da3 2020-01-29 tracey kresps[KRESP_CONTENT_DISPOSITION],
1987 017d6da3 2020-01-29 tracey "attachment; filename=%s", gw_trans->repo_file);
1988 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
1989 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1990 017d6da3 2020-01-29 tracey }
1991 017d6da3 2020-01-29 tracey
1992 c25c2314 2020-01-28 stsp kerr = khttp_body(gw_trans->gw_req);
1993 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1994 2c251c14 2020-01-15 tracey }
1995 2c251c14 2020-01-15 tracey
1996 c25c2314 2020-01-28 stsp static const struct got_error *
1997 6d8d8a26 2020-01-29 stsp gw_display_index(struct gw_trans *gw_trans)
1998 2c251c14 2020-01-15 tracey {
1999 4bec7539 2020-01-29 stsp const struct got_error *error;
2000 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
2001 c25c2314 2020-01-28 stsp
2002 cc18a904 2020-02-13 tracey /* catch early querystring errors */
2003 cc18a904 2020-02-13 tracey if (gw_trans->error)
2004 cc18a904 2020-02-13 tracey gw_trans->action = GW_ERR;
2005 cc18a904 2020-02-13 tracey
2006 4bec7539 2020-01-29 stsp error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
2007 4bec7539 2020-01-29 stsp if (error)
2008 4bec7539 2020-01-29 stsp return error;
2009 4bec7539 2020-01-29 stsp
2010 f7cc9480 2020-02-05 tracey kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
2011 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2012 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
2013 2c251c14 2020-01-15 tracey
2014 017d6da3 2020-01-29 tracey if (gw_trans->action != GW_BLOB) {
2015 017d6da3 2020-01-29 tracey kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
2016 017d6da3 2020-01-29 tracey gw_query_funcs[gw_trans->action].template);
2017 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK) {
2018 017d6da3 2020-01-29 tracey khtml_close(gw_trans->gw_html_req);
2019 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
2020 017d6da3 2020-01-29 tracey }
2021 7a9bfbff 2020-01-29 stsp }
2022 2c251c14 2020-01-15 tracey
2023 7a9bfbff 2020-01-29 stsp return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
2024 2c251c14 2020-01-15 tracey }
2025 2c251c14 2020-01-15 tracey
2026 f1200fe3 2020-02-13 tracey static const struct got_error *
2027 f1200fe3 2020-02-13 tracey gw_error(struct gw_trans *gw_trans)
2028 6d8d8a26 2020-01-29 stsp {
2029 f1200fe3 2020-02-13 tracey enum kcgi_err kerr;
2030 6d8d8a26 2020-01-29 stsp
2031 f1200fe3 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
2032 f1200fe3 2020-02-13 tracey
2033 f1200fe3 2020-02-13 tracey return gw_kcgi_error(kerr);
2034 6d8d8a26 2020-01-29 stsp }
2035 6d8d8a26 2020-01-29 stsp
2036 2c251c14 2020-01-15 tracey static int
2037 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
2038 2c251c14 2020-01-15 tracey {
2039 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2040 bd275801 2020-02-03 tracey enum kcgi_err kerr;
2041 54415d85 2020-01-15 tracey struct gw_trans *gw_trans = arg;
2042 4ae179a2 2020-02-08 tracey char *img_src = NULL;
2043 2c251c14 2020-01-15 tracey
2044 2c251c14 2020-01-15 tracey switch (key) {
2045 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
2046 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2047 1dcb8908 2020-02-12 tracey KATTR_NAME, "viewport",
2048 1dcb8908 2020-02-12 tracey KATTR_CONTENT, "initial-scale=.75, user-scalable=yes",
2049 aaed5792 2020-02-08 tracey KATTR__MAX);
2050 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2051 bd275801 2020-02-03 tracey return 0;
2052 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2053 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2054 aaed5792 2020-02-08 tracey return 0;
2055 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2056 aaed5792 2020-02-08 tracey KATTR_CHARSET, "utf-8",
2057 aaed5792 2020-02-08 tracey KATTR__MAX);
2058 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2059 aaed5792 2020-02-08 tracey return 0;
2060 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2061 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2062 aaed5792 2020-02-08 tracey return 0;
2063 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2064 aaed5792 2020-02-08 tracey KATTR_NAME, "msapplication-TileColor",
2065 aaed5792 2020-02-08 tracey KATTR_CONTENT, "#da532c", KATTR__MAX);
2066 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2067 aaed5792 2020-02-08 tracey return 0;
2068 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2069 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2070 aaed5792 2020-02-08 tracey return 0;
2071 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_META,
2072 aaed5792 2020-02-08 tracey KATTR_NAME, "theme-color",
2073 aaed5792 2020-02-08 tracey KATTR_CONTENT, "#ffffff", KATTR__MAX);
2074 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2075 aaed5792 2020-02-08 tracey return 0;
2076 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2077 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2078 aaed5792 2020-02-08 tracey return 0;
2079 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2080 aaed5792 2020-02-08 tracey KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
2081 aaed5792 2020-02-08 tracey KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
2082 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2083 aaed5792 2020-02-08 tracey return 0;
2084 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2085 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2086 aaed5792 2020-02-08 tracey return 0;
2087 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2088 aaed5792 2020-02-08 tracey KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2089 aaed5792 2020-02-08 tracey "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
2090 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2091 aaed5792 2020-02-08 tracey return 0;
2092 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2093 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2094 aaed5792 2020-02-08 tracey return 0;
2095 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2096 aaed5792 2020-02-08 tracey KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
2097 aaed5792 2020-02-08 tracey "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
2098 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2099 aaed5792 2020-02-08 tracey return 0;
2100 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2101 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2102 aaed5792 2020-02-08 tracey return 0;
2103 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2104 aaed5792 2020-02-08 tracey KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
2105 aaed5792 2020-02-08 tracey KATTR__MAX);
2106 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2107 aaed5792 2020-02-08 tracey return 0;
2108 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2109 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2110 aaed5792 2020-02-08 tracey return 0;
2111 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2112 aaed5792 2020-02-08 tracey KATTR_REL, "mask-icon", KATTR_HREF,
2113 aaed5792 2020-02-08 tracey "/safari-pinned-tab.svg", KATTR__MAX);
2114 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2115 aaed5792 2020-02-08 tracey return 0;
2116 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2117 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2118 aaed5792 2020-02-08 tracey return 0;
2119 aaed5792 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
2120 aaed5792 2020-02-08 tracey KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
2121 aaed5792 2020-02-08 tracey KATTR_HREF, "/gotweb.css", KATTR__MAX);
2122 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2123 aaed5792 2020-02-08 tracey return 0;
2124 aaed5792 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2125 aaed5792 2020-02-08 tracey if (kerr != KCGI_OK)
2126 aaed5792 2020-02-08 tracey return 0;
2127 2c251c14 2020-01-15 tracey break;
2128 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
2129 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2130 185ba3ba 2020-02-04 tracey KATTR_ID, "got_link", KATTR__MAX);
2131 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
2132 185ba3ba 2020-02-04 tracey return 0;
2133 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2134 185ba3ba 2020-02-04 tracey KATTR_HREF, gw_trans->gw_conf->got_logo_url,
2135 185ba3ba 2020-02-04 tracey KATTR_TARGET, "_sotd", KATTR__MAX);
2136 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
2137 185ba3ba 2020-02-04 tracey return 0;
2138 185ba3ba 2020-02-04 tracey if (asprintf(&img_src, "/%s",
2139 185ba3ba 2020-02-04 tracey gw_trans->gw_conf->got_logo) == -1)
2140 185ba3ba 2020-02-04 tracey return 0;
2141 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
2142 185ba3ba 2020-02-04 tracey KATTR_SRC, img_src, KATTR__MAX);
2143 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
2144 185ba3ba 2020-02-04 tracey free(img_src);
2145 185ba3ba 2020-02-04 tracey return 0;
2146 bd275801 2020-02-03 tracey }
2147 185ba3ba 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2148 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
2149 185ba3ba 2020-02-04 tracey free(img_src);
2150 185ba3ba 2020-02-04 tracey return 0;
2151 185ba3ba 2020-02-04 tracey }
2152 2c251c14 2020-01-15 tracey break;
2153 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
2154 4ae179a2 2020-02-08 tracey error = gw_output_site_link(gw_trans);
2155 4ae179a2 2020-02-08 tracey if (error)
2156 4ae179a2 2020-02-08 tracey return 0;
2157 2c251c14 2020-01-15 tracey break;
2158 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
2159 bd275801 2020-02-03 tracey if (gw_trans->gw_conf->got_site_name != NULL) {
2160 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2161 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
2162 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2163 bd275801 2020-02-03 tracey return 0;
2164 bd275801 2020-02-03 tracey }
2165 2c251c14 2020-01-15 tracey break;
2166 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
2167 63ee0dca 2020-02-08 tracey break;
2168 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
2169 63ee0dca 2020-02-08 tracey "search", KATTR__MAX);
2170 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2171 bd275801 2020-02-03 tracey return 0;
2172 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_FORM,
2173 63ee0dca 2020-02-08 tracey KATTR_METHOD, "POST", KATTR__MAX);
2174 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2175 63ee0dca 2020-02-08 tracey return 0;
2176 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_INPUT, KATTR_ID,
2177 63ee0dca 2020-02-08 tracey "got-search", KATTR_NAME, "got-search", KATTR_SIZE, "15",
2178 63ee0dca 2020-02-08 tracey KATTR_MAXLENGTH, "50", KATTR__MAX);
2179 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2180 63ee0dca 2020-02-08 tracey return 0;
2181 63ee0dca 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BUTTON,
2182 63ee0dca 2020-02-08 tracey KATTR__MAX);
2183 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2184 63ee0dca 2020-02-08 tracey return 0;
2185 63ee0dca 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Search");
2186 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2187 63ee0dca 2020-02-08 tracey return 0;
2188 63ee0dca 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 4);
2189 63ee0dca 2020-02-08 tracey if (kerr != KCGI_OK)
2190 63ee0dca 2020-02-08 tracey return 0;
2191 2c251c14 2020-01-15 tracey break;
2192 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
2193 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
2194 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
2195 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2196 bd275801 2020-02-03 tracey KATTR_ID, "site_owner_wrapper", KATTR__MAX);
2197 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2198 070fee27 2020-02-03 stsp return 0;
2199 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2200 bd275801 2020-02-03 tracey KATTR_ID, "site_owner", KATTR__MAX);
2201 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2202 2c251c14 2020-01-15 tracey return 0;
2203 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2204 bd275801 2020-02-03 tracey gw_trans->gw_conf->got_site_owner);
2205 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
2206 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2207 bd275801 2020-02-03 tracey return 0;
2208 2c251c14 2020-01-15 tracey }
2209 2c251c14 2020-01-15 tracey break;
2210 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
2211 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
2212 bd275801 2020-02-03 tracey if (error) {
2213 0d100d0b 2020-02-07 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2214 0d100d0b 2020-02-07 tracey KATTR_ID, "tmpl_err", KATTR__MAX);
2215 0d100d0b 2020-02-07 tracey if (kerr != KCGI_OK)
2216 0d100d0b 2020-02-07 tracey return 0;
2217 0d100d0b 2020-02-07 tracey kerr = khttp_puts(gw_trans->gw_req, "Error: ");
2218 0d100d0b 2020-02-07 tracey if (kerr != KCGI_OK)
2219 0d100d0b 2020-02-07 tracey return 0;
2220 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, error->msg);
2221 0d100d0b 2020-02-07 tracey if (kerr != KCGI_OK)
2222 0d100d0b 2020-02-07 tracey return 0;
2223 0d100d0b 2020-02-07 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2224 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
2225 bd275801 2020-02-03 tracey return 0;
2226 bd275801 2020-02-03 tracey }
2227 2c251c14 2020-01-15 tracey break;
2228 2c251c14 2020-01-15 tracey default:
2229 2c251c14 2020-01-15 tracey return 0;
2230 2c251c14 2020-01-15 tracey }
2231 2c251c14 2020-01-15 tracey return 1;
2232 f2f46662 2020-01-23 tracey }
2233 21a55cc7 2020-02-04 tracey
2234 21a55cc7 2020-02-04 tracey static const struct got_error *
2235 21a55cc7 2020-02-04 tracey gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
2236 21a55cc7 2020-02-04 tracey {
2237 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2238 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
2239 21a55cc7 2020-02-04 tracey
2240 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2241 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit_title", KATTR__MAX);
2242 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2243 21a55cc7 2020-02-04 tracey goto done;
2244 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
2245 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2246 21a55cc7 2020-02-04 tracey goto done;
2247 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2248 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2249 21a55cc7 2020-02-04 tracey goto done;
2250 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2251 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit", KATTR__MAX);
2252 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2253 21a55cc7 2020-02-04 tracey goto done;
2254 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str1);
2255 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2256 21a55cc7 2020-02-04 tracey goto done;
2257 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, " ");
2258 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2259 21a55cc7 2020-02-04 tracey goto done;
2260 5c65becf 2020-02-13 tracey if (str2 != NULL) {
2261 bf93a5c0 2020-02-15 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_SPAN,
2262 bf93a5c0 2020-02-15 tracey KATTR_ID, "refs_str", KATTR__MAX);
2263 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
2264 bf93a5c0 2020-02-15 tracey goto done;
2265 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, "(");
2266 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2267 5c65becf 2020-02-13 tracey goto done;
2268 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, str2);
2269 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2270 5c65becf 2020-02-13 tracey goto done;
2271 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, ")");
2272 bf93a5c0 2020-02-15 tracey if (kerr != KCGI_OK)
2273 bf93a5c0 2020-02-15 tracey goto done;
2274 bf93a5c0 2020-02-15 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2275 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2276 5c65becf 2020-02-13 tracey goto done;
2277 5c65becf 2020-02-13 tracey }
2278 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2279 21a55cc7 2020-02-04 tracey done:
2280 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2281 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2282 21a55cc7 2020-02-04 tracey return error;
2283 21a55cc7 2020-02-04 tracey }
2284 21a55cc7 2020-02-04 tracey
2285 f7cc9480 2020-02-05 tracey static const struct got_error *
2286 f7cc9480 2020-02-05 tracey gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
2287 f2f46662 2020-01-23 tracey {
2288 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
2289 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
2290 f2f46662 2020-01-23 tracey
2291 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2292 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff_title", KATTR__MAX);
2293 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2294 f7cc9480 2020-02-05 tracey goto done;
2295 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
2296 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2297 f7cc9480 2020-02-05 tracey goto done;
2298 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2299 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2300 f7cc9480 2020-02-05 tracey goto done;
2301 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2302 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff", KATTR__MAX);
2303 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2304 f7cc9480 2020-02-05 tracey goto done;
2305 5c65becf 2020-02-13 tracey if (str1 != NULL) {
2306 5c65becf 2020-02-13 tracey kerr = khtml_puts(gw_trans->gw_html_req, str1);
2307 5c65becf 2020-02-13 tracey if (kerr != KCGI_OK)
2308 5c65becf 2020-02-13 tracey goto done;
2309 5c65becf 2020-02-13 tracey }
2310 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
2311 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2312 f7cc9480 2020-02-05 tracey goto done;
2313 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, str2);
2314 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2315 f7cc9480 2020-02-05 tracey goto done;
2316 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2317 f7cc9480 2020-02-05 tracey done:
2318 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2319 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2320 f7cc9480 2020-02-05 tracey return error;
2321 21a55cc7 2020-02-04 tracey }
2322 21a55cc7 2020-02-04 tracey
2323 21a55cc7 2020-02-04 tracey static const struct got_error *
2324 21a55cc7 2020-02-04 tracey gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
2325 21a55cc7 2020-02-04 tracey {
2326 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2327 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
2328 21a55cc7 2020-02-04 tracey
2329 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2330 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age_title", KATTR__MAX);
2331 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2332 21a55cc7 2020-02-04 tracey goto done;
2333 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
2334 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2335 21a55cc7 2020-02-04 tracey goto done;
2336 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2337 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2338 21a55cc7 2020-02-04 tracey goto done;
2339 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2340 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age", KATTR__MAX);
2341 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2342 21a55cc7 2020-02-04 tracey goto done;
2343 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2344 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2345 21a55cc7 2020-02-04 tracey goto done;
2346 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2347 21a55cc7 2020-02-04 tracey done:
2348 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2349 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2350 21a55cc7 2020-02-04 tracey return error;
2351 21a55cc7 2020-02-04 tracey }
2352 21a55cc7 2020-02-04 tracey
2353 21a55cc7 2020-02-04 tracey static const struct got_error *
2354 21a55cc7 2020-02-04 tracey gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
2355 21a55cc7 2020-02-04 tracey {
2356 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2357 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
2358 21a55cc7 2020-02-04 tracey
2359 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2360 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author_title", KATTR__MAX);
2361 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2362 21a55cc7 2020-02-04 tracey goto done;
2363 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
2364 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2365 21a55cc7 2020-02-04 tracey goto done;
2366 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2367 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2368 21a55cc7 2020-02-04 tracey goto done;
2369 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2370 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author", KATTR__MAX);
2371 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2372 21a55cc7 2020-02-04 tracey goto done;
2373 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2374 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2375 21a55cc7 2020-02-04 tracey goto done;
2376 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2377 21a55cc7 2020-02-04 tracey done:
2378 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2379 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2380 21a55cc7 2020-02-04 tracey return error;
2381 f2f46662 2020-01-23 tracey }
2382 f2f46662 2020-01-23 tracey
2383 21a55cc7 2020-02-04 tracey static const struct got_error *
2384 21a55cc7 2020-02-04 tracey gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
2385 21a55cc7 2020-02-04 tracey {
2386 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
2387 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
2388 21a55cc7 2020-02-04 tracey
2389 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2390 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer_title", KATTR__MAX);
2391 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2392 21a55cc7 2020-02-04 tracey goto done;
2393 87ca24ac 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
2394 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2395 21a55cc7 2020-02-04 tracey goto done;
2396 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2397 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2398 21a55cc7 2020-02-04 tracey goto done;
2399 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2400 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer", KATTR__MAX);
2401 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2402 21a55cc7 2020-02-04 tracey goto done;
2403 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2404 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
2405 21a55cc7 2020-02-04 tracey goto done;
2406 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2407 21a55cc7 2020-02-04 tracey done:
2408 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
2409 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2410 21a55cc7 2020-02-04 tracey return error;
2411 21a55cc7 2020-02-04 tracey }
2412 21a55cc7 2020-02-04 tracey
2413 f7cc9480 2020-02-05 tracey static const struct got_error *
2414 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
2415 f2f46662 2020-01-23 tracey {
2416 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
2417 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
2418 f2f46662 2020-01-23 tracey
2419 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2420 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg_title", KATTR__MAX);
2421 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2422 f7cc9480 2020-02-05 tracey goto done;
2423 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
2424 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2425 f7cc9480 2020-02-05 tracey goto done;
2426 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2427 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2428 f7cc9480 2020-02-05 tracey goto done;
2429 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2430 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg", KATTR__MAX);
2431 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2432 f7cc9480 2020-02-05 tracey goto done;
2433 f7cc9480 2020-02-05 tracey kerr = khttp_puts(gw_trans->gw_req, str);
2434 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2435 f7cc9480 2020-02-05 tracey goto done;
2436 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2437 f7cc9480 2020-02-05 tracey done:
2438 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2439 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2440 f7cc9480 2020-02-05 tracey return error;
2441 f2f46662 2020-01-23 tracey }
2442 f2f46662 2020-01-23 tracey
2443 f7cc9480 2020-02-05 tracey static const struct got_error *
2444 f7cc9480 2020-02-05 tracey gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
2445 f2f46662 2020-01-23 tracey {
2446 f7cc9480 2020-02-05 tracey const struct got_error *error = NULL;
2447 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
2448 f2f46662 2020-01-23 tracey
2449 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2450 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree_title", KATTR__MAX);
2451 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2452 f7cc9480 2020-02-05 tracey goto done;
2453 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
2454 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2455 f7cc9480 2020-02-05 tracey goto done;
2456 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2457 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2458 f7cc9480 2020-02-05 tracey goto done;
2459 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2460 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree", KATTR__MAX);
2461 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2462 f7cc9480 2020-02-05 tracey goto done;
2463 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
2464 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2465 f7cc9480 2020-02-05 tracey goto done;
2466 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2467 f7cc9480 2020-02-05 tracey done:
2468 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2469 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2470 f7cc9480 2020-02-05 tracey return error;
2471 f2f46662 2020-01-23 tracey }
2472 f2f46662 2020-01-23 tracey
2473 32cd4d18 2020-02-03 stsp static const struct got_error *
2474 32cd4d18 2020-02-03 stsp gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2475 32cd4d18 2020-02-03 stsp char *dir)
2476 2c251c14 2020-01-15 tracey {
2477 32cd4d18 2020-02-03 stsp const struct got_error *error = NULL;
2478 b8b04565 2020-02-02 tracey FILE *f = NULL;
2479 32cd4d18 2020-02-03 stsp char *d_file = NULL;
2480 2c251c14 2020-01-15 tracey unsigned int len;
2481 1ab830c1 2020-02-03 stsp size_t n;
2482 2c251c14 2020-01-15 tracey
2483 32cd4d18 2020-02-03 stsp *description = NULL;
2484 0b20762b 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_description == 0)
2485 a942aa37 2020-02-10 tracey return NULL;
2486 2c251c14 2020-01-15 tracey
2487 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/description", dir) == -1)
2488 32cd4d18 2020-02-03 stsp return got_error_from_errno("asprintf");
2489 2c251c14 2020-01-15 tracey
2490 32cd4d18 2020-02-03 stsp f = fopen(d_file, "r");
2491 32cd4d18 2020-02-03 stsp if (f == NULL) {
2492 32cd4d18 2020-02-03 stsp if (errno == ENOENT || errno == EACCES)
2493 a942aa37 2020-02-10 tracey return NULL;
2494 32cd4d18 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2495 32cd4d18 2020-02-03 stsp goto done;
2496 32cd4d18 2020-02-03 stsp }
2497 2c251c14 2020-01-15 tracey
2498 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2499 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2500 32cd4d18 2020-02-03 stsp goto done;
2501 32cd4d18 2020-02-03 stsp }
2502 32cd4d18 2020-02-03 stsp len = ftell(f);
2503 32cd4d18 2020-02-03 stsp if (len == -1) {
2504 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2505 32cd4d18 2020-02-03 stsp goto done;
2506 32cd4d18 2020-02-03 stsp }
2507 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2508 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2509 32cd4d18 2020-02-03 stsp goto done;
2510 32cd4d18 2020-02-03 stsp }
2511 32cd4d18 2020-02-03 stsp *description = calloc(len + 1, sizeof(**description));
2512 32cd4d18 2020-02-03 stsp if (*description == NULL) {
2513 32cd4d18 2020-02-03 stsp error = got_error_from_errno("calloc");
2514 32cd4d18 2020-02-03 stsp goto done;
2515 32cd4d18 2020-02-03 stsp }
2516 32cd4d18 2020-02-03 stsp
2517 32cd4d18 2020-02-03 stsp n = fread(*description, 1, len, f);
2518 1ab830c1 2020-02-03 stsp if (n == 0 && ferror(f))
2519 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2520 32cd4d18 2020-02-03 stsp done:
2521 32cd4d18 2020-02-03 stsp if (f != NULL && fclose(f) == -1 && error == NULL)
2522 32cd4d18 2020-02-03 stsp error = got_error_from_errno("fclose");
2523 2c251c14 2020-01-15 tracey free(d_file);
2524 32cd4d18 2020-02-03 stsp return error;
2525 2c251c14 2020-01-15 tracey }
2526 2c251c14 2020-01-15 tracey
2527 55ccf528 2020-02-03 stsp static const struct got_error *
2528 55ccf528 2020-02-03 stsp gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2529 474370cb 2020-01-15 tracey {
2530 474370cb 2020-01-15 tracey struct tm tm;
2531 474370cb 2020-01-15 tracey time_t diff_time;
2532 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
2533 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2534 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
2535 474370cb 2020-01-15 tracey char *now = "right now";
2536 55ccf528 2020-02-03 stsp char *s;
2537 6c6c85af 2020-01-15 tracey char datebuf[29];
2538 474370cb 2020-01-15 tracey
2539 55ccf528 2020-02-03 stsp *repo_age = NULL;
2540 55ccf528 2020-02-03 stsp
2541 474370cb 2020-01-15 tracey switch (ref_tm) {
2542 474370cb 2020-01-15 tracey case TM_DIFF:
2543 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
2544 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
2545 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2546 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 365), years) == -1)
2547 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2548 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2549 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2550 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
2551 88d59c36 2020-01-29 stsp months) == -1)
2552 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2553 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2554 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2555 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2556 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2557 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
2558 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2559 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24), days) == -1)
2560 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2561 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
2562 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2563 88d59c36 2020-01-29 stsp (diff_time / 60 / 60), hours) == -1)
2564 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2565 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
2566 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2567 88d59c36 2020-01-29 stsp minutes) == -1)
2568 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2569 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
2570 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", diff_time,
2571 88d59c36 2020-01-29 stsp seconds) == -1)
2572 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2573 474370cb 2020-01-15 tracey } else {
2574 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s", now) == -1)
2575 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2576 474370cb 2020-01-15 tracey }
2577 474370cb 2020-01-15 tracey break;
2578 474370cb 2020-01-15 tracey case TM_LONG:
2579 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
2580 55ccf528 2020-02-03 stsp return got_error_from_errno("gmtime_r");
2581 474370cb 2020-01-15 tracey
2582 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
2583 474370cb 2020-01-15 tracey if (s == NULL)
2584 55ccf528 2020-02-03 stsp return got_error_from_errno("asctime_r");
2585 474370cb 2020-01-15 tracey
2586 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2587 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2588 474370cb 2020-01-15 tracey break;
2589 474370cb 2020-01-15 tracey }
2590 55ccf528 2020-02-03 stsp return NULL;
2591 474370cb 2020-01-15 tracey }
2592 474370cb 2020-01-15 tracey
2593 d126c1b7 2020-01-29 stsp static const struct got_error *
2594 d126c1b7 2020-01-29 stsp gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2595 2011c142 2020-02-14 stsp const char *refname, int ref_tm)
2596 2c251c14 2020-01-15 tracey {
2597 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2598 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
2599 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
2600 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
2601 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
2602 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
2603 a0f36e03 2020-01-28 stsp
2604 d126c1b7 2020-01-29 stsp *repo_age = NULL;
2605 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2606 387a29ba 2020-01-15 tracey
2607 55ccf528 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_age == 0)
2608 d126c1b7 2020-01-29 stsp return NULL;
2609 87f9ebf5 2020-01-15 tracey
2610 deac617c 2020-02-14 tracey if (gw_trans->repo)
2611 deac617c 2020-02-14 tracey repo = gw_trans->repo;
2612 deac617c 2020-02-14 tracey else {
2613 deac617c 2020-02-14 tracey error = got_repo_open(&repo, dir, NULL);
2614 deac617c 2020-02-14 tracey if (error)
2615 deac617c 2020-02-14 tracey return error;
2616 deac617c 2020-02-14 tracey }
2617 2c251c14 2020-01-15 tracey
2618 2011c142 2020-02-14 stsp error = got_ref_list(&refs, repo, "refs/heads",
2619 2011c142 2020-02-14 stsp got_ref_cmp_by_name, NULL);
2620 ec46ccd7 2020-01-15 tracey if (error)
2621 d126c1b7 2020-01-29 stsp goto done;
2622 2c251c14 2020-01-15 tracey
2623 2011c142 2020-02-14 stsp /*
2624 2011c142 2020-02-14 stsp * Find the youngest branch tip in the repository, or the age of
2625 2011c142 2020-02-14 stsp * the a specific branch tip if a name was provided by the caller.
2626 2011c142 2020-02-14 stsp */
2627 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2628 2011c142 2020-02-14 stsp struct got_object_id *id = NULL;
2629 17d4bf8d 2020-02-14 stsp
2630 2011c142 2020-02-14 stsp if (refname && strcmp(got_ref_get_name(re->ref), refname) != 0)
2631 2011c142 2020-02-14 stsp continue;
2632 4e8d6e3e 2020-02-14 tracey
2633 2011c142 2020-02-14 stsp error = got_ref_resolve(&id, repo, re->ref);
2634 ec46ccd7 2020-01-15 tracey if (error)
2635 d126c1b7 2020-01-29 stsp goto done;
2636 2c251c14 2020-01-15 tracey
2637 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
2638 2011c142 2020-02-14 stsp free(id);
2639 ec46ccd7 2020-01-15 tracey if (error)
2640 d126c1b7 2020-01-29 stsp goto done;
2641 2c251c14 2020-01-15 tracey
2642 2c251c14 2020-01-15 tracey committer_time =
2643 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
2644 2011c142 2020-02-14 stsp got_object_commit_close(commit);
2645 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
2646 2c251c14 2020-01-15 tracey cmp_time = committer_time;
2647 2011c142 2020-02-14 stsp
2648 2011c142 2020-02-14 stsp if (refname)
2649 2011c142 2020-02-14 stsp break;
2650 2c251c14 2020-01-15 tracey }
2651 2c251c14 2020-01-15 tracey
2652 474370cb 2020-01-15 tracey if (cmp_time != 0) {
2653 2c251c14 2020-01-15 tracey committer_time = cmp_time;
2654 55ccf528 2020-02-03 stsp error = gw_get_time_str(repo_age, committer_time, ref_tm);
2655 d126c1b7 2020-01-29 stsp }
2656 d126c1b7 2020-01-29 stsp done:
2657 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
2658 deac617c 2020-02-14 tracey if (gw_trans->repo == NULL)
2659 deac617c 2020-02-14 tracey got_repo_close(repo);
2660 d126c1b7 2020-01-29 stsp return error;
2661 ec46ccd7 2020-01-15 tracey }
2662 ec46ccd7 2020-01-15 tracey
2663 83eb9a68 2020-02-03 stsp static const struct got_error *
2664 38b240fb 2020-02-06 tracey gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
2665 ec46ccd7 2020-01-15 tracey {
2666 ec46ccd7 2020-01-15 tracey const struct got_error *error;
2667 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
2668 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
2669 f7cc9480 2020-02-05 tracey char *label1 = NULL, *label2 = NULL, *line = NULL;
2670 05ce9a79 2020-01-24 tracey int obj_type;
2671 f7cc9480 2020-02-05 tracey size_t linesize = 0;
2672 83eb9a68 2020-02-03 stsp ssize_t linelen;
2673 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
2674 ec46ccd7 2020-01-15 tracey
2675 ec46ccd7 2020-01-15 tracey f = got_opentemp();
2676 ec46ccd7 2020-01-15 tracey if (f == NULL)
2677 ec46ccd7 2020-01-15 tracey return NULL;
2678 ec46ccd7 2020-01-15 tracey
2679 5c65becf 2020-02-13 tracey if (header->parent_id != NULL &&
2680 5c65becf 2020-02-13 tracey strncmp(header->parent_id, "/dev/null", 9) != 0) {
2681 05ce9a79 2020-01-24 tracey error = got_repo_match_object_id(&id1, &label1,
2682 deac617c 2020-02-14 tracey header->parent_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2683 05ce9a79 2020-01-24 tracey if (error)
2684 05ce9a79 2020-01-24 tracey goto done;
2685 05ce9a79 2020-01-24 tracey }
2686 ec46ccd7 2020-01-15 tracey
2687 90f16cb8 2020-01-24 tracey error = got_repo_match_object_id(&id2, &label2,
2688 deac617c 2020-02-14 tracey header->commit_id, GOT_OBJ_TYPE_ANY, 1, gw_trans->repo);
2689 90f16cb8 2020-01-24 tracey if (error)
2690 90f16cb8 2020-01-24 tracey goto done;
2691 ec46ccd7 2020-01-15 tracey
2692 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo, id2);
2693 90f16cb8 2020-01-24 tracey if (error)
2694 90f16cb8 2020-01-24 tracey goto done;
2695 05ce9a79 2020-01-24 tracey switch (obj_type) {
2696 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
2697 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2698 deac617c 2020-02-14 tracey gw_trans->repo, f);
2699 ec46ccd7 2020-01-15 tracey break;
2700 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
2701 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2702 deac617c 2020-02-14 tracey gw_trans->repo, f);
2703 ec46ccd7 2020-01-15 tracey break;
2704 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
2705 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_commits(id1, id2, 3, 0,
2706 deac617c 2020-02-14 tracey gw_trans->repo, f);
2707 ec46ccd7 2020-01-15 tracey break;
2708 ec46ccd7 2020-01-15 tracey default:
2709 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2710 ec46ccd7 2020-01-15 tracey }
2711 b8b04565 2020-02-02 tracey if (error)
2712 b8b04565 2020-02-02 tracey goto done;
2713 b8b04565 2020-02-02 tracey
2714 d4729381 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2715 d4729381 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2716 ec46ccd7 2020-01-15 tracey goto done;
2717 d4729381 2020-02-03 stsp }
2718 ec46ccd7 2020-01-15 tracey
2719 83eb9a68 2020-02-03 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2720 f7cc9480 2020-02-05 tracey error = gw_colordiff_line(gw_trans, line);
2721 ec46ccd7 2020-01-15 tracey if (error)
2722 b8b04565 2020-02-02 tracey goto done;
2723 f7cc9480 2020-02-05 tracey /* XXX: KHTML_PRETTY breaks this */
2724 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, line);
2725 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2726 83eb9a68 2020-02-03 stsp goto done;
2727 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2728 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2729 b8b04565 2020-02-02 tracey goto done;
2730 ec46ccd7 2020-01-15 tracey }
2731 f7cc9480 2020-02-05 tracey if (linelen == -1 && ferror(f))
2732 83eb9a68 2020-02-03 stsp error = got_error_from_errno("getline");
2733 ec46ccd7 2020-01-15 tracey done:
2734 d4729381 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2735 d4729381 2020-02-03 stsp error = got_error_from_errno("fclose");
2736 d4729381 2020-02-03 stsp free(line);
2737 ec46ccd7 2020-01-15 tracey free(label1);
2738 ec46ccd7 2020-01-15 tracey free(label2);
2739 ec46ccd7 2020-01-15 tracey free(id1);
2740 ec46ccd7 2020-01-15 tracey free(id2);
2741 ec46ccd7 2020-01-15 tracey
2742 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2743 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2744 83eb9a68 2020-02-03 stsp return error;
2745 2c251c14 2020-01-15 tracey }
2746 2c251c14 2020-01-15 tracey
2747 9a1cc63f 2020-02-03 stsp static const struct got_error *
2748 9a1cc63f 2020-02-03 stsp gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2749 9a1cc63f 2020-02-03 stsp {
2750 9a1cc63f 2020-02-03 stsp const struct got_error *error = NULL;
2751 9a1cc63f 2020-02-03 stsp struct got_repository *repo;
2752 9a1cc63f 2020-02-03 stsp const char *gitconfig_owner;
2753 2c251c14 2020-01-15 tracey
2754 9a1cc63f 2020-02-03 stsp *owner = NULL;
2755 2c251c14 2020-01-15 tracey
2756 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner == 0)
2757 9a1cc63f 2020-02-03 stsp return NULL;
2758 2c251c14 2020-01-15 tracey
2759 9a1cc63f 2020-02-03 stsp error = got_repo_open(&repo, dir, NULL);
2760 9a1cc63f 2020-02-03 stsp if (error)
2761 9a1cc63f 2020-02-03 stsp return error;
2762 9a1cc63f 2020-02-03 stsp gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2763 d1635ae4 2020-02-13 tracey if (gitconfig_owner) {
2764 d1635ae4 2020-02-13 tracey *owner = strdup(gitconfig_owner);
2765 d1635ae4 2020-02-13 tracey if (*owner == NULL)
2766 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
2767 d1635ae4 2020-02-13 tracey }
2768 9a1cc63f 2020-02-03 stsp got_repo_close(repo);
2769 9a1cc63f 2020-02-03 stsp return error;
2770 2c251c14 2020-01-15 tracey }
2771 2c251c14 2020-01-15 tracey
2772 59d3c40e 2020-02-03 stsp static const struct got_error *
2773 59d3c40e 2020-02-03 stsp gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2774 2c251c14 2020-01-15 tracey {
2775 59d3c40e 2020-02-03 stsp const struct got_error *error = NULL;
2776 2c251c14 2020-01-15 tracey FILE *f;
2777 59d3c40e 2020-02-03 stsp char *d_file = NULL;
2778 2c251c14 2020-01-15 tracey unsigned int len;
2779 59d3c40e 2020-02-03 stsp size_t n;
2780 2c251c14 2020-01-15 tracey
2781 59d3c40e 2020-02-03 stsp *url = NULL;
2782 2c251c14 2020-01-15 tracey
2783 59d3c40e 2020-02-03 stsp if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2784 59d3c40e 2020-02-03 stsp return got_error_from_errno("asprintf");
2785 2c251c14 2020-01-15 tracey
2786 59d3c40e 2020-02-03 stsp f = fopen(d_file, "r");
2787 59d3c40e 2020-02-03 stsp if (f == NULL) {
2788 59d3c40e 2020-02-03 stsp if (errno != ENOENT && errno != EACCES)
2789 59d3c40e 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2790 59d3c40e 2020-02-03 stsp goto done;
2791 59d3c40e 2020-02-03 stsp }
2792 59d3c40e 2020-02-03 stsp
2793 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2794 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2795 59d3c40e 2020-02-03 stsp goto done;
2796 59d3c40e 2020-02-03 stsp }
2797 59d3c40e 2020-02-03 stsp len = ftell(f);
2798 59d3c40e 2020-02-03 stsp if (len == -1) {
2799 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2800 59d3c40e 2020-02-03 stsp goto done;
2801 59d3c40e 2020-02-03 stsp }
2802 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2803 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2804 59d3c40e 2020-02-03 stsp goto done;
2805 59d3c40e 2020-02-03 stsp }
2806 2c251c14 2020-01-15 tracey
2807 59d3c40e 2020-02-03 stsp *url = calloc(len + 1, sizeof(**url));
2808 59d3c40e 2020-02-03 stsp if (*url == NULL) {
2809 59d3c40e 2020-02-03 stsp error = got_error_from_errno("calloc");
2810 59d3c40e 2020-02-03 stsp goto done;
2811 59d3c40e 2020-02-03 stsp }
2812 2c251c14 2020-01-15 tracey
2813 59d3c40e 2020-02-03 stsp n = fread(*url, 1, len, f);
2814 59d3c40e 2020-02-03 stsp if (n == 0 && ferror(f))
2815 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2816 59d3c40e 2020-02-03 stsp done:
2817 59d3c40e 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2818 59d3c40e 2020-02-03 stsp error = got_error_from_errno("fclose");
2819 2c251c14 2020-01-15 tracey free(d_file);
2820 59d3c40e 2020-02-03 stsp return NULL;
2821 8d4d2453 2020-01-15 tracey }
2822 8d4d2453 2020-01-15 tracey
2823 6eccd105 2020-02-03 stsp static const struct got_error *
2824 38b240fb 2020-02-06 tracey gw_output_repo_tags(struct gw_trans *gw_trans, struct gw_header *header,
2825 38b240fb 2020-02-06 tracey int limit, int tag_type)
2826 8d4d2453 2020-01-15 tracey {
2827 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2828 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2829 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2830 38b240fb 2020-02-06 tracey char *age = NULL;
2831 ef2c200c 2020-02-07 tracey char *id_str = NULL, *newline, *href_commits = NULL;
2832 38b240fb 2020-02-06 tracey char *tag_commit0 = NULL, *href_tag = NULL, *href_briefs = NULL;
2833 6eccd105 2020-02-03 stsp struct got_tag_object *tag = NULL;
2834 38b240fb 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
2835 38b240fb 2020-02-06 tracey int summary_header_displayed = 0;
2836 8d4d2453 2020-01-15 tracey
2837 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2838 a0f36e03 2020-01-28 stsp
2839 deac617c 2020-02-14 tracey error = got_ref_list(&refs, gw_trans->repo, "refs/tags",
2840 deac617c 2020-02-14 tracey got_ref_cmp_tags, gw_trans->repo);
2841 ec46ccd7 2020-01-15 tracey if (error)
2842 87f9ebf5 2020-01-15 tracey goto done;
2843 87f9ebf5 2020-01-15 tracey
2844 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2845 87f9ebf5 2020-01-15 tracey const char *refname;
2846 4ff7391f 2020-01-28 tracey const char *tagger;
2847 6eccd105 2020-02-03 stsp const char *tag_commit;
2848 87f9ebf5 2020-01-15 tracey time_t tagger_time;
2849 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
2850 ef2c200c 2020-02-07 tracey struct got_commit_object *commit = NULL;
2851 87f9ebf5 2020-01-15 tracey
2852 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
2853 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
2854 87f9ebf5 2020-01-15 tracey continue;
2855 87f9ebf5 2020-01-15 tracey refname += 10;
2856 87f9ebf5 2020-01-15 tracey
2857 deac617c 2020-02-14 tracey error = got_ref_resolve(&id, gw_trans->repo, re->ref);
2858 87f9ebf5 2020-01-15 tracey if (error)
2859 87f9ebf5 2020-01-15 tracey goto done;
2860 38b240fb 2020-02-06 tracey
2861 deac617c 2020-02-14 tracey error = got_object_open_as_tag(&tag, gw_trans->repo, id);
2862 38b240fb 2020-02-06 tracey if (error) {
2863 ef2c200c 2020-02-07 tracey if (error->code != GOT_ERR_OBJ_TYPE) {
2864 ef2c200c 2020-02-07 tracey free(id);
2865 ef2c200c 2020-02-07 tracey goto done;
2866 ef2c200c 2020-02-07 tracey }
2867 ef2c200c 2020-02-07 tracey /* "lightweight" tag */
2868 deac617c 2020-02-14 tracey error = got_object_open_as_commit(&commit,
2869 deac617c 2020-02-14 tracey gw_trans->repo, id);
2870 ef2c200c 2020-02-07 tracey if (error) {
2871 ef2c200c 2020-02-07 tracey free(id);
2872 ef2c200c 2020-02-07 tracey goto done;
2873 ef2c200c 2020-02-07 tracey }
2874 ef2c200c 2020-02-07 tracey tagger = got_object_commit_get_committer(commit);
2875 ef2c200c 2020-02-07 tracey tagger_time =
2876 ef2c200c 2020-02-07 tracey got_object_commit_get_committer_time(commit);
2877 ef2c200c 2020-02-07 tracey error = got_object_id_str(&id_str, id);
2878 ef2c200c 2020-02-07 tracey free(id);
2879 ef2c200c 2020-02-07 tracey } else {
2880 ef2c200c 2020-02-07 tracey free(id);
2881 ef2c200c 2020-02-07 tracey tagger = got_object_tag_get_tagger(tag);
2882 ef2c200c 2020-02-07 tracey tagger_time = got_object_tag_get_tagger_time(tag);
2883 ef2c200c 2020-02-07 tracey error = got_object_id_str(&id_str,
2884 ef2c200c 2020-02-07 tracey got_object_tag_get_object_id(tag));
2885 38b240fb 2020-02-06 tracey }
2886 87f9ebf5 2020-01-15 tracey if (error)
2887 87f9ebf5 2020-01-15 tracey goto done;
2888 87f9ebf5 2020-01-15 tracey
2889 38b240fb 2020-02-06 tracey if (tag_type == TAGFULL && strncmp(id_str, header->commit_id,
2890 38b240fb 2020-02-06 tracey strlen(id_str)) != 0)
2891 38b240fb 2020-02-06 tracey continue;
2892 38b240fb 2020-02-06 tracey
2893 ef2c200c 2020-02-07 tracey if (commit) {
2894 ef2c200c 2020-02-07 tracey error = got_object_commit_get_logmsg(&tag_commit0,
2895 ef2c200c 2020-02-07 tracey commit);
2896 ef2c200c 2020-02-07 tracey if (error)
2897 ef2c200c 2020-02-07 tracey goto done;
2898 ef2c200c 2020-02-07 tracey got_object_commit_close(commit);
2899 ef2c200c 2020-02-07 tracey } else {
2900 ef2c200c 2020-02-07 tracey tag_commit0 = strdup(got_object_tag_get_message(tag));
2901 ef2c200c 2020-02-07 tracey if (tag_commit0 == NULL) {
2902 ef2c200c 2020-02-07 tracey error = got_error_from_errno("strdup");
2903 ef2c200c 2020-02-07 tracey goto done;
2904 ef2c200c 2020-02-07 tracey }
2905 87f9ebf5 2020-01-15 tracey }
2906 87f9ebf5 2020-01-15 tracey
2907 f2f46662 2020-01-23 tracey tag_commit = tag_commit0;
2908 f2f46662 2020-01-23 tracey while (*tag_commit == '\n')
2909 f2f46662 2020-01-23 tracey tag_commit++;
2910 87f9ebf5 2020-01-15 tracey
2911 87f9ebf5 2020-01-15 tracey switch (tag_type) {
2912 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
2913 f2f46662 2020-01-23 tracey newline = strchr(tag_commit, '\n');
2914 87f9ebf5 2020-01-15 tracey if (newline)
2915 87f9ebf5 2020-01-15 tracey *newline = '\0';
2916 87f9ebf5 2020-01-15 tracey
2917 38b240fb 2020-02-06 tracey if (summary_header_displayed == 0) {
2918 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req,
2919 38b240fb 2020-02-06 tracey KELEM_DIV, KATTR_ID,
2920 38b240fb 2020-02-06 tracey "summary_tags_title_wrapper", KATTR__MAX);
2921 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2922 38b240fb 2020-02-06 tracey goto done;
2923 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req,
2924 38b240fb 2020-02-06 tracey KELEM_DIV, KATTR_ID,
2925 38b240fb 2020-02-06 tracey "summary_tags_title", KATTR__MAX);
2926 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2927 38b240fb 2020-02-06 tracey goto done;
2928 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2929 38b240fb 2020-02-06 tracey "Tags");
2930 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2931 38b240fb 2020-02-06 tracey goto done;
2932 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req,
2933 38b240fb 2020-02-06 tracey 2);
2934 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2935 38b240fb 2020-02-06 tracey goto done;
2936 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req,
2937 38b240fb 2020-02-06 tracey KELEM_DIV, KATTR_ID,
2938 38b240fb 2020-02-06 tracey "summary_tags_content", KATTR__MAX);
2939 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2940 38b240fb 2020-02-06 tracey goto done;
2941 38b240fb 2020-02-06 tracey summary_header_displayed = 1;
2942 38b240fb 2020-02-06 tracey }
2943 38b240fb 2020-02-06 tracey
2944 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2945 38b240fb 2020-02-06 tracey KATTR_ID, "tags_wrapper", KATTR__MAX);
2946 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2947 38b240fb 2020-02-06 tracey goto done;
2948 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2949 38b240fb 2020-02-06 tracey KATTR_ID, "tags_age", KATTR__MAX);
2950 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2951 38b240fb 2020-02-06 tracey goto done;
2952 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2953 55ccf528 2020-02-03 stsp if (error)
2954 87f9ebf5 2020-01-15 tracey goto done;
2955 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
2956 38b240fb 2020-02-06 tracey age ? age : "");
2957 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2958 38b240fb 2020-02-06 tracey goto done;
2959 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2960 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2961 38b240fb 2020-02-06 tracey goto done;
2962 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2963 38b240fb 2020-02-06 tracey KATTR_ID, "tags", KATTR__MAX);
2964 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2965 38b240fb 2020-02-06 tracey goto done;
2966 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, refname);
2967 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2968 38b240fb 2020-02-06 tracey goto done;
2969 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2970 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2971 38b240fb 2020-02-06 tracey goto done;
2972 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2973 38b240fb 2020-02-06 tracey KATTR_ID, "tags_name", KATTR__MAX);
2974 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2975 dad0fde4 2020-02-06 tracey goto done;
2976 dad0fde4 2020-02-06 tracey if (asprintf(&href_tag, "?path=%s&action=tag&commit=%s",
2977 dad0fde4 2020-02-06 tracey gw_trans->repo_name, id_str) == -1) {
2978 dad0fde4 2020-02-06 tracey error = got_error_from_errno("asprintf");
2979 38b240fb 2020-02-06 tracey goto done;
2980 dad0fde4 2020-02-06 tracey }
2981 056e30c8 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
2982 056e30c8 2020-02-06 tracey KATTR_HREF, href_tag, KATTR__MAX);
2983 056e30c8 2020-02-06 tracey if (kerr != KCGI_OK)
2984 056e30c8 2020-02-06 tracey goto done;
2985 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, tag_commit);
2986 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2987 38b240fb 2020-02-06 tracey goto done;
2988 dad0fde4 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
2989 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2990 38b240fb 2020-02-06 tracey goto done;
2991 87f9ebf5 2020-01-15 tracey
2992 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2993 38b240fb 2020-02-06 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
2994 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2995 38b240fb 2020-02-06 tracey goto done;
2996 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
2997 38b240fb 2020-02-06 tracey KATTR_ID, "navs", KATTR__MAX);
2998 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
2999 38b240fb 2020-02-06 tracey goto done;
3000 38b240fb 2020-02-06 tracey
3001 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3002 38b240fb 2020-02-06 tracey KATTR_HREF, href_tag, KATTR__MAX);
3003 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3004 38b240fb 2020-02-06 tracey goto done;
3005 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "tag");
3006 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3007 38b240fb 2020-02-06 tracey goto done;
3008 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3009 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3010 38b240fb 2020-02-06 tracey goto done;
3011 87f9ebf5 2020-01-15 tracey
3012 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3013 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3014 38b240fb 2020-02-06 tracey goto done;
3015 38b240fb 2020-02-06 tracey if (asprintf(&href_briefs,
3016 38b240fb 2020-02-06 tracey "?path=%s&action=briefs&commit=%s",
3017 38b240fb 2020-02-06 tracey gw_trans->repo_name, id_str) == -1) {
3018 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
3019 87f9ebf5 2020-01-15 tracey goto done;
3020 87f9ebf5 2020-01-15 tracey }
3021 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3022 38b240fb 2020-02-06 tracey KATTR_HREF, href_briefs, KATTR__MAX);
3023 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3024 38b240fb 2020-02-06 tracey goto done;
3025 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
3026 38b240fb 2020-02-06 tracey "commit briefs");
3027 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3028 38b240fb 2020-02-06 tracey goto done;
3029 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3030 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3031 38b240fb 2020-02-06 tracey goto done;
3032 87f9ebf5 2020-01-15 tracey
3033 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
3034 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3035 38b240fb 2020-02-06 tracey goto done;
3036 38b240fb 2020-02-06 tracey
3037 38b240fb 2020-02-06 tracey if (asprintf(&href_commits,
3038 38b240fb 2020-02-06 tracey "?path=%s&action=commits&commit=%s",
3039 38b240fb 2020-02-06 tracey gw_trans->repo_name, id_str) == -1) {
3040 38b240fb 2020-02-06 tracey error = got_error_from_errno("asprintf");
3041 38b240fb 2020-02-06 tracey goto done;
3042 38b240fb 2020-02-06 tracey }
3043 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
3044 38b240fb 2020-02-06 tracey KATTR_HREF, href_commits, KATTR__MAX);
3045 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3046 38b240fb 2020-02-06 tracey goto done;
3047 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
3048 38b240fb 2020-02-06 tracey "commits");
3049 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3050 38b240fb 2020-02-06 tracey goto done;
3051 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
3052 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3053 38b240fb 2020-02-06 tracey goto done;
3054 38b240fb 2020-02-06 tracey
3055 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3056 38b240fb 2020-02-06 tracey KATTR_ID, "dotted_line", KATTR__MAX);
3057 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3058 38b240fb 2020-02-06 tracey goto done;
3059 ef2c200c 2020-02-07 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
3060 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3061 38b240fb 2020-02-06 tracey goto done;
3062 87f9ebf5 2020-01-15 tracey break;
3063 87f9ebf5 2020-01-15 tracey case TAGFULL:
3064 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3065 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_date_title", KATTR__MAX);
3066 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3067 38b240fb 2020-02-06 tracey goto done;
3068 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tag Date:");
3069 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3070 38b240fb 2020-02-06 tracey goto done;
3071 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3072 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3073 38b240fb 2020-02-06 tracey goto done;
3074 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3075 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_date", KATTR__MAX);
3076 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3077 38b240fb 2020-02-06 tracey goto done;
3078 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_LONG);
3079 55ccf528 2020-02-03 stsp if (error)
3080 4ff7391f 2020-01-28 tracey goto done;
3081 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
3082 38b240fb 2020-02-06 tracey age ? age : "");
3083 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3084 070fee27 2020-02-03 stsp goto done;
3085 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3086 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3087 38b240fb 2020-02-06 tracey goto done;
3088 38b240fb 2020-02-06 tracey
3089 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3090 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_tagger_title", KATTR__MAX);
3091 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3092 38b240fb 2020-02-06 tracey goto done;
3093 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tagger:");
3094 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3095 38b240fb 2020-02-06 tracey goto done;
3096 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3097 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3098 38b240fb 2020-02-06 tracey goto done;
3099 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3100 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info_date", KATTR__MAX);
3101 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3102 38b240fb 2020-02-06 tracey goto done;
3103 38b240fb 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, tagger);
3104 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3105 38b240fb 2020-02-06 tracey goto done;
3106 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3107 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3108 38b240fb 2020-02-06 tracey goto done;
3109 38b240fb 2020-02-06 tracey
3110 38b240fb 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
3111 38b240fb 2020-02-06 tracey KATTR_ID, "tag_info", KATTR__MAX);
3112 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3113 38b240fb 2020-02-06 tracey goto done;
3114 78a9dab5 2020-02-07 tracey kerr = khttp_puts(gw_trans->gw_req, tag_commit);
3115 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3116 4ff7391f 2020-01-28 tracey goto done;
3117 87f9ebf5 2020-01-15 tracey break;
3118 87f9ebf5 2020-01-15 tracey default:
3119 87f9ebf5 2020-01-15 tracey break;
3120 87f9ebf5 2020-01-15 tracey }
3121 38b240fb 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
3122 38b240fb 2020-02-06 tracey if (kerr != KCGI_OK)
3123 6eccd105 2020-02-03 stsp goto done;
3124 87ca24ac 2020-02-04 tracey
3125 6eccd105 2020-02-03 stsp if (limit && --limit == 0)
3126 6eccd105 2020-02-03 stsp break;
3127 87f9ebf5 2020-01-15 tracey
3128 ef2c200c 2020-02-07 tracey if (tag)
3129 ef2c200c 2020-02-07 tracey got_object_tag_close(tag);
3130 6eccd105 2020-02-03 stsp tag = NULL;
3131 6c6c85af 2020-01-15 tracey free(id_str);
3132 6eccd105 2020-02-03 stsp id_str = NULL;
3133 6c6c85af 2020-01-15 tracey free(age);
3134 55ccf528 2020-02-03 stsp age = NULL;
3135 f2f46662 2020-01-23 tracey free(tag_commit0);
3136 6eccd105 2020-02-03 stsp tag_commit0 = NULL;
3137 38b240fb 2020-02-06 tracey free(href_tag);
3138 38b240fb 2020-02-06 tracey href_tag = NULL;
3139 38b240fb 2020-02-06 tracey free(href_briefs);
3140 38b240fb 2020-02-06 tracey href_briefs = NULL;
3141 38b240fb 2020-02-06 tracey free(href_commits);
3142 38b240fb 2020-02-06 tracey href_commits = NULL;
3143 6c6c85af 2020-01-15 tracey }
3144 87f9ebf5 2020-01-15 tracey done:
3145 6eccd105 2020-02-03 stsp if (tag)
3146 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
3147 6eccd105 2020-02-03 stsp free(id_str);
3148 55ccf528 2020-02-03 stsp free(age);
3149 6eccd105 2020-02-03 stsp free(tag_commit0);
3150 38b240fb 2020-02-06 tracey free(href_tag);
3151 38b240fb 2020-02-06 tracey free(href_briefs);
3152 38b240fb 2020-02-06 tracey free(href_commits);
3153 6eccd105 2020-02-03 stsp got_ref_list_free(&refs);
3154 38b240fb 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
3155 38b240fb 2020-02-06 tracey error = gw_kcgi_error(kerr);
3156 6eccd105 2020-02-03 stsp return error;
3157 f2f46662 2020-01-23 tracey }
3158 f2f46662 2020-01-23 tracey
3159 f2f46662 2020-01-23 tracey static void
3160 b0a1bc86 2020-02-14 stsp gw_free_header(struct gw_header *header)
3161 f2f46662 2020-01-23 tracey {
3162 f2f46662 2020-01-23 tracey free(header->path);
3163 4e8d6e3e 2020-02-14 tracey free(header->author);
3164 4e8d6e3e 2020-02-14 tracey free(header->committer);
3165 f2f46662 2020-01-23 tracey free(header->refs_str);
3166 f2f46662 2020-01-23 tracey free(header->commit_id);
3167 f2f46662 2020-01-23 tracey free(header->parent_id);
3168 f2f46662 2020-01-23 tracey free(header->tree_id);
3169 f2f46662 2020-01-23 tracey free(header->commit_msg);
3170 f2f46662 2020-01-23 tracey }
3171 f2f46662 2020-01-23 tracey
3172 f2f46662 2020-01-23 tracey static struct gw_header *
3173 f2f46662 2020-01-23 tracey gw_init_header()
3174 f2f46662 2020-01-23 tracey {
3175 f2f46662 2020-01-23 tracey struct gw_header *header;
3176 f2f46662 2020-01-23 tracey
3177 ae36ed87 2020-01-28 stsp header = malloc(sizeof(*header));
3178 ae36ed87 2020-01-28 stsp if (header == NULL)
3179 f2f46662 2020-01-23 tracey return NULL;
3180 f2f46662 2020-01-23 tracey
3181 f2f46662 2020-01-23 tracey header->path = NULL;
3182 ae36ed87 2020-01-28 stsp SIMPLEQ_INIT(&header->refs);
3183 5c65becf 2020-02-13 tracey
3184 5c65becf 2020-02-13 tracey header->refs_str = NULL;
3185 5c65becf 2020-02-13 tracey header->commit_id = NULL;
3186 f9315db0 2020-02-14 tracey header->committer = NULL;
3187 f9315db0 2020-02-14 tracey header->author = NULL;
3188 5c65becf 2020-02-13 tracey header->parent_id = NULL;
3189 5c65becf 2020-02-13 tracey header->tree_id = NULL;
3190 5c65becf 2020-02-13 tracey header->commit_msg = NULL;
3191 f2f46662 2020-01-23 tracey
3192 f2f46662 2020-01-23 tracey return header;
3193 f2f46662 2020-01-23 tracey }
3194 f2f46662 2020-01-23 tracey
3195 f2f46662 2020-01-23 tracey static const struct got_error *
3196 f2f46662 2020-01-23 tracey gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
3197 3c245a0e 2020-02-14 tracey int limit, struct got_object_id *id)
3198 f2f46662 2020-01-23 tracey {
3199 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
3200 f2f46662 2020-01-23 tracey struct got_commit_graph *graph = NULL;
3201 4e8d6e3e 2020-02-14 tracey struct got_commit_object *commit = NULL;
3202 55e46400 2020-02-18 tracey int chk_next = 0, chk_multi = 0, prev_set = 0;
3203 f2f46662 2020-01-23 tracey
3204 f2f46662 2020-01-23 tracey error = got_commit_graph_open(&graph, header->path, 0);
3205 f2f46662 2020-01-23 tracey if (error)
3206 00f13350 2020-02-10 tracey return error;
3207 f2f46662 2020-01-23 tracey
3208 deac617c 2020-02-14 tracey error = got_commit_graph_iter_start(graph, id, gw_trans->repo, NULL,
3209 3c245a0e 2020-02-14 tracey NULL);
3210 f2f46662 2020-01-23 tracey if (error)
3211 f2f46662 2020-01-23 tracey goto done;
3212 f2f46662 2020-01-23 tracey
3213 f2f46662 2020-01-23 tracey for (;;) {
3214 deac617c 2020-02-14 tracey error = got_commit_graph_iter_next(&id, graph, gw_trans->repo,
3215 3c245a0e 2020-02-14 tracey NULL, NULL);
3216 f2f46662 2020-01-23 tracey if (error) {
3217 f2f46662 2020-01-23 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
3218 f2f46662 2020-01-23 tracey error = NULL;
3219 f2f46662 2020-01-23 tracey goto done;
3220 f2f46662 2020-01-23 tracey }
3221 3c245a0e 2020-02-14 tracey if (id == NULL)
3222 f2f46662 2020-01-23 tracey goto done;
3223 f2f46662 2020-01-23 tracey
3224 deac617c 2020-02-14 tracey error = got_object_open_as_commit(&commit, gw_trans->repo, id);
3225 4e8d6e3e 2020-02-14 tracey if (error)
3226 4e8d6e3e 2020-02-14 tracey goto done;
3227 55e46400 2020-02-18 tracey if (limit == 1 && chk_multi == 0) {
3228 3c245a0e 2020-02-14 tracey error = gw_get_commit(gw_trans, header, commit, id);
3229 4e8d6e3e 2020-02-14 tracey if (error)
3230 4e8d6e3e 2020-02-14 tracey goto done;
3231 4e8d6e3e 2020-02-14 tracey } else {
3232 55e46400 2020-02-18 tracey chk_multi = 1;
3233 f2f46662 2020-01-23 tracey struct gw_header *n_header = NULL;
3234 5147ab56 2020-01-29 stsp if ((n_header = gw_init_header()) == NULL) {
3235 f2f46662 2020-01-23 tracey error = got_error_from_errno("malloc");
3236 a942aa37 2020-02-10 tracey goto done;
3237 d1635ae4 2020-02-13 tracey }
3238 bf93a5c0 2020-02-15 tracey error = got_ref_list(&n_header->refs, gw_trans->repo,
3239 bf93a5c0 2020-02-15 tracey NULL, got_ref_cmp_by_name, NULL);
3240 bf93a5c0 2020-02-15 tracey if (error)
3241 bf93a5c0 2020-02-15 tracey goto done;
3242 bf93a5c0 2020-02-15 tracey
3243 3c245a0e 2020-02-14 tracey error = gw_get_commit(gw_trans, n_header, commit, id);
3244 4e8d6e3e 2020-02-14 tracey if (error)
3245 d1635ae4 2020-02-13 tracey goto done;
3246 bc8c6114 2020-02-17 tracey got_ref_list_free(&n_header->refs);
3247 55e46400 2020-02-18 tracey
3248 55e46400 2020-02-18 tracey /*
3249 55e46400 2020-02-18 tracey * we have a commit_id now, so copy it to next_prev_id
3250 55e46400 2020-02-18 tracey * for navigation through gw_briefs and gw_commits
3251 55e46400 2020-02-18 tracey */
3252 55e46400 2020-02-18 tracey if (gw_trans->next_prev_id == NULL && prev_set == 0 &&
3253 55e46400 2020-02-18 tracey (gw_trans->action == GW_BRIEFS ||
3254 55e46400 2020-02-18 tracey gw_trans->action == GW_COMMITS ||
3255 55e46400 2020-02-18 tracey gw_trans->action == GW_SUMMARY)) {
3256 55e46400 2020-02-18 tracey prev_set = 1;
3257 55e46400 2020-02-18 tracey gw_trans->next_prev_id =
3258 55e46400 2020-02-18 tracey strdup(n_header->commit_id);
3259 55e46400 2020-02-18 tracey if (gw_trans->next_prev_id == NULL) {
3260 55e46400 2020-02-18 tracey error = got_error_from_errno("strdup");
3261 55e46400 2020-02-18 tracey goto done;
3262 55e46400 2020-02-18 tracey }
3263 55e46400 2020-02-18 tracey }
3264 55e46400 2020-02-18 tracey
3265 55e46400 2020-02-18 tracey /*
3266 55e46400 2020-02-18 tracey * check for one more commit before breaking,
3267 55e46400 2020-02-18 tracey * so we know whether to navicate through gw_briefs
3268 55e46400 2020-02-18 tracey * gw_commits and gw_summary
3269 55e46400 2020-02-18 tracey */
3270 55e46400 2020-02-18 tracey if (chk_next && (gw_trans->action == GW_BRIEFS||
3271 55e46400 2020-02-18 tracey gw_trans->action == GW_COMMITS ||
3272 55e46400 2020-02-18 tracey gw_trans->action == GW_SUMMARY)) {
3273 55e46400 2020-02-18 tracey gw_trans->next_id = strdup(n_header->commit_id);
3274 55e46400 2020-02-18 tracey if (gw_trans->next_id == NULL)
3275 55e46400 2020-02-18 tracey error = got_error_from_errno("strdup");
3276 55e46400 2020-02-18 tracey goto done;
3277 55e46400 2020-02-18 tracey }
3278 55e46400 2020-02-18 tracey
3279 f2f46662 2020-01-23 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
3280 f2f46662 2020-01-23 tracey entry);
3281 f2f46662 2020-01-23 tracey }
3282 55e46400 2020-02-18 tracey if (error || (limit && --limit == 0)) {
3283 55e46400 2020-02-18 tracey if (chk_multi == 0)
3284 55e46400 2020-02-18 tracey break;
3285 55e46400 2020-02-18 tracey chk_next = 1;
3286 55e46400 2020-02-18 tracey }
3287 f2f46662 2020-01-23 tracey }
3288 f2f46662 2020-01-23 tracey done:
3289 4e8d6e3e 2020-02-14 tracey if (commit != NULL)
3290 4e8d6e3e 2020-02-14 tracey got_object_commit_close(commit);
3291 f2f46662 2020-01-23 tracey if (graph)
3292 f2f46662 2020-01-23 tracey got_commit_graph_close(graph);
3293 f2f46662 2020-01-23 tracey return error;
3294 f2f46662 2020-01-23 tracey }
3295 f2f46662 2020-01-23 tracey
3296 f2f46662 2020-01-23 tracey static const struct got_error *
3297 4e8d6e3e 2020-02-14 tracey gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header,
3298 4e8d6e3e 2020-02-14 tracey struct got_commit_object *commit, struct got_object_id *id)
3299 f2f46662 2020-01-23 tracey {
3300 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
3301 f2f46662 2020-01-23 tracey struct got_reflist_entry *re;
3302 f2f46662 2020-01-23 tracey struct got_object_id *id2 = NULL;
3303 f2f46662 2020-01-23 tracey struct got_object_qid *parent_id;
3304 a81394a7 2020-02-13 stsp char *commit_msg = NULL, *commit_msg0;
3305 f2f46662 2020-01-23 tracey
3306 f2f46662 2020-01-23 tracey /*print commit*/
3307 f2f46662 2020-01-23 tracey SIMPLEQ_FOREACH(re, &header->refs, entry) {
3308 f2f46662 2020-01-23 tracey char *s;
3309 f2f46662 2020-01-23 tracey const char *name;
3310 f2f46662 2020-01-23 tracey struct got_tag_object *tag = NULL;
3311 f2f46662 2020-01-23 tracey int cmp;
3312 f2f46662 2020-01-23 tracey
3313 eb4f3ad3 2020-02-14 stsp if (got_ref_is_symbolic(re->ref))
3314 f2f46662 2020-01-23 tracey continue;
3315 eb4f3ad3 2020-02-14 stsp
3316 eb4f3ad3 2020-02-14 stsp name = got_ref_get_name(re->ref);
3317 f2f46662 2020-01-23 tracey if (strncmp(name, "refs/", 5) == 0)
3318 f2f46662 2020-01-23 tracey name += 5;
3319 f2f46662 2020-01-23 tracey if (strncmp(name, "got/", 4) == 0)
3320 f2f46662 2020-01-23 tracey continue;
3321 f2f46662 2020-01-23 tracey if (strncmp(name, "heads/", 6) == 0)
3322 f2f46662 2020-01-23 tracey name += 6;
3323 f2f46662 2020-01-23 tracey if (strncmp(name, "remotes/", 8) == 0)
3324 f2f46662 2020-01-23 tracey name += 8;
3325 f2f46662 2020-01-23 tracey if (strncmp(name, "tags/", 5) == 0) {
3326 deac617c 2020-02-14 tracey error = got_object_open_as_tag(&tag, gw_trans->repo,
3327 f2f46662 2020-01-23 tracey re->id);
3328 f2f46662 2020-01-23 tracey if (error) {
3329 f2f46662 2020-01-23 tracey if (error->code != GOT_ERR_OBJ_TYPE)
3330 f2f46662 2020-01-23 tracey continue;
3331 f2f46662 2020-01-23 tracey /*
3332 f2f46662 2020-01-23 tracey * Ref points at something other
3333 f2f46662 2020-01-23 tracey * than a tag.
3334 f2f46662 2020-01-23 tracey */
3335 f2f46662 2020-01-23 tracey error = NULL;
3336 f2f46662 2020-01-23 tracey tag = NULL;
3337 f2f46662 2020-01-23 tracey }
3338 f2f46662 2020-01-23 tracey }
3339 f2f46662 2020-01-23 tracey cmp = got_object_id_cmp(tag ?
3340 4e8d6e3e 2020-02-14 tracey got_object_tag_get_object_id(tag) : re->id, id);
3341 f2f46662 2020-01-23 tracey if (tag)
3342 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
3343 f2f46662 2020-01-23 tracey if (cmp != 0)
3344 f2f46662 2020-01-23 tracey continue;
3345 a81394a7 2020-02-13 stsp s = header->refs_str;
3346 a81394a7 2020-02-13 stsp if (asprintf(&header->refs_str, "%s%s%s", s ? s : "",
3347 88d59c36 2020-01-29 stsp s ? ", " : "", name) == -1) {
3348 f2f46662 2020-01-23 tracey error = got_error_from_errno("asprintf");
3349 f2f46662 2020-01-23 tracey free(s);
3350 a81394a7 2020-02-13 stsp header->refs_str = NULL;
3351 f2f46662 2020-01-23 tracey return error;
3352 f2f46662 2020-01-23 tracey }
3353 f2f46662 2020-01-23 tracey free(s);
3354 f2f46662 2020-01-23 tracey }
3355 f1200fe3 2020-02-13 tracey
3356 4e8d6e3e 2020-02-14 tracey error = got_object_id_str(&header->commit_id, id);
3357 f2f46662 2020-01-23 tracey if (error)
3358 f2f46662 2020-01-23 tracey return error;
3359 f2f46662 2020-01-23 tracey
3360 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->tree_id,
3361 4e8d6e3e 2020-02-14 tracey got_object_commit_get_tree_id(commit));
3362 f2f46662 2020-01-23 tracey if (error)
3363 f2f46662 2020-01-23 tracey return error;
3364 f2f46662 2020-01-23 tracey
3365 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_DIFF) {
3366 f2f46662 2020-01-23 tracey parent_id = SIMPLEQ_FIRST(
3367 4e8d6e3e 2020-02-14 tracey got_object_commit_get_parent_ids(commit));
3368 f2f46662 2020-01-23 tracey if (parent_id != NULL) {
3369 f2f46662 2020-01-23 tracey id2 = got_object_id_dup(parent_id->id);
3370 f2f46662 2020-01-23 tracey free (parent_id);
3371 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->parent_id, id2);
3372 f2f46662 2020-01-23 tracey if (error)
3373 f2f46662 2020-01-23 tracey return error;
3374 f2f46662 2020-01-23 tracey free(id2);
3375 a942aa37 2020-02-10 tracey } else {
3376 d1635ae4 2020-02-13 tracey header->parent_id = strdup("/dev/null");
3377 d1635ae4 2020-02-13 tracey if (header->parent_id == NULL) {
3378 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3379 a942aa37 2020-02-10 tracey return error;
3380 d1635ae4 2020-02-13 tracey }
3381 d1635ae4 2020-02-13 tracey }
3382 a942aa37 2020-02-10 tracey }
3383 f2f46662 2020-01-23 tracey
3384 f2f46662 2020-01-23 tracey header->committer_time =
3385 4e8d6e3e 2020-02-14 tracey got_object_commit_get_committer_time(commit);
3386 1177268c 2020-01-28 tracey
3387 d1635ae4 2020-02-13 tracey header->author =
3388 4e8d6e3e 2020-02-14 tracey strdup(got_object_commit_get_author(commit));
3389 4e8d6e3e 2020-02-14 tracey if (header->author == NULL) {
3390 4e8d6e3e 2020-02-14 tracey error = got_error_from_errno("strdup");
3391 4e8d6e3e 2020-02-14 tracey return error;
3392 4e8d6e3e 2020-02-14 tracey }
3393 d1635ae4 2020-02-13 tracey header->committer =
3394 4e8d6e3e 2020-02-14 tracey strdup(got_object_commit_get_committer(commit));
3395 4e8d6e3e 2020-02-14 tracey if (header->committer == NULL) {
3396 4e8d6e3e 2020-02-14 tracey error = got_error_from_errno("strdup");
3397 a942aa37 2020-02-10 tracey return error;
3398 4e8d6e3e 2020-02-14 tracey }
3399 4e8d6e3e 2020-02-14 tracey error = got_object_commit_get_logmsg(&commit_msg0, commit);
3400 f2f46662 2020-01-23 tracey if (error)
3401 f2f46662 2020-01-23 tracey return error;
3402 f2f46662 2020-01-23 tracey
3403 f2f46662 2020-01-23 tracey commit_msg = commit_msg0;
3404 f2f46662 2020-01-23 tracey while (*commit_msg == '\n')
3405 f2f46662 2020-01-23 tracey commit_msg++;
3406 f2f46662 2020-01-23 tracey
3407 d1635ae4 2020-02-13 tracey header->commit_msg = strdup(commit_msg);
3408 d1635ae4 2020-02-13 tracey if (header->commit_msg == NULL)
3409 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3410 f2f46662 2020-01-23 tracey free(commit_msg0);
3411 f2f46662 2020-01-23 tracey return error;
3412 f2f46662 2020-01-23 tracey }
3413 f2f46662 2020-01-23 tracey
3414 f2f46662 2020-01-23 tracey static const struct got_error *
3415 f2f46662 2020-01-23 tracey gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
3416 f2f46662 2020-01-23 tracey {
3417 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
3418 f2f46662 2020-01-23 tracey char *in_repo_path = NULL;
3419 3c245a0e 2020-02-14 tracey struct got_object_id *id = NULL;
3420 f2f46662 2020-01-23 tracey
3421 deac617c 2020-02-14 tracey error = got_repo_open(&gw_trans->repo, gw_trans->repo_path, NULL);
3422 f2f46662 2020-01-23 tracey if (error)
3423 f2f46662 2020-01-23 tracey return error;
3424 f2f46662 2020-01-23 tracey
3425 56997cd3 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
3426 f2f46662 2020-01-23 tracey struct got_reference *head_ref;
3427 deac617c 2020-02-14 tracey error = got_ref_open(&head_ref, gw_trans->repo,
3428 f2f46662 2020-01-23 tracey gw_trans->headref, 0);
3429 f2f46662 2020-01-23 tracey if (error)
3430 f2f46662 2020-01-23 tracey return error;
3431 f2f46662 2020-01-23 tracey
3432 deac617c 2020-02-14 tracey error = got_ref_resolve(&id, gw_trans->repo, head_ref);
3433 f2f46662 2020-01-23 tracey got_ref_close(head_ref);
3434 f2f46662 2020-01-23 tracey if (error)
3435 f2f46662 2020-01-23 tracey return error;
3436 f2f46662 2020-01-23 tracey } else {
3437 f2f46662 2020-01-23 tracey struct got_reference *ref;
3438 55e46400 2020-02-18 tracey
3439 56997cd3 2020-02-14 tracey error = got_ref_open(&ref, gw_trans->repo,
3440 56997cd3 2020-02-14 tracey gw_trans->commit_id, 0);
3441 f2f46662 2020-01-23 tracey if (error == NULL) {
3442 f2f46662 2020-01-23 tracey int obj_type;
3443 deac617c 2020-02-14 tracey error = got_ref_resolve(&id, gw_trans->repo, ref);
3444 f2f46662 2020-01-23 tracey got_ref_close(ref);
3445 f2f46662 2020-01-23 tracey if (error)
3446 f2f46662 2020-01-23 tracey return error;
3447 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo,
3448 3c245a0e 2020-02-14 tracey id);
3449 f2f46662 2020-01-23 tracey if (error)
3450 4e8d6e3e 2020-02-14 tracey goto done;
3451 f2f46662 2020-01-23 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
3452 f2f46662 2020-01-23 tracey struct got_tag_object *tag;
3453 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag,
3454 deac617c 2020-02-14 tracey gw_trans->repo, id);
3455 f2f46662 2020-01-23 tracey if (error)
3456 4e8d6e3e 2020-02-14 tracey goto done;
3457 f2f46662 2020-01-23 tracey if (got_object_tag_get_object_type(tag) !=
3458 f2f46662 2020-01-23 tracey GOT_OBJ_TYPE_COMMIT) {
3459 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
3460 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3461 4e8d6e3e 2020-02-14 tracey goto done;
3462 f2f46662 2020-01-23 tracey }
3463 3c245a0e 2020-02-14 tracey free(id);
3464 3c245a0e 2020-02-14 tracey id = got_object_id_dup(
3465 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag));
3466 3c245a0e 2020-02-14 tracey if (id == NULL)
3467 f2f46662 2020-01-23 tracey error = got_error_from_errno(
3468 f2f46662 2020-01-23 tracey "got_object_id_dup");
3469 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
3470 f2f46662 2020-01-23 tracey if (error)
3471 4e8d6e3e 2020-02-14 tracey goto done;
3472 f2f46662 2020-01-23 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3473 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3474 4e8d6e3e 2020-02-14 tracey goto done;
3475 f2f46662 2020-01-23 tracey }
3476 f2f46662 2020-01-23 tracey }
3477 3c245a0e 2020-02-14 tracey error = got_repo_match_object_id_prefix(&id,
3478 56997cd3 2020-02-14 tracey gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT,
3479 deac617c 2020-02-14 tracey gw_trans->repo);
3480 122f3af5 2020-02-14 stsp if (error)
3481 4e8d6e3e 2020-02-14 tracey goto done;
3482 f2f46662 2020-01-23 tracey }
3483 f2f46662 2020-01-23 tracey
3484 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3485 f2f46662 2020-01-23 tracey gw_trans->repo_path, 1);
3486 f2f46662 2020-01-23 tracey if (error)
3487 4e8d6e3e 2020-02-14 tracey goto done;
3488 f2f46662 2020-01-23 tracey
3489 f2f46662 2020-01-23 tracey if (in_repo_path) {
3490 d1635ae4 2020-02-13 tracey header->path = strdup(in_repo_path);
3491 d1635ae4 2020-02-13 tracey if (header->path == NULL) {
3492 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3493 4e8d6e3e 2020-02-14 tracey goto done;
3494 a942aa37 2020-02-10 tracey }
3495 f2f46662 2020-01-23 tracey }
3496 f2f46662 2020-01-23 tracey
3497 deac617c 2020-02-14 tracey error = got_ref_list(&header->refs, gw_trans->repo, NULL,
3498 f2f46662 2020-01-23 tracey got_ref_cmp_by_name, NULL);
3499 f2f46662 2020-01-23 tracey if (error)
3500 4e8d6e3e 2020-02-14 tracey goto done;
3501 f2f46662 2020-01-23 tracey
3502 3c245a0e 2020-02-14 tracey error = gw_get_commits(gw_trans, header, limit, id);
3503 4e8d6e3e 2020-02-14 tracey done:
3504 4e8d6e3e 2020-02-14 tracey got_ref_list_free(&header->refs);
3505 3c245a0e 2020-02-14 tracey free(id);
3506 4e8d6e3e 2020-02-14 tracey free(in_repo_path);
3507 f2f46662 2020-01-23 tracey return error;
3508 ec46ccd7 2020-01-15 tracey }
3509 ec46ccd7 2020-01-15 tracey
3510 ec46ccd7 2020-01-15 tracey struct blame_line {
3511 ec46ccd7 2020-01-15 tracey int annotated;
3512 ec46ccd7 2020-01-15 tracey char *id_str;
3513 ec46ccd7 2020-01-15 tracey char *committer;
3514 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
3515 ec46ccd7 2020-01-15 tracey };
3516 ec46ccd7 2020-01-15 tracey
3517 147269d5 2020-01-15 tracey struct gw_blame_cb_args {
3518 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
3519 ec46ccd7 2020-01-15 tracey int nlines;
3520 ec46ccd7 2020-01-15 tracey int nlines_prec;
3521 ec46ccd7 2020-01-15 tracey int lineno_cur;
3522 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
3523 ec46ccd7 2020-01-15 tracey FILE *f;
3524 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
3525 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
3526 ec46ccd7 2020-01-15 tracey };
3527 ec46ccd7 2020-01-15 tracey
3528 ec46ccd7 2020-01-15 tracey static const struct got_error *
3529 147269d5 2020-01-15 tracey gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3530 ec46ccd7 2020-01-15 tracey {
3531 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
3532 147269d5 2020-01-15 tracey struct gw_blame_cb_args *a = arg;
3533 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
3534 ec46ccd7 2020-01-15 tracey char *line = NULL;
3535 9a9d12ca 2020-02-06 tracey size_t linesize = 0;
3536 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
3537 ec46ccd7 2020-01-15 tracey off_t offset;
3538 ec46ccd7 2020-01-15 tracey struct tm tm;
3539 ec46ccd7 2020-01-15 tracey time_t committer_time;
3540 9a9d12ca 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3541 ec46ccd7 2020-01-15 tracey
3542 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
3543 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
3544 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
3545 ec46ccd7 2020-01-15 tracey
3546 ec46ccd7 2020-01-15 tracey if (lineno == -1)
3547 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
3548 ec46ccd7 2020-01-15 tracey
3549 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
3550 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
3551 ec46ccd7 2020-01-15 tracey if (bline->annotated)
3552 ec46ccd7 2020-01-15 tracey return NULL;
3553 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
3554 ec46ccd7 2020-01-15 tracey if (err)
3555 ec46ccd7 2020-01-15 tracey return err;
3556 ec46ccd7 2020-01-15 tracey
3557 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
3558 ec46ccd7 2020-01-15 tracey if (err)
3559 ec46ccd7 2020-01-15 tracey goto done;
3560 ec46ccd7 2020-01-15 tracey
3561 d1635ae4 2020-02-13 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
3562 d1635ae4 2020-02-13 tracey if (bline->committer == NULL) {
3563 d1635ae4 2020-02-13 tracey err = got_error_from_errno("strdup");
3564 ec46ccd7 2020-01-15 tracey goto done;
3565 d1635ae4 2020-02-13 tracey }
3566 ec46ccd7 2020-01-15 tracey
3567 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
3568 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
3569 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
3570 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3571 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
3572 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
3573 ec46ccd7 2020-01-15 tracey goto done;
3574 ec46ccd7 2020-01-15 tracey }
3575 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
3576 ec46ccd7 2020-01-15 tracey
3577 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
3578 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
3579 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
3580 ec46ccd7 2020-01-15 tracey goto done;
3581 ec46ccd7 2020-01-15 tracey
3582 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
3583 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
3584 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
3585 ec46ccd7 2020-01-15 tracey goto done;
3586 ec46ccd7 2020-01-15 tracey }
3587 ec46ccd7 2020-01-15 tracey
3588 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
3589 9a9d12ca 2020-02-06 tracey char *smallerthan, *at, *nl, *committer;
3590 9a9d12ca 2020-02-06 tracey char *lineno = NULL, *href_diff = NULL, *href_link = NULL;
3591 ec46ccd7 2020-01-15 tracey size_t len;
3592 ec46ccd7 2020-01-15 tracey
3593 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
3594 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
3595 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
3596 ec46ccd7 2020-01-15 tracey break;
3597 ec46ccd7 2020-01-15 tracey }
3598 ec46ccd7 2020-01-15 tracey
3599 ec46ccd7 2020-01-15 tracey committer = bline->committer;
3600 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
3601 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
3602 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
3603 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
3604 ec46ccd7 2020-01-15 tracey if (at)
3605 ec46ccd7 2020-01-15 tracey *at = '\0';
3606 ec46ccd7 2020-01-15 tracey len = strlen(committer);
3607 ec46ccd7 2020-01-15 tracey if (len >= 9)
3608 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
3609 ec46ccd7 2020-01-15 tracey
3610 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
3611 ec46ccd7 2020-01-15 tracey if (nl)
3612 ec46ccd7 2020-01-15 tracey *nl = '\0';
3613 0311ce2d 2020-01-15 tracey
3614 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3615 9a9d12ca 2020-02-06 tracey "blame_wrapper", KATTR__MAX);
3616 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3617 9a9d12ca 2020-02-06 tracey goto err;
3618 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3619 9a9d12ca 2020-02-06 tracey "blame_number", KATTR__MAX);
3620 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3621 9a9d12ca 2020-02-06 tracey goto err;
3622 9a9d12ca 2020-02-06 tracey if (asprintf(&lineno, "%.*d", a->nlines_prec,
3623 9a9d12ca 2020-02-06 tracey a->lineno_cur) == -1)
3624 9a9d12ca 2020-02-06 tracey goto err;
3625 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, lineno);
3626 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3627 9a9d12ca 2020-02-06 tracey goto err;
3628 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3629 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3630 9a9d12ca 2020-02-06 tracey goto err;
3631 9a9d12ca 2020-02-06 tracey
3632 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3633 9a9d12ca 2020-02-06 tracey "blame_hash", KATTR__MAX);
3634 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3635 9a9d12ca 2020-02-06 tracey goto err;
3636 1b33afc0 2020-02-14 tracey
3637 9a9d12ca 2020-02-06 tracey if (asprintf(&href_diff,
3638 fa44fa48 2020-02-17 stsp "?path=%s&action=diff&commit=%s",
3639 fa44fa48 2020-02-17 stsp a->gw_trans->repo_name, bline->id_str) == -1) {
3640 9a9d12ca 2020-02-06 tracey err = got_error_from_errno("asprintf");
3641 9a9d12ca 2020-02-06 tracey goto err;
3642 9a9d12ca 2020-02-06 tracey }
3643 9a9d12ca 2020-02-06 tracey if (asprintf(&href_link, "%.8s", bline->id_str) == -1) {
3644 9a9d12ca 2020-02-06 tracey err = got_error_from_errno("asprintf");
3645 9a9d12ca 2020-02-06 tracey goto err;
3646 9a9d12ca 2020-02-06 tracey }
3647 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_A,
3648 9a9d12ca 2020-02-06 tracey KATTR_HREF, href_diff, KATTR__MAX);
3649 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3650 9a9d12ca 2020-02-06 tracey goto done;
3651 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, href_link);
3652 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3653 9a9d12ca 2020-02-06 tracey goto err;
3654 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 2);
3655 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3656 9a9d12ca 2020-02-06 tracey goto err;
3657 2e676fc5 2020-01-15 tracey
3658 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3659 9a9d12ca 2020-02-06 tracey "blame_date", KATTR__MAX);
3660 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3661 9a9d12ca 2020-02-06 tracey goto err;
3662 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, bline->datebuf);
3663 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3664 9a9d12ca 2020-02-06 tracey goto err;
3665 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3666 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3667 9a9d12ca 2020-02-06 tracey goto err;
3668 9a9d12ca 2020-02-06 tracey
3669 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3670 9a9d12ca 2020-02-06 tracey "blame_author", KATTR__MAX);
3671 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3672 9a9d12ca 2020-02-06 tracey goto err;
3673 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, committer);
3674 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3675 9a9d12ca 2020-02-06 tracey goto err;
3676 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3677 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3678 9a9d12ca 2020-02-06 tracey goto err;
3679 9a9d12ca 2020-02-06 tracey
3680 9a9d12ca 2020-02-06 tracey kerr = khtml_attr(a->gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3681 9a9d12ca 2020-02-06 tracey "blame_code", KATTR__MAX);
3682 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3683 9a9d12ca 2020-02-06 tracey goto err;
3684 9a9d12ca 2020-02-06 tracey kerr = khtml_puts(a->gw_trans->gw_html_req, line);
3685 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3686 9a9d12ca 2020-02-06 tracey goto err;
3687 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3688 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3689 9a9d12ca 2020-02-06 tracey goto err;
3690 9a9d12ca 2020-02-06 tracey
3691 9a9d12ca 2020-02-06 tracey kerr = khtml_closeelem(a->gw_trans->gw_html_req, 1);
3692 9a9d12ca 2020-02-06 tracey if (kerr != KCGI_OK)
3693 9a9d12ca 2020-02-06 tracey goto err;
3694 9a9d12ca 2020-02-06 tracey
3695 9a9d12ca 2020-02-06 tracey a->lineno_cur++;
3696 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
3697 de6bdba4 2020-01-31 tracey err:
3698 9a9d12ca 2020-02-06 tracey free(lineno);
3699 9a9d12ca 2020-02-06 tracey free(href_diff);
3700 9a9d12ca 2020-02-06 tracey free(href_link);
3701 ec46ccd7 2020-01-15 tracey }
3702 ec46ccd7 2020-01-15 tracey done:
3703 ec46ccd7 2020-01-15 tracey if (commit)
3704 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
3705 ec46ccd7 2020-01-15 tracey free(line);
3706 9a9d12ca 2020-02-06 tracey if (err == NULL && kerr != KCGI_OK)
3707 9a9d12ca 2020-02-06 tracey err = gw_kcgi_error(kerr);
3708 ec46ccd7 2020-01-15 tracey return err;
3709 872742ce 2020-02-01 stsp }
3710 872742ce 2020-02-01 stsp
3711 a81f3554 2020-02-03 stsp static const struct got_error *
3712 9a9d12ca 2020-02-06 tracey gw_output_file_blame(struct gw_trans *gw_trans)
3713 bcbc97d8 2020-01-15 tracey {
3714 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
3715 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
3716 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
3717 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
3718 a81f3554 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
3719 147269d5 2020-01-15 tracey struct gw_blame_cb_args bca;
3720 119bf4ed 2020-01-15 tracey int i, obj_type;
3721 ec46ccd7 2020-01-15 tracey size_t filesize;
3722 ec46ccd7 2020-01-15 tracey
3723 a81f3554 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
3724 a81f3554 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3725 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
3726 a81f3554 2020-02-03 stsp gw_trans->repo_file) == -1) {
3727 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
3728 2e676fc5 2020-01-15 tracey goto done;
3729 2e676fc5 2020-01-15 tracey }
3730 2e676fc5 2020-01-15 tracey
3731 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3732 ec46ccd7 2020-01-15 tracey if (error)
3733 ec46ccd7 2020-01-15 tracey goto done;
3734 ec46ccd7 2020-01-15 tracey
3735 56997cd3 2020-02-14 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3736 deac617c 2020-02-14 tracey GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3737 ec46ccd7 2020-01-15 tracey if (error)
3738 ec46ccd7 2020-01-15 tracey goto done;
3739 ec46ccd7 2020-01-15 tracey
3740 deac617c 2020-02-14 tracey error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3741 deac617c 2020-02-14 tracey in_repo_path);
3742 ec46ccd7 2020-01-15 tracey if (error)
3743 ec46ccd7 2020-01-15 tracey goto done;
3744 2e676fc5 2020-01-15 tracey
3745 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
3746 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
3747 ec46ccd7 2020-01-15 tracey goto done;
3748 ec46ccd7 2020-01-15 tracey }
3749 ec46ccd7 2020-01-15 tracey
3750 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3751 ec46ccd7 2020-01-15 tracey if (error)
3752 ec46ccd7 2020-01-15 tracey goto done;
3753 ec46ccd7 2020-01-15 tracey
3754 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
3755 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3756 ec46ccd7 2020-01-15 tracey goto done;
3757 ec46ccd7 2020-01-15 tracey }
3758 ec46ccd7 2020-01-15 tracey
3759 deac617c 2020-02-14 tracey error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3760 ec46ccd7 2020-01-15 tracey if (error)
3761 ec46ccd7 2020-01-15 tracey goto done;
3762 ec46ccd7 2020-01-15 tracey
3763 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
3764 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
3765 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
3766 ec46ccd7 2020-01-15 tracey goto done;
3767 ec46ccd7 2020-01-15 tracey }
3768 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3769 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
3770 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
3771 ec46ccd7 2020-01-15 tracey goto done;
3772 a81f3554 2020-02-03 stsp
3773 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
3774 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
3775 ec46ccd7 2020-01-15 tracey bca.nlines--;
3776 ec46ccd7 2020-01-15 tracey
3777 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3778 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
3779 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
3780 ec46ccd7 2020-01-15 tracey goto done;
3781 ec46ccd7 2020-01-15 tracey }
3782 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
3783 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
3784 ec46ccd7 2020-01-15 tracey i = bca.nlines;
3785 ec46ccd7 2020-01-15 tracey while (i > 0) {
3786 ec46ccd7 2020-01-15 tracey i /= 10;
3787 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
3788 ec46ccd7 2020-01-15 tracey }
3789 deac617c 2020-02-14 tracey bca.repo = gw_trans->repo;
3790 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
3791 ec46ccd7 2020-01-15 tracey
3792 deac617c 2020-02-14 tracey error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
3793 deac617c 2020-02-14 tracey &bca, NULL, NULL);
3794 ec46ccd7 2020-01-15 tracey done:
3795 2e676fc5 2020-01-15 tracey free(in_repo_path);
3796 2e676fc5 2020-01-15 tracey free(commit_id);
3797 2e676fc5 2020-01-15 tracey free(obj_id);
3798 2e676fc5 2020-01-15 tracey free(path);
3799 2e676fc5 2020-01-15 tracey
3800 1b33afc0 2020-02-14 tracey if (blob) {
3801 1b33afc0 2020-02-14 tracey free(bca.line_offsets);
3802 1b33afc0 2020-02-14 tracey for (i = 0; i < bca.nlines; i++) {
3803 1b33afc0 2020-02-14 tracey struct blame_line *bline = &bca.lines[i];
3804 1b33afc0 2020-02-14 tracey free(bline->id_str);
3805 1b33afc0 2020-02-14 tracey free(bline->committer);
3806 1b33afc0 2020-02-14 tracey }
3807 1b33afc0 2020-02-14 tracey free(bca.lines);
3808 1b33afc0 2020-02-14 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
3809 1b33afc0 2020-02-14 tracey error = got_error_from_errno("fclose");
3810 2e676fc5 2020-01-15 tracey }
3811 4fd4726a 2020-02-03 stsp if (blob)
3812 4fd4726a 2020-02-03 stsp got_object_blob_close(blob);
3813 4fd4726a 2020-02-03 stsp return error;
3814 4fd4726a 2020-02-03 stsp }
3815 4fd4726a 2020-02-03 stsp
3816 4fd4726a 2020-02-03 stsp static const struct got_error *
3817 e0857cfe 2020-02-06 tracey gw_output_blob_buf(struct gw_trans *gw_trans)
3818 4fd4726a 2020-02-03 stsp {
3819 4fd4726a 2020-02-03 stsp const struct got_error *error = NULL;
3820 4fd4726a 2020-02-03 stsp struct got_object_id *obj_id = NULL;
3821 4fd4726a 2020-02-03 stsp struct got_object_id *commit_id = NULL;
3822 4fd4726a 2020-02-03 stsp struct got_blob_object *blob = NULL;
3823 4fd4726a 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
3824 e0857cfe 2020-02-06 tracey int obj_type, set_mime = 0;
3825 e0857cfe 2020-02-06 tracey size_t len, hdrlen;
3826 e0857cfe 2020-02-06 tracey const uint8_t *buf;
3827 e0857cfe 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3828 4fd4726a 2020-02-03 stsp
3829 4fd4726a 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
3830 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3831 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
3832 4fd4726a 2020-02-03 stsp gw_trans->repo_file) == -1) {
3833 4fd4726a 2020-02-03 stsp error = got_error_from_errno("asprintf");
3834 4fd4726a 2020-02-03 stsp goto done;
3835 4fd4726a 2020-02-03 stsp }
3836 4fd4726a 2020-02-03 stsp
3837 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo, path, 1);
3838 4fd4726a 2020-02-03 stsp if (error)
3839 4fd4726a 2020-02-03 stsp goto done;
3840 4fd4726a 2020-02-03 stsp
3841 56997cd3 2020-02-14 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit_id,
3842 deac617c 2020-02-14 tracey GOT_OBJ_TYPE_COMMIT, 1, gw_trans->repo);
3843 4fd4726a 2020-02-03 stsp if (error)
3844 4fd4726a 2020-02-03 stsp goto done;
3845 4fd4726a 2020-02-03 stsp
3846 deac617c 2020-02-14 tracey error = got_object_id_by_path(&obj_id, gw_trans->repo, commit_id,
3847 deac617c 2020-02-14 tracey in_repo_path);
3848 4fd4726a 2020-02-03 stsp if (error)
3849 4fd4726a 2020-02-03 stsp goto done;
3850 4fd4726a 2020-02-03 stsp
3851 4fd4726a 2020-02-03 stsp if (obj_id == NULL) {
3852 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_NO_OBJ);
3853 4fd4726a 2020-02-03 stsp goto done;
3854 4fd4726a 2020-02-03 stsp }
3855 4fd4726a 2020-02-03 stsp
3856 deac617c 2020-02-14 tracey error = got_object_get_type(&obj_type, gw_trans->repo, obj_id);
3857 4fd4726a 2020-02-03 stsp if (error)
3858 4fd4726a 2020-02-03 stsp goto done;
3859 4fd4726a 2020-02-03 stsp
3860 4fd4726a 2020-02-03 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3861 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3862 4fd4726a 2020-02-03 stsp goto done;
3863 4fd4726a 2020-02-03 stsp }
3864 4fd4726a 2020-02-03 stsp
3865 deac617c 2020-02-14 tracey error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192);
3866 4fd4726a 2020-02-03 stsp if (error)
3867 4fd4726a 2020-02-03 stsp goto done;
3868 4fd4726a 2020-02-03 stsp
3869 e0857cfe 2020-02-06 tracey hdrlen = got_object_blob_get_hdrlen(blob);
3870 e0857cfe 2020-02-06 tracey do {
3871 e0857cfe 2020-02-06 tracey error = got_object_blob_read_block(&len, blob);
3872 e0857cfe 2020-02-06 tracey if (error)
3873 e0857cfe 2020-02-06 tracey goto done;
3874 e0857cfe 2020-02-06 tracey buf = got_object_blob_get_read_buf(blob);
3875 4fd4726a 2020-02-03 stsp
3876 e0857cfe 2020-02-06 tracey /*
3877 e0857cfe 2020-02-06 tracey * Skip blob object header first time around,
3878 e0857cfe 2020-02-06 tracey * which also contains a zero byte.
3879 e0857cfe 2020-02-06 tracey */
3880 e0857cfe 2020-02-06 tracey buf += hdrlen;
3881 e0857cfe 2020-02-06 tracey if (set_mime == 0) {
3882 e0857cfe 2020-02-06 tracey if (isbinary(buf, len - hdrlen))
3883 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_APP_OCTET_STREAM;
3884 e0857cfe 2020-02-06 tracey else
3885 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
3886 e0857cfe 2020-02-06 tracey set_mime = 1;
3887 e0857cfe 2020-02-06 tracey error = gw_display_index(gw_trans);
3888 e0857cfe 2020-02-06 tracey if (error)
3889 e0857cfe 2020-02-06 tracey goto done;
3890 e0857cfe 2020-02-06 tracey }
3891 3b7fdcf4 2020-02-14 tracey kerr = khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3892 c56af3fd 2020-02-17 stsp if (kerr != KCGI_OK)
3893 c56af3fd 2020-02-17 stsp goto done;
3894 e0857cfe 2020-02-06 tracey hdrlen = 0;
3895 e0857cfe 2020-02-06 tracey } while (len != 0);
3896 4fd4726a 2020-02-03 stsp done:
3897 4fd4726a 2020-02-03 stsp free(in_repo_path);
3898 4fd4726a 2020-02-03 stsp free(commit_id);
3899 4fd4726a 2020-02-03 stsp free(obj_id);
3900 4fd4726a 2020-02-03 stsp free(path);
3901 e46f587c 2020-01-29 tracey if (blob)
3902 a81f3554 2020-02-03 stsp got_object_blob_close(blob);
3903 e0857cfe 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
3904 e0857cfe 2020-02-06 tracey error = gw_kcgi_error(kerr);
3905 a81f3554 2020-02-03 stsp return error;
3906 ec46ccd7 2020-01-15 tracey }
3907 ec46ccd7 2020-01-15 tracey
3908 84bf4df2 2020-02-03 stsp static const struct got_error *
3909 626b25b6 2020-02-06 tracey gw_output_repo_tree(struct gw_trans *gw_trans)
3910 ec46ccd7 2020-01-15 tracey {
3911 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
3912 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
3913 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
3914 626b25b6 2020-02-06 tracey char *path = NULL, *in_repo_path = NULL;
3915 84bf4df2 2020-02-03 stsp char *id_str = NULL;
3916 84bf4df2 2020-02-03 stsp char *build_folder = NULL;
3917 626b25b6 2020-02-06 tracey char *href_blob = NULL, *href_blame = NULL;
3918 84bf4df2 2020-02-03 stsp const char *class = NULL;
3919 c3bcdfd5 2020-01-29 tracey int nentries, i, class_flip = 0;
3920 626b25b6 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3921 8d4d2453 2020-01-15 tracey
3922 a942aa37 2020-02-10 tracey if (gw_trans->repo_folder != NULL) {
3923 d1635ae4 2020-02-13 tracey path = strdup(gw_trans->repo_folder);
3924 d1635ae4 2020-02-13 tracey if (path == NULL) {
3925 d1635ae4 2020-02-13 tracey error = got_error_from_errno("strdup");
3926 a942aa37 2020-02-10 tracey goto done;
3927 d1635ae4 2020-02-13 tracey }
3928 a942aa37 2020-02-10 tracey } else {
3929 deac617c 2020-02-14 tracey error = got_repo_map_path(&in_repo_path, gw_trans->repo,
3930 84bf4df2 2020-02-03 stsp gw_trans->repo_path, 1);
3931 84bf4df2 2020-02-03 stsp if (error)
3932 84bf4df2 2020-02-03 stsp goto done;
3933 bcbc97d8 2020-01-15 tracey free(path);
3934 bcbc97d8 2020-01-15 tracey path = in_repo_path;
3935 bcbc97d8 2020-01-15 tracey }
3936 bcbc97d8 2020-01-15 tracey
3937 56997cd3 2020-02-14 tracey if (gw_trans->commit_id == NULL) {
3938 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
3939 deac617c 2020-02-14 tracey error = got_ref_open(&head_ref, gw_trans->repo,
3940 deac617c 2020-02-14 tracey gw_trans->headref, 0);
3941 ec46ccd7 2020-01-15 tracey if (error)
3942 ec46ccd7 2020-01-15 tracey goto done;
3943 deac617c 2020-02-14 tracey error = got_ref_resolve(&commit_id, gw_trans->repo, head_ref);
3944 84bf4df2 2020-02-03 stsp if (error)
3945 84bf4df2 2020-02-03 stsp goto done;
3946 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
3947 56997cd3 2020-02-14 tracey /*
3948 56997cd3 2020-02-14 tracey * gw_trans->commit_id was not parsed from the querystring
3949 56997cd3 2020-02-14 tracey * we hit this code path from gw_index, where we don't know the
3950 56997cd3 2020-02-14 tracey * commit values for the tree link yet, so set
3951 56997cd3 2020-02-14 tracey * gw_trans->commit_id here to continue further into the tree
3952 56997cd3 2020-02-14 tracey */
3953 56997cd3 2020-02-14 tracey error = got_object_id_str(&gw_trans->commit_id, commit_id);
3954 56997cd3 2020-02-14 tracey if (error)
3955 56997cd3 2020-02-14 tracey goto done;
3956 ec46ccd7 2020-01-15 tracey
3957 84bf4df2 2020-02-03 stsp } else {
3958 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL,
3959 56997cd3 2020-02-14 tracey gw_trans->commit_id, GOT_OBJ_TYPE_COMMIT, 1,
3960 56997cd3 2020-02-14 tracey gw_trans->repo);
3961 84bf4df2 2020-02-03 stsp if (error)
3962 84bf4df2 2020-02-03 stsp goto done;
3963 84bf4df2 2020-02-03 stsp }
3964 f2f46662 2020-01-23 tracey
3965 56997cd3 2020-02-14 tracey error = got_object_id_by_path(&tree_id, gw_trans->repo, commit_id,
3966 56997cd3 2020-02-14 tracey path);
3967 bcbc97d8 2020-01-15 tracey if (error)
3968 bcbc97d8 2020-01-15 tracey goto done;
3969 bcbc97d8 2020-01-15 tracey
3970 deac617c 2020-02-14 tracey error = got_object_open_as_tree(&tree, gw_trans->repo, tree_id);
3971 bcbc97d8 2020-01-15 tracey if (error)
3972 bcbc97d8 2020-01-15 tracey goto done;
3973 bcbc97d8 2020-01-15 tracey
3974 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
3975 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
3976 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
3977 ec46ccd7 2020-01-15 tracey const char *modestr = "";
3978 84bf4df2 2020-02-03 stsp mode_t mode;
3979 bcbc97d8 2020-01-15 tracey
3980 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
3981 bcbc97d8 2020-01-15 tracey
3982 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3983 bcbc97d8 2020-01-15 tracey if (error)
3984 bcbc97d8 2020-01-15 tracey goto done;
3985 bcbc97d8 2020-01-15 tracey
3986 84bf4df2 2020-02-03 stsp mode = got_tree_entry_get_mode(te);
3987 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
3988 bcbc97d8 2020-01-15 tracey modestr = "$";
3989 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
3990 bcbc97d8 2020-01-15 tracey modestr = "@";
3991 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
3992 bcbc97d8 2020-01-15 tracey modestr = "/";
3993 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
3994 bcbc97d8 2020-01-15 tracey modestr = "*";
3995 c3bcdfd5 2020-01-29 tracey
3996 c3bcdfd5 2020-01-29 tracey if (class_flip == 0) {
3997 84bf4df2 2020-02-03 stsp class = "back_lightgray";
3998 c3bcdfd5 2020-01-29 tracey class_flip = 1;
3999 c3bcdfd5 2020-01-29 tracey } else {
4000 84bf4df2 2020-02-03 stsp class = "back_white";
4001 c3bcdfd5 2020-01-29 tracey class_flip = 0;
4002 c3bcdfd5 2020-01-29 tracey }
4003 bcbc97d8 2020-01-15 tracey
4004 84bf4df2 2020-02-03 stsp if (S_ISDIR(mode)) {
4005 a5594e37 2020-02-05 tracey if (asprintf(&build_folder, "%s/%s",
4006 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
4007 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te)) == -1) {
4008 53e81e48 2020-02-13 tracey error = got_error_from_errno("asprintf");
4009 84bf4df2 2020-02-03 stsp goto done;
4010 ec46ccd7 2020-01-15 tracey }
4011 626b25b6 2020-02-06 tracey if (asprintf(&href_blob,
4012 626b25b6 2020-02-06 tracey "?path=%s&action=%s&commit=%s&folder=%s",
4013 5d1296de 2020-02-13 stsp gw_trans->repo_name, gw_get_action_name(gw_trans),
4014 56997cd3 2020-02-14 tracey gw_trans->commit_id, build_folder) == -1) {
4015 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
4016 ec46ccd7 2020-01-15 tracey goto done;
4017 ec46ccd7 2020-01-15 tracey }
4018 626b25b6 2020-02-06 tracey
4019 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4020 626b25b6 2020-02-06 tracey KATTR_ID, "tree_wrapper", KATTR__MAX);
4021 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4022 626b25b6 2020-02-06 tracey goto done;
4023 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4024 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line", KATTR_CLASS, class,
4025 626b25b6 2020-02-06 tracey KATTR__MAX);
4026 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4027 626b25b6 2020-02-06 tracey goto done;
4028 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4029 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR_CLASS,
4030 626b25b6 2020-02-06 tracey "diff_directory", KATTR__MAX);
4031 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4032 626b25b6 2020-02-06 tracey goto done;
4033 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
4034 626b25b6 2020-02-06 tracey got_tree_entry_get_name(te));
4035 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4036 626b25b6 2020-02-06 tracey goto done;
4037 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4038 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4039 626b25b6 2020-02-06 tracey goto done;
4040 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4041 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4042 626b25b6 2020-02-06 tracey goto done;
4043 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4044 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line_blank", KATTR_CLASS, class,
4045 626b25b6 2020-02-06 tracey KATTR__MAX);
4046 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4047 84bf4df2 2020-02-03 stsp goto done;
4048 626b25b6 2020-02-06 tracey kerr = khtml_entity(gw_trans->gw_html_req,
4049 626b25b6 2020-02-06 tracey KENTITY_nbsp);
4050 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4051 626b25b6 2020-02-06 tracey goto done;
4052 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4053 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4054 626b25b6 2020-02-06 tracey goto done;
4055 ec46ccd7 2020-01-15 tracey } else {
4056 626b25b6 2020-02-06 tracey if (asprintf(&href_blob,
4057 626b25b6 2020-02-06 tracey "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4058 56997cd3 2020-02-14 tracey gw_trans->repo_name, "blob", gw_trans->commit_id,
4059 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
4060 626b25b6 2020-02-06 tracey gw_trans->repo_folder ?
4061 626b25b6 2020-02-06 tracey gw_trans->repo_folder : "") == -1) {
4062 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
4063 ec46ccd7 2020-01-15 tracey goto done;
4064 ec46ccd7 2020-01-15 tracey }
4065 626b25b6 2020-02-06 tracey if (asprintf(&href_blame,
4066 626b25b6 2020-02-06 tracey "?path=%s&action=%s&commit=%s&file=%s&folder=%s",
4067 56997cd3 2020-02-14 tracey gw_trans->repo_name, "blame", gw_trans->commit_id,
4068 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
4069 626b25b6 2020-02-06 tracey gw_trans->repo_folder ?
4070 626b25b6 2020-02-06 tracey gw_trans->repo_folder : "") == -1) {
4071 e46f587c 2020-01-29 tracey error = got_error_from_errno("asprintf");
4072 e46f587c 2020-01-29 tracey goto done;
4073 e46f587c 2020-01-29 tracey }
4074 ec46ccd7 2020-01-15 tracey
4075 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4076 626b25b6 2020-02-06 tracey KATTR_ID, "tree_wrapper", KATTR__MAX);
4077 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4078 626b25b6 2020-02-06 tracey goto done;
4079 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4080 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line", KATTR_CLASS, class,
4081 626b25b6 2020-02-06 tracey KATTR__MAX);
4082 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4083 626b25b6 2020-02-06 tracey goto done;
4084 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4085 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
4086 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4087 626b25b6 2020-02-06 tracey goto done;
4088 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req,
4089 626b25b6 2020-02-06 tracey got_tree_entry_get_name(te));
4090 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4091 626b25b6 2020-02-06 tracey goto done;
4092 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, modestr);
4093 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4094 626b25b6 2020-02-06 tracey goto done;
4095 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4096 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4097 626b25b6 2020-02-06 tracey goto done;
4098 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4099 626b25b6 2020-02-06 tracey KATTR_ID, "tree_line_navs", KATTR_CLASS, class,
4100 626b25b6 2020-02-06 tracey KATTR__MAX);
4101 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4102 626b25b6 2020-02-06 tracey goto done;
4103 ec46ccd7 2020-01-15 tracey
4104 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4105 626b25b6 2020-02-06 tracey KATTR_HREF, href_blob, KATTR__MAX);
4106 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4107 626b25b6 2020-02-06 tracey goto done;
4108 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "blob");
4109 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4110 626b25b6 2020-02-06 tracey goto done;
4111 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4112 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4113 626b25b6 2020-02-06 tracey goto done;
4114 626b25b6 2020-02-06 tracey
4115 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4116 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4117 626b25b6 2020-02-06 tracey goto done;
4118 626b25b6 2020-02-06 tracey
4119 626b25b6 2020-02-06 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
4120 626b25b6 2020-02-06 tracey KATTR_HREF, href_blame, KATTR__MAX);
4121 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4122 626b25b6 2020-02-06 tracey goto done;
4123 626b25b6 2020-02-06 tracey kerr = khtml_puts(gw_trans->gw_html_req, "blame");
4124 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4125 626b25b6 2020-02-06 tracey goto done;
4126 626b25b6 2020-02-06 tracey
4127 626b25b6 2020-02-06 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4128 626b25b6 2020-02-06 tracey if (kerr != KCGI_OK)
4129 626b25b6 2020-02-06 tracey goto done;
4130 626b25b6 2020-02-06 tracey }
4131 bcbc97d8 2020-01-15 tracey free(id_str);
4132 84bf4df2 2020-02-03 stsp id_str = NULL;
4133 626b25b6 2020-02-06 tracey free(href_blob);
4134 626b25b6 2020-02-06 tracey href_blob = NULL;
4135 84bf4df2 2020-02-03 stsp free(build_folder);
4136 84bf4df2 2020-02-03 stsp build_folder = NULL;
4137 bcbc97d8 2020-01-15 tracey }
4138 bcbc97d8 2020-01-15 tracey done:
4139 bcbc97d8 2020-01-15 tracey if (tree)
4140 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
4141 84bf4df2 2020-02-03 stsp free(id_str);
4142 626b25b6 2020-02-06 tracey free(href_blob);
4143 626b25b6 2020-02-06 tracey free(href_blame);
4144 2e676fc5 2020-01-15 tracey free(in_repo_path);
4145 bcbc97d8 2020-01-15 tracey free(tree_id);
4146 84bf4df2 2020-02-03 stsp free(build_folder);
4147 626b25b6 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
4148 626b25b6 2020-02-06 tracey error = gw_kcgi_error(kerr);
4149 84bf4df2 2020-02-03 stsp return error;
4150 bcbc97d8 2020-01-15 tracey }
4151 bcbc97d8 2020-01-15 tracey
4152 51d4a92d 2020-02-03 tracey static const struct got_error *
4153 9eed6f10 2020-02-08 tracey gw_output_repo_heads(struct gw_trans *gw_trans)
4154 8d4d2453 2020-01-15 tracey {
4155 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
4156 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
4157 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
4158 9eed6f10 2020-02-08 tracey char *age = NULL, *href_summary = NULL, *href_briefs = NULL;
4159 9eed6f10 2020-02-08 tracey char *href_commits = NULL;
4160 9eed6f10 2020-02-08 tracey enum kcgi_err kerr = KCGI_OK;
4161 51d4a92d 2020-02-03 tracey
4162 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
4163 87f9ebf5 2020-01-15 tracey
4164 deac617c 2020-02-14 tracey error = got_ref_list(&refs, gw_trans->repo, "refs/heads",
4165 deac617c 2020-02-14 tracey got_ref_cmp_by_name, NULL);
4166 87f9ebf5 2020-01-15 tracey if (error)
4167 87f9ebf5 2020-01-15 tracey goto done;
4168 87f9ebf5 2020-01-15 tracey
4169 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4170 9eed6f10 2020-02-08 tracey KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
4171 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4172 9eed6f10 2020-02-08 tracey goto done;
4173 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4174 9eed6f10 2020-02-08 tracey KATTR_ID, "summary_heads_title", KATTR__MAX);
4175 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4176 9eed6f10 2020-02-08 tracey goto done;
4177 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
4178 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4179 9eed6f10 2020-02-08 tracey goto done;
4180 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4181 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4182 9eed6f10 2020-02-08 tracey goto done;
4183 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4184 9eed6f10 2020-02-08 tracey KATTR_ID, "summary_heads_content", KATTR__MAX);
4185 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4186 9eed6f10 2020-02-08 tracey goto done;
4187 9eed6f10 2020-02-08 tracey
4188 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
4189 7eb2c8df 2020-02-14 stsp const char *refname;
4190 87f9ebf5 2020-01-15 tracey
4191 eb4f3ad3 2020-02-14 stsp if (got_ref_is_symbolic(re->ref))
4192 eb4f3ad3 2020-02-14 stsp continue;
4193 eb4f3ad3 2020-02-14 stsp
4194 7eb2c8df 2020-02-14 stsp refname = got_ref_get_name(re->ref);
4195 7eb2c8df 2020-02-14 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
4196 87f9ebf5 2020-01-15 tracey continue;
4197 87f9ebf5 2020-01-15 tracey
4198 017d6da3 2020-01-29 tracey error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
4199 017d6da3 2020-01-29 tracey refname, TM_DIFF);
4200 d126c1b7 2020-01-29 stsp if (error)
4201 d126c1b7 2020-01-29 stsp goto done;
4202 87f9ebf5 2020-01-15 tracey
4203 9eed6f10 2020-02-08 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
4204 9eed6f10 2020-02-08 tracey refname += 11;
4205 9eed6f10 2020-02-08 tracey
4206 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4207 9eed6f10 2020-02-08 tracey KATTR_ID, "heads_wrapper", KATTR__MAX);
4208 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4209 9eed6f10 2020-02-08 tracey goto done;
4210 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4211 9eed6f10 2020-02-08 tracey KATTR_ID, "heads_age", KATTR__MAX);
4212 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4213 9eed6f10 2020-02-08 tracey goto done;
4214 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, age ? age : "");
4215 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4216 9eed6f10 2020-02-08 tracey goto done;
4217 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4218 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4219 9eed6f10 2020-02-08 tracey goto done;
4220 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4221 9eed6f10 2020-02-08 tracey KATTR_ID, "heads_space", KATTR__MAX);
4222 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4223 9eed6f10 2020-02-08 tracey goto done;
4224 9eed6f10 2020-02-08 tracey kerr = khtml_entity(gw_trans->gw_html_req, KENTITY_nbsp);
4225 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4226 9eed6f10 2020-02-08 tracey goto done;
4227 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4228 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4229 9eed6f10 2020-02-08 tracey goto done;
4230 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
4231 9eed6f10 2020-02-08 tracey KATTR_ID, "head", KATTR__MAX);
4232 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4233 9eed6f10 2020-02-08 tracey goto done;
4234 9eed6f10 2020-02-08 tracey if (asprintf(&href_summary,
4235 9eed6f10 2020-02-08 tracey "?path=%s&action=summary&headref=%s",
4236 9eed6f10 2020-02-08 tracey gw_trans->repo_name, refname) == -1) {
4237 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
4238 87f9ebf5 2020-01-15 tracey goto done;
4239 87f9ebf5 2020-01-15 tracey }
4240 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4241 9eed6f10 2020-02-08 tracey href_summary, KATTR__MAX);
4242 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, refname);
4243 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4244 9eed6f10 2020-02-08 tracey goto done;
4245 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4246 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4247 9eed6f10 2020-02-08 tracey goto done;
4248 87f9ebf5 2020-01-15 tracey
4249 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4250 9eed6f10 2020-02-08 tracey "navs_wrapper", KATTR__MAX);
4251 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4252 9eed6f10 2020-02-08 tracey goto done;
4253 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4254 9eed6f10 2020-02-08 tracey "navs", KATTR__MAX);
4255 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4256 9eed6f10 2020-02-08 tracey goto done;
4257 87f9ebf5 2020-01-15 tracey
4258 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4259 9eed6f10 2020-02-08 tracey href_summary, KATTR__MAX);
4260 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4261 9eed6f10 2020-02-08 tracey goto done;
4262 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "summary");
4263 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4264 9eed6f10 2020-02-08 tracey goto done;
4265 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4266 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4267 9eed6f10 2020-02-08 tracey goto done;
4268 9eed6f10 2020-02-08 tracey
4269 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4270 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4271 9eed6f10 2020-02-08 tracey goto done;
4272 9eed6f10 2020-02-08 tracey if (asprintf(&href_briefs, "?path=%s&action=briefs&headref=%s",
4273 9eed6f10 2020-02-08 tracey gw_trans->repo_name, refname) == -1) {
4274 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
4275 87f9ebf5 2020-01-15 tracey goto done;
4276 87f9ebf5 2020-01-15 tracey }
4277 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4278 9eed6f10 2020-02-08 tracey href_briefs, KATTR__MAX);
4279 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4280 9eed6f10 2020-02-08 tracey goto done;
4281 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commit briefs");
4282 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4283 9eed6f10 2020-02-08 tracey goto done;
4284 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4285 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4286 9eed6f10 2020-02-08 tracey goto done;
4287 87f9ebf5 2020-01-15 tracey
4288 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
4289 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4290 9eed6f10 2020-02-08 tracey goto done;
4291 87f9ebf5 2020-01-15 tracey
4292 9eed6f10 2020-02-08 tracey if (asprintf(&href_commits,
4293 9eed6f10 2020-02-08 tracey "?path=%s&action=commits&headref=%s",
4294 9eed6f10 2020-02-08 tracey gw_trans->repo_name, refname) == -1) {
4295 9eed6f10 2020-02-08 tracey error = got_error_from_errno("asprintf");
4296 9eed6f10 2020-02-08 tracey goto done;
4297 9eed6f10 2020-02-08 tracey }
4298 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4299 9eed6f10 2020-02-08 tracey href_commits, KATTR__MAX);
4300 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4301 9eed6f10 2020-02-08 tracey goto done;
4302 9eed6f10 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, "commits");
4303 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4304 9eed6f10 2020-02-08 tracey goto done;
4305 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
4306 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4307 9eed6f10 2020-02-08 tracey goto done;
4308 87f9ebf5 2020-01-15 tracey
4309 9eed6f10 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4310 9eed6f10 2020-02-08 tracey "dotted_line", KATTR__MAX);
4311 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4312 9eed6f10 2020-02-08 tracey goto done;
4313 9eed6f10 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
4314 9eed6f10 2020-02-08 tracey if (kerr != KCGI_OK)
4315 9eed6f10 2020-02-08 tracey goto done;
4316 9eed6f10 2020-02-08 tracey free(href_summary);
4317 9eed6f10 2020-02-08 tracey href_summary = NULL;
4318 9eed6f10 2020-02-08 tracey free(href_briefs);
4319 9eed6f10 2020-02-08 tracey href_briefs = NULL;
4320 9eed6f10 2020-02-08 tracey free(href_commits);
4321 9eed6f10 2020-02-08 tracey href_commits = NULL;
4322 6c6c85af 2020-01-15 tracey }
4323 87f9ebf5 2020-01-15 tracey done:
4324 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
4325 9eed6f10 2020-02-08 tracey free(href_summary);
4326 9eed6f10 2020-02-08 tracey free(href_briefs);
4327 9eed6f10 2020-02-08 tracey free(href_commits);
4328 51d4a92d 2020-02-03 tracey return error;
4329 8d4d2453 2020-01-15 tracey }
4330 8d4d2453 2020-01-15 tracey
4331 4ae179a2 2020-02-08 tracey static const struct got_error *
4332 4ae179a2 2020-02-08 tracey gw_output_site_link(struct gw_trans *gw_trans)
4333 2c251c14 2020-01-15 tracey {
4334 4ae179a2 2020-02-08 tracey const struct got_error *error = NULL;
4335 4ae179a2 2020-02-08 tracey char *href_summary = NULL;
4336 4ae179a2 2020-02-08 tracey enum kcgi_err kerr = KCGI_OK;
4337 2c251c14 2020-01-15 tracey
4338 4ae179a2 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4339 4ae179a2 2020-02-08 tracey "site_link", KATTR__MAX);
4340 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4341 4ae179a2 2020-02-08 tracey goto done;
4342 4ae179a2 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF, GOTWEB,
4343 4ae179a2 2020-02-08 tracey KATTR__MAX);
4344 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4345 4ae179a2 2020-02-08 tracey goto done;
4346 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req,
4347 4ae179a2 2020-02-08 tracey gw_trans->gw_conf->got_site_link);
4348 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4349 4ae179a2 2020-02-08 tracey goto done;
4350 4ae179a2 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4351 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4352 4ae179a2 2020-02-08 tracey goto done;
4353 2c251c14 2020-01-15 tracey
4354 4ae179a2 2020-02-08 tracey if (gw_trans->repo_name != NULL) {
4355 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4356 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4357 4ae179a2 2020-02-08 tracey goto done;
4358 4ae179a2 2020-02-08 tracey if (asprintf(&href_summary, "?path=%s&action=summary",
4359 4ae179a2 2020-02-08 tracey gw_trans->repo_name) == -1)
4360 4ae179a2 2020-02-08 tracey goto done;
4361 4ae179a2 2020-02-08 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A, KATTR_HREF,
4362 4ae179a2 2020-02-08 tracey href_summary, KATTR__MAX);
4363 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4364 4ae179a2 2020-02-08 tracey goto done;
4365 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->repo_name);
4366 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4367 4ae179a2 2020-02-08 tracey goto done;
4368 4ae179a2 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4369 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4370 4ae179a2 2020-02-08 tracey goto done;
4371 4ae179a2 2020-02-08 tracey kerr = khtml_puts(gw_trans->gw_html_req, " / ");
4372 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4373 4ae179a2 2020-02-08 tracey goto done;
4374 5d1296de 2020-02-13 stsp kerr = khtml_puts(gw_trans->gw_html_req,
4375 5d1296de 2020-02-13 stsp gw_get_action_name(gw_trans));
4376 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4377 4ae179a2 2020-02-08 tracey goto done;
4378 eb89db64 2020-02-03 stsp }
4379 2c251c14 2020-01-15 tracey
4380 4ae179a2 2020-02-08 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
4381 4ae179a2 2020-02-08 tracey if (kerr != KCGI_OK)
4382 4ae179a2 2020-02-08 tracey goto done;
4383 4ae179a2 2020-02-08 tracey done:
4384 4ae179a2 2020-02-08 tracey free(href_summary);
4385 4ae179a2 2020-02-08 tracey return error;
4386 2c251c14 2020-01-15 tracey }
4387 2c251c14 2020-01-15 tracey
4388 83eb9a68 2020-02-03 stsp static const struct got_error *
4389 f7cc9480 2020-02-05 tracey gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
4390 ec46ccd7 2020-01-15 tracey {
4391 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
4392 f7cc9480 2020-02-05 tracey char *color = NULL;
4393 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
4394 83eb9a68 2020-02-03 stsp
4395 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
4396 ec46ccd7 2020-01-15 tracey color = "diff_minus";
4397 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "+", 1) == 0)
4398 ec46ccd7 2020-01-15 tracey color = "diff_plus";
4399 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
4400 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
4401 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
4402 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
4403 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit +", 8) == 0)
4404 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4405 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit -", 8) == 0)
4406 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4407 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob +", 6) == 0)
4408 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4409 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob -", 6) == 0)
4410 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4411 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file +", 6) == 0)
4412 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4413 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file -", 6) == 0)
4414 ec46ccd7 2020-01-15 tracey color = "diff_meta";
4415 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "from:", 5) == 0)
4416 ec46ccd7 2020-01-15 tracey color = "diff_author";
4417 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "via:", 4) == 0)
4418 ec46ccd7 2020-01-15 tracey color = "diff_author";
4419 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "date:", 5) == 0)
4420 ec46ccd7 2020-01-15 tracey color = "diff_date";
4421 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
4422 f7cc9480 2020-02-05 tracey "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
4423 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
4424 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
4425 070fee27 2020-02-03 stsp return error;
4426 2c251c14 2020-01-15 tracey }
4427 2c251c14 2020-01-15 tracey
4428 2c251c14 2020-01-15 tracey int
4429 c08369d7 2020-01-15 tracey main(int argc, char *argv[])
4430 2c251c14 2020-01-15 tracey {
4431 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
4432 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
4433 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
4434 2c251c14 2020-01-15 tracey const char *page = "index";
4435 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
4436 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
4437 2c251c14 2020-01-15 tracey
4438 54415d85 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
4439 2c251c14 2020-01-15 tracey errx(1, "malloc");
4440 2c251c14 2020-01-15 tracey
4441 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
4442 2c251c14 2020-01-15 tracey errx(1, "malloc");
4443 2c251c14 2020-01-15 tracey
4444 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
4445 2c251c14 2020-01-15 tracey errx(1, "malloc");
4446 2c251c14 2020-01-15 tracey
4447 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
4448 2c251c14 2020-01-15 tracey errx(1, "malloc");
4449 2c251c14 2020-01-15 tracey
4450 c25c2314 2020-01-28 stsp kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
4451 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK) {
4452 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
4453 c119608e 2020-01-28 stsp goto done;
4454 c25c2314 2020-01-28 stsp }
4455 2c251c14 2020-01-15 tracey
4456 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
4457 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
4458 4ceb8155 2020-01-15 tracey gw_malloc = 0;
4459 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
4460 c119608e 2020-01-28 stsp goto done;
4461 46b9c89b 2020-01-15 tracey }
4462 46b9c89b 2020-01-15 tracey
4463 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
4464 f2f46662 2020-01-23 tracey TAILQ_INIT(&gw_trans->gw_headers);
4465 2c251c14 2020-01-15 tracey
4466 f1200fe3 2020-02-13 tracey gw_trans->action = -1;
4467 2c251c14 2020-01-15 tracey gw_trans->page = 0;
4468 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
4469 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
4470 56997cd3 2020-02-14 tracey gw_trans->commit_id = NULL;
4471 55e46400 2020-02-18 tracey gw_trans->next_id = NULL;
4472 55e46400 2020-02-18 tracey gw_trans->next_prev_id = NULL;
4473 55e46400 2020-02-18 tracey gw_trans->prev_id = NULL;
4474 55e46400 2020-02-18 tracey gw_trans->prev_prev_id = NULL;
4475 742ba378 2020-02-14 stsp gw_trans->headref = GOT_REF_HEAD;
4476 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
4477 54415d85 2020-01-15 tracey gw_trans->gw_tmpl->key = gw_templs;
4478 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
4479 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
4480 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
4481 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
4482 c119608e 2020-01-28 stsp if (error)
4483 2c251c14 2020-01-15 tracey goto done;
4484 2c251c14 2020-01-15 tracey
4485 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
4486 2c251c14 2020-01-15 tracey if (error)
4487 c119608e 2020-01-28 stsp goto done;
4488 2c251c14 2020-01-15 tracey
4489 017d6da3 2020-01-29 tracey if (gw_trans->action == GW_BLOB)
4490 017d6da3 2020-01-29 tracey error = gw_blob(gw_trans);
4491 017d6da3 2020-01-29 tracey else
4492 017d6da3 2020-01-29 tracey error = gw_display_index(gw_trans);
4493 2c251c14 2020-01-15 tracey done:
4494 2c251c14 2020-01-15 tracey if (gw_malloc) {
4495 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
4496 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
4497 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
4498 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
4499 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
4500 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
4501 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
4502 56997cd3 2020-02-14 tracey free(gw_trans->commit_id);
4503 55e46400 2020-02-18 tracey free(gw_trans->next_id);
4504 55e46400 2020-02-18 tracey free(gw_trans->next_prev_id);
4505 55e46400 2020-02-18 tracey free(gw_trans->prev_id);
4506 55e46400 2020-02-18 tracey free(gw_trans->prev_prev_id);
4507 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
4508 deac617c 2020-02-14 tracey if (gw_trans->repo)
4509 deac617c 2020-02-14 tracey got_repo_close(gw_trans->repo);
4510 2c251c14 2020-01-15 tracey
4511 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
4512 2c251c14 2020-01-15 tracey free(dir->name);
4513 2c251c14 2020-01-15 tracey free(dir->description);
4514 2c251c14 2020-01-15 tracey free(dir->age);
4515 2c251c14 2020-01-15 tracey free(dir->url);
4516 2c251c14 2020-01-15 tracey free(dir->path);
4517 2c251c14 2020-01-15 tracey free(dir);
4518 2c251c14 2020-01-15 tracey }
4519 2c251c14 2020-01-15 tracey
4520 2c251c14 2020-01-15 tracey }
4521 2c251c14 2020-01-15 tracey
4522 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
4523 d56b34ca 2020-01-29 stsp return 0;
4524 2c251c14 2020-01-15 tracey }