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