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>
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.
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.
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>
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>
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>
46 2c251c14 2020-01-15 tracey #include <kcgi.h>
47 2c251c14 2020-01-15 tracey #include <kcgihtml.h>
49 474370cb 2020-01-15 tracey #include "buf.h"
50 2c251c14 2020-01-15 tracey #include "gotweb.h"
51 2c251c14 2020-01-15 tracey #include "gotweb_ui.h"
53 2c251c14 2020-01-15 tracey #ifndef nitems
54 2c251c14 2020-01-15 tracey #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 54415d85 2020-01-15 tracey struct gw_trans {
58 f2f46662 2020-01-23 tracey TAILQ_HEAD(headers, gw_header) gw_headers;
59 f2f46662 2020-01-23 tracey TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir;
61 2c251c14 2020-01-15 tracey struct gotweb_conf *gw_conf;
62 2c251c14 2020-01-15 tracey struct ktemplate *gw_tmpl;
63 2c251c14 2020-01-15 tracey struct khtmlreq *gw_html_req;
64 2c251c14 2020-01-15 tracey struct kreq *gw_req;
65 2c251c14 2020-01-15 tracey char *repo_name;
66 2c251c14 2020-01-15 tracey char *repo_path;
67 2c251c14 2020-01-15 tracey char *commit;
68 2c251c14 2020-01-15 tracey char *repo_file;
69 ec46ccd7 2020-01-15 tracey char *repo_folder;
70 2c251c14 2020-01-15 tracey char *action_name;
71 8087c3c5 2020-01-15 tracey char *headref;
72 2c251c14 2020-01-15 tracey unsigned int action;
73 2c251c14 2020-01-15 tracey unsigned int page;
74 2c251c14 2020-01-15 tracey unsigned int repos_total;
75 387a29ba 2020-01-15 tracey enum kmime mime;
78 f2f46662 2020-01-23 tracey struct gw_header {
79 f2f46662 2020-01-23 tracey TAILQ_ENTRY(gw_header) entry;
80 f2f46662 2020-01-23 tracey struct got_repository *repo;
81 f2f46662 2020-01-23 tracey struct got_reflist_head refs;
82 f2f46662 2020-01-23 tracey struct got_commit_object *commit;
83 f2f46662 2020-01-23 tracey struct got_object_id *id;
84 f2f46662 2020-01-23 tracey char *path;
86 f2f46662 2020-01-23 tracey char *refs_str;
87 f2f46662 2020-01-23 tracey char *commit_id; /* id_str1 */
88 f2f46662 2020-01-23 tracey char *parent_id; /* id_str2 */
89 f2f46662 2020-01-23 tracey char *tree_id;
90 bd275801 2020-02-03 tracey const char *author;
91 27192be7 2020-02-04 tracey const char *committer;
92 f2f46662 2020-01-23 tracey char *commit_msg;
93 f2f46662 2020-01-23 tracey time_t committer_time;
96 2c251c14 2020-01-15 tracey struct gw_dir {
97 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
98 2c251c14 2020-01-15 tracey char *name;
99 2c251c14 2020-01-15 tracey char *owner;
100 2c251c14 2020-01-15 tracey char *description;
101 2c251c14 2020-01-15 tracey char *url;
102 2c251c14 2020-01-15 tracey char *age;
103 2c251c14 2020-01-15 tracey char *path;
106 f2f46662 2020-01-23 tracey enum gw_key {
107 f2f46662 2020-01-23 tracey KEY_ACTION,
108 f2f46662 2020-01-23 tracey KEY_COMMIT_ID,
109 f2f46662 2020-01-23 tracey KEY_FILE,
110 f2f46662 2020-01-23 tracey KEY_FOLDER,
111 f2f46662 2020-01-23 tracey KEY_HEADREF,
112 f2f46662 2020-01-23 tracey KEY_PAGE,
113 f2f46662 2020-01-23 tracey KEY_PATH,
114 f2f46662 2020-01-23 tracey KEY__ZMAX
117 54415d85 2020-01-15 tracey enum gw_tmpl {
118 f2f46662 2020-01-23 tracey TEMPL_CONTENT,
119 2c251c14 2020-01-15 tracey TEMPL_HEAD,
120 2c251c14 2020-01-15 tracey TEMPL_HEADER,
121 f2f46662 2020-01-23 tracey TEMPL_SEARCH,
122 2c251c14 2020-01-15 tracey TEMPL_SITEPATH,
123 2c251c14 2020-01-15 tracey TEMPL_SITEOWNER,
124 2c251c14 2020-01-15 tracey TEMPL_TITLE,
125 2c251c14 2020-01-15 tracey TEMPL__MAX
128 54415d85 2020-01-15 tracey enum gw_ref_tm {
133 54415d85 2020-01-15 tracey enum gw_tags {
134 87f9ebf5 2020-01-15 tracey TAGBRIEF,
138 54415d85 2020-01-15 tracey static const char *const gw_templs[TEMPL__MAX] = {
139 f2f46662 2020-01-23 tracey "content",
141 2c251c14 2020-01-15 tracey "header",
142 f2f46662 2020-01-23 tracey "search",
143 2c251c14 2020-01-15 tracey "sitepath",
144 2c251c14 2020-01-15 tracey "siteowner",
148 ec46ccd7 2020-01-15 tracey static const struct kvalid gw_keys[KEY__ZMAX] = {
149 2c251c14 2020-01-15 tracey { kvalid_stringne, "action" },
150 2c251c14 2020-01-15 tracey { kvalid_stringne, "commit" },
151 2c251c14 2020-01-15 tracey { kvalid_stringne, "file" },
152 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "folder" },
153 8087c3c5 2020-01-15 tracey { kvalid_stringne, "headref" },
154 ec46ccd7 2020-01-15 tracey { kvalid_int, "page" },
155 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "path" },
158 2c251c14 2020-01-15 tracey static struct gw_dir *gw_init_gw_dir(char *);
159 f2f46662 2020-01-23 tracey static struct gw_header *gw_init_header(void);
161 185ba3ba 2020-02-04 tracey static const struct got_error *gw_get_repo_description(char **,
162 185ba3ba 2020-02-04 tracey struct gw_trans *, char *);
163 9a1cc63f 2020-02-03 stsp static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
165 55ccf528 2020-02-03 stsp static const struct got_error *gw_get_time_str(char **, time_t, int);
166 d126c1b7 2020-01-29 stsp static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 387a29ba 2020-01-15 tracey char *, char *, int);
168 185ba3ba 2020-02-04 tracey static const struct got_error *gw_get_file_blame_blob(char **,
169 185ba3ba 2020-02-04 tracey struct gw_trans *);
170 e0857cfe 2020-02-06 tracey static const struct got_error *gw_output_blob_buf(struct gw_trans *);
171 84bf4df2 2020-02-03 stsp static const struct got_error *gw_get_repo_tree(char **, struct gw_trans *);
172 f7cc9480 2020-02-05 tracey static const struct got_error *gw_get_diff(struct gw_trans *,
173 f2f46662 2020-01-23 tracey struct gw_header *);
174 6eccd105 2020-02-03 stsp static const struct got_error *gw_get_repo_tags(char **, struct gw_trans *,
175 4ff7391f 2020-01-28 tracey struct gw_header *, int, int);
176 51d4a92d 2020-02-03 tracey static const struct got_error *gw_get_repo_heads(char **, struct gw_trans *);
177 185ba3ba 2020-02-04 tracey static const struct got_error *gw_get_clone_url(char **, struct gw_trans *,
179 54415d85 2020-01-15 tracey static char *gw_get_site_link(struct gw_trans *);
180 070fee27 2020-02-03 stsp static const struct got_error *gw_html_escape(char **, const char *);
181 f7cc9480 2020-02-05 tracey static const struct got_error *gw_colordiff_line(struct gw_trans *, char *);
183 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_commit_header(struct gw_trans *, char *,
185 21a55cc7 2020-02-04 tracey static char *gw_gen_commit_header_old(char *, char*);
186 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_diff_header(struct gw_trans *, char *,
188 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_author_header(struct gw_trans *,
189 21a55cc7 2020-02-04 tracey const char *);
190 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_age_header(struct gw_trans *,
191 21a55cc7 2020-02-04 tracey const char *);
192 21a55cc7 2020-02-04 tracey static const struct got_error *gw_gen_committer_header(struct gw_trans *,
193 21a55cc7 2020-02-04 tracey const char *);
194 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_commit_msg_header(struct gw_trans*,
196 f7cc9480 2020-02-05 tracey static char *gw_gen_commit_msg_header_old(char *);
197 f7cc9480 2020-02-05 tracey static const struct got_error *gw_gen_tree_header(struct gw_trans *, char *);
199 f2f46662 2020-01-23 tracey static void gw_free_headers(struct gw_header *);
200 c25c2314 2020-01-28 stsp static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
201 2c251c14 2020-01-15 tracey enum kmime);
202 6d8d8a26 2020-01-29 stsp static const struct got_error* gw_display_index(struct gw_trans *);
203 6d8d8a26 2020-01-29 stsp static void gw_display_error(struct gw_trans *,
204 2c251c14 2020-01-15 tracey const struct got_error *);
206 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
208 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_header(struct gw_trans *,
209 f2f46662 2020-01-23 tracey struct gw_header *, int);
210 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commits(struct gw_trans *,
211 f2f46662 2020-01-23 tracey struct gw_header *, int);
212 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commit(struct gw_trans *,
213 f2f46662 2020-01-23 tracey struct gw_header *);
214 54415d85 2020-01-15 tracey static const struct got_error* gw_apply_unveil(const char *, const char *);
215 147269d5 2020-01-15 tracey static const struct got_error* gw_blame_cb(void *, int, int,
216 ec46ccd7 2020-01-15 tracey struct got_object_id *);
217 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct gw_trans *);
218 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct gw_trans *,
219 2c251c14 2020-01-15 tracey struct gw_dir *);
220 54415d85 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct gw_trans *);
222 54415d85 2020-01-15 tracey static const struct got_error* gw_blame(struct gw_trans *);
223 e46f587c 2020-01-29 tracey static const struct got_error* gw_blob(struct gw_trans *);
224 f2f46662 2020-01-23 tracey static const struct got_error* gw_diff(struct gw_trans *);
225 54415d85 2020-01-15 tracey static const struct got_error* gw_index(struct gw_trans *);
226 f2f46662 2020-01-23 tracey static const struct got_error* gw_commits(struct gw_trans *);
227 f2f46662 2020-01-23 tracey static const struct got_error* gw_briefs(struct gw_trans *);
228 54415d85 2020-01-15 tracey static const struct got_error* gw_summary(struct gw_trans *);
229 54415d85 2020-01-15 tracey static const struct got_error* gw_tree(struct gw_trans *);
230 4ff7391f 2020-01-28 tracey static const struct got_error* gw_tag(struct gw_trans *);
232 2c251c14 2020-01-15 tracey struct gw_query_action {
233 2c251c14 2020-01-15 tracey unsigned int func_id;
234 2c251c14 2020-01-15 tracey const char *func_name;
235 54415d85 2020-01-15 tracey const struct got_error *(*func_main)(struct gw_trans *);
236 2c251c14 2020-01-15 tracey char *template;
239 2c251c14 2020-01-15 tracey enum gw_query_actions {
240 2c251c14 2020-01-15 tracey GW_BLAME,
242 f2f46662 2020-01-23 tracey GW_BRIEFS,
243 f2f46662 2020-01-23 tracey GW_COMMITS,
246 2c251c14 2020-01-15 tracey GW_INDEX,
247 2c251c14 2020-01-15 tracey GW_SUMMARY,
252 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
253 f2f46662 2020-01-23 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
254 017d6da3 2020-01-29 tracey { GW_BLOB, "blob", NULL, NULL },
255 f2f46662 2020-01-23 tracey { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
256 f2f46662 2020-01-23 tracey { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
257 f2f46662 2020-01-23 tracey { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
258 f2f46662 2020-01-23 tracey { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
259 f2f46662 2020-01-23 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
260 f2f46662 2020-01-23 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
261 4ff7391f 2020-01-28 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
262 f2f46662 2020-01-23 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
265 c25c2314 2020-01-28 stsp static const struct got_error *
266 c25c2314 2020-01-28 stsp gw_kcgi_error(enum kcgi_err kerr)
268 c25c2314 2020-01-28 stsp if (kerr == KCGI_OK)
269 c25c2314 2020-01-28 stsp return NULL;
271 c25c2314 2020-01-28 stsp if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
272 c25c2314 2020-01-28 stsp return got_error(GOT_ERR_CANCELLED);
274 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENOMEM)
275 f7cc9480 2020-02-05 tracey return got_error_set_errno(ENOMEM,
276 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
278 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENFILE)
279 f7cc9480 2020-02-05 tracey return got_error_set_errno(ENFILE,
280 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
282 c25c2314 2020-01-28 stsp if (kerr == KCGI_EAGAIN)
283 f7cc9480 2020-02-05 tracey return got_error_set_errno(EAGAIN,
284 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
286 c25c2314 2020-01-28 stsp if (kerr == KCGI_FORM)
287 f7cc9480 2020-02-05 tracey return got_error_msg(GOT_ERR_IO,
288 7afec891 2020-02-06 stsp kcgi_strerror(kerr));
290 7afec891 2020-02-06 stsp return got_error_from_errno(kcgi_strerror(kerr));
293 2c251c14 2020-01-15 tracey static const struct got_error *
294 54415d85 2020-01-15 tracey gw_apply_unveil(const char *repo_path, const char *repo_file)
296 2c251c14 2020-01-15 tracey const struct got_error *err;
298 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
299 2c251c14 2020-01-15 tracey char *full_path;
300 88d59c36 2020-01-29 stsp if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
301 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
302 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
303 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
306 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
307 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
309 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
310 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
312 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
313 2c251c14 2020-01-15 tracey if (err != NULL)
314 2c251c14 2020-01-15 tracey return err;
316 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
317 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
319 2c251c14 2020-01-15 tracey return NULL;
322 2c251c14 2020-01-15 tracey static const struct got_error *
323 32cd4d18 2020-02-03 stsp gw_empty_string(char **s)
325 32cd4d18 2020-02-03 stsp *s = strdup("");
326 32cd4d18 2020-02-03 stsp if (*s == NULL)
327 32cd4d18 2020-02-03 stsp return got_error_from_errno("strdup");
328 32cd4d18 2020-02-03 stsp return NULL;
332 e0857cfe 2020-02-06 tracey isbinary(const uint8_t *buf, size_t n)
334 e0857cfe 2020-02-06 tracey size_t i;
336 e0857cfe 2020-02-06 tracey for (i = 0; i < n; i++)
337 e0857cfe 2020-02-06 tracey if (buf[i] == 0)
338 e0857cfe 2020-02-06 tracey return 1;
339 e0857cfe 2020-02-06 tracey return 0;
342 32cd4d18 2020-02-03 stsp static const struct got_error *
343 54415d85 2020-01-15 tracey gw_blame(struct gw_trans *gw_trans)
345 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
346 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
347 f2f46662 2020-01-23 tracey char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
348 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
349 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
351 6bee13de 2020-01-16 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
352 f2f46662 2020-01-23 tracey NULL) == -1)
353 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
355 f2f46662 2020-01-23 tracey if ((header = gw_init_header()) == NULL)
356 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
358 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
359 ec46ccd7 2020-01-15 tracey if (error)
362 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
363 f2f46662 2020-01-23 tracey if (error)
366 a81f3554 2020-02-03 stsp error = gw_get_file_blame_blob(&blame_html, gw_trans);
370 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, header->committer_time, TM_LONG);
373 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
374 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
378 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
381 55ccf528 2020-02-03 stsp if (asprintf(&blame_html_disp, blame_header, age_html,
382 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(escaped_commit_msg), blame_html) == -1) {
383 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
387 6d9fc692 2020-01-28 stsp if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
388 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
392 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, blame);
393 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
394 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
396 f2f46662 2020-01-23 tracey got_ref_list_free(&header->refs);
397 f2f46662 2020-01-23 tracey gw_free_headers(header);
398 f2f46662 2020-01-23 tracey free(blame_html_disp);
399 f2f46662 2020-01-23 tracey free(blame_html);
400 f2f46662 2020-01-23 tracey free(blame);
401 070fee27 2020-02-03 stsp free(escaped_commit_msg);
402 2c251c14 2020-01-15 tracey return error;
405 2c251c14 2020-01-15 tracey static const struct got_error *
406 e46f587c 2020-01-29 tracey gw_blob(struct gw_trans *gw_trans)
408 e46f587c 2020-01-29 tracey const struct got_error *error = NULL;
409 e46f587c 2020-01-29 tracey struct gw_header *header = NULL;
411 e46f587c 2020-01-29 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
412 e46f587c 2020-01-29 tracey NULL) == -1)
413 e46f587c 2020-01-29 tracey return got_error_from_errno("pledge");
415 e46f587c 2020-01-29 tracey if ((header = gw_init_header()) == NULL)
416 e46f587c 2020-01-29 tracey return got_error_from_errno("malloc");
418 e46f587c 2020-01-29 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
419 e46f587c 2020-01-29 tracey if (error)
420 e46f587c 2020-01-29 tracey goto done;
422 e46f587c 2020-01-29 tracey error = gw_get_header(gw_trans, header, 1);
423 e46f587c 2020-01-29 tracey if (error)
424 e46f587c 2020-01-29 tracey goto done;
426 e0857cfe 2020-02-06 tracey error = gw_output_blob_buf(gw_trans);
428 e46f587c 2020-01-29 tracey got_ref_list_free(&header->refs);
429 e46f587c 2020-01-29 tracey gw_free_headers(header);
430 e46f587c 2020-01-29 tracey return error;
433 e46f587c 2020-01-29 tracey static const struct got_error *
434 f2f46662 2020-01-23 tracey gw_diff(struct gw_trans *gw_trans)
436 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
437 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
438 f7cc9480 2020-02-05 tracey char *age = NULL, *escaped_commit_msg = NULL;
439 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
441 f2f46662 2020-01-23 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
442 f2f46662 2020-01-23 tracey NULL) == -1)
443 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
445 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
446 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
448 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
449 8087c3c5 2020-01-15 tracey if (error)
452 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
453 f2f46662 2020-01-23 tracey if (error)
456 f7cc9480 2020-02-05 tracey /* diff header */
457 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
458 f7cc9480 2020-02-05 tracey "diff_header_wrapper", KATTR__MAX);
459 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
460 f7cc9480 2020-02-05 tracey goto done;
461 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
462 f7cc9480 2020-02-05 tracey "diff_header", KATTR__MAX);
463 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
464 f7cc9480 2020-02-05 tracey goto done;
465 f7cc9480 2020-02-05 tracey error = gw_gen_diff_header(gw_trans, header->parent_id,
466 f7cc9480 2020-02-05 tracey header->commit_id);
467 f7cc9480 2020-02-05 tracey if (error)
468 f7cc9480 2020-02-05 tracey goto done;
469 f7cc9480 2020-02-05 tracey error = gw_gen_commit_header(gw_trans, header->commit_id,
470 f7cc9480 2020-02-05 tracey header->refs_str);
471 f7cc9480 2020-02-05 tracey if (error)
472 f7cc9480 2020-02-05 tracey goto done;
473 f7cc9480 2020-02-05 tracey error = gw_gen_tree_header(gw_trans, header->tree_id);
474 f7cc9480 2020-02-05 tracey if (error)
475 f7cc9480 2020-02-05 tracey goto done;
476 f7cc9480 2020-02-05 tracey error = gw_gen_author_header(gw_trans, header->author);
477 f7cc9480 2020-02-05 tracey if (error)
478 f7cc9480 2020-02-05 tracey goto done;
479 f7cc9480 2020-02-05 tracey error = gw_gen_committer_header(gw_trans, header->author);
480 f7cc9480 2020-02-05 tracey if (error)
481 f7cc9480 2020-02-05 tracey goto done;
482 f7cc9480 2020-02-05 tracey error = gw_get_time_str(&age, header->committer_time,
483 f7cc9480 2020-02-05 tracey TM_LONG);
484 f7cc9480 2020-02-05 tracey if (error)
485 f7cc9480 2020-02-05 tracey goto done;
486 f7cc9480 2020-02-05 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
487 f7cc9480 2020-02-05 tracey if (error)
488 f7cc9480 2020-02-05 tracey goto done;
490 f7cc9480 2020-02-05 tracey * XXX: keeping this for now, since kcgihtml does not convert
491 f7cc9480 2020-02-05 tracey * \n into <br /> yet.
493 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
496 f7cc9480 2020-02-05 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
497 f7cc9480 2020-02-05 tracey if (error)
499 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
500 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
502 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
503 f7cc9480 2020-02-05 tracey "dotted_line", KATTR__MAX);
504 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
505 f7cc9480 2020-02-05 tracey goto done;
506 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
507 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
508 f7cc9480 2020-02-05 tracey goto done;
510 f7cc9480 2020-02-05 tracey /* diff */
511 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
512 f7cc9480 2020-02-05 tracey "diff", KATTR__MAX);
513 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
514 f7cc9480 2020-02-05 tracey goto done;
515 f7cc9480 2020-02-05 tracey error = gw_get_diff(gw_trans, header);
516 f7cc9480 2020-02-05 tracey if (error)
517 f7cc9480 2020-02-05 tracey goto done;
519 f7cc9480 2020-02-05 tracey /* diff content close */
520 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
521 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
522 f7cc9480 2020-02-05 tracey goto done;
524 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
525 f2f46662 2020-01-23 tracey gw_free_headers(header);
527 070fee27 2020-02-03 stsp free(escaped_commit_msg);
528 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
529 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
530 2204c934 2020-01-15 tracey return error;
533 2204c934 2020-01-15 tracey static const struct got_error *
534 54415d85 2020-01-15 tracey gw_index(struct gw_trans *gw_trans)
536 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
537 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
538 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
539 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
540 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
542 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
543 6bee13de 2020-01-16 tracey NULL) == -1) {
544 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
545 6bee13de 2020-01-16 tracey return error;
548 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
549 46b9c89b 2020-01-15 tracey if (error)
550 46b9c89b 2020-01-15 tracey return error;
552 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
553 46b9c89b 2020-01-15 tracey if (error)
554 2c251c14 2020-01-15 tracey return error;
556 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
557 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
558 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
560 bce5dac1 2020-01-28 stsp if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
561 bce5dac1 2020-01-28 stsp if (asprintf(&html, index_projects_empty,
562 bce5dac1 2020-01-28 stsp gw_trans->gw_conf->got_repos_path) == -1)
563 bce5dac1 2020-01-28 stsp return got_error_from_errno("asprintf");
564 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
565 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
566 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
567 bce5dac1 2020-01-28 stsp free(html);
568 c25c2314 2020-01-28 stsp return error;
571 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
574 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
575 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
576 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
577 2c251c14 2020-01-15 tracey prev_disp++;
578 2c251c14 2020-01-15 tracey continue;
581 2c251c14 2020-01-15 tracey prev_disp++;
583 f2f46662 2020-01-23 tracey if (error)
584 f2f46662 2020-01-23 tracey return error;
585 88d59c36 2020-01-29 stsp if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
586 88d59c36 2020-01-29 stsp gw_dir->name, gw_dir->name) == -1)
587 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
589 88d59c36 2020-01-29 stsp if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
590 9a1cc63f 2020-02-03 stsp gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
591 9a1cc63f 2020-02-03 stsp gw_dir->age,
592 88d59c36 2020-01-29 stsp navs) == -1)
593 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
595 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
596 2c251c14 2020-01-15 tracey free(navs);
597 2c251c14 2020-01-15 tracey free(html);
598 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
599 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
601 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
602 2c251c14 2020-01-15 tracey continue;
604 c25c2314 2020-01-28 stsp if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
605 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
606 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
607 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
608 c25c2314 2020-01-28 stsp } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
609 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
610 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
611 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
612 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
613 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
614 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
617 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
618 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
619 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
620 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
621 88d59c36 2020-01-29 stsp if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
622 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
623 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, prev);
624 2c251c14 2020-01-15 tracey free(prev);
625 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
626 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
629 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
630 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
631 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
633 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
634 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
635 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
636 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
637 88d59c36 2020-01-29 stsp if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
638 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
639 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, next);
640 2c251c14 2020-01-15 tracey free(next);
641 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
642 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
643 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
644 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
645 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
646 2c251c14 2020-01-15 tracey next_disp = 0;
650 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
651 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
652 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
653 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
654 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
655 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
656 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
659 2c251c14 2020-01-15 tracey next_disp++;
661 2c251c14 2020-01-15 tracey return error;
664 2c251c14 2020-01-15 tracey static const struct got_error *
665 f2f46662 2020-01-23 tracey gw_commits(struct gw_trans *gw_trans)
667 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
668 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
669 f7cc9480 2020-02-05 tracey char *age = NULL, *escaped_commit_msg = NULL;
670 21a55cc7 2020-02-04 tracey char *href_diff = NULL, *href_tree = NULL;
671 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
673 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
674 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
676 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
677 6bee13de 2020-01-16 tracey NULL) == -1) {
678 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
682 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
683 8087c3c5 2020-01-15 tracey if (error)
686 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
687 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
691 21a55cc7 2020-02-04 tracey /* commit content */
692 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
693 21a55cc7 2020-02-04 tracey /* commit line */
694 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
695 21a55cc7 2020-02-04 tracey "commits_line_wrapper", KATTR__MAX);
696 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
698 21a55cc7 2020-02-04 tracey error = gw_gen_commit_header(gw_trans, n_header->commit_id,
699 21a55cc7 2020-02-04 tracey n_header->refs_str);
700 f7cc9480 2020-02-05 tracey if (error)
701 f7cc9480 2020-02-05 tracey goto done;
702 21a55cc7 2020-02-04 tracey error = gw_gen_author_header(gw_trans, n_header->author);
703 21a55cc7 2020-02-04 tracey if (error)
704 21a55cc7 2020-02-04 tracey goto done;
705 21a55cc7 2020-02-04 tracey error = gw_gen_committer_header(gw_trans, n_header->author);
706 21a55cc7 2020-02-04 tracey if (error)
707 21a55cc7 2020-02-04 tracey goto done;
708 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, n_header->committer_time,
712 21a55cc7 2020-02-04 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
713 21a55cc7 2020-02-04 tracey if (error)
715 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
716 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
717 21a55cc7 2020-02-04 tracey goto done;
719 21a55cc7 2020-02-04 tracey /* dotted line */
720 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
721 21a55cc7 2020-02-04 tracey "dotted_line", KATTR__MAX);
722 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
723 21a55cc7 2020-02-04 tracey goto done;
724 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
725 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
726 21a55cc7 2020-02-04 tracey goto done;
728 21a55cc7 2020-02-04 tracey /* commit */
729 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
730 21a55cc7 2020-02-04 tracey "commit", KATTR__MAX);
731 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
732 21a55cc7 2020-02-04 tracey goto done;
734 21a55cc7 2020-02-04 tracey * XXX: keeping this for now, since kcgihtml does not convert
735 21a55cc7 2020-02-04 tracey * \n into <br /> yet.
737 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg,
738 070fee27 2020-02-03 stsp n_header->commit_msg);
741 21a55cc7 2020-02-04 tracey kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
742 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
743 21a55cc7 2020-02-04 tracey goto done;
744 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
745 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
746 21a55cc7 2020-02-04 tracey goto done;
748 21a55cc7 2020-02-04 tracey /* navs */
750 21a55cc7 2020-02-04 tracey /* XXX: create gen code for this */
751 21a55cc7 2020-02-04 tracey /* build diff nav */
752 21a55cc7 2020-02-04 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
753 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
754 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
757 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
758 21a55cc7 2020-02-04 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
759 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
760 21a55cc7 2020-02-04 tracey goto done;
761 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
762 21a55cc7 2020-02-04 tracey KATTR_ID, "navs", KATTR__MAX);
763 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
764 21a55cc7 2020-02-04 tracey goto done;
765 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
766 21a55cc7 2020-02-04 tracey KATTR_HREF, href_diff, KATTR__MAX);
767 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
768 21a55cc7 2020-02-04 tracey goto done;
769 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "diff");
770 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
771 21a55cc7 2020-02-04 tracey goto done;
772 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
773 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
774 21a55cc7 2020-02-04 tracey goto done;
776 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
777 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
778 21a55cc7 2020-02-04 tracey goto done;
780 21a55cc7 2020-02-04 tracey /* XXX: create gen code for this */
781 21a55cc7 2020-02-04 tracey /* build tree nav */
782 21a55cc7 2020-02-04 tracey if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
783 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
784 21a55cc7 2020-02-04 tracey error = got_error_from_errno("asprintf");
785 21a55cc7 2020-02-04 tracey goto done;
787 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
788 21a55cc7 2020-02-04 tracey KATTR_HREF, href_tree, KATTR__MAX);
789 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
790 21a55cc7 2020-02-04 tracey goto done;
791 21a55cc7 2020-02-04 tracey khtml_puts(gw_trans->gw_html_req, "tree");
792 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
793 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
794 21a55cc7 2020-02-04 tracey goto done;
795 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
796 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
797 21a55cc7 2020-02-04 tracey goto done;
799 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
800 21a55cc7 2020-02-04 tracey "solid_line", KATTR__MAX);
801 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
802 21a55cc7 2020-02-04 tracey goto done;
803 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
804 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
805 21a55cc7 2020-02-04 tracey goto done;
808 55ccf528 2020-02-03 stsp age = NULL;
809 070fee27 2020-02-03 stsp free(escaped_commit_msg);
810 070fee27 2020-02-03 stsp escaped_commit_msg = NULL;
813 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
814 f2f46662 2020-01-23 tracey gw_free_headers(header);
815 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
816 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
818 21a55cc7 2020-02-04 tracey free(href_diff);
819 21a55cc7 2020-02-04 tracey free(href_tree);
820 070fee27 2020-02-03 stsp free(escaped_commit_msg);
821 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
822 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
823 2c251c14 2020-01-15 tracey return error;
826 2c251c14 2020-01-15 tracey static const struct got_error *
827 f2f46662 2020-01-23 tracey gw_briefs(struct gw_trans *gw_trans)
829 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
830 bd275801 2020-02-03 tracey struct gw_header *header = NULL, *n_header = NULL;
831 bd275801 2020-02-03 tracey char *age = NULL, *age_html = NULL;
832 bd275801 2020-02-03 tracey char *href_diff = NULL, *href_tree = NULL;
833 bd275801 2020-02-03 tracey char *newline, *smallerthan;
834 ef72fe2c 2020-02-04 stsp enum kcgi_err kerr = KCGI_OK;
836 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
837 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
839 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
840 6bee13de 2020-01-16 tracey NULL) == -1) {
841 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
845 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
846 8087c3c5 2020-01-15 tracey if (error)
849 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_SUMMARY)
850 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
852 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
853 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
857 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
858 bd275801 2020-02-03 tracey error = gw_get_time_str(&age, n_header->committer_time,
859 bd275801 2020-02-03 tracey TM_DIFF);
860 bd275801 2020-02-03 tracey if (error)
861 bd275801 2020-02-03 tracey goto done;
863 bd275801 2020-02-03 tracey /* briefs wrapper */
864 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
865 bd275801 2020-02-03 tracey KATTR_ID, "briefs_wrapper", KATTR__MAX);
866 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
867 bd275801 2020-02-03 tracey goto done;
869 bd275801 2020-02-03 tracey /* briefs age */
870 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
871 bd275801 2020-02-03 tracey KATTR_ID, "briefs_age", KATTR__MAX);
872 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
873 bd275801 2020-02-03 tracey goto done;
874 bd275801 2020-02-03 tracey if (asprintf(&age_html, "%s", age ? age : "") == -1) {
875 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
878 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, age_html);
879 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
880 bd275801 2020-02-03 tracey goto done;
881 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
882 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
883 bd275801 2020-02-03 tracey goto done;
885 bd275801 2020-02-03 tracey /* briefs author */
886 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
887 bd275801 2020-02-03 tracey KATTR_ID, "briefs_author", KATTR__MAX);
888 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
889 bd275801 2020-02-03 tracey goto done;
890 bd275801 2020-02-03 tracey smallerthan = strchr(n_header->author, '<');
891 bd275801 2020-02-03 tracey if (smallerthan)
892 bd275801 2020-02-03 tracey *smallerthan = '\0';
893 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
894 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
895 bd275801 2020-02-03 tracey goto done;
896 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
897 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
898 bd275801 2020-02-03 tracey goto done;
900 bd275801 2020-02-03 tracey /* briefs log */
901 52f8346c 2020-02-03 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
902 52f8346c 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
903 52f8346c 2020-02-03 tracey error = got_error_from_errno("asprintf");
904 52f8346c 2020-02-03 tracey goto done;
906 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
907 bd275801 2020-02-03 tracey KATTR_ID, "briefs_log", KATTR__MAX);
908 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
909 bd275801 2020-02-03 tracey goto done;
910 f2f46662 2020-01-23 tracey newline = strchr(n_header->commit_msg, '\n');
911 f2f46662 2020-01-23 tracey if (newline)
912 f2f46662 2020-01-23 tracey *newline = '\0';
913 52f8346c 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
914 52f8346c 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
915 52f8346c 2020-02-03 tracey if (kerr != KCGI_OK)
916 52f8346c 2020-02-03 tracey goto done;
917 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
918 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
920 52f8346c 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
921 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
922 bd275801 2020-02-03 tracey goto done;
924 bd275801 2020-02-03 tracey /* build diff nav */
925 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
926 bd275801 2020-02-03 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
927 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
928 bd275801 2020-02-03 tracey goto done;
929 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
930 bd275801 2020-02-03 tracey KATTR_ID, "navs", KATTR__MAX);
931 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
932 bd275801 2020-02-03 tracey goto done;
933 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
934 bd275801 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
935 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
937 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "diff");
938 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
939 bd275801 2020-02-03 tracey goto done;
940 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
941 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
942 bd275801 2020-02-03 tracey goto done;
944 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
945 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
946 bd275801 2020-02-03 tracey goto done;
948 bd275801 2020-02-03 tracey /* build tree nav */
949 bd275801 2020-02-03 tracey if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
950 bd275801 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
951 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
954 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
955 bd275801 2020-02-03 tracey KATTR_HREF, href_tree, KATTR__MAX);
956 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
957 bd275801 2020-02-03 tracey goto done;
958 bd275801 2020-02-03 tracey khtml_puts(gw_trans->gw_html_req, "tree");
959 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
960 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
961 bd275801 2020-02-03 tracey goto done;
962 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
963 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
964 bd275801 2020-02-03 tracey goto done;
966 bd275801 2020-02-03 tracey /* dotted line */
967 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
968 bd275801 2020-02-03 tracey KATTR_ID, "dotted_line", KATTR__MAX);
969 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
970 bd275801 2020-02-03 tracey goto done;
971 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
972 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
973 bd275801 2020-02-03 tracey goto done;
976 55ccf528 2020-02-03 stsp age = NULL;
977 55ccf528 2020-02-03 stsp free(age_html);
978 55ccf528 2020-02-03 stsp age_html = NULL;
979 bd275801 2020-02-03 tracey free(href_diff);
980 bd275801 2020-02-03 tracey href_diff = NULL;
981 bd275801 2020-02-03 tracey free(href_tree);
982 bd275801 2020-02-03 tracey href_tree = NULL;
985 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
986 f2f46662 2020-01-23 tracey gw_free_headers(header);
987 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
988 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
990 55ccf528 2020-02-03 stsp free(age_html);
991 bd275801 2020-02-03 tracey free(href_diff);
992 bd275801 2020-02-03 tracey free(href_tree);
993 ef72fe2c 2020-02-04 stsp if (error == NULL && kerr != KCGI_OK)
994 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
995 2c251c14 2020-01-15 tracey return error;
998 2c251c14 2020-01-15 tracey static const struct got_error *
999 54415d85 2020-01-15 tracey gw_summary(struct gw_trans *gw_trans)
1001 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
1002 783ec107 2020-02-03 tracey char *age = NULL, *tags = NULL, *heads = NULL;
1003 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1005 f4df82f9 2020-01-29 stsp if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1006 f4df82f9 2020-01-29 stsp return got_error_from_errno("pledge");
1008 f2f46662 2020-01-23 tracey /* unveil is applied with gw_briefs below */
1010 783ec107 2020-02-03 tracey /* summary wrapper */
1011 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1012 783ec107 2020-02-03 tracey "summary_wrapper", KATTR__MAX);
1013 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1014 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1016 783ec107 2020-02-03 tracey /* description */
1017 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_description &&
1018 783ec107 2020-02-03 tracey gw_trans->gw_dir->description != NULL &&
1019 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1020 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1021 783ec107 2020-02-03 tracey KATTR_ID, "description_title", KATTR__MAX);
1022 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1023 783ec107 2020-02-03 tracey goto done;
1024 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Description: ");
1025 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1026 783ec107 2020-02-03 tracey goto done;
1027 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1028 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1029 783ec107 2020-02-03 tracey goto done;
1030 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1031 783ec107 2020-02-03 tracey KATTR_ID, "description", KATTR__MAX);
1032 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1033 783ec107 2020-02-03 tracey goto done;
1034 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1035 783ec107 2020-02-03 tracey gw_trans->gw_dir->description);
1036 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1037 783ec107 2020-02-03 tracey goto done;
1038 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1039 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1040 783ec107 2020-02-03 tracey goto done;
1043 783ec107 2020-02-03 tracey /* repo owner */
1044 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner &&
1045 9a1cc63f 2020-02-03 stsp gw_trans->gw_dir->owner != NULL) {
1046 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1047 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner_title", KATTR__MAX);
1048 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1049 9a1cc63f 2020-02-03 stsp goto done;
1050 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Owner: ");
1051 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1052 9a1cc63f 2020-02-03 stsp goto done;
1053 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1054 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1055 783ec107 2020-02-03 tracey goto done;
1056 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1057 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner", KATTR__MAX);
1058 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1059 783ec107 2020-02-03 tracey goto done;
1060 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1061 783ec107 2020-02-03 tracey gw_trans->gw_dir->owner);
1062 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1063 783ec107 2020-02-03 tracey goto done;
1064 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1065 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1066 783ec107 2020-02-03 tracey goto done;
1069 783ec107 2020-02-03 tracey /* last change */
1070 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
1071 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1072 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
1073 d126c1b7 2020-01-29 stsp if (error)
1074 20f34652 2020-01-29 stsp goto done;
1075 55ccf528 2020-02-03 stsp if (age != NULL) {
1076 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1077 783ec107 2020-02-03 tracey KATTR_ID, "last_change_title", KATTR__MAX);
1078 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1079 20f34652 2020-01-29 stsp goto done;
1080 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1081 783ec107 2020-02-03 tracey "Last Change: ");
1082 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1083 20f34652 2020-01-29 stsp goto done;
1084 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1085 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1086 20f34652 2020-01-29 stsp goto done;
1087 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1088 783ec107 2020-02-03 tracey KATTR_ID, "last_change", KATTR__MAX);
1089 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1090 20f34652 2020-01-29 stsp goto done;
1091 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, age);
1092 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1093 783ec107 2020-02-03 tracey goto done;
1094 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1095 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1096 783ec107 2020-02-03 tracey goto done;
1100 783ec107 2020-02-03 tracey /* cloneurl */
1101 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1102 783ec107 2020-02-03 tracey gw_trans->gw_dir->url != NULL &&
1103 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1104 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1105 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl_title", KATTR__MAX);
1106 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1107 783ec107 2020-02-03 tracey goto done;
1108 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Clone URL: ");
1109 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1110 783ec107 2020-02-03 tracey goto done;
1111 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1112 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1113 783ec107 2020-02-03 tracey goto done;
1114 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1115 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl", KATTR__MAX);
1116 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1117 783ec107 2020-02-03 tracey goto done;
1118 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1119 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1120 21a55cc7 2020-02-04 tracey goto done;
1121 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1122 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1123 783ec107 2020-02-03 tracey goto done;
1126 783ec107 2020-02-03 tracey /* close summary wrapper */
1127 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1128 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1129 bd275801 2020-02-03 tracey goto done;
1131 bd275801 2020-02-03 tracey /* commit briefs header */
1132 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1133 bd275801 2020-02-03 tracey "briefs_title_wrapper", KATTR__MAX);
1134 21a55cc7 2020-02-04 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, KATTR_ID,
1137 bd275801 2020-02-03 tracey "briefs_title", KATTR__MAX);
1138 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1139 20f34652 2020-01-29 stsp goto done;
1140 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1141 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1142 bd275801 2020-02-03 tracey goto done;
1143 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1144 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1145 bd275801 2020-02-03 tracey goto done;
1146 f2f46662 2020-01-23 tracey error = gw_briefs(gw_trans);
1147 f2f46662 2020-01-23 tracey if (error)
1148 20f34652 2020-01-29 stsp goto done;
1150 783ec107 2020-02-03 tracey /* tags */
1151 6eccd105 2020-02-03 stsp error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
1152 6eccd105 2020-02-03 stsp TAGBRIEF);
1153 6eccd105 2020-02-03 stsp if (error)
1154 6eccd105 2020-02-03 stsp goto done;
1156 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
1157 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1158 783ec107 2020-02-03 tracey KATTR_ID, "summary_tags_title_wrapper", KATTR__MAX);
1159 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1160 20f34652 2020-01-29 stsp goto done;
1161 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1162 783ec107 2020-02-03 tracey KATTR_ID, "summary_tags_title", KATTR__MAX);
1163 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1164 783ec107 2020-02-03 tracey goto done;
1165 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tags");
1166 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1167 783ec107 2020-02-03 tracey goto done;
1168 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1169 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1170 20f34652 2020-01-29 stsp goto done;
1171 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1172 783ec107 2020-02-03 tracey KATTR_ID, "summary_tags_content", KATTR__MAX);
1173 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1174 783ec107 2020-02-03 tracey goto done;
1175 783ec107 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, tags);
1176 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1177 783ec107 2020-02-03 tracey goto done;
1178 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1179 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1180 783ec107 2020-02-03 tracey goto done;
1183 783ec107 2020-02-03 tracey /* heads */
1184 51d4a92d 2020-02-03 tracey error = gw_get_repo_heads(&heads, gw_trans);
1185 51d4a92d 2020-02-03 tracey if (error)
1186 51d4a92d 2020-02-03 tracey goto done;
1187 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
1188 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1189 783ec107 2020-02-03 tracey KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1190 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1191 20f34652 2020-01-29 stsp goto done;
1192 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1193 783ec107 2020-02-03 tracey KATTR_ID, "summary_heads_title", KATTR__MAX);
1194 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1195 783ec107 2020-02-03 tracey goto done;
1196 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Heads");
1197 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1198 783ec107 2020-02-03 tracey goto done;
1199 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1200 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1201 783ec107 2020-02-03 tracey goto done;
1202 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1203 783ec107 2020-02-03 tracey KATTR_ID, "summary_heads_content", KATTR__MAX);
1204 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1205 783ec107 2020-02-03 tracey goto done;
1206 783ec107 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, heads);
1207 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1208 783ec107 2020-02-03 tracey goto done;
1209 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1210 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1211 20f34652 2020-01-29 stsp goto done;
1214 20f34652 2020-01-29 stsp free(age);
1215 20f34652 2020-01-29 stsp free(tags);
1216 20f34652 2020-01-29 stsp free(heads);
1217 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1218 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1219 2204c934 2020-01-15 tracey return error;
1222 2204c934 2020-01-15 tracey static const struct got_error *
1223 f2f46662 2020-01-23 tracey gw_tree(struct gw_trans *gw_trans)
1225 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
1226 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
1227 f2f46662 2020-01-23 tracey char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1228 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1229 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1231 f2f46662 2020-01-23 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1232 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
1234 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
1235 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
1237 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1238 b772de24 2020-01-15 tracey if (error)
1239 5ddf0079 2020-01-29 stsp goto done;
1241 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
1242 f2f46662 2020-01-23 tracey if (error)
1243 42281175 2020-01-29 stsp goto done;
1245 84bf4df2 2020-02-03 stsp error = gw_get_repo_tree(&tree_html, gw_trans);
1246 84bf4df2 2020-02-03 stsp if (error)
1247 84bf4df2 2020-02-03 stsp goto done;
1249 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, header->committer_time, TM_LONG);
1250 55ccf528 2020-02-03 stsp if (error)
1251 55ccf528 2020-02-03 stsp goto done;
1252 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
1253 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
1254 55ccf528 2020-02-03 stsp goto done;
1256 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1257 070fee27 2020-02-03 stsp if (error)
1258 070fee27 2020-02-03 stsp goto done;
1259 55ccf528 2020-02-03 stsp if (asprintf(&tree_html_disp, tree_header, age_html,
1260 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(escaped_commit_msg),
1261 84bf4df2 2020-02-03 stsp tree_html ? tree_html : "") == -1) {
1262 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
1263 42281175 2020-01-29 stsp goto done;
1266 42281175 2020-01-29 stsp if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1267 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
1268 42281175 2020-01-29 stsp goto done;
1271 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tree);
1272 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1273 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1275 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
1276 f2f46662 2020-01-23 tracey gw_free_headers(header);
1277 f2f46662 2020-01-23 tracey free(tree_html_disp);
1278 f2f46662 2020-01-23 tracey free(tree_html);
1279 f2f46662 2020-01-23 tracey free(tree);
1280 55ccf528 2020-02-03 stsp free(age);
1281 55ccf528 2020-02-03 stsp free(age_html);
1282 070fee27 2020-02-03 stsp free(escaped_commit_msg);
1283 4ff7391f 2020-01-28 tracey return error;
1286 4ff7391f 2020-01-28 tracey static const struct got_error *
1287 4ff7391f 2020-01-28 tracey gw_tag(struct gw_trans *gw_trans)
1289 4ff7391f 2020-01-28 tracey const struct got_error *error = NULL;
1290 4ff7391f 2020-01-28 tracey struct gw_header *header = NULL;
1291 4ff7391f 2020-01-28 tracey char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1292 070fee27 2020-02-03 stsp char *escaped_commit_msg = NULL;
1293 4ff7391f 2020-01-28 tracey enum kcgi_err kerr;
1295 4ff7391f 2020-01-28 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1296 4ff7391f 2020-01-28 tracey return got_error_from_errno("pledge");
1298 4ff7391f 2020-01-28 tracey if ((header = gw_init_header()) == NULL)
1299 4ff7391f 2020-01-28 tracey return got_error_from_errno("malloc");
1301 4ff7391f 2020-01-28 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1302 4ff7391f 2020-01-28 tracey if (error)
1303 5ddf0079 2020-01-29 stsp goto done;
1305 4ff7391f 2020-01-28 tracey error = gw_get_header(gw_trans, header, 1);
1306 4ff7391f 2020-01-28 tracey if (error)
1307 470cd826 2020-01-29 stsp goto done;
1309 6eccd105 2020-02-03 stsp error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1310 6eccd105 2020-02-03 stsp if (error)
1311 6eccd105 2020-02-03 stsp goto done;
1313 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1314 070fee27 2020-02-03 stsp if (error)
1315 070fee27 2020-02-03 stsp goto done;
1316 88d59c36 2020-01-29 stsp if (asprintf(&tag_html_disp, tag_header,
1317 21a55cc7 2020-02-04 tracey gw_gen_commit_header_old(header->commit_id, header->refs_str),
1318 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(escaped_commit_msg),
1319 6eccd105 2020-02-03 stsp tag_html ? tag_html : "") == -1) {
1320 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
1321 470cd826 2020-01-29 stsp goto done;
1324 470cd826 2020-01-29 stsp if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1325 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
1326 470cd826 2020-01-29 stsp goto done;
1329 4ff7391f 2020-01-28 tracey kerr = khttp_puts(gw_trans->gw_req, tag);
1330 4ff7391f 2020-01-28 tracey if (kerr != KCGI_OK)
1331 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1333 4ff7391f 2020-01-28 tracey got_ref_list_free(&header->refs);
1334 4ff7391f 2020-01-28 tracey gw_free_headers(header);
1335 4ff7391f 2020-01-28 tracey free(tag_html_disp);
1336 4ff7391f 2020-01-28 tracey free(tag_html);
1337 4ff7391f 2020-01-28 tracey free(tag);
1338 070fee27 2020-02-03 stsp free(escaped_commit_msg);
1339 2c251c14 2020-01-15 tracey return error;
1342 2c251c14 2020-01-15 tracey static const struct got_error *
1343 54415d85 2020-01-15 tracey gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1345 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1346 2c251c14 2020-01-15 tracey DIR *dt;
1347 2c251c14 2020-01-15 tracey char *dir_test;
1348 4ceb8155 2020-01-15 tracey int opened = 0;
1350 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1351 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1352 88d59c36 2020-01-29 stsp GOTWEB_GIT_DIR) == -1)
1353 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1355 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1356 2c251c14 2020-01-15 tracey if (dt == NULL) {
1357 2c251c14 2020-01-15 tracey free(dir_test);
1358 2c251c14 2020-01-15 tracey } else {
1359 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1360 4ceb8155 2020-01-15 tracey opened = 1;
1361 2c251c14 2020-01-15 tracey goto done;
1364 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1365 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1366 88d59c36 2020-01-29 stsp GOTWEB_GOT_DIR) == -1)
1367 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1369 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1370 2c251c14 2020-01-15 tracey if (dt == NULL)
1371 2c251c14 2020-01-15 tracey free(dir_test);
1373 4ceb8155 2020-01-15 tracey opened = 1;
1374 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
1375 2c251c14 2020-01-15 tracey goto errored;
1378 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s",
1379 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1380 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1382 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1385 32cd4d18 2020-02-03 stsp error = gw_get_repo_description(&gw_dir->description, gw_trans,
1386 2c251c14 2020-01-15 tracey gw_dir->path);
1387 32cd4d18 2020-02-03 stsp if (error)
1388 32cd4d18 2020-02-03 stsp goto errored;
1389 9a1cc63f 2020-02-03 stsp error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1390 9a1cc63f 2020-02-03 stsp if (error)
1391 9a1cc63f 2020-02-03 stsp goto errored;
1392 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1393 d126c1b7 2020-01-29 stsp "refs/heads", TM_DIFF);
1394 d126c1b7 2020-01-29 stsp if (error)
1395 d126c1b7 2020-01-29 stsp goto errored;
1396 59d3c40e 2020-02-03 stsp error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1397 2c251c14 2020-01-15 tracey errored:
1398 2c251c14 2020-01-15 tracey free(dir_test);
1399 2c251c14 2020-01-15 tracey if (opened)
1400 2c251c14 2020-01-15 tracey closedir(dt);
1401 2c251c14 2020-01-15 tracey return error;
1404 2c251c14 2020-01-15 tracey static const struct got_error *
1405 54415d85 2020-01-15 tracey gw_load_got_paths(struct gw_trans *gw_trans)
1407 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1409 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
1410 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1411 2c251c14 2020-01-15 tracey struct stat st;
1412 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
1414 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
1415 2c251c14 2020-01-15 tracey if (d == NULL) {
1416 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
1417 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1418 2c251c14 2020-01-15 tracey return error;
1421 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1422 2c251c14 2020-01-15 tracey alphasort);
1423 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
1424 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
1425 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1426 2c251c14 2020-01-15 tracey return error;
1429 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
1430 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
1431 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1432 2c251c14 2020-01-15 tracey break; /* account for parent and self */
1434 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1435 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
1436 2c251c14 2020-01-15 tracey continue;
1438 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1439 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1441 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
1442 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1443 2c251c14 2020-01-15 tracey continue;
1444 2c251c14 2020-01-15 tracey else if (error)
1445 2c251c14 2020-01-15 tracey return error;
1447 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1448 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
1449 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1451 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
1455 2c251c14 2020-01-15 tracey closedir(d);
1456 2c251c14 2020-01-15 tracey return error;
1459 2c251c14 2020-01-15 tracey static const struct got_error *
1460 54415d85 2020-01-15 tracey gw_parse_querystring(struct gw_trans *gw_trans)
1462 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1463 2c251c14 2020-01-15 tracey struct kpair *p;
1464 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
1465 2c251c14 2020-01-15 tracey unsigned int i;
1467 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
1468 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
1469 2c251c14 2020-01-15 tracey return error;
1470 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1471 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
1472 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1473 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1475 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_path, "%s/%s",
1476 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1477 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1479 2c251c14 2020-01-15 tracey /* get action and set function */
1480 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1481 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
1482 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
1483 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
1484 2c251c14 2020-01-15 tracey continue;
1486 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
1487 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
1488 2c251c14 2020-01-15 tracey gw_trans->action = i;
1489 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->action_name,
1490 88d59c36 2020-01-29 stsp "%s", action->func_name) == -1)
1492 b772de24 2020-01-15 tracey got_error_from_errno(
1493 2c251c14 2020-01-15 tracey "asprintf");
1498 2c251c14 2020-01-15 tracey action = NULL;
1501 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1502 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->commit, "%s",
1503 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1504 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1506 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1507 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_file, "%s",
1508 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1509 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
1511 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1512 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_folder, "%s",
1513 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1514 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1516 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1517 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->headref, "%s",
1518 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1519 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1521 2c251c14 2020-01-15 tracey if (action == NULL) {
1522 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
1523 2c251c14 2020-01-15 tracey return error;
1525 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
1526 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1527 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1529 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1530 46b9c89b 2020-01-15 tracey if (error)
1531 46b9c89b 2020-01-15 tracey return error;
1533 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1535 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1536 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1538 2c251c14 2020-01-15 tracey return error;
1541 2c251c14 2020-01-15 tracey static struct gw_dir *
1542 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
1544 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1546 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1547 2c251c14 2020-01-15 tracey return NULL;
1549 88d59c36 2020-01-29 stsp if (asprintf(&gw_dir->name, "%s", dir) == -1)
1550 2c251c14 2020-01-15 tracey return NULL;
1552 2c251c14 2020-01-15 tracey return gw_dir;
1555 c25c2314 2020-01-28 stsp static const struct got_error *
1556 54415d85 2020-01-15 tracey gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1558 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1560 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1561 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1562 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1563 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1564 2c251c14 2020-01-15 tracey khttps[code]);
1565 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1566 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1567 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1568 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1569 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1570 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1571 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1572 017d6da3 2020-01-29 tracey "nosniff");
1573 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1574 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1575 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1576 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1577 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1578 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1579 017d6da3 2020-01-29 tracey "1; mode=block");
1580 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1581 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1583 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1584 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req,
1585 017d6da3 2020-01-29 tracey kresps[KRESP_CONTENT_DISPOSITION],
1586 017d6da3 2020-01-29 tracey "attachment; filename=%s", gw_trans->repo_file);
1587 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
1588 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1591 c25c2314 2020-01-28 stsp kerr = khttp_body(gw_trans->gw_req);
1592 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1595 c25c2314 2020-01-28 stsp static const struct got_error *
1596 6d8d8a26 2020-01-29 stsp gw_display_index(struct gw_trans *gw_trans)
1598 4bec7539 2020-01-29 stsp const struct got_error *error;
1599 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1601 4bec7539 2020-01-29 stsp error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1602 4bec7539 2020-01-29 stsp if (error)
1603 4bec7539 2020-01-29 stsp return error;
1605 f7cc9480 2020-02-05 tracey kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1606 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1607 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1609 017d6da3 2020-01-29 tracey if (gw_trans->action != GW_BLOB) {
1610 017d6da3 2020-01-29 tracey kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1611 017d6da3 2020-01-29 tracey gw_query_funcs[gw_trans->action].template);
1612 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK) {
1613 017d6da3 2020-01-29 tracey khtml_close(gw_trans->gw_html_req);
1614 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1618 7a9bfbff 2020-01-29 stsp return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1621 6d8d8a26 2020-01-29 stsp static void
1622 6d8d8a26 2020-01-29 stsp gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1624 6d8d8a26 2020-01-29 stsp if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1627 6d8d8a26 2020-01-29 stsp if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1629 33dc7bd2 2020-02-03 stsp khtml_puts(gw_trans->gw_html_req, err->msg);
1630 6d8d8a26 2020-01-29 stsp khtml_close(gw_trans->gw_html_req);
1633 2c251c14 2020-01-15 tracey static int
1634 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1636 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1637 bd275801 2020-02-03 tracey enum kcgi_err kerr;
1638 54415d85 2020-01-15 tracey struct gw_trans *gw_trans = arg;
1639 185ba3ba 2020-02-04 tracey char *gw_site_link, *img_src = NULL;
1641 2c251c14 2020-01-15 tracey switch (key) {
1642 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1643 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, head);
1644 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1645 bd275801 2020-02-03 tracey return 0;
1647 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1648 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1649 185ba3ba 2020-02-04 tracey KATTR_ID, "got_link", KATTR__MAX);
1650 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
1651 185ba3ba 2020-02-04 tracey return 0;
1652 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1653 185ba3ba 2020-02-04 tracey KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1654 185ba3ba 2020-02-04 tracey KATTR_TARGET, "_sotd", KATTR__MAX);
1655 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
1656 185ba3ba 2020-02-04 tracey return 0;
1657 185ba3ba 2020-02-04 tracey if (asprintf(&img_src, "/%s",
1658 185ba3ba 2020-02-04 tracey gw_trans->gw_conf->got_logo) == -1)
1659 185ba3ba 2020-02-04 tracey return 0;
1660 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1661 185ba3ba 2020-02-04 tracey KATTR_SRC, img_src, KATTR__MAX);
1662 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
1663 185ba3ba 2020-02-04 tracey free(img_src);
1664 185ba3ba 2020-02-04 tracey return 0;
1666 185ba3ba 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1667 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
1668 185ba3ba 2020-02-04 tracey free(img_src);
1669 185ba3ba 2020-02-04 tracey return 0;
1672 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1673 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
1674 bd275801 2020-02-03 tracey if (gw_site_link != NULL) {
1675 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1676 bd275801 2020-02-03 tracey if (kerr != KCGI_OK) {
1677 bd275801 2020-02-03 tracey free(gw_site_link);
1678 bd275801 2020-02-03 tracey return 0;
1681 2c251c14 2020-01-15 tracey free(gw_site_link);
1683 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1684 bd275801 2020-02-03 tracey if (gw_trans->gw_conf->got_site_name != NULL) {
1685 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1686 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1687 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1688 bd275801 2020-02-03 tracey return 0;
1691 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1692 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, search);
1693 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1694 bd275801 2020-02-03 tracey return 0;
1696 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
1697 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
1698 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
1699 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1700 bd275801 2020-02-03 tracey KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1701 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1703 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1704 bd275801 2020-02-03 tracey KATTR_ID, "site_owner", KATTR__MAX);
1705 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1706 2c251c14 2020-01-15 tracey return 0;
1707 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1708 bd275801 2020-02-03 tracey gw_trans->gw_conf->got_site_owner);
1709 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1710 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1711 bd275801 2020-02-03 tracey return 0;
1714 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1715 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1716 bd275801 2020-02-03 tracey if (error) {
1717 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, error->msg);
1718 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1719 bd275801 2020-02-03 tracey return 0;
1722 2c251c14 2020-01-15 tracey default:
1723 2c251c14 2020-01-15 tracey return 0;
1725 2c251c14 2020-01-15 tracey return 1;
1728 2c251c14 2020-01-15 tracey static char *
1729 21a55cc7 2020-02-04 tracey gw_gen_commit_header_old(char *str1, char *str2)
1731 f2f46662 2020-01-23 tracey char *return_html = NULL, *ref_str = NULL;
1733 f2f46662 2020-01-23 tracey if (strcmp(str2, "") != 0) {
1734 88d59c36 2020-01-29 stsp if (asprintf(&ref_str, "(%s)", str2) == -1) {
1735 f2f46662 2020-01-23 tracey return_html = strdup("");
1736 f2f46662 2020-01-23 tracey return return_html;
1739 f2f46662 2020-01-23 tracey ref_str = strdup("");
1741 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1742 f2f46662 2020-01-23 tracey return_html = strdup("");
1744 f2f46662 2020-01-23 tracey free(ref_str);
1745 f2f46662 2020-01-23 tracey return return_html;
1748 21a55cc7 2020-02-04 tracey static const struct got_error *
1749 21a55cc7 2020-02-04 tracey gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1751 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1752 21a55cc7 2020-02-04 tracey char *ref_str = NULL;
1753 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1755 21a55cc7 2020-02-04 tracey if (strcmp(str2, "") != 0) {
1756 21a55cc7 2020-02-04 tracey if (asprintf(&ref_str, "(%s)", str2) == -1) {
1757 21a55cc7 2020-02-04 tracey error = got_error_from_errno("asprintf");
1758 21a55cc7 2020-02-04 tracey goto done;
1761 21a55cc7 2020-02-04 tracey ref_str = strdup("");
1763 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1764 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit_title", KATTR__MAX);
1765 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1766 21a55cc7 2020-02-04 tracey goto done;
1767 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Commit: ");
1768 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1769 21a55cc7 2020-02-04 tracey goto done;
1770 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1771 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1772 21a55cc7 2020-02-04 tracey goto done;
1773 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1774 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit", KATTR__MAX);
1775 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1776 21a55cc7 2020-02-04 tracey goto done;
1777 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str1);
1778 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1779 21a55cc7 2020-02-04 tracey goto done;
1780 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, " ");
1781 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1782 21a55cc7 2020-02-04 tracey goto done;
1783 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, ref_str);
1784 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1785 21a55cc7 2020-02-04 tracey goto done;
1786 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1787 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1788 21a55cc7 2020-02-04 tracey goto done;
1790 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1791 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1792 21a55cc7 2020-02-04 tracey return error;
1795 f7cc9480 2020-02-05 tracey static const struct got_error *
1796 f7cc9480 2020-02-05 tracey gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1798 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
1799 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
1801 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1802 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff_title", KATTR__MAX);
1803 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1804 f7cc9480 2020-02-05 tracey goto done;
1805 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Diff: ");
1806 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1807 f7cc9480 2020-02-05 tracey goto done;
1808 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1809 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1810 f7cc9480 2020-02-05 tracey goto done;
1811 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1812 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff", KATTR__MAX);
1813 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1814 f7cc9480 2020-02-05 tracey goto done;
1815 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, str1);
1816 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1817 f7cc9480 2020-02-05 tracey goto done;
1818 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
1819 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1820 f7cc9480 2020-02-05 tracey goto done;
1821 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, str2);
1822 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1823 f7cc9480 2020-02-05 tracey goto done;
1824 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1825 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1826 f7cc9480 2020-02-05 tracey goto done;
1828 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1829 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1830 f7cc9480 2020-02-05 tracey return error;
1833 21a55cc7 2020-02-04 tracey static const struct got_error *
1834 21a55cc7 2020-02-04 tracey gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1836 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1837 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1839 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1840 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age_title", KATTR__MAX);
1841 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1842 21a55cc7 2020-02-04 tracey goto done;
1843 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Date: ");
1844 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1845 21a55cc7 2020-02-04 tracey goto done;
1846 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1847 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1848 21a55cc7 2020-02-04 tracey goto done;
1849 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1850 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age", KATTR__MAX);
1851 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1852 21a55cc7 2020-02-04 tracey goto done;
1853 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
1854 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1855 21a55cc7 2020-02-04 tracey goto done;
1856 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1857 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1858 21a55cc7 2020-02-04 tracey goto done;
1860 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1861 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1862 21a55cc7 2020-02-04 tracey return error;
1865 21a55cc7 2020-02-04 tracey static const struct got_error *
1866 21a55cc7 2020-02-04 tracey gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1868 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1869 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
1871 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1872 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author_title", KATTR__MAX);
1873 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1874 21a55cc7 2020-02-04 tracey goto done;
1875 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Author: ");
1876 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1877 21a55cc7 2020-02-04 tracey goto done;
1878 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1879 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1880 21a55cc7 2020-02-04 tracey goto done;
1881 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1882 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author", KATTR__MAX);
1883 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1884 21a55cc7 2020-02-04 tracey goto done;
1885 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
1886 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1887 21a55cc7 2020-02-04 tracey goto done;
1888 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1889 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1890 21a55cc7 2020-02-04 tracey goto done;
1892 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1893 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1894 21a55cc7 2020-02-04 tracey return error;
1897 21a55cc7 2020-02-04 tracey static const struct got_error *
1898 21a55cc7 2020-02-04 tracey gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1900 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1901 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
1903 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1904 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer_title", KATTR__MAX);
1905 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1906 21a55cc7 2020-02-04 tracey goto done;
1907 87ca24ac 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
1908 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1909 21a55cc7 2020-02-04 tracey goto done;
1910 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1911 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1912 21a55cc7 2020-02-04 tracey goto done;
1913 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1914 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer", KATTR__MAX);
1915 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1916 21a55cc7 2020-02-04 tracey goto done;
1917 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
1918 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1919 21a55cc7 2020-02-04 tracey goto done;
1920 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1921 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1922 21a55cc7 2020-02-04 tracey goto done;
1924 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1925 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1926 21a55cc7 2020-02-04 tracey return error;
1929 f7cc9480 2020-02-05 tracey static const struct got_error *
1930 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1932 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
1933 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
1935 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1936 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg_title", KATTR__MAX);
1937 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1938 f7cc9480 2020-02-05 tracey goto done;
1939 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Message: ");
1940 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1941 f7cc9480 2020-02-05 tracey goto done;
1942 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1943 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1944 f7cc9480 2020-02-05 tracey goto done;
1945 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1946 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg", KATTR__MAX);
1947 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1948 f7cc9480 2020-02-05 tracey goto done;
1949 f7cc9480 2020-02-05 tracey kerr = khttp_puts(gw_trans->gw_req, str);
1950 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1951 f7cc9480 2020-02-05 tracey goto done;
1952 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1953 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1954 f7cc9480 2020-02-05 tracey goto done;
1956 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1957 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1958 f7cc9480 2020-02-05 tracey return error;
1961 f7cc9480 2020-02-05 tracey /* XXX: slated for deletion */
1962 f2f46662 2020-01-23 tracey static char *
1963 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(char *str)
1965 f2f46662 2020-01-23 tracey char *return_html = NULL;
1967 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1968 f2f46662 2020-01-23 tracey return_html = strdup("");
1970 f2f46662 2020-01-23 tracey return return_html;
1973 f7cc9480 2020-02-05 tracey static const struct got_error *
1974 f7cc9480 2020-02-05 tracey gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1976 f7cc9480 2020-02-05 tracey const struct got_error *error = NULL;
1977 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
1979 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1980 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree_title", KATTR__MAX);
1981 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1982 f7cc9480 2020-02-05 tracey goto done;
1983 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Tree: ");
1984 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1985 f7cc9480 2020-02-05 tracey goto done;
1986 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1987 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1988 f7cc9480 2020-02-05 tracey goto done;
1989 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1990 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree", KATTR__MAX);
1991 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1992 f7cc9480 2020-02-05 tracey goto done;
1993 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, str);
1994 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1995 f7cc9480 2020-02-05 tracey goto done;
1996 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1997 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1998 f7cc9480 2020-02-05 tracey goto done;
2000 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2001 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2002 f7cc9480 2020-02-05 tracey return error;
2005 32cd4d18 2020-02-03 stsp static const struct got_error *
2006 32cd4d18 2020-02-03 stsp gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2007 32cd4d18 2020-02-03 stsp char *dir)
2009 32cd4d18 2020-02-03 stsp const struct got_error *error = NULL;
2010 b8b04565 2020-02-02 tracey FILE *f = NULL;
2011 32cd4d18 2020-02-03 stsp char *d_file = NULL;
2012 2c251c14 2020-01-15 tracey unsigned int len;
2015 32cd4d18 2020-02-03 stsp *description = NULL;
2016 0b20762b 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_description == 0)
2017 32cd4d18 2020-02-03 stsp return gw_empty_string(description);
2019 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/description", dir) == -1)
2020 32cd4d18 2020-02-03 stsp return got_error_from_errno("asprintf");
2022 32cd4d18 2020-02-03 stsp f = fopen(d_file, "r");
2023 32cd4d18 2020-02-03 stsp if (f == NULL) {
2024 32cd4d18 2020-02-03 stsp if (errno == ENOENT || errno == EACCES)
2025 32cd4d18 2020-02-03 stsp return gw_empty_string(description);
2026 32cd4d18 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2027 32cd4d18 2020-02-03 stsp goto done;
2030 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2031 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2032 32cd4d18 2020-02-03 stsp goto done;
2034 32cd4d18 2020-02-03 stsp len = ftell(f);
2035 32cd4d18 2020-02-03 stsp if (len == -1) {
2036 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2037 32cd4d18 2020-02-03 stsp goto done;
2039 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2040 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2041 32cd4d18 2020-02-03 stsp goto done;
2043 32cd4d18 2020-02-03 stsp *description = calloc(len + 1, sizeof(**description));
2044 32cd4d18 2020-02-03 stsp if (*description == NULL) {
2045 32cd4d18 2020-02-03 stsp error = got_error_from_errno("calloc");
2046 32cd4d18 2020-02-03 stsp goto done;
2049 32cd4d18 2020-02-03 stsp n = fread(*description, 1, len, f);
2050 1ab830c1 2020-02-03 stsp if (n == 0 && ferror(f))
2051 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2053 32cd4d18 2020-02-03 stsp if (f != NULL && fclose(f) == -1 && error == NULL)
2054 32cd4d18 2020-02-03 stsp error = got_error_from_errno("fclose");
2055 2c251c14 2020-01-15 tracey free(d_file);
2056 32cd4d18 2020-02-03 stsp return error;
2059 55ccf528 2020-02-03 stsp static const struct got_error *
2060 55ccf528 2020-02-03 stsp gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2062 474370cb 2020-01-15 tracey struct tm tm;
2063 474370cb 2020-01-15 tracey time_t diff_time;
2064 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
2065 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2066 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
2067 474370cb 2020-01-15 tracey char *now = "right now";
2069 6c6c85af 2020-01-15 tracey char datebuf[29];
2071 55ccf528 2020-02-03 stsp *repo_age = NULL;
2073 474370cb 2020-01-15 tracey switch (ref_tm) {
2074 474370cb 2020-01-15 tracey case TM_DIFF:
2075 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
2076 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
2077 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2078 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 365), years) == -1)
2079 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2080 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2081 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2082 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
2083 88d59c36 2020-01-29 stsp months) == -1)
2084 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2085 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2086 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2087 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2088 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2089 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
2090 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2091 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24), days) == -1)
2092 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2093 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
2094 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2095 88d59c36 2020-01-29 stsp (diff_time / 60 / 60), hours) == -1)
2096 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2097 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
2098 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2099 88d59c36 2020-01-29 stsp minutes) == -1)
2100 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2101 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
2102 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", diff_time,
2103 88d59c36 2020-01-29 stsp seconds) == -1)
2104 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2105 474370cb 2020-01-15 tracey } else {
2106 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s", now) == -1)
2107 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2110 474370cb 2020-01-15 tracey case TM_LONG:
2111 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
2112 55ccf528 2020-02-03 stsp return got_error_from_errno("gmtime_r");
2114 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
2115 474370cb 2020-01-15 tracey if (s == NULL)
2116 55ccf528 2020-02-03 stsp return got_error_from_errno("asctime_r");
2118 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2119 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2122 55ccf528 2020-02-03 stsp return NULL;
2125 d126c1b7 2020-01-29 stsp static const struct got_error *
2126 d126c1b7 2020-01-29 stsp gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2127 d126c1b7 2020-01-29 stsp char *repo_ref, int ref_tm)
2129 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2130 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
2131 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
2132 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
2133 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
2134 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
2135 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
2136 87f9ebf5 2020-01-15 tracey int is_head = 0;
2137 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
2138 87f9ebf5 2020-01-15 tracey const char *refname;
2140 d126c1b7 2020-01-29 stsp *repo_age = NULL;
2141 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2143 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
2144 387a29ba 2020-01-15 tracey return NULL;
2146 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2147 87f9ebf5 2020-01-15 tracey is_head = 1;
2149 55ccf528 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_age == 0)
2150 d126c1b7 2020-01-29 stsp return NULL;
2152 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
2153 ec46ccd7 2020-01-15 tracey if (error)
2154 d126c1b7 2020-01-29 stsp goto done;
2156 87f9ebf5 2020-01-15 tracey if (is_head)
2157 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
2158 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
2160 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
2161 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
2162 ec46ccd7 2020-01-15 tracey if (error)
2163 d126c1b7 2020-01-29 stsp goto done;
2165 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2166 87f9ebf5 2020-01-15 tracey if (is_head)
2167 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
2169 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
2170 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
2171 ec46ccd7 2020-01-15 tracey if (error)
2172 d126c1b7 2020-01-29 stsp goto done;
2174 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
2175 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
2176 ec46ccd7 2020-01-15 tracey if (error)
2177 d126c1b7 2020-01-29 stsp goto done;
2179 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
2180 ec46ccd7 2020-01-15 tracey if (error)
2181 d126c1b7 2020-01-29 stsp goto done;
2183 2c251c14 2020-01-15 tracey committer_time =
2184 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
2186 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
2187 2c251c14 2020-01-15 tracey cmp_time = committer_time;
2190 474370cb 2020-01-15 tracey if (cmp_time != 0) {
2191 2c251c14 2020-01-15 tracey committer_time = cmp_time;
2192 55ccf528 2020-02-03 stsp error = gw_get_time_str(repo_age, committer_time, ref_tm);
2195 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
2196 2c251c14 2020-01-15 tracey free(id);
2197 d126c1b7 2020-01-29 stsp return error;
2200 83eb9a68 2020-02-03 stsp static const struct got_error *
2201 f7cc9480 2020-02-05 tracey gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
2203 ec46ccd7 2020-01-15 tracey const struct got_error *error;
2204 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
2205 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
2206 f7cc9480 2020-02-05 tracey char *label1 = NULL, *label2 = NULL, *line = NULL;
2207 05ce9a79 2020-01-24 tracey int obj_type;
2208 f7cc9480 2020-02-05 tracey size_t linesize = 0;
2209 83eb9a68 2020-02-03 stsp ssize_t linelen;
2210 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
2212 ec46ccd7 2020-01-15 tracey f = got_opentemp();
2213 ec46ccd7 2020-01-15 tracey if (f == NULL)
2214 ec46ccd7 2020-01-15 tracey return NULL;
2216 90f16cb8 2020-01-24 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2217 ec46ccd7 2020-01-15 tracey if (error)
2218 ec46ccd7 2020-01-15 tracey goto done;
2220 05ce9a79 2020-01-24 tracey if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2221 05ce9a79 2020-01-24 tracey error = got_repo_match_object_id(&id1, &label1,
2222 05ce9a79 2020-01-24 tracey header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2223 05ce9a79 2020-01-24 tracey if (error)
2224 05ce9a79 2020-01-24 tracey goto done;
2227 90f16cb8 2020-01-24 tracey error = got_repo_match_object_id(&id2, &label2,
2228 2ac037ec 2020-01-24 tracey header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2229 90f16cb8 2020-01-24 tracey if (error)
2230 90f16cb8 2020-01-24 tracey goto done;
2232 05ce9a79 2020-01-24 tracey error = got_object_get_type(&obj_type, header->repo, id2);
2233 90f16cb8 2020-01-24 tracey if (error)
2234 90f16cb8 2020-01-24 tracey goto done;
2235 05ce9a79 2020-01-24 tracey switch (obj_type) {
2236 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
2237 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2238 90f16cb8 2020-01-24 tracey header->repo, f);
2240 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
2241 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2242 90f16cb8 2020-01-24 tracey header->repo, f);
2244 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
2245 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_commits(id1, id2, 3, 0,
2246 90f16cb8 2020-01-24 tracey header->repo, f);
2248 ec46ccd7 2020-01-15 tracey default:
2249 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2251 b8b04565 2020-02-02 tracey if (error)
2252 b8b04565 2020-02-02 tracey goto done;
2254 d4729381 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2255 d4729381 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2256 ec46ccd7 2020-01-15 tracey goto done;
2259 83eb9a68 2020-02-03 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2260 f7cc9480 2020-02-05 tracey error = gw_colordiff_line(gw_trans, line);
2261 ec46ccd7 2020-01-15 tracey if (error)
2262 b8b04565 2020-02-02 tracey goto done;
2263 f7cc9480 2020-02-05 tracey /* XXX: KHTML_PRETTY breaks this */
2264 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, line);
2265 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2266 83eb9a68 2020-02-03 stsp goto done;
2267 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2268 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2269 b8b04565 2020-02-02 tracey goto done;
2271 f7cc9480 2020-02-05 tracey if (linelen == -1 && ferror(f))
2272 83eb9a68 2020-02-03 stsp error = got_error_from_errno("getline");
2274 d4729381 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2275 d4729381 2020-02-03 stsp error = got_error_from_errno("fclose");
2276 d4729381 2020-02-03 stsp free(line);
2277 ec46ccd7 2020-01-15 tracey free(label1);
2278 ec46ccd7 2020-01-15 tracey free(label2);
2279 ec46ccd7 2020-01-15 tracey free(id1);
2280 ec46ccd7 2020-01-15 tracey free(id2);
2282 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2283 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2284 83eb9a68 2020-02-03 stsp return error;
2287 9a1cc63f 2020-02-03 stsp static const struct got_error *
2288 9a1cc63f 2020-02-03 stsp gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2290 9a1cc63f 2020-02-03 stsp const struct got_error *error = NULL;
2291 9a1cc63f 2020-02-03 stsp struct got_repository *repo;
2292 9a1cc63f 2020-02-03 stsp const char *gitconfig_owner;
2294 9a1cc63f 2020-02-03 stsp *owner = NULL;
2296 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner == 0)
2297 9a1cc63f 2020-02-03 stsp return NULL;
2299 9a1cc63f 2020-02-03 stsp error = got_repo_open(&repo, dir, NULL);
2300 9a1cc63f 2020-02-03 stsp if (error)
2301 9a1cc63f 2020-02-03 stsp return error;
2302 9a1cc63f 2020-02-03 stsp gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2303 9a1cc63f 2020-02-03 stsp if (gitconfig_owner) {
2304 9a1cc63f 2020-02-03 stsp *owner = strdup(gitconfig_owner);
2305 9a1cc63f 2020-02-03 stsp if (*owner == NULL)
2306 9a1cc63f 2020-02-03 stsp error = got_error_from_errno("strdup");
2308 9a1cc63f 2020-02-03 stsp got_repo_close(repo);
2309 9a1cc63f 2020-02-03 stsp return error;
2312 59d3c40e 2020-02-03 stsp static const struct got_error *
2313 59d3c40e 2020-02-03 stsp gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2315 59d3c40e 2020-02-03 stsp const struct got_error *error = NULL;
2316 2c251c14 2020-01-15 tracey FILE *f;
2317 59d3c40e 2020-02-03 stsp char *d_file = NULL;
2318 2c251c14 2020-01-15 tracey unsigned int len;
2321 59d3c40e 2020-02-03 stsp *url = NULL;
2323 59d3c40e 2020-02-03 stsp if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2324 59d3c40e 2020-02-03 stsp return got_error_from_errno("asprintf");
2326 59d3c40e 2020-02-03 stsp f = fopen(d_file, "r");
2327 59d3c40e 2020-02-03 stsp if (f == NULL) {
2328 59d3c40e 2020-02-03 stsp if (errno != ENOENT && errno != EACCES)
2329 59d3c40e 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2330 59d3c40e 2020-02-03 stsp goto done;
2333 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2334 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2335 59d3c40e 2020-02-03 stsp goto done;
2337 59d3c40e 2020-02-03 stsp len = ftell(f);
2338 59d3c40e 2020-02-03 stsp if (len == -1) {
2339 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2340 59d3c40e 2020-02-03 stsp goto done;
2342 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2343 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2344 59d3c40e 2020-02-03 stsp goto done;
2347 59d3c40e 2020-02-03 stsp *url = calloc(len + 1, sizeof(**url));
2348 59d3c40e 2020-02-03 stsp if (*url == NULL) {
2349 59d3c40e 2020-02-03 stsp error = got_error_from_errno("calloc");
2350 59d3c40e 2020-02-03 stsp goto done;
2353 59d3c40e 2020-02-03 stsp n = fread(*url, 1, len, f);
2354 59d3c40e 2020-02-03 stsp if (n == 0 && ferror(f))
2355 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2357 59d3c40e 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2358 59d3c40e 2020-02-03 stsp error = got_error_from_errno("fclose");
2359 2c251c14 2020-01-15 tracey free(d_file);
2360 59d3c40e 2020-02-03 stsp return NULL;
2363 6eccd105 2020-02-03 stsp static const struct got_error *
2364 6eccd105 2020-02-03 stsp gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
2365 6eccd105 2020-02-03 stsp struct gw_header *header, int limit, int tag_type)
2367 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2368 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2369 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2370 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2371 6eccd105 2020-02-03 stsp char *tag_row = NULL, *tags_navs_disp = NULL;
2372 6eccd105 2020-02-03 stsp char *age = NULL, *age_html = NULL, *newline;
2373 070fee27 2020-02-03 stsp char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
2374 6eccd105 2020-02-03 stsp char *id_str = NULL, *refstr = NULL;
2375 6eccd105 2020-02-03 stsp char *tag_commit0 = NULL;
2376 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2377 87f9ebf5 2020-01-15 tracey size_t newsize;
2378 6eccd105 2020-02-03 stsp struct got_tag_object *tag = NULL;
2380 6eccd105 2020-02-03 stsp *tag_html = NULL;
2382 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2384 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2385 ec46ccd7 2020-01-15 tracey if (error)
2386 6c6c85af 2020-01-15 tracey return NULL;
2388 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2389 ec46ccd7 2020-01-15 tracey if (error)
2390 87f9ebf5 2020-01-15 tracey goto done;
2392 add40c4f 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2393 87f9ebf5 2020-01-15 tracey if (error)
2394 87f9ebf5 2020-01-15 tracey goto done;
2396 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2397 87f9ebf5 2020-01-15 tracey const char *refname;
2398 4ff7391f 2020-01-28 tracey const char *tagger;
2399 6eccd105 2020-02-03 stsp const char *tag_commit;
2400 87f9ebf5 2020-01-15 tracey time_t tagger_time;
2401 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
2403 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
2404 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
2405 87f9ebf5 2020-01-15 tracey continue;
2406 87f9ebf5 2020-01-15 tracey refname += 10;
2407 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
2408 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
2409 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2410 87f9ebf5 2020-01-15 tracey goto done;
2413 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
2414 87f9ebf5 2020-01-15 tracey if (error)
2415 87f9ebf5 2020-01-15 tracey goto done;
2416 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
2417 87f9ebf5 2020-01-15 tracey free(id);
2418 87f9ebf5 2020-01-15 tracey if (error)
2419 87f9ebf5 2020-01-15 tracey goto done;
2421 4ff7391f 2020-01-28 tracey tagger = got_object_tag_get_tagger(tag);
2422 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
2424 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
2425 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
2426 87f9ebf5 2020-01-15 tracey if (error)
2427 87f9ebf5 2020-01-15 tracey goto done;
2429 f2f46662 2020-01-23 tracey tag_commit0 = strdup(got_object_tag_get_message(tag));
2430 f2f46662 2020-01-23 tracey if (tag_commit0 == NULL) {
2431 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
2432 87f9ebf5 2020-01-15 tracey goto done;
2435 f2f46662 2020-01-23 tracey tag_commit = tag_commit0;
2436 f2f46662 2020-01-23 tracey while (*tag_commit == '\n')
2437 f2f46662 2020-01-23 tracey tag_commit++;
2439 87f9ebf5 2020-01-15 tracey switch (tag_type) {
2440 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
2441 f2f46662 2020-01-23 tracey newline = strchr(tag_commit, '\n');
2442 87f9ebf5 2020-01-15 tracey if (newline)
2443 87f9ebf5 2020-01-15 tracey *newline = '\0';
2445 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2446 55ccf528 2020-02-03 stsp if (error)
2447 87f9ebf5 2020-01-15 tracey goto done;
2449 88d59c36 2020-01-29 stsp if (asprintf(&tags_navs_disp, tags_navs,
2450 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
2451 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
2452 88d59c36 2020-01-29 stsp gw_trans->repo_name, id_str) == -1) {
2453 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2454 87f9ebf5 2020-01-15 tracey goto done;
2457 55ccf528 2020-02-03 stsp if (asprintf(&tag_row, tags_row, age ? age : "",
2458 55ccf528 2020-02-03 stsp refname, tag_commit, tags_navs_disp) == -1) {
2459 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2460 87f9ebf5 2020-01-15 tracey goto done;
2463 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
2465 87f9ebf5 2020-01-15 tracey case TAGFULL:
2466 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_LONG);
2467 55ccf528 2020-02-03 stsp if (error)
2468 4ff7391f 2020-01-28 tracey goto done;
2469 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_tagger, tagger);
2470 070fee27 2020-02-03 stsp if (error)
2471 070fee27 2020-02-03 stsp goto done;
2472 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_tag_commit, tag_commit);
2473 070fee27 2020-02-03 stsp if (error)
2474 070fee27 2020-02-03 stsp goto done;
2475 55ccf528 2020-02-03 stsp if (asprintf(&tag_row, tag_info, age ? age : "",
2476 070fee27 2020-02-03 stsp escaped_tagger, escaped_tag_commit) == -1) {
2477 4ff7391f 2020-01-28 tracey error = got_error_from_errno("asprintf");
2478 4ff7391f 2020-01-28 tracey goto done;
2481 87f9ebf5 2020-01-15 tracey default:
2485 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
2486 6eccd105 2020-02-03 stsp if (error)
2487 6eccd105 2020-02-03 stsp goto done;
2489 6eccd105 2020-02-03 stsp if (limit && --limit == 0)
2492 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
2493 6eccd105 2020-02-03 stsp tag = NULL;
2494 6c6c85af 2020-01-15 tracey free(id_str);
2495 6eccd105 2020-02-03 stsp id_str = NULL;
2496 6c6c85af 2020-01-15 tracey free(refstr);
2497 6eccd105 2020-02-03 stsp refstr = NULL;
2498 6c6c85af 2020-01-15 tracey free(age);
2499 55ccf528 2020-02-03 stsp age = NULL;
2500 55ccf528 2020-02-03 stsp free(age_html);
2501 55ccf528 2020-02-03 stsp age_html = NULL;
2502 070fee27 2020-02-03 stsp free(escaped_tagger);
2503 070fee27 2020-02-03 stsp escaped_tagger = NULL;
2504 070fee27 2020-02-03 stsp free(escaped_tag_commit);
2505 070fee27 2020-02-03 stsp escaped_tag_commit = NULL;
2506 f2f46662 2020-01-23 tracey free(tag_commit0);
2507 6eccd105 2020-02-03 stsp tag_commit0 = NULL;
2508 87f9ebf5 2020-01-15 tracey free(tag_row);
2509 6eccd105 2020-02-03 stsp tag_row = NULL;
2512 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2513 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2514 6eccd105 2020-02-03 stsp *tag_html = strdup(buf_get(diffbuf));
2515 6eccd105 2020-02-03 stsp if (*tag_html == NULL)
2516 6eccd105 2020-02-03 stsp error = got_error_from_errno("strdup");
2520 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
2521 6eccd105 2020-02-03 stsp free(id_str);
2522 6eccd105 2020-02-03 stsp free(refstr);
2523 55ccf528 2020-02-03 stsp free(age);
2524 55ccf528 2020-02-03 stsp free(age_html);
2525 070fee27 2020-02-03 stsp free(escaped_tagger);
2526 070fee27 2020-02-03 stsp free(escaped_tag_commit);
2527 6eccd105 2020-02-03 stsp free(tag_commit0);
2528 6eccd105 2020-02-03 stsp free(tag_row);
2529 6eccd105 2020-02-03 stsp buf_free(diffbuf);
2530 6eccd105 2020-02-03 stsp got_ref_list_free(&refs);
2532 6eccd105 2020-02-03 stsp got_repo_close(repo);
2533 6eccd105 2020-02-03 stsp return error;
2536 f2f46662 2020-01-23 tracey static void
2537 f2f46662 2020-01-23 tracey gw_free_headers(struct gw_header *header)
2539 f2f46662 2020-01-23 tracey free(header->id);
2540 f2f46662 2020-01-23 tracey free(header->path);
2541 f2f46662 2020-01-23 tracey if (header->commit != NULL)
2542 f2f46662 2020-01-23 tracey got_object_commit_close(header->commit);
2543 f2f46662 2020-01-23 tracey if (header->repo)
2544 f2f46662 2020-01-23 tracey got_repo_close(header->repo);
2545 f2f46662 2020-01-23 tracey free(header->refs_str);
2546 f2f46662 2020-01-23 tracey free(header->commit_id);
2547 f2f46662 2020-01-23 tracey free(header->parent_id);
2548 f2f46662 2020-01-23 tracey free(header->tree_id);
2549 f2f46662 2020-01-23 tracey free(header->commit_msg);
2552 f2f46662 2020-01-23 tracey static struct gw_header *
2553 f2f46662 2020-01-23 tracey gw_init_header()
2555 f2f46662 2020-01-23 tracey struct gw_header *header;
2557 ae36ed87 2020-01-28 stsp header = malloc(sizeof(*header));
2558 ae36ed87 2020-01-28 stsp if (header == NULL)
2559 f2f46662 2020-01-23 tracey return NULL;
2561 f2f46662 2020-01-23 tracey header->repo = NULL;
2562 f2f46662 2020-01-23 tracey header->commit = NULL;
2563 f2f46662 2020-01-23 tracey header->id = NULL;
2564 f2f46662 2020-01-23 tracey header->path = NULL;
2565 ae36ed87 2020-01-28 stsp SIMPLEQ_INIT(&header->refs);
2567 f2f46662 2020-01-23 tracey return header;
2570 f2f46662 2020-01-23 tracey static const struct got_error *
2571 f2f46662 2020-01-23 tracey gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2572 f2f46662 2020-01-23 tracey int limit)
2574 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2575 f2f46662 2020-01-23 tracey struct got_commit_graph *graph = NULL;
2577 f2f46662 2020-01-23 tracey error = got_commit_graph_open(&graph, header->path, 0);
2578 f2f46662 2020-01-23 tracey if (error)
2579 f2f46662 2020-01-23 tracey goto done;
2581 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_start(graph, header->id, header->repo,
2582 f2f46662 2020-01-23 tracey NULL, NULL);
2583 f2f46662 2020-01-23 tracey if (error)
2584 f2f46662 2020-01-23 tracey goto done;
2586 f2f46662 2020-01-23 tracey for (;;) {
2587 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_next(&header->id, graph,
2588 f2f46662 2020-01-23 tracey header->repo, NULL, NULL);
2589 f2f46662 2020-01-23 tracey if (error) {
2590 f2f46662 2020-01-23 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
2591 f2f46662 2020-01-23 tracey error = NULL;
2592 f2f46662 2020-01-23 tracey goto done;
2594 f2f46662 2020-01-23 tracey if (header->id == NULL)
2595 f2f46662 2020-01-23 tracey goto done;
2597 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit, header->repo,
2598 f2f46662 2020-01-23 tracey header->id);
2599 f2f46662 2020-01-23 tracey if (error)
2600 f2f46662 2020-01-23 tracey goto done;
2602 f2f46662 2020-01-23 tracey error = gw_get_commit(gw_trans, header);
2603 f2f46662 2020-01-23 tracey if (limit > 1) {
2604 f2f46662 2020-01-23 tracey struct gw_header *n_header = NULL;
2605 5147ab56 2020-01-29 stsp if ((n_header = gw_init_header()) == NULL) {
2606 f2f46662 2020-01-23 tracey error = got_error_from_errno("malloc");
2607 5147ab56 2020-01-29 stsp goto done;
2610 f2f46662 2020-01-23 tracey n_header->refs_str = strdup(header->refs_str);
2611 f2f46662 2020-01-23 tracey n_header->commit_id = strdup(header->commit_id);
2612 f2f46662 2020-01-23 tracey n_header->parent_id = strdup(header->parent_id);
2613 f2f46662 2020-01-23 tracey n_header->tree_id = strdup(header->tree_id);
2614 f2f46662 2020-01-23 tracey n_header->author = strdup(header->author);
2615 f2f46662 2020-01-23 tracey n_header->committer = strdup(header->committer);
2616 f2f46662 2020-01-23 tracey n_header->commit_msg = strdup(header->commit_msg);
2617 f2f46662 2020-01-23 tracey n_header->committer_time = header->committer_time;
2618 f2f46662 2020-01-23 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2621 f2f46662 2020-01-23 tracey if (error || (limit && --limit == 0))
2625 f2f46662 2020-01-23 tracey if (graph)
2626 f2f46662 2020-01-23 tracey got_commit_graph_close(graph);
2627 f2f46662 2020-01-23 tracey return error;
2630 f2f46662 2020-01-23 tracey static const struct got_error *
2631 f2f46662 2020-01-23 tracey gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2633 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2634 f2f46662 2020-01-23 tracey struct got_reflist_entry *re;
2635 f2f46662 2020-01-23 tracey struct got_object_id *id2 = NULL;
2636 f2f46662 2020-01-23 tracey struct got_object_qid *parent_id;
2637 e46f587c 2020-01-29 tracey char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2639 f2f46662 2020-01-23 tracey /*print commit*/
2640 f2f46662 2020-01-23 tracey SIMPLEQ_FOREACH(re, &header->refs, entry) {
2641 f2f46662 2020-01-23 tracey char *s;
2642 f2f46662 2020-01-23 tracey const char *name;
2643 f2f46662 2020-01-23 tracey struct got_tag_object *tag = NULL;
2644 f2f46662 2020-01-23 tracey int cmp;
2646 f2f46662 2020-01-23 tracey name = got_ref_get_name(re->ref);
2647 f2f46662 2020-01-23 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
2648 f2f46662 2020-01-23 tracey continue;
2649 f2f46662 2020-01-23 tracey if (strncmp(name, "refs/", 5) == 0)
2650 f2f46662 2020-01-23 tracey name += 5;
2651 f2f46662 2020-01-23 tracey if (strncmp(name, "got/", 4) == 0)
2652 f2f46662 2020-01-23 tracey continue;
2653 f2f46662 2020-01-23 tracey if (strncmp(name, "heads/", 6) == 0)
2654 f2f46662 2020-01-23 tracey name += 6;
2655 f2f46662 2020-01-23 tracey if (strncmp(name, "remotes/", 8) == 0)
2656 f2f46662 2020-01-23 tracey name += 8;
2657 f2f46662 2020-01-23 tracey if (strncmp(name, "tags/", 5) == 0) {
2658 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag, header->repo,
2659 f2f46662 2020-01-23 tracey re->id);
2660 f2f46662 2020-01-23 tracey if (error) {
2661 f2f46662 2020-01-23 tracey if (error->code != GOT_ERR_OBJ_TYPE)
2662 f2f46662 2020-01-23 tracey continue;
2664 f2f46662 2020-01-23 tracey * Ref points at something other
2665 f2f46662 2020-01-23 tracey * than a tag.
2667 f2f46662 2020-01-23 tracey error = NULL;
2668 f2f46662 2020-01-23 tracey tag = NULL;
2671 f2f46662 2020-01-23 tracey cmp = got_object_id_cmp(tag ?
2672 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag) : re->id, header->id);
2673 f2f46662 2020-01-23 tracey if (tag)
2674 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2675 f2f46662 2020-01-23 tracey if (cmp != 0)
2676 f2f46662 2020-01-23 tracey continue;
2677 f2f46662 2020-01-23 tracey s = refs_str;
2678 88d59c36 2020-01-29 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2679 88d59c36 2020-01-29 stsp s ? ", " : "", name) == -1) {
2680 f2f46662 2020-01-23 tracey error = got_error_from_errno("asprintf");
2681 f2f46662 2020-01-23 tracey free(s);
2682 f2f46662 2020-01-23 tracey return error;
2684 f2f46662 2020-01-23 tracey header->refs_str = strdup(refs_str);
2685 f2f46662 2020-01-23 tracey free(s);
2688 f2f46662 2020-01-23 tracey if (refs_str == NULL)
2689 f2f46662 2020-01-23 tracey header->refs_str = strdup("");
2690 f2f46662 2020-01-23 tracey free(refs_str);
2692 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->commit_id, header->id);
2693 f2f46662 2020-01-23 tracey if (error)
2694 f2f46662 2020-01-23 tracey return error;
2696 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->tree_id,
2697 f2f46662 2020-01-23 tracey got_object_commit_get_tree_id(header->commit));
2698 f2f46662 2020-01-23 tracey if (error)
2699 f2f46662 2020-01-23 tracey return error;
2701 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_DIFF) {
2702 f2f46662 2020-01-23 tracey parent_id = SIMPLEQ_FIRST(
2703 f2f46662 2020-01-23 tracey got_object_commit_get_parent_ids(header->commit));
2704 f2f46662 2020-01-23 tracey if (parent_id != NULL) {
2705 f2f46662 2020-01-23 tracey id2 = got_object_id_dup(parent_id->id);
2706 f2f46662 2020-01-23 tracey free (parent_id);
2707 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->parent_id, id2);
2708 f2f46662 2020-01-23 tracey if (error)
2709 f2f46662 2020-01-23 tracey return error;
2710 f2f46662 2020-01-23 tracey free(id2);
2712 f2f46662 2020-01-23 tracey header->parent_id = strdup("/dev/null");
2714 f2f46662 2020-01-23 tracey header->parent_id = strdup("");
2716 f2f46662 2020-01-23 tracey header->committer_time =
2717 f2f46662 2020-01-23 tracey got_object_commit_get_committer_time(header->commit);
2719 bd275801 2020-02-03 tracey header->author =
2720 bd275801 2020-02-03 tracey got_object_commit_get_author(header->commit);
2721 27192be7 2020-02-04 tracey header->committer =
2722 27192be7 2020-02-04 tracey got_object_commit_get_committer(header->commit);
2723 070fee27 2020-02-03 stsp if (error)
2724 070fee27 2020-02-03 stsp return error;
2726 070fee27 2020-02-03 stsp /* XXX Doesn't the log message require escaping? */
2727 f2f46662 2020-01-23 tracey error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2728 f2f46662 2020-01-23 tracey if (error)
2729 f2f46662 2020-01-23 tracey return error;
2731 f2f46662 2020-01-23 tracey commit_msg = commit_msg0;
2732 f2f46662 2020-01-23 tracey while (*commit_msg == '\n')
2733 f2f46662 2020-01-23 tracey commit_msg++;
2735 f2f46662 2020-01-23 tracey header->commit_msg = strdup(commit_msg);
2736 f2f46662 2020-01-23 tracey free(commit_msg0);
2737 f2f46662 2020-01-23 tracey return error;
2740 f2f46662 2020-01-23 tracey static const struct got_error *
2741 f2f46662 2020-01-23 tracey gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2743 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2744 f2f46662 2020-01-23 tracey char *in_repo_path = NULL;
2746 f2f46662 2020-01-23 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2747 f2f46662 2020-01-23 tracey if (error)
2748 f2f46662 2020-01-23 tracey return error;
2750 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
2751 f2f46662 2020-01-23 tracey struct got_reference *head_ref;
2752 f2f46662 2020-01-23 tracey error = got_ref_open(&head_ref, header->repo,
2753 f2f46662 2020-01-23 tracey gw_trans->headref, 0);
2754 f2f46662 2020-01-23 tracey if (error)
2755 f2f46662 2020-01-23 tracey return error;
2757 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, head_ref);
2758 f2f46662 2020-01-23 tracey got_ref_close(head_ref);
2759 f2f46662 2020-01-23 tracey if (error)
2760 f2f46662 2020-01-23 tracey return error;
2762 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2763 f2f46662 2020-01-23 tracey header->repo, header->id);
2764 f2f46662 2020-01-23 tracey } else {
2765 f2f46662 2020-01-23 tracey struct got_reference *ref;
2766 f2f46662 2020-01-23 tracey error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2767 f2f46662 2020-01-23 tracey if (error == NULL) {
2768 f2f46662 2020-01-23 tracey int obj_type;
2769 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, ref);
2770 f2f46662 2020-01-23 tracey got_ref_close(ref);
2771 f2f46662 2020-01-23 tracey if (error)
2772 f2f46662 2020-01-23 tracey return error;
2773 f2f46662 2020-01-23 tracey error = got_object_get_type(&obj_type, header->repo,
2774 f2f46662 2020-01-23 tracey header->id);
2775 f2f46662 2020-01-23 tracey if (error)
2776 f2f46662 2020-01-23 tracey return error;
2777 f2f46662 2020-01-23 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
2778 f2f46662 2020-01-23 tracey struct got_tag_object *tag;
2779 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag,
2780 f2f46662 2020-01-23 tracey header->repo, header->id);
2781 f2f46662 2020-01-23 tracey if (error)
2782 f2f46662 2020-01-23 tracey return error;
2783 f2f46662 2020-01-23 tracey if (got_object_tag_get_object_type(tag) !=
2784 f2f46662 2020-01-23 tracey GOT_OBJ_TYPE_COMMIT) {
2785 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2786 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2787 f2f46662 2020-01-23 tracey return error;
2789 f2f46662 2020-01-23 tracey free(header->id);
2790 f2f46662 2020-01-23 tracey header->id = got_object_id_dup(
2791 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag));
2792 f2f46662 2020-01-23 tracey if (header->id == NULL)
2793 f2f46662 2020-01-23 tracey error = got_error_from_errno(
2794 f2f46662 2020-01-23 tracey "got_object_id_dup");
2795 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2796 f2f46662 2020-01-23 tracey if (error)
2797 f2f46662 2020-01-23 tracey return error;
2798 f2f46662 2020-01-23 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2799 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2800 f2f46662 2020-01-23 tracey return error;
2802 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2803 f2f46662 2020-01-23 tracey header->repo, header->id);
2804 f2f46662 2020-01-23 tracey if (error)
2805 f2f46662 2020-01-23 tracey return error;
2807 f2f46662 2020-01-23 tracey if (header->commit == NULL) {
2808 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2809 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2810 f2f46662 2020-01-23 tracey header->repo);
2811 f2f46662 2020-01-23 tracey if (error)
2812 f2f46662 2020-01-23 tracey return error;
2814 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2815 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2816 f2f46662 2020-01-23 tracey header->repo);
2819 f2f46662 2020-01-23 tracey error = got_repo_map_path(&in_repo_path, header->repo,
2820 f2f46662 2020-01-23 tracey gw_trans->repo_path, 1);
2821 f2f46662 2020-01-23 tracey if (error)
2822 f2f46662 2020-01-23 tracey return error;
2824 f2f46662 2020-01-23 tracey if (in_repo_path) {
2825 f2f46662 2020-01-23 tracey header->path = strdup(in_repo_path);
2827 f2f46662 2020-01-23 tracey free(in_repo_path);
2829 f2f46662 2020-01-23 tracey error = got_ref_list(&header->refs, header->repo, NULL,
2830 f2f46662 2020-01-23 tracey got_ref_cmp_by_name, NULL);
2831 f2f46662 2020-01-23 tracey if (error)
2832 f2f46662 2020-01-23 tracey return error;
2834 f2f46662 2020-01-23 tracey error = gw_get_commits(gw_trans, header, limit);
2835 f2f46662 2020-01-23 tracey return error;
2838 ec46ccd7 2020-01-15 tracey struct blame_line {
2839 ec46ccd7 2020-01-15 tracey int annotated;
2840 ec46ccd7 2020-01-15 tracey char *id_str;
2841 ec46ccd7 2020-01-15 tracey char *committer;
2842 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
2845 147269d5 2020-01-15 tracey struct gw_blame_cb_args {
2846 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
2847 ec46ccd7 2020-01-15 tracey int nlines;
2848 ec46ccd7 2020-01-15 tracey int nlines_prec;
2849 ec46ccd7 2020-01-15 tracey int lineno_cur;
2850 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
2851 ec46ccd7 2020-01-15 tracey FILE *f;
2852 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
2853 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
2854 2e676fc5 2020-01-15 tracey struct buf *blamebuf;
2857 ec46ccd7 2020-01-15 tracey static const struct got_error *
2858 147269d5 2020-01-15 tracey gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2860 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
2861 147269d5 2020-01-15 tracey struct gw_blame_cb_args *a = arg;
2862 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
2863 ec46ccd7 2020-01-15 tracey char *line = NULL;
2864 2e676fc5 2020-01-15 tracey size_t linesize = 0, newsize;
2865 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
2866 ec46ccd7 2020-01-15 tracey off_t offset;
2867 ec46ccd7 2020-01-15 tracey struct tm tm;
2868 ec46ccd7 2020-01-15 tracey time_t committer_time;
2870 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
2871 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
2872 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
2874 ec46ccd7 2020-01-15 tracey if (lineno == -1)
2875 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
2877 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
2878 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
2879 ec46ccd7 2020-01-15 tracey if (bline->annotated)
2880 ec46ccd7 2020-01-15 tracey return NULL;
2881 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
2882 ec46ccd7 2020-01-15 tracey if (err)
2883 ec46ccd7 2020-01-15 tracey return err;
2885 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
2886 ec46ccd7 2020-01-15 tracey if (err)
2887 ec46ccd7 2020-01-15 tracey goto done;
2889 ec46ccd7 2020-01-15 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
2890 ec46ccd7 2020-01-15 tracey if (bline->committer == NULL) {
2891 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
2892 ec46ccd7 2020-01-15 tracey goto done;
2895 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
2896 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
2897 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
2898 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2899 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
2900 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
2901 ec46ccd7 2020-01-15 tracey goto done;
2903 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
2905 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
2906 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2907 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
2908 ec46ccd7 2020-01-15 tracey goto done;
2910 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
2911 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
2912 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
2913 ec46ccd7 2020-01-15 tracey goto done;
2916 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
2917 0311ce2d 2020-01-15 tracey char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2918 0311ce2d 2020-01-15 tracey *line_escape = NULL;
2919 ec46ccd7 2020-01-15 tracey size_t len;
2921 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
2922 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
2923 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
2927 ec46ccd7 2020-01-15 tracey committer = bline->committer;
2928 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
2929 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
2930 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
2931 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
2933 ec46ccd7 2020-01-15 tracey *at = '\0';
2934 ec46ccd7 2020-01-15 tracey len = strlen(committer);
2935 ec46ccd7 2020-01-15 tracey if (len >= 9)
2936 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
2938 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
2940 ec46ccd7 2020-01-15 tracey *nl = '\0';
2942 070fee27 2020-02-03 stsp err = gw_html_escape(&line_escape, line);
2946 de6bdba4 2020-01-31 tracey if (a->gw_trans->repo_folder == NULL)
2947 de6bdba4 2020-01-31 tracey a->gw_trans->repo_folder = strdup("");
2948 de6bdba4 2020-01-31 tracey if (a->gw_trans->repo_folder == NULL)
2949 de6bdba4 2020-01-31 tracey goto err;
2950 10d47faf 2020-01-31 tracey asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2951 10d47faf 2020-01-31 tracey a->gw_trans->repo_name, bline->id_str,
2952 10d47faf 2020-01-31 tracey a->gw_trans->repo_file, a->gw_trans->repo_folder,
2953 10d47faf 2020-01-31 tracey bline->id_str, bline->datebuf, committer, line_escape);
2954 ec46ccd7 2020-01-15 tracey a->lineno_cur++;
2955 2e676fc5 2020-01-15 tracey err = buf_puts(&newsize, a->blamebuf, blame_row);
2956 2e676fc5 2020-01-15 tracey if (err)
2957 2e676fc5 2020-01-15 tracey return err;
2959 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2961 0311ce2d 2020-01-15 tracey free(line_escape);
2962 2e676fc5 2020-01-15 tracey free(blame_row);
2965 ec46ccd7 2020-01-15 tracey if (commit)
2966 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
2967 ec46ccd7 2020-01-15 tracey free(line);
2968 ec46ccd7 2020-01-15 tracey return err;
2971 a81f3554 2020-02-03 stsp static const struct got_error *
2972 a81f3554 2020-02-03 stsp gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2974 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
2975 bcbc97d8 2020-01-15 tracey struct got_repository *repo = NULL;
2976 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
2977 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
2978 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
2979 a81f3554 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
2980 147269d5 2020-01-15 tracey struct gw_blame_cb_args bca;
2981 119bf4ed 2020-01-15 tracey int i, obj_type;
2982 ec46ccd7 2020-01-15 tracey size_t filesize;
2984 a81f3554 2020-02-03 stsp *blame_html = NULL;
2986 ec46ccd7 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2987 ec46ccd7 2020-01-15 tracey if (error)
2988 a81f3554 2020-02-03 stsp return error;
2990 a81f3554 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
2991 a81f3554 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2992 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
2993 a81f3554 2020-02-03 stsp gw_trans->repo_file) == -1) {
2994 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2995 2e676fc5 2020-01-15 tracey goto done;
2998 2e676fc5 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, path, 1);
2999 ec46ccd7 2020-01-15 tracey if (error)
3000 ec46ccd7 2020-01-15 tracey goto done;
3002 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3003 147269d5 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT, 1, repo);
3004 ec46ccd7 2020-01-15 tracey if (error)
3005 ec46ccd7 2020-01-15 tracey goto done;
3007 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3008 ec46ccd7 2020-01-15 tracey if (error)
3009 ec46ccd7 2020-01-15 tracey goto done;
3011 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
3012 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
3013 ec46ccd7 2020-01-15 tracey goto done;
3016 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, obj_id);
3017 ec46ccd7 2020-01-15 tracey if (error)
3018 ec46ccd7 2020-01-15 tracey goto done;
3020 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
3021 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3022 ec46ccd7 2020-01-15 tracey goto done;
3025 ec46ccd7 2020-01-15 tracey error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3026 ec46ccd7 2020-01-15 tracey if (error)
3027 ec46ccd7 2020-01-15 tracey goto done;
3029 2e676fc5 2020-01-15 tracey error = buf_alloc(&bca.blamebuf, 0);
3030 2e676fc5 2020-01-15 tracey if (error)
3031 2e676fc5 2020-01-15 tracey goto done;
3033 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
3034 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
3035 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
3036 ec46ccd7 2020-01-15 tracey goto done;
3038 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3039 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
3040 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
3041 ec46ccd7 2020-01-15 tracey goto done;
3043 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
3044 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
3045 ec46ccd7 2020-01-15 tracey bca.nlines--;
3047 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3048 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
3049 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
3050 ec46ccd7 2020-01-15 tracey goto done;
3052 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
3053 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
3054 ec46ccd7 2020-01-15 tracey i = bca.nlines;
3055 ec46ccd7 2020-01-15 tracey while (i > 0) {
3056 ec46ccd7 2020-01-15 tracey i /= 10;
3057 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
3059 ec46ccd7 2020-01-15 tracey bca.repo = repo;
3060 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
3062 147269d5 2020-01-15 tracey error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3063 147269d5 2020-01-15 tracey NULL, NULL);
3064 e46f587c 2020-01-29 tracey if (error)
3065 e46f587c 2020-01-29 tracey goto done;
3066 2e676fc5 2020-01-15 tracey if (buf_len(bca.blamebuf) > 0) {
3067 2e676fc5 2020-01-15 tracey error = buf_putc(bca.blamebuf, '\0');
3068 a81f3554 2020-02-03 stsp if (error)
3069 a81f3554 2020-02-03 stsp goto done;
3070 a81f3554 2020-02-03 stsp *blame_html = strdup(buf_get(bca.blamebuf));
3071 a81f3554 2020-02-03 stsp if (*blame_html == NULL) {
3072 a81f3554 2020-02-03 stsp error = got_error_from_errno("strdup");
3073 a81f3554 2020-02-03 stsp goto done;
3077 e46f587c 2020-01-29 tracey free(bca.line_offsets);
3078 2e676fc5 2020-01-15 tracey free(bca.blamebuf);
3079 2e676fc5 2020-01-15 tracey free(in_repo_path);
3080 2e676fc5 2020-01-15 tracey free(commit_id);
3081 2e676fc5 2020-01-15 tracey free(obj_id);
3082 2e676fc5 2020-01-15 tracey free(path);
3084 4fd4726a 2020-02-03 stsp for (i = 0; i < bca.nlines; i++) {
3085 4fd4726a 2020-02-03 stsp struct blame_line *bline = &bca.lines[i];
3086 4fd4726a 2020-02-03 stsp free(bline->id_str);
3087 4fd4726a 2020-02-03 stsp free(bline->committer);
3089 4fd4726a 2020-02-03 stsp free(bca.lines);
3090 2e676fc5 2020-01-15 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
3091 a81f3554 2020-02-03 stsp error = got_error_from_errno("fclose");
3093 4fd4726a 2020-02-03 stsp got_object_blob_close(blob);
3095 4fd4726a 2020-02-03 stsp got_repo_close(repo);
3096 4fd4726a 2020-02-03 stsp return error;
3099 4fd4726a 2020-02-03 stsp static const struct got_error *
3100 e0857cfe 2020-02-06 tracey gw_output_blob_buf(struct gw_trans *gw_trans)
3102 4fd4726a 2020-02-03 stsp const struct got_error *error = NULL;
3103 4fd4726a 2020-02-03 stsp struct got_repository *repo = NULL;
3104 4fd4726a 2020-02-03 stsp struct got_object_id *obj_id = NULL;
3105 4fd4726a 2020-02-03 stsp struct got_object_id *commit_id = NULL;
3106 4fd4726a 2020-02-03 stsp struct got_blob_object *blob = NULL;
3107 4fd4726a 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
3108 e0857cfe 2020-02-06 tracey int obj_type, set_mime = 0;
3109 e0857cfe 2020-02-06 tracey size_t len, hdrlen;
3110 e0857cfe 2020-02-06 tracey const uint8_t *buf;
3111 e0857cfe 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3113 4fd4726a 2020-02-03 stsp error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3114 4fd4726a 2020-02-03 stsp if (error)
3115 4fd4726a 2020-02-03 stsp return error;
3117 4fd4726a 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
3118 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3119 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
3120 4fd4726a 2020-02-03 stsp gw_trans->repo_file) == -1) {
3121 4fd4726a 2020-02-03 stsp error = got_error_from_errno("asprintf");
3122 4fd4726a 2020-02-03 stsp goto done;
3125 4fd4726a 2020-02-03 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3126 4fd4726a 2020-02-03 stsp if (error)
3127 4fd4726a 2020-02-03 stsp goto done;
3129 4fd4726a 2020-02-03 stsp error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3130 4fd4726a 2020-02-03 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3131 4fd4726a 2020-02-03 stsp if (error)
3132 4fd4726a 2020-02-03 stsp goto done;
3134 4fd4726a 2020-02-03 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3135 4fd4726a 2020-02-03 stsp if (error)
3136 4fd4726a 2020-02-03 stsp goto done;
3138 4fd4726a 2020-02-03 stsp if (obj_id == NULL) {
3139 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_NO_OBJ);
3140 4fd4726a 2020-02-03 stsp goto done;
3143 4fd4726a 2020-02-03 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3144 4fd4726a 2020-02-03 stsp if (error)
3145 4fd4726a 2020-02-03 stsp goto done;
3147 4fd4726a 2020-02-03 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3148 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3149 4fd4726a 2020-02-03 stsp goto done;
3152 4fd4726a 2020-02-03 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3153 4fd4726a 2020-02-03 stsp if (error)
3154 4fd4726a 2020-02-03 stsp goto done;
3156 e0857cfe 2020-02-06 tracey hdrlen = got_object_blob_get_hdrlen(blob);
3158 e0857cfe 2020-02-06 tracey error = got_object_blob_read_block(&len, blob);
3159 e0857cfe 2020-02-06 tracey if (error)
3160 e0857cfe 2020-02-06 tracey goto done;
3161 e0857cfe 2020-02-06 tracey if (len == 0)
3163 e0857cfe 2020-02-06 tracey buf = got_object_blob_get_read_buf(blob);
3166 e0857cfe 2020-02-06 tracey * Skip blob object header first time around,
3167 e0857cfe 2020-02-06 tracey * which also contains a zero byte.
3169 e0857cfe 2020-02-06 tracey buf += hdrlen;
3170 e0857cfe 2020-02-06 tracey if (set_mime == 0) {
3171 e0857cfe 2020-02-06 tracey if (isbinary(buf, len - hdrlen))
3172 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_APP_OCTET_STREAM;
3174 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
3175 e0857cfe 2020-02-06 tracey set_mime = 1;
3176 e0857cfe 2020-02-06 tracey error = gw_display_index(gw_trans);
3177 e0857cfe 2020-02-06 tracey if (error)
3178 e0857cfe 2020-02-06 tracey goto done;
3180 e0857cfe 2020-02-06 tracey khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3181 e0857cfe 2020-02-06 tracey hdrlen = 0;
3182 e0857cfe 2020-02-06 tracey } while (len != 0);
3184 4fd4726a 2020-02-03 stsp free(in_repo_path);
3185 4fd4726a 2020-02-03 stsp free(commit_id);
3186 4fd4726a 2020-02-03 stsp free(obj_id);
3187 4fd4726a 2020-02-03 stsp free(path);
3188 e46f587c 2020-01-29 tracey if (blob)
3189 a81f3554 2020-02-03 stsp got_object_blob_close(blob);
3190 e46f587c 2020-01-29 tracey if (repo)
3191 a81f3554 2020-02-03 stsp got_repo_close(repo);
3192 e0857cfe 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
3193 e0857cfe 2020-02-06 tracey error = gw_kcgi_error(kerr);
3194 a81f3554 2020-02-03 stsp return error;
3197 84bf4df2 2020-02-03 stsp static const struct got_error *
3198 84bf4df2 2020-02-03 stsp gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
3200 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
3201 ec46ccd7 2020-01-15 tracey struct got_repository *repo = NULL;
3202 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
3203 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
3204 bcbc97d8 2020-01-15 tracey struct buf *diffbuf = NULL;
3205 bcbc97d8 2020-01-15 tracey size_t newsize;
3206 84bf4df2 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
3207 84bf4df2 2020-02-03 stsp char *id_str = NULL;
3208 84bf4df2 2020-02-03 stsp char *build_folder = NULL;
3209 84bf4df2 2020-02-03 stsp char *url_html = NULL;
3210 84bf4df2 2020-02-03 stsp const char *class = NULL;
3211 c3bcdfd5 2020-01-29 tracey int nentries, i, class_flip = 0;
3213 84bf4df2 2020-02-03 stsp *tree_html = NULL;
3215 bcbc97d8 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
3216 ec46ccd7 2020-01-15 tracey if (error)
3217 84bf4df2 2020-02-03 stsp return error;
3219 bcbc97d8 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3220 ec46ccd7 2020-01-15 tracey if (error)
3221 bcbc97d8 2020-01-15 tracey goto done;
3223 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL)
3224 ec46ccd7 2020-01-15 tracey path = strdup(gw_trans->repo_folder);
3226 84bf4df2 2020-02-03 stsp error = got_repo_map_path(&in_repo_path, repo,
3227 84bf4df2 2020-02-03 stsp gw_trans->repo_path, 1);
3228 84bf4df2 2020-02-03 stsp if (error)
3229 84bf4df2 2020-02-03 stsp goto done;
3230 bcbc97d8 2020-01-15 tracey free(path);
3231 bcbc97d8 2020-01-15 tracey path = in_repo_path;
3234 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
3235 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
3236 ec46ccd7 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3237 ec46ccd7 2020-01-15 tracey if (error)
3238 ec46ccd7 2020-01-15 tracey goto done;
3239 ec46ccd7 2020-01-15 tracey error = got_ref_resolve(&commit_id, repo, head_ref);
3240 84bf4df2 2020-02-03 stsp if (error)
3241 84bf4df2 2020-02-03 stsp goto done;
3242 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
3245 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL,
3246 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3247 84bf4df2 2020-02-03 stsp if (error)
3248 84bf4df2 2020-02-03 stsp goto done;
3251 f2f46662 2020-01-23 tracey error = got_object_id_str(&gw_trans->commit, commit_id);
3252 bcbc97d8 2020-01-15 tracey if (error)
3253 bcbc97d8 2020-01-15 tracey goto done;
3255 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3256 bcbc97d8 2020-01-15 tracey if (error)
3257 bcbc97d8 2020-01-15 tracey goto done;
3259 bcbc97d8 2020-01-15 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
3260 bcbc97d8 2020-01-15 tracey if (error)
3261 bcbc97d8 2020-01-15 tracey goto done;
3263 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
3264 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
3265 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
3266 ec46ccd7 2020-01-15 tracey const char *modestr = "";
3267 84bf4df2 2020-02-03 stsp mode_t mode;
3269 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
3271 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3272 bcbc97d8 2020-01-15 tracey if (error)
3273 bcbc97d8 2020-01-15 tracey goto done;
3275 84bf4df2 2020-02-03 stsp mode = got_tree_entry_get_mode(te);
3276 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
3277 bcbc97d8 2020-01-15 tracey modestr = "$";
3278 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
3279 bcbc97d8 2020-01-15 tracey modestr = "@";
3280 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
3281 bcbc97d8 2020-01-15 tracey modestr = "/";
3282 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
3283 bcbc97d8 2020-01-15 tracey modestr = "*";
3285 c3bcdfd5 2020-01-29 tracey if (class_flip == 0) {
3286 84bf4df2 2020-02-03 stsp class = "back_lightgray";
3287 c3bcdfd5 2020-01-29 tracey class_flip = 1;
3288 c3bcdfd5 2020-01-29 tracey } else {
3289 84bf4df2 2020-02-03 stsp class = "back_white";
3290 c3bcdfd5 2020-01-29 tracey class_flip = 0;
3293 84bf4df2 2020-02-03 stsp if (S_ISDIR(mode)) {
3294 a5594e37 2020-02-05 tracey if (asprintf(&build_folder, "%s/%s",
3295 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3296 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te)) == -1) {
3297 84bf4df2 2020-02-03 stsp error = got_error_from_errno(
3298 84bf4df2 2020-02-03 stsp "asprintf");
3299 84bf4df2 2020-02-03 stsp goto done;
3302 88d59c36 2020-01-29 stsp if (asprintf(&url_html, folder_html,
3303 ec46ccd7 2020-01-15 tracey gw_trans->repo_name, gw_trans->action_name,
3304 ec46ccd7 2020-01-15 tracey gw_trans->commit, build_folder,
3305 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
3306 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
3307 ec46ccd7 2020-01-15 tracey goto done;
3309 84bf4df2 2020-02-03 stsp if (asprintf(&tree_row, tree_line, class, url_html,
3310 84bf4df2 2020-02-03 stsp class) == -1) {
3311 84bf4df2 2020-02-03 stsp error = got_error_from_errno("asprintf");
3312 84bf4df2 2020-02-03 stsp goto done;
3314 ec46ccd7 2020-01-15 tracey } else {
3315 88d59c36 2020-01-29 stsp if (asprintf(&url_html, file_html, gw_trans->repo_name,
3316 e46f587c 2020-01-29 tracey "blob", gw_trans->commit,
3317 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
3318 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3319 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
3320 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
3321 ec46ccd7 2020-01-15 tracey goto done;
3324 e46f587c 2020-01-29 tracey if (asprintf(&tree_row, tree_line_with_navs, class,
3325 84bf4df2 2020-02-03 stsp url_html, class, gw_trans->repo_name, "blob",
3326 84bf4df2 2020-02-03 stsp gw_trans->commit, got_tree_entry_get_name(te),
3327 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3328 84bf4df2 2020-02-03 stsp "blob", gw_trans->repo_name,
3329 84bf4df2 2020-02-03 stsp "blame", gw_trans->commit,
3330 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
3331 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3332 84bf4df2 2020-02-03 stsp "blame") == -1) {
3333 e46f587c 2020-01-29 tracey error = got_error_from_errno("asprintf");
3334 e46f587c 2020-01-29 tracey goto done;
3338 bcbc97d8 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tree_row);
3339 ec46ccd7 2020-01-15 tracey if (error)
3340 ec46ccd7 2020-01-15 tracey goto done;
3342 bcbc97d8 2020-01-15 tracey free(id_str);
3343 84bf4df2 2020-02-03 stsp id_str = NULL;
3344 ec46ccd7 2020-01-15 tracey free(url_html);
3345 84bf4df2 2020-02-03 stsp url_html = NULL;
3346 ec46ccd7 2020-01-15 tracey free(tree_row);
3347 84bf4df2 2020-02-03 stsp tree_row = NULL;
3348 84bf4df2 2020-02-03 stsp free(build_folder);
3349 84bf4df2 2020-02-03 stsp build_folder = NULL;
3352 bcbc97d8 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
3353 bcbc97d8 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
3354 84bf4df2 2020-02-03 stsp if (error)
3355 84bf4df2 2020-02-03 stsp goto done;
3356 84bf4df2 2020-02-03 stsp *tree_html = strdup(buf_get(diffbuf));
3357 84bf4df2 2020-02-03 stsp if (*tree_html == NULL) {
3358 84bf4df2 2020-02-03 stsp error = got_error_from_errno("strdup");
3359 84bf4df2 2020-02-03 stsp goto done;
3363 bcbc97d8 2020-01-15 tracey if (tree)
3364 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
3365 2e676fc5 2020-01-15 tracey if (repo)
3366 2e676fc5 2020-01-15 tracey got_repo_close(repo);
3368 84bf4df2 2020-02-03 stsp free(id_str);
3369 84bf4df2 2020-02-03 stsp free(url_html);
3370 84bf4df2 2020-02-03 stsp free(tree_row);
3371 2e676fc5 2020-01-15 tracey free(in_repo_path);
3372 bcbc97d8 2020-01-15 tracey free(tree_id);
3373 bcbc97d8 2020-01-15 tracey free(diffbuf);
3374 84bf4df2 2020-02-03 stsp free(build_folder);
3375 84bf4df2 2020-02-03 stsp return error;
3378 51d4a92d 2020-02-03 tracey static const struct got_error *
3379 51d4a92d 2020-02-03 tracey gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3381 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
3382 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
3383 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
3384 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
3385 51d4a92d 2020-02-03 tracey char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3386 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
3387 87f9ebf5 2020-01-15 tracey size_t newsize;
3389 51d4a92d 2020-02-03 tracey *head_html = NULL;
3391 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
3393 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
3394 ec46ccd7 2020-01-15 tracey if (error)
3395 6c6c85af 2020-01-15 tracey return NULL;
3397 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3398 ec46ccd7 2020-01-15 tracey if (error)
3399 87f9ebf5 2020-01-15 tracey goto done;
3401 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3403 87f9ebf5 2020-01-15 tracey if (error)
3404 87f9ebf5 2020-01-15 tracey goto done;
3406 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
3407 87f9ebf5 2020-01-15 tracey char *refname;
3409 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
3410 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
3411 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
3412 87f9ebf5 2020-01-15 tracey goto done;
3415 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
3416 87f9ebf5 2020-01-15 tracey free(refname);
3417 87f9ebf5 2020-01-15 tracey continue;
3420 017d6da3 2020-01-29 tracey error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3421 017d6da3 2020-01-29 tracey refname, TM_DIFF);
3422 d126c1b7 2020-01-29 stsp if (error)
3423 d126c1b7 2020-01-29 stsp goto done;
3425 88d59c36 2020-01-29 stsp if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3426 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
3427 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
3428 88d59c36 2020-01-29 stsp refname) == -1) {
3429 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
3430 87f9ebf5 2020-01-15 tracey goto done;
3433 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
3434 87f9ebf5 2020-01-15 tracey refname += 11;
3436 88d59c36 2020-01-29 stsp if (asprintf(&head_row, heads_row, age, refname,
3437 88d59c36 2020-01-29 stsp head_navs_disp) == -1) {
3438 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
3439 87f9ebf5 2020-01-15 tracey goto done;
3442 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
3444 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
3445 87f9ebf5 2020-01-15 tracey free(head_row);
3448 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
3449 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
3450 51d4a92d 2020-02-03 tracey *head_html = strdup(buf_get(diffbuf));
3451 51d4a92d 2020-02-03 tracey if (*head_html == NULL)
3452 51d4a92d 2020-02-03 tracey error = got_error_from_errno("strdup");
3455 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
3456 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
3457 87f9ebf5 2020-01-15 tracey if (repo)
3458 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
3459 51d4a92d 2020-02-03 tracey return error;
3462 8d4d2453 2020-01-15 tracey static char *
3463 54415d85 2020-01-15 tracey gw_get_site_link(struct gw_trans *gw_trans)
3465 eb89db64 2020-02-03 stsp char *link = NULL, *repo = NULL, *action = NULL;
3467 eb89db64 2020-02-03 stsp if (gw_trans->repo_name != NULL &&
3468 eb89db64 2020-02-03 stsp asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3469 eb89db64 2020-02-03 stsp gw_trans->repo_name, gw_trans->repo_name) == -1)
3470 eb89db64 2020-02-03 stsp return NULL;
3472 eb89db64 2020-02-03 stsp if (gw_trans->action_name != NULL &&
3473 eb89db64 2020-02-03 stsp asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3474 eb89db64 2020-02-03 stsp free(repo);
3475 eb89db64 2020-02-03 stsp return NULL;
3478 88d59c36 2020-01-29 stsp if (asprintf(&link, site_link, GOTWEB,
3479 eb89db64 2020-02-03 stsp gw_trans->gw_conf->got_site_link,
3480 eb89db64 2020-02-03 stsp repo ? repo : "", action ? action : "") == -1) {
3481 eb89db64 2020-02-03 stsp free(repo);
3482 eb89db64 2020-02-03 stsp free(action);
3483 2c251c14 2020-01-15 tracey return NULL;
3486 eb89db64 2020-02-03 stsp free(repo);
3487 eb89db64 2020-02-03 stsp free(action);
3488 2c251c14 2020-01-15 tracey return link;
3491 83eb9a68 2020-02-03 stsp static const struct got_error *
3492 f7cc9480 2020-02-05 tracey gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3494 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
3495 f7cc9480 2020-02-05 tracey char *color = NULL;
3496 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
3498 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
3499 ec46ccd7 2020-01-15 tracey color = "diff_minus";
3500 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "+", 1) == 0)
3501 ec46ccd7 2020-01-15 tracey color = "diff_plus";
3502 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
3503 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
3504 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
3505 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
3506 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit +", 8) == 0)
3507 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3508 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit -", 8) == 0)
3509 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3510 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob +", 6) == 0)
3511 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3512 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob -", 6) == 0)
3513 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3514 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file +", 6) == 0)
3515 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3516 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file -", 6) == 0)
3517 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3518 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "from:", 5) == 0)
3519 ec46ccd7 2020-01-15 tracey color = "diff_author";
3520 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "via:", 4) == 0)
3521 ec46ccd7 2020-01-15 tracey color = "diff_author";
3522 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "date:", 5) == 0)
3523 ec46ccd7 2020-01-15 tracey color = "diff_date";
3524 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3525 f7cc9480 2020-02-05 tracey "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3526 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
3527 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
3528 83eb9a68 2020-02-03 stsp return error;
3532 070fee27 2020-02-03 stsp * XXX This function should not exist.
3533 070fee27 2020-02-03 stsp * We should let khtml_puts(3) handle HTML escaping.
3535 070fee27 2020-02-03 stsp static const struct got_error *
3536 070fee27 2020-02-03 stsp gw_html_escape(char **escaped_html, const char *orig_html)
3538 070fee27 2020-02-03 stsp const struct got_error *error = NULL;
3539 070fee27 2020-02-03 stsp struct escape_pair {
3541 070fee27 2020-02-03 stsp const char *s;
3542 070fee27 2020-02-03 stsp } esc[] = {
3543 070fee27 2020-02-03 stsp { '>', ">" },
3544 070fee27 2020-02-03 stsp { '<', "<" },
3545 070fee27 2020-02-03 stsp { '&', "&" },
3546 070fee27 2020-02-03 stsp { '"', """ },
3547 070fee27 2020-02-03 stsp { '\'', "'" },
3548 070fee27 2020-02-03 stsp { '\n', "<br />" },
3550 070fee27 2020-02-03 stsp size_t orig_len, len;
3551 070fee27 2020-02-03 stsp int i, j, x;
3553 070fee27 2020-02-03 stsp orig_len = strlen(orig_html);
3554 070fee27 2020-02-03 stsp len = orig_len;
3555 070fee27 2020-02-03 stsp for (i = 0; i < orig_len; i++) {
3556 070fee27 2020-02-03 stsp for (j = 0; j < nitems(esc); j++) {
3557 070fee27 2020-02-03 stsp if (orig_html[i] != esc[j].c)
3559 070fee27 2020-02-03 stsp len += strlen(esc[j].s) - 1 /* escaped char */;
3563 070fee27 2020-02-03 stsp *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3564 070fee27 2020-02-03 stsp if (*escaped_html == NULL)
3565 070fee27 2020-02-03 stsp return got_error_from_errno("calloc");
3568 070fee27 2020-02-03 stsp for (i = 0; i < orig_len; i++) {
3569 070fee27 2020-02-03 stsp int escaped = 0;
3570 070fee27 2020-02-03 stsp for (j = 0; j < nitems(esc); j++) {
3571 070fee27 2020-02-03 stsp if (orig_html[i] != esc[j].c)
3574 070fee27 2020-02-03 stsp if (strlcat(*escaped_html, esc[j].s, len + 1)
3575 070fee27 2020-02-03 stsp >= len + 1) {
3576 070fee27 2020-02-03 stsp error = got_error(GOT_ERR_NO_SPACE);
3577 070fee27 2020-02-03 stsp goto done;
3579 070fee27 2020-02-03 stsp x += strlen(esc[j].s);
3580 070fee27 2020-02-03 stsp escaped = 1;
3583 070fee27 2020-02-03 stsp if (!escaped) {
3584 070fee27 2020-02-03 stsp (*escaped_html)[x] = orig_html[i];
3589 070fee27 2020-02-03 stsp if (error) {
3590 070fee27 2020-02-03 stsp free(*escaped_html);
3591 070fee27 2020-02-03 stsp *escaped_html = NULL;
3593 070fee27 2020-02-03 stsp (*escaped_html)[x] = '\0';
3595 070fee27 2020-02-03 stsp return error;
3599 c08369d7 2020-01-15 tracey main(int argc, char *argv[])
3601 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
3602 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
3603 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
3604 2c251c14 2020-01-15 tracey const char *page = "index";
3605 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
3606 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
3608 54415d85 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3609 2c251c14 2020-01-15 tracey errx(1, "malloc");
3611 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3612 2c251c14 2020-01-15 tracey errx(1, "malloc");
3614 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3615 2c251c14 2020-01-15 tracey errx(1, "malloc");
3617 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3618 2c251c14 2020-01-15 tracey errx(1, "malloc");
3620 c25c2314 2020-01-28 stsp kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3621 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK) {
3622 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
3623 c119608e 2020-01-28 stsp goto done;
3626 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
3627 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
3628 4ceb8155 2020-01-15 tracey gw_malloc = 0;
3629 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
3630 c119608e 2020-01-28 stsp goto done;
3633 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
3634 f2f46662 2020-01-23 tracey TAILQ_INIT(&gw_trans->gw_headers);
3636 2c251c14 2020-01-15 tracey gw_trans->page = 0;
3637 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
3638 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
3639 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
3640 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
3641 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
3642 54415d85 2020-01-15 tracey gw_trans->gw_tmpl->key = gw_templs;
3643 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3644 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
3645 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
3646 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3647 c119608e 2020-01-28 stsp if (error)
3648 2c251c14 2020-01-15 tracey goto done;
3650 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
3651 2c251c14 2020-01-15 tracey if (error)
3652 c119608e 2020-01-28 stsp goto done;
3654 017d6da3 2020-01-29 tracey if (gw_trans->action == GW_BLOB)
3655 017d6da3 2020-01-29 tracey error = gw_blob(gw_trans);
3657 017d6da3 2020-01-29 tracey error = gw_display_index(gw_trans);
3659 c119608e 2020-01-28 stsp if (error) {
3660 c119608e 2020-01-28 stsp gw_trans->mime = KMIME_TEXT_PLAIN;
3661 c119608e 2020-01-28 stsp gw_trans->action = GW_ERR;
3662 6d8d8a26 2020-01-29 stsp gw_display_error(gw_trans, error);
3664 2c251c14 2020-01-15 tracey if (gw_malloc) {
3665 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
3666 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
3667 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
3668 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
3669 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
3670 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
3671 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
3672 2c251c14 2020-01-15 tracey free(gw_trans->commit);
3673 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
3674 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
3675 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
3676 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
3677 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
3679 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3680 2c251c14 2020-01-15 tracey free(dir->name);
3681 2c251c14 2020-01-15 tracey free(dir->description);
3682 2c251c14 2020-01-15 tracey free(dir->age);
3683 2c251c14 2020-01-15 tracey free(dir->url);
3684 2c251c14 2020-01-15 tracey free(dir->path);
3685 2c251c14 2020-01-15 tracey free(dir);
3690 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);