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