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 2c251c14 2020-01-15 tracey #include <dirent.h>
23 2c251c14 2020-01-15 tracey #include <err.h>
24 c25c2314 2020-01-28 stsp #include <errno.h>
25 474370cb 2020-01-15 tracey #include <regex.h>
26 2c251c14 2020-01-15 tracey #include <stdarg.h>
27 2c251c14 2020-01-15 tracey #include <stdbool.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 f2f46662 2020-01-23 tracey char *author;
91 f2f46662 2020-01-23 tracey 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 54415d85 2020-01-15 tracey static char *gw_get_repo_description(struct gw_trans *,
163 54415d85 2020-01-15 tracey static char *gw_get_repo_owner(struct gw_trans *,
165 474370cb 2020-01-15 tracey static char *gw_get_time_str(time_t, int);
166 54415d85 2020-01-15 tracey static char *gw_get_repo_age(struct gw_trans *,
167 387a29ba 2020-01-15 tracey char *, char *, int);
168 f2f46662 2020-01-23 tracey static char *gw_get_file_blame(struct gw_trans *);
169 f2f46662 2020-01-23 tracey static char *gw_get_repo_tree(struct gw_trans *);
170 f2f46662 2020-01-23 tracey static char *gw_get_diff(struct gw_trans *,
171 f2f46662 2020-01-23 tracey struct gw_header *);
172 54415d85 2020-01-15 tracey static char *gw_get_repo_tags(struct gw_trans *, int, int);
173 54415d85 2020-01-15 tracey static char *gw_get_repo_heads(struct gw_trans *);
174 54415d85 2020-01-15 tracey static char *gw_get_clone_url(struct gw_trans *, char *);
175 54415d85 2020-01-15 tracey static char *gw_get_got_link(struct gw_trans *);
176 54415d85 2020-01-15 tracey static char *gw_get_site_link(struct gw_trans *);
177 2c251c14 2020-01-15 tracey static char *gw_html_escape(const char *);
178 d0ea9c5b 2020-01-15 tracey static char *gw_colordiff_line(char *);
180 f2f46662 2020-01-23 tracey static char *gw_gen_commit_header(char *, char*);
181 f2f46662 2020-01-23 tracey static char *gw_gen_diff_header(char *, char*);
182 f2f46662 2020-01-23 tracey static char *gw_gen_author_header(char *);
183 f2f46662 2020-01-23 tracey static char *gw_gen_committer_header(char *);
184 f2f46662 2020-01-23 tracey static char *gw_gen_age_header(char *);
185 f2f46662 2020-01-23 tracey static char *gw_gen_commit_msg_header(char *);
186 f2f46662 2020-01-23 tracey static char *gw_gen_tree_header(char *);
188 f2f46662 2020-01-23 tracey static void gw_free_headers(struct gw_header *);
189 c25c2314 2020-01-28 stsp static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
190 2c251c14 2020-01-15 tracey enum kmime);
191 c25c2314 2020-01-28 stsp static const struct got_error* gw_display_index(struct gw_trans *,
192 2c251c14 2020-01-15 tracey const struct got_error *);
194 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
196 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_header(struct gw_trans *,
197 f2f46662 2020-01-23 tracey struct gw_header *, int);
198 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commits(struct gw_trans *,
199 f2f46662 2020-01-23 tracey struct gw_header *, int);
200 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commit(struct gw_trans *,
201 f2f46662 2020-01-23 tracey struct gw_header *);
202 54415d85 2020-01-15 tracey static const struct got_error* gw_apply_unveil(const char *, const char *);
203 147269d5 2020-01-15 tracey static const struct got_error* gw_blame_cb(void *, int, int,
204 ec46ccd7 2020-01-15 tracey struct got_object_id *);
205 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct gw_trans *);
206 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct gw_trans *,
207 2c251c14 2020-01-15 tracey struct gw_dir *);
208 54415d85 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct gw_trans *);
210 54415d85 2020-01-15 tracey static const struct got_error* gw_blame(struct gw_trans *);
211 f2f46662 2020-01-23 tracey static const struct got_error* gw_diff(struct gw_trans *);
212 54415d85 2020-01-15 tracey static const struct got_error* gw_index(struct gw_trans *);
213 f2f46662 2020-01-23 tracey static const struct got_error* gw_commits(struct gw_trans *);
214 f2f46662 2020-01-23 tracey static const struct got_error* gw_briefs(struct gw_trans *);
215 54415d85 2020-01-15 tracey static const struct got_error* gw_summary(struct gw_trans *);
216 54415d85 2020-01-15 tracey static const struct got_error* gw_tree(struct gw_trans *);
218 2c251c14 2020-01-15 tracey struct gw_query_action {
219 2c251c14 2020-01-15 tracey unsigned int func_id;
220 2c251c14 2020-01-15 tracey const char *func_name;
221 54415d85 2020-01-15 tracey const struct got_error *(*func_main)(struct gw_trans *);
222 2c251c14 2020-01-15 tracey char *template;
225 2c251c14 2020-01-15 tracey enum gw_query_actions {
226 2c251c14 2020-01-15 tracey GW_BLAME,
227 f2f46662 2020-01-23 tracey GW_BRIEFS,
228 f2f46662 2020-01-23 tracey GW_COMMITS,
231 2c251c14 2020-01-15 tracey GW_INDEX,
232 2c251c14 2020-01-15 tracey GW_SUMMARY,
236 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
237 f2f46662 2020-01-23 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
238 f2f46662 2020-01-23 tracey { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
239 f2f46662 2020-01-23 tracey { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
240 f2f46662 2020-01-23 tracey { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
241 f2f46662 2020-01-23 tracey { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
242 f2f46662 2020-01-23 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
243 f2f46662 2020-01-23 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
244 f2f46662 2020-01-23 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
247 c25c2314 2020-01-28 stsp static const struct got_error *
248 c25c2314 2020-01-28 stsp gw_kcgi_error(enum kcgi_err kerr)
250 c25c2314 2020-01-28 stsp if (kerr == KCGI_OK)
251 c25c2314 2020-01-28 stsp return NULL;
253 c25c2314 2020-01-28 stsp if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
254 c25c2314 2020-01-28 stsp return got_error(GOT_ERR_CANCELLED);
256 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENOMEM)
257 c25c2314 2020-01-28 stsp return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
259 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENFILE)
260 c25c2314 2020-01-28 stsp return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
262 c25c2314 2020-01-28 stsp if (kerr == KCGI_EAGAIN)
263 c25c2314 2020-01-28 stsp return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
265 c25c2314 2020-01-28 stsp if (kerr == KCGI_FORM)
266 c25c2314 2020-01-28 stsp return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
268 c25c2314 2020-01-28 stsp return got_error_from_errno(kcgi_strerror(kerr));
271 2c251c14 2020-01-15 tracey static const struct got_error *
272 54415d85 2020-01-15 tracey gw_apply_unveil(const char *repo_path, const char *repo_file)
274 2c251c14 2020-01-15 tracey const struct got_error *err;
276 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
277 2c251c14 2020-01-15 tracey char *full_path;
278 2c251c14 2020-01-15 tracey if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
279 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
280 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
281 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
284 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
285 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
287 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
288 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
290 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
291 2c251c14 2020-01-15 tracey if (err != NULL)
292 2c251c14 2020-01-15 tracey return err;
294 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
295 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
297 2c251c14 2020-01-15 tracey return NULL;
300 2c251c14 2020-01-15 tracey static const struct got_error *
301 54415d85 2020-01-15 tracey gw_blame(struct gw_trans *gw_trans)
303 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
304 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
305 f2f46662 2020-01-23 tracey char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
306 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
308 6bee13de 2020-01-16 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
309 f2f46662 2020-01-23 tracey NULL) == -1)
310 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
312 f2f46662 2020-01-23 tracey if ((header = gw_init_header()) == NULL)
313 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
315 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
316 ec46ccd7 2020-01-15 tracey if (error)
317 ec46ccd7 2020-01-15 tracey return error;
319 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
320 f2f46662 2020-01-23 tracey if (error)
321 f2f46662 2020-01-23 tracey return error;
323 f2f46662 2020-01-23 tracey blame_html = gw_get_file_blame(gw_trans);
325 6d9fc692 2020-01-28 stsp if (blame_html == NULL) {
326 f2f46662 2020-01-23 tracey blame_html = strdup("");
327 6d9fc692 2020-01-28 stsp if (blame_html == NULL)
328 6d9fc692 2020-01-28 stsp return got_error_from_errno("strdup");
331 f2f46662 2020-01-23 tracey if ((asprintf(&blame_html_disp, blame_header,
332 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
333 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
334 6d9fc692 2020-01-28 stsp blame_html)) == -1) {
335 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
339 6d9fc692 2020-01-28 stsp if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
340 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
344 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, blame);
345 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
346 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
348 f2f46662 2020-01-23 tracey got_ref_list_free(&header->refs);
349 f2f46662 2020-01-23 tracey gw_free_headers(header);
350 f2f46662 2020-01-23 tracey free(blame_html_disp);
351 f2f46662 2020-01-23 tracey free(blame_html);
352 f2f46662 2020-01-23 tracey free(blame);
353 2c251c14 2020-01-15 tracey return error;
356 2c251c14 2020-01-15 tracey static const struct got_error *
357 f2f46662 2020-01-23 tracey gw_diff(struct gw_trans *gw_trans)
359 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
360 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
361 f2f46662 2020-01-23 tracey char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
362 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
364 f2f46662 2020-01-23 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
365 f2f46662 2020-01-23 tracey NULL) == -1)
366 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
368 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
369 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
371 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
372 8087c3c5 2020-01-15 tracey if (error)
373 8087c3c5 2020-01-15 tracey return error;
375 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
376 f2f46662 2020-01-23 tracey if (error)
377 f2f46662 2020-01-23 tracey return error;
379 f2f46662 2020-01-23 tracey diff_html = gw_get_diff(gw_trans, header);
381 f2f46662 2020-01-23 tracey if (diff_html == NULL)
382 f2f46662 2020-01-23 tracey diff_html = strdup("");
384 f2f46662 2020-01-23 tracey if ((asprintf(&diff_html_disp, diff_header,
385 2ac037ec 2020-01-24 tracey gw_gen_diff_header(header->parent_id, header->commit_id),
386 f2f46662 2020-01-23 tracey gw_gen_commit_header(header->commit_id, header->refs_str),
387 f2f46662 2020-01-23 tracey gw_gen_tree_header(header->tree_id),
388 f2f46662 2020-01-23 tracey gw_gen_author_header(header->author),
389 f2f46662 2020-01-23 tracey gw_gen_committer_header(header->committer),
390 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
391 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
392 f2f46662 2020-01-23 tracey diff_html)) == -1)
393 f2f46662 2020-01-23 tracey return got_error_from_errno("asprintf");
395 f2f46662 2020-01-23 tracey if ((asprintf(&diff, diff_wrapper, diff_html_disp)) == -1)
396 f2f46662 2020-01-23 tracey return got_error_from_errno("asprintf");
398 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, diff);
399 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
400 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
401 f2f46662 2020-01-23 tracey got_ref_list_free(&header->refs);
402 f2f46662 2020-01-23 tracey gw_free_headers(header);
403 f2f46662 2020-01-23 tracey free(diff_html_disp);
404 f2f46662 2020-01-23 tracey free(diff_html);
405 f2f46662 2020-01-23 tracey free(diff);
406 2204c934 2020-01-15 tracey return error;
409 2204c934 2020-01-15 tracey static const struct got_error *
410 54415d85 2020-01-15 tracey gw_index(struct gw_trans *gw_trans)
412 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
413 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
414 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
415 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
416 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
418 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
419 6bee13de 2020-01-16 tracey NULL) == -1) {
420 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
421 6bee13de 2020-01-16 tracey return error;
424 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
425 46b9c89b 2020-01-15 tracey if (error)
426 46b9c89b 2020-01-15 tracey return error;
428 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
429 46b9c89b 2020-01-15 tracey if (error)
430 2c251c14 2020-01-15 tracey return error;
432 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
433 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
434 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
436 bce5dac1 2020-01-28 stsp if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
437 bce5dac1 2020-01-28 stsp if (asprintf(&html, index_projects_empty,
438 bce5dac1 2020-01-28 stsp gw_trans->gw_conf->got_repos_path) == -1)
439 bce5dac1 2020-01-28 stsp return got_error_from_errno("asprintf");
440 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
441 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
442 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
443 bce5dac1 2020-01-28 stsp free(html);
444 c25c2314 2020-01-28 stsp return error;
447 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
450 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
451 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
452 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
453 2c251c14 2020-01-15 tracey prev_disp++;
454 2c251c14 2020-01-15 tracey continue;
457 2c251c14 2020-01-15 tracey prev_disp++;
459 f2f46662 2020-01-23 tracey if (error)
460 f2f46662 2020-01-23 tracey return error;
461 46b9c89b 2020-01-15 tracey if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
462 46b9c89b 2020-01-15 tracey gw_dir->name, gw_dir->name)) == -1)
463 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
465 46b9c89b 2020-01-15 tracey if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
466 46b9c89b 2020-01-15 tracey gw_dir->description, gw_dir->owner, gw_dir->age,
467 46b9c89b 2020-01-15 tracey navs)) == -1)
468 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
470 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
471 2c251c14 2020-01-15 tracey free(navs);
472 2c251c14 2020-01-15 tracey free(html);
473 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
474 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
476 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
477 2c251c14 2020-01-15 tracey continue;
479 c25c2314 2020-01-28 stsp if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
480 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
481 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
482 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
483 c25c2314 2020-01-28 stsp } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
484 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
485 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
486 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
487 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
488 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
489 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
492 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
493 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
494 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
495 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
496 2c251c14 2020-01-15 tracey if ((asprintf(&prev, nav_prev,
497 2c251c14 2020-01-15 tracey gw_trans->page - 1)) == -1)
498 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
499 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, prev);
500 2c251c14 2020-01-15 tracey free(prev);
501 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
502 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
505 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
506 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
507 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
509 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
510 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
511 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
512 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
513 2c251c14 2020-01-15 tracey if ((asprintf(&next, nav_next,
514 2c251c14 2020-01-15 tracey gw_trans->page + 1)) == -1)
515 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
516 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, next);
517 2c251c14 2020-01-15 tracey free(next);
518 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
519 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
520 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
521 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
522 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
523 2c251c14 2020-01-15 tracey next_disp = 0;
527 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
528 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
529 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
530 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
531 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
532 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
533 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
536 2c251c14 2020-01-15 tracey next_disp++;
538 2c251c14 2020-01-15 tracey return error;
541 2c251c14 2020-01-15 tracey static const struct got_error *
542 f2f46662 2020-01-23 tracey gw_commits(struct gw_trans *gw_trans)
544 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
545 f2f46662 2020-01-23 tracey char *commits_html, *commits_navs_html;
546 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
547 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
549 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
550 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
552 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
553 6bee13de 2020-01-16 tracey NULL) == -1) {
554 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
555 6bee13de 2020-01-16 tracey return error;
558 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
559 8087c3c5 2020-01-15 tracey if (error)
560 8087c3c5 2020-01-15 tracey return error;
562 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
563 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
565 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
566 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
567 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
568 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
569 f2f46662 2020-01-23 tracey if ((asprintf(&commits_navs_html, commits_navs,
570 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
571 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
572 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id)) == -1)
573 4ceb8155 2020-01-15 tracey return got_error_from_errno("asprintf");
575 f2f46662 2020-01-23 tracey if ((asprintf(&commits_html, commits_line,
576 f2f46662 2020-01-23 tracey gw_gen_commit_header(n_header->commit_id,
577 f2f46662 2020-01-23 tracey n_header->refs_str),
578 f2f46662 2020-01-23 tracey gw_gen_author_header(n_header->author),
579 f2f46662 2020-01-23 tracey gw_gen_committer_header(n_header->committer),
580 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(n_header->committer_time,
581 f2f46662 2020-01-23 tracey TM_LONG)), gw_html_escape(n_header->commit_msg),
582 f2f46662 2020-01-23 tracey commits_navs_html)) == -1)
583 f2f46662 2020-01-23 tracey return got_error_from_errno("asprintf");
584 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, commits_html);
585 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
586 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
588 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
589 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
590 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
592 ae36ed87 2020-01-28 stsp if (header)
593 ae36ed87 2020-01-28 stsp got_ref_list_free(&header->refs);
594 f2f46662 2020-01-23 tracey gw_free_headers(header);
595 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
596 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
597 2c251c14 2020-01-15 tracey return error;
600 2c251c14 2020-01-15 tracey static const struct got_error *
601 f2f46662 2020-01-23 tracey gw_briefs(struct gw_trans *gw_trans)
603 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
604 f2f46662 2020-01-23 tracey char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
605 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
606 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
608 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
609 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
611 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
612 6bee13de 2020-01-16 tracey NULL) == -1) {
613 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
614 6bee13de 2020-01-16 tracey return error;
617 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
618 8087c3c5 2020-01-15 tracey if (error)
619 8087c3c5 2020-01-15 tracey return error;
621 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_SUMMARY)
622 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
624 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
625 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
628 c25c2314 2020-01-28 stsp return error;
630 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
631 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
632 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
634 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
635 f2f46662 2020-01-23 tracey if ((asprintf(&briefs_navs_html, briefs_navs,
636 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
637 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
638 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id)) == -1)
639 9d84e7dd 2020-01-15 tracey return got_error_from_errno("asprintf");
640 f2f46662 2020-01-23 tracey newline = strchr(n_header->commit_msg, '\n');
641 f2f46662 2020-01-23 tracey if (newline)
642 f2f46662 2020-01-23 tracey *newline = '\0';
643 f2f46662 2020-01-23 tracey if ((asprintf(&briefs_html, briefs_line,
644 f2f46662 2020-01-23 tracey gw_get_time_str(n_header->committer_time, TM_DIFF),
645 f2f46662 2020-01-23 tracey n_header->author, gw_html_escape(n_header->commit_msg),
646 f2f46662 2020-01-23 tracey briefs_navs_html)) == -1)
647 f2f46662 2020-01-23 tracey return got_error_from_errno("asprintf");
648 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, briefs_html);
649 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
650 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
652 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
653 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
654 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
656 ae36ed87 2020-01-28 stsp if (header)
657 ae36ed87 2020-01-28 stsp got_ref_list_free(&header->refs);
658 f2f46662 2020-01-23 tracey gw_free_headers(header);
659 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
660 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
661 2c251c14 2020-01-15 tracey return error;
664 2c251c14 2020-01-15 tracey static const struct got_error *
665 54415d85 2020-01-15 tracey gw_summary(struct gw_trans *gw_trans)
667 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
668 46b9c89b 2020-01-15 tracey char *description_html, *repo_owner_html, *repo_age_html,
669 f2f46662 2020-01-23 tracey *cloneurl_html, *tags, *heads, *tags_html,
670 8087c3c5 2020-01-15 tracey *heads_html, *age;
671 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
673 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
674 6bee13de 2020-01-16 tracey NULL) == -1) {
675 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
676 6bee13de 2020-01-16 tracey return error;
679 f2f46662 2020-01-23 tracey /* unveil is applied with gw_briefs below */
681 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
682 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
683 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
685 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
686 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
687 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
688 46b9c89b 2020-01-15 tracey if ((asprintf(&description_html, description,
689 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->description)) == -1)
690 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
692 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, description_html);
693 46b9c89b 2020-01-15 tracey free(description_html);
694 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
695 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
699 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
700 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->owner != NULL &&
701 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
702 46b9c89b 2020-01-15 tracey if ((asprintf(&repo_owner_html, repo_owner,
703 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->owner)) == -1)
704 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
706 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
707 46b9c89b 2020-01-15 tracey free(repo_owner_html);
708 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
709 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
713 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
714 c6b62706 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
715 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
716 c6b62706 2020-01-15 tracey if (age != NULL && (strcmp(age, "") != 0)) {
717 c6b62706 2020-01-15 tracey if ((asprintf(&repo_age_html, last_change, age)) == -1)
718 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
720 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
721 46b9c89b 2020-01-15 tracey free(repo_age_html);
722 c6b62706 2020-01-15 tracey free(age);
723 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
724 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
728 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
729 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
730 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
731 46b9c89b 2020-01-15 tracey if ((asprintf(&cloneurl_html, cloneurl,
732 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->url)) == -1)
733 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
735 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
736 46b9c89b 2020-01-15 tracey free(cloneurl_html);
737 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
738 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
741 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
742 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
743 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
745 f2f46662 2020-01-23 tracey error = gw_briefs(gw_trans);
746 f2f46662 2020-01-23 tracey if (error)
747 f2f46662 2020-01-23 tracey return error;
749 87f9ebf5 2020-01-15 tracey tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
750 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
752 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
753 8d4d2453 2020-01-15 tracey if ((asprintf(&tags_html, summary_tags,
754 8d4d2453 2020-01-15 tracey tags)) == -1)
755 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
756 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tags_html);
757 8d4d2453 2020-01-15 tracey free(tags_html);
758 8d4d2453 2020-01-15 tracey free(tags);
759 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
760 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
763 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
764 8d4d2453 2020-01-15 tracey if ((asprintf(&heads_html, summary_heads,
765 8d4d2453 2020-01-15 tracey heads)) == -1)
766 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
767 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, heads_html);
768 8d4d2453 2020-01-15 tracey free(heads_html);
769 8d4d2453 2020-01-15 tracey free(heads);
770 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
771 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
773 2204c934 2020-01-15 tracey return error;
776 2204c934 2020-01-15 tracey static const struct got_error *
777 f2f46662 2020-01-23 tracey gw_tree(struct gw_trans *gw_trans)
779 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
780 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
781 f2f46662 2020-01-23 tracey char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
782 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
784 f2f46662 2020-01-23 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
785 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
787 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
788 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
790 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
791 b772de24 2020-01-15 tracey if (error)
792 b772de24 2020-01-15 tracey return error;
794 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
795 f2f46662 2020-01-23 tracey if (error)
796 f2f46662 2020-01-23 tracey return error;
798 f2f46662 2020-01-23 tracey tree_html = gw_get_repo_tree(gw_trans);
800 f2f46662 2020-01-23 tracey if (tree_html == NULL)
801 f2f46662 2020-01-23 tracey tree_html = strdup("");
803 f2f46662 2020-01-23 tracey if ((asprintf(&tree_html_disp, tree_header,
804 f2f46662 2020-01-23 tracey gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
805 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
806 f2f46662 2020-01-23 tracey tree_html)) == -1)
807 f2f46662 2020-01-23 tracey return got_error_from_errno("asprintf");
809 f2f46662 2020-01-23 tracey if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
810 f2f46662 2020-01-23 tracey return got_error_from_errno("asprintf");
812 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tree);
813 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
814 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
815 ae36ed87 2020-01-28 stsp if (header)
816 ae36ed87 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 free(tree_html_disp);
819 f2f46662 2020-01-23 tracey free(tree_html);
820 f2f46662 2020-01-23 tracey free(tree);
821 2c251c14 2020-01-15 tracey return error;
824 2c251c14 2020-01-15 tracey static const struct got_error *
825 54415d85 2020-01-15 tracey gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
827 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
829 2c251c14 2020-01-15 tracey char *dir_test;
830 4ceb8155 2020-01-15 tracey int opened = 0;
832 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
833 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
834 2c251c14 2020-01-15 tracey GOTWEB_GIT_DIR)) == -1)
835 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
837 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
838 2c251c14 2020-01-15 tracey if (dt == NULL) {
839 2c251c14 2020-01-15 tracey free(dir_test);
841 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
842 4ceb8155 2020-01-15 tracey opened = 1;
843 2c251c14 2020-01-15 tracey goto done;
846 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
847 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
848 2c251c14 2020-01-15 tracey GOTWEB_GOT_DIR)) == -1)
849 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
851 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
852 2c251c14 2020-01-15 tracey if (dt == NULL)
853 2c251c14 2020-01-15 tracey free(dir_test);
855 4ceb8155 2020-01-15 tracey opened = 1;
856 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
857 2c251c14 2020-01-15 tracey goto errored;
860 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s",
861 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
862 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
864 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
867 2c251c14 2020-01-15 tracey gw_dir->description = gw_get_repo_description(gw_trans,
868 2c251c14 2020-01-15 tracey gw_dir->path);
869 2c251c14 2020-01-15 tracey gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
870 387a29ba 2020-01-15 tracey gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
871 387a29ba 2020-01-15 tracey TM_DIFF);
872 2c251c14 2020-01-15 tracey gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
875 2c251c14 2020-01-15 tracey free(dir_test);
876 2c251c14 2020-01-15 tracey if (opened)
877 2c251c14 2020-01-15 tracey closedir(dt);
878 2c251c14 2020-01-15 tracey return error;
881 2c251c14 2020-01-15 tracey static const struct got_error *
882 54415d85 2020-01-15 tracey gw_load_got_paths(struct gw_trans *gw_trans)
884 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
886 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
887 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
888 2c251c14 2020-01-15 tracey struct stat st;
889 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
891 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
892 2c251c14 2020-01-15 tracey if (d == NULL) {
893 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
894 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
895 2c251c14 2020-01-15 tracey return error;
898 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
899 2c251c14 2020-01-15 tracey alphasort);
900 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
901 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
902 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
903 2c251c14 2020-01-15 tracey return error;
906 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
907 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
908 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
909 2c251c14 2020-01-15 tracey break; /* account for parent and self */
911 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
912 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
913 2c251c14 2020-01-15 tracey continue;
915 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
916 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
918 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
919 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
920 2c251c14 2020-01-15 tracey continue;
921 2c251c14 2020-01-15 tracey else if (error)
922 2c251c14 2020-01-15 tracey return error;
924 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
925 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
926 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
928 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
932 2c251c14 2020-01-15 tracey closedir(d);
933 2c251c14 2020-01-15 tracey return error;
936 2c251c14 2020-01-15 tracey static const struct got_error *
937 54415d85 2020-01-15 tracey gw_parse_querystring(struct gw_trans *gw_trans)
939 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
940 2c251c14 2020-01-15 tracey struct kpair *p;
941 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
942 2c251c14 2020-01-15 tracey unsigned int i;
944 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
945 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
946 2c251c14 2020-01-15 tracey return error;
947 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
948 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
949 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
950 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
952 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_path, "%s/%s",
953 387a29ba 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
954 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
956 2c251c14 2020-01-15 tracey /* get action and set function */
957 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
958 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
959 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
960 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
961 2c251c14 2020-01-15 tracey continue;
963 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
964 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
965 2c251c14 2020-01-15 tracey gw_trans->action = i;
966 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->action_name,
967 2c251c14 2020-01-15 tracey "%s", action->func_name)) == -1)
969 b772de24 2020-01-15 tracey got_error_from_errno(
970 2c251c14 2020-01-15 tracey "asprintf");
975 2c251c14 2020-01-15 tracey action = NULL;
978 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
979 ec46ccd7 2020-01-15 tracey if ((asprintf(&gw_trans->commit, "%s",
980 ec46ccd7 2020-01-15 tracey p->parsed.s)) == -1)
981 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
983 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
984 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_file, "%s",
985 8087c3c5 2020-01-15 tracey p->parsed.s)) == -1)
986 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
988 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
989 ec46ccd7 2020-01-15 tracey if ((asprintf(&gw_trans->repo_folder, "%s",
990 ec46ccd7 2020-01-15 tracey p->parsed.s)) == -1)
991 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
993 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
994 8087c3c5 2020-01-15 tracey if ((asprintf(&gw_trans->headref, "%s",
995 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
996 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
998 2c251c14 2020-01-15 tracey if (action == NULL) {
999 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
1000 2c251c14 2020-01-15 tracey return error;
1002 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
1003 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1004 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1006 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1007 46b9c89b 2020-01-15 tracey if (error)
1008 46b9c89b 2020-01-15 tracey return error;
1010 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1012 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1013 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1015 f2f46662 2020-01-23 tracey /* if (gw_trans->action == GW_RAW) */
1016 f2f46662 2020-01-23 tracey /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1018 2c251c14 2020-01-15 tracey return error;
1021 2c251c14 2020-01-15 tracey static struct gw_dir *
1022 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
1024 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1026 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1027 2c251c14 2020-01-15 tracey return NULL;
1029 2c251c14 2020-01-15 tracey if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1030 2c251c14 2020-01-15 tracey return NULL;
1032 2c251c14 2020-01-15 tracey return gw_dir;
1035 c25c2314 2020-01-28 stsp static const struct got_error *
1036 54415d85 2020-01-15 tracey gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1038 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1040 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1041 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1042 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1043 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1044 2c251c14 2020-01-15 tracey khttps[code]);
1045 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1046 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1047 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1048 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1049 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1050 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1051 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1052 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1053 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1054 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1055 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1056 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1057 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1058 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1059 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1061 c25c2314 2020-01-28 stsp kerr = khttp_body(gw_trans->gw_req);
1062 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1065 c25c2314 2020-01-28 stsp static const struct got_error *
1066 54415d85 2020-01-15 tracey gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1068 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1070 2c251c14 2020-01-15 tracey gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1071 c25c2314 2020-01-28 stsp kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1073 2c251c14 2020-01-15 tracey if (err)
1074 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, err->msg);
1076 c25c2314 2020-01-28 stsp kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1077 2c251c14 2020-01-15 tracey gw_query_funcs[gw_trans->action].template);
1078 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1079 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1081 c25c2314 2020-01-28 stsp kerr = khtml_close(gw_trans->gw_html_req);
1082 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1085 2c251c14 2020-01-15 tracey static int
1086 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1088 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1089 54415d85 2020-01-15 tracey struct gw_trans *gw_trans = arg;
1090 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
1091 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
1093 2c251c14 2020-01-15 tracey switch (key) {
1094 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1095 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
1097 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1098 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
1099 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
1100 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
1102 2c251c14 2020-01-15 tracey free(gw_got_link);
1104 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1105 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
1106 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
1107 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
1109 2c251c14 2020-01-15 tracey free(gw_site_link);
1111 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1112 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
1113 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
1114 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1117 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1118 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
1120 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
1121 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
1122 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
1123 2c251c14 2020-01-15 tracey site_owner_name =
1124 2c251c14 2020-01-15 tracey gw_html_escape(gw_trans->gw_conf->got_site_owner);
1125 2c251c14 2020-01-15 tracey if ((asprintf(&site_owner_name_h, site_owner,
1126 2c251c14 2020-01-15 tracey site_owner_name))
1128 2c251c14 2020-01-15 tracey return 0;
1130 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
1131 2c251c14 2020-01-15 tracey free(site_owner_name);
1132 2c251c14 2020-01-15 tracey free(site_owner_name_h);
1135 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1136 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1137 2c251c14 2020-01-15 tracey if (error)
1138 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1140 2c251c14 2020-01-15 tracey default:
1141 2c251c14 2020-01-15 tracey return 0;
1143 2c251c14 2020-01-15 tracey return 1;
1146 2c251c14 2020-01-15 tracey static char *
1147 f2f46662 2020-01-23 tracey gw_gen_commit_header(char *str1, char *str2)
1149 f2f46662 2020-01-23 tracey char *return_html = NULL, *ref_str = NULL;
1151 f2f46662 2020-01-23 tracey if (strcmp(str2, "") != 0) {
1152 f2f46662 2020-01-23 tracey if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1153 f2f46662 2020-01-23 tracey return_html = strdup("");
1154 f2f46662 2020-01-23 tracey return return_html;
1157 f2f46662 2020-01-23 tracey ref_str = strdup("");
1160 f2f46662 2020-01-23 tracey if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1161 f2f46662 2020-01-23 tracey return_html = strdup("");
1163 f2f46662 2020-01-23 tracey free(ref_str);
1164 f2f46662 2020-01-23 tracey return return_html;
1167 f2f46662 2020-01-23 tracey static char *
1168 f2f46662 2020-01-23 tracey gw_gen_diff_header(char *str1, char *str2)
1170 f2f46662 2020-01-23 tracey char *return_html = NULL;
1172 f2f46662 2020-01-23 tracey if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1173 f2f46662 2020-01-23 tracey return_html = strdup("");
1175 f2f46662 2020-01-23 tracey return return_html;
1178 f2f46662 2020-01-23 tracey static char *
1179 f2f46662 2020-01-23 tracey gw_gen_author_header(char *str)
1181 f2f46662 2020-01-23 tracey char *return_html = NULL;
1183 f2f46662 2020-01-23 tracey if ((asprintf(&return_html, header_author_html, str)) == -1)
1184 f2f46662 2020-01-23 tracey return_html = strdup("");
1186 f2f46662 2020-01-23 tracey return return_html;
1189 f2f46662 2020-01-23 tracey static char *
1190 f2f46662 2020-01-23 tracey gw_gen_committer_header(char *str)
1192 f2f46662 2020-01-23 tracey char *return_html = NULL;
1194 f2f46662 2020-01-23 tracey if ((asprintf(&return_html, header_committer_html, str)) == -1)
1195 f2f46662 2020-01-23 tracey return_html = strdup("");
1197 f2f46662 2020-01-23 tracey return return_html;
1200 f2f46662 2020-01-23 tracey static char *
1201 f2f46662 2020-01-23 tracey gw_gen_age_header(char *str)
1203 f2f46662 2020-01-23 tracey char *return_html = NULL;
1205 f2f46662 2020-01-23 tracey if ((asprintf(&return_html, header_age_html, str)) == -1)
1206 f2f46662 2020-01-23 tracey return_html = strdup("");
1208 f2f46662 2020-01-23 tracey return return_html;
1211 f2f46662 2020-01-23 tracey static char *
1212 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(char *str)
1214 f2f46662 2020-01-23 tracey char *return_html = NULL;
1216 f2f46662 2020-01-23 tracey if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1217 f2f46662 2020-01-23 tracey return_html = strdup("");
1219 f2f46662 2020-01-23 tracey return return_html;
1222 f2f46662 2020-01-23 tracey static char *
1223 f2f46662 2020-01-23 tracey gw_gen_tree_header(char *str)
1225 f2f46662 2020-01-23 tracey char *return_html = NULL;
1227 f2f46662 2020-01-23 tracey if ((asprintf(&return_html, header_tree_html, str)) == -1)
1228 f2f46662 2020-01-23 tracey return_html = strdup("");
1230 f2f46662 2020-01-23 tracey return return_html;
1233 f2f46662 2020-01-23 tracey static char *
1234 54415d85 2020-01-15 tracey gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1236 2c251c14 2020-01-15 tracey FILE *f;
1237 2c251c14 2020-01-15 tracey char *description = NULL, *d_file = NULL;
1238 2c251c14 2020-01-15 tracey unsigned int len;
1240 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description == false)
1241 2c251c14 2020-01-15 tracey goto err;
1243 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/description", dir)) == -1)
1244 2c251c14 2020-01-15 tracey goto err;
1246 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1247 2c251c14 2020-01-15 tracey goto err;
1249 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1250 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1251 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1252 2c251c14 2020-01-15 tracey if ((description = calloc(len, sizeof(char *))) == NULL)
1253 2c251c14 2020-01-15 tracey goto err;
1255 2c251c14 2020-01-15 tracey fread(description, 1, len, f);
1256 2c251c14 2020-01-15 tracey fclose(f);
1257 2c251c14 2020-01-15 tracey free(d_file);
1258 2c251c14 2020-01-15 tracey return description;
1260 2c251c14 2020-01-15 tracey if ((asprintf(&description, "%s", "")) == -1)
1261 2c251c14 2020-01-15 tracey return NULL;
1263 2c251c14 2020-01-15 tracey return description;
1266 2c251c14 2020-01-15 tracey static char *
1267 474370cb 2020-01-15 tracey gw_get_time_str(time_t committer_time, int ref_tm)
1269 474370cb 2020-01-15 tracey struct tm tm;
1270 474370cb 2020-01-15 tracey time_t diff_time;
1271 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
1272 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1273 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
1274 474370cb 2020-01-15 tracey char *now = "right now";
1275 474370cb 2020-01-15 tracey char *repo_age, *s;
1276 6c6c85af 2020-01-15 tracey char datebuf[29];
1278 474370cb 2020-01-15 tracey switch (ref_tm) {
1279 474370cb 2020-01-15 tracey case TM_DIFF:
1280 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
1281 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1282 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1283 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1284 474370cb 2020-01-15 tracey return NULL;
1285 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1286 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1287 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1288 474370cb 2020-01-15 tracey months)) == -1)
1289 474370cb 2020-01-15 tracey return NULL;
1290 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1291 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1292 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1293 474370cb 2020-01-15 tracey return NULL;
1294 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1295 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1296 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24), days)) == -1)
1297 474370cb 2020-01-15 tracey return NULL;
1298 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
1299 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1300 474370cb 2020-01-15 tracey (diff_time / 60 / 60), hours)) == -1)
1301 474370cb 2020-01-15 tracey return NULL;
1302 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
1303 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1304 474370cb 2020-01-15 tracey minutes)) == -1)
1305 474370cb 2020-01-15 tracey return NULL;
1306 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
1307 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", diff_time,
1308 474370cb 2020-01-15 tracey seconds)) == -1)
1309 474370cb 2020-01-15 tracey return NULL;
1310 474370cb 2020-01-15 tracey } else {
1311 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s", now)) == -1)
1312 474370cb 2020-01-15 tracey return NULL;
1315 474370cb 2020-01-15 tracey case TM_LONG:
1316 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1317 474370cb 2020-01-15 tracey return NULL;
1319 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
1320 474370cb 2020-01-15 tracey if (s == NULL)
1321 474370cb 2020-01-15 tracey return NULL;
1323 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1324 474370cb 2020-01-15 tracey return NULL;
1327 474370cb 2020-01-15 tracey return repo_age;
1330 474370cb 2020-01-15 tracey static char *
1331 d0ea9c5b 2020-01-15 tracey gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1332 d0ea9c5b 2020-01-15 tracey int ref_tm)
1334 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1335 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
1336 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
1337 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
1338 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
1339 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
1340 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
1341 87f9ebf5 2020-01-15 tracey int is_head = 0;
1342 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
1343 87f9ebf5 2020-01-15 tracey const char *refname;
1344 474370cb 2020-01-15 tracey char *repo_age = NULL;
1346 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
1347 387a29ba 2020-01-15 tracey return NULL;
1349 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1350 87f9ebf5 2020-01-15 tracey is_head = 1;
1352 2c251c14 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1353 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age == false) {
1354 8087c3c5 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1355 8087c3c5 2020-01-15 tracey return NULL;
1356 2c251c14 2020-01-15 tracey return repo_age;
1359 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
1360 ec46ccd7 2020-01-15 tracey if (error)
1361 2c251c14 2020-01-15 tracey goto err;
1363 87f9ebf5 2020-01-15 tracey if (is_head)
1364 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
1365 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1367 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
1368 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1369 ec46ccd7 2020-01-15 tracey if (error)
1370 2c251c14 2020-01-15 tracey goto err;
1372 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1373 87f9ebf5 2020-01-15 tracey if (is_head)
1374 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
1376 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1377 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
1378 ec46ccd7 2020-01-15 tracey if (error)
1379 2c251c14 2020-01-15 tracey goto err;
1381 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1382 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
1383 ec46ccd7 2020-01-15 tracey if (error)
1384 2c251c14 2020-01-15 tracey goto err;
1386 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1387 ec46ccd7 2020-01-15 tracey if (error)
1388 2c251c14 2020-01-15 tracey goto err;
1390 2c251c14 2020-01-15 tracey committer_time =
1391 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1393 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
1394 2c251c14 2020-01-15 tracey cmp_time = committer_time;
1397 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1398 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1399 474370cb 2020-01-15 tracey repo_age = gw_get_time_str(committer_time, ref_tm);
1401 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1402 474370cb 2020-01-15 tracey return NULL;
1403 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1404 2c251c14 2020-01-15 tracey free(id);
1405 2c251c14 2020-01-15 tracey return repo_age;
1407 2c251c14 2020-01-15 tracey if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1408 2c251c14 2020-01-15 tracey return NULL;
1410 2c251c14 2020-01-15 tracey return repo_age;
1413 ec46ccd7 2020-01-15 tracey static char *
1414 f2f46662 2020-01-23 tracey gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1416 ec46ccd7 2020-01-15 tracey const struct got_error *error;
1417 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
1418 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1419 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
1420 ec46ccd7 2020-01-15 tracey char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1421 65559f29 2020-01-24 tracey *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1422 05ce9a79 2020-01-24 tracey int obj_type;
1423 ec46ccd7 2020-01-15 tracey size_t newsize;
1425 ec46ccd7 2020-01-15 tracey f = got_opentemp();
1426 ec46ccd7 2020-01-15 tracey if (f == NULL)
1427 ec46ccd7 2020-01-15 tracey return NULL;
1429 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1430 ec46ccd7 2020-01-15 tracey if (error)
1431 ec46ccd7 2020-01-15 tracey return NULL;
1433 90f16cb8 2020-01-24 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1434 ec46ccd7 2020-01-15 tracey if (error)
1435 ec46ccd7 2020-01-15 tracey goto done;
1437 05ce9a79 2020-01-24 tracey if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1438 05ce9a79 2020-01-24 tracey error = got_repo_match_object_id(&id1, &label1,
1439 05ce9a79 2020-01-24 tracey header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1440 05ce9a79 2020-01-24 tracey if (error)
1441 05ce9a79 2020-01-24 tracey goto done;
1444 90f16cb8 2020-01-24 tracey error = got_repo_match_object_id(&id2, &label2,
1445 2ac037ec 2020-01-24 tracey header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1446 90f16cb8 2020-01-24 tracey if (error)
1447 90f16cb8 2020-01-24 tracey goto done;
1449 05ce9a79 2020-01-24 tracey error = got_object_get_type(&obj_type, header->repo, id2);
1450 90f16cb8 2020-01-24 tracey if (error)
1451 90f16cb8 2020-01-24 tracey goto done;
1452 05ce9a79 2020-01-24 tracey switch (obj_type) {
1453 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
1454 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1455 90f16cb8 2020-01-24 tracey header->repo, f);
1457 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
1458 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1459 90f16cb8 2020-01-24 tracey header->repo, f);
1461 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
1462 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_commits(id1, id2, 3, 0,
1463 90f16cb8 2020-01-24 tracey header->repo, f);
1465 ec46ccd7 2020-01-15 tracey default:
1466 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1469 ec46ccd7 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1470 ec46ccd7 2020-01-15 tracey goto done;
1472 ec46ccd7 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1474 bfd30182 2020-01-24 tracey while ((fgets(buf, 2048, f)) != NULL) {
1475 65559f29 2020-01-24 tracey n_buf = buf;
1476 65559f29 2020-01-24 tracey while (*n_buf == '\n')
1477 65559f29 2020-01-24 tracey n_buf++;
1478 65559f29 2020-01-24 tracey newline = strchr(n_buf, '\n');
1479 65559f29 2020-01-24 tracey if (newline)
1480 65559f29 2020-01-24 tracey *newline = ' ';
1482 65559f29 2020-01-24 tracey buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1483 bfd30182 2020-01-24 tracey if (buf_color == NULL)
1484 bfd30182 2020-01-24 tracey continue;
1486 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, buf_color);
1487 ec46ccd7 2020-01-15 tracey if (error)
1488 ec46ccd7 2020-01-15 tracey return NULL;
1490 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_end);
1491 ec46ccd7 2020-01-15 tracey if (error)
1492 ec46ccd7 2020-01-15 tracey return NULL;
1495 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1496 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1497 ec46ccd7 2020-01-15 tracey diff_html = strdup(buf_get(diffbuf));
1500 ec46ccd7 2020-01-15 tracey fclose(f);
1501 ec46ccd7 2020-01-15 tracey free(buf_color);
1502 ec46ccd7 2020-01-15 tracey free(buf);
1503 ec46ccd7 2020-01-15 tracey free(diffbuf);
1504 ec46ccd7 2020-01-15 tracey free(label1);
1505 ec46ccd7 2020-01-15 tracey free(label2);
1506 ec46ccd7 2020-01-15 tracey free(id1);
1507 ec46ccd7 2020-01-15 tracey free(id2);
1509 ec46ccd7 2020-01-15 tracey if (error)
1510 ec46ccd7 2020-01-15 tracey return NULL;
1512 ec46ccd7 2020-01-15 tracey return diff_html;
1515 2c251c14 2020-01-15 tracey static char *
1516 54415d85 2020-01-15 tracey gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1518 2c251c14 2020-01-15 tracey FILE *f;
1519 2c251c14 2020-01-15 tracey char *owner = NULL, *d_file = NULL;
1520 2c251c14 2020-01-15 tracey char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1521 2c251c14 2020-01-15 tracey char *comp, *pos, *buf;
1522 2c251c14 2020-01-15 tracey unsigned int i;
1524 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner == false)
1525 2c251c14 2020-01-15 tracey goto err;
1527 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/config", dir)) == -1)
1528 2c251c14 2020-01-15 tracey goto err;
1530 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1531 2c251c14 2020-01-15 tracey goto err;
1533 6c6c85af 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1534 2c251c14 2020-01-15 tracey goto err;
1536 6c6c85af 2020-01-15 tracey while ((fgets(buf, 128, f)) != NULL) {
1537 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gotweb)) != NULL)
1540 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gitweb)) != NULL)
1544 2c251c14 2020-01-15 tracey if (pos == NULL)
1545 2c251c14 2020-01-15 tracey goto err;
1548 6c6c85af 2020-01-15 tracey fgets(buf, 128, f);
1549 2c251c14 2020-01-15 tracey } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1551 2c251c14 2020-01-15 tracey if (comp == NULL)
1552 2c251c14 2020-01-15 tracey goto err;
1554 2c251c14 2020-01-15 tracey if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1555 2c251c14 2020-01-15 tracey goto err;
1557 2c251c14 2020-01-15 tracey for (i = 0; i < 2; i++) {
1558 2c251c14 2020-01-15 tracey owner = strsep(&buf, "\"");
1561 2c251c14 2020-01-15 tracey if (owner == NULL)
1562 2c251c14 2020-01-15 tracey goto err;
1564 2c251c14 2020-01-15 tracey fclose(f);
1565 2c251c14 2020-01-15 tracey free(d_file);
1566 2c251c14 2020-01-15 tracey return owner;
1568 2c251c14 2020-01-15 tracey if ((asprintf(&owner, "%s", "")) == -1)
1569 2c251c14 2020-01-15 tracey return NULL;
1571 2c251c14 2020-01-15 tracey return owner;
1574 2c251c14 2020-01-15 tracey static char *
1575 54415d85 2020-01-15 tracey gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1577 2c251c14 2020-01-15 tracey FILE *f;
1578 2c251c14 2020-01-15 tracey char *url = NULL, *d_file = NULL;
1579 2c251c14 2020-01-15 tracey unsigned int len;
1581 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1582 2c251c14 2020-01-15 tracey return NULL;
1584 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1585 2c251c14 2020-01-15 tracey return NULL;
1587 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1588 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1589 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1591 2c251c14 2020-01-15 tracey if ((url = calloc(len, sizeof(char *))) == NULL)
1592 2c251c14 2020-01-15 tracey return NULL;
1594 2c251c14 2020-01-15 tracey fread(url, 1, len, f);
1595 2c251c14 2020-01-15 tracey fclose(f);
1596 2c251c14 2020-01-15 tracey free(d_file);
1597 2c251c14 2020-01-15 tracey return url;
1600 8d4d2453 2020-01-15 tracey static char *
1601 54415d85 2020-01-15 tracey gw_get_repo_tags(struct gw_trans *gw_trans, int limit, int tag_type)
1603 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
1604 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
1605 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
1606 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
1607 87f9ebf5 2020-01-15 tracey char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1608 87f9ebf5 2020-01-15 tracey *age = NULL;
1609 87f9ebf5 2020-01-15 tracey char *newline;
1610 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
1611 87f9ebf5 2020-01-15 tracey size_t newsize;
1613 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1614 ec46ccd7 2020-01-15 tracey if (error)
1615 6c6c85af 2020-01-15 tracey return NULL;
1616 6c6c85af 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1618 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1619 ec46ccd7 2020-01-15 tracey if (error)
1620 87f9ebf5 2020-01-15 tracey goto done;
1622 add40c4f 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1623 87f9ebf5 2020-01-15 tracey if (error)
1624 87f9ebf5 2020-01-15 tracey goto done;
1626 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1627 87f9ebf5 2020-01-15 tracey const char *refname;
1628 f2f46662 2020-01-23 tracey char *refstr, *tag_commit0, *tag_commit, *id_str;
1629 87f9ebf5 2020-01-15 tracey time_t tagger_time;
1630 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
1631 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag;
1633 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1634 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
1635 87f9ebf5 2020-01-15 tracey continue;
1636 87f9ebf5 2020-01-15 tracey refname += 10;
1637 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
1638 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
1639 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
1640 87f9ebf5 2020-01-15 tracey goto done;
1643 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
1644 87f9ebf5 2020-01-15 tracey if (error)
1645 87f9ebf5 2020-01-15 tracey goto done;
1646 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
1647 87f9ebf5 2020-01-15 tracey free(id);
1648 87f9ebf5 2020-01-15 tracey if (error)
1649 87f9ebf5 2020-01-15 tracey goto done;
1651 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
1653 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
1654 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
1655 87f9ebf5 2020-01-15 tracey if (error)
1656 87f9ebf5 2020-01-15 tracey goto done;
1658 f2f46662 2020-01-23 tracey tag_commit0 = strdup(got_object_tag_get_message(tag));
1660 f2f46662 2020-01-23 tracey if (tag_commit0 == NULL) {
1661 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
1662 87f9ebf5 2020-01-15 tracey goto done;
1665 f2f46662 2020-01-23 tracey tag_commit = tag_commit0;
1666 f2f46662 2020-01-23 tracey while (*tag_commit == '\n')
1667 f2f46662 2020-01-23 tracey tag_commit++;
1669 87f9ebf5 2020-01-15 tracey switch (tag_type) {
1670 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
1671 f2f46662 2020-01-23 tracey newline = strchr(tag_commit, '\n');
1672 87f9ebf5 2020-01-15 tracey if (newline)
1673 87f9ebf5 2020-01-15 tracey *newline = '\0';
1675 87f9ebf5 2020-01-15 tracey if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1676 87f9ebf5 2020-01-15 tracey TM_DIFF))) == -1) {
1677 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1678 87f9ebf5 2020-01-15 tracey goto done;
1681 87f9ebf5 2020-01-15 tracey if ((asprintf(&tags_navs_disp, tags_navs,
1682 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
1683 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
1684 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str)) == -1) {
1685 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1686 87f9ebf5 2020-01-15 tracey goto done;
1689 f2f46662 2020-01-23 tracey if ((asprintf(&tag_row, tags_row, age, refname,
1690 f2f46662 2020-01-23 tracey tag_commit, tags_navs_disp)) == -1) {
1691 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1692 87f9ebf5 2020-01-15 tracey goto done;
1695 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
1697 87f9ebf5 2020-01-15 tracey case TAGFULL:
1699 87f9ebf5 2020-01-15 tracey default:
1703 6c6c85af 2020-01-15 tracey got_object_tag_close(tag);
1705 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
1707 6c6c85af 2020-01-15 tracey free(id_str);
1708 6c6c85af 2020-01-15 tracey free(refstr);
1709 6c6c85af 2020-01-15 tracey free(age);
1710 f2f46662 2020-01-23 tracey free(tag_commit0);
1711 87f9ebf5 2020-01-15 tracey free(tag_row);
1713 87f9ebf5 2020-01-15 tracey if (error || (limit && --limit == 0))
1717 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1718 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1719 6c6c85af 2020-01-15 tracey tags = strdup(buf_get(diffbuf));
1722 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
1723 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
1724 87f9ebf5 2020-01-15 tracey if (repo)
1725 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
1726 87f9ebf5 2020-01-15 tracey if (error)
1727 87f9ebf5 2020-01-15 tracey return NULL;
1729 87f9ebf5 2020-01-15 tracey return tags;
1732 f2f46662 2020-01-23 tracey static void
1733 f2f46662 2020-01-23 tracey gw_free_headers(struct gw_header *header)
1735 f2f46662 2020-01-23 tracey free(header->id);
1736 f2f46662 2020-01-23 tracey free(header->path);
1737 f2f46662 2020-01-23 tracey if (header->commit != NULL)
1738 f2f46662 2020-01-23 tracey got_object_commit_close(header->commit);
1739 f2f46662 2020-01-23 tracey if (header->repo)
1740 f2f46662 2020-01-23 tracey got_repo_close(header->repo);
1741 f2f46662 2020-01-23 tracey free(header->refs_str);
1742 f2f46662 2020-01-23 tracey free(header->commit_id);
1743 f2f46662 2020-01-23 tracey free(header->parent_id);
1744 f2f46662 2020-01-23 tracey free(header->tree_id);
1745 f2f46662 2020-01-23 tracey free(header->author);
1746 f2f46662 2020-01-23 tracey free(header->committer);
1747 f2f46662 2020-01-23 tracey free(header->commit_msg);
1750 f2f46662 2020-01-23 tracey static struct gw_header *
1751 f2f46662 2020-01-23 tracey gw_init_header()
1753 f2f46662 2020-01-23 tracey struct gw_header *header;
1755 ae36ed87 2020-01-28 stsp header = malloc(sizeof(*header));
1756 ae36ed87 2020-01-28 stsp if (header == NULL)
1757 f2f46662 2020-01-23 tracey return NULL;
1759 f2f46662 2020-01-23 tracey header->repo = NULL;
1760 f2f46662 2020-01-23 tracey header->commit = NULL;
1761 f2f46662 2020-01-23 tracey header->id = NULL;
1762 f2f46662 2020-01-23 tracey header->path = NULL;
1763 ae36ed87 2020-01-28 stsp SIMPLEQ_INIT(&header->refs);
1765 f2f46662 2020-01-23 tracey return header;
1768 f2f46662 2020-01-23 tracey static const struct got_error *
1769 f2f46662 2020-01-23 tracey gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1770 f2f46662 2020-01-23 tracey int limit)
1772 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
1773 f2f46662 2020-01-23 tracey struct got_commit_graph *graph = NULL;
1775 f2f46662 2020-01-23 tracey error = got_commit_graph_open(&graph, header->path, 0);
1776 f2f46662 2020-01-23 tracey if (error)
1777 f2f46662 2020-01-23 tracey goto done;
1779 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_start(graph, header->id, header->repo,
1780 f2f46662 2020-01-23 tracey NULL, NULL);
1781 f2f46662 2020-01-23 tracey if (error)
1782 f2f46662 2020-01-23 tracey goto done;
1784 f2f46662 2020-01-23 tracey for (;;) {
1785 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_next(&header->id, graph,
1786 f2f46662 2020-01-23 tracey header->repo, NULL, NULL);
1787 f2f46662 2020-01-23 tracey if (error) {
1788 f2f46662 2020-01-23 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
1789 f2f46662 2020-01-23 tracey error = NULL;
1790 f2f46662 2020-01-23 tracey goto done;
1792 f2f46662 2020-01-23 tracey if (header->id == NULL)
1793 f2f46662 2020-01-23 tracey goto done;
1795 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit, header->repo,
1796 f2f46662 2020-01-23 tracey header->id);
1797 f2f46662 2020-01-23 tracey if (error)
1798 f2f46662 2020-01-23 tracey goto done;
1800 f2f46662 2020-01-23 tracey error = gw_get_commit(gw_trans, header);
1801 f2f46662 2020-01-23 tracey if (limit > 1) {
1802 f2f46662 2020-01-23 tracey struct gw_header *n_header = NULL;
1803 f2f46662 2020-01-23 tracey if ((n_header = gw_init_header()) == NULL)
1804 f2f46662 2020-01-23 tracey error = got_error_from_errno("malloc");
1806 f2f46662 2020-01-23 tracey n_header->refs_str = strdup(header->refs_str);
1807 f2f46662 2020-01-23 tracey n_header->commit_id = strdup(header->commit_id);
1808 f2f46662 2020-01-23 tracey n_header->parent_id = strdup(header->parent_id);
1809 f2f46662 2020-01-23 tracey n_header->tree_id = strdup(header->tree_id);
1810 f2f46662 2020-01-23 tracey n_header->author = strdup(header->author);
1811 f2f46662 2020-01-23 tracey n_header->committer = strdup(header->committer);
1812 f2f46662 2020-01-23 tracey n_header->commit_msg = strdup(header->commit_msg);
1813 f2f46662 2020-01-23 tracey n_header->committer_time = header->committer_time;
1814 f2f46662 2020-01-23 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1817 f2f46662 2020-01-23 tracey if (error || (limit && --limit == 0))
1821 f2f46662 2020-01-23 tracey if (graph)
1822 f2f46662 2020-01-23 tracey got_commit_graph_close(graph);
1823 f2f46662 2020-01-23 tracey return error;
1826 f2f46662 2020-01-23 tracey static const struct got_error *
1827 f2f46662 2020-01-23 tracey gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1829 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
1830 f2f46662 2020-01-23 tracey struct got_reflist_entry *re;
1831 f2f46662 2020-01-23 tracey struct got_object_id *id2 = NULL;
1832 f2f46662 2020-01-23 tracey struct got_object_qid *parent_id;
1833 f2f46662 2020-01-23 tracey char *refs_str = NULL,
1834 f2f46662 2020-01-23 tracey *commit_msg = NULL, *commit_msg0;
1836 f2f46662 2020-01-23 tracey /*print commit*/
1837 f2f46662 2020-01-23 tracey SIMPLEQ_FOREACH(re, &header->refs, entry) {
1838 f2f46662 2020-01-23 tracey char *s;
1839 f2f46662 2020-01-23 tracey const char *name;
1840 f2f46662 2020-01-23 tracey struct got_tag_object *tag = NULL;
1841 f2f46662 2020-01-23 tracey int cmp;
1843 f2f46662 2020-01-23 tracey name = got_ref_get_name(re->ref);
1844 f2f46662 2020-01-23 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
1845 f2f46662 2020-01-23 tracey continue;
1846 f2f46662 2020-01-23 tracey if (strncmp(name, "refs/", 5) == 0)
1847 f2f46662 2020-01-23 tracey name += 5;
1848 f2f46662 2020-01-23 tracey if (strncmp(name, "got/", 4) == 0)
1849 f2f46662 2020-01-23 tracey continue;
1850 f2f46662 2020-01-23 tracey if (strncmp(name, "heads/", 6) == 0)
1851 f2f46662 2020-01-23 tracey name += 6;
1852 f2f46662 2020-01-23 tracey if (strncmp(name, "remotes/", 8) == 0)
1853 f2f46662 2020-01-23 tracey name += 8;
1854 f2f46662 2020-01-23 tracey if (strncmp(name, "tags/", 5) == 0) {
1855 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag, header->repo,
1856 f2f46662 2020-01-23 tracey re->id);
1857 f2f46662 2020-01-23 tracey if (error) {
1858 f2f46662 2020-01-23 tracey if (error->code != GOT_ERR_OBJ_TYPE)
1859 f2f46662 2020-01-23 tracey continue;
1861 f2f46662 2020-01-23 tracey * Ref points at something other
1862 f2f46662 2020-01-23 tracey * than a tag.
1864 f2f46662 2020-01-23 tracey error = NULL;
1865 f2f46662 2020-01-23 tracey tag = NULL;
1868 f2f46662 2020-01-23 tracey cmp = got_object_id_cmp(tag ?
1869 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag) : re->id, header->id);
1870 f2f46662 2020-01-23 tracey if (tag)
1871 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
1872 f2f46662 2020-01-23 tracey if (cmp != 0)
1873 f2f46662 2020-01-23 tracey continue;
1874 f2f46662 2020-01-23 tracey s = refs_str;
1875 f2f46662 2020-01-23 tracey if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1876 f2f46662 2020-01-23 tracey s ? ", " : "", name)) == -1) {
1877 f2f46662 2020-01-23 tracey error = got_error_from_errno("asprintf");
1878 f2f46662 2020-01-23 tracey free(s);
1879 f2f46662 2020-01-23 tracey return error;
1881 f2f46662 2020-01-23 tracey header->refs_str = strdup(refs_str);
1882 f2f46662 2020-01-23 tracey free(s);
1885 f2f46662 2020-01-23 tracey if (refs_str == NULL)
1886 f2f46662 2020-01-23 tracey header->refs_str = strdup("");
1887 f2f46662 2020-01-23 tracey free(refs_str);
1889 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->commit_id, header->id);
1890 f2f46662 2020-01-23 tracey if (error)
1891 f2f46662 2020-01-23 tracey return error;
1893 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->tree_id,
1894 f2f46662 2020-01-23 tracey got_object_commit_get_tree_id(header->commit));
1895 f2f46662 2020-01-23 tracey if (error)
1896 f2f46662 2020-01-23 tracey return error;
1898 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_DIFF) {
1899 f2f46662 2020-01-23 tracey parent_id = SIMPLEQ_FIRST(
1900 f2f46662 2020-01-23 tracey got_object_commit_get_parent_ids(header->commit));
1901 f2f46662 2020-01-23 tracey if (parent_id != NULL) {
1902 f2f46662 2020-01-23 tracey id2 = got_object_id_dup(parent_id->id);
1903 f2f46662 2020-01-23 tracey free (parent_id);
1904 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->parent_id, id2);
1905 f2f46662 2020-01-23 tracey if (error)
1906 f2f46662 2020-01-23 tracey return error;
1907 f2f46662 2020-01-23 tracey free(id2);
1909 f2f46662 2020-01-23 tracey header->parent_id = strdup("/dev/null");
1911 f2f46662 2020-01-23 tracey header->parent_id = strdup("");
1913 f2f46662 2020-01-23 tracey header->committer_time =
1914 f2f46662 2020-01-23 tracey got_object_commit_get_committer_time(header->commit);
1915 f2f46662 2020-01-23 tracey header->author = strdup(got_object_commit_get_author(header->commit));
1916 f2f46662 2020-01-23 tracey header->committer =
1917 f2f46662 2020-01-23 tracey strdup(got_object_commit_get_committer(header->commit));
1919 f2f46662 2020-01-23 tracey error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
1920 f2f46662 2020-01-23 tracey if (error)
1921 f2f46662 2020-01-23 tracey return error;
1923 f2f46662 2020-01-23 tracey commit_msg = commit_msg0;
1924 f2f46662 2020-01-23 tracey while (*commit_msg == '\n')
1925 f2f46662 2020-01-23 tracey commit_msg++;
1927 f2f46662 2020-01-23 tracey header->commit_msg = strdup(commit_msg);
1928 f2f46662 2020-01-23 tracey free(commit_msg0);
1929 f2f46662 2020-01-23 tracey return error;
1932 f2f46662 2020-01-23 tracey static const struct got_error *
1933 f2f46662 2020-01-23 tracey gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
1935 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
1936 f2f46662 2020-01-23 tracey char *in_repo_path = NULL;
1938 f2f46662 2020-01-23 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1939 f2f46662 2020-01-23 tracey if (error)
1940 f2f46662 2020-01-23 tracey return error;
1942 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
1943 f2f46662 2020-01-23 tracey struct got_reference *head_ref;
1944 f2f46662 2020-01-23 tracey error = got_ref_open(&head_ref, header->repo,
1945 f2f46662 2020-01-23 tracey gw_trans->headref, 0);
1946 f2f46662 2020-01-23 tracey if (error)
1947 f2f46662 2020-01-23 tracey return error;
1949 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, head_ref);
1950 f2f46662 2020-01-23 tracey got_ref_close(head_ref);
1951 f2f46662 2020-01-23 tracey if (error)
1952 f2f46662 2020-01-23 tracey return error;
1954 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
1955 f2f46662 2020-01-23 tracey header->repo, header->id);
1956 f2f46662 2020-01-23 tracey } else {
1957 f2f46662 2020-01-23 tracey struct got_reference *ref;
1958 f2f46662 2020-01-23 tracey error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
1959 f2f46662 2020-01-23 tracey if (error == NULL) {
1960 f2f46662 2020-01-23 tracey int obj_type;
1961 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, ref);
1962 f2f46662 2020-01-23 tracey got_ref_close(ref);
1963 f2f46662 2020-01-23 tracey if (error)
1964 f2f46662 2020-01-23 tracey return error;
1965 f2f46662 2020-01-23 tracey error = got_object_get_type(&obj_type, header->repo,
1966 f2f46662 2020-01-23 tracey header->id);
1967 f2f46662 2020-01-23 tracey if (error)
1968 f2f46662 2020-01-23 tracey return error;
1969 f2f46662 2020-01-23 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
1970 f2f46662 2020-01-23 tracey struct got_tag_object *tag;
1971 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag,
1972 f2f46662 2020-01-23 tracey header->repo, header->id);
1973 f2f46662 2020-01-23 tracey if (error)
1974 f2f46662 2020-01-23 tracey return error;
1975 f2f46662 2020-01-23 tracey if (got_object_tag_get_object_type(tag) !=
1976 f2f46662 2020-01-23 tracey GOT_OBJ_TYPE_COMMIT) {
1977 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
1978 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1979 f2f46662 2020-01-23 tracey return error;
1981 f2f46662 2020-01-23 tracey free(header->id);
1982 f2f46662 2020-01-23 tracey header->id = got_object_id_dup(
1983 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag));
1984 f2f46662 2020-01-23 tracey if (header->id == NULL)
1985 f2f46662 2020-01-23 tracey error = got_error_from_errno(
1986 f2f46662 2020-01-23 tracey "got_object_id_dup");
1987 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
1988 f2f46662 2020-01-23 tracey if (error)
1989 f2f46662 2020-01-23 tracey return error;
1990 f2f46662 2020-01-23 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1991 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1992 f2f46662 2020-01-23 tracey return error;
1994 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
1995 f2f46662 2020-01-23 tracey header->repo, header->id);
1996 f2f46662 2020-01-23 tracey if (error)
1997 f2f46662 2020-01-23 tracey return error;
1999 f2f46662 2020-01-23 tracey if (header->commit == NULL) {
2000 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2001 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2002 f2f46662 2020-01-23 tracey header->repo);
2003 f2f46662 2020-01-23 tracey if (error)
2004 f2f46662 2020-01-23 tracey return error;
2006 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2007 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2008 f2f46662 2020-01-23 tracey header->repo);
2011 f2f46662 2020-01-23 tracey error = got_repo_map_path(&in_repo_path, header->repo,
2012 f2f46662 2020-01-23 tracey gw_trans->repo_path, 1);
2013 f2f46662 2020-01-23 tracey if (error)
2014 f2f46662 2020-01-23 tracey return error;
2016 f2f46662 2020-01-23 tracey if (in_repo_path) {
2017 f2f46662 2020-01-23 tracey header->path = strdup(in_repo_path);
2019 f2f46662 2020-01-23 tracey free(in_repo_path);
2021 f2f46662 2020-01-23 tracey error = got_ref_list(&header->refs, header->repo, NULL,
2022 f2f46662 2020-01-23 tracey got_ref_cmp_by_name, NULL);
2023 f2f46662 2020-01-23 tracey if (error)
2024 f2f46662 2020-01-23 tracey return error;
2026 f2f46662 2020-01-23 tracey error = gw_get_commits(gw_trans, header, limit);
2027 f2f46662 2020-01-23 tracey return error;
2030 ec46ccd7 2020-01-15 tracey struct blame_line {
2031 ec46ccd7 2020-01-15 tracey int annotated;
2032 ec46ccd7 2020-01-15 tracey char *id_str;
2033 ec46ccd7 2020-01-15 tracey char *committer;
2034 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
2037 147269d5 2020-01-15 tracey struct gw_blame_cb_args {
2038 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
2039 ec46ccd7 2020-01-15 tracey int nlines;
2040 ec46ccd7 2020-01-15 tracey int nlines_prec;
2041 ec46ccd7 2020-01-15 tracey int lineno_cur;
2042 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
2043 ec46ccd7 2020-01-15 tracey FILE *f;
2044 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
2045 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
2046 2e676fc5 2020-01-15 tracey struct buf *blamebuf;
2049 ec46ccd7 2020-01-15 tracey static const struct got_error *
2050 147269d5 2020-01-15 tracey gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2052 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
2053 147269d5 2020-01-15 tracey struct gw_blame_cb_args *a = arg;
2054 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
2055 ec46ccd7 2020-01-15 tracey char *line = NULL;
2056 2e676fc5 2020-01-15 tracey size_t linesize = 0, newsize;
2057 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
2058 ec46ccd7 2020-01-15 tracey off_t offset;
2059 ec46ccd7 2020-01-15 tracey struct tm tm;
2060 ec46ccd7 2020-01-15 tracey time_t committer_time;
2062 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
2063 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
2064 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
2066 ec46ccd7 2020-01-15 tracey if (lineno == -1)
2067 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
2069 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
2070 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
2071 ec46ccd7 2020-01-15 tracey if (bline->annotated)
2072 ec46ccd7 2020-01-15 tracey return NULL;
2073 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
2074 ec46ccd7 2020-01-15 tracey if (err)
2075 ec46ccd7 2020-01-15 tracey return err;
2077 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
2078 ec46ccd7 2020-01-15 tracey if (err)
2079 ec46ccd7 2020-01-15 tracey goto done;
2081 ec46ccd7 2020-01-15 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
2082 ec46ccd7 2020-01-15 tracey if (bline->committer == NULL) {
2083 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
2084 ec46ccd7 2020-01-15 tracey goto done;
2087 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
2088 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
2089 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
2090 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2091 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
2092 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
2093 ec46ccd7 2020-01-15 tracey goto done;
2095 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
2097 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
2098 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2099 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
2100 ec46ccd7 2020-01-15 tracey goto done;
2102 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
2103 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
2104 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
2105 ec46ccd7 2020-01-15 tracey goto done;
2108 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
2109 0311ce2d 2020-01-15 tracey char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2110 0311ce2d 2020-01-15 tracey *line_escape = NULL;
2111 ec46ccd7 2020-01-15 tracey size_t len;
2113 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
2114 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
2115 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
2119 ec46ccd7 2020-01-15 tracey committer = bline->committer;
2120 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
2121 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
2122 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
2123 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
2125 ec46ccd7 2020-01-15 tracey *at = '\0';
2126 ec46ccd7 2020-01-15 tracey len = strlen(committer);
2127 ec46ccd7 2020-01-15 tracey if (len >= 9)
2128 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
2130 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
2132 ec46ccd7 2020-01-15 tracey *nl = '\0';
2134 0311ce2d 2020-01-15 tracey if (strcmp(line, "") != 0)
2135 0311ce2d 2020-01-15 tracey line_escape = strdup(gw_html_escape(line));
2137 0311ce2d 2020-01-15 tracey line_escape = strdup("");
2139 f2f46662 2020-01-23 tracey asprintf(&blame_row, blame_line, a->nlines_prec,
2140 2e676fc5 2020-01-15 tracey a->lineno_cur, bline->id_str, bline->datebuf, committer,
2141 0311ce2d 2020-01-15 tracey line_escape);
2142 ec46ccd7 2020-01-15 tracey a->lineno_cur++;
2143 2e676fc5 2020-01-15 tracey err = buf_puts(&newsize, a->blamebuf, blame_row);
2144 2e676fc5 2020-01-15 tracey if (err)
2145 2e676fc5 2020-01-15 tracey return err;
2147 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2148 0311ce2d 2020-01-15 tracey free(line_escape);
2149 2e676fc5 2020-01-15 tracey free(blame_row);
2152 ec46ccd7 2020-01-15 tracey if (commit)
2153 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
2154 ec46ccd7 2020-01-15 tracey free(line);
2155 ec46ccd7 2020-01-15 tracey return err;
2158 bcbc97d8 2020-01-15 tracey static char*
2159 f2f46662 2020-01-23 tracey gw_get_file_blame(struct gw_trans *gw_trans)
2161 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
2162 bcbc97d8 2020-01-15 tracey struct got_repository *repo = NULL;
2163 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
2164 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
2165 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
2166 2e676fc5 2020-01-15 tracey char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2167 119bf4ed 2020-01-15 tracey *folder = NULL;
2168 147269d5 2020-01-15 tracey struct gw_blame_cb_args bca;
2169 119bf4ed 2020-01-15 tracey int i, obj_type;
2170 ec46ccd7 2020-01-15 tracey size_t filesize;
2172 ec46ccd7 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2173 ec46ccd7 2020-01-15 tracey if (error)
2174 ec46ccd7 2020-01-15 tracey goto done;
2176 2e676fc5 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2177 2e676fc5 2020-01-15 tracey if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2178 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2179 2e676fc5 2020-01-15 tracey goto done;
2182 2e676fc5 2020-01-15 tracey folder = strdup("");
2184 2e676fc5 2020-01-15 tracey if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2185 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2186 2e676fc5 2020-01-15 tracey goto done;
2188 2e676fc5 2020-01-15 tracey free(folder);
2190 2e676fc5 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, path, 1);
2191 ec46ccd7 2020-01-15 tracey if (error)
2192 ec46ccd7 2020-01-15 tracey goto done;
2194 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2195 147269d5 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT, 1, repo);
2196 ec46ccd7 2020-01-15 tracey if (error)
2197 ec46ccd7 2020-01-15 tracey goto done;
2199 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2200 ec46ccd7 2020-01-15 tracey if (error)
2201 ec46ccd7 2020-01-15 tracey goto done;
2203 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
2204 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
2205 ec46ccd7 2020-01-15 tracey goto done;
2208 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, obj_id);
2209 ec46ccd7 2020-01-15 tracey if (error)
2210 ec46ccd7 2020-01-15 tracey goto done;
2212 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
2213 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2214 ec46ccd7 2020-01-15 tracey goto done;
2217 ec46ccd7 2020-01-15 tracey error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2218 ec46ccd7 2020-01-15 tracey if (error)
2219 ec46ccd7 2020-01-15 tracey goto done;
2221 2e676fc5 2020-01-15 tracey error = buf_alloc(&bca.blamebuf, 0);
2222 2e676fc5 2020-01-15 tracey if (error)
2223 2e676fc5 2020-01-15 tracey goto done;
2225 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
2226 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
2227 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
2228 ec46ccd7 2020-01-15 tracey goto done;
2230 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2231 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
2232 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
2233 ec46ccd7 2020-01-15 tracey goto done;
2235 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
2236 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
2237 ec46ccd7 2020-01-15 tracey bca.nlines--;
2239 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2240 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
2241 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
2242 ec46ccd7 2020-01-15 tracey goto done;
2244 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
2245 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
2246 ec46ccd7 2020-01-15 tracey i = bca.nlines;
2247 ec46ccd7 2020-01-15 tracey while (i > 0) {
2248 ec46ccd7 2020-01-15 tracey i /= 10;
2249 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
2251 ec46ccd7 2020-01-15 tracey bca.repo = repo;
2252 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
2254 147269d5 2020-01-15 tracey error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2255 147269d5 2020-01-15 tracey NULL, NULL);
2256 2e676fc5 2020-01-15 tracey if (buf_len(bca.blamebuf) > 0) {
2257 2e676fc5 2020-01-15 tracey error = buf_putc(bca.blamebuf, '\0');
2258 2e676fc5 2020-01-15 tracey blame_html = strdup(buf_get(bca.blamebuf));
2261 2e676fc5 2020-01-15 tracey free(bca.blamebuf);
2262 2e676fc5 2020-01-15 tracey free(in_repo_path);
2263 2e676fc5 2020-01-15 tracey free(commit_id);
2264 2e676fc5 2020-01-15 tracey free(obj_id);
2265 2e676fc5 2020-01-15 tracey free(path);
2267 2e676fc5 2020-01-15 tracey if (blob)
2268 2e676fc5 2020-01-15 tracey error = got_object_blob_close(blob);
2269 2e676fc5 2020-01-15 tracey if (repo)
2270 2e676fc5 2020-01-15 tracey error = got_repo_close(repo);
2271 ec46ccd7 2020-01-15 tracey if (error)
2272 ec46ccd7 2020-01-15 tracey return NULL;
2273 2e676fc5 2020-01-15 tracey if (bca.lines) {
2274 2e676fc5 2020-01-15 tracey for (i = 0; i < bca.nlines; i++) {
2275 2e676fc5 2020-01-15 tracey struct blame_line *bline = &bca.lines[i];
2276 2e676fc5 2020-01-15 tracey free(bline->id_str);
2277 2e676fc5 2020-01-15 tracey free(bline->committer);
2279 2e676fc5 2020-01-15 tracey free(bca.lines);
2281 2e676fc5 2020-01-15 tracey free(bca.line_offsets);
2282 2e676fc5 2020-01-15 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
2283 2e676fc5 2020-01-15 tracey error = got_error_from_errno("fclose");
2284 2e676fc5 2020-01-15 tracey if (error)
2285 2e676fc5 2020-01-15 tracey return NULL;
2287 ec46ccd7 2020-01-15 tracey return blame_html;
2290 ec46ccd7 2020-01-15 tracey static char*
2291 f2f46662 2020-01-23 tracey gw_get_repo_tree(struct gw_trans *gw_trans)
2293 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
2294 ec46ccd7 2020-01-15 tracey struct got_repository *repo = NULL;
2295 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
2296 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
2297 bcbc97d8 2020-01-15 tracey struct buf *diffbuf = NULL;
2298 bcbc97d8 2020-01-15 tracey size_t newsize;
2299 bcbc97d8 2020-01-15 tracey char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2300 bcbc97d8 2020-01-15 tracey *tree_row = NULL, *id_str;
2301 bcbc97d8 2020-01-15 tracey int nentries, i;
2303 bcbc97d8 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2304 ec46ccd7 2020-01-15 tracey if (error)
2305 bcbc97d8 2020-01-15 tracey return NULL;
2307 bcbc97d8 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2308 ec46ccd7 2020-01-15 tracey if (error)
2309 bcbc97d8 2020-01-15 tracey goto done;
2311 bcbc97d8 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2312 ec46ccd7 2020-01-15 tracey if (error)
2313 bcbc97d8 2020-01-15 tracey goto done;
2315 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL)
2316 ec46ccd7 2020-01-15 tracey path = strdup(gw_trans->repo_folder);
2317 ec46ccd7 2020-01-15 tracey else if (in_repo_path) {
2318 bcbc97d8 2020-01-15 tracey free(path);
2319 bcbc97d8 2020-01-15 tracey path = in_repo_path;
2322 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
2323 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
2324 ec46ccd7 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2325 ec46ccd7 2020-01-15 tracey if (error)
2326 ec46ccd7 2020-01-15 tracey goto done;
2328 ec46ccd7 2020-01-15 tracey error = got_ref_resolve(&commit_id, repo, head_ref);
2329 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
2332 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL,
2333 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2334 f2f46662 2020-01-23 tracey if (error)
2335 f2f46662 2020-01-23 tracey goto done;
2337 f2f46662 2020-01-23 tracey error = got_object_id_str(&gw_trans->commit, commit_id);
2338 bcbc97d8 2020-01-15 tracey if (error)
2339 bcbc97d8 2020-01-15 tracey goto done;
2341 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2342 bcbc97d8 2020-01-15 tracey if (error)
2343 bcbc97d8 2020-01-15 tracey goto done;
2345 bcbc97d8 2020-01-15 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
2346 bcbc97d8 2020-01-15 tracey if (error)
2347 bcbc97d8 2020-01-15 tracey goto done;
2349 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
2351 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
2352 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
2353 ec46ccd7 2020-01-15 tracey const char *modestr = "";
2354 ec46ccd7 2020-01-15 tracey char *id = NULL, *url_html = NULL;
2356 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
2358 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2359 bcbc97d8 2020-01-15 tracey if (error)
2360 bcbc97d8 2020-01-15 tracey goto done;
2362 ec46ccd7 2020-01-15 tracey if ((asprintf(&id, "%s", id_str)) == -1) {
2363 bcbc97d8 2020-01-15 tracey error = got_error_from_errno("asprintf");
2364 bcbc97d8 2020-01-15 tracey free(id_str);
2365 bcbc97d8 2020-01-15 tracey goto done;
2368 bcbc97d8 2020-01-15 tracey mode_t mode = got_tree_entry_get_mode(te);
2370 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
2371 bcbc97d8 2020-01-15 tracey modestr = "$";
2372 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
2373 bcbc97d8 2020-01-15 tracey modestr = "@";
2374 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
2375 bcbc97d8 2020-01-15 tracey modestr = "/";
2376 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
2377 bcbc97d8 2020-01-15 tracey modestr = "*";
2379 ec46ccd7 2020-01-15 tracey char *build_folder = NULL;
2380 ec46ccd7 2020-01-15 tracey if (S_ISDIR(got_tree_entry_get_mode(te))) {
2381 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2382 ec46ccd7 2020-01-15 tracey if ((asprintf(&build_folder, "%s/%s",
2383 ec46ccd7 2020-01-15 tracey gw_trans->repo_folder,
2384 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te))) == -1) {
2386 ec46ccd7 2020-01-15 tracey got_error_from_errno("asprintf");
2387 ec46ccd7 2020-01-15 tracey goto done;
2389 ec46ccd7 2020-01-15 tracey } else {
2390 ec46ccd7 2020-01-15 tracey if (asprintf(&build_folder, "%s",
2391 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te)) == -1)
2392 ec46ccd7 2020-01-15 tracey goto done;
2395 ec46ccd7 2020-01-15 tracey if ((asprintf(&url_html, folder_html,
2396 ec46ccd7 2020-01-15 tracey gw_trans->repo_name, gw_trans->action_name,
2397 ec46ccd7 2020-01-15 tracey gw_trans->commit, build_folder,
2398 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te), modestr)) == -1) {
2399 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2400 ec46ccd7 2020-01-15 tracey goto done;
2402 ec46ccd7 2020-01-15 tracey } else {
2403 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2404 ec46ccd7 2020-01-15 tracey if ((asprintf(&build_folder, "%s",
2405 ec46ccd7 2020-01-15 tracey gw_trans->repo_folder)) == -1) {
2407 ec46ccd7 2020-01-15 tracey got_error_from_errno("asprintf");
2408 ec46ccd7 2020-01-15 tracey goto done;
2411 ec46ccd7 2020-01-15 tracey build_folder = strdup("");
2413 ec46ccd7 2020-01-15 tracey if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2414 ec46ccd7 2020-01-15 tracey "blame", gw_trans->commit,
2415 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te), build_folder,
2416 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te), modestr)) == -1) {
2417 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2418 ec46ccd7 2020-01-15 tracey goto done;
2421 ec46ccd7 2020-01-15 tracey free(build_folder);
2423 ec46ccd7 2020-01-15 tracey if (error)
2424 ec46ccd7 2020-01-15 tracey goto done;
2426 f2f46662 2020-01-23 tracey if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2427 bcbc97d8 2020-01-15 tracey error = got_error_from_errno("asprintf");
2428 bcbc97d8 2020-01-15 tracey goto done;
2430 bcbc97d8 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tree_row);
2431 ec46ccd7 2020-01-15 tracey if (error)
2432 ec46ccd7 2020-01-15 tracey goto done;
2434 bcbc97d8 2020-01-15 tracey free(id);
2435 bcbc97d8 2020-01-15 tracey free(id_str);
2436 ec46ccd7 2020-01-15 tracey free(url_html);
2437 ec46ccd7 2020-01-15 tracey free(tree_row);
2440 bcbc97d8 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2441 bcbc97d8 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2442 bcbc97d8 2020-01-15 tracey tree_html = strdup(buf_get(diffbuf));
2445 bcbc97d8 2020-01-15 tracey if (tree)
2446 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
2447 2e676fc5 2020-01-15 tracey if (repo)
2448 2e676fc5 2020-01-15 tracey got_repo_close(repo);
2450 2e676fc5 2020-01-15 tracey free(in_repo_path);
2451 bcbc97d8 2020-01-15 tracey free(tree_id);
2452 bcbc97d8 2020-01-15 tracey free(diffbuf);
2453 bcbc97d8 2020-01-15 tracey if (error)
2454 bcbc97d8 2020-01-15 tracey return NULL;
2456 bcbc97d8 2020-01-15 tracey return tree_html;
2459 8d4d2453 2020-01-15 tracey static char *
2460 54415d85 2020-01-15 tracey gw_get_repo_heads(struct gw_trans *gw_trans)
2462 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2463 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2464 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2465 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2466 87f9ebf5 2020-01-15 tracey char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2467 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2468 87f9ebf5 2020-01-15 tracey size_t newsize;
2470 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2471 ec46ccd7 2020-01-15 tracey if (error)
2472 6c6c85af 2020-01-15 tracey return NULL;
2474 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2475 ec46ccd7 2020-01-15 tracey if (error)
2476 87f9ebf5 2020-01-15 tracey goto done;
2478 87f9ebf5 2020-01-15 tracey SIMPLEQ_INIT(&refs);
2479 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2481 87f9ebf5 2020-01-15 tracey if (error)
2482 87f9ebf5 2020-01-15 tracey goto done;
2484 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2485 87f9ebf5 2020-01-15 tracey char *refname;
2487 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
2488 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
2489 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2490 87f9ebf5 2020-01-15 tracey goto done;
2493 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
2494 87f9ebf5 2020-01-15 tracey free(refname);
2495 87f9ebf5 2020-01-15 tracey continue;
2498 87f9ebf5 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2499 87f9ebf5 2020-01-15 tracey TM_DIFF);
2501 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2502 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
2503 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
2504 87f9ebf5 2020-01-15 tracey refname)) == -1) {
2505 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2506 87f9ebf5 2020-01-15 tracey goto done;
2509 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
2510 87f9ebf5 2020-01-15 tracey refname += 11;
2512 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_row, heads_row, age, refname,
2513 87f9ebf5 2020-01-15 tracey head_navs_disp)) == -1) {
2514 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2515 87f9ebf5 2020-01-15 tracey goto done;
2518 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
2520 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
2521 87f9ebf5 2020-01-15 tracey free(head_row);
2524 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2525 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2526 6c6c85af 2020-01-15 tracey heads = strdup(buf_get(diffbuf));
2529 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2530 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2531 87f9ebf5 2020-01-15 tracey if (repo)
2532 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2533 87f9ebf5 2020-01-15 tracey if (error)
2534 87f9ebf5 2020-01-15 tracey return NULL;
2536 87f9ebf5 2020-01-15 tracey return heads;
2539 8d4d2453 2020-01-15 tracey static char *
2540 54415d85 2020-01-15 tracey gw_get_got_link(struct gw_trans *gw_trans)
2542 2c251c14 2020-01-15 tracey char *link;
2544 2c251c14 2020-01-15 tracey if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2545 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_logo)) == -1)
2546 2c251c14 2020-01-15 tracey return NULL;
2548 2c251c14 2020-01-15 tracey return link;
2551 2c251c14 2020-01-15 tracey static char *
2552 54415d85 2020-01-15 tracey gw_get_site_link(struct gw_trans *gw_trans)
2554 2c251c14 2020-01-15 tracey char *link, *repo = "", *action = "";
2556 2c251c14 2020-01-15 tracey if (gw_trans->repo_name != NULL)
2557 2c251c14 2020-01-15 tracey if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2558 2c251c14 2020-01-15 tracey "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2559 2c251c14 2020-01-15 tracey return NULL;
2561 2c251c14 2020-01-15 tracey if (gw_trans->action_name != NULL)
2562 2c251c14 2020-01-15 tracey if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2563 2c251c14 2020-01-15 tracey return NULL;
2565 2c251c14 2020-01-15 tracey if ((asprintf(&link, site_link, GOTWEB,
2566 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2567 2c251c14 2020-01-15 tracey return NULL;
2569 2c251c14 2020-01-15 tracey return link;
2572 2c251c14 2020-01-15 tracey static char *
2573 d0ea9c5b 2020-01-15 tracey gw_colordiff_line(char *buf)
2575 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
2576 ec46ccd7 2020-01-15 tracey char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2577 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
2578 ec46ccd7 2020-01-15 tracey size_t newsize;
2580 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2581 ec46ccd7 2020-01-15 tracey if (error)
2582 ec46ccd7 2020-01-15 tracey return NULL;
2584 bfd30182 2020-01-24 tracey if (buf == NULL)
2585 bfd30182 2020-01-24 tracey return NULL;
2586 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
2587 ec46ccd7 2020-01-15 tracey color = "diff_minus";
2588 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "+", 1) == 0)
2589 ec46ccd7 2020-01-15 tracey color = "diff_plus";
2590 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "@@", 2) == 0)
2591 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
2592 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "@@", 2) == 0)
2593 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
2594 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "commit +", 8) == 0)
2595 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2596 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "commit -", 8) == 0)
2597 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2598 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "blob +", 6) == 0)
2599 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2600 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "blob -", 6) == 0)
2601 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2602 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "file +", 6) == 0)
2603 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2604 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "file -", 6) == 0)
2605 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2606 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "from:", 5) == 0)
2607 ec46ccd7 2020-01-15 tracey color = "diff_author";
2608 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "via:", 4) == 0)
2609 ec46ccd7 2020-01-15 tracey color = "diff_author";
2610 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "date:", 5) == 0)
2611 ec46ccd7 2020-01-15 tracey color = "diff_date";
2613 ec46ccd7 2020-01-15 tracey if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2614 ec46ccd7 2020-01-15 tracey return NULL;
2616 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2617 ec46ccd7 2020-01-15 tracey if (error)
2618 ec46ccd7 2020-01-15 tracey return NULL;
2620 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, buf);
2621 ec46ccd7 2020-01-15 tracey if (error)
2622 ec46ccd7 2020-01-15 tracey return NULL;
2624 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2625 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2626 ec46ccd7 2020-01-15 tracey colorized_line = strdup(buf_get(diffbuf));
2629 ec46ccd7 2020-01-15 tracey free(diffbuf);
2630 ec46ccd7 2020-01-15 tracey free(div_diff_line_div);
2631 ec46ccd7 2020-01-15 tracey return colorized_line;
2634 ec46ccd7 2020-01-15 tracey static char *
2635 2c251c14 2020-01-15 tracey gw_html_escape(const char *html)
2637 2c251c14 2020-01-15 tracey char *escaped_str = NULL, *buf;
2638 2c251c14 2020-01-15 tracey char c[1];
2639 6c6c85af 2020-01-15 tracey size_t sz, i, buff_sz = 2048;
2641 6c6c85af 2020-01-15 tracey if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2642 2c251c14 2020-01-15 tracey return NULL;
2644 2c251c14 2020-01-15 tracey if (html == NULL)
2645 2c251c14 2020-01-15 tracey return NULL;
2647 2c251c14 2020-01-15 tracey if ((sz = strlen(html)) == 0)
2648 2c251c14 2020-01-15 tracey return NULL;
2650 6c6c85af 2020-01-15 tracey /* only work with buff_sz */
2651 6c6c85af 2020-01-15 tracey if (buff_sz < sz)
2652 6c6c85af 2020-01-15 tracey sz = buff_sz;
2654 2c251c14 2020-01-15 tracey for (i = 0; i < sz; i++) {
2655 2c251c14 2020-01-15 tracey c[0] = html[i];
2656 2c251c14 2020-01-15 tracey switch (c[0]) {
2657 2c251c14 2020-01-15 tracey case ('>'):
2658 2c251c14 2020-01-15 tracey strcat(buf, ">");
2660 2c251c14 2020-01-15 tracey case ('&'):
2661 2c251c14 2020-01-15 tracey strcat(buf, "&");
2663 2c251c14 2020-01-15 tracey case ('<'):
2664 2c251c14 2020-01-15 tracey strcat(buf, "<");
2666 2c251c14 2020-01-15 tracey case ('"'):
2667 2c251c14 2020-01-15 tracey strcat(buf, """);
2669 2c251c14 2020-01-15 tracey case ('\''):
2670 2c251c14 2020-01-15 tracey strcat(buf, "'");
2672 2c251c14 2020-01-15 tracey case ('\n'):
2673 2c251c14 2020-01-15 tracey strcat(buf, "<br />");
2674 2c251c14 2020-01-15 tracey default:
2675 2c251c14 2020-01-15 tracey strcat(buf, &c[0]);
2679 2c251c14 2020-01-15 tracey asprintf(&escaped_str, "%s", buf);
2680 2c251c14 2020-01-15 tracey free(buf);
2681 2c251c14 2020-01-15 tracey return escaped_str;
2685 c08369d7 2020-01-15 tracey main(int argc, char *argv[])
2687 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2688 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
2689 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
2690 2c251c14 2020-01-15 tracey const char *page = "index";
2691 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
2692 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
2694 54415d85 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2695 2c251c14 2020-01-15 tracey errx(1, "malloc");
2697 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2698 2c251c14 2020-01-15 tracey errx(1, "malloc");
2700 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2701 2c251c14 2020-01-15 tracey errx(1, "malloc");
2703 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2704 2c251c14 2020-01-15 tracey errx(1, "malloc");
2706 c25c2314 2020-01-28 stsp kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2707 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK) {
2708 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
2712 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
2713 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
2714 4ceb8155 2020-01-15 tracey gw_malloc = 0;
2715 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
2716 46b9c89b 2020-01-15 tracey goto err;
2719 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
2720 f2f46662 2020-01-23 tracey TAILQ_INIT(&gw_trans->gw_headers);
2722 2c251c14 2020-01-15 tracey gw_trans->page = 0;
2723 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
2724 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
2725 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
2726 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
2727 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
2728 54415d85 2020-01-15 tracey gw_trans->gw_tmpl->key = gw_templs;
2729 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2730 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
2731 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
2732 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2735 2c251c14 2020-01-15 tracey if (error) {
2736 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
2737 2c251c14 2020-01-15 tracey gw_trans->action = GW_ERR;
2738 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
2739 2c251c14 2020-01-15 tracey goto done;
2742 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
2743 2c251c14 2020-01-15 tracey if (error)
2744 2c251c14 2020-01-15 tracey goto err;
2746 c25c2314 2020-01-28 stsp error = gw_display_index(gw_trans, error);
2747 c25c2314 2020-01-28 stsp if (error)
2751 2c251c14 2020-01-15 tracey if (gw_malloc) {
2752 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
2753 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_www_path);
2754 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
2755 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
2756 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
2757 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
2758 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
2759 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
2760 2c251c14 2020-01-15 tracey free(gw_trans->commit);
2761 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
2762 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
2763 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
2764 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
2765 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
2767 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2768 2c251c14 2020-01-15 tracey free(dir->name);
2769 2c251c14 2020-01-15 tracey free(dir->description);
2770 2c251c14 2020-01-15 tracey free(dir->age);
2771 2c251c14 2020-01-15 tracey free(dir->url);
2772 2c251c14 2020-01-15 tracey free(dir->path);
2773 2c251c14 2020-01-15 tracey free(dir);
2778 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
2779 2c251c14 2020-01-15 tracey return EXIT_SUCCESS;