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