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 if (n == 0)
337 e0857cfe 2020-02-06 tracey return 0;
339 e0857cfe 2020-02-06 tracey for (i = 0; i < n; i++)
340 e0857cfe 2020-02-06 tracey if (buf[i] == 0)
341 e0857cfe 2020-02-06 tracey return 1;
342 e0857cfe 2020-02-06 tracey return 0;
345 32cd4d18 2020-02-03 stsp static const struct got_error *
346 54415d85 2020-01-15 tracey gw_blame(struct gw_trans *gw_trans)
348 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
349 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
350 f2f46662 2020-01-23 tracey char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
351 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
352 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
354 6bee13de 2020-01-16 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
355 f2f46662 2020-01-23 tracey NULL) == -1)
356 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
358 f2f46662 2020-01-23 tracey if ((header = gw_init_header()) == NULL)
359 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
361 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
362 ec46ccd7 2020-01-15 tracey if (error)
365 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
366 f2f46662 2020-01-23 tracey if (error)
369 a81f3554 2020-02-03 stsp error = gw_get_file_blame_blob(&blame_html, gw_trans);
373 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, header->committer_time, TM_LONG);
376 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
377 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
381 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
384 55ccf528 2020-02-03 stsp if (asprintf(&blame_html_disp, blame_header, age_html,
385 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(escaped_commit_msg), blame_html) == -1) {
386 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
390 6d9fc692 2020-01-28 stsp if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
391 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
395 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, blame);
396 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
397 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
399 f2f46662 2020-01-23 tracey got_ref_list_free(&header->refs);
400 f2f46662 2020-01-23 tracey gw_free_headers(header);
401 f2f46662 2020-01-23 tracey free(blame_html_disp);
402 f2f46662 2020-01-23 tracey free(blame_html);
403 f2f46662 2020-01-23 tracey free(blame);
404 070fee27 2020-02-03 stsp free(escaped_commit_msg);
405 2c251c14 2020-01-15 tracey return error;
408 2c251c14 2020-01-15 tracey static const struct got_error *
409 e46f587c 2020-01-29 tracey gw_blob(struct gw_trans *gw_trans)
411 e46f587c 2020-01-29 tracey const struct got_error *error = NULL;
412 e46f587c 2020-01-29 tracey struct gw_header *header = NULL;
414 e46f587c 2020-01-29 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
415 e46f587c 2020-01-29 tracey NULL) == -1)
416 e46f587c 2020-01-29 tracey return got_error_from_errno("pledge");
418 e46f587c 2020-01-29 tracey if ((header = gw_init_header()) == NULL)
419 e46f587c 2020-01-29 tracey return got_error_from_errno("malloc");
421 e46f587c 2020-01-29 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
422 e46f587c 2020-01-29 tracey if (error)
423 e46f587c 2020-01-29 tracey goto done;
425 e46f587c 2020-01-29 tracey error = gw_get_header(gw_trans, header, 1);
426 e46f587c 2020-01-29 tracey if (error)
427 e46f587c 2020-01-29 tracey goto done;
429 e0857cfe 2020-02-06 tracey error = gw_output_blob_buf(gw_trans);
431 e46f587c 2020-01-29 tracey got_ref_list_free(&header->refs);
432 e46f587c 2020-01-29 tracey gw_free_headers(header);
433 e46f587c 2020-01-29 tracey return error;
436 e46f587c 2020-01-29 tracey static const struct got_error *
437 f2f46662 2020-01-23 tracey gw_diff(struct gw_trans *gw_trans)
439 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
440 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
441 f7cc9480 2020-02-05 tracey char *age = NULL, *escaped_commit_msg = NULL;
442 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
444 f2f46662 2020-01-23 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
445 f2f46662 2020-01-23 tracey NULL) == -1)
446 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
448 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
449 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
451 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
452 8087c3c5 2020-01-15 tracey if (error)
455 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
456 f2f46662 2020-01-23 tracey if (error)
459 f7cc9480 2020-02-05 tracey /* diff header */
460 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
461 f7cc9480 2020-02-05 tracey "diff_header_wrapper", KATTR__MAX);
462 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
463 f7cc9480 2020-02-05 tracey goto done;
464 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
465 f7cc9480 2020-02-05 tracey "diff_header", KATTR__MAX);
466 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
467 f7cc9480 2020-02-05 tracey goto done;
468 f7cc9480 2020-02-05 tracey error = gw_gen_diff_header(gw_trans, header->parent_id,
469 f7cc9480 2020-02-05 tracey header->commit_id);
470 f7cc9480 2020-02-05 tracey if (error)
471 f7cc9480 2020-02-05 tracey goto done;
472 f7cc9480 2020-02-05 tracey error = gw_gen_commit_header(gw_trans, header->commit_id,
473 f7cc9480 2020-02-05 tracey header->refs_str);
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_tree_header(gw_trans, header->tree_id);
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_author_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_gen_committer_header(gw_trans, header->author);
483 f7cc9480 2020-02-05 tracey if (error)
484 f7cc9480 2020-02-05 tracey goto done;
485 f7cc9480 2020-02-05 tracey error = gw_get_time_str(&age, header->committer_time,
486 f7cc9480 2020-02-05 tracey TM_LONG);
487 f7cc9480 2020-02-05 tracey if (error)
488 f7cc9480 2020-02-05 tracey goto done;
489 f7cc9480 2020-02-05 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
490 f7cc9480 2020-02-05 tracey if (error)
491 f7cc9480 2020-02-05 tracey goto done;
493 f7cc9480 2020-02-05 tracey * XXX: keeping this for now, since kcgihtml does not convert
494 f7cc9480 2020-02-05 tracey * \n into <br /> yet.
496 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
499 f7cc9480 2020-02-05 tracey error = gw_gen_commit_msg_header(gw_trans, header->commit_msg);
500 f7cc9480 2020-02-05 tracey if (error)
502 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
503 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
505 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
506 f7cc9480 2020-02-05 tracey "dotted_line", KATTR__MAX);
507 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
508 f7cc9480 2020-02-05 tracey goto done;
509 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
510 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
511 f7cc9480 2020-02-05 tracey goto done;
513 f7cc9480 2020-02-05 tracey /* diff */
514 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
515 f7cc9480 2020-02-05 tracey "diff", KATTR__MAX);
516 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
517 f7cc9480 2020-02-05 tracey goto done;
518 f7cc9480 2020-02-05 tracey error = gw_get_diff(gw_trans, header);
519 f7cc9480 2020-02-05 tracey if (error)
520 f7cc9480 2020-02-05 tracey goto done;
522 f7cc9480 2020-02-05 tracey /* diff content close */
523 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
524 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
525 f7cc9480 2020-02-05 tracey goto done;
527 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
528 f2f46662 2020-01-23 tracey gw_free_headers(header);
530 070fee27 2020-02-03 stsp free(escaped_commit_msg);
531 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
532 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
533 2204c934 2020-01-15 tracey return error;
536 2204c934 2020-01-15 tracey static const struct got_error *
537 54415d85 2020-01-15 tracey gw_index(struct gw_trans *gw_trans)
539 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
540 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
541 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
542 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
543 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
545 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
546 6bee13de 2020-01-16 tracey NULL) == -1) {
547 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
548 6bee13de 2020-01-16 tracey return error;
551 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
552 46b9c89b 2020-01-15 tracey if (error)
553 46b9c89b 2020-01-15 tracey return error;
555 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
556 46b9c89b 2020-01-15 tracey if (error)
557 2c251c14 2020-01-15 tracey return error;
559 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
560 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
561 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
563 bce5dac1 2020-01-28 stsp if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
564 bce5dac1 2020-01-28 stsp if (asprintf(&html, index_projects_empty,
565 bce5dac1 2020-01-28 stsp gw_trans->gw_conf->got_repos_path) == -1)
566 bce5dac1 2020-01-28 stsp return got_error_from_errno("asprintf");
567 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
568 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
569 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
570 bce5dac1 2020-01-28 stsp free(html);
571 c25c2314 2020-01-28 stsp return error;
574 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
577 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
578 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
579 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
580 2c251c14 2020-01-15 tracey prev_disp++;
581 2c251c14 2020-01-15 tracey continue;
584 2c251c14 2020-01-15 tracey prev_disp++;
586 f2f46662 2020-01-23 tracey if (error)
587 f2f46662 2020-01-23 tracey return error;
588 88d59c36 2020-01-29 stsp if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
589 88d59c36 2020-01-29 stsp gw_dir->name, gw_dir->name) == -1)
590 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
592 88d59c36 2020-01-29 stsp if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
593 9a1cc63f 2020-02-03 stsp gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
594 9a1cc63f 2020-02-03 stsp gw_dir->age,
595 88d59c36 2020-01-29 stsp navs) == -1)
596 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
598 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
599 2c251c14 2020-01-15 tracey free(navs);
600 2c251c14 2020-01-15 tracey free(html);
601 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
602 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
604 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
605 2c251c14 2020-01-15 tracey continue;
607 c25c2314 2020-01-28 stsp if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
608 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
609 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
610 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
611 c25c2314 2020-01-28 stsp } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
612 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
613 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
614 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
615 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
616 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
617 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
620 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
621 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
622 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
623 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
624 88d59c36 2020-01-29 stsp if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
625 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
626 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, prev);
627 2c251c14 2020-01-15 tracey free(prev);
628 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
629 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
632 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
633 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
634 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
636 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
637 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
638 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
639 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
640 88d59c36 2020-01-29 stsp if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
641 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
642 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, next);
643 2c251c14 2020-01-15 tracey free(next);
644 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
645 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
646 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
647 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
648 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
649 2c251c14 2020-01-15 tracey next_disp = 0;
653 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
654 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
655 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
656 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
657 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
658 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
659 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
662 2c251c14 2020-01-15 tracey next_disp++;
664 2c251c14 2020-01-15 tracey return error;
667 2c251c14 2020-01-15 tracey static const struct got_error *
668 f2f46662 2020-01-23 tracey gw_commits(struct gw_trans *gw_trans)
670 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
671 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
672 f7cc9480 2020-02-05 tracey char *age = NULL, *escaped_commit_msg = NULL;
673 21a55cc7 2020-02-04 tracey char *href_diff = NULL, *href_tree = NULL;
674 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
676 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
677 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
679 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
680 6bee13de 2020-01-16 tracey NULL) == -1) {
681 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
685 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
686 8087c3c5 2020-01-15 tracey if (error)
689 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
690 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
694 21a55cc7 2020-02-04 tracey /* commit content */
695 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
696 21a55cc7 2020-02-04 tracey /* commit line */
697 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
698 21a55cc7 2020-02-04 tracey "commits_line_wrapper", KATTR__MAX);
699 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
701 21a55cc7 2020-02-04 tracey error = gw_gen_commit_header(gw_trans, n_header->commit_id,
702 21a55cc7 2020-02-04 tracey n_header->refs_str);
703 f7cc9480 2020-02-05 tracey if (error)
704 f7cc9480 2020-02-05 tracey goto done;
705 21a55cc7 2020-02-04 tracey error = gw_gen_author_header(gw_trans, n_header->author);
706 21a55cc7 2020-02-04 tracey if (error)
707 21a55cc7 2020-02-04 tracey goto done;
708 21a55cc7 2020-02-04 tracey error = gw_gen_committer_header(gw_trans, n_header->author);
709 21a55cc7 2020-02-04 tracey if (error)
710 21a55cc7 2020-02-04 tracey goto done;
711 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, n_header->committer_time,
715 21a55cc7 2020-02-04 tracey error = gw_gen_age_header(gw_trans, age ?age : "");
716 21a55cc7 2020-02-04 tracey if (error)
718 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
719 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
720 21a55cc7 2020-02-04 tracey goto done;
722 21a55cc7 2020-02-04 tracey /* dotted line */
723 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
724 21a55cc7 2020-02-04 tracey "dotted_line", KATTR__MAX);
725 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
726 21a55cc7 2020-02-04 tracey goto done;
727 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
728 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
729 21a55cc7 2020-02-04 tracey goto done;
731 21a55cc7 2020-02-04 tracey /* commit */
732 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
733 21a55cc7 2020-02-04 tracey "commit", KATTR__MAX);
734 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
735 21a55cc7 2020-02-04 tracey goto done;
737 21a55cc7 2020-02-04 tracey * XXX: keeping this for now, since kcgihtml does not convert
738 21a55cc7 2020-02-04 tracey * \n into <br /> yet.
740 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg,
741 070fee27 2020-02-03 stsp n_header->commit_msg);
744 21a55cc7 2020-02-04 tracey kerr = khttp_puts(gw_trans->gw_req, escaped_commit_msg);
745 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
746 21a55cc7 2020-02-04 tracey goto done;
747 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
748 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
749 21a55cc7 2020-02-04 tracey goto done;
751 21a55cc7 2020-02-04 tracey /* navs */
753 21a55cc7 2020-02-04 tracey /* XXX: create gen code for this */
754 21a55cc7 2020-02-04 tracey /* build diff nav */
755 21a55cc7 2020-02-04 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
756 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
757 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
760 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
761 21a55cc7 2020-02-04 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
762 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
763 21a55cc7 2020-02-04 tracey goto done;
764 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
765 21a55cc7 2020-02-04 tracey KATTR_ID, "navs", KATTR__MAX);
766 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
767 21a55cc7 2020-02-04 tracey goto done;
768 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
769 21a55cc7 2020-02-04 tracey KATTR_HREF, href_diff, KATTR__MAX);
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_puts(gw_trans->gw_html_req, "diff");
773 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
774 21a55cc7 2020-02-04 tracey goto done;
775 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
776 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
777 21a55cc7 2020-02-04 tracey goto done;
779 21a55cc7 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
780 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
781 21a55cc7 2020-02-04 tracey goto done;
783 21a55cc7 2020-02-04 tracey /* XXX: create gen code for this */
784 21a55cc7 2020-02-04 tracey /* build tree nav */
785 21a55cc7 2020-02-04 tracey if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
786 21a55cc7 2020-02-04 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
787 21a55cc7 2020-02-04 tracey error = got_error_from_errno("asprintf");
788 21a55cc7 2020-02-04 tracey goto done;
790 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
791 21a55cc7 2020-02-04 tracey KATTR_HREF, href_tree, KATTR__MAX);
792 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
793 21a55cc7 2020-02-04 tracey goto done;
794 21a55cc7 2020-02-04 tracey khtml_puts(gw_trans->gw_html_req, "tree");
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;
798 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
799 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
800 21a55cc7 2020-02-04 tracey goto done;
802 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
803 21a55cc7 2020-02-04 tracey "solid_line", KATTR__MAX);
804 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
805 21a55cc7 2020-02-04 tracey goto done;
806 b4fba448 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
807 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
808 21a55cc7 2020-02-04 tracey goto done;
811 55ccf528 2020-02-03 stsp age = NULL;
812 070fee27 2020-02-03 stsp free(escaped_commit_msg);
813 070fee27 2020-02-03 stsp escaped_commit_msg = NULL;
816 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
817 f2f46662 2020-01-23 tracey gw_free_headers(header);
818 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
819 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
821 21a55cc7 2020-02-04 tracey free(href_diff);
822 21a55cc7 2020-02-04 tracey free(href_tree);
823 070fee27 2020-02-03 stsp free(escaped_commit_msg);
824 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
825 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
826 2c251c14 2020-01-15 tracey return error;
829 2c251c14 2020-01-15 tracey static const struct got_error *
830 f2f46662 2020-01-23 tracey gw_briefs(struct gw_trans *gw_trans)
832 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
833 bd275801 2020-02-03 tracey struct gw_header *header = NULL, *n_header = NULL;
834 bd275801 2020-02-03 tracey char *age = NULL, *age_html = NULL;
835 bd275801 2020-02-03 tracey char *href_diff = NULL, *href_tree = NULL;
836 bd275801 2020-02-03 tracey char *newline, *smallerthan;
837 ef72fe2c 2020-02-04 stsp enum kcgi_err kerr = KCGI_OK;
839 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
840 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
842 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
843 6bee13de 2020-01-16 tracey NULL) == -1) {
844 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
848 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
849 8087c3c5 2020-01-15 tracey if (error)
852 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_SUMMARY)
853 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
855 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
856 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
860 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
861 bd275801 2020-02-03 tracey error = gw_get_time_str(&age, n_header->committer_time,
862 bd275801 2020-02-03 tracey TM_DIFF);
863 bd275801 2020-02-03 tracey if (error)
864 bd275801 2020-02-03 tracey goto done;
866 bd275801 2020-02-03 tracey /* briefs wrapper */
867 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
868 bd275801 2020-02-03 tracey KATTR_ID, "briefs_wrapper", KATTR__MAX);
869 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
870 bd275801 2020-02-03 tracey goto done;
872 bd275801 2020-02-03 tracey /* briefs age */
873 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
874 bd275801 2020-02-03 tracey KATTR_ID, "briefs_age", KATTR__MAX);
875 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
876 bd275801 2020-02-03 tracey goto done;
877 bd275801 2020-02-03 tracey if (asprintf(&age_html, "%s", age ? age : "") == -1) {
878 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
881 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, age_html);
882 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
883 bd275801 2020-02-03 tracey goto done;
884 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
885 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
886 bd275801 2020-02-03 tracey goto done;
888 bd275801 2020-02-03 tracey /* briefs author */
889 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
890 bd275801 2020-02-03 tracey KATTR_ID, "briefs_author", KATTR__MAX);
891 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
892 bd275801 2020-02-03 tracey goto done;
893 bd275801 2020-02-03 tracey smallerthan = strchr(n_header->author, '<');
894 bd275801 2020-02-03 tracey if (smallerthan)
895 bd275801 2020-02-03 tracey *smallerthan = '\0';
896 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->author);
897 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
898 bd275801 2020-02-03 tracey goto done;
899 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
900 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
901 bd275801 2020-02-03 tracey goto done;
903 bd275801 2020-02-03 tracey /* briefs log */
904 52f8346c 2020-02-03 tracey if (asprintf(&href_diff, "?path=%s&action=diff&commit=%s",
905 52f8346c 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
906 52f8346c 2020-02-03 tracey error = got_error_from_errno("asprintf");
907 52f8346c 2020-02-03 tracey goto done;
909 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
910 bd275801 2020-02-03 tracey KATTR_ID, "briefs_log", KATTR__MAX);
911 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
912 bd275801 2020-02-03 tracey goto done;
913 f2f46662 2020-01-23 tracey newline = strchr(n_header->commit_msg, '\n');
914 f2f46662 2020-01-23 tracey if (newline)
915 f2f46662 2020-01-23 tracey *newline = '\0';
916 52f8346c 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
917 52f8346c 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
918 52f8346c 2020-02-03 tracey if (kerr != KCGI_OK)
919 52f8346c 2020-02-03 tracey goto done;
920 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, n_header->commit_msg);
921 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
923 52f8346c 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
924 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
925 bd275801 2020-02-03 tracey goto done;
927 bd275801 2020-02-03 tracey /* build diff nav */
928 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
929 bd275801 2020-02-03 tracey KATTR_ID, "navs_wrapper", KATTR__MAX);
930 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
931 bd275801 2020-02-03 tracey goto done;
932 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
933 bd275801 2020-02-03 tracey KATTR_ID, "navs", KATTR__MAX);
934 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
935 bd275801 2020-02-03 tracey goto done;
936 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
937 bd275801 2020-02-03 tracey KATTR_HREF, href_diff, KATTR__MAX);
938 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
940 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "diff");
941 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
942 bd275801 2020-02-03 tracey goto done;
943 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
944 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
945 bd275801 2020-02-03 tracey goto done;
947 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, " | ");
948 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
949 bd275801 2020-02-03 tracey goto done;
951 bd275801 2020-02-03 tracey /* build tree nav */
952 bd275801 2020-02-03 tracey if (asprintf(&href_tree, "?path=%s&action=tree&commit=%s",
953 bd275801 2020-02-03 tracey gw_trans->repo_name, n_header->commit_id) == -1) {
954 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
957 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
958 bd275801 2020-02-03 tracey KATTR_HREF, href_tree, KATTR__MAX);
959 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
960 bd275801 2020-02-03 tracey goto done;
961 bd275801 2020-02-03 tracey khtml_puts(gw_trans->gw_html_req, "tree");
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;
965 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
966 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
967 bd275801 2020-02-03 tracey goto done;
969 bd275801 2020-02-03 tracey /* dotted line */
970 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
971 bd275801 2020-02-03 tracey KATTR_ID, "dotted_line", KATTR__MAX);
972 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
973 bd275801 2020-02-03 tracey goto done;
974 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
975 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
976 bd275801 2020-02-03 tracey goto done;
979 55ccf528 2020-02-03 stsp age = NULL;
980 55ccf528 2020-02-03 stsp free(age_html);
981 55ccf528 2020-02-03 stsp age_html = NULL;
982 bd275801 2020-02-03 tracey free(href_diff);
983 bd275801 2020-02-03 tracey href_diff = NULL;
984 bd275801 2020-02-03 tracey free(href_tree);
985 bd275801 2020-02-03 tracey href_tree = NULL;
988 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
989 f2f46662 2020-01-23 tracey gw_free_headers(header);
990 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
991 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
993 55ccf528 2020-02-03 stsp free(age_html);
994 bd275801 2020-02-03 tracey free(href_diff);
995 bd275801 2020-02-03 tracey free(href_tree);
996 ef72fe2c 2020-02-04 stsp if (error == NULL && kerr != KCGI_OK)
997 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
998 2c251c14 2020-01-15 tracey return error;
1001 2c251c14 2020-01-15 tracey static const struct got_error *
1002 54415d85 2020-01-15 tracey gw_summary(struct gw_trans *gw_trans)
1004 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
1005 783ec107 2020-02-03 tracey char *age = NULL, *tags = NULL, *heads = NULL;
1006 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1008 f4df82f9 2020-01-29 stsp if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1009 f4df82f9 2020-01-29 stsp return got_error_from_errno("pledge");
1011 f2f46662 2020-01-23 tracey /* unveil is applied with gw_briefs below */
1013 783ec107 2020-02-03 tracey /* summary wrapper */
1014 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1015 783ec107 2020-02-03 tracey "summary_wrapper", KATTR__MAX);
1016 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1017 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1019 783ec107 2020-02-03 tracey /* description */
1020 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_description &&
1021 783ec107 2020-02-03 tracey gw_trans->gw_dir->description != NULL &&
1022 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
1023 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1024 783ec107 2020-02-03 tracey KATTR_ID, "description_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Description: ");
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_closeelem(gw_trans->gw_html_req, 1);
1031 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1032 783ec107 2020-02-03 tracey goto done;
1033 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1034 783ec107 2020-02-03 tracey KATTR_ID, "description", KATTR__MAX);
1035 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1036 783ec107 2020-02-03 tracey goto done;
1037 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1038 783ec107 2020-02-03 tracey gw_trans->gw_dir->description);
1039 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1040 783ec107 2020-02-03 tracey goto done;
1041 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1042 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1043 783ec107 2020-02-03 tracey goto done;
1046 783ec107 2020-02-03 tracey /* repo owner */
1047 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner &&
1048 9a1cc63f 2020-02-03 stsp gw_trans->gw_dir->owner != NULL) {
1049 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1050 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Owner: ");
1054 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1055 9a1cc63f 2020-02-03 stsp goto done;
1056 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1057 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1058 783ec107 2020-02-03 tracey goto done;
1059 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1060 783ec107 2020-02-03 tracey KATTR_ID, "repo_owner", KATTR__MAX);
1061 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1062 783ec107 2020-02-03 tracey goto done;
1063 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1064 783ec107 2020-02-03 tracey gw_trans->gw_dir->owner);
1065 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1066 783ec107 2020-02-03 tracey goto done;
1067 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1068 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1069 783ec107 2020-02-03 tracey goto done;
1072 783ec107 2020-02-03 tracey /* last change */
1073 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
1074 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
1075 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
1076 d126c1b7 2020-01-29 stsp if (error)
1077 20f34652 2020-01-29 stsp goto done;
1078 55ccf528 2020-02-03 stsp if (age != NULL) {
1079 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1080 783ec107 2020-02-03 tracey KATTR_ID, "last_change_title", KATTR__MAX);
1081 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1082 20f34652 2020-01-29 stsp goto done;
1083 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1084 783ec107 2020-02-03 tracey "Last Change: ");
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_closeelem(gw_trans->gw_html_req, 1);
1088 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1089 20f34652 2020-01-29 stsp goto done;
1090 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1091 783ec107 2020-02-03 tracey KATTR_ID, "last_change", KATTR__MAX);
1092 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1093 20f34652 2020-01-29 stsp goto done;
1094 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, age);
1095 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1096 783ec107 2020-02-03 tracey goto done;
1097 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1098 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1099 783ec107 2020-02-03 tracey goto done;
1103 783ec107 2020-02-03 tracey /* cloneurl */
1104 783ec107 2020-02-03 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl &&
1105 783ec107 2020-02-03 tracey gw_trans->gw_dir->url != NULL &&
1106 783ec107 2020-02-03 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
1107 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1108 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Clone URL: ");
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_closeelem(gw_trans->gw_html_req, 1);
1115 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1116 783ec107 2020-02-03 tracey goto done;
1117 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1118 783ec107 2020-02-03 tracey KATTR_ID, "cloneurl", KATTR__MAX);
1119 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1120 783ec107 2020-02-03 tracey goto done;
1121 783ec107 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->gw_dir->url);
1122 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1123 21a55cc7 2020-02-04 tracey goto done;
1124 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1125 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1126 783ec107 2020-02-03 tracey goto done;
1129 783ec107 2020-02-03 tracey /* close summary wrapper */
1130 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1131 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1132 bd275801 2020-02-03 tracey goto done;
1134 bd275801 2020-02-03 tracey /* commit briefs header */
1135 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1136 bd275801 2020-02-03 tracey "briefs_title_wrapper", KATTR__MAX);
1137 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1138 bd275801 2020-02-03 tracey goto done;
1139 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
1140 bd275801 2020-02-03 tracey "briefs_title", KATTR__MAX);
1141 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1142 20f34652 2020-01-29 stsp goto done;
1143 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Commit Briefs");
1144 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1145 bd275801 2020-02-03 tracey goto done;
1146 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1147 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1148 bd275801 2020-02-03 tracey goto done;
1149 f2f46662 2020-01-23 tracey error = gw_briefs(gw_trans);
1150 f2f46662 2020-01-23 tracey if (error)
1151 20f34652 2020-01-29 stsp goto done;
1153 783ec107 2020-02-03 tracey /* tags */
1154 6eccd105 2020-02-03 stsp error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
1155 6eccd105 2020-02-03 stsp TAGBRIEF);
1156 6eccd105 2020-02-03 stsp if (error)
1157 6eccd105 2020-02-03 stsp goto done;
1159 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
1160 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1161 783ec107 2020-02-03 tracey KATTR_ID, "summary_tags_title_wrapper", KATTR__MAX);
1162 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1163 20f34652 2020-01-29 stsp goto done;
1164 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1165 783ec107 2020-02-03 tracey KATTR_ID, "summary_tags_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Tags");
1169 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1170 783ec107 2020-02-03 tracey goto done;
1171 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1172 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1173 20f34652 2020-01-29 stsp goto done;
1174 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1175 783ec107 2020-02-03 tracey KATTR_ID, "summary_tags_content", KATTR__MAX);
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 = khttp_puts(gw_trans->gw_req, tags);
1179 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1180 783ec107 2020-02-03 tracey goto done;
1181 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1182 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1183 783ec107 2020-02-03 tracey goto done;
1186 783ec107 2020-02-03 tracey /* heads */
1187 51d4a92d 2020-02-03 tracey error = gw_get_repo_heads(&heads, gw_trans);
1188 51d4a92d 2020-02-03 tracey if (error)
1189 51d4a92d 2020-02-03 tracey goto done;
1190 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
1191 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1192 783ec107 2020-02-03 tracey KATTR_ID, "summary_heads_title_wrapper", KATTR__MAX);
1193 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1194 20f34652 2020-01-29 stsp goto done;
1195 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1196 783ec107 2020-02-03 tracey KATTR_ID, "summary_heads_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Heads");
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_closeelem(gw_trans->gw_html_req, 2);
1203 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1204 783ec107 2020-02-03 tracey goto done;
1205 783ec107 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1206 783ec107 2020-02-03 tracey KATTR_ID, "summary_heads_content", KATTR__MAX);
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 = khttp_puts(gw_trans->gw_req, heads);
1210 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1211 783ec107 2020-02-03 tracey goto done;
1212 783ec107 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1213 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1214 20f34652 2020-01-29 stsp goto done;
1217 20f34652 2020-01-29 stsp free(age);
1218 20f34652 2020-01-29 stsp free(tags);
1219 20f34652 2020-01-29 stsp free(heads);
1220 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1221 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1222 2204c934 2020-01-15 tracey return error;
1225 2204c934 2020-01-15 tracey static const struct got_error *
1226 f2f46662 2020-01-23 tracey gw_tree(struct gw_trans *gw_trans)
1228 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
1229 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
1230 f2f46662 2020-01-23 tracey char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
1231 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
1232 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1234 f2f46662 2020-01-23 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1235 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
1237 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
1238 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
1240 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1241 b772de24 2020-01-15 tracey if (error)
1242 5ddf0079 2020-01-29 stsp goto done;
1244 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
1245 f2f46662 2020-01-23 tracey if (error)
1246 42281175 2020-01-29 stsp goto done;
1248 84bf4df2 2020-02-03 stsp error = gw_get_repo_tree(&tree_html, gw_trans);
1249 84bf4df2 2020-02-03 stsp if (error)
1250 84bf4df2 2020-02-03 stsp goto done;
1252 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, header->committer_time, TM_LONG);
1253 55ccf528 2020-02-03 stsp if (error)
1254 55ccf528 2020-02-03 stsp goto done;
1255 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
1256 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
1257 55ccf528 2020-02-03 stsp goto done;
1259 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1260 070fee27 2020-02-03 stsp if (error)
1261 070fee27 2020-02-03 stsp goto done;
1262 55ccf528 2020-02-03 stsp if (asprintf(&tree_html_disp, tree_header, age_html,
1263 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(escaped_commit_msg),
1264 84bf4df2 2020-02-03 stsp tree_html ? tree_html : "") == -1) {
1265 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
1266 42281175 2020-01-29 stsp goto done;
1269 42281175 2020-01-29 stsp if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1270 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
1271 42281175 2020-01-29 stsp goto done;
1274 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tree);
1275 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1276 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1278 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
1279 f2f46662 2020-01-23 tracey gw_free_headers(header);
1280 f2f46662 2020-01-23 tracey free(tree_html_disp);
1281 f2f46662 2020-01-23 tracey free(tree_html);
1282 f2f46662 2020-01-23 tracey free(tree);
1283 55ccf528 2020-02-03 stsp free(age);
1284 55ccf528 2020-02-03 stsp free(age_html);
1285 070fee27 2020-02-03 stsp free(escaped_commit_msg);
1286 4ff7391f 2020-01-28 tracey return error;
1289 4ff7391f 2020-01-28 tracey static const struct got_error *
1290 4ff7391f 2020-01-28 tracey gw_tag(struct gw_trans *gw_trans)
1292 4ff7391f 2020-01-28 tracey const struct got_error *error = NULL;
1293 4ff7391f 2020-01-28 tracey struct gw_header *header = NULL;
1294 4ff7391f 2020-01-28 tracey char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1295 070fee27 2020-02-03 stsp char *escaped_commit_msg = NULL;
1296 4ff7391f 2020-01-28 tracey enum kcgi_err kerr;
1298 4ff7391f 2020-01-28 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1299 4ff7391f 2020-01-28 tracey return got_error_from_errno("pledge");
1301 4ff7391f 2020-01-28 tracey if ((header = gw_init_header()) == NULL)
1302 4ff7391f 2020-01-28 tracey return got_error_from_errno("malloc");
1304 4ff7391f 2020-01-28 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1305 4ff7391f 2020-01-28 tracey if (error)
1306 5ddf0079 2020-01-29 stsp goto done;
1308 4ff7391f 2020-01-28 tracey error = gw_get_header(gw_trans, header, 1);
1309 4ff7391f 2020-01-28 tracey if (error)
1310 470cd826 2020-01-29 stsp goto done;
1312 6eccd105 2020-02-03 stsp error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1313 6eccd105 2020-02-03 stsp if (error)
1314 6eccd105 2020-02-03 stsp goto done;
1316 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1317 070fee27 2020-02-03 stsp if (error)
1318 070fee27 2020-02-03 stsp goto done;
1319 88d59c36 2020-01-29 stsp if (asprintf(&tag_html_disp, tag_header,
1320 21a55cc7 2020-02-04 tracey gw_gen_commit_header_old(header->commit_id, header->refs_str),
1321 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(escaped_commit_msg),
1322 6eccd105 2020-02-03 stsp tag_html ? tag_html : "") == -1) {
1323 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
1324 470cd826 2020-01-29 stsp goto done;
1327 470cd826 2020-01-29 stsp if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1328 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
1329 470cd826 2020-01-29 stsp goto done;
1332 4ff7391f 2020-01-28 tracey kerr = khttp_puts(gw_trans->gw_req, tag);
1333 4ff7391f 2020-01-28 tracey if (kerr != KCGI_OK)
1334 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1336 4ff7391f 2020-01-28 tracey got_ref_list_free(&header->refs);
1337 4ff7391f 2020-01-28 tracey gw_free_headers(header);
1338 4ff7391f 2020-01-28 tracey free(tag_html_disp);
1339 4ff7391f 2020-01-28 tracey free(tag_html);
1340 4ff7391f 2020-01-28 tracey free(tag);
1341 070fee27 2020-02-03 stsp free(escaped_commit_msg);
1342 2c251c14 2020-01-15 tracey return error;
1345 2c251c14 2020-01-15 tracey static const struct got_error *
1346 54415d85 2020-01-15 tracey gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1348 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1349 2c251c14 2020-01-15 tracey DIR *dt;
1350 2c251c14 2020-01-15 tracey char *dir_test;
1351 4ceb8155 2020-01-15 tracey int opened = 0;
1353 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1354 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1355 88d59c36 2020-01-29 stsp GOTWEB_GIT_DIR) == -1)
1356 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1358 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1359 2c251c14 2020-01-15 tracey if (dt == NULL) {
1360 2c251c14 2020-01-15 tracey free(dir_test);
1361 2c251c14 2020-01-15 tracey } else {
1362 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1363 4ceb8155 2020-01-15 tracey opened = 1;
1364 2c251c14 2020-01-15 tracey goto done;
1367 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1368 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1369 88d59c36 2020-01-29 stsp GOTWEB_GOT_DIR) == -1)
1370 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1372 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1373 2c251c14 2020-01-15 tracey if (dt == NULL)
1374 2c251c14 2020-01-15 tracey free(dir_test);
1376 4ceb8155 2020-01-15 tracey opened = 1;
1377 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
1378 2c251c14 2020-01-15 tracey goto errored;
1381 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s",
1382 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1383 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1385 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1388 32cd4d18 2020-02-03 stsp error = gw_get_repo_description(&gw_dir->description, gw_trans,
1389 2c251c14 2020-01-15 tracey gw_dir->path);
1390 32cd4d18 2020-02-03 stsp if (error)
1391 32cd4d18 2020-02-03 stsp goto errored;
1392 9a1cc63f 2020-02-03 stsp error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1393 9a1cc63f 2020-02-03 stsp if (error)
1394 9a1cc63f 2020-02-03 stsp goto errored;
1395 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1396 d126c1b7 2020-01-29 stsp "refs/heads", TM_DIFF);
1397 d126c1b7 2020-01-29 stsp if (error)
1398 d126c1b7 2020-01-29 stsp goto errored;
1399 59d3c40e 2020-02-03 stsp error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1400 2c251c14 2020-01-15 tracey errored:
1401 2c251c14 2020-01-15 tracey free(dir_test);
1402 2c251c14 2020-01-15 tracey if (opened)
1403 2c251c14 2020-01-15 tracey closedir(dt);
1404 2c251c14 2020-01-15 tracey return error;
1407 2c251c14 2020-01-15 tracey static const struct got_error *
1408 54415d85 2020-01-15 tracey gw_load_got_paths(struct gw_trans *gw_trans)
1410 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1412 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
1413 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1414 2c251c14 2020-01-15 tracey struct stat st;
1415 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
1417 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
1418 2c251c14 2020-01-15 tracey if (d == NULL) {
1419 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
1420 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1421 2c251c14 2020-01-15 tracey return error;
1424 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1425 2c251c14 2020-01-15 tracey alphasort);
1426 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
1427 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
1428 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1429 2c251c14 2020-01-15 tracey return error;
1432 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
1433 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
1434 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1435 2c251c14 2020-01-15 tracey break; /* account for parent and self */
1437 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1438 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
1439 2c251c14 2020-01-15 tracey continue;
1441 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1442 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1444 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
1445 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1446 2c251c14 2020-01-15 tracey continue;
1447 2c251c14 2020-01-15 tracey else if (error)
1448 2c251c14 2020-01-15 tracey return error;
1450 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1451 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
1452 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1454 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
1458 2c251c14 2020-01-15 tracey closedir(d);
1459 2c251c14 2020-01-15 tracey return error;
1462 2c251c14 2020-01-15 tracey static const struct got_error *
1463 54415d85 2020-01-15 tracey gw_parse_querystring(struct gw_trans *gw_trans)
1465 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1466 2c251c14 2020-01-15 tracey struct kpair *p;
1467 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
1468 2c251c14 2020-01-15 tracey unsigned int i;
1470 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
1471 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
1472 2c251c14 2020-01-15 tracey return error;
1473 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1474 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
1475 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1476 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1478 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_path, "%s/%s",
1479 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1480 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1482 2c251c14 2020-01-15 tracey /* get action and set function */
1483 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1484 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
1485 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
1486 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
1487 2c251c14 2020-01-15 tracey continue;
1489 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
1490 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
1491 2c251c14 2020-01-15 tracey gw_trans->action = i;
1492 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->action_name,
1493 88d59c36 2020-01-29 stsp "%s", action->func_name) == -1)
1495 b772de24 2020-01-15 tracey got_error_from_errno(
1496 2c251c14 2020-01-15 tracey "asprintf");
1501 2c251c14 2020-01-15 tracey action = NULL;
1504 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1505 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->commit, "%s",
1506 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1507 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1509 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1510 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_file, "%s",
1511 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1512 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
1514 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1515 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_folder, "%s",
1516 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1517 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1519 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1520 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->headref, "%s",
1521 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1522 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1524 2c251c14 2020-01-15 tracey if (action == NULL) {
1525 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
1526 2c251c14 2020-01-15 tracey return error;
1528 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
1529 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1530 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1532 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1533 46b9c89b 2020-01-15 tracey if (error)
1534 46b9c89b 2020-01-15 tracey return error;
1536 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1538 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1539 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1541 2c251c14 2020-01-15 tracey return error;
1544 2c251c14 2020-01-15 tracey static struct gw_dir *
1545 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
1547 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1549 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1550 2c251c14 2020-01-15 tracey return NULL;
1552 88d59c36 2020-01-29 stsp if (asprintf(&gw_dir->name, "%s", dir) == -1)
1553 2c251c14 2020-01-15 tracey return NULL;
1555 2c251c14 2020-01-15 tracey return gw_dir;
1558 c25c2314 2020-01-28 stsp static const struct got_error *
1559 54415d85 2020-01-15 tracey gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1561 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1563 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1564 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1565 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1566 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1567 2c251c14 2020-01-15 tracey khttps[code]);
1568 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1569 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1570 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1571 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1572 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1573 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1574 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1575 017d6da3 2020-01-29 tracey "nosniff");
1576 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1577 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1578 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1579 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1580 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1581 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1582 017d6da3 2020-01-29 tracey "1; mode=block");
1583 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1584 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1586 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1587 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req,
1588 017d6da3 2020-01-29 tracey kresps[KRESP_CONTENT_DISPOSITION],
1589 017d6da3 2020-01-29 tracey "attachment; filename=%s", gw_trans->repo_file);
1590 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
1591 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1594 c25c2314 2020-01-28 stsp kerr = khttp_body(gw_trans->gw_req);
1595 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1598 c25c2314 2020-01-28 stsp static const struct got_error *
1599 6d8d8a26 2020-01-29 stsp gw_display_index(struct gw_trans *gw_trans)
1601 4bec7539 2020-01-29 stsp const struct got_error *error;
1602 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1604 4bec7539 2020-01-29 stsp error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1605 4bec7539 2020-01-29 stsp if (error)
1606 4bec7539 2020-01-29 stsp return error;
1608 f7cc9480 2020-02-05 tracey kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1609 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1610 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1612 017d6da3 2020-01-29 tracey if (gw_trans->action != GW_BLOB) {
1613 017d6da3 2020-01-29 tracey kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1614 017d6da3 2020-01-29 tracey gw_query_funcs[gw_trans->action].template);
1615 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK) {
1616 017d6da3 2020-01-29 tracey khtml_close(gw_trans->gw_html_req);
1617 2447adad 2020-02-06 stsp return gw_kcgi_error(kerr);
1621 7a9bfbff 2020-01-29 stsp return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1624 6d8d8a26 2020-01-29 stsp static void
1625 6d8d8a26 2020-01-29 stsp gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1627 6d8d8a26 2020-01-29 stsp if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1630 6d8d8a26 2020-01-29 stsp if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1632 33dc7bd2 2020-02-03 stsp khtml_puts(gw_trans->gw_html_req, err->msg);
1633 6d8d8a26 2020-01-29 stsp khtml_close(gw_trans->gw_html_req);
1636 2c251c14 2020-01-15 tracey static int
1637 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1639 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1640 bd275801 2020-02-03 tracey enum kcgi_err kerr;
1641 54415d85 2020-01-15 tracey struct gw_trans *gw_trans = arg;
1642 185ba3ba 2020-02-04 tracey char *gw_site_link, *img_src = NULL;
1644 2c251c14 2020-01-15 tracey switch (key) {
1645 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1646 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, head);
1647 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1648 bd275801 2020-02-03 tracey return 0;
1650 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1651 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1652 185ba3ba 2020-02-04 tracey KATTR_ID, "got_link", KATTR__MAX);
1653 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
1654 185ba3ba 2020-02-04 tracey return 0;
1655 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_A,
1656 185ba3ba 2020-02-04 tracey KATTR_HREF, gw_trans->gw_conf->got_logo_url,
1657 185ba3ba 2020-02-04 tracey KATTR_TARGET, "_sotd", KATTR__MAX);
1658 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK)
1659 185ba3ba 2020-02-04 tracey return 0;
1660 185ba3ba 2020-02-04 tracey if (asprintf(&img_src, "/%s",
1661 185ba3ba 2020-02-04 tracey gw_trans->gw_conf->got_logo) == -1)
1662 185ba3ba 2020-02-04 tracey return 0;
1663 185ba3ba 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
1664 185ba3ba 2020-02-04 tracey KATTR_SRC, img_src, KATTR__MAX);
1665 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
1666 185ba3ba 2020-02-04 tracey free(img_src);
1667 185ba3ba 2020-02-04 tracey return 0;
1669 185ba3ba 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
1670 185ba3ba 2020-02-04 tracey if (kerr != KCGI_OK) {
1671 185ba3ba 2020-02-04 tracey free(img_src);
1672 185ba3ba 2020-02-04 tracey return 0;
1675 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1676 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
1677 bd275801 2020-02-03 tracey if (gw_site_link != NULL) {
1678 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, gw_site_link);
1679 bd275801 2020-02-03 tracey if (kerr != KCGI_OK) {
1680 bd275801 2020-02-03 tracey free(gw_site_link);
1681 bd275801 2020-02-03 tracey return 0;
1684 2c251c14 2020-01-15 tracey free(gw_site_link);
1686 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1687 bd275801 2020-02-03 tracey if (gw_trans->gw_conf->got_site_name != NULL) {
1688 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1689 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1690 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1691 bd275801 2020-02-03 tracey return 0;
1694 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1695 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, search);
1696 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1697 bd275801 2020-02-03 tracey return 0;
1699 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
1700 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
1701 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
1702 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1703 bd275801 2020-02-03 tracey KATTR_ID, "site_owner_wrapper", KATTR__MAX);
1704 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1706 bd275801 2020-02-03 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1707 bd275801 2020-02-03 tracey KATTR_ID, "site_owner", KATTR__MAX);
1708 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1709 2c251c14 2020-01-15 tracey return 0;
1710 bd275801 2020-02-03 tracey kerr = khtml_puts(gw_trans->gw_html_req,
1711 bd275801 2020-02-03 tracey gw_trans->gw_conf->got_site_owner);
1712 bd275801 2020-02-03 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 2);
1713 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1714 bd275801 2020-02-03 tracey return 0;
1717 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1718 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1719 bd275801 2020-02-03 tracey if (error) {
1720 bd275801 2020-02-03 tracey kerr = khttp_puts(gw_trans->gw_req, error->msg);
1721 bd275801 2020-02-03 tracey if (kerr != KCGI_OK)
1722 bd275801 2020-02-03 tracey return 0;
1725 2c251c14 2020-01-15 tracey default:
1726 2c251c14 2020-01-15 tracey return 0;
1728 2c251c14 2020-01-15 tracey return 1;
1731 2c251c14 2020-01-15 tracey static char *
1732 21a55cc7 2020-02-04 tracey gw_gen_commit_header_old(char *str1, char *str2)
1734 f2f46662 2020-01-23 tracey char *return_html = NULL, *ref_str = NULL;
1736 f2f46662 2020-01-23 tracey if (strcmp(str2, "") != 0) {
1737 88d59c36 2020-01-29 stsp if (asprintf(&ref_str, "(%s)", str2) == -1) {
1738 f2f46662 2020-01-23 tracey return_html = strdup("");
1739 f2f46662 2020-01-23 tracey return return_html;
1742 f2f46662 2020-01-23 tracey ref_str = strdup("");
1744 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1745 f2f46662 2020-01-23 tracey return_html = strdup("");
1747 f2f46662 2020-01-23 tracey free(ref_str);
1748 f2f46662 2020-01-23 tracey return return_html;
1751 21a55cc7 2020-02-04 tracey static const struct got_error *
1752 21a55cc7 2020-02-04 tracey gw_gen_commit_header(struct gw_trans *gw_trans, char *str1, char *str2)
1754 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1755 21a55cc7 2020-02-04 tracey char *ref_str = NULL;
1756 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1758 21a55cc7 2020-02-04 tracey if (strcmp(str2, "") != 0) {
1759 21a55cc7 2020-02-04 tracey if (asprintf(&ref_str, "(%s)", str2) == -1) {
1760 21a55cc7 2020-02-04 tracey error = got_error_from_errno("asprintf");
1761 21a55cc7 2020-02-04 tracey goto done;
1764 21a55cc7 2020-02-04 tracey ref_str = strdup("");
1766 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1767 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Commit: ");
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_closeelem(gw_trans->gw_html_req, 1);
1774 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1775 21a55cc7 2020-02-04 tracey goto done;
1776 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1777 21a55cc7 2020-02-04 tracey KATTR_ID, "header_commit", KATTR__MAX);
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, str1);
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, " ");
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_puts(gw_trans->gw_html_req, ref_str);
1787 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1788 21a55cc7 2020-02-04 tracey goto done;
1789 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1790 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1791 21a55cc7 2020-02-04 tracey goto done;
1793 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1794 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1795 21a55cc7 2020-02-04 tracey return error;
1798 f7cc9480 2020-02-05 tracey static const struct got_error *
1799 f7cc9480 2020-02-05 tracey gw_gen_diff_header(struct gw_trans *gw_trans, char *str1, char *str2)
1801 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
1802 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
1804 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1805 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Diff: ");
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_closeelem(gw_trans->gw_html_req, 1);
1812 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1813 f7cc9480 2020-02-05 tracey goto done;
1814 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1815 f7cc9480 2020-02-05 tracey KATTR_ID, "header_diff", KATTR__MAX);
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_puts(gw_trans->gw_html_req, str1);
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_attr(gw_trans->gw_html_req, KELEM_BR, KATTR__MAX);
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_puts(gw_trans->gw_html_req, str2);
1825 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1826 f7cc9480 2020-02-05 tracey goto done;
1827 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1828 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1829 f7cc9480 2020-02-05 tracey goto done;
1831 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1832 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1833 f7cc9480 2020-02-05 tracey return error;
1836 21a55cc7 2020-02-04 tracey static const struct got_error *
1837 21a55cc7 2020-02-04 tracey gw_gen_age_header(struct gw_trans *gw_trans, const char *str)
1839 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1840 21a55cc7 2020-02-04 tracey enum kcgi_err kerr = KCGI_OK;
1842 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1843 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Date: ");
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_closeelem(gw_trans->gw_html_req, 1);
1850 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1851 21a55cc7 2020-02-04 tracey goto done;
1852 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1853 21a55cc7 2020-02-04 tracey KATTR_ID, "header_age", KATTR__MAX);
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_puts(gw_trans->gw_html_req, str);
1857 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1858 21a55cc7 2020-02-04 tracey goto done;
1859 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1860 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1861 21a55cc7 2020-02-04 tracey goto done;
1863 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1864 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1865 21a55cc7 2020-02-04 tracey return error;
1868 21a55cc7 2020-02-04 tracey static const struct got_error *
1869 21a55cc7 2020-02-04 tracey gw_gen_author_header(struct gw_trans *gw_trans, const char *str)
1871 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1872 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
1874 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1875 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Author: ");
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_closeelem(gw_trans->gw_html_req, 1);
1882 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1883 21a55cc7 2020-02-04 tracey goto done;
1884 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1885 21a55cc7 2020-02-04 tracey KATTR_ID, "header_author", KATTR__MAX);
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_puts(gw_trans->gw_html_req, str);
1889 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1890 21a55cc7 2020-02-04 tracey goto done;
1891 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1892 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1893 21a55cc7 2020-02-04 tracey goto done;
1895 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1896 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1897 21a55cc7 2020-02-04 tracey return error;
1900 21a55cc7 2020-02-04 tracey static const struct got_error *
1901 21a55cc7 2020-02-04 tracey gw_gen_committer_header(struct gw_trans *gw_trans, const char *str)
1903 21a55cc7 2020-02-04 tracey const struct got_error *error = NULL;
1904 21a55cc7 2020-02-04 tracey enum kcgi_err kerr;
1906 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1907 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer_title", KATTR__MAX);
1908 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1909 21a55cc7 2020-02-04 tracey goto done;
1910 87ca24ac 2020-02-04 tracey kerr = khtml_puts(gw_trans->gw_html_req, "Committer: ");
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_closeelem(gw_trans->gw_html_req, 1);
1914 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1915 21a55cc7 2020-02-04 tracey goto done;
1916 21a55cc7 2020-02-04 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1917 21a55cc7 2020-02-04 tracey KATTR_ID, "header_committer", KATTR__MAX);
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_puts(gw_trans->gw_html_req, str);
1921 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1922 21a55cc7 2020-02-04 tracey goto done;
1923 21a55cc7 2020-02-04 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1924 21a55cc7 2020-02-04 tracey if (kerr != KCGI_OK)
1925 21a55cc7 2020-02-04 tracey goto done;
1927 21a55cc7 2020-02-04 tracey if (error == NULL && kerr != KCGI_OK)
1928 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1929 21a55cc7 2020-02-04 tracey return error;
1932 f7cc9480 2020-02-05 tracey static const struct got_error *
1933 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header(struct gw_trans *gw_trans, char *str)
1935 27192be7 2020-02-04 tracey const struct got_error *error = NULL;
1936 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
1938 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1939 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Message: ");
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_closeelem(gw_trans->gw_html_req, 1);
1946 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1947 f7cc9480 2020-02-05 tracey goto done;
1948 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1949 f7cc9480 2020-02-05 tracey KATTR_ID, "header_commit_msg", KATTR__MAX);
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 = khttp_puts(gw_trans->gw_req, str);
1953 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1954 f7cc9480 2020-02-05 tracey goto done;
1955 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
1956 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1957 f7cc9480 2020-02-05 tracey goto done;
1959 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
1960 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
1961 f7cc9480 2020-02-05 tracey return error;
1964 f7cc9480 2020-02-05 tracey /* XXX: slated for deletion */
1965 f2f46662 2020-01-23 tracey static char *
1966 f7cc9480 2020-02-05 tracey gw_gen_commit_msg_header_old(char *str)
1968 f2f46662 2020-01-23 tracey char *return_html = NULL;
1970 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1971 f2f46662 2020-01-23 tracey return_html = strdup("");
1973 f2f46662 2020-01-23 tracey return return_html;
1976 f7cc9480 2020-02-05 tracey static const struct got_error *
1977 f7cc9480 2020-02-05 tracey gw_gen_tree_header(struct gw_trans *gw_trans, char *str)
1979 f7cc9480 2020-02-05 tracey const struct got_error *error = NULL;
1980 f7cc9480 2020-02-05 tracey enum kcgi_err kerr;
1982 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1983 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree_title", KATTR__MAX);
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_puts(gw_trans->gw_html_req, "Tree: ");
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_closeelem(gw_trans->gw_html_req, 1);
1990 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1991 f7cc9480 2020-02-05 tracey goto done;
1992 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV,
1993 f7cc9480 2020-02-05 tracey KATTR_ID, "header_tree", KATTR__MAX);
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_puts(gw_trans->gw_html_req, str);
1997 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
1998 f7cc9480 2020-02-05 tracey goto done;
1999 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2000 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2001 f7cc9480 2020-02-05 tracey goto done;
2003 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2004 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2005 f7cc9480 2020-02-05 tracey return error;
2008 32cd4d18 2020-02-03 stsp static const struct got_error *
2009 32cd4d18 2020-02-03 stsp gw_get_repo_description(char **description, struct gw_trans *gw_trans,
2010 32cd4d18 2020-02-03 stsp char *dir)
2012 32cd4d18 2020-02-03 stsp const struct got_error *error = NULL;
2013 b8b04565 2020-02-02 tracey FILE *f = NULL;
2014 32cd4d18 2020-02-03 stsp char *d_file = NULL;
2015 2c251c14 2020-01-15 tracey unsigned int len;
2018 32cd4d18 2020-02-03 stsp *description = NULL;
2019 0b20762b 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_description == 0)
2020 32cd4d18 2020-02-03 stsp return gw_empty_string(description);
2022 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/description", dir) == -1)
2023 32cd4d18 2020-02-03 stsp return got_error_from_errno("asprintf");
2025 32cd4d18 2020-02-03 stsp f = fopen(d_file, "r");
2026 32cd4d18 2020-02-03 stsp if (f == NULL) {
2027 32cd4d18 2020-02-03 stsp if (errno == ENOENT || errno == EACCES)
2028 32cd4d18 2020-02-03 stsp return gw_empty_string(description);
2029 32cd4d18 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2030 32cd4d18 2020-02-03 stsp goto done;
2033 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2034 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2035 32cd4d18 2020-02-03 stsp goto done;
2037 32cd4d18 2020-02-03 stsp len = ftell(f);
2038 32cd4d18 2020-02-03 stsp if (len == -1) {
2039 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2040 32cd4d18 2020-02-03 stsp goto done;
2042 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2043 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2044 32cd4d18 2020-02-03 stsp goto done;
2046 32cd4d18 2020-02-03 stsp *description = calloc(len + 1, sizeof(**description));
2047 32cd4d18 2020-02-03 stsp if (*description == NULL) {
2048 32cd4d18 2020-02-03 stsp error = got_error_from_errno("calloc");
2049 32cd4d18 2020-02-03 stsp goto done;
2052 32cd4d18 2020-02-03 stsp n = fread(*description, 1, len, f);
2053 1ab830c1 2020-02-03 stsp if (n == 0 && ferror(f))
2054 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2056 32cd4d18 2020-02-03 stsp if (f != NULL && fclose(f) == -1 && error == NULL)
2057 32cd4d18 2020-02-03 stsp error = got_error_from_errno("fclose");
2058 2c251c14 2020-01-15 tracey free(d_file);
2059 32cd4d18 2020-02-03 stsp return error;
2062 55ccf528 2020-02-03 stsp static const struct got_error *
2063 55ccf528 2020-02-03 stsp gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2065 474370cb 2020-01-15 tracey struct tm tm;
2066 474370cb 2020-01-15 tracey time_t diff_time;
2067 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
2068 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
2069 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
2070 474370cb 2020-01-15 tracey char *now = "right now";
2072 6c6c85af 2020-01-15 tracey char datebuf[29];
2074 55ccf528 2020-02-03 stsp *repo_age = NULL;
2076 474370cb 2020-01-15 tracey switch (ref_tm) {
2077 474370cb 2020-01-15 tracey case TM_DIFF:
2078 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
2079 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
2080 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2081 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 365), years) == -1)
2082 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2083 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2084 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2085 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
2086 88d59c36 2020-01-29 stsp months) == -1)
2087 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2088 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2089 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2090 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2091 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2092 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
2093 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2094 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24), days) == -1)
2095 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2096 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
2097 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
2098 88d59c36 2020-01-29 stsp (diff_time / 60 / 60), hours) == -1)
2099 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2100 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
2101 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2102 88d59c36 2020-01-29 stsp minutes) == -1)
2103 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2104 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
2105 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", diff_time,
2106 88d59c36 2020-01-29 stsp seconds) == -1)
2107 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2108 474370cb 2020-01-15 tracey } else {
2109 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s", now) == -1)
2110 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2113 474370cb 2020-01-15 tracey case TM_LONG:
2114 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
2115 55ccf528 2020-02-03 stsp return got_error_from_errno("gmtime_r");
2117 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
2118 474370cb 2020-01-15 tracey if (s == NULL)
2119 55ccf528 2020-02-03 stsp return got_error_from_errno("asctime_r");
2121 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2122 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
2125 55ccf528 2020-02-03 stsp return NULL;
2128 d126c1b7 2020-01-29 stsp static const struct got_error *
2129 d126c1b7 2020-01-29 stsp gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
2130 d126c1b7 2020-01-29 stsp char *repo_ref, int ref_tm)
2132 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2133 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
2134 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
2135 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
2136 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
2137 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
2138 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
2139 87f9ebf5 2020-01-15 tracey int is_head = 0;
2140 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
2141 87f9ebf5 2020-01-15 tracey const char *refname;
2143 d126c1b7 2020-01-29 stsp *repo_age = NULL;
2144 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2146 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
2147 387a29ba 2020-01-15 tracey return NULL;
2149 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
2150 87f9ebf5 2020-01-15 tracey is_head = 1;
2152 55ccf528 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_age == 0)
2153 d126c1b7 2020-01-29 stsp return NULL;
2155 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
2156 ec46ccd7 2020-01-15 tracey if (error)
2157 d126c1b7 2020-01-29 stsp goto done;
2159 87f9ebf5 2020-01-15 tracey if (is_head)
2160 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
2161 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
2163 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
2164 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
2165 ec46ccd7 2020-01-15 tracey if (error)
2166 d126c1b7 2020-01-29 stsp goto done;
2168 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2169 87f9ebf5 2020-01-15 tracey if (is_head)
2170 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
2172 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
2173 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
2174 ec46ccd7 2020-01-15 tracey if (error)
2175 d126c1b7 2020-01-29 stsp goto done;
2177 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
2178 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
2179 ec46ccd7 2020-01-15 tracey if (error)
2180 d126c1b7 2020-01-29 stsp goto done;
2182 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
2183 ec46ccd7 2020-01-15 tracey if (error)
2184 d126c1b7 2020-01-29 stsp goto done;
2186 2c251c14 2020-01-15 tracey committer_time =
2187 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
2189 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
2190 2c251c14 2020-01-15 tracey cmp_time = committer_time;
2193 474370cb 2020-01-15 tracey if (cmp_time != 0) {
2194 2c251c14 2020-01-15 tracey committer_time = cmp_time;
2195 55ccf528 2020-02-03 stsp error = gw_get_time_str(repo_age, committer_time, ref_tm);
2198 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
2199 2c251c14 2020-01-15 tracey free(id);
2200 d126c1b7 2020-01-29 stsp return error;
2203 83eb9a68 2020-02-03 stsp static const struct got_error *
2204 f7cc9480 2020-02-05 tracey gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
2206 ec46ccd7 2020-01-15 tracey const struct got_error *error;
2207 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
2208 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
2209 f7cc9480 2020-02-05 tracey char *label1 = NULL, *label2 = NULL, *line = NULL;
2210 05ce9a79 2020-01-24 tracey int obj_type;
2211 f7cc9480 2020-02-05 tracey size_t linesize = 0;
2212 83eb9a68 2020-02-03 stsp ssize_t linelen;
2213 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
2215 ec46ccd7 2020-01-15 tracey f = got_opentemp();
2216 ec46ccd7 2020-01-15 tracey if (f == NULL)
2217 ec46ccd7 2020-01-15 tracey return NULL;
2219 90f16cb8 2020-01-24 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2220 ec46ccd7 2020-01-15 tracey if (error)
2221 ec46ccd7 2020-01-15 tracey goto done;
2223 05ce9a79 2020-01-24 tracey if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
2224 05ce9a79 2020-01-24 tracey error = got_repo_match_object_id(&id1, &label1,
2225 05ce9a79 2020-01-24 tracey header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2226 05ce9a79 2020-01-24 tracey if (error)
2227 05ce9a79 2020-01-24 tracey goto done;
2230 90f16cb8 2020-01-24 tracey error = got_repo_match_object_id(&id2, &label2,
2231 2ac037ec 2020-01-24 tracey header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
2232 90f16cb8 2020-01-24 tracey if (error)
2233 90f16cb8 2020-01-24 tracey goto done;
2235 05ce9a79 2020-01-24 tracey error = got_object_get_type(&obj_type, header->repo, id2);
2236 90f16cb8 2020-01-24 tracey if (error)
2237 90f16cb8 2020-01-24 tracey goto done;
2238 05ce9a79 2020-01-24 tracey switch (obj_type) {
2239 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
2240 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
2241 90f16cb8 2020-01-24 tracey header->repo, f);
2243 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
2244 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
2245 90f16cb8 2020-01-24 tracey header->repo, f);
2247 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
2248 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_commits(id1, id2, 3, 0,
2249 90f16cb8 2020-01-24 tracey header->repo, f);
2251 ec46ccd7 2020-01-15 tracey default:
2252 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2254 b8b04565 2020-02-02 tracey if (error)
2255 b8b04565 2020-02-02 tracey goto done;
2257 d4729381 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2258 d4729381 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2259 ec46ccd7 2020-01-15 tracey goto done;
2262 83eb9a68 2020-02-03 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2263 f7cc9480 2020-02-05 tracey error = gw_colordiff_line(gw_trans, line);
2264 ec46ccd7 2020-01-15 tracey if (error)
2265 b8b04565 2020-02-02 tracey goto done;
2266 f7cc9480 2020-02-05 tracey /* XXX: KHTML_PRETTY breaks this */
2267 f7cc9480 2020-02-05 tracey kerr = khtml_puts(gw_trans->gw_html_req, line);
2268 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2269 83eb9a68 2020-02-03 stsp goto done;
2270 f7cc9480 2020-02-05 tracey kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
2271 f7cc9480 2020-02-05 tracey if (kerr != KCGI_OK)
2272 b8b04565 2020-02-02 tracey goto done;
2274 f7cc9480 2020-02-05 tracey if (linelen == -1 && ferror(f))
2275 83eb9a68 2020-02-03 stsp error = got_error_from_errno("getline");
2277 d4729381 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2278 d4729381 2020-02-03 stsp error = got_error_from_errno("fclose");
2279 d4729381 2020-02-03 stsp free(line);
2280 ec46ccd7 2020-01-15 tracey free(label1);
2281 ec46ccd7 2020-01-15 tracey free(label2);
2282 ec46ccd7 2020-01-15 tracey free(id1);
2283 ec46ccd7 2020-01-15 tracey free(id2);
2285 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
2286 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
2287 83eb9a68 2020-02-03 stsp return error;
2290 9a1cc63f 2020-02-03 stsp static const struct got_error *
2291 9a1cc63f 2020-02-03 stsp gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
2293 9a1cc63f 2020-02-03 stsp const struct got_error *error = NULL;
2294 9a1cc63f 2020-02-03 stsp struct got_repository *repo;
2295 9a1cc63f 2020-02-03 stsp const char *gitconfig_owner;
2297 9a1cc63f 2020-02-03 stsp *owner = NULL;
2299 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner == 0)
2300 9a1cc63f 2020-02-03 stsp return NULL;
2302 9a1cc63f 2020-02-03 stsp error = got_repo_open(&repo, dir, NULL);
2303 9a1cc63f 2020-02-03 stsp if (error)
2304 9a1cc63f 2020-02-03 stsp return error;
2305 9a1cc63f 2020-02-03 stsp gitconfig_owner = got_repo_get_gitconfig_owner(repo);
2306 9a1cc63f 2020-02-03 stsp if (gitconfig_owner) {
2307 9a1cc63f 2020-02-03 stsp *owner = strdup(gitconfig_owner);
2308 9a1cc63f 2020-02-03 stsp if (*owner == NULL)
2309 9a1cc63f 2020-02-03 stsp error = got_error_from_errno("strdup");
2311 9a1cc63f 2020-02-03 stsp got_repo_close(repo);
2312 9a1cc63f 2020-02-03 stsp return error;
2315 59d3c40e 2020-02-03 stsp static const struct got_error *
2316 59d3c40e 2020-02-03 stsp gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
2318 59d3c40e 2020-02-03 stsp const struct got_error *error = NULL;
2319 2c251c14 2020-01-15 tracey FILE *f;
2320 59d3c40e 2020-02-03 stsp char *d_file = NULL;
2321 2c251c14 2020-01-15 tracey unsigned int len;
2324 59d3c40e 2020-02-03 stsp *url = NULL;
2326 59d3c40e 2020-02-03 stsp if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2327 59d3c40e 2020-02-03 stsp return got_error_from_errno("asprintf");
2329 59d3c40e 2020-02-03 stsp f = fopen(d_file, "r");
2330 59d3c40e 2020-02-03 stsp if (f == NULL) {
2331 59d3c40e 2020-02-03 stsp if (errno != ENOENT && errno != EACCES)
2332 59d3c40e 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
2333 59d3c40e 2020-02-03 stsp goto done;
2336 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
2337 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2338 59d3c40e 2020-02-03 stsp goto done;
2340 59d3c40e 2020-02-03 stsp len = ftell(f);
2341 59d3c40e 2020-02-03 stsp if (len == -1) {
2342 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2343 59d3c40e 2020-02-03 stsp goto done;
2345 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
2346 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2347 59d3c40e 2020-02-03 stsp goto done;
2350 59d3c40e 2020-02-03 stsp *url = calloc(len + 1, sizeof(**url));
2351 59d3c40e 2020-02-03 stsp if (*url == NULL) {
2352 59d3c40e 2020-02-03 stsp error = got_error_from_errno("calloc");
2353 59d3c40e 2020-02-03 stsp goto done;
2356 59d3c40e 2020-02-03 stsp n = fread(*url, 1, len, f);
2357 59d3c40e 2020-02-03 stsp if (n == 0 && ferror(f))
2358 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2360 59d3c40e 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
2361 59d3c40e 2020-02-03 stsp error = got_error_from_errno("fclose");
2362 2c251c14 2020-01-15 tracey free(d_file);
2363 59d3c40e 2020-02-03 stsp return NULL;
2366 6eccd105 2020-02-03 stsp static const struct got_error *
2367 6eccd105 2020-02-03 stsp gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
2368 6eccd105 2020-02-03 stsp struct gw_header *header, int limit, int tag_type)
2370 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2371 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2372 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2373 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2374 6eccd105 2020-02-03 stsp char *tag_row = NULL, *tags_navs_disp = NULL;
2375 6eccd105 2020-02-03 stsp char *age = NULL, *age_html = NULL, *newline;
2376 070fee27 2020-02-03 stsp char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
2377 6eccd105 2020-02-03 stsp char *id_str = NULL, *refstr = NULL;
2378 6eccd105 2020-02-03 stsp char *tag_commit0 = NULL;
2379 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2380 87f9ebf5 2020-01-15 tracey size_t newsize;
2381 6eccd105 2020-02-03 stsp struct got_tag_object *tag = NULL;
2383 6eccd105 2020-02-03 stsp *tag_html = NULL;
2385 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2387 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2388 ec46ccd7 2020-01-15 tracey if (error)
2389 6c6c85af 2020-01-15 tracey return NULL;
2391 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2392 ec46ccd7 2020-01-15 tracey if (error)
2393 87f9ebf5 2020-01-15 tracey goto done;
2395 add40c4f 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
2396 87f9ebf5 2020-01-15 tracey if (error)
2397 87f9ebf5 2020-01-15 tracey goto done;
2399 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2400 87f9ebf5 2020-01-15 tracey const char *refname;
2401 4ff7391f 2020-01-28 tracey const char *tagger;
2402 6eccd105 2020-02-03 stsp const char *tag_commit;
2403 87f9ebf5 2020-01-15 tracey time_t tagger_time;
2404 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
2406 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
2407 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
2408 87f9ebf5 2020-01-15 tracey continue;
2409 87f9ebf5 2020-01-15 tracey refname += 10;
2410 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
2411 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
2412 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2413 87f9ebf5 2020-01-15 tracey goto done;
2416 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
2417 87f9ebf5 2020-01-15 tracey if (error)
2418 87f9ebf5 2020-01-15 tracey goto done;
2419 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
2420 87f9ebf5 2020-01-15 tracey free(id);
2421 87f9ebf5 2020-01-15 tracey if (error)
2422 87f9ebf5 2020-01-15 tracey goto done;
2424 4ff7391f 2020-01-28 tracey tagger = got_object_tag_get_tagger(tag);
2425 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
2427 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
2428 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
2429 87f9ebf5 2020-01-15 tracey if (error)
2430 87f9ebf5 2020-01-15 tracey goto done;
2432 f2f46662 2020-01-23 tracey tag_commit0 = strdup(got_object_tag_get_message(tag));
2433 f2f46662 2020-01-23 tracey if (tag_commit0 == NULL) {
2434 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
2435 87f9ebf5 2020-01-15 tracey goto done;
2438 f2f46662 2020-01-23 tracey tag_commit = tag_commit0;
2439 f2f46662 2020-01-23 tracey while (*tag_commit == '\n')
2440 f2f46662 2020-01-23 tracey tag_commit++;
2442 87f9ebf5 2020-01-15 tracey switch (tag_type) {
2443 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
2444 f2f46662 2020-01-23 tracey newline = strchr(tag_commit, '\n');
2445 87f9ebf5 2020-01-15 tracey if (newline)
2446 87f9ebf5 2020-01-15 tracey *newline = '\0';
2448 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_DIFF);
2449 55ccf528 2020-02-03 stsp if (error)
2450 87f9ebf5 2020-01-15 tracey goto done;
2452 88d59c36 2020-01-29 stsp if (asprintf(&tags_navs_disp, tags_navs,
2453 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
2454 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
2455 88d59c36 2020-01-29 stsp gw_trans->repo_name, id_str) == -1) {
2456 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2457 87f9ebf5 2020-01-15 tracey goto done;
2460 55ccf528 2020-02-03 stsp if (asprintf(&tag_row, tags_row, age ? age : "",
2461 55ccf528 2020-02-03 stsp refname, tag_commit, tags_navs_disp) == -1) {
2462 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2463 87f9ebf5 2020-01-15 tracey goto done;
2466 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
2468 87f9ebf5 2020-01-15 tracey case TAGFULL:
2469 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_LONG);
2470 55ccf528 2020-02-03 stsp if (error)
2471 4ff7391f 2020-01-28 tracey goto done;
2472 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_tagger, tagger);
2473 070fee27 2020-02-03 stsp if (error)
2474 070fee27 2020-02-03 stsp goto done;
2475 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_tag_commit, tag_commit);
2476 070fee27 2020-02-03 stsp if (error)
2477 070fee27 2020-02-03 stsp goto done;
2478 55ccf528 2020-02-03 stsp if (asprintf(&tag_row, tag_info, age ? age : "",
2479 070fee27 2020-02-03 stsp escaped_tagger, escaped_tag_commit) == -1) {
2480 4ff7391f 2020-01-28 tracey error = got_error_from_errno("asprintf");
2481 4ff7391f 2020-01-28 tracey goto done;
2484 87f9ebf5 2020-01-15 tracey default:
2488 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
2489 6eccd105 2020-02-03 stsp if (error)
2490 6eccd105 2020-02-03 stsp goto done;
2492 6eccd105 2020-02-03 stsp if (limit && --limit == 0)
2495 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
2496 6eccd105 2020-02-03 stsp tag = NULL;
2497 6c6c85af 2020-01-15 tracey free(id_str);
2498 6eccd105 2020-02-03 stsp id_str = NULL;
2499 6c6c85af 2020-01-15 tracey free(refstr);
2500 6eccd105 2020-02-03 stsp refstr = NULL;
2501 6c6c85af 2020-01-15 tracey free(age);
2502 55ccf528 2020-02-03 stsp age = NULL;
2503 55ccf528 2020-02-03 stsp free(age_html);
2504 55ccf528 2020-02-03 stsp age_html = NULL;
2505 070fee27 2020-02-03 stsp free(escaped_tagger);
2506 070fee27 2020-02-03 stsp escaped_tagger = NULL;
2507 070fee27 2020-02-03 stsp free(escaped_tag_commit);
2508 070fee27 2020-02-03 stsp escaped_tag_commit = NULL;
2509 f2f46662 2020-01-23 tracey free(tag_commit0);
2510 6eccd105 2020-02-03 stsp tag_commit0 = NULL;
2511 87f9ebf5 2020-01-15 tracey free(tag_row);
2512 6eccd105 2020-02-03 stsp tag_row = NULL;
2515 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2516 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2517 6eccd105 2020-02-03 stsp *tag_html = strdup(buf_get(diffbuf));
2518 6eccd105 2020-02-03 stsp if (*tag_html == NULL)
2519 6eccd105 2020-02-03 stsp error = got_error_from_errno("strdup");
2523 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
2524 6eccd105 2020-02-03 stsp free(id_str);
2525 6eccd105 2020-02-03 stsp free(refstr);
2526 55ccf528 2020-02-03 stsp free(age);
2527 55ccf528 2020-02-03 stsp free(age_html);
2528 070fee27 2020-02-03 stsp free(escaped_tagger);
2529 070fee27 2020-02-03 stsp free(escaped_tag_commit);
2530 6eccd105 2020-02-03 stsp free(tag_commit0);
2531 6eccd105 2020-02-03 stsp free(tag_row);
2532 6eccd105 2020-02-03 stsp buf_free(diffbuf);
2533 6eccd105 2020-02-03 stsp got_ref_list_free(&refs);
2535 6eccd105 2020-02-03 stsp got_repo_close(repo);
2536 6eccd105 2020-02-03 stsp return error;
2539 f2f46662 2020-01-23 tracey static void
2540 f2f46662 2020-01-23 tracey gw_free_headers(struct gw_header *header)
2542 f2f46662 2020-01-23 tracey free(header->id);
2543 f2f46662 2020-01-23 tracey free(header->path);
2544 f2f46662 2020-01-23 tracey if (header->commit != NULL)
2545 f2f46662 2020-01-23 tracey got_object_commit_close(header->commit);
2546 f2f46662 2020-01-23 tracey if (header->repo)
2547 f2f46662 2020-01-23 tracey got_repo_close(header->repo);
2548 f2f46662 2020-01-23 tracey free(header->refs_str);
2549 f2f46662 2020-01-23 tracey free(header->commit_id);
2550 f2f46662 2020-01-23 tracey free(header->parent_id);
2551 f2f46662 2020-01-23 tracey free(header->tree_id);
2552 f2f46662 2020-01-23 tracey free(header->commit_msg);
2555 f2f46662 2020-01-23 tracey static struct gw_header *
2556 f2f46662 2020-01-23 tracey gw_init_header()
2558 f2f46662 2020-01-23 tracey struct gw_header *header;
2560 ae36ed87 2020-01-28 stsp header = malloc(sizeof(*header));
2561 ae36ed87 2020-01-28 stsp if (header == NULL)
2562 f2f46662 2020-01-23 tracey return NULL;
2564 f2f46662 2020-01-23 tracey header->repo = NULL;
2565 f2f46662 2020-01-23 tracey header->commit = NULL;
2566 f2f46662 2020-01-23 tracey header->id = NULL;
2567 f2f46662 2020-01-23 tracey header->path = NULL;
2568 ae36ed87 2020-01-28 stsp SIMPLEQ_INIT(&header->refs);
2570 f2f46662 2020-01-23 tracey return header;
2573 f2f46662 2020-01-23 tracey static const struct got_error *
2574 f2f46662 2020-01-23 tracey gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2575 f2f46662 2020-01-23 tracey int limit)
2577 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2578 f2f46662 2020-01-23 tracey struct got_commit_graph *graph = NULL;
2580 f2f46662 2020-01-23 tracey error = got_commit_graph_open(&graph, header->path, 0);
2581 f2f46662 2020-01-23 tracey if (error)
2582 f2f46662 2020-01-23 tracey goto done;
2584 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_start(graph, header->id, header->repo,
2585 f2f46662 2020-01-23 tracey NULL, NULL);
2586 f2f46662 2020-01-23 tracey if (error)
2587 f2f46662 2020-01-23 tracey goto done;
2589 f2f46662 2020-01-23 tracey for (;;) {
2590 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_next(&header->id, graph,
2591 f2f46662 2020-01-23 tracey header->repo, NULL, NULL);
2592 f2f46662 2020-01-23 tracey if (error) {
2593 f2f46662 2020-01-23 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
2594 f2f46662 2020-01-23 tracey error = NULL;
2595 f2f46662 2020-01-23 tracey goto done;
2597 f2f46662 2020-01-23 tracey if (header->id == NULL)
2598 f2f46662 2020-01-23 tracey goto done;
2600 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit, header->repo,
2601 f2f46662 2020-01-23 tracey header->id);
2602 f2f46662 2020-01-23 tracey if (error)
2603 f2f46662 2020-01-23 tracey goto done;
2605 f2f46662 2020-01-23 tracey error = gw_get_commit(gw_trans, header);
2606 f2f46662 2020-01-23 tracey if (limit > 1) {
2607 f2f46662 2020-01-23 tracey struct gw_header *n_header = NULL;
2608 5147ab56 2020-01-29 stsp if ((n_header = gw_init_header()) == NULL) {
2609 f2f46662 2020-01-23 tracey error = got_error_from_errno("malloc");
2610 5147ab56 2020-01-29 stsp goto done;
2613 f2f46662 2020-01-23 tracey n_header->refs_str = strdup(header->refs_str);
2614 f2f46662 2020-01-23 tracey n_header->commit_id = strdup(header->commit_id);
2615 f2f46662 2020-01-23 tracey n_header->parent_id = strdup(header->parent_id);
2616 f2f46662 2020-01-23 tracey n_header->tree_id = strdup(header->tree_id);
2617 f2f46662 2020-01-23 tracey n_header->author = strdup(header->author);
2618 f2f46662 2020-01-23 tracey n_header->committer = strdup(header->committer);
2619 f2f46662 2020-01-23 tracey n_header->commit_msg = strdup(header->commit_msg);
2620 f2f46662 2020-01-23 tracey n_header->committer_time = header->committer_time;
2621 f2f46662 2020-01-23 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2624 f2f46662 2020-01-23 tracey if (error || (limit && --limit == 0))
2628 f2f46662 2020-01-23 tracey if (graph)
2629 f2f46662 2020-01-23 tracey got_commit_graph_close(graph);
2630 f2f46662 2020-01-23 tracey return error;
2633 f2f46662 2020-01-23 tracey static const struct got_error *
2634 f2f46662 2020-01-23 tracey gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2636 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2637 f2f46662 2020-01-23 tracey struct got_reflist_entry *re;
2638 f2f46662 2020-01-23 tracey struct got_object_id *id2 = NULL;
2639 f2f46662 2020-01-23 tracey struct got_object_qid *parent_id;
2640 e46f587c 2020-01-29 tracey char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2642 f2f46662 2020-01-23 tracey /*print commit*/
2643 f2f46662 2020-01-23 tracey SIMPLEQ_FOREACH(re, &header->refs, entry) {
2644 f2f46662 2020-01-23 tracey char *s;
2645 f2f46662 2020-01-23 tracey const char *name;
2646 f2f46662 2020-01-23 tracey struct got_tag_object *tag = NULL;
2647 f2f46662 2020-01-23 tracey int cmp;
2649 f2f46662 2020-01-23 tracey name = got_ref_get_name(re->ref);
2650 f2f46662 2020-01-23 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
2651 f2f46662 2020-01-23 tracey continue;
2652 f2f46662 2020-01-23 tracey if (strncmp(name, "refs/", 5) == 0)
2653 f2f46662 2020-01-23 tracey name += 5;
2654 f2f46662 2020-01-23 tracey if (strncmp(name, "got/", 4) == 0)
2655 f2f46662 2020-01-23 tracey continue;
2656 f2f46662 2020-01-23 tracey if (strncmp(name, "heads/", 6) == 0)
2657 f2f46662 2020-01-23 tracey name += 6;
2658 f2f46662 2020-01-23 tracey if (strncmp(name, "remotes/", 8) == 0)
2659 f2f46662 2020-01-23 tracey name += 8;
2660 f2f46662 2020-01-23 tracey if (strncmp(name, "tags/", 5) == 0) {
2661 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag, header->repo,
2662 f2f46662 2020-01-23 tracey re->id);
2663 f2f46662 2020-01-23 tracey if (error) {
2664 f2f46662 2020-01-23 tracey if (error->code != GOT_ERR_OBJ_TYPE)
2665 f2f46662 2020-01-23 tracey continue;
2667 f2f46662 2020-01-23 tracey * Ref points at something other
2668 f2f46662 2020-01-23 tracey * than a tag.
2670 f2f46662 2020-01-23 tracey error = NULL;
2671 f2f46662 2020-01-23 tracey tag = NULL;
2674 f2f46662 2020-01-23 tracey cmp = got_object_id_cmp(tag ?
2675 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag) : re->id, header->id);
2676 f2f46662 2020-01-23 tracey if (tag)
2677 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2678 f2f46662 2020-01-23 tracey if (cmp != 0)
2679 f2f46662 2020-01-23 tracey continue;
2680 f2f46662 2020-01-23 tracey s = refs_str;
2681 88d59c36 2020-01-29 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2682 88d59c36 2020-01-29 stsp s ? ", " : "", name) == -1) {
2683 f2f46662 2020-01-23 tracey error = got_error_from_errno("asprintf");
2684 f2f46662 2020-01-23 tracey free(s);
2685 f2f46662 2020-01-23 tracey return error;
2687 f2f46662 2020-01-23 tracey header->refs_str = strdup(refs_str);
2688 f2f46662 2020-01-23 tracey free(s);
2691 f2f46662 2020-01-23 tracey if (refs_str == NULL)
2692 f2f46662 2020-01-23 tracey header->refs_str = strdup("");
2693 f2f46662 2020-01-23 tracey free(refs_str);
2695 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->commit_id, header->id);
2696 f2f46662 2020-01-23 tracey if (error)
2697 f2f46662 2020-01-23 tracey return error;
2699 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->tree_id,
2700 f2f46662 2020-01-23 tracey got_object_commit_get_tree_id(header->commit));
2701 f2f46662 2020-01-23 tracey if (error)
2702 f2f46662 2020-01-23 tracey return error;
2704 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_DIFF) {
2705 f2f46662 2020-01-23 tracey parent_id = SIMPLEQ_FIRST(
2706 f2f46662 2020-01-23 tracey got_object_commit_get_parent_ids(header->commit));
2707 f2f46662 2020-01-23 tracey if (parent_id != NULL) {
2708 f2f46662 2020-01-23 tracey id2 = got_object_id_dup(parent_id->id);
2709 f2f46662 2020-01-23 tracey free (parent_id);
2710 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->parent_id, id2);
2711 f2f46662 2020-01-23 tracey if (error)
2712 f2f46662 2020-01-23 tracey return error;
2713 f2f46662 2020-01-23 tracey free(id2);
2715 f2f46662 2020-01-23 tracey header->parent_id = strdup("/dev/null");
2717 f2f46662 2020-01-23 tracey header->parent_id = strdup("");
2719 f2f46662 2020-01-23 tracey header->committer_time =
2720 f2f46662 2020-01-23 tracey got_object_commit_get_committer_time(header->commit);
2722 bd275801 2020-02-03 tracey header->author =
2723 bd275801 2020-02-03 tracey got_object_commit_get_author(header->commit);
2724 27192be7 2020-02-04 tracey header->committer =
2725 27192be7 2020-02-04 tracey got_object_commit_get_committer(header->commit);
2726 070fee27 2020-02-03 stsp if (error)
2727 070fee27 2020-02-03 stsp return error;
2729 070fee27 2020-02-03 stsp /* XXX Doesn't the log message require escaping? */
2730 f2f46662 2020-01-23 tracey error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2731 f2f46662 2020-01-23 tracey if (error)
2732 f2f46662 2020-01-23 tracey return error;
2734 f2f46662 2020-01-23 tracey commit_msg = commit_msg0;
2735 f2f46662 2020-01-23 tracey while (*commit_msg == '\n')
2736 f2f46662 2020-01-23 tracey commit_msg++;
2738 f2f46662 2020-01-23 tracey header->commit_msg = strdup(commit_msg);
2739 f2f46662 2020-01-23 tracey free(commit_msg0);
2740 f2f46662 2020-01-23 tracey return error;
2743 f2f46662 2020-01-23 tracey static const struct got_error *
2744 f2f46662 2020-01-23 tracey gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2746 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2747 f2f46662 2020-01-23 tracey char *in_repo_path = NULL;
2749 f2f46662 2020-01-23 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2750 f2f46662 2020-01-23 tracey if (error)
2751 f2f46662 2020-01-23 tracey return error;
2753 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
2754 f2f46662 2020-01-23 tracey struct got_reference *head_ref;
2755 f2f46662 2020-01-23 tracey error = got_ref_open(&head_ref, header->repo,
2756 f2f46662 2020-01-23 tracey gw_trans->headref, 0);
2757 f2f46662 2020-01-23 tracey if (error)
2758 f2f46662 2020-01-23 tracey return error;
2760 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, head_ref);
2761 f2f46662 2020-01-23 tracey got_ref_close(head_ref);
2762 f2f46662 2020-01-23 tracey if (error)
2763 f2f46662 2020-01-23 tracey return error;
2765 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2766 f2f46662 2020-01-23 tracey header->repo, header->id);
2767 f2f46662 2020-01-23 tracey } else {
2768 f2f46662 2020-01-23 tracey struct got_reference *ref;
2769 f2f46662 2020-01-23 tracey error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2770 f2f46662 2020-01-23 tracey if (error == NULL) {
2771 f2f46662 2020-01-23 tracey int obj_type;
2772 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, ref);
2773 f2f46662 2020-01-23 tracey got_ref_close(ref);
2774 f2f46662 2020-01-23 tracey if (error)
2775 f2f46662 2020-01-23 tracey return error;
2776 f2f46662 2020-01-23 tracey error = got_object_get_type(&obj_type, header->repo,
2777 f2f46662 2020-01-23 tracey header->id);
2778 f2f46662 2020-01-23 tracey if (error)
2779 f2f46662 2020-01-23 tracey return error;
2780 f2f46662 2020-01-23 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
2781 f2f46662 2020-01-23 tracey struct got_tag_object *tag;
2782 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag,
2783 f2f46662 2020-01-23 tracey header->repo, header->id);
2784 f2f46662 2020-01-23 tracey if (error)
2785 f2f46662 2020-01-23 tracey return error;
2786 f2f46662 2020-01-23 tracey if (got_object_tag_get_object_type(tag) !=
2787 f2f46662 2020-01-23 tracey GOT_OBJ_TYPE_COMMIT) {
2788 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2789 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2790 f2f46662 2020-01-23 tracey return error;
2792 f2f46662 2020-01-23 tracey free(header->id);
2793 f2f46662 2020-01-23 tracey header->id = got_object_id_dup(
2794 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag));
2795 f2f46662 2020-01-23 tracey if (header->id == NULL)
2796 f2f46662 2020-01-23 tracey error = got_error_from_errno(
2797 f2f46662 2020-01-23 tracey "got_object_id_dup");
2798 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2799 f2f46662 2020-01-23 tracey if (error)
2800 f2f46662 2020-01-23 tracey return error;
2801 f2f46662 2020-01-23 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2802 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2803 f2f46662 2020-01-23 tracey return error;
2805 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2806 f2f46662 2020-01-23 tracey header->repo, header->id);
2807 f2f46662 2020-01-23 tracey if (error)
2808 f2f46662 2020-01-23 tracey return error;
2810 f2f46662 2020-01-23 tracey if (header->commit == NULL) {
2811 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2812 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2813 f2f46662 2020-01-23 tracey header->repo);
2814 f2f46662 2020-01-23 tracey if (error)
2815 f2f46662 2020-01-23 tracey return error;
2817 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2818 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2819 f2f46662 2020-01-23 tracey header->repo);
2822 f2f46662 2020-01-23 tracey error = got_repo_map_path(&in_repo_path, header->repo,
2823 f2f46662 2020-01-23 tracey gw_trans->repo_path, 1);
2824 f2f46662 2020-01-23 tracey if (error)
2825 f2f46662 2020-01-23 tracey return error;
2827 f2f46662 2020-01-23 tracey if (in_repo_path) {
2828 f2f46662 2020-01-23 tracey header->path = strdup(in_repo_path);
2830 f2f46662 2020-01-23 tracey free(in_repo_path);
2832 f2f46662 2020-01-23 tracey error = got_ref_list(&header->refs, header->repo, NULL,
2833 f2f46662 2020-01-23 tracey got_ref_cmp_by_name, NULL);
2834 f2f46662 2020-01-23 tracey if (error)
2835 f2f46662 2020-01-23 tracey return error;
2837 f2f46662 2020-01-23 tracey error = gw_get_commits(gw_trans, header, limit);
2838 f2f46662 2020-01-23 tracey return error;
2841 ec46ccd7 2020-01-15 tracey struct blame_line {
2842 ec46ccd7 2020-01-15 tracey int annotated;
2843 ec46ccd7 2020-01-15 tracey char *id_str;
2844 ec46ccd7 2020-01-15 tracey char *committer;
2845 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
2848 147269d5 2020-01-15 tracey struct gw_blame_cb_args {
2849 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
2850 ec46ccd7 2020-01-15 tracey int nlines;
2851 ec46ccd7 2020-01-15 tracey int nlines_prec;
2852 ec46ccd7 2020-01-15 tracey int lineno_cur;
2853 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
2854 ec46ccd7 2020-01-15 tracey FILE *f;
2855 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
2856 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
2857 2e676fc5 2020-01-15 tracey struct buf *blamebuf;
2860 ec46ccd7 2020-01-15 tracey static const struct got_error *
2861 147269d5 2020-01-15 tracey gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2863 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
2864 147269d5 2020-01-15 tracey struct gw_blame_cb_args *a = arg;
2865 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
2866 ec46ccd7 2020-01-15 tracey char *line = NULL;
2867 2e676fc5 2020-01-15 tracey size_t linesize = 0, newsize;
2868 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
2869 ec46ccd7 2020-01-15 tracey off_t offset;
2870 ec46ccd7 2020-01-15 tracey struct tm tm;
2871 ec46ccd7 2020-01-15 tracey time_t committer_time;
2873 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
2874 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
2875 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
2877 ec46ccd7 2020-01-15 tracey if (lineno == -1)
2878 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
2880 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
2881 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
2882 ec46ccd7 2020-01-15 tracey if (bline->annotated)
2883 ec46ccd7 2020-01-15 tracey return NULL;
2884 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
2885 ec46ccd7 2020-01-15 tracey if (err)
2886 ec46ccd7 2020-01-15 tracey return err;
2888 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
2889 ec46ccd7 2020-01-15 tracey if (err)
2890 ec46ccd7 2020-01-15 tracey goto done;
2892 ec46ccd7 2020-01-15 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
2893 ec46ccd7 2020-01-15 tracey if (bline->committer == NULL) {
2894 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
2895 ec46ccd7 2020-01-15 tracey goto done;
2898 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
2899 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
2900 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
2901 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2902 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
2903 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
2904 ec46ccd7 2020-01-15 tracey goto done;
2906 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
2908 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
2909 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2910 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
2911 ec46ccd7 2020-01-15 tracey goto done;
2913 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
2914 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
2915 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
2916 ec46ccd7 2020-01-15 tracey goto done;
2919 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
2920 0311ce2d 2020-01-15 tracey char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2921 0311ce2d 2020-01-15 tracey *line_escape = NULL;
2922 ec46ccd7 2020-01-15 tracey size_t len;
2924 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
2925 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
2926 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
2930 ec46ccd7 2020-01-15 tracey committer = bline->committer;
2931 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
2932 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
2933 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
2934 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
2936 ec46ccd7 2020-01-15 tracey *at = '\0';
2937 ec46ccd7 2020-01-15 tracey len = strlen(committer);
2938 ec46ccd7 2020-01-15 tracey if (len >= 9)
2939 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
2941 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
2943 ec46ccd7 2020-01-15 tracey *nl = '\0';
2945 070fee27 2020-02-03 stsp err = gw_html_escape(&line_escape, line);
2949 de6bdba4 2020-01-31 tracey if (a->gw_trans->repo_folder == NULL)
2950 de6bdba4 2020-01-31 tracey a->gw_trans->repo_folder = strdup("");
2951 de6bdba4 2020-01-31 tracey if (a->gw_trans->repo_folder == NULL)
2952 de6bdba4 2020-01-31 tracey goto err;
2953 10d47faf 2020-01-31 tracey asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2954 10d47faf 2020-01-31 tracey a->gw_trans->repo_name, bline->id_str,
2955 10d47faf 2020-01-31 tracey a->gw_trans->repo_file, a->gw_trans->repo_folder,
2956 10d47faf 2020-01-31 tracey bline->id_str, bline->datebuf, committer, line_escape);
2957 ec46ccd7 2020-01-15 tracey a->lineno_cur++;
2958 2e676fc5 2020-01-15 tracey err = buf_puts(&newsize, a->blamebuf, blame_row);
2959 2e676fc5 2020-01-15 tracey if (err)
2960 2e676fc5 2020-01-15 tracey return err;
2962 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2964 0311ce2d 2020-01-15 tracey free(line_escape);
2965 2e676fc5 2020-01-15 tracey free(blame_row);
2968 ec46ccd7 2020-01-15 tracey if (commit)
2969 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
2970 ec46ccd7 2020-01-15 tracey free(line);
2971 ec46ccd7 2020-01-15 tracey return err;
2974 a81f3554 2020-02-03 stsp static const struct got_error *
2975 a81f3554 2020-02-03 stsp gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2977 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
2978 bcbc97d8 2020-01-15 tracey struct got_repository *repo = NULL;
2979 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
2980 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
2981 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
2982 a81f3554 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
2983 147269d5 2020-01-15 tracey struct gw_blame_cb_args bca;
2984 119bf4ed 2020-01-15 tracey int i, obj_type;
2985 ec46ccd7 2020-01-15 tracey size_t filesize;
2987 a81f3554 2020-02-03 stsp *blame_html = NULL;
2989 ec46ccd7 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2990 ec46ccd7 2020-01-15 tracey if (error)
2991 a81f3554 2020-02-03 stsp return error;
2993 a81f3554 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
2994 a81f3554 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2995 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
2996 a81f3554 2020-02-03 stsp gw_trans->repo_file) == -1) {
2997 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2998 2e676fc5 2020-01-15 tracey goto done;
3001 2e676fc5 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, path, 1);
3002 ec46ccd7 2020-01-15 tracey if (error)
3003 ec46ccd7 2020-01-15 tracey goto done;
3005 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3006 147269d5 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT, 1, repo);
3007 ec46ccd7 2020-01-15 tracey if (error)
3008 ec46ccd7 2020-01-15 tracey goto done;
3010 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3011 ec46ccd7 2020-01-15 tracey if (error)
3012 ec46ccd7 2020-01-15 tracey goto done;
3014 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
3015 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
3016 ec46ccd7 2020-01-15 tracey goto done;
3019 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, obj_id);
3020 ec46ccd7 2020-01-15 tracey if (error)
3021 ec46ccd7 2020-01-15 tracey goto done;
3023 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
3024 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
3025 ec46ccd7 2020-01-15 tracey goto done;
3028 ec46ccd7 2020-01-15 tracey error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3029 ec46ccd7 2020-01-15 tracey if (error)
3030 ec46ccd7 2020-01-15 tracey goto done;
3032 2e676fc5 2020-01-15 tracey error = buf_alloc(&bca.blamebuf, 0);
3033 2e676fc5 2020-01-15 tracey if (error)
3034 2e676fc5 2020-01-15 tracey goto done;
3036 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
3037 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
3038 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
3039 ec46ccd7 2020-01-15 tracey goto done;
3041 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3042 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
3043 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
3044 ec46ccd7 2020-01-15 tracey goto done;
3046 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
3047 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
3048 ec46ccd7 2020-01-15 tracey bca.nlines--;
3050 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3051 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
3052 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
3053 ec46ccd7 2020-01-15 tracey goto done;
3055 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
3056 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
3057 ec46ccd7 2020-01-15 tracey i = bca.nlines;
3058 ec46ccd7 2020-01-15 tracey while (i > 0) {
3059 ec46ccd7 2020-01-15 tracey i /= 10;
3060 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
3062 ec46ccd7 2020-01-15 tracey bca.repo = repo;
3063 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
3065 147269d5 2020-01-15 tracey error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
3066 147269d5 2020-01-15 tracey NULL, NULL);
3067 e46f587c 2020-01-29 tracey if (error)
3068 e46f587c 2020-01-29 tracey goto done;
3069 2e676fc5 2020-01-15 tracey if (buf_len(bca.blamebuf) > 0) {
3070 2e676fc5 2020-01-15 tracey error = buf_putc(bca.blamebuf, '\0');
3071 a81f3554 2020-02-03 stsp if (error)
3072 a81f3554 2020-02-03 stsp goto done;
3073 a81f3554 2020-02-03 stsp *blame_html = strdup(buf_get(bca.blamebuf));
3074 a81f3554 2020-02-03 stsp if (*blame_html == NULL) {
3075 a81f3554 2020-02-03 stsp error = got_error_from_errno("strdup");
3076 a81f3554 2020-02-03 stsp goto done;
3080 e46f587c 2020-01-29 tracey free(bca.line_offsets);
3081 2e676fc5 2020-01-15 tracey free(bca.blamebuf);
3082 2e676fc5 2020-01-15 tracey free(in_repo_path);
3083 2e676fc5 2020-01-15 tracey free(commit_id);
3084 2e676fc5 2020-01-15 tracey free(obj_id);
3085 2e676fc5 2020-01-15 tracey free(path);
3087 4fd4726a 2020-02-03 stsp for (i = 0; i < bca.nlines; i++) {
3088 4fd4726a 2020-02-03 stsp struct blame_line *bline = &bca.lines[i];
3089 4fd4726a 2020-02-03 stsp free(bline->id_str);
3090 4fd4726a 2020-02-03 stsp free(bline->committer);
3092 4fd4726a 2020-02-03 stsp free(bca.lines);
3093 2e676fc5 2020-01-15 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
3094 a81f3554 2020-02-03 stsp error = got_error_from_errno("fclose");
3096 4fd4726a 2020-02-03 stsp got_object_blob_close(blob);
3098 4fd4726a 2020-02-03 stsp got_repo_close(repo);
3099 4fd4726a 2020-02-03 stsp return error;
3102 4fd4726a 2020-02-03 stsp static const struct got_error *
3103 e0857cfe 2020-02-06 tracey gw_output_blob_buf(struct gw_trans *gw_trans)
3105 4fd4726a 2020-02-03 stsp const struct got_error *error = NULL;
3106 4fd4726a 2020-02-03 stsp struct got_repository *repo = NULL;
3107 4fd4726a 2020-02-03 stsp struct got_object_id *obj_id = NULL;
3108 4fd4726a 2020-02-03 stsp struct got_object_id *commit_id = NULL;
3109 4fd4726a 2020-02-03 stsp struct got_blob_object *blob = NULL;
3110 4fd4726a 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
3111 e0857cfe 2020-02-06 tracey int obj_type, set_mime = 0;
3112 e0857cfe 2020-02-06 tracey size_t len, hdrlen;
3113 e0857cfe 2020-02-06 tracey const uint8_t *buf;
3114 e0857cfe 2020-02-06 tracey enum kcgi_err kerr = KCGI_OK;
3116 4fd4726a 2020-02-03 stsp error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3117 4fd4726a 2020-02-03 stsp if (error)
3118 4fd4726a 2020-02-03 stsp return error;
3120 4fd4726a 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
3121 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3122 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
3123 4fd4726a 2020-02-03 stsp gw_trans->repo_file) == -1) {
3124 4fd4726a 2020-02-03 stsp error = got_error_from_errno("asprintf");
3125 4fd4726a 2020-02-03 stsp goto done;
3128 4fd4726a 2020-02-03 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3129 4fd4726a 2020-02-03 stsp if (error)
3130 4fd4726a 2020-02-03 stsp goto done;
3132 4fd4726a 2020-02-03 stsp error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
3133 4fd4726a 2020-02-03 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3134 4fd4726a 2020-02-03 stsp if (error)
3135 4fd4726a 2020-02-03 stsp goto done;
3137 4fd4726a 2020-02-03 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3138 4fd4726a 2020-02-03 stsp if (error)
3139 4fd4726a 2020-02-03 stsp goto done;
3141 4fd4726a 2020-02-03 stsp if (obj_id == NULL) {
3142 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_NO_OBJ);
3143 4fd4726a 2020-02-03 stsp goto done;
3146 4fd4726a 2020-02-03 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3147 4fd4726a 2020-02-03 stsp if (error)
3148 4fd4726a 2020-02-03 stsp goto done;
3150 4fd4726a 2020-02-03 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3151 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3152 4fd4726a 2020-02-03 stsp goto done;
3155 4fd4726a 2020-02-03 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3156 4fd4726a 2020-02-03 stsp if (error)
3157 4fd4726a 2020-02-03 stsp goto done;
3159 e0857cfe 2020-02-06 tracey hdrlen = got_object_blob_get_hdrlen(blob);
3161 e0857cfe 2020-02-06 tracey error = got_object_blob_read_block(&len, blob);
3162 e0857cfe 2020-02-06 tracey if (error)
3163 e0857cfe 2020-02-06 tracey goto done;
3164 e0857cfe 2020-02-06 tracey if (len == 0)
3166 e0857cfe 2020-02-06 tracey buf = got_object_blob_get_read_buf(blob);
3169 e0857cfe 2020-02-06 tracey * Skip blob object header first time around,
3170 e0857cfe 2020-02-06 tracey * which also contains a zero byte.
3172 e0857cfe 2020-02-06 tracey buf += hdrlen;
3173 e0857cfe 2020-02-06 tracey if (set_mime == 0) {
3174 e0857cfe 2020-02-06 tracey if (isbinary(buf, len - hdrlen))
3175 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_APP_OCTET_STREAM;
3177 e0857cfe 2020-02-06 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
3178 e0857cfe 2020-02-06 tracey set_mime = 1;
3179 e0857cfe 2020-02-06 tracey error = gw_display_index(gw_trans);
3180 e0857cfe 2020-02-06 tracey if (error)
3181 e0857cfe 2020-02-06 tracey goto done;
3183 e0857cfe 2020-02-06 tracey khttp_write(gw_trans->gw_req, buf, len - hdrlen);
3184 e0857cfe 2020-02-06 tracey hdrlen = 0;
3185 e0857cfe 2020-02-06 tracey } while (len != 0);
3187 4fd4726a 2020-02-03 stsp free(in_repo_path);
3188 4fd4726a 2020-02-03 stsp free(commit_id);
3189 4fd4726a 2020-02-03 stsp free(obj_id);
3190 4fd4726a 2020-02-03 stsp free(path);
3191 e46f587c 2020-01-29 tracey if (blob)
3192 a81f3554 2020-02-03 stsp got_object_blob_close(blob);
3193 e46f587c 2020-01-29 tracey if (repo)
3194 a81f3554 2020-02-03 stsp got_repo_close(repo);
3195 e0857cfe 2020-02-06 tracey if (error == NULL && kerr != KCGI_OK)
3196 e0857cfe 2020-02-06 tracey error = gw_kcgi_error(kerr);
3197 a81f3554 2020-02-03 stsp return error;
3200 84bf4df2 2020-02-03 stsp static const struct got_error *
3201 84bf4df2 2020-02-03 stsp gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
3203 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
3204 ec46ccd7 2020-01-15 tracey struct got_repository *repo = NULL;
3205 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
3206 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
3207 bcbc97d8 2020-01-15 tracey struct buf *diffbuf = NULL;
3208 bcbc97d8 2020-01-15 tracey size_t newsize;
3209 84bf4df2 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
3210 84bf4df2 2020-02-03 stsp char *id_str = NULL;
3211 84bf4df2 2020-02-03 stsp char *build_folder = NULL;
3212 84bf4df2 2020-02-03 stsp char *url_html = NULL;
3213 84bf4df2 2020-02-03 stsp const char *class = NULL;
3214 c3bcdfd5 2020-01-29 tracey int nentries, i, class_flip = 0;
3216 84bf4df2 2020-02-03 stsp *tree_html = NULL;
3218 bcbc97d8 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
3219 ec46ccd7 2020-01-15 tracey if (error)
3220 84bf4df2 2020-02-03 stsp return error;
3222 bcbc97d8 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3223 ec46ccd7 2020-01-15 tracey if (error)
3224 bcbc97d8 2020-01-15 tracey goto done;
3226 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL)
3227 ec46ccd7 2020-01-15 tracey path = strdup(gw_trans->repo_folder);
3229 84bf4df2 2020-02-03 stsp error = got_repo_map_path(&in_repo_path, repo,
3230 84bf4df2 2020-02-03 stsp gw_trans->repo_path, 1);
3231 84bf4df2 2020-02-03 stsp if (error)
3232 84bf4df2 2020-02-03 stsp goto done;
3233 bcbc97d8 2020-01-15 tracey free(path);
3234 bcbc97d8 2020-01-15 tracey path = in_repo_path;
3237 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
3238 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
3239 ec46ccd7 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
3240 ec46ccd7 2020-01-15 tracey if (error)
3241 ec46ccd7 2020-01-15 tracey goto done;
3242 ec46ccd7 2020-01-15 tracey error = got_ref_resolve(&commit_id, repo, head_ref);
3243 84bf4df2 2020-02-03 stsp if (error)
3244 84bf4df2 2020-02-03 stsp goto done;
3245 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
3248 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL,
3249 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3250 84bf4df2 2020-02-03 stsp if (error)
3251 84bf4df2 2020-02-03 stsp goto done;
3254 f2f46662 2020-01-23 tracey error = got_object_id_str(&gw_trans->commit, commit_id);
3255 bcbc97d8 2020-01-15 tracey if (error)
3256 bcbc97d8 2020-01-15 tracey goto done;
3258 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&tree_id, repo, commit_id, path);
3259 bcbc97d8 2020-01-15 tracey if (error)
3260 bcbc97d8 2020-01-15 tracey goto done;
3262 bcbc97d8 2020-01-15 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
3263 bcbc97d8 2020-01-15 tracey if (error)
3264 bcbc97d8 2020-01-15 tracey goto done;
3266 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
3267 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
3268 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
3269 ec46ccd7 2020-01-15 tracey const char *modestr = "";
3270 84bf4df2 2020-02-03 stsp mode_t mode;
3272 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
3274 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
3275 bcbc97d8 2020-01-15 tracey if (error)
3276 bcbc97d8 2020-01-15 tracey goto done;
3278 84bf4df2 2020-02-03 stsp mode = got_tree_entry_get_mode(te);
3279 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
3280 bcbc97d8 2020-01-15 tracey modestr = "$";
3281 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
3282 bcbc97d8 2020-01-15 tracey modestr = "@";
3283 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
3284 bcbc97d8 2020-01-15 tracey modestr = "/";
3285 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
3286 bcbc97d8 2020-01-15 tracey modestr = "*";
3288 c3bcdfd5 2020-01-29 tracey if (class_flip == 0) {
3289 84bf4df2 2020-02-03 stsp class = "back_lightgray";
3290 c3bcdfd5 2020-01-29 tracey class_flip = 1;
3291 c3bcdfd5 2020-01-29 tracey } else {
3292 84bf4df2 2020-02-03 stsp class = "back_white";
3293 c3bcdfd5 2020-01-29 tracey class_flip = 0;
3296 84bf4df2 2020-02-03 stsp if (S_ISDIR(mode)) {
3297 a5594e37 2020-02-05 tracey if (asprintf(&build_folder, "%s/%s",
3298 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3299 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te)) == -1) {
3300 84bf4df2 2020-02-03 stsp error = got_error_from_errno(
3301 84bf4df2 2020-02-03 stsp "asprintf");
3302 84bf4df2 2020-02-03 stsp goto done;
3305 88d59c36 2020-01-29 stsp if (asprintf(&url_html, folder_html,
3306 ec46ccd7 2020-01-15 tracey gw_trans->repo_name, gw_trans->action_name,
3307 ec46ccd7 2020-01-15 tracey gw_trans->commit, build_folder,
3308 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
3309 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
3310 ec46ccd7 2020-01-15 tracey goto done;
3312 84bf4df2 2020-02-03 stsp if (asprintf(&tree_row, tree_line, class, url_html,
3313 84bf4df2 2020-02-03 stsp class) == -1) {
3314 84bf4df2 2020-02-03 stsp error = got_error_from_errno("asprintf");
3315 84bf4df2 2020-02-03 stsp goto done;
3317 ec46ccd7 2020-01-15 tracey } else {
3318 88d59c36 2020-01-29 stsp if (asprintf(&url_html, file_html, gw_trans->repo_name,
3319 e46f587c 2020-01-29 tracey "blob", gw_trans->commit,
3320 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
3321 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3322 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
3323 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
3324 ec46ccd7 2020-01-15 tracey goto done;
3327 e46f587c 2020-01-29 tracey if (asprintf(&tree_row, tree_line_with_navs, class,
3328 84bf4df2 2020-02-03 stsp url_html, class, gw_trans->repo_name, "blob",
3329 84bf4df2 2020-02-03 stsp gw_trans->commit, got_tree_entry_get_name(te),
3330 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3331 84bf4df2 2020-02-03 stsp "blob", gw_trans->repo_name,
3332 84bf4df2 2020-02-03 stsp "blame", gw_trans->commit,
3333 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
3334 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
3335 84bf4df2 2020-02-03 stsp "blame") == -1) {
3336 e46f587c 2020-01-29 tracey error = got_error_from_errno("asprintf");
3337 e46f587c 2020-01-29 tracey goto done;
3341 bcbc97d8 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tree_row);
3342 ec46ccd7 2020-01-15 tracey if (error)
3343 ec46ccd7 2020-01-15 tracey goto done;
3345 bcbc97d8 2020-01-15 tracey free(id_str);
3346 84bf4df2 2020-02-03 stsp id_str = NULL;
3347 ec46ccd7 2020-01-15 tracey free(url_html);
3348 84bf4df2 2020-02-03 stsp url_html = NULL;
3349 ec46ccd7 2020-01-15 tracey free(tree_row);
3350 84bf4df2 2020-02-03 stsp tree_row = NULL;
3351 84bf4df2 2020-02-03 stsp free(build_folder);
3352 84bf4df2 2020-02-03 stsp build_folder = NULL;
3355 bcbc97d8 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
3356 bcbc97d8 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
3357 84bf4df2 2020-02-03 stsp if (error)
3358 84bf4df2 2020-02-03 stsp goto done;
3359 84bf4df2 2020-02-03 stsp *tree_html = strdup(buf_get(diffbuf));
3360 84bf4df2 2020-02-03 stsp if (*tree_html == NULL) {
3361 84bf4df2 2020-02-03 stsp error = got_error_from_errno("strdup");
3362 84bf4df2 2020-02-03 stsp goto done;
3366 bcbc97d8 2020-01-15 tracey if (tree)
3367 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
3368 2e676fc5 2020-01-15 tracey if (repo)
3369 2e676fc5 2020-01-15 tracey got_repo_close(repo);
3371 84bf4df2 2020-02-03 stsp free(id_str);
3372 84bf4df2 2020-02-03 stsp free(url_html);
3373 84bf4df2 2020-02-03 stsp free(tree_row);
3374 2e676fc5 2020-01-15 tracey free(in_repo_path);
3375 bcbc97d8 2020-01-15 tracey free(tree_id);
3376 bcbc97d8 2020-01-15 tracey free(diffbuf);
3377 84bf4df2 2020-02-03 stsp free(build_folder);
3378 84bf4df2 2020-02-03 stsp return error;
3381 51d4a92d 2020-02-03 tracey static const struct got_error *
3382 51d4a92d 2020-02-03 tracey gw_get_repo_heads(char **head_html, struct gw_trans *gw_trans)
3384 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
3385 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
3386 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
3387 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
3388 51d4a92d 2020-02-03 tracey char *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
3389 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
3390 87f9ebf5 2020-01-15 tracey size_t newsize;
3392 51d4a92d 2020-02-03 tracey *head_html = NULL;
3394 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
3396 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
3397 ec46ccd7 2020-01-15 tracey if (error)
3398 6c6c85af 2020-01-15 tracey return NULL;
3400 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
3401 ec46ccd7 2020-01-15 tracey if (error)
3402 87f9ebf5 2020-01-15 tracey goto done;
3404 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
3406 87f9ebf5 2020-01-15 tracey if (error)
3407 87f9ebf5 2020-01-15 tracey goto done;
3409 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
3410 87f9ebf5 2020-01-15 tracey char *refname;
3412 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
3413 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
3414 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
3415 87f9ebf5 2020-01-15 tracey goto done;
3418 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
3419 87f9ebf5 2020-01-15 tracey free(refname);
3420 87f9ebf5 2020-01-15 tracey continue;
3423 017d6da3 2020-01-29 tracey error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
3424 017d6da3 2020-01-29 tracey refname, TM_DIFF);
3425 d126c1b7 2020-01-29 stsp if (error)
3426 d126c1b7 2020-01-29 stsp goto done;
3428 88d59c36 2020-01-29 stsp if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
3429 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
3430 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
3431 88d59c36 2020-01-29 stsp refname) == -1) {
3432 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
3433 87f9ebf5 2020-01-15 tracey goto done;
3436 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
3437 87f9ebf5 2020-01-15 tracey refname += 11;
3439 88d59c36 2020-01-29 stsp if (asprintf(&head_row, heads_row, age, refname,
3440 88d59c36 2020-01-29 stsp head_navs_disp) == -1) {
3441 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
3442 87f9ebf5 2020-01-15 tracey goto done;
3445 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
3447 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
3448 87f9ebf5 2020-01-15 tracey free(head_row);
3451 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
3452 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
3453 51d4a92d 2020-02-03 tracey *head_html = strdup(buf_get(diffbuf));
3454 51d4a92d 2020-02-03 tracey if (*head_html == NULL)
3455 51d4a92d 2020-02-03 tracey error = got_error_from_errno("strdup");
3458 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
3459 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
3460 87f9ebf5 2020-01-15 tracey if (repo)
3461 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
3462 51d4a92d 2020-02-03 tracey return error;
3465 8d4d2453 2020-01-15 tracey static char *
3466 54415d85 2020-01-15 tracey gw_get_site_link(struct gw_trans *gw_trans)
3468 eb89db64 2020-02-03 stsp char *link = NULL, *repo = NULL, *action = NULL;
3470 eb89db64 2020-02-03 stsp if (gw_trans->repo_name != NULL &&
3471 eb89db64 2020-02-03 stsp asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3472 eb89db64 2020-02-03 stsp gw_trans->repo_name, gw_trans->repo_name) == -1)
3473 eb89db64 2020-02-03 stsp return NULL;
3475 eb89db64 2020-02-03 stsp if (gw_trans->action_name != NULL &&
3476 eb89db64 2020-02-03 stsp asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3477 eb89db64 2020-02-03 stsp free(repo);
3478 eb89db64 2020-02-03 stsp return NULL;
3481 88d59c36 2020-01-29 stsp if (asprintf(&link, site_link, GOTWEB,
3482 eb89db64 2020-02-03 stsp gw_trans->gw_conf->got_site_link,
3483 eb89db64 2020-02-03 stsp repo ? repo : "", action ? action : "") == -1) {
3484 eb89db64 2020-02-03 stsp free(repo);
3485 eb89db64 2020-02-03 stsp free(action);
3486 2c251c14 2020-01-15 tracey return NULL;
3489 eb89db64 2020-02-03 stsp free(repo);
3490 eb89db64 2020-02-03 stsp free(action);
3491 2c251c14 2020-01-15 tracey return link;
3494 83eb9a68 2020-02-03 stsp static const struct got_error *
3495 f7cc9480 2020-02-05 tracey gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
3497 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
3498 f7cc9480 2020-02-05 tracey char *color = NULL;
3499 f7cc9480 2020-02-05 tracey enum kcgi_err kerr = KCGI_OK;
3501 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
3502 ec46ccd7 2020-01-15 tracey color = "diff_minus";
3503 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "+", 1) == 0)
3504 ec46ccd7 2020-01-15 tracey color = "diff_plus";
3505 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
3506 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
3507 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
3508 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
3509 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit +", 8) == 0)
3510 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3511 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit -", 8) == 0)
3512 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3513 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob +", 6) == 0)
3514 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3515 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob -", 6) == 0)
3516 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3517 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file +", 6) == 0)
3518 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3519 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file -", 6) == 0)
3520 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3521 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "from:", 5) == 0)
3522 ec46ccd7 2020-01-15 tracey color = "diff_author";
3523 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "via:", 4) == 0)
3524 ec46ccd7 2020-01-15 tracey color = "diff_author";
3525 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "date:", 5) == 0)
3526 ec46ccd7 2020-01-15 tracey color = "diff_date";
3527 f7cc9480 2020-02-05 tracey kerr = khtml_attr(gw_trans->gw_html_req, KELEM_DIV, KATTR_ID,
3528 f7cc9480 2020-02-05 tracey "diff_line", KATTR_CLASS, color ? color : "", KATTR__MAX);
3529 f7cc9480 2020-02-05 tracey if (error == NULL && kerr != KCGI_OK)
3530 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
3531 83eb9a68 2020-02-03 stsp return error;
3535 070fee27 2020-02-03 stsp * XXX This function should not exist.
3536 070fee27 2020-02-03 stsp * We should let khtml_puts(3) handle HTML escaping.
3538 070fee27 2020-02-03 stsp static const struct got_error *
3539 070fee27 2020-02-03 stsp gw_html_escape(char **escaped_html, const char *orig_html)
3541 070fee27 2020-02-03 stsp const struct got_error *error = NULL;
3542 070fee27 2020-02-03 stsp struct escape_pair {
3544 070fee27 2020-02-03 stsp const char *s;
3545 070fee27 2020-02-03 stsp } esc[] = {
3546 070fee27 2020-02-03 stsp { '>', ">" },
3547 070fee27 2020-02-03 stsp { '<', "<" },
3548 070fee27 2020-02-03 stsp { '&', "&" },
3549 070fee27 2020-02-03 stsp { '"', """ },
3550 070fee27 2020-02-03 stsp { '\'', "'" },
3551 070fee27 2020-02-03 stsp { '\n', "<br />" },
3553 070fee27 2020-02-03 stsp size_t orig_len, len;
3554 070fee27 2020-02-03 stsp int i, j, x;
3556 070fee27 2020-02-03 stsp orig_len = strlen(orig_html);
3557 070fee27 2020-02-03 stsp len = orig_len;
3558 070fee27 2020-02-03 stsp for (i = 0; i < orig_len; i++) {
3559 070fee27 2020-02-03 stsp for (j = 0; j < nitems(esc); j++) {
3560 070fee27 2020-02-03 stsp if (orig_html[i] != esc[j].c)
3562 070fee27 2020-02-03 stsp len += strlen(esc[j].s) - 1 /* escaped char */;
3566 070fee27 2020-02-03 stsp *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3567 070fee27 2020-02-03 stsp if (*escaped_html == NULL)
3568 070fee27 2020-02-03 stsp return got_error_from_errno("calloc");
3571 070fee27 2020-02-03 stsp for (i = 0; i < orig_len; i++) {
3572 070fee27 2020-02-03 stsp int escaped = 0;
3573 070fee27 2020-02-03 stsp for (j = 0; j < nitems(esc); j++) {
3574 070fee27 2020-02-03 stsp if (orig_html[i] != esc[j].c)
3577 070fee27 2020-02-03 stsp if (strlcat(*escaped_html, esc[j].s, len + 1)
3578 070fee27 2020-02-03 stsp >= len + 1) {
3579 070fee27 2020-02-03 stsp error = got_error(GOT_ERR_NO_SPACE);
3580 070fee27 2020-02-03 stsp goto done;
3582 070fee27 2020-02-03 stsp x += strlen(esc[j].s);
3583 070fee27 2020-02-03 stsp escaped = 1;
3586 070fee27 2020-02-03 stsp if (!escaped) {
3587 070fee27 2020-02-03 stsp (*escaped_html)[x] = orig_html[i];
3592 070fee27 2020-02-03 stsp if (error) {
3593 070fee27 2020-02-03 stsp free(*escaped_html);
3594 070fee27 2020-02-03 stsp *escaped_html = NULL;
3596 070fee27 2020-02-03 stsp (*escaped_html)[x] = '\0';
3598 070fee27 2020-02-03 stsp return error;
3602 c08369d7 2020-01-15 tracey main(int argc, char *argv[])
3604 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
3605 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
3606 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
3607 2c251c14 2020-01-15 tracey const char *page = "index";
3608 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
3609 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
3611 54415d85 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3612 2c251c14 2020-01-15 tracey errx(1, "malloc");
3614 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3615 2c251c14 2020-01-15 tracey errx(1, "malloc");
3617 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3618 2c251c14 2020-01-15 tracey errx(1, "malloc");
3620 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3621 2c251c14 2020-01-15 tracey errx(1, "malloc");
3623 c25c2314 2020-01-28 stsp kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3624 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK) {
3625 2447adad 2020-02-06 stsp error = gw_kcgi_error(kerr);
3626 c119608e 2020-01-28 stsp goto done;
3629 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
3630 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
3631 4ceb8155 2020-01-15 tracey gw_malloc = 0;
3632 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
3633 c119608e 2020-01-28 stsp goto done;
3636 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
3637 f2f46662 2020-01-23 tracey TAILQ_INIT(&gw_trans->gw_headers);
3639 2c251c14 2020-01-15 tracey gw_trans->page = 0;
3640 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
3641 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
3642 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
3643 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
3644 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
3645 54415d85 2020-01-15 tracey gw_trans->gw_tmpl->key = gw_templs;
3646 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3647 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
3648 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
3649 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3650 c119608e 2020-01-28 stsp if (error)
3651 2c251c14 2020-01-15 tracey goto done;
3653 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
3654 2c251c14 2020-01-15 tracey if (error)
3655 c119608e 2020-01-28 stsp goto done;
3657 017d6da3 2020-01-29 tracey if (gw_trans->action == GW_BLOB)
3658 017d6da3 2020-01-29 tracey error = gw_blob(gw_trans);
3660 017d6da3 2020-01-29 tracey error = gw_display_index(gw_trans);
3662 c119608e 2020-01-28 stsp if (error) {
3663 c119608e 2020-01-28 stsp gw_trans->mime = KMIME_TEXT_PLAIN;
3664 c119608e 2020-01-28 stsp gw_trans->action = GW_ERR;
3665 6d8d8a26 2020-01-29 stsp gw_display_error(gw_trans, error);
3667 2c251c14 2020-01-15 tracey if (gw_malloc) {
3668 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
3669 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
3670 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
3671 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
3672 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
3673 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
3674 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
3675 2c251c14 2020-01-15 tracey free(gw_trans->commit);
3676 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
3677 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
3678 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
3679 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
3680 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
3682 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3683 2c251c14 2020-01-15 tracey free(dir->name);
3684 2c251c14 2020-01-15 tracey free(dir->description);
3685 2c251c14 2020-01-15 tracey free(dir->age);
3686 2c251c14 2020-01-15 tracey free(dir->url);
3687 2c251c14 2020-01-15 tracey free(dir->path);
3688 2c251c14 2020-01-15 tracey free(dir);
3693 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);