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