Blame


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