Blame


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