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