Blame


1 2c251c14 2020-01-15 tracey /*
2 9460dac0 2020-01-15 tracey * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 2c251c14 2020-01-15 tracey * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 2c251c14 2020-01-15 tracey *
5 2c251c14 2020-01-15 tracey * Permission to use, copy, modify, and distribute this software for any
6 2c251c14 2020-01-15 tracey * purpose with or without fee is hereby granted, provided that the above
7 2c251c14 2020-01-15 tracey * copyright notice and this permission notice appear in all copies.
8 2c251c14 2020-01-15 tracey *
9 2c251c14 2020-01-15 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2c251c14 2020-01-15 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2c251c14 2020-01-15 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2c251c14 2020-01-15 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2c251c14 2020-01-15 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2c251c14 2020-01-15 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2c251c14 2020-01-15 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 2c251c14 2020-01-15 tracey */
17 2c251c14 2020-01-15 tracey
18 2c251c14 2020-01-15 tracey #include <sys/queue.h>
19 2c251c14 2020-01-15 tracey #include <sys/stat.h>
20 2c251c14 2020-01-15 tracey #include <sys/types.h>
21 2c251c14 2020-01-15 tracey
22 017d6da3 2020-01-29 tracey #include <ctype.h>
23 2c251c14 2020-01-15 tracey #include <dirent.h>
24 2c251c14 2020-01-15 tracey #include <err.h>
25 c25c2314 2020-01-28 stsp #include <errno.h>
26 474370cb 2020-01-15 tracey #include <regex.h>
27 2c251c14 2020-01-15 tracey #include <stdarg.h>
28 2c251c14 2020-01-15 tracey #include <stdint.h>
29 2c251c14 2020-01-15 tracey #include <stdio.h>
30 2c251c14 2020-01-15 tracey #include <stdlib.h>
31 2c251c14 2020-01-15 tracey #include <string.h>
32 2c251c14 2020-01-15 tracey #include <unistd.h>
33 2c251c14 2020-01-15 tracey
34 2c251c14 2020-01-15 tracey #include <got_object.h>
35 2c251c14 2020-01-15 tracey #include <got_reference.h>
36 2c251c14 2020-01-15 tracey #include <got_repository.h>
37 2c251c14 2020-01-15 tracey #include <got_path.h>
38 2c251c14 2020-01-15 tracey #include <got_cancel.h>
39 2c251c14 2020-01-15 tracey #include <got_worktree.h>
40 2c251c14 2020-01-15 tracey #include <got_diff.h>
41 2c251c14 2020-01-15 tracey #include <got_commit_graph.h>
42 2c251c14 2020-01-15 tracey #include <got_blame.h>
43 2c251c14 2020-01-15 tracey #include <got_privsep.h>
44 2c251c14 2020-01-15 tracey #include <got_opentemp.h>
45 2c251c14 2020-01-15 tracey
46 2c251c14 2020-01-15 tracey #include <kcgi.h>
47 2c251c14 2020-01-15 tracey #include <kcgihtml.h>
48 2c251c14 2020-01-15 tracey
49 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"
52 2c251c14 2020-01-15 tracey
53 2c251c14 2020-01-15 tracey #ifndef nitems
54 2c251c14 2020-01-15 tracey #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 2c251c14 2020-01-15 tracey #endif
56 2c251c14 2020-01-15 tracey
57 54415d85 2020-01-15 tracey struct gw_trans {
58 f2f46662 2020-01-23 tracey TAILQ_HEAD(headers, gw_header) gw_headers;
59 f2f46662 2020-01-23 tracey TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 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;
76 2c251c14 2020-01-15 tracey };
77 2c251c14 2020-01-15 tracey
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;
85 f2f46662 2020-01-23 tracey
86 f2f46662 2020-01-23 tracey char *refs_str;
87 f2f46662 2020-01-23 tracey char *commit_id; /* id_str1 */
88 f2f46662 2020-01-23 tracey char *parent_id; /* id_str2 */
89 f2f46662 2020-01-23 tracey char *tree_id;
90 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;
94 2c251c14 2020-01-15 tracey };
95 2c251c14 2020-01-15 tracey
96 2c251c14 2020-01-15 tracey struct gw_dir {
97 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
98 2c251c14 2020-01-15 tracey char *name;
99 2c251c14 2020-01-15 tracey char *owner;
100 2c251c14 2020-01-15 tracey char *description;
101 2c251c14 2020-01-15 tracey char *url;
102 2c251c14 2020-01-15 tracey char *age;
103 2c251c14 2020-01-15 tracey char *path;
104 2c251c14 2020-01-15 tracey };
105 2c251c14 2020-01-15 tracey
106 f2f46662 2020-01-23 tracey enum gw_key {
107 f2f46662 2020-01-23 tracey KEY_ACTION,
108 f2f46662 2020-01-23 tracey KEY_COMMIT_ID,
109 f2f46662 2020-01-23 tracey KEY_FILE,
110 f2f46662 2020-01-23 tracey KEY_FOLDER,
111 f2f46662 2020-01-23 tracey KEY_HEADREF,
112 f2f46662 2020-01-23 tracey KEY_PAGE,
113 f2f46662 2020-01-23 tracey KEY_PATH,
114 f2f46662 2020-01-23 tracey KEY__ZMAX
115 f2f46662 2020-01-23 tracey };
116 f2f46662 2020-01-23 tracey
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
126 2c251c14 2020-01-15 tracey };
127 2c251c14 2020-01-15 tracey
128 54415d85 2020-01-15 tracey enum gw_ref_tm {
129 387a29ba 2020-01-15 tracey TM_DIFF,
130 387a29ba 2020-01-15 tracey TM_LONG,
131 387a29ba 2020-01-15 tracey };
132 387a29ba 2020-01-15 tracey
133 54415d85 2020-01-15 tracey enum gw_tags {
134 87f9ebf5 2020-01-15 tracey TAGBRIEF,
135 87f9ebf5 2020-01-15 tracey TAGFULL,
136 87f9ebf5 2020-01-15 tracey };
137 87f9ebf5 2020-01-15 tracey
138 54415d85 2020-01-15 tracey static const char *const gw_templs[TEMPL__MAX] = {
139 f2f46662 2020-01-23 tracey "content",
140 2c251c14 2020-01-15 tracey "head",
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",
145 2c251c14 2020-01-15 tracey "title",
146 2c251c14 2020-01-15 tracey };
147 2c251c14 2020-01-15 tracey
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" },
156 2c251c14 2020-01-15 tracey };
157 2c251c14 2020-01-15 tracey
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);
160 2c251c14 2020-01-15 tracey
161 32cd4d18 2020-02-03 stsp static const struct got_error *gw_get_repo_description(char **, struct gw_trans *,
162 2c251c14 2020-01-15 tracey char *);
163 9a1cc63f 2020-02-03 stsp static const struct got_error *gw_get_repo_owner(char **, struct gw_trans *,
164 2c251c14 2020-01-15 tracey char *);
165 55ccf528 2020-02-03 stsp static const struct got_error *gw_get_time_str(char **, time_t, int);
166 d126c1b7 2020-01-29 stsp static const struct got_error *gw_get_repo_age(char **, struct gw_trans *,
167 387a29ba 2020-01-15 tracey char *, char *, int);
168 a81f3554 2020-02-03 stsp static const struct got_error *gw_get_file_blame_blob(char **, struct gw_trans *);
169 5894d523 2020-02-03 stsp static const struct got_error *gw_get_file_read_blob(char **, size_t *,
170 5894d523 2020-02-03 stsp struct gw_trans *);
171 84bf4df2 2020-02-03 stsp static const struct got_error *gw_get_repo_tree(char **, struct gw_trans *);
172 83eb9a68 2020-02-03 stsp static const struct got_error *gw_get_diff(char **, struct gw_trans *,
173 f2f46662 2020-01-23 tracey struct gw_header *);
174 6eccd105 2020-02-03 stsp static const struct got_error *gw_get_repo_tags(char **, struct gw_trans *,
175 4ff7391f 2020-01-28 tracey struct gw_header *, int, int);
176 54415d85 2020-01-15 tracey static char *gw_get_repo_heads(struct gw_trans *);
177 59d3c40e 2020-02-03 stsp static const struct got_error *gw_get_clone_url(char **, struct gw_trans *, char *);
178 54415d85 2020-01-15 tracey static char *gw_get_got_link(struct gw_trans *);
179 54415d85 2020-01-15 tracey static char *gw_get_site_link(struct gw_trans *);
180 070fee27 2020-02-03 stsp static const struct got_error *gw_html_escape(char **, const char *);
181 83eb9a68 2020-02-03 stsp static const struct got_error *gw_colordiff_line(char **, char *);
182 2c251c14 2020-01-15 tracey
183 f2f46662 2020-01-23 tracey static char *gw_gen_commit_header(char *, char*);
184 f2f46662 2020-01-23 tracey static char *gw_gen_diff_header(char *, char*);
185 f2f46662 2020-01-23 tracey static char *gw_gen_author_header(char *);
186 f2f46662 2020-01-23 tracey static char *gw_gen_committer_header(char *);
187 f2f46662 2020-01-23 tracey static char *gw_gen_commit_msg_header(char *);
188 f2f46662 2020-01-23 tracey static char *gw_gen_tree_header(char *);
189 f2f46662 2020-01-23 tracey
190 f2f46662 2020-01-23 tracey static void gw_free_headers(struct gw_header *);
191 c25c2314 2020-01-28 stsp static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
192 2c251c14 2020-01-15 tracey enum kmime);
193 6d8d8a26 2020-01-29 stsp static const struct got_error* gw_display_index(struct gw_trans *);
194 6d8d8a26 2020-01-29 stsp static void gw_display_error(struct gw_trans *,
195 2c251c14 2020-01-15 tracey const struct got_error *);
196 2c251c14 2020-01-15 tracey
197 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
198 2c251c14 2020-01-15 tracey
199 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_header(struct gw_trans *,
200 f2f46662 2020-01-23 tracey struct gw_header *, int);
201 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commits(struct gw_trans *,
202 f2f46662 2020-01-23 tracey struct gw_header *, int);
203 f2f46662 2020-01-23 tracey static const struct got_error* gw_get_commit(struct gw_trans *,
204 f2f46662 2020-01-23 tracey struct gw_header *);
205 54415d85 2020-01-15 tracey static const struct got_error* gw_apply_unveil(const char *, const char *);
206 147269d5 2020-01-15 tracey static const struct got_error* gw_blame_cb(void *, int, int,
207 ec46ccd7 2020-01-15 tracey struct got_object_id *);
208 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct gw_trans *);
209 54415d85 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct gw_trans *,
210 2c251c14 2020-01-15 tracey struct gw_dir *);
211 54415d85 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct gw_trans *);
212 2c251c14 2020-01-15 tracey
213 54415d85 2020-01-15 tracey static const struct got_error* gw_blame(struct gw_trans *);
214 e46f587c 2020-01-29 tracey static const struct got_error* gw_blob(struct gw_trans *);
215 f2f46662 2020-01-23 tracey static const struct got_error* gw_diff(struct gw_trans *);
216 54415d85 2020-01-15 tracey static const struct got_error* gw_index(struct gw_trans *);
217 f2f46662 2020-01-23 tracey static const struct got_error* gw_commits(struct gw_trans *);
218 f2f46662 2020-01-23 tracey static const struct got_error* gw_briefs(struct gw_trans *);
219 54415d85 2020-01-15 tracey static const struct got_error* gw_summary(struct gw_trans *);
220 54415d85 2020-01-15 tracey static const struct got_error* gw_tree(struct gw_trans *);
221 4ff7391f 2020-01-28 tracey static const struct got_error* gw_tag(struct gw_trans *);
222 2c251c14 2020-01-15 tracey
223 2c251c14 2020-01-15 tracey struct gw_query_action {
224 2c251c14 2020-01-15 tracey unsigned int func_id;
225 2c251c14 2020-01-15 tracey const char *func_name;
226 54415d85 2020-01-15 tracey const struct got_error *(*func_main)(struct gw_trans *);
227 2c251c14 2020-01-15 tracey char *template;
228 2c251c14 2020-01-15 tracey };
229 2c251c14 2020-01-15 tracey
230 2c251c14 2020-01-15 tracey enum gw_query_actions {
231 2c251c14 2020-01-15 tracey GW_BLAME,
232 e46f587c 2020-01-29 tracey GW_BLOB,
233 f2f46662 2020-01-23 tracey GW_BRIEFS,
234 f2f46662 2020-01-23 tracey GW_COMMITS,
235 f2f46662 2020-01-23 tracey GW_DIFF,
236 2c251c14 2020-01-15 tracey GW_ERR,
237 2c251c14 2020-01-15 tracey GW_INDEX,
238 2c251c14 2020-01-15 tracey GW_SUMMARY,
239 4ff7391f 2020-01-28 tracey GW_TAG,
240 b772de24 2020-01-15 tracey GW_TREE,
241 2c251c14 2020-01-15 tracey };
242 2c251c14 2020-01-15 tracey
243 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
244 f2f46662 2020-01-23 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
245 017d6da3 2020-01-29 tracey { GW_BLOB, "blob", NULL, NULL },
246 f2f46662 2020-01-23 tracey { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
247 f2f46662 2020-01-23 tracey { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
248 f2f46662 2020-01-23 tracey { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
249 f2f46662 2020-01-23 tracey { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
250 f2f46662 2020-01-23 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
251 f2f46662 2020-01-23 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
252 4ff7391f 2020-01-28 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
253 f2f46662 2020-01-23 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
254 2c251c14 2020-01-15 tracey };
255 c25c2314 2020-01-28 stsp
256 c25c2314 2020-01-28 stsp static const struct got_error *
257 c25c2314 2020-01-28 stsp gw_kcgi_error(enum kcgi_err kerr)
258 c25c2314 2020-01-28 stsp {
259 c25c2314 2020-01-28 stsp if (kerr == KCGI_OK)
260 c25c2314 2020-01-28 stsp return NULL;
261 c25c2314 2020-01-28 stsp
262 c25c2314 2020-01-28 stsp if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
263 c25c2314 2020-01-28 stsp return got_error(GOT_ERR_CANCELLED);
264 c25c2314 2020-01-28 stsp
265 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENOMEM)
266 c25c2314 2020-01-28 stsp return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
267 2c251c14 2020-01-15 tracey
268 c25c2314 2020-01-28 stsp if (kerr == KCGI_ENFILE)
269 c25c2314 2020-01-28 stsp return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
270 c25c2314 2020-01-28 stsp
271 c25c2314 2020-01-28 stsp if (kerr == KCGI_EAGAIN)
272 c25c2314 2020-01-28 stsp return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
273 c25c2314 2020-01-28 stsp
274 c25c2314 2020-01-28 stsp if (kerr == KCGI_FORM)
275 c25c2314 2020-01-28 stsp return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
276 c25c2314 2020-01-28 stsp
277 c25c2314 2020-01-28 stsp return got_error_from_errno(kcgi_strerror(kerr));
278 c25c2314 2020-01-28 stsp }
279 c25c2314 2020-01-28 stsp
280 2c251c14 2020-01-15 tracey static const struct got_error *
281 54415d85 2020-01-15 tracey gw_apply_unveil(const char *repo_path, const char *repo_file)
282 2c251c14 2020-01-15 tracey {
283 2c251c14 2020-01-15 tracey const struct got_error *err;
284 2c251c14 2020-01-15 tracey
285 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
286 2c251c14 2020-01-15 tracey char *full_path;
287 88d59c36 2020-01-29 stsp if (asprintf(&full_path, "%s/%s", repo_path, repo_file) == -1)
288 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
289 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
290 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
291 2c251c14 2020-01-15 tracey }
292 2c251c14 2020-01-15 tracey
293 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
294 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
295 2c251c14 2020-01-15 tracey
296 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
297 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
298 2c251c14 2020-01-15 tracey
299 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
300 2c251c14 2020-01-15 tracey if (err != NULL)
301 2c251c14 2020-01-15 tracey return err;
302 2c251c14 2020-01-15 tracey
303 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
304 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
305 2c251c14 2020-01-15 tracey
306 2c251c14 2020-01-15 tracey return NULL;
307 87f9ebf5 2020-01-15 tracey }
308 65b95fb2 2020-01-15 tracey
309 2c251c14 2020-01-15 tracey static const struct got_error *
310 32cd4d18 2020-02-03 stsp gw_empty_string(char **s)
311 32cd4d18 2020-02-03 stsp {
312 32cd4d18 2020-02-03 stsp *s = strdup("");
313 32cd4d18 2020-02-03 stsp if (*s == NULL)
314 32cd4d18 2020-02-03 stsp return got_error_from_errno("strdup");
315 32cd4d18 2020-02-03 stsp return NULL;
316 5894d523 2020-02-03 stsp }
317 5894d523 2020-02-03 stsp
318 5894d523 2020-02-03 stsp static int
319 5894d523 2020-02-03 stsp isbinary(const char *buf, size_t n)
320 5894d523 2020-02-03 stsp {
321 5894d523 2020-02-03 stsp return (memchr(buf, '\0', n) != NULL);
322 32cd4d18 2020-02-03 stsp }
323 32cd4d18 2020-02-03 stsp
324 5894d523 2020-02-03 stsp
325 32cd4d18 2020-02-03 stsp static const struct got_error *
326 54415d85 2020-01-15 tracey gw_blame(struct gw_trans *gw_trans)
327 2c251c14 2020-01-15 tracey {
328 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
329 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
330 f2f46662 2020-01-23 tracey char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
331 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
332 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
333 2c251c14 2020-01-15 tracey
334 6bee13de 2020-01-16 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
335 f2f46662 2020-01-23 tracey NULL) == -1)
336 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
337 6bee13de 2020-01-16 tracey
338 f2f46662 2020-01-23 tracey if ((header = gw_init_header()) == NULL)
339 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
340 f2f46662 2020-01-23 tracey
341 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
342 ec46ccd7 2020-01-15 tracey if (error)
343 5ddf0079 2020-01-29 stsp goto done;
344 ec46ccd7 2020-01-15 tracey
345 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
346 f2f46662 2020-01-23 tracey if (error)
347 5ddf0079 2020-01-29 stsp goto done;
348 ec46ccd7 2020-01-15 tracey
349 a81f3554 2020-02-03 stsp error = gw_get_file_blame_blob(&blame_html, gw_trans);
350 a81f3554 2020-02-03 stsp if (error)
351 a81f3554 2020-02-03 stsp goto done;
352 55ccf528 2020-02-03 stsp
353 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, header->committer_time, TM_LONG);
354 55ccf528 2020-02-03 stsp if (error)
355 55ccf528 2020-02-03 stsp goto done;
356 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
357 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
358 55ccf528 2020-02-03 stsp goto done;
359 55ccf528 2020-02-03 stsp }
360 55ccf528 2020-02-03 stsp
361 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
362 070fee27 2020-02-03 stsp if (error)
363 070fee27 2020-02-03 stsp goto done;
364 55ccf528 2020-02-03 stsp if (asprintf(&blame_html_disp, blame_header, age_html,
365 f146a4ea 2020-02-03 tracey gw_gen_commit_msg_header(escaped_commit_msg), blame_html) == -1) {
366 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
367 6d9fc692 2020-01-28 stsp goto done;
368 6d9fc692 2020-01-28 stsp }
369 f2f46662 2020-01-23 tracey
370 6d9fc692 2020-01-28 stsp if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
371 6d9fc692 2020-01-28 stsp error = got_error_from_errno("asprintf");
372 6d9fc692 2020-01-28 stsp goto done;
373 6d9fc692 2020-01-28 stsp }
374 f2f46662 2020-01-23 tracey
375 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, blame);
376 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
377 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
378 6d9fc692 2020-01-28 stsp done:
379 f2f46662 2020-01-23 tracey got_ref_list_free(&header->refs);
380 f2f46662 2020-01-23 tracey gw_free_headers(header);
381 f2f46662 2020-01-23 tracey free(blame_html_disp);
382 f2f46662 2020-01-23 tracey free(blame_html);
383 f2f46662 2020-01-23 tracey free(blame);
384 070fee27 2020-02-03 stsp free(escaped_commit_msg);
385 2c251c14 2020-01-15 tracey return error;
386 2c251c14 2020-01-15 tracey }
387 2c251c14 2020-01-15 tracey
388 2c251c14 2020-01-15 tracey static const struct got_error *
389 e46f587c 2020-01-29 tracey gw_blob(struct gw_trans *gw_trans)
390 e46f587c 2020-01-29 tracey {
391 e46f587c 2020-01-29 tracey const struct got_error *error = NULL;
392 e46f587c 2020-01-29 tracey struct gw_header *header = NULL;
393 5894d523 2020-02-03 stsp char *content = NULL;
394 5894d523 2020-02-03 stsp size_t filesize = 0;
395 e46f587c 2020-01-29 tracey enum kcgi_err kerr;
396 e46f587c 2020-01-29 tracey
397 e46f587c 2020-01-29 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
398 e46f587c 2020-01-29 tracey NULL) == -1)
399 e46f587c 2020-01-29 tracey return got_error_from_errno("pledge");
400 e46f587c 2020-01-29 tracey
401 e46f587c 2020-01-29 tracey if ((header = gw_init_header()) == NULL)
402 e46f587c 2020-01-29 tracey return got_error_from_errno("malloc");
403 e46f587c 2020-01-29 tracey
404 e46f587c 2020-01-29 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
405 e46f587c 2020-01-29 tracey if (error)
406 e46f587c 2020-01-29 tracey goto done;
407 e46f587c 2020-01-29 tracey
408 e46f587c 2020-01-29 tracey error = gw_get_header(gw_trans, header, 1);
409 e46f587c 2020-01-29 tracey if (error)
410 e46f587c 2020-01-29 tracey goto done;
411 e46f587c 2020-01-29 tracey
412 5894d523 2020-02-03 stsp error = gw_get_file_read_blob(&content, &filesize, gw_trans);
413 a81f3554 2020-02-03 stsp if (error)
414 a81f3554 2020-02-03 stsp goto done;
415 e46f587c 2020-01-29 tracey
416 5894d523 2020-02-03 stsp if (isbinary(content, filesize))
417 5894d523 2020-02-03 stsp gw_trans->mime = KMIME_APP_OCTET_STREAM;
418 5894d523 2020-02-03 stsp else
419 5894d523 2020-02-03 stsp gw_trans->mime = KMIME_TEXT_PLAIN;
420 5894d523 2020-02-03 stsp
421 5894d523 2020-02-03 stsp error = gw_display_index(gw_trans);
422 5894d523 2020-02-03 stsp if (error)
423 017d6da3 2020-01-29 tracey goto done;
424 5894d523 2020-02-03 stsp
425 5894d523 2020-02-03 stsp kerr = khttp_write(gw_trans->gw_req, content, filesize);
426 5894d523 2020-02-03 stsp if (kerr != KCGI_OK)
427 5894d523 2020-02-03 stsp error = gw_kcgi_error(kerr);
428 e46f587c 2020-01-29 tracey done:
429 e46f587c 2020-01-29 tracey got_ref_list_free(&header->refs);
430 e46f587c 2020-01-29 tracey gw_free_headers(header);
431 5894d523 2020-02-03 stsp free(content);
432 e46f587c 2020-01-29 tracey return error;
433 e46f587c 2020-01-29 tracey }
434 e46f587c 2020-01-29 tracey
435 e46f587c 2020-01-29 tracey static const struct got_error *
436 f2f46662 2020-01-23 tracey gw_diff(struct gw_trans *gw_trans)
437 2c251c14 2020-01-15 tracey {
438 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
439 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
440 f2f46662 2020-01-23 tracey char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
441 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
442 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
443 2c251c14 2020-01-15 tracey
444 f2f46662 2020-01-23 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
445 f2f46662 2020-01-23 tracey NULL) == -1)
446 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
447 6bee13de 2020-01-16 tracey
448 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
449 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
450 f2f46662 2020-01-23 tracey
451 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
452 8087c3c5 2020-01-15 tracey if (error)
453 390d412c 2020-01-28 stsp goto done;
454 8087c3c5 2020-01-15 tracey
455 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
456 f2f46662 2020-01-23 tracey if (error)
457 390d412c 2020-01-28 stsp goto done;
458 2c251c14 2020-01-15 tracey
459 83eb9a68 2020-02-03 stsp error = gw_get_diff(&diff_html, gw_trans, header);
460 83eb9a68 2020-02-03 stsp if (error)
461 83eb9a68 2020-02-03 stsp goto done;
462 87f9ebf5 2020-01-15 tracey
463 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, header->committer_time, TM_LONG);
464 55ccf528 2020-02-03 stsp if (error)
465 55ccf528 2020-02-03 stsp goto done;
466 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
467 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
468 55ccf528 2020-02-03 stsp goto done;
469 55ccf528 2020-02-03 stsp }
470 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
471 070fee27 2020-02-03 stsp if (error)
472 070fee27 2020-02-03 stsp goto done;
473 390d412c 2020-01-28 stsp if (asprintf(&diff_html_disp, diff_header,
474 2ac037ec 2020-01-24 tracey gw_gen_diff_header(header->parent_id, header->commit_id),
475 f2f46662 2020-01-23 tracey gw_gen_commit_header(header->commit_id, header->refs_str),
476 f2f46662 2020-01-23 tracey gw_gen_tree_header(header->tree_id),
477 f2f46662 2020-01-23 tracey gw_gen_author_header(header->author),
478 55ccf528 2020-02-03 stsp gw_gen_committer_header(header->committer), age_html,
479 070fee27 2020-02-03 stsp gw_gen_commit_msg_header(escaped_commit_msg),
480 83eb9a68 2020-02-03 stsp diff_html ? diff_html : "") == -1) {
481 390d412c 2020-01-28 stsp error = got_error_from_errno("asprintf");
482 390d412c 2020-01-28 stsp goto done;
483 390d412c 2020-01-28 stsp }
484 2204c934 2020-01-15 tracey
485 390d412c 2020-01-28 stsp if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
486 390d412c 2020-01-28 stsp error = got_error_from_errno("asprintf");
487 390d412c 2020-01-28 stsp goto done;
488 390d412c 2020-01-28 stsp }
489 87f9ebf5 2020-01-15 tracey
490 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, diff);
491 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
492 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
493 390d412c 2020-01-28 stsp done:
494 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
495 f2f46662 2020-01-23 tracey gw_free_headers(header);
496 f2f46662 2020-01-23 tracey free(diff_html_disp);
497 f2f46662 2020-01-23 tracey free(diff_html);
498 f2f46662 2020-01-23 tracey free(diff);
499 55ccf528 2020-02-03 stsp free(age);
500 55ccf528 2020-02-03 stsp free(age_html);
501 070fee27 2020-02-03 stsp free(escaped_commit_msg);
502 2204c934 2020-01-15 tracey return error;
503 2204c934 2020-01-15 tracey }
504 2204c934 2020-01-15 tracey
505 2204c934 2020-01-15 tracey static const struct got_error *
506 54415d85 2020-01-15 tracey gw_index(struct gw_trans *gw_trans)
507 2c251c14 2020-01-15 tracey {
508 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
509 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
510 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
511 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
512 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
513 2c251c14 2020-01-15 tracey
514 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
515 6bee13de 2020-01-16 tracey NULL) == -1) {
516 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
517 6bee13de 2020-01-16 tracey return error;
518 6bee13de 2020-01-16 tracey }
519 6bee13de 2020-01-16 tracey
520 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
521 46b9c89b 2020-01-15 tracey if (error)
522 46b9c89b 2020-01-15 tracey return error;
523 46b9c89b 2020-01-15 tracey
524 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
525 46b9c89b 2020-01-15 tracey if (error)
526 2c251c14 2020-01-15 tracey return error;
527 2c251c14 2020-01-15 tracey
528 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
529 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
530 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
531 2c251c14 2020-01-15 tracey
532 bce5dac1 2020-01-28 stsp if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
533 bce5dac1 2020-01-28 stsp if (asprintf(&html, index_projects_empty,
534 bce5dac1 2020-01-28 stsp gw_trans->gw_conf->got_repos_path) == -1)
535 bce5dac1 2020-01-28 stsp return got_error_from_errno("asprintf");
536 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
537 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
538 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
539 bce5dac1 2020-01-28 stsp free(html);
540 c25c2314 2020-01-28 stsp return error;
541 bce5dac1 2020-01-28 stsp }
542 bce5dac1 2020-01-28 stsp
543 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
544 2c251c14 2020-01-15 tracey dir_c++;
545 2c251c14 2020-01-15 tracey
546 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
547 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
548 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
549 2c251c14 2020-01-15 tracey prev_disp++;
550 2c251c14 2020-01-15 tracey continue;
551 2c251c14 2020-01-15 tracey }
552 2c251c14 2020-01-15 tracey
553 2c251c14 2020-01-15 tracey prev_disp++;
554 f2f46662 2020-01-23 tracey
555 f2f46662 2020-01-23 tracey if (error)
556 f2f46662 2020-01-23 tracey return error;
557 88d59c36 2020-01-29 stsp if(asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
558 88d59c36 2020-01-29 stsp gw_dir->name, gw_dir->name) == -1)
559 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
560 2c251c14 2020-01-15 tracey
561 88d59c36 2020-01-29 stsp if (asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
562 9a1cc63f 2020-02-03 stsp gw_dir->description, gw_dir->owner ? gw_dir->owner : "",
563 9a1cc63f 2020-02-03 stsp gw_dir->age,
564 88d59c36 2020-01-29 stsp navs) == -1)
565 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
566 2c251c14 2020-01-15 tracey
567 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, html);
568 2c251c14 2020-01-15 tracey free(navs);
569 2c251c14 2020-01-15 tracey free(html);
570 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
571 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
572 2c251c14 2020-01-15 tracey
573 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
574 2c251c14 2020-01-15 tracey continue;
575 2c251c14 2020-01-15 tracey
576 c25c2314 2020-01-28 stsp if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
577 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
578 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
579 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
580 c25c2314 2020-01-28 stsp } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
581 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
582 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
583 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
584 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
585 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
586 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
587 c25c2314 2020-01-28 stsp }
588 2c251c14 2020-01-15 tracey
589 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
590 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
591 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
592 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
593 88d59c36 2020-01-29 stsp if (asprintf(&prev, nav_prev, gw_trans->page - 1) == -1)
594 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
595 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, prev);
596 2c251c14 2020-01-15 tracey free(prev);
597 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
598 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
599 2c251c14 2020-01-15 tracey }
600 2c251c14 2020-01-15 tracey
601 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
602 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
603 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
604 2c251c14 2020-01-15 tracey
605 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
606 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
607 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
608 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
609 88d59c36 2020-01-29 stsp if (asprintf(&next, nav_next, gw_trans->page + 1) == -1)
610 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
611 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, next);
612 2c251c14 2020-01-15 tracey free(next);
613 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
614 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
615 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
616 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
617 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
618 2c251c14 2020-01-15 tracey next_disp = 0;
619 2c251c14 2020-01-15 tracey break;
620 2c251c14 2020-01-15 tracey }
621 2c251c14 2020-01-15 tracey
622 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
623 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
624 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
625 c25c2314 2020-01-28 stsp prev_disp == gw_trans->repos_total)) {
626 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
627 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
628 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
629 c25c2314 2020-01-28 stsp }
630 2c251c14 2020-01-15 tracey
631 2c251c14 2020-01-15 tracey next_disp++;
632 2c251c14 2020-01-15 tracey }
633 2c251c14 2020-01-15 tracey return error;
634 2c251c14 2020-01-15 tracey }
635 2c251c14 2020-01-15 tracey
636 2c251c14 2020-01-15 tracey static const struct got_error *
637 f2f46662 2020-01-23 tracey gw_commits(struct gw_trans *gw_trans)
638 2c251c14 2020-01-15 tracey {
639 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
640 f2f46662 2020-01-23 tracey char *commits_html, *commits_navs_html;
641 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
642 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
643 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
644 6bee13de 2020-01-16 tracey
645 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
646 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
647 f2f46662 2020-01-23 tracey
648 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
649 6bee13de 2020-01-16 tracey NULL) == -1) {
650 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
651 5ddf0079 2020-01-29 stsp goto done;
652 6bee13de 2020-01-16 tracey }
653 8087c3c5 2020-01-15 tracey
654 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
655 8087c3c5 2020-01-15 tracey if (error)
656 5ddf0079 2020-01-29 stsp goto done;
657 2c251c14 2020-01-15 tracey
658 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
659 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
660 18da9978 2020-01-29 stsp if (error)
661 18da9978 2020-01-29 stsp goto done;
662 4ceb8155 2020-01-15 tracey
663 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
664 18da9978 2020-01-29 stsp if (kerr != KCGI_OK) {
665 18da9978 2020-01-29 stsp error = gw_kcgi_error(kerr);
666 18da9978 2020-01-29 stsp goto done;
667 18da9978 2020-01-29 stsp }
668 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
669 88d59c36 2020-01-29 stsp if (asprintf(&commits_navs_html, commits_navs,
670 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
671 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
672 88d59c36 2020-01-29 stsp gw_trans->repo_name, n_header->commit_id) == -1) {
673 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
674 18da9978 2020-01-29 stsp goto done;
675 18da9978 2020-01-29 stsp }
676 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, n_header->committer_time,
677 55ccf528 2020-02-03 stsp TM_LONG);
678 55ccf528 2020-02-03 stsp if (error)
679 55ccf528 2020-02-03 stsp goto done;
680 070fee27 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "")
681 070fee27 2020-02-03 stsp == -1) {
682 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
683 55ccf528 2020-02-03 stsp goto done;
684 55ccf528 2020-02-03 stsp }
685 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg,
686 070fee27 2020-02-03 stsp n_header->commit_msg);
687 070fee27 2020-02-03 stsp if (error)
688 070fee27 2020-02-03 stsp goto done;
689 88d59c36 2020-01-29 stsp if (asprintf(&commits_html, commits_line,
690 e46f587c 2020-01-29 tracey gw_gen_commit_header(n_header->commit_id,
691 f2f46662 2020-01-23 tracey n_header->refs_str),
692 e46f587c 2020-01-29 tracey gw_gen_author_header(n_header->author),
693 e46f587c 2020-01-29 tracey gw_gen_committer_header(n_header->committer),
694 070fee27 2020-02-03 stsp age_html, escaped_commit_msg,
695 88d59c36 2020-01-29 stsp commits_navs_html) == -1) {
696 18da9978 2020-01-29 stsp error = got_error_from_errno("asprintf");
697 18da9978 2020-01-29 stsp goto done;
698 18da9978 2020-01-29 stsp }
699 55ccf528 2020-02-03 stsp free(age);
700 55ccf528 2020-02-03 stsp age = NULL;
701 55ccf528 2020-02-03 stsp free(age_html);
702 55ccf528 2020-02-03 stsp age_html = NULL;
703 070fee27 2020-02-03 stsp free(escaped_commit_msg);
704 070fee27 2020-02-03 stsp escaped_commit_msg = NULL;
705 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, commits_html);
706 18da9978 2020-01-29 stsp if (kerr != KCGI_OK) {
707 18da9978 2020-01-29 stsp error = gw_kcgi_error(kerr);
708 18da9978 2020-01-29 stsp goto done;
709 18da9978 2020-01-29 stsp }
710 f2f46662 2020-01-23 tracey }
711 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
712 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
713 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
714 18da9978 2020-01-29 stsp done:
715 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
716 f2f46662 2020-01-23 tracey gw_free_headers(header);
717 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
718 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
719 55ccf528 2020-02-03 stsp free(age);
720 55ccf528 2020-02-03 stsp free(age_html);
721 070fee27 2020-02-03 stsp free(escaped_commit_msg);
722 2c251c14 2020-01-15 tracey return error;
723 2c251c14 2020-01-15 tracey }
724 2c251c14 2020-01-15 tracey
725 2c251c14 2020-01-15 tracey static const struct got_error *
726 f2f46662 2020-01-23 tracey gw_briefs(struct gw_trans *gw_trans)
727 2c251c14 2020-01-15 tracey {
728 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
729 f2f46662 2020-01-23 tracey char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
730 f2f46662 2020-01-23 tracey struct gw_header *header = NULL, *n_header = NULL;
731 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
732 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
733 17a96b9f 2020-01-15 tracey
734 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
735 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
736 f2f46662 2020-01-23 tracey
737 6bee13de 2020-01-16 tracey if (pledge("stdio rpath proc exec sendfd unveil",
738 6bee13de 2020-01-16 tracey NULL) == -1) {
739 6bee13de 2020-01-16 tracey error = got_error_from_errno("pledge");
740 5ddf0079 2020-01-29 stsp goto done;
741 6bee13de 2020-01-16 tracey }
742 6bee13de 2020-01-16 tracey
743 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
744 8087c3c5 2020-01-15 tracey if (error)
745 5ddf0079 2020-01-29 stsp goto done;
746 9d84e7dd 2020-01-15 tracey
747 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_SUMMARY)
748 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
749 f2f46662 2020-01-23 tracey else
750 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header,
751 f2f46662 2020-01-23 tracey gw_trans->gw_conf->got_max_commits_display);
752 c25c2314 2020-01-28 stsp if (error)
753 0e00e8f4 2020-01-29 stsp goto done;
754 c25c2314 2020-01-28 stsp
755 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
756 0e00e8f4 2020-01-29 stsp if (kerr != KCGI_OK) {
757 0e00e8f4 2020-01-29 stsp error = gw_kcgi_error(kerr);
758 0e00e8f4 2020-01-29 stsp goto done;
759 0e00e8f4 2020-01-29 stsp }
760 c25c2314 2020-01-28 stsp
761 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
762 88d59c36 2020-01-29 stsp if (asprintf(&briefs_navs_html, briefs_navs,
763 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
764 f2f46662 2020-01-23 tracey gw_trans->repo_name, n_header->commit_id,
765 88d59c36 2020-01-29 stsp gw_trans->repo_name, n_header->commit_id) == -1) {
766 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
767 0e00e8f4 2020-01-29 stsp goto done;
768 0e00e8f4 2020-01-29 stsp }
769 f2f46662 2020-01-23 tracey newline = strchr(n_header->commit_msg, '\n');
770 f2f46662 2020-01-23 tracey if (newline)
771 f2f46662 2020-01-23 tracey *newline = '\0';
772 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, n_header->committer_time,
773 55ccf528 2020-02-03 stsp TM_DIFF);
774 55ccf528 2020-02-03 stsp if (error)
775 55ccf528 2020-02-03 stsp goto done;
776 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
777 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
778 55ccf528 2020-02-03 stsp goto done;
779 55ccf528 2020-02-03 stsp }
780 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg,
781 070fee27 2020-02-03 stsp n_header->commit_msg);
782 070fee27 2020-02-03 stsp if (error)
783 070fee27 2020-02-03 stsp goto done;
784 55ccf528 2020-02-03 stsp if (asprintf(&briefs_html, briefs_line, age_html,
785 070fee27 2020-02-03 stsp n_header->author, escaped_commit_msg,
786 88d59c36 2020-01-29 stsp briefs_navs_html) == -1) {
787 0e00e8f4 2020-01-29 stsp error = got_error_from_errno("asprintf");
788 0e00e8f4 2020-01-29 stsp goto done;
789 0e00e8f4 2020-01-29 stsp }
790 55ccf528 2020-02-03 stsp free(age);
791 55ccf528 2020-02-03 stsp age = NULL;
792 55ccf528 2020-02-03 stsp free(age_html);
793 55ccf528 2020-02-03 stsp age_html = NULL;
794 070fee27 2020-02-03 stsp free(escaped_commit_msg);
795 070fee27 2020-02-03 stsp escaped_commit_msg = NULL;
796 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, briefs_html);
797 0e00e8f4 2020-01-29 stsp if (kerr != KCGI_OK) {
798 0e00e8f4 2020-01-29 stsp error = gw_kcgi_error(kerr);
799 0e00e8f4 2020-01-29 stsp goto done;
800 0e00e8f4 2020-01-29 stsp }
801 9d84e7dd 2020-01-15 tracey }
802 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
803 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
804 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
805 0e00e8f4 2020-01-29 stsp done:
806 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
807 f2f46662 2020-01-23 tracey gw_free_headers(header);
808 f2f46662 2020-01-23 tracey TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
809 f2f46662 2020-01-23 tracey gw_free_headers(n_header);
810 55ccf528 2020-02-03 stsp free(age);
811 55ccf528 2020-02-03 stsp free(age_html);
812 070fee27 2020-02-03 stsp free(escaped_commit_msg);
813 2c251c14 2020-01-15 tracey return error;
814 2c251c14 2020-01-15 tracey }
815 2c251c14 2020-01-15 tracey
816 2c251c14 2020-01-15 tracey static const struct got_error *
817 54415d85 2020-01-15 tracey gw_summary(struct gw_trans *gw_trans)
818 387a29ba 2020-01-15 tracey {
819 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
820 20f34652 2020-01-29 stsp char *description_html = NULL, *repo_owner_html = NULL;
821 20f34652 2020-01-29 stsp char *age = NULL, *repo_age_html = NULL, *cloneurl_html = NULL;
822 20f34652 2020-01-29 stsp char *tags = NULL, *tags_html = NULL;
823 20f34652 2020-01-29 stsp char *heads = NULL, *heads_html = NULL;
824 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
825 387a29ba 2020-01-15 tracey
826 f4df82f9 2020-01-29 stsp if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
827 f4df82f9 2020-01-29 stsp return got_error_from_errno("pledge");
828 6bee13de 2020-01-16 tracey
829 f2f46662 2020-01-23 tracey /* unveil is applied with gw_briefs below */
830 46b9c89b 2020-01-15 tracey
831 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
832 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
833 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
834 c25c2314 2020-01-28 stsp
835 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
836 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
837 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
838 88d59c36 2020-01-29 stsp if (asprintf(&description_html, description,
839 20f34652 2020-01-29 stsp gw_trans->gw_dir->description) == -1) {
840 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
841 20f34652 2020-01-29 stsp goto done;
842 20f34652 2020-01-29 stsp }
843 46b9c89b 2020-01-15 tracey
844 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, description_html);
845 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
846 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
847 20f34652 2020-01-29 stsp goto done;
848 20f34652 2020-01-29 stsp }
849 46b9c89b 2020-01-15 tracey }
850 46b9c89b 2020-01-15 tracey }
851 46b9c89b 2020-01-15 tracey
852 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner &&
853 9a1cc63f 2020-02-03 stsp gw_trans->gw_dir->owner != NULL) {
854 9a1cc63f 2020-02-03 stsp if (asprintf(&repo_owner_html, repo_owner,
855 9a1cc63f 2020-02-03 stsp gw_trans->gw_dir->owner) == -1) {
856 9a1cc63f 2020-02-03 stsp error = got_error_from_errno("asprintf");
857 9a1cc63f 2020-02-03 stsp goto done;
858 9a1cc63f 2020-02-03 stsp }
859 46b9c89b 2020-01-15 tracey
860 9a1cc63f 2020-02-03 stsp kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
861 9a1cc63f 2020-02-03 stsp if (kerr != KCGI_OK) {
862 9a1cc63f 2020-02-03 stsp error = gw_kcgi_error(kerr);
863 9a1cc63f 2020-02-03 stsp goto done;
864 46b9c89b 2020-01-15 tracey }
865 46b9c89b 2020-01-15 tracey }
866 46b9c89b 2020-01-15 tracey
867 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
868 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
869 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
870 d126c1b7 2020-01-29 stsp if (error)
871 20f34652 2020-01-29 stsp goto done;
872 55ccf528 2020-02-03 stsp if (age != NULL) {
873 20f34652 2020-01-29 stsp if (asprintf(&repo_age_html, last_change, age) == -1) {
874 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
875 20f34652 2020-01-29 stsp goto done;
876 20f34652 2020-01-29 stsp }
877 46b9c89b 2020-01-15 tracey
878 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
879 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
880 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
881 20f34652 2020-01-29 stsp goto done;
882 20f34652 2020-01-29 stsp }
883 46b9c89b 2020-01-15 tracey }
884 46b9c89b 2020-01-15 tracey }
885 46b9c89b 2020-01-15 tracey
886 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
887 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
888 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
889 88d59c36 2020-01-29 stsp if (asprintf(&cloneurl_html, cloneurl,
890 20f34652 2020-01-29 stsp gw_trans->gw_dir->url) == -1) {
891 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
892 20f34652 2020-01-29 stsp goto done;
893 20f34652 2020-01-29 stsp }
894 46b9c89b 2020-01-15 tracey
895 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
896 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
897 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
898 20f34652 2020-01-29 stsp goto done;
899 20f34652 2020-01-29 stsp }
900 46b9c89b 2020-01-15 tracey }
901 46b9c89b 2020-01-15 tracey }
902 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, div_end);
903 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
904 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
905 20f34652 2020-01-29 stsp goto done;
906 20f34652 2020-01-29 stsp }
907 46b9c89b 2020-01-15 tracey
908 f2f46662 2020-01-23 tracey error = gw_briefs(gw_trans);
909 f2f46662 2020-01-23 tracey if (error)
910 20f34652 2020-01-29 stsp goto done;
911 f2f46662 2020-01-23 tracey
912 6eccd105 2020-02-03 stsp error = gw_get_repo_tags(&tags, gw_trans, NULL, D_MAXSLCOMMDISP,
913 6eccd105 2020-02-03 stsp TAGBRIEF);
914 6eccd105 2020-02-03 stsp if (error)
915 6eccd105 2020-02-03 stsp goto done;
916 6eccd105 2020-02-03 stsp
917 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
918 387a29ba 2020-01-15 tracey
919 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
920 20f34652 2020-01-29 stsp if (asprintf(&tags_html, summary_tags, tags) == -1) {
921 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
922 20f34652 2020-01-29 stsp goto done;
923 20f34652 2020-01-29 stsp }
924 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tags_html);
925 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
926 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
927 20f34652 2020-01-29 stsp goto done;
928 20f34652 2020-01-29 stsp }
929 8d4d2453 2020-01-15 tracey }
930 8d4d2453 2020-01-15 tracey
931 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
932 20f34652 2020-01-29 stsp if (asprintf(&heads_html, summary_heads, heads) == -1) {
933 20f34652 2020-01-29 stsp error = got_error_from_errno("asprintf");
934 20f34652 2020-01-29 stsp goto done;
935 20f34652 2020-01-29 stsp }
936 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, heads_html);
937 20f34652 2020-01-29 stsp if (kerr != KCGI_OK) {
938 20f34652 2020-01-29 stsp error = gw_kcgi_error(kerr);
939 20f34652 2020-01-29 stsp goto done;
940 20f34652 2020-01-29 stsp }
941 8d4d2453 2020-01-15 tracey }
942 20f34652 2020-01-29 stsp done:
943 20f34652 2020-01-29 stsp free(description_html);
944 20f34652 2020-01-29 stsp free(repo_owner_html);
945 20f34652 2020-01-29 stsp free(age);
946 20f34652 2020-01-29 stsp free(repo_age_html);
947 20f34652 2020-01-29 stsp free(cloneurl_html);
948 20f34652 2020-01-29 stsp free(tags);
949 20f34652 2020-01-29 stsp free(tags_html);
950 20f34652 2020-01-29 stsp free(heads);
951 20f34652 2020-01-29 stsp free(heads_html);
952 2204c934 2020-01-15 tracey return error;
953 2204c934 2020-01-15 tracey }
954 2204c934 2020-01-15 tracey
955 2204c934 2020-01-15 tracey static const struct got_error *
956 f2f46662 2020-01-23 tracey gw_tree(struct gw_trans *gw_trans)
957 b772de24 2020-01-15 tracey {
958 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
959 f2f46662 2020-01-23 tracey struct gw_header *header = NULL;
960 f2f46662 2020-01-23 tracey char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
961 070fee27 2020-02-03 stsp char *age = NULL, *age_html = NULL, *escaped_commit_msg = NULL;
962 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
963 6bee13de 2020-01-16 tracey
964 f2f46662 2020-01-23 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
965 f2f46662 2020-01-23 tracey return got_error_from_errno("pledge");
966 b772de24 2020-01-15 tracey
967 ae36ed87 2020-01-28 stsp if ((header = gw_init_header()) == NULL)
968 f2f46662 2020-01-23 tracey return got_error_from_errno("malloc");
969 f2f46662 2020-01-23 tracey
970 54415d85 2020-01-15 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
971 b772de24 2020-01-15 tracey if (error)
972 5ddf0079 2020-01-29 stsp goto done;
973 b772de24 2020-01-15 tracey
974 f2f46662 2020-01-23 tracey error = gw_get_header(gw_trans, header, 1);
975 f2f46662 2020-01-23 tracey if (error)
976 42281175 2020-01-29 stsp goto done;
977 b772de24 2020-01-15 tracey
978 84bf4df2 2020-02-03 stsp error = gw_get_repo_tree(&tree_html, gw_trans);
979 84bf4df2 2020-02-03 stsp if (error)
980 84bf4df2 2020-02-03 stsp goto done;
981 6bee13de 2020-01-16 tracey
982 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, header->committer_time, TM_LONG);
983 55ccf528 2020-02-03 stsp if (error)
984 55ccf528 2020-02-03 stsp goto done;
985 55ccf528 2020-02-03 stsp if (asprintf(&age_html, header_age_html, age ? age : "") == -1) {
986 55ccf528 2020-02-03 stsp error = got_error_from_errno("asprintf");
987 55ccf528 2020-02-03 stsp goto done;
988 55ccf528 2020-02-03 stsp }
989 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
990 070fee27 2020-02-03 stsp if (error)
991 070fee27 2020-02-03 stsp goto done;
992 55ccf528 2020-02-03 stsp if (asprintf(&tree_html_disp, tree_header, age_html,
993 070fee27 2020-02-03 stsp gw_gen_commit_msg_header(escaped_commit_msg),
994 84bf4df2 2020-02-03 stsp tree_html ? tree_html : "") == -1) {
995 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
996 42281175 2020-01-29 stsp goto done;
997 42281175 2020-01-29 stsp }
998 8087c3c5 2020-01-15 tracey
999 42281175 2020-01-29 stsp if (asprintf(&tree, tree_wrapper, tree_html_disp) == -1) {
1000 42281175 2020-01-29 stsp error = got_error_from_errno("asprintf");
1001 42281175 2020-01-29 stsp goto done;
1002 42281175 2020-01-29 stsp }
1003 8087c3c5 2020-01-15 tracey
1004 c25c2314 2020-01-28 stsp kerr = khttp_puts(gw_trans->gw_req, tree);
1005 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1006 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
1007 4ff7391f 2020-01-28 tracey done:
1008 2dcb56a8 2020-01-28 stsp got_ref_list_free(&header->refs);
1009 f2f46662 2020-01-23 tracey gw_free_headers(header);
1010 f2f46662 2020-01-23 tracey free(tree_html_disp);
1011 f2f46662 2020-01-23 tracey free(tree_html);
1012 f2f46662 2020-01-23 tracey free(tree);
1013 55ccf528 2020-02-03 stsp free(age);
1014 55ccf528 2020-02-03 stsp free(age_html);
1015 070fee27 2020-02-03 stsp free(escaped_commit_msg);
1016 4ff7391f 2020-01-28 tracey return error;
1017 4ff7391f 2020-01-28 tracey }
1018 4ff7391f 2020-01-28 tracey
1019 4ff7391f 2020-01-28 tracey static const struct got_error *
1020 4ff7391f 2020-01-28 tracey gw_tag(struct gw_trans *gw_trans)
1021 4ff7391f 2020-01-28 tracey {
1022 4ff7391f 2020-01-28 tracey const struct got_error *error = NULL;
1023 4ff7391f 2020-01-28 tracey struct gw_header *header = NULL;
1024 4ff7391f 2020-01-28 tracey char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
1025 070fee27 2020-02-03 stsp char *escaped_commit_msg = NULL;
1026 4ff7391f 2020-01-28 tracey enum kcgi_err kerr;
1027 4ff7391f 2020-01-28 tracey
1028 4ff7391f 2020-01-28 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
1029 4ff7391f 2020-01-28 tracey return got_error_from_errno("pledge");
1030 4ff7391f 2020-01-28 tracey
1031 4ff7391f 2020-01-28 tracey if ((header = gw_init_header()) == NULL)
1032 4ff7391f 2020-01-28 tracey return got_error_from_errno("malloc");
1033 4ff7391f 2020-01-28 tracey
1034 4ff7391f 2020-01-28 tracey error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
1035 4ff7391f 2020-01-28 tracey if (error)
1036 5ddf0079 2020-01-29 stsp goto done;
1037 4ff7391f 2020-01-28 tracey
1038 4ff7391f 2020-01-28 tracey error = gw_get_header(gw_trans, header, 1);
1039 4ff7391f 2020-01-28 tracey if (error)
1040 470cd826 2020-01-29 stsp goto done;
1041 4ff7391f 2020-01-28 tracey
1042 6eccd105 2020-02-03 stsp error = gw_get_repo_tags(&tag_html, gw_trans, header, 1, TAGFULL);
1043 6eccd105 2020-02-03 stsp if (error)
1044 6eccd105 2020-02-03 stsp goto done;
1045 4ff7391f 2020-01-28 tracey
1046 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_commit_msg, header->commit_msg);
1047 070fee27 2020-02-03 stsp if (error)
1048 070fee27 2020-02-03 stsp goto done;
1049 88d59c36 2020-01-29 stsp if (asprintf(&tag_html_disp, tag_header,
1050 4ff7391f 2020-01-28 tracey gw_gen_commit_header(header->commit_id, header->refs_str),
1051 070fee27 2020-02-03 stsp gw_gen_commit_msg_header(escaped_commit_msg),
1052 6eccd105 2020-02-03 stsp tag_html ? tag_html : "") == -1) {
1053 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
1054 470cd826 2020-01-29 stsp goto done;
1055 470cd826 2020-01-29 stsp }
1056 4ff7391f 2020-01-28 tracey
1057 470cd826 2020-01-29 stsp if (asprintf(&tag, tag_wrapper, tag_html_disp) == -1) {
1058 470cd826 2020-01-29 stsp error = got_error_from_errno("asprintf");
1059 470cd826 2020-01-29 stsp goto done;
1060 470cd826 2020-01-29 stsp }
1061 4ff7391f 2020-01-28 tracey
1062 4ff7391f 2020-01-28 tracey kerr = khttp_puts(gw_trans->gw_req, tag);
1063 4ff7391f 2020-01-28 tracey if (kerr != KCGI_OK)
1064 4ff7391f 2020-01-28 tracey error = gw_kcgi_error(kerr);
1065 4ff7391f 2020-01-28 tracey done:
1066 4ff7391f 2020-01-28 tracey got_ref_list_free(&header->refs);
1067 4ff7391f 2020-01-28 tracey gw_free_headers(header);
1068 4ff7391f 2020-01-28 tracey free(tag_html_disp);
1069 4ff7391f 2020-01-28 tracey free(tag_html);
1070 4ff7391f 2020-01-28 tracey free(tag);
1071 070fee27 2020-02-03 stsp free(escaped_commit_msg);
1072 2c251c14 2020-01-15 tracey return error;
1073 2c251c14 2020-01-15 tracey }
1074 2c251c14 2020-01-15 tracey
1075 2c251c14 2020-01-15 tracey static const struct got_error *
1076 54415d85 2020-01-15 tracey gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
1077 2c251c14 2020-01-15 tracey {
1078 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1079 2c251c14 2020-01-15 tracey DIR *dt;
1080 2c251c14 2020-01-15 tracey char *dir_test;
1081 4ceb8155 2020-01-15 tracey int opened = 0;
1082 2c251c14 2020-01-15 tracey
1083 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1084 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1085 88d59c36 2020-01-29 stsp GOTWEB_GIT_DIR) == -1)
1086 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1087 2c251c14 2020-01-15 tracey
1088 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1089 2c251c14 2020-01-15 tracey if (dt == NULL) {
1090 2c251c14 2020-01-15 tracey free(dir_test);
1091 2c251c14 2020-01-15 tracey } else {
1092 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1093 4ceb8155 2020-01-15 tracey opened = 1;
1094 2c251c14 2020-01-15 tracey goto done;
1095 2c251c14 2020-01-15 tracey }
1096 2c251c14 2020-01-15 tracey
1097 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s/%s",
1098 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
1099 88d59c36 2020-01-29 stsp GOTWEB_GOT_DIR) == -1)
1100 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1101 2c251c14 2020-01-15 tracey
1102 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
1103 2c251c14 2020-01-15 tracey if (dt == NULL)
1104 2c251c14 2020-01-15 tracey free(dir_test);
1105 2c251c14 2020-01-15 tracey else {
1106 4ceb8155 2020-01-15 tracey opened = 1;
1107 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
1108 2c251c14 2020-01-15 tracey goto errored;
1109 2c251c14 2020-01-15 tracey }
1110 2c251c14 2020-01-15 tracey
1111 88d59c36 2020-01-29 stsp if (asprintf(&dir_test, "%s/%s",
1112 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, gw_dir->name) == -1)
1113 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1114 2c251c14 2020-01-15 tracey
1115 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
1116 2c251c14 2020-01-15 tracey
1117 2c251c14 2020-01-15 tracey done:
1118 32cd4d18 2020-02-03 stsp error = gw_get_repo_description(&gw_dir->description, gw_trans,
1119 2c251c14 2020-01-15 tracey gw_dir->path);
1120 32cd4d18 2020-02-03 stsp if (error)
1121 32cd4d18 2020-02-03 stsp goto errored;
1122 9a1cc63f 2020-02-03 stsp error = gw_get_repo_owner(&gw_dir->owner, gw_trans, gw_dir->path);
1123 9a1cc63f 2020-02-03 stsp if (error)
1124 9a1cc63f 2020-02-03 stsp goto errored;
1125 d126c1b7 2020-01-29 stsp error = gw_get_repo_age(&gw_dir->age, gw_trans, gw_dir->path,
1126 d126c1b7 2020-01-29 stsp "refs/heads", TM_DIFF);
1127 d126c1b7 2020-01-29 stsp if (error)
1128 d126c1b7 2020-01-29 stsp goto errored;
1129 59d3c40e 2020-02-03 stsp error = gw_get_clone_url(&gw_dir->url, gw_trans, gw_dir->path);
1130 2c251c14 2020-01-15 tracey errored:
1131 2c251c14 2020-01-15 tracey free(dir_test);
1132 2c251c14 2020-01-15 tracey if (opened)
1133 2c251c14 2020-01-15 tracey closedir(dt);
1134 2c251c14 2020-01-15 tracey return error;
1135 2c251c14 2020-01-15 tracey }
1136 2c251c14 2020-01-15 tracey
1137 2c251c14 2020-01-15 tracey static const struct got_error *
1138 54415d85 2020-01-15 tracey gw_load_got_paths(struct gw_trans *gw_trans)
1139 2c251c14 2020-01-15 tracey {
1140 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1141 2c251c14 2020-01-15 tracey DIR *d;
1142 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
1143 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1144 2c251c14 2020-01-15 tracey struct stat st;
1145 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
1146 2c251c14 2020-01-15 tracey
1147 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
1148 2c251c14 2020-01-15 tracey if (d == NULL) {
1149 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
1150 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1151 2c251c14 2020-01-15 tracey return error;
1152 2c251c14 2020-01-15 tracey }
1153 2c251c14 2020-01-15 tracey
1154 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
1155 2c251c14 2020-01-15 tracey alphasort);
1156 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
1157 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
1158 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
1159 2c251c14 2020-01-15 tracey return error;
1160 2c251c14 2020-01-15 tracey }
1161 2c251c14 2020-01-15 tracey
1162 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
1163 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
1164 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
1165 2c251c14 2020-01-15 tracey break; /* account for parent and self */
1166 2c251c14 2020-01-15 tracey
1167 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1168 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
1169 2c251c14 2020-01-15 tracey continue;
1170 2c251c14 2020-01-15 tracey
1171 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1172 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1173 2c251c14 2020-01-15 tracey
1174 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
1175 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1176 2c251c14 2020-01-15 tracey continue;
1177 2c251c14 2020-01-15 tracey else if (error)
1178 2c251c14 2020-01-15 tracey return error;
1179 2c251c14 2020-01-15 tracey
1180 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1181 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
1182 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1183 2c251c14 2020-01-15 tracey entry);
1184 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
1185 2c251c14 2020-01-15 tracey }
1186 2c251c14 2020-01-15 tracey }
1187 2c251c14 2020-01-15 tracey
1188 2c251c14 2020-01-15 tracey closedir(d);
1189 2c251c14 2020-01-15 tracey return error;
1190 2c251c14 2020-01-15 tracey }
1191 2c251c14 2020-01-15 tracey
1192 2c251c14 2020-01-15 tracey static const struct got_error *
1193 54415d85 2020-01-15 tracey gw_parse_querystring(struct gw_trans *gw_trans)
1194 2c251c14 2020-01-15 tracey {
1195 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1196 2c251c14 2020-01-15 tracey struct kpair *p;
1197 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
1198 2c251c14 2020-01-15 tracey unsigned int i;
1199 2c251c14 2020-01-15 tracey
1200 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
1201 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
1202 2c251c14 2020-01-15 tracey return error;
1203 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1204 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
1205 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_name, "%s", p->parsed.s) == -1)
1206 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1207 2c251c14 2020-01-15 tracey
1208 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_path, "%s/%s",
1209 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_repos_path, p->parsed.s) == -1)
1210 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1211 2c251c14 2020-01-15 tracey
1212 2c251c14 2020-01-15 tracey /* get action and set function */
1213 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1214 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
1215 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
1216 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
1217 2c251c14 2020-01-15 tracey continue;
1218 077f6c5a 2020-01-15 tracey
1219 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
1220 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
1221 2c251c14 2020-01-15 tracey gw_trans->action = i;
1222 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->action_name,
1223 88d59c36 2020-01-29 stsp "%s", action->func_name) == -1)
1224 2c251c14 2020-01-15 tracey return
1225 b772de24 2020-01-15 tracey got_error_from_errno(
1226 2c251c14 2020-01-15 tracey "asprintf");
1227 2c251c14 2020-01-15 tracey
1228 2c251c14 2020-01-15 tracey break;
1229 2c251c14 2020-01-15 tracey }
1230 2c251c14 2020-01-15 tracey
1231 2c251c14 2020-01-15 tracey action = NULL;
1232 2c251c14 2020-01-15 tracey }
1233 ec46ccd7 2020-01-15 tracey
1234 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1235 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->commit, "%s",
1236 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1237 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1238 2c251c14 2020-01-15 tracey
1239 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1240 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_file, "%s",
1241 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1242 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
1243 8087c3c5 2020-01-15 tracey
1244 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1245 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->repo_folder, "%s",
1246 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1247 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1248 ec46ccd7 2020-01-15 tracey
1249 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1250 88d59c36 2020-01-29 stsp if (asprintf(&gw_trans->headref, "%s",
1251 88d59c36 2020-01-29 stsp p->parsed.s) == -1)
1252 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1253 2c251c14 2020-01-15 tracey
1254 2c251c14 2020-01-15 tracey if (action == NULL) {
1255 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
1256 2c251c14 2020-01-15 tracey return error;
1257 2c251c14 2020-01-15 tracey }
1258 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
1259 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1260 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1261 46b9c89b 2020-01-15 tracey
1262 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1263 46b9c89b 2020-01-15 tracey if (error)
1264 46b9c89b 2020-01-15 tracey return error;
1265 2c251c14 2020-01-15 tracey } else
1266 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1267 2c251c14 2020-01-15 tracey
1268 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1269 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1270 2c251c14 2020-01-15 tracey
1271 2c251c14 2020-01-15 tracey return error;
1272 2c251c14 2020-01-15 tracey }
1273 2c251c14 2020-01-15 tracey
1274 2c251c14 2020-01-15 tracey static struct gw_dir *
1275 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
1276 2c251c14 2020-01-15 tracey {
1277 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1278 2c251c14 2020-01-15 tracey
1279 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1280 2c251c14 2020-01-15 tracey return NULL;
1281 2c251c14 2020-01-15 tracey
1282 88d59c36 2020-01-29 stsp if (asprintf(&gw_dir->name, "%s", dir) == -1)
1283 2c251c14 2020-01-15 tracey return NULL;
1284 2c251c14 2020-01-15 tracey
1285 2c251c14 2020-01-15 tracey return gw_dir;
1286 474370cb 2020-01-15 tracey }
1287 474370cb 2020-01-15 tracey
1288 c25c2314 2020-01-28 stsp static const struct got_error *
1289 54415d85 2020-01-15 tracey gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1290 2c251c14 2020-01-15 tracey {
1291 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1292 c25c2314 2020-01-28 stsp
1293 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1294 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1295 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1296 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1297 2c251c14 2020-01-15 tracey khttps[code]);
1298 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1299 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1300 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1301 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1302 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1303 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1304 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options",
1305 017d6da3 2020-01-29 tracey "nosniff");
1306 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1307 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1308 c25c2314 2020-01-28 stsp kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1309 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1310 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1311 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection",
1312 017d6da3 2020-01-29 tracey "1; mode=block");
1313 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK)
1314 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1315 c25c2314 2020-01-28 stsp
1316 017d6da3 2020-01-29 tracey if (gw_trans->mime == KMIME_APP_OCTET_STREAM) {
1317 017d6da3 2020-01-29 tracey kerr = khttp_head(gw_trans->gw_req,
1318 017d6da3 2020-01-29 tracey kresps[KRESP_CONTENT_DISPOSITION],
1319 017d6da3 2020-01-29 tracey "attachment; filename=%s", gw_trans->repo_file);
1320 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK)
1321 017d6da3 2020-01-29 tracey return gw_kcgi_error(kerr);
1322 017d6da3 2020-01-29 tracey }
1323 017d6da3 2020-01-29 tracey
1324 c25c2314 2020-01-28 stsp kerr = khttp_body(gw_trans->gw_req);
1325 c25c2314 2020-01-28 stsp return gw_kcgi_error(kerr);
1326 2c251c14 2020-01-15 tracey }
1327 2c251c14 2020-01-15 tracey
1328 c25c2314 2020-01-28 stsp static const struct got_error *
1329 6d8d8a26 2020-01-29 stsp gw_display_index(struct gw_trans *gw_trans)
1330 2c251c14 2020-01-15 tracey {
1331 4bec7539 2020-01-29 stsp const struct got_error *error;
1332 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
1333 c25c2314 2020-01-28 stsp
1334 4bec7539 2020-01-29 stsp error = gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1335 4bec7539 2020-01-29 stsp if (error)
1336 4bec7539 2020-01-29 stsp return error;
1337 4bec7539 2020-01-29 stsp
1338 c25c2314 2020-01-28 stsp kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1339 6d8d8a26 2020-01-29 stsp if (kerr)
1340 6d8d8a26 2020-01-29 stsp return gw_kcgi_error(kerr);
1341 2c251c14 2020-01-15 tracey
1342 017d6da3 2020-01-29 tracey if (gw_trans->action != GW_BLOB) {
1343 017d6da3 2020-01-29 tracey kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1344 017d6da3 2020-01-29 tracey gw_query_funcs[gw_trans->action].template);
1345 017d6da3 2020-01-29 tracey if (kerr != KCGI_OK) {
1346 017d6da3 2020-01-29 tracey khtml_close(gw_trans->gw_html_req);
1347 017d6da3 2020-01-29 tracey return gw_kcgi_error(kerr);
1348 017d6da3 2020-01-29 tracey }
1349 7a9bfbff 2020-01-29 stsp }
1350 2c251c14 2020-01-15 tracey
1351 7a9bfbff 2020-01-29 stsp return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
1352 2c251c14 2020-01-15 tracey }
1353 2c251c14 2020-01-15 tracey
1354 6d8d8a26 2020-01-29 stsp static void
1355 6d8d8a26 2020-01-29 stsp gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
1356 6d8d8a26 2020-01-29 stsp {
1357 6d8d8a26 2020-01-29 stsp if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
1358 6d8d8a26 2020-01-29 stsp return;
1359 6d8d8a26 2020-01-29 stsp
1360 6d8d8a26 2020-01-29 stsp if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
1361 6d8d8a26 2020-01-29 stsp return;
1362 33dc7bd2 2020-02-03 stsp khtml_puts(gw_trans->gw_html_req, err->msg);
1363 6d8d8a26 2020-01-29 stsp khtml_close(gw_trans->gw_html_req);
1364 6d8d8a26 2020-01-29 stsp }
1365 6d8d8a26 2020-01-29 stsp
1366 2c251c14 2020-01-15 tracey static int
1367 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1368 2c251c14 2020-01-15 tracey {
1369 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1370 54415d85 2020-01-15 tracey struct gw_trans *gw_trans = arg;
1371 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
1372 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
1373 2c251c14 2020-01-15 tracey
1374 2c251c14 2020-01-15 tracey switch (key) {
1375 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1376 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
1377 2c251c14 2020-01-15 tracey break;
1378 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1379 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
1380 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
1381 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
1382 2c251c14 2020-01-15 tracey
1383 2c251c14 2020-01-15 tracey free(gw_got_link);
1384 2c251c14 2020-01-15 tracey break;
1385 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1386 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
1387 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
1388 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
1389 2c251c14 2020-01-15 tracey
1390 2c251c14 2020-01-15 tracey free(gw_site_link);
1391 2c251c14 2020-01-15 tracey break;
1392 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1393 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
1394 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
1395 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1396 2c251c14 2020-01-15 tracey
1397 2c251c14 2020-01-15 tracey break;
1398 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1399 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
1400 2c251c14 2020-01-15 tracey break;
1401 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
1402 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
1403 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
1404 070fee27 2020-02-03 stsp error = gw_html_escape(&site_owner_name,
1405 070fee27 2020-02-03 stsp gw_trans->gw_conf->got_site_owner);
1406 070fee27 2020-02-03 stsp if (error)
1407 070fee27 2020-02-03 stsp return 0;
1408 88d59c36 2020-01-29 stsp if (asprintf(&site_owner_name_h, site_owner,
1409 070fee27 2020-02-03 stsp site_owner_name) == -1) {
1410 070fee27 2020-02-03 stsp free(site_owner_name);
1411 2c251c14 2020-01-15 tracey return 0;
1412 070fee27 2020-02-03 stsp }
1413 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
1414 2c251c14 2020-01-15 tracey free(site_owner_name);
1415 2c251c14 2020-01-15 tracey free(site_owner_name_h);
1416 2c251c14 2020-01-15 tracey }
1417 2c251c14 2020-01-15 tracey break;
1418 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1419 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1420 2c251c14 2020-01-15 tracey if (error)
1421 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1422 2c251c14 2020-01-15 tracey break;
1423 2c251c14 2020-01-15 tracey default:
1424 2c251c14 2020-01-15 tracey return 0;
1425 2c251c14 2020-01-15 tracey }
1426 2c251c14 2020-01-15 tracey return 1;
1427 2c251c14 2020-01-15 tracey }
1428 2c251c14 2020-01-15 tracey
1429 2c251c14 2020-01-15 tracey static char *
1430 f2f46662 2020-01-23 tracey gw_gen_commit_header(char *str1, char *str2)
1431 f2f46662 2020-01-23 tracey {
1432 f2f46662 2020-01-23 tracey char *return_html = NULL, *ref_str = NULL;
1433 f2f46662 2020-01-23 tracey
1434 f2f46662 2020-01-23 tracey if (strcmp(str2, "") != 0) {
1435 88d59c36 2020-01-29 stsp if (asprintf(&ref_str, "(%s)", str2) == -1) {
1436 f2f46662 2020-01-23 tracey return_html = strdup("");
1437 f2f46662 2020-01-23 tracey return return_html;
1438 f2f46662 2020-01-23 tracey }
1439 f2f46662 2020-01-23 tracey } else
1440 f2f46662 2020-01-23 tracey ref_str = strdup("");
1441 f2f46662 2020-01-23 tracey
1442 f2f46662 2020-01-23 tracey
1443 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_html, str1, ref_str) == -1)
1444 f2f46662 2020-01-23 tracey return_html = strdup("");
1445 f2f46662 2020-01-23 tracey
1446 f2f46662 2020-01-23 tracey free(ref_str);
1447 f2f46662 2020-01-23 tracey return return_html;
1448 f2f46662 2020-01-23 tracey }
1449 f2f46662 2020-01-23 tracey
1450 f2f46662 2020-01-23 tracey static char *
1451 f2f46662 2020-01-23 tracey gw_gen_diff_header(char *str1, char *str2)
1452 f2f46662 2020-01-23 tracey {
1453 f2f46662 2020-01-23 tracey char *return_html = NULL;
1454 f2f46662 2020-01-23 tracey
1455 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_diff_html, str1, str2) == -1)
1456 f2f46662 2020-01-23 tracey return_html = strdup("");
1457 f2f46662 2020-01-23 tracey
1458 f2f46662 2020-01-23 tracey return return_html;
1459 f2f46662 2020-01-23 tracey }
1460 f2f46662 2020-01-23 tracey
1461 f2f46662 2020-01-23 tracey static char *
1462 f2f46662 2020-01-23 tracey gw_gen_author_header(char *str)
1463 f2f46662 2020-01-23 tracey {
1464 f2f46662 2020-01-23 tracey char *return_html = NULL;
1465 f2f46662 2020-01-23 tracey
1466 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_author_html, str) == -1)
1467 f2f46662 2020-01-23 tracey return_html = strdup("");
1468 f2f46662 2020-01-23 tracey
1469 f2f46662 2020-01-23 tracey return return_html;
1470 f2f46662 2020-01-23 tracey }
1471 f2f46662 2020-01-23 tracey
1472 f2f46662 2020-01-23 tracey static char *
1473 f2f46662 2020-01-23 tracey gw_gen_committer_header(char *str)
1474 f2f46662 2020-01-23 tracey {
1475 f2f46662 2020-01-23 tracey char *return_html = NULL;
1476 f2f46662 2020-01-23 tracey
1477 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_committer_html, str) == -1)
1478 f2f46662 2020-01-23 tracey return_html = strdup("");
1479 f2f46662 2020-01-23 tracey
1480 f2f46662 2020-01-23 tracey return return_html;
1481 f2f46662 2020-01-23 tracey }
1482 f2f46662 2020-01-23 tracey
1483 f2f46662 2020-01-23 tracey static char *
1484 f2f46662 2020-01-23 tracey gw_gen_commit_msg_header(char *str)
1485 f2f46662 2020-01-23 tracey {
1486 f2f46662 2020-01-23 tracey char *return_html = NULL;
1487 f2f46662 2020-01-23 tracey
1488 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_commit_msg_html, str) == -1)
1489 f2f46662 2020-01-23 tracey return_html = strdup("");
1490 f2f46662 2020-01-23 tracey
1491 f2f46662 2020-01-23 tracey return return_html;
1492 f2f46662 2020-01-23 tracey }
1493 f2f46662 2020-01-23 tracey
1494 f2f46662 2020-01-23 tracey static char *
1495 f2f46662 2020-01-23 tracey gw_gen_tree_header(char *str)
1496 f2f46662 2020-01-23 tracey {
1497 f2f46662 2020-01-23 tracey char *return_html = NULL;
1498 f2f46662 2020-01-23 tracey
1499 88d59c36 2020-01-29 stsp if (asprintf(&return_html, header_tree_html, str) == -1)
1500 f2f46662 2020-01-23 tracey return_html = strdup("");
1501 f2f46662 2020-01-23 tracey
1502 f2f46662 2020-01-23 tracey return return_html;
1503 f2f46662 2020-01-23 tracey }
1504 f2f46662 2020-01-23 tracey
1505 32cd4d18 2020-02-03 stsp static const struct got_error *
1506 32cd4d18 2020-02-03 stsp gw_get_repo_description(char **description, struct gw_trans *gw_trans,
1507 32cd4d18 2020-02-03 stsp char *dir)
1508 2c251c14 2020-01-15 tracey {
1509 32cd4d18 2020-02-03 stsp const struct got_error *error = NULL;
1510 b8b04565 2020-02-02 tracey FILE *f = NULL;
1511 32cd4d18 2020-02-03 stsp char *d_file = NULL;
1512 2c251c14 2020-01-15 tracey unsigned int len;
1513 1ab830c1 2020-02-03 stsp size_t n;
1514 2c251c14 2020-01-15 tracey
1515 32cd4d18 2020-02-03 stsp *description = NULL;
1516 0b20762b 2020-01-29 stsp if (gw_trans->gw_conf->got_show_repo_description == 0)
1517 32cd4d18 2020-02-03 stsp return gw_empty_string(description);
1518 2c251c14 2020-01-15 tracey
1519 88d59c36 2020-01-29 stsp if (asprintf(&d_file, "%s/description", dir) == -1)
1520 32cd4d18 2020-02-03 stsp return got_error_from_errno("asprintf");
1521 2c251c14 2020-01-15 tracey
1522 32cd4d18 2020-02-03 stsp f = fopen(d_file, "r");
1523 32cd4d18 2020-02-03 stsp if (f == NULL) {
1524 32cd4d18 2020-02-03 stsp if (errno == ENOENT || errno == EACCES)
1525 32cd4d18 2020-02-03 stsp return gw_empty_string(description);
1526 32cd4d18 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
1527 32cd4d18 2020-02-03 stsp goto done;
1528 32cd4d18 2020-02-03 stsp }
1529 2c251c14 2020-01-15 tracey
1530 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
1531 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1532 32cd4d18 2020-02-03 stsp goto done;
1533 32cd4d18 2020-02-03 stsp }
1534 32cd4d18 2020-02-03 stsp len = ftell(f);
1535 32cd4d18 2020-02-03 stsp if (len == -1) {
1536 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1537 32cd4d18 2020-02-03 stsp goto done;
1538 32cd4d18 2020-02-03 stsp }
1539 32cd4d18 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
1540 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1541 32cd4d18 2020-02-03 stsp goto done;
1542 32cd4d18 2020-02-03 stsp }
1543 32cd4d18 2020-02-03 stsp *description = calloc(len + 1, sizeof(**description));
1544 32cd4d18 2020-02-03 stsp if (*description == NULL) {
1545 32cd4d18 2020-02-03 stsp error = got_error_from_errno("calloc");
1546 32cd4d18 2020-02-03 stsp goto done;
1547 32cd4d18 2020-02-03 stsp }
1548 32cd4d18 2020-02-03 stsp
1549 32cd4d18 2020-02-03 stsp n = fread(*description, 1, len, f);
1550 1ab830c1 2020-02-03 stsp if (n == 0 && ferror(f))
1551 32cd4d18 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1552 32cd4d18 2020-02-03 stsp done:
1553 32cd4d18 2020-02-03 stsp if (f != NULL && fclose(f) == -1 && error == NULL)
1554 32cd4d18 2020-02-03 stsp error = got_error_from_errno("fclose");
1555 2c251c14 2020-01-15 tracey free(d_file);
1556 32cd4d18 2020-02-03 stsp return error;
1557 2c251c14 2020-01-15 tracey }
1558 2c251c14 2020-01-15 tracey
1559 55ccf528 2020-02-03 stsp static const struct got_error *
1560 55ccf528 2020-02-03 stsp gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1561 474370cb 2020-01-15 tracey {
1562 474370cb 2020-01-15 tracey struct tm tm;
1563 474370cb 2020-01-15 tracey time_t diff_time;
1564 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
1565 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1566 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
1567 474370cb 2020-01-15 tracey char *now = "right now";
1568 55ccf528 2020-02-03 stsp char *s;
1569 6c6c85af 2020-01-15 tracey char datebuf[29];
1570 474370cb 2020-01-15 tracey
1571 55ccf528 2020-02-03 stsp *repo_age = NULL;
1572 55ccf528 2020-02-03 stsp
1573 474370cb 2020-01-15 tracey switch (ref_tm) {
1574 474370cb 2020-01-15 tracey case TM_DIFF:
1575 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
1576 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1577 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
1578 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 365), years) == -1)
1579 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1580 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1581 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
1582 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1583 88d59c36 2020-01-29 stsp months) == -1)
1584 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1585 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1586 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
1587 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1588 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1589 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1590 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
1591 88d59c36 2020-01-29 stsp (diff_time / 60 / 60 / 24), days) == -1)
1592 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1593 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
1594 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s",
1595 88d59c36 2020-01-29 stsp (diff_time / 60 / 60), hours) == -1)
1596 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1597 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
1598 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1599 88d59c36 2020-01-29 stsp minutes) == -1)
1600 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1601 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
1602 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%lld %s", diff_time,
1603 88d59c36 2020-01-29 stsp seconds) == -1)
1604 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1605 474370cb 2020-01-15 tracey } else {
1606 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s", now) == -1)
1607 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1608 474370cb 2020-01-15 tracey }
1609 474370cb 2020-01-15 tracey break;
1610 474370cb 2020-01-15 tracey case TM_LONG:
1611 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1612 55ccf528 2020-02-03 stsp return got_error_from_errno("gmtime_r");
1613 474370cb 2020-01-15 tracey
1614 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
1615 474370cb 2020-01-15 tracey if (s == NULL)
1616 55ccf528 2020-02-03 stsp return got_error_from_errno("asctime_r");
1617 474370cb 2020-01-15 tracey
1618 55ccf528 2020-02-03 stsp if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1619 55ccf528 2020-02-03 stsp return got_error_from_errno("asprintf");
1620 474370cb 2020-01-15 tracey break;
1621 474370cb 2020-01-15 tracey }
1622 55ccf528 2020-02-03 stsp return NULL;
1623 474370cb 2020-01-15 tracey }
1624 474370cb 2020-01-15 tracey
1625 d126c1b7 2020-01-29 stsp static const struct got_error *
1626 d126c1b7 2020-01-29 stsp gw_get_repo_age(char **repo_age, struct gw_trans *gw_trans, char *dir,
1627 d126c1b7 2020-01-29 stsp char *repo_ref, int ref_tm)
1628 2c251c14 2020-01-15 tracey {
1629 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1630 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
1631 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
1632 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
1633 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
1634 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
1635 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
1636 87f9ebf5 2020-01-15 tracey int is_head = 0;
1637 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
1638 87f9ebf5 2020-01-15 tracey const char *refname;
1639 a0f36e03 2020-01-28 stsp
1640 d126c1b7 2020-01-29 stsp *repo_age = NULL;
1641 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
1642 387a29ba 2020-01-15 tracey
1643 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
1644 387a29ba 2020-01-15 tracey return NULL;
1645 87f9ebf5 2020-01-15 tracey
1646 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1647 87f9ebf5 2020-01-15 tracey is_head = 1;
1648 2c251c14 2020-01-15 tracey
1649 55ccf528 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_age == 0)
1650 d126c1b7 2020-01-29 stsp return NULL;
1651 87f9ebf5 2020-01-15 tracey
1652 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
1653 ec46ccd7 2020-01-15 tracey if (error)
1654 d126c1b7 2020-01-29 stsp goto done;
1655 2c251c14 2020-01-15 tracey
1656 87f9ebf5 2020-01-15 tracey if (is_head)
1657 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
1658 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1659 87f9ebf5 2020-01-15 tracey else
1660 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
1661 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1662 ec46ccd7 2020-01-15 tracey if (error)
1663 d126c1b7 2020-01-29 stsp goto done;
1664 2c251c14 2020-01-15 tracey
1665 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1666 87f9ebf5 2020-01-15 tracey if (is_head)
1667 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
1668 87f9ebf5 2020-01-15 tracey else
1669 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1670 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
1671 ec46ccd7 2020-01-15 tracey if (error)
1672 d126c1b7 2020-01-29 stsp goto done;
1673 2c251c14 2020-01-15 tracey
1674 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1675 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
1676 ec46ccd7 2020-01-15 tracey if (error)
1677 d126c1b7 2020-01-29 stsp goto done;
1678 2c251c14 2020-01-15 tracey
1679 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1680 ec46ccd7 2020-01-15 tracey if (error)
1681 d126c1b7 2020-01-29 stsp goto done;
1682 2c251c14 2020-01-15 tracey
1683 2c251c14 2020-01-15 tracey committer_time =
1684 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1685 2c251c14 2020-01-15 tracey
1686 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
1687 2c251c14 2020-01-15 tracey cmp_time = committer_time;
1688 2c251c14 2020-01-15 tracey }
1689 2c251c14 2020-01-15 tracey
1690 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1691 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1692 55ccf528 2020-02-03 stsp error = gw_get_time_str(repo_age, committer_time, ref_tm);
1693 d126c1b7 2020-01-29 stsp }
1694 d126c1b7 2020-01-29 stsp done:
1695 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1696 2c251c14 2020-01-15 tracey free(id);
1697 d126c1b7 2020-01-29 stsp return error;
1698 ec46ccd7 2020-01-15 tracey }
1699 ec46ccd7 2020-01-15 tracey
1700 83eb9a68 2020-02-03 stsp static const struct got_error *
1701 83eb9a68 2020-02-03 stsp gw_get_diff(char **diff_html, struct gw_trans *gw_trans,
1702 83eb9a68 2020-02-03 stsp struct gw_header *header)
1703 ec46ccd7 2020-01-15 tracey {
1704 ec46ccd7 2020-01-15 tracey const struct got_error *error;
1705 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
1706 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1707 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
1708 83eb9a68 2020-02-03 stsp char *label1 = NULL, *label2 = NULL, *line = NULL;
1709 83eb9a68 2020-02-03 stsp char *diff_line_html = NULL;
1710 05ce9a79 2020-01-24 tracey int obj_type;
1711 d4729381 2020-02-03 stsp size_t newsize, linesize = 0;
1712 83eb9a68 2020-02-03 stsp ssize_t linelen;
1713 ec46ccd7 2020-01-15 tracey
1714 ec46ccd7 2020-01-15 tracey f = got_opentemp();
1715 ec46ccd7 2020-01-15 tracey if (f == NULL)
1716 ec46ccd7 2020-01-15 tracey return NULL;
1717 ec46ccd7 2020-01-15 tracey
1718 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1719 ec46ccd7 2020-01-15 tracey if (error)
1720 d4729381 2020-02-03 stsp goto done;
1721 ec46ccd7 2020-01-15 tracey
1722 90f16cb8 2020-01-24 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1723 ec46ccd7 2020-01-15 tracey if (error)
1724 ec46ccd7 2020-01-15 tracey goto done;
1725 ec46ccd7 2020-01-15 tracey
1726 05ce9a79 2020-01-24 tracey if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1727 05ce9a79 2020-01-24 tracey error = got_repo_match_object_id(&id1, &label1,
1728 05ce9a79 2020-01-24 tracey header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1729 05ce9a79 2020-01-24 tracey if (error)
1730 05ce9a79 2020-01-24 tracey goto done;
1731 05ce9a79 2020-01-24 tracey }
1732 ec46ccd7 2020-01-15 tracey
1733 90f16cb8 2020-01-24 tracey error = got_repo_match_object_id(&id2, &label2,
1734 2ac037ec 2020-01-24 tracey header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1735 90f16cb8 2020-01-24 tracey if (error)
1736 90f16cb8 2020-01-24 tracey goto done;
1737 ec46ccd7 2020-01-15 tracey
1738 05ce9a79 2020-01-24 tracey error = got_object_get_type(&obj_type, header->repo, id2);
1739 90f16cb8 2020-01-24 tracey if (error)
1740 90f16cb8 2020-01-24 tracey goto done;
1741 05ce9a79 2020-01-24 tracey switch (obj_type) {
1742 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
1743 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1744 90f16cb8 2020-01-24 tracey header->repo, f);
1745 ec46ccd7 2020-01-15 tracey break;
1746 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
1747 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1748 90f16cb8 2020-01-24 tracey header->repo, f);
1749 ec46ccd7 2020-01-15 tracey break;
1750 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
1751 90f16cb8 2020-01-24 tracey error = got_diff_objects_as_commits(id1, id2, 3, 0,
1752 90f16cb8 2020-01-24 tracey header->repo, f);
1753 ec46ccd7 2020-01-15 tracey break;
1754 ec46ccd7 2020-01-15 tracey default:
1755 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1756 ec46ccd7 2020-01-15 tracey }
1757 b8b04565 2020-02-02 tracey if (error)
1758 b8b04565 2020-02-02 tracey goto done;
1759 b8b04565 2020-02-02 tracey
1760 d4729381 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
1761 d4729381 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1762 ec46ccd7 2020-01-15 tracey goto done;
1763 d4729381 2020-02-03 stsp }
1764 ec46ccd7 2020-01-15 tracey
1765 83eb9a68 2020-02-03 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
1766 d4729381 2020-02-03 stsp char *escaped_line;
1767 d4729381 2020-02-03 stsp error = gw_html_escape(&escaped_line, line);
1768 070fee27 2020-02-03 stsp if (error)
1769 070fee27 2020-02-03 stsp goto done;
1770 bfd30182 2020-01-24 tracey
1771 83eb9a68 2020-02-03 stsp error = gw_colordiff_line(&diff_line_html, escaped_line);
1772 ec46ccd7 2020-01-15 tracey if (error)
1773 b8b04565 2020-02-02 tracey goto done;
1774 ec46ccd7 2020-01-15 tracey
1775 83eb9a68 2020-02-03 stsp error = buf_puts(&newsize, diffbuf, diff_line_html);
1776 83eb9a68 2020-02-03 stsp if (error)
1777 83eb9a68 2020-02-03 stsp goto done;
1778 83eb9a68 2020-02-03 stsp
1779 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_end);
1780 ec46ccd7 2020-01-15 tracey if (error)
1781 b8b04565 2020-02-02 tracey goto done;
1782 ec46ccd7 2020-01-15 tracey }
1783 83eb9a68 2020-02-03 stsp if (linelen == -1 && ferror(f)) {
1784 83eb9a68 2020-02-03 stsp error = got_error_from_errno("getline");
1785 83eb9a68 2020-02-03 stsp goto done;
1786 83eb9a68 2020-02-03 stsp }
1787 ec46ccd7 2020-01-15 tracey
1788 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1789 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1790 d4729381 2020-02-03 stsp if (error)
1791 d4729381 2020-02-03 stsp goto done;
1792 83eb9a68 2020-02-03 stsp *diff_html = strdup(buf_get(diffbuf));
1793 83eb9a68 2020-02-03 stsp if (*diff_html == NULL)
1794 d4729381 2020-02-03 stsp error = got_error_from_errno("strdup");
1795 ec46ccd7 2020-01-15 tracey }
1796 ec46ccd7 2020-01-15 tracey done:
1797 d4729381 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
1798 d4729381 2020-02-03 stsp error = got_error_from_errno("fclose");
1799 83eb9a68 2020-02-03 stsp free(diff_line_html);
1800 d4729381 2020-02-03 stsp free(line);
1801 ec46ccd7 2020-01-15 tracey free(diffbuf);
1802 ec46ccd7 2020-01-15 tracey free(label1);
1803 ec46ccd7 2020-01-15 tracey free(label2);
1804 ec46ccd7 2020-01-15 tracey free(id1);
1805 ec46ccd7 2020-01-15 tracey free(id2);
1806 ec46ccd7 2020-01-15 tracey
1807 83eb9a68 2020-02-03 stsp return error;
1808 2c251c14 2020-01-15 tracey }
1809 2c251c14 2020-01-15 tracey
1810 9a1cc63f 2020-02-03 stsp static const struct got_error *
1811 9a1cc63f 2020-02-03 stsp gw_get_repo_owner(char **owner, struct gw_trans *gw_trans, char *dir)
1812 9a1cc63f 2020-02-03 stsp {
1813 9a1cc63f 2020-02-03 stsp const struct got_error *error = NULL;
1814 9a1cc63f 2020-02-03 stsp struct got_repository *repo;
1815 9a1cc63f 2020-02-03 stsp const char *gitconfig_owner;
1816 2c251c14 2020-01-15 tracey
1817 9a1cc63f 2020-02-03 stsp *owner = NULL;
1818 2c251c14 2020-01-15 tracey
1819 9a1cc63f 2020-02-03 stsp if (gw_trans->gw_conf->got_show_repo_owner == 0)
1820 9a1cc63f 2020-02-03 stsp return NULL;
1821 2c251c14 2020-01-15 tracey
1822 9a1cc63f 2020-02-03 stsp error = got_repo_open(&repo, dir, NULL);
1823 9a1cc63f 2020-02-03 stsp if (error)
1824 9a1cc63f 2020-02-03 stsp return error;
1825 9a1cc63f 2020-02-03 stsp gitconfig_owner = got_repo_get_gitconfig_owner(repo);
1826 9a1cc63f 2020-02-03 stsp if (gitconfig_owner) {
1827 9a1cc63f 2020-02-03 stsp *owner = strdup(gitconfig_owner);
1828 9a1cc63f 2020-02-03 stsp if (*owner == NULL)
1829 9a1cc63f 2020-02-03 stsp error = got_error_from_errno("strdup");
1830 2c251c14 2020-01-15 tracey }
1831 9a1cc63f 2020-02-03 stsp got_repo_close(repo);
1832 9a1cc63f 2020-02-03 stsp return error;
1833 2c251c14 2020-01-15 tracey }
1834 2c251c14 2020-01-15 tracey
1835 59d3c40e 2020-02-03 stsp static const struct got_error *
1836 59d3c40e 2020-02-03 stsp gw_get_clone_url(char **url, struct gw_trans *gw_trans, char *dir)
1837 2c251c14 2020-01-15 tracey {
1838 59d3c40e 2020-02-03 stsp const struct got_error *error = NULL;
1839 2c251c14 2020-01-15 tracey FILE *f;
1840 59d3c40e 2020-02-03 stsp char *d_file = NULL;
1841 2c251c14 2020-01-15 tracey unsigned int len;
1842 59d3c40e 2020-02-03 stsp size_t n;
1843 2c251c14 2020-01-15 tracey
1844 59d3c40e 2020-02-03 stsp *url = NULL;
1845 2c251c14 2020-01-15 tracey
1846 59d3c40e 2020-02-03 stsp if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
1847 59d3c40e 2020-02-03 stsp return got_error_from_errno("asprintf");
1848 2c251c14 2020-01-15 tracey
1849 59d3c40e 2020-02-03 stsp f = fopen(d_file, "r");
1850 59d3c40e 2020-02-03 stsp if (f == NULL) {
1851 59d3c40e 2020-02-03 stsp if (errno != ENOENT && errno != EACCES)
1852 59d3c40e 2020-02-03 stsp error = got_error_from_errno2("fopen", d_file);
1853 59d3c40e 2020-02-03 stsp goto done;
1854 59d3c40e 2020-02-03 stsp }
1855 59d3c40e 2020-02-03 stsp
1856 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_END) == -1) {
1857 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1858 59d3c40e 2020-02-03 stsp goto done;
1859 59d3c40e 2020-02-03 stsp }
1860 59d3c40e 2020-02-03 stsp len = ftell(f);
1861 59d3c40e 2020-02-03 stsp if (len == -1) {
1862 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1863 59d3c40e 2020-02-03 stsp goto done;
1864 59d3c40e 2020-02-03 stsp }
1865 59d3c40e 2020-02-03 stsp if (fseek(f, 0, SEEK_SET) == -1) {
1866 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1867 59d3c40e 2020-02-03 stsp goto done;
1868 59d3c40e 2020-02-03 stsp }
1869 2c251c14 2020-01-15 tracey
1870 59d3c40e 2020-02-03 stsp *url = calloc(len + 1, sizeof(**url));
1871 59d3c40e 2020-02-03 stsp if (*url == NULL) {
1872 59d3c40e 2020-02-03 stsp error = got_error_from_errno("calloc");
1873 59d3c40e 2020-02-03 stsp goto done;
1874 59d3c40e 2020-02-03 stsp }
1875 2c251c14 2020-01-15 tracey
1876 59d3c40e 2020-02-03 stsp n = fread(*url, 1, len, f);
1877 59d3c40e 2020-02-03 stsp if (n == 0 && ferror(f))
1878 59d3c40e 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
1879 59d3c40e 2020-02-03 stsp done:
1880 59d3c40e 2020-02-03 stsp if (f && fclose(f) == -1 && error == NULL)
1881 59d3c40e 2020-02-03 stsp error = got_error_from_errno("fclose");
1882 2c251c14 2020-01-15 tracey free(d_file);
1883 59d3c40e 2020-02-03 stsp return NULL;
1884 8d4d2453 2020-01-15 tracey }
1885 8d4d2453 2020-01-15 tracey
1886 6eccd105 2020-02-03 stsp static const struct got_error *
1887 6eccd105 2020-02-03 stsp gw_get_repo_tags(char **tag_html, struct gw_trans *gw_trans,
1888 6eccd105 2020-02-03 stsp struct gw_header *header, int limit, int tag_type)
1889 8d4d2453 2020-01-15 tracey {
1890 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
1891 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
1892 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
1893 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
1894 6eccd105 2020-02-03 stsp char *tag_row = NULL, *tags_navs_disp = NULL;
1895 6eccd105 2020-02-03 stsp char *age = NULL, *age_html = NULL, *newline;
1896 070fee27 2020-02-03 stsp char *escaped_tagger = NULL, *escaped_tag_commit = NULL;
1897 6eccd105 2020-02-03 stsp char *id_str = NULL, *refstr = NULL;
1898 6eccd105 2020-02-03 stsp char *tag_commit0 = NULL;
1899 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
1900 87f9ebf5 2020-01-15 tracey size_t newsize;
1901 6eccd105 2020-02-03 stsp struct got_tag_object *tag = NULL;
1902 8d4d2453 2020-01-15 tracey
1903 6eccd105 2020-02-03 stsp *tag_html = NULL;
1904 6eccd105 2020-02-03 stsp
1905 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
1906 a0f36e03 2020-01-28 stsp
1907 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1908 ec46ccd7 2020-01-15 tracey if (error)
1909 6c6c85af 2020-01-15 tracey return NULL;
1910 87f9ebf5 2020-01-15 tracey
1911 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1912 ec46ccd7 2020-01-15 tracey if (error)
1913 87f9ebf5 2020-01-15 tracey goto done;
1914 87f9ebf5 2020-01-15 tracey
1915 add40c4f 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1916 87f9ebf5 2020-01-15 tracey if (error)
1917 87f9ebf5 2020-01-15 tracey goto done;
1918 87f9ebf5 2020-01-15 tracey
1919 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1920 87f9ebf5 2020-01-15 tracey const char *refname;
1921 4ff7391f 2020-01-28 tracey const char *tagger;
1922 6eccd105 2020-02-03 stsp const char *tag_commit;
1923 87f9ebf5 2020-01-15 tracey time_t tagger_time;
1924 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
1925 87f9ebf5 2020-01-15 tracey
1926 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1927 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
1928 87f9ebf5 2020-01-15 tracey continue;
1929 87f9ebf5 2020-01-15 tracey refname += 10;
1930 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
1931 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
1932 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
1933 87f9ebf5 2020-01-15 tracey goto done;
1934 87f9ebf5 2020-01-15 tracey }
1935 87f9ebf5 2020-01-15 tracey
1936 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
1937 87f9ebf5 2020-01-15 tracey if (error)
1938 87f9ebf5 2020-01-15 tracey goto done;
1939 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
1940 87f9ebf5 2020-01-15 tracey free(id);
1941 87f9ebf5 2020-01-15 tracey if (error)
1942 87f9ebf5 2020-01-15 tracey goto done;
1943 87f9ebf5 2020-01-15 tracey
1944 4ff7391f 2020-01-28 tracey tagger = got_object_tag_get_tagger(tag);
1945 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
1946 87f9ebf5 2020-01-15 tracey
1947 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
1948 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
1949 87f9ebf5 2020-01-15 tracey if (error)
1950 87f9ebf5 2020-01-15 tracey goto done;
1951 87f9ebf5 2020-01-15 tracey
1952 f2f46662 2020-01-23 tracey tag_commit0 = strdup(got_object_tag_get_message(tag));
1953 f2f46662 2020-01-23 tracey if (tag_commit0 == NULL) {
1954 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
1955 87f9ebf5 2020-01-15 tracey goto done;
1956 87f9ebf5 2020-01-15 tracey }
1957 87f9ebf5 2020-01-15 tracey
1958 f2f46662 2020-01-23 tracey tag_commit = tag_commit0;
1959 f2f46662 2020-01-23 tracey while (*tag_commit == '\n')
1960 f2f46662 2020-01-23 tracey tag_commit++;
1961 87f9ebf5 2020-01-15 tracey
1962 87f9ebf5 2020-01-15 tracey switch (tag_type) {
1963 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
1964 f2f46662 2020-01-23 tracey newline = strchr(tag_commit, '\n');
1965 87f9ebf5 2020-01-15 tracey if (newline)
1966 87f9ebf5 2020-01-15 tracey *newline = '\0';
1967 87f9ebf5 2020-01-15 tracey
1968 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_DIFF);
1969 55ccf528 2020-02-03 stsp if (error)
1970 87f9ebf5 2020-01-15 tracey goto done;
1971 87f9ebf5 2020-01-15 tracey
1972 88d59c36 2020-01-29 stsp if (asprintf(&tags_navs_disp, tags_navs,
1973 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
1974 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
1975 88d59c36 2020-01-29 stsp gw_trans->repo_name, id_str) == -1) {
1976 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1977 87f9ebf5 2020-01-15 tracey goto done;
1978 87f9ebf5 2020-01-15 tracey }
1979 87f9ebf5 2020-01-15 tracey
1980 55ccf528 2020-02-03 stsp if (asprintf(&tag_row, tags_row, age ? age : "",
1981 55ccf528 2020-02-03 stsp refname, tag_commit, tags_navs_disp) == -1) {
1982 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1983 87f9ebf5 2020-01-15 tracey goto done;
1984 87f9ebf5 2020-01-15 tracey }
1985 87f9ebf5 2020-01-15 tracey
1986 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
1987 87f9ebf5 2020-01-15 tracey break;
1988 87f9ebf5 2020-01-15 tracey case TAGFULL:
1989 55ccf528 2020-02-03 stsp error = gw_get_time_str(&age, tagger_time, TM_LONG);
1990 55ccf528 2020-02-03 stsp if (error)
1991 4ff7391f 2020-01-28 tracey goto done;
1992 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_tagger, tagger);
1993 070fee27 2020-02-03 stsp if (error)
1994 070fee27 2020-02-03 stsp goto done;
1995 070fee27 2020-02-03 stsp error = gw_html_escape(&escaped_tag_commit, tag_commit);
1996 070fee27 2020-02-03 stsp if (error)
1997 070fee27 2020-02-03 stsp goto done;
1998 55ccf528 2020-02-03 stsp if (asprintf(&tag_row, tag_info, age ? age : "",
1999 070fee27 2020-02-03 stsp escaped_tagger, escaped_tag_commit) == -1) {
2000 4ff7391f 2020-01-28 tracey error = got_error_from_errno("asprintf");
2001 4ff7391f 2020-01-28 tracey goto done;
2002 4ff7391f 2020-01-28 tracey }
2003 87f9ebf5 2020-01-15 tracey break;
2004 87f9ebf5 2020-01-15 tracey default:
2005 87f9ebf5 2020-01-15 tracey break;
2006 87f9ebf5 2020-01-15 tracey }
2007 87f9ebf5 2020-01-15 tracey
2008 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
2009 6eccd105 2020-02-03 stsp if (error)
2010 6eccd105 2020-02-03 stsp goto done;
2011 6eccd105 2020-02-03 stsp
2012 6eccd105 2020-02-03 stsp if (limit && --limit == 0)
2013 6eccd105 2020-02-03 stsp break;
2014 87f9ebf5 2020-01-15 tracey
2015 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
2016 6eccd105 2020-02-03 stsp tag = NULL;
2017 6c6c85af 2020-01-15 tracey free(id_str);
2018 6eccd105 2020-02-03 stsp id_str = NULL;
2019 6c6c85af 2020-01-15 tracey free(refstr);
2020 6eccd105 2020-02-03 stsp refstr = NULL;
2021 6c6c85af 2020-01-15 tracey free(age);
2022 55ccf528 2020-02-03 stsp age = NULL;
2023 55ccf528 2020-02-03 stsp free(age_html);
2024 55ccf528 2020-02-03 stsp age_html = NULL;
2025 070fee27 2020-02-03 stsp free(escaped_tagger);
2026 070fee27 2020-02-03 stsp escaped_tagger = NULL;
2027 070fee27 2020-02-03 stsp free(escaped_tag_commit);
2028 070fee27 2020-02-03 stsp escaped_tag_commit = NULL;
2029 f2f46662 2020-01-23 tracey free(tag_commit0);
2030 6eccd105 2020-02-03 stsp tag_commit0 = NULL;
2031 87f9ebf5 2020-01-15 tracey free(tag_row);
2032 6eccd105 2020-02-03 stsp tag_row = NULL;
2033 87f9ebf5 2020-01-15 tracey }
2034 6c6c85af 2020-01-15 tracey
2035 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2036 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2037 6eccd105 2020-02-03 stsp *tag_html = strdup(buf_get(diffbuf));
2038 6eccd105 2020-02-03 stsp if (*tag_html == NULL)
2039 6eccd105 2020-02-03 stsp error = got_error_from_errno("strdup");
2040 6c6c85af 2020-01-15 tracey }
2041 87f9ebf5 2020-01-15 tracey done:
2042 6eccd105 2020-02-03 stsp if (tag)
2043 6eccd105 2020-02-03 stsp got_object_tag_close(tag);
2044 6eccd105 2020-02-03 stsp free(id_str);
2045 6eccd105 2020-02-03 stsp free(refstr);
2046 55ccf528 2020-02-03 stsp free(age);
2047 55ccf528 2020-02-03 stsp free(age_html);
2048 070fee27 2020-02-03 stsp free(escaped_tagger);
2049 070fee27 2020-02-03 stsp free(escaped_tag_commit);
2050 6eccd105 2020-02-03 stsp free(tag_commit0);
2051 6eccd105 2020-02-03 stsp free(tag_row);
2052 6eccd105 2020-02-03 stsp buf_free(diffbuf);
2053 6eccd105 2020-02-03 stsp got_ref_list_free(&refs);
2054 6eccd105 2020-02-03 stsp if (repo)
2055 6eccd105 2020-02-03 stsp got_repo_close(repo);
2056 6eccd105 2020-02-03 stsp return error;
2057 f2f46662 2020-01-23 tracey }
2058 f2f46662 2020-01-23 tracey
2059 f2f46662 2020-01-23 tracey static void
2060 f2f46662 2020-01-23 tracey gw_free_headers(struct gw_header *header)
2061 f2f46662 2020-01-23 tracey {
2062 f2f46662 2020-01-23 tracey free(header->id);
2063 f2f46662 2020-01-23 tracey free(header->path);
2064 f2f46662 2020-01-23 tracey if (header->commit != NULL)
2065 f2f46662 2020-01-23 tracey got_object_commit_close(header->commit);
2066 f2f46662 2020-01-23 tracey if (header->repo)
2067 f2f46662 2020-01-23 tracey got_repo_close(header->repo);
2068 f2f46662 2020-01-23 tracey free(header->refs_str);
2069 f2f46662 2020-01-23 tracey free(header->commit_id);
2070 f2f46662 2020-01-23 tracey free(header->parent_id);
2071 f2f46662 2020-01-23 tracey free(header->tree_id);
2072 f2f46662 2020-01-23 tracey free(header->author);
2073 f2f46662 2020-01-23 tracey free(header->committer);
2074 f2f46662 2020-01-23 tracey free(header->commit_msg);
2075 f2f46662 2020-01-23 tracey }
2076 f2f46662 2020-01-23 tracey
2077 f2f46662 2020-01-23 tracey static struct gw_header *
2078 f2f46662 2020-01-23 tracey gw_init_header()
2079 f2f46662 2020-01-23 tracey {
2080 f2f46662 2020-01-23 tracey struct gw_header *header;
2081 f2f46662 2020-01-23 tracey
2082 ae36ed87 2020-01-28 stsp header = malloc(sizeof(*header));
2083 ae36ed87 2020-01-28 stsp if (header == NULL)
2084 f2f46662 2020-01-23 tracey return NULL;
2085 f2f46662 2020-01-23 tracey
2086 f2f46662 2020-01-23 tracey header->repo = NULL;
2087 f2f46662 2020-01-23 tracey header->commit = NULL;
2088 f2f46662 2020-01-23 tracey header->id = NULL;
2089 f2f46662 2020-01-23 tracey header->path = NULL;
2090 ae36ed87 2020-01-28 stsp SIMPLEQ_INIT(&header->refs);
2091 f2f46662 2020-01-23 tracey
2092 f2f46662 2020-01-23 tracey return header;
2093 f2f46662 2020-01-23 tracey }
2094 f2f46662 2020-01-23 tracey
2095 f2f46662 2020-01-23 tracey static const struct got_error *
2096 f2f46662 2020-01-23 tracey gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
2097 f2f46662 2020-01-23 tracey int limit)
2098 f2f46662 2020-01-23 tracey {
2099 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2100 f2f46662 2020-01-23 tracey struct got_commit_graph *graph = NULL;
2101 f2f46662 2020-01-23 tracey
2102 f2f46662 2020-01-23 tracey error = got_commit_graph_open(&graph, header->path, 0);
2103 f2f46662 2020-01-23 tracey if (error)
2104 f2f46662 2020-01-23 tracey goto done;
2105 f2f46662 2020-01-23 tracey
2106 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_start(graph, header->id, header->repo,
2107 f2f46662 2020-01-23 tracey NULL, NULL);
2108 f2f46662 2020-01-23 tracey if (error)
2109 f2f46662 2020-01-23 tracey goto done;
2110 f2f46662 2020-01-23 tracey
2111 f2f46662 2020-01-23 tracey for (;;) {
2112 f2f46662 2020-01-23 tracey error = got_commit_graph_iter_next(&header->id, graph,
2113 f2f46662 2020-01-23 tracey header->repo, NULL, NULL);
2114 f2f46662 2020-01-23 tracey if (error) {
2115 f2f46662 2020-01-23 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
2116 f2f46662 2020-01-23 tracey error = NULL;
2117 f2f46662 2020-01-23 tracey goto done;
2118 f2f46662 2020-01-23 tracey }
2119 f2f46662 2020-01-23 tracey if (header->id == NULL)
2120 f2f46662 2020-01-23 tracey goto done;
2121 f2f46662 2020-01-23 tracey
2122 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit, header->repo,
2123 f2f46662 2020-01-23 tracey header->id);
2124 f2f46662 2020-01-23 tracey if (error)
2125 f2f46662 2020-01-23 tracey goto done;
2126 f2f46662 2020-01-23 tracey
2127 f2f46662 2020-01-23 tracey error = gw_get_commit(gw_trans, header);
2128 f2f46662 2020-01-23 tracey if (limit > 1) {
2129 f2f46662 2020-01-23 tracey struct gw_header *n_header = NULL;
2130 5147ab56 2020-01-29 stsp if ((n_header = gw_init_header()) == NULL) {
2131 f2f46662 2020-01-23 tracey error = got_error_from_errno("malloc");
2132 5147ab56 2020-01-29 stsp goto done;
2133 5147ab56 2020-01-29 stsp }
2134 f2f46662 2020-01-23 tracey
2135 f2f46662 2020-01-23 tracey n_header->refs_str = strdup(header->refs_str);
2136 f2f46662 2020-01-23 tracey n_header->commit_id = strdup(header->commit_id);
2137 f2f46662 2020-01-23 tracey n_header->parent_id = strdup(header->parent_id);
2138 f2f46662 2020-01-23 tracey n_header->tree_id = strdup(header->tree_id);
2139 f2f46662 2020-01-23 tracey n_header->author = strdup(header->author);
2140 f2f46662 2020-01-23 tracey n_header->committer = strdup(header->committer);
2141 f2f46662 2020-01-23 tracey n_header->commit_msg = strdup(header->commit_msg);
2142 f2f46662 2020-01-23 tracey n_header->committer_time = header->committer_time;
2143 f2f46662 2020-01-23 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
2144 f2f46662 2020-01-23 tracey entry);
2145 f2f46662 2020-01-23 tracey }
2146 f2f46662 2020-01-23 tracey if (error || (limit && --limit == 0))
2147 f2f46662 2020-01-23 tracey break;
2148 f2f46662 2020-01-23 tracey }
2149 f2f46662 2020-01-23 tracey done:
2150 f2f46662 2020-01-23 tracey if (graph)
2151 f2f46662 2020-01-23 tracey got_commit_graph_close(graph);
2152 f2f46662 2020-01-23 tracey return error;
2153 f2f46662 2020-01-23 tracey }
2154 f2f46662 2020-01-23 tracey
2155 f2f46662 2020-01-23 tracey static const struct got_error *
2156 f2f46662 2020-01-23 tracey gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
2157 f2f46662 2020-01-23 tracey {
2158 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2159 f2f46662 2020-01-23 tracey struct got_reflist_entry *re;
2160 f2f46662 2020-01-23 tracey struct got_object_id *id2 = NULL;
2161 f2f46662 2020-01-23 tracey struct got_object_qid *parent_id;
2162 e46f587c 2020-01-29 tracey char *refs_str = NULL, *commit_msg = NULL, *commit_msg0;
2163 f2f46662 2020-01-23 tracey
2164 f2f46662 2020-01-23 tracey /*print commit*/
2165 f2f46662 2020-01-23 tracey SIMPLEQ_FOREACH(re, &header->refs, entry) {
2166 f2f46662 2020-01-23 tracey char *s;
2167 f2f46662 2020-01-23 tracey const char *name;
2168 f2f46662 2020-01-23 tracey struct got_tag_object *tag = NULL;
2169 f2f46662 2020-01-23 tracey int cmp;
2170 f2f46662 2020-01-23 tracey
2171 f2f46662 2020-01-23 tracey name = got_ref_get_name(re->ref);
2172 f2f46662 2020-01-23 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
2173 f2f46662 2020-01-23 tracey continue;
2174 f2f46662 2020-01-23 tracey if (strncmp(name, "refs/", 5) == 0)
2175 f2f46662 2020-01-23 tracey name += 5;
2176 f2f46662 2020-01-23 tracey if (strncmp(name, "got/", 4) == 0)
2177 f2f46662 2020-01-23 tracey continue;
2178 f2f46662 2020-01-23 tracey if (strncmp(name, "heads/", 6) == 0)
2179 f2f46662 2020-01-23 tracey name += 6;
2180 f2f46662 2020-01-23 tracey if (strncmp(name, "remotes/", 8) == 0)
2181 f2f46662 2020-01-23 tracey name += 8;
2182 f2f46662 2020-01-23 tracey if (strncmp(name, "tags/", 5) == 0) {
2183 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag, header->repo,
2184 f2f46662 2020-01-23 tracey re->id);
2185 f2f46662 2020-01-23 tracey if (error) {
2186 f2f46662 2020-01-23 tracey if (error->code != GOT_ERR_OBJ_TYPE)
2187 f2f46662 2020-01-23 tracey continue;
2188 f2f46662 2020-01-23 tracey /*
2189 f2f46662 2020-01-23 tracey * Ref points at something other
2190 f2f46662 2020-01-23 tracey * than a tag.
2191 f2f46662 2020-01-23 tracey */
2192 f2f46662 2020-01-23 tracey error = NULL;
2193 f2f46662 2020-01-23 tracey tag = NULL;
2194 f2f46662 2020-01-23 tracey }
2195 f2f46662 2020-01-23 tracey }
2196 f2f46662 2020-01-23 tracey cmp = got_object_id_cmp(tag ?
2197 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag) : re->id, header->id);
2198 f2f46662 2020-01-23 tracey if (tag)
2199 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2200 f2f46662 2020-01-23 tracey if (cmp != 0)
2201 f2f46662 2020-01-23 tracey continue;
2202 f2f46662 2020-01-23 tracey s = refs_str;
2203 88d59c36 2020-01-29 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "",
2204 88d59c36 2020-01-29 stsp s ? ", " : "", name) == -1) {
2205 f2f46662 2020-01-23 tracey error = got_error_from_errno("asprintf");
2206 f2f46662 2020-01-23 tracey free(s);
2207 f2f46662 2020-01-23 tracey return error;
2208 f2f46662 2020-01-23 tracey }
2209 f2f46662 2020-01-23 tracey header->refs_str = strdup(refs_str);
2210 f2f46662 2020-01-23 tracey free(s);
2211 f2f46662 2020-01-23 tracey }
2212 f2f46662 2020-01-23 tracey
2213 f2f46662 2020-01-23 tracey if (refs_str == NULL)
2214 f2f46662 2020-01-23 tracey header->refs_str = strdup("");
2215 f2f46662 2020-01-23 tracey free(refs_str);
2216 f2f46662 2020-01-23 tracey
2217 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->commit_id, header->id);
2218 f2f46662 2020-01-23 tracey if (error)
2219 f2f46662 2020-01-23 tracey return error;
2220 f2f46662 2020-01-23 tracey
2221 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->tree_id,
2222 f2f46662 2020-01-23 tracey got_object_commit_get_tree_id(header->commit));
2223 f2f46662 2020-01-23 tracey if (error)
2224 f2f46662 2020-01-23 tracey return error;
2225 f2f46662 2020-01-23 tracey
2226 f2f46662 2020-01-23 tracey if (gw_trans->action == GW_DIFF) {
2227 f2f46662 2020-01-23 tracey parent_id = SIMPLEQ_FIRST(
2228 f2f46662 2020-01-23 tracey got_object_commit_get_parent_ids(header->commit));
2229 f2f46662 2020-01-23 tracey if (parent_id != NULL) {
2230 f2f46662 2020-01-23 tracey id2 = got_object_id_dup(parent_id->id);
2231 f2f46662 2020-01-23 tracey free (parent_id);
2232 f2f46662 2020-01-23 tracey error = got_object_id_str(&header->parent_id, id2);
2233 f2f46662 2020-01-23 tracey if (error)
2234 f2f46662 2020-01-23 tracey return error;
2235 f2f46662 2020-01-23 tracey free(id2);
2236 f2f46662 2020-01-23 tracey } else
2237 f2f46662 2020-01-23 tracey header->parent_id = strdup("/dev/null");
2238 f2f46662 2020-01-23 tracey } else
2239 f2f46662 2020-01-23 tracey header->parent_id = strdup("");
2240 f2f46662 2020-01-23 tracey
2241 f2f46662 2020-01-23 tracey header->committer_time =
2242 f2f46662 2020-01-23 tracey got_object_commit_get_committer_time(header->commit);
2243 1177268c 2020-01-28 tracey
2244 070fee27 2020-02-03 stsp error = gw_html_escape(&header->author,
2245 070fee27 2020-02-03 stsp got_object_commit_get_author(header->commit));
2246 070fee27 2020-02-03 stsp if (error)
2247 070fee27 2020-02-03 stsp return error;
2248 070fee27 2020-02-03 stsp error = gw_html_escape(&header->committer,
2249 070fee27 2020-02-03 stsp got_object_commit_get_committer(header->commit));
2250 070fee27 2020-02-03 stsp if (error)
2251 070fee27 2020-02-03 stsp return error;
2252 1177268c 2020-01-28 tracey
2253 070fee27 2020-02-03 stsp /* XXX Doesn't the log message require escaping? */
2254 f2f46662 2020-01-23 tracey error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2255 f2f46662 2020-01-23 tracey if (error)
2256 f2f46662 2020-01-23 tracey return error;
2257 f2f46662 2020-01-23 tracey
2258 f2f46662 2020-01-23 tracey commit_msg = commit_msg0;
2259 f2f46662 2020-01-23 tracey while (*commit_msg == '\n')
2260 f2f46662 2020-01-23 tracey commit_msg++;
2261 f2f46662 2020-01-23 tracey
2262 f2f46662 2020-01-23 tracey header->commit_msg = strdup(commit_msg);
2263 f2f46662 2020-01-23 tracey free(commit_msg0);
2264 f2f46662 2020-01-23 tracey return error;
2265 f2f46662 2020-01-23 tracey }
2266 f2f46662 2020-01-23 tracey
2267 f2f46662 2020-01-23 tracey static const struct got_error *
2268 f2f46662 2020-01-23 tracey gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2269 f2f46662 2020-01-23 tracey {
2270 f2f46662 2020-01-23 tracey const struct got_error *error = NULL;
2271 f2f46662 2020-01-23 tracey char *in_repo_path = NULL;
2272 f2f46662 2020-01-23 tracey
2273 f2f46662 2020-01-23 tracey error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2274 f2f46662 2020-01-23 tracey if (error)
2275 f2f46662 2020-01-23 tracey return error;
2276 f2f46662 2020-01-23 tracey
2277 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
2278 f2f46662 2020-01-23 tracey struct got_reference *head_ref;
2279 f2f46662 2020-01-23 tracey error = got_ref_open(&head_ref, header->repo,
2280 f2f46662 2020-01-23 tracey gw_trans->headref, 0);
2281 f2f46662 2020-01-23 tracey if (error)
2282 f2f46662 2020-01-23 tracey return error;
2283 f2f46662 2020-01-23 tracey
2284 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, head_ref);
2285 f2f46662 2020-01-23 tracey got_ref_close(head_ref);
2286 f2f46662 2020-01-23 tracey if (error)
2287 f2f46662 2020-01-23 tracey return error;
2288 f2f46662 2020-01-23 tracey
2289 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2290 f2f46662 2020-01-23 tracey header->repo, header->id);
2291 f2f46662 2020-01-23 tracey } else {
2292 f2f46662 2020-01-23 tracey struct got_reference *ref;
2293 f2f46662 2020-01-23 tracey error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2294 f2f46662 2020-01-23 tracey if (error == NULL) {
2295 f2f46662 2020-01-23 tracey int obj_type;
2296 f2f46662 2020-01-23 tracey error = got_ref_resolve(&header->id, header->repo, ref);
2297 f2f46662 2020-01-23 tracey got_ref_close(ref);
2298 f2f46662 2020-01-23 tracey if (error)
2299 f2f46662 2020-01-23 tracey return error;
2300 f2f46662 2020-01-23 tracey error = got_object_get_type(&obj_type, header->repo,
2301 f2f46662 2020-01-23 tracey header->id);
2302 f2f46662 2020-01-23 tracey if (error)
2303 f2f46662 2020-01-23 tracey return error;
2304 f2f46662 2020-01-23 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
2305 f2f46662 2020-01-23 tracey struct got_tag_object *tag;
2306 f2f46662 2020-01-23 tracey error = got_object_open_as_tag(&tag,
2307 f2f46662 2020-01-23 tracey header->repo, header->id);
2308 f2f46662 2020-01-23 tracey if (error)
2309 f2f46662 2020-01-23 tracey return error;
2310 f2f46662 2020-01-23 tracey if (got_object_tag_get_object_type(tag) !=
2311 f2f46662 2020-01-23 tracey GOT_OBJ_TYPE_COMMIT) {
2312 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2313 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2314 f2f46662 2020-01-23 tracey return error;
2315 f2f46662 2020-01-23 tracey }
2316 f2f46662 2020-01-23 tracey free(header->id);
2317 f2f46662 2020-01-23 tracey header->id = got_object_id_dup(
2318 f2f46662 2020-01-23 tracey got_object_tag_get_object_id(tag));
2319 f2f46662 2020-01-23 tracey if (header->id == NULL)
2320 f2f46662 2020-01-23 tracey error = got_error_from_errno(
2321 f2f46662 2020-01-23 tracey "got_object_id_dup");
2322 f2f46662 2020-01-23 tracey got_object_tag_close(tag);
2323 f2f46662 2020-01-23 tracey if (error)
2324 f2f46662 2020-01-23 tracey return error;
2325 f2f46662 2020-01-23 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2326 f2f46662 2020-01-23 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2327 f2f46662 2020-01-23 tracey return error;
2328 f2f46662 2020-01-23 tracey }
2329 f2f46662 2020-01-23 tracey error = got_object_open_as_commit(&header->commit,
2330 f2f46662 2020-01-23 tracey header->repo, header->id);
2331 f2f46662 2020-01-23 tracey if (error)
2332 f2f46662 2020-01-23 tracey return error;
2333 f2f46662 2020-01-23 tracey }
2334 f2f46662 2020-01-23 tracey if (header->commit == NULL) {
2335 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2336 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2337 f2f46662 2020-01-23 tracey header->repo);
2338 f2f46662 2020-01-23 tracey if (error)
2339 f2f46662 2020-01-23 tracey return error;
2340 f2f46662 2020-01-23 tracey }
2341 f2f46662 2020-01-23 tracey error = got_repo_match_object_id_prefix(&header->id,
2342 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2343 f2f46662 2020-01-23 tracey header->repo);
2344 f2f46662 2020-01-23 tracey }
2345 f2f46662 2020-01-23 tracey
2346 f2f46662 2020-01-23 tracey error = got_repo_map_path(&in_repo_path, header->repo,
2347 f2f46662 2020-01-23 tracey gw_trans->repo_path, 1);
2348 f2f46662 2020-01-23 tracey if (error)
2349 f2f46662 2020-01-23 tracey return error;
2350 f2f46662 2020-01-23 tracey
2351 f2f46662 2020-01-23 tracey if (in_repo_path) {
2352 f2f46662 2020-01-23 tracey header->path = strdup(in_repo_path);
2353 f2f46662 2020-01-23 tracey }
2354 f2f46662 2020-01-23 tracey free(in_repo_path);
2355 f2f46662 2020-01-23 tracey
2356 f2f46662 2020-01-23 tracey error = got_ref_list(&header->refs, header->repo, NULL,
2357 f2f46662 2020-01-23 tracey got_ref_cmp_by_name, NULL);
2358 f2f46662 2020-01-23 tracey if (error)
2359 f2f46662 2020-01-23 tracey return error;
2360 f2f46662 2020-01-23 tracey
2361 f2f46662 2020-01-23 tracey error = gw_get_commits(gw_trans, header, limit);
2362 f2f46662 2020-01-23 tracey return error;
2363 ec46ccd7 2020-01-15 tracey }
2364 ec46ccd7 2020-01-15 tracey
2365 ec46ccd7 2020-01-15 tracey struct blame_line {
2366 ec46ccd7 2020-01-15 tracey int annotated;
2367 ec46ccd7 2020-01-15 tracey char *id_str;
2368 ec46ccd7 2020-01-15 tracey char *committer;
2369 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
2370 ec46ccd7 2020-01-15 tracey };
2371 ec46ccd7 2020-01-15 tracey
2372 147269d5 2020-01-15 tracey struct gw_blame_cb_args {
2373 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
2374 ec46ccd7 2020-01-15 tracey int nlines;
2375 ec46ccd7 2020-01-15 tracey int nlines_prec;
2376 ec46ccd7 2020-01-15 tracey int lineno_cur;
2377 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
2378 ec46ccd7 2020-01-15 tracey FILE *f;
2379 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
2380 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
2381 2e676fc5 2020-01-15 tracey struct buf *blamebuf;
2382 ec46ccd7 2020-01-15 tracey };
2383 ec46ccd7 2020-01-15 tracey
2384 ec46ccd7 2020-01-15 tracey static const struct got_error *
2385 147269d5 2020-01-15 tracey gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2386 ec46ccd7 2020-01-15 tracey {
2387 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
2388 147269d5 2020-01-15 tracey struct gw_blame_cb_args *a = arg;
2389 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
2390 ec46ccd7 2020-01-15 tracey char *line = NULL;
2391 2e676fc5 2020-01-15 tracey size_t linesize = 0, newsize;
2392 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
2393 ec46ccd7 2020-01-15 tracey off_t offset;
2394 ec46ccd7 2020-01-15 tracey struct tm tm;
2395 ec46ccd7 2020-01-15 tracey time_t committer_time;
2396 ec46ccd7 2020-01-15 tracey
2397 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
2398 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
2399 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
2400 ec46ccd7 2020-01-15 tracey
2401 ec46ccd7 2020-01-15 tracey if (lineno == -1)
2402 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
2403 ec46ccd7 2020-01-15 tracey
2404 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
2405 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
2406 ec46ccd7 2020-01-15 tracey if (bline->annotated)
2407 ec46ccd7 2020-01-15 tracey return NULL;
2408 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
2409 ec46ccd7 2020-01-15 tracey if (err)
2410 ec46ccd7 2020-01-15 tracey return err;
2411 ec46ccd7 2020-01-15 tracey
2412 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
2413 ec46ccd7 2020-01-15 tracey if (err)
2414 ec46ccd7 2020-01-15 tracey goto done;
2415 ec46ccd7 2020-01-15 tracey
2416 ec46ccd7 2020-01-15 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
2417 ec46ccd7 2020-01-15 tracey if (bline->committer == NULL) {
2418 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
2419 ec46ccd7 2020-01-15 tracey goto done;
2420 ec46ccd7 2020-01-15 tracey }
2421 ec46ccd7 2020-01-15 tracey
2422 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
2423 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
2424 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
2425 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2426 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
2427 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
2428 ec46ccd7 2020-01-15 tracey goto done;
2429 ec46ccd7 2020-01-15 tracey }
2430 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
2431 ec46ccd7 2020-01-15 tracey
2432 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
2433 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2434 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
2435 ec46ccd7 2020-01-15 tracey goto done;
2436 ec46ccd7 2020-01-15 tracey
2437 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
2438 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
2439 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
2440 ec46ccd7 2020-01-15 tracey goto done;
2441 ec46ccd7 2020-01-15 tracey }
2442 ec46ccd7 2020-01-15 tracey
2443 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
2444 0311ce2d 2020-01-15 tracey char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2445 0311ce2d 2020-01-15 tracey *line_escape = NULL;
2446 ec46ccd7 2020-01-15 tracey size_t len;
2447 ec46ccd7 2020-01-15 tracey
2448 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
2449 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
2450 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
2451 ec46ccd7 2020-01-15 tracey break;
2452 ec46ccd7 2020-01-15 tracey }
2453 ec46ccd7 2020-01-15 tracey
2454 ec46ccd7 2020-01-15 tracey committer = bline->committer;
2455 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
2456 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
2457 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
2458 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
2459 ec46ccd7 2020-01-15 tracey if (at)
2460 ec46ccd7 2020-01-15 tracey *at = '\0';
2461 ec46ccd7 2020-01-15 tracey len = strlen(committer);
2462 ec46ccd7 2020-01-15 tracey if (len >= 9)
2463 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
2464 ec46ccd7 2020-01-15 tracey
2465 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
2466 ec46ccd7 2020-01-15 tracey if (nl)
2467 ec46ccd7 2020-01-15 tracey *nl = '\0';
2468 0311ce2d 2020-01-15 tracey
2469 070fee27 2020-02-03 stsp err = gw_html_escape(&line_escape, line);
2470 070fee27 2020-02-03 stsp if (err)
2471 070fee27 2020-02-03 stsp goto err;
2472 0311ce2d 2020-01-15 tracey
2473 de6bdba4 2020-01-31 tracey if (a->gw_trans->repo_folder == NULL)
2474 de6bdba4 2020-01-31 tracey a->gw_trans->repo_folder = strdup("");
2475 de6bdba4 2020-01-31 tracey if (a->gw_trans->repo_folder == NULL)
2476 de6bdba4 2020-01-31 tracey goto err;
2477 10d47faf 2020-01-31 tracey asprintf(&blame_row, blame_line, a->nlines_prec, a->lineno_cur,
2478 10d47faf 2020-01-31 tracey a->gw_trans->repo_name, bline->id_str,
2479 10d47faf 2020-01-31 tracey a->gw_trans->repo_file, a->gw_trans->repo_folder,
2480 10d47faf 2020-01-31 tracey bline->id_str, bline->datebuf, committer, line_escape);
2481 ec46ccd7 2020-01-15 tracey a->lineno_cur++;
2482 2e676fc5 2020-01-15 tracey err = buf_puts(&newsize, a->blamebuf, blame_row);
2483 2e676fc5 2020-01-15 tracey if (err)
2484 2e676fc5 2020-01-15 tracey return err;
2485 2e676fc5 2020-01-15 tracey
2486 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2487 de6bdba4 2020-01-31 tracey err:
2488 0311ce2d 2020-01-15 tracey free(line_escape);
2489 2e676fc5 2020-01-15 tracey free(blame_row);
2490 ec46ccd7 2020-01-15 tracey }
2491 ec46ccd7 2020-01-15 tracey done:
2492 ec46ccd7 2020-01-15 tracey if (commit)
2493 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
2494 ec46ccd7 2020-01-15 tracey free(line);
2495 ec46ccd7 2020-01-15 tracey return err;
2496 872742ce 2020-02-01 stsp }
2497 872742ce 2020-02-01 stsp
2498 a81f3554 2020-02-03 stsp static const struct got_error *
2499 a81f3554 2020-02-03 stsp gw_get_file_blame_blob(char **blame_html, struct gw_trans *gw_trans)
2500 bcbc97d8 2020-01-15 tracey {
2501 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
2502 bcbc97d8 2020-01-15 tracey struct got_repository *repo = NULL;
2503 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
2504 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
2505 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
2506 a81f3554 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
2507 147269d5 2020-01-15 tracey struct gw_blame_cb_args bca;
2508 119bf4ed 2020-01-15 tracey int i, obj_type;
2509 ec46ccd7 2020-01-15 tracey size_t filesize;
2510 ec46ccd7 2020-01-15 tracey
2511 a81f3554 2020-02-03 stsp *blame_html = NULL;
2512 a81f3554 2020-02-03 stsp
2513 ec46ccd7 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2514 ec46ccd7 2020-01-15 tracey if (error)
2515 a81f3554 2020-02-03 stsp return error;
2516 ec46ccd7 2020-01-15 tracey
2517 a81f3554 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
2518 a81f3554 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2519 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
2520 a81f3554 2020-02-03 stsp gw_trans->repo_file) == -1) {
2521 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2522 2e676fc5 2020-01-15 tracey goto done;
2523 2e676fc5 2020-01-15 tracey }
2524 2e676fc5 2020-01-15 tracey
2525 2e676fc5 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, path, 1);
2526 ec46ccd7 2020-01-15 tracey if (error)
2527 ec46ccd7 2020-01-15 tracey goto done;
2528 ec46ccd7 2020-01-15 tracey
2529 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2530 147269d5 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT, 1, repo);
2531 ec46ccd7 2020-01-15 tracey if (error)
2532 ec46ccd7 2020-01-15 tracey goto done;
2533 ec46ccd7 2020-01-15 tracey
2534 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2535 ec46ccd7 2020-01-15 tracey if (error)
2536 ec46ccd7 2020-01-15 tracey goto done;
2537 2e676fc5 2020-01-15 tracey
2538 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
2539 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
2540 ec46ccd7 2020-01-15 tracey goto done;
2541 ec46ccd7 2020-01-15 tracey }
2542 ec46ccd7 2020-01-15 tracey
2543 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, obj_id);
2544 ec46ccd7 2020-01-15 tracey if (error)
2545 ec46ccd7 2020-01-15 tracey goto done;
2546 ec46ccd7 2020-01-15 tracey
2547 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
2548 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2549 ec46ccd7 2020-01-15 tracey goto done;
2550 ec46ccd7 2020-01-15 tracey }
2551 ec46ccd7 2020-01-15 tracey
2552 ec46ccd7 2020-01-15 tracey error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2553 ec46ccd7 2020-01-15 tracey if (error)
2554 ec46ccd7 2020-01-15 tracey goto done;
2555 ec46ccd7 2020-01-15 tracey
2556 2e676fc5 2020-01-15 tracey error = buf_alloc(&bca.blamebuf, 0);
2557 2e676fc5 2020-01-15 tracey if (error)
2558 2e676fc5 2020-01-15 tracey goto done;
2559 2e676fc5 2020-01-15 tracey
2560 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
2561 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
2562 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
2563 ec46ccd7 2020-01-15 tracey goto done;
2564 ec46ccd7 2020-01-15 tracey }
2565 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2566 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
2567 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
2568 ec46ccd7 2020-01-15 tracey goto done;
2569 a81f3554 2020-02-03 stsp
2570 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
2571 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
2572 ec46ccd7 2020-01-15 tracey bca.nlines--;
2573 ec46ccd7 2020-01-15 tracey
2574 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2575 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
2576 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
2577 ec46ccd7 2020-01-15 tracey goto done;
2578 ec46ccd7 2020-01-15 tracey }
2579 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
2580 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
2581 ec46ccd7 2020-01-15 tracey i = bca.nlines;
2582 ec46ccd7 2020-01-15 tracey while (i > 0) {
2583 ec46ccd7 2020-01-15 tracey i /= 10;
2584 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
2585 ec46ccd7 2020-01-15 tracey }
2586 ec46ccd7 2020-01-15 tracey bca.repo = repo;
2587 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
2588 ec46ccd7 2020-01-15 tracey
2589 147269d5 2020-01-15 tracey error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2590 147269d5 2020-01-15 tracey NULL, NULL);
2591 e46f587c 2020-01-29 tracey if (error)
2592 e46f587c 2020-01-29 tracey goto done;
2593 2e676fc5 2020-01-15 tracey if (buf_len(bca.blamebuf) > 0) {
2594 2e676fc5 2020-01-15 tracey error = buf_putc(bca.blamebuf, '\0');
2595 a81f3554 2020-02-03 stsp if (error)
2596 a81f3554 2020-02-03 stsp goto done;
2597 a81f3554 2020-02-03 stsp *blame_html = strdup(buf_get(bca.blamebuf));
2598 a81f3554 2020-02-03 stsp if (*blame_html == NULL) {
2599 a81f3554 2020-02-03 stsp error = got_error_from_errno("strdup");
2600 a81f3554 2020-02-03 stsp goto done;
2601 a81f3554 2020-02-03 stsp }
2602 2e676fc5 2020-01-15 tracey }
2603 ec46ccd7 2020-01-15 tracey done:
2604 e46f587c 2020-01-29 tracey free(bca.line_offsets);
2605 2e676fc5 2020-01-15 tracey free(bca.blamebuf);
2606 2e676fc5 2020-01-15 tracey free(in_repo_path);
2607 2e676fc5 2020-01-15 tracey free(commit_id);
2608 2e676fc5 2020-01-15 tracey free(obj_id);
2609 2e676fc5 2020-01-15 tracey free(path);
2610 2e676fc5 2020-01-15 tracey
2611 4fd4726a 2020-02-03 stsp for (i = 0; i < bca.nlines; i++) {
2612 4fd4726a 2020-02-03 stsp struct blame_line *bline = &bca.lines[i];
2613 4fd4726a 2020-02-03 stsp free(bline->id_str);
2614 4fd4726a 2020-02-03 stsp free(bline->committer);
2615 2e676fc5 2020-01-15 tracey }
2616 4fd4726a 2020-02-03 stsp free(bca.lines);
2617 2e676fc5 2020-01-15 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
2618 a81f3554 2020-02-03 stsp error = got_error_from_errno("fclose");
2619 4fd4726a 2020-02-03 stsp if (blob)
2620 4fd4726a 2020-02-03 stsp got_object_blob_close(blob);
2621 4fd4726a 2020-02-03 stsp if (repo)
2622 4fd4726a 2020-02-03 stsp got_repo_close(repo);
2623 4fd4726a 2020-02-03 stsp return error;
2624 4fd4726a 2020-02-03 stsp }
2625 4fd4726a 2020-02-03 stsp
2626 4fd4726a 2020-02-03 stsp static const struct got_error *
2627 5894d523 2020-02-03 stsp gw_get_file_read_blob(char **blobstr, size_t *filesize, struct gw_trans *gw_trans)
2628 4fd4726a 2020-02-03 stsp {
2629 4fd4726a 2020-02-03 stsp const struct got_error *error = NULL;
2630 4fd4726a 2020-02-03 stsp struct got_repository *repo = NULL;
2631 4fd4726a 2020-02-03 stsp struct got_object_id *obj_id = NULL;
2632 4fd4726a 2020-02-03 stsp struct got_object_id *commit_id = NULL;
2633 4fd4726a 2020-02-03 stsp struct got_blob_object *blob = NULL;
2634 4fd4726a 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL;
2635 4fd4726a 2020-02-03 stsp int obj_type;
2636 5894d523 2020-02-03 stsp size_t n;
2637 4fd4726a 2020-02-03 stsp FILE *f = NULL;
2638 4fd4726a 2020-02-03 stsp
2639 4fd4726a 2020-02-03 stsp *blobstr = NULL;
2640 5894d523 2020-02-03 stsp *filesize = 0;
2641 4fd4726a 2020-02-03 stsp
2642 4fd4726a 2020-02-03 stsp error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2643 4fd4726a 2020-02-03 stsp if (error)
2644 4fd4726a 2020-02-03 stsp return error;
2645 4fd4726a 2020-02-03 stsp
2646 4fd4726a 2020-02-03 stsp if (asprintf(&path, "%s%s%s",
2647 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2648 4fd4726a 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
2649 4fd4726a 2020-02-03 stsp gw_trans->repo_file) == -1) {
2650 4fd4726a 2020-02-03 stsp error = got_error_from_errno("asprintf");
2651 4fd4726a 2020-02-03 stsp goto done;
2652 4fd4726a 2020-02-03 stsp }
2653 4fd4726a 2020-02-03 stsp
2654 4fd4726a 2020-02-03 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2655 4fd4726a 2020-02-03 stsp if (error)
2656 4fd4726a 2020-02-03 stsp goto done;
2657 4fd4726a 2020-02-03 stsp
2658 4fd4726a 2020-02-03 stsp error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2659 4fd4726a 2020-02-03 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2660 4fd4726a 2020-02-03 stsp if (error)
2661 4fd4726a 2020-02-03 stsp goto done;
2662 4fd4726a 2020-02-03 stsp
2663 4fd4726a 2020-02-03 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2664 4fd4726a 2020-02-03 stsp if (error)
2665 4fd4726a 2020-02-03 stsp goto done;
2666 4fd4726a 2020-02-03 stsp
2667 4fd4726a 2020-02-03 stsp if (obj_id == NULL) {
2668 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_NO_OBJ);
2669 4fd4726a 2020-02-03 stsp goto done;
2670 4fd4726a 2020-02-03 stsp }
2671 4fd4726a 2020-02-03 stsp
2672 4fd4726a 2020-02-03 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2673 4fd4726a 2020-02-03 stsp if (error)
2674 4fd4726a 2020-02-03 stsp goto done;
2675 4fd4726a 2020-02-03 stsp
2676 4fd4726a 2020-02-03 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2677 4fd4726a 2020-02-03 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2678 4fd4726a 2020-02-03 stsp goto done;
2679 4fd4726a 2020-02-03 stsp }
2680 4fd4726a 2020-02-03 stsp
2681 4fd4726a 2020-02-03 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2682 4fd4726a 2020-02-03 stsp if (error)
2683 4fd4726a 2020-02-03 stsp goto done;
2684 4fd4726a 2020-02-03 stsp
2685 4fd4726a 2020-02-03 stsp f = got_opentemp();
2686 4fd4726a 2020-02-03 stsp if (f == NULL) {
2687 4fd4726a 2020-02-03 stsp error = got_error_from_errno("got_opentemp");
2688 4fd4726a 2020-02-03 stsp goto done;
2689 4fd4726a 2020-02-03 stsp }
2690 5894d523 2020-02-03 stsp error = got_object_blob_dump_to_file(filesize, NULL, NULL, f, blob);
2691 4fd4726a 2020-02-03 stsp if (error)
2692 4fd4726a 2020-02-03 stsp goto done;
2693 4fd4726a 2020-02-03 stsp
2694 4fd4726a 2020-02-03 stsp /* XXX This will fail on large files... */
2695 5894d523 2020-02-03 stsp *blobstr = calloc(*filesize + 1, sizeof(**blobstr));
2696 4fd4726a 2020-02-03 stsp if (*blobstr == NULL) {
2697 4fd4726a 2020-02-03 stsp error = got_error_from_errno("calloc");
2698 4fd4726a 2020-02-03 stsp goto done;
2699 4fd4726a 2020-02-03 stsp }
2700 4fd4726a 2020-02-03 stsp
2701 5894d523 2020-02-03 stsp n = fread(*blobstr, 1, *filesize, f);
2702 4fd4726a 2020-02-03 stsp if (n == 0) {
2703 4fd4726a 2020-02-03 stsp if (ferror(f))
2704 4fd4726a 2020-02-03 stsp error = got_ferror(f, GOT_ERR_IO);
2705 4fd4726a 2020-02-03 stsp goto done;
2706 4fd4726a 2020-02-03 stsp }
2707 4fd4726a 2020-02-03 stsp done:
2708 4fd4726a 2020-02-03 stsp free(in_repo_path);
2709 4fd4726a 2020-02-03 stsp free(commit_id);
2710 4fd4726a 2020-02-03 stsp free(obj_id);
2711 4fd4726a 2020-02-03 stsp free(path);
2712 e46f587c 2020-01-29 tracey if (blob)
2713 a81f3554 2020-02-03 stsp got_object_blob_close(blob);
2714 e46f587c 2020-01-29 tracey if (repo)
2715 a81f3554 2020-02-03 stsp got_repo_close(repo);
2716 4fd4726a 2020-02-03 stsp if (f != NULL && fclose(f) == -1 && error == NULL)
2717 4fd4726a 2020-02-03 stsp error = got_error_from_errno("fclose");
2718 5894d523 2020-02-03 stsp if (error) {
2719 5894d523 2020-02-03 stsp free(*blobstr);
2720 5894d523 2020-02-03 stsp *blobstr = NULL;
2721 5894d523 2020-02-03 stsp *filesize = 0;
2722 5894d523 2020-02-03 stsp }
2723 a81f3554 2020-02-03 stsp return error;
2724 ec46ccd7 2020-01-15 tracey }
2725 ec46ccd7 2020-01-15 tracey
2726 84bf4df2 2020-02-03 stsp static const struct got_error *
2727 84bf4df2 2020-02-03 stsp gw_get_repo_tree(char **tree_html, struct gw_trans *gw_trans)
2728 ec46ccd7 2020-01-15 tracey {
2729 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
2730 ec46ccd7 2020-01-15 tracey struct got_repository *repo = NULL;
2731 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
2732 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
2733 bcbc97d8 2020-01-15 tracey struct buf *diffbuf = NULL;
2734 bcbc97d8 2020-01-15 tracey size_t newsize;
2735 84bf4df2 2020-02-03 stsp char *path = NULL, *in_repo_path = NULL, *tree_row = NULL;
2736 84bf4df2 2020-02-03 stsp char *id_str = NULL;
2737 84bf4df2 2020-02-03 stsp char *build_folder = NULL;
2738 84bf4df2 2020-02-03 stsp char *url_html = NULL;
2739 84bf4df2 2020-02-03 stsp const char *class = NULL;
2740 c3bcdfd5 2020-01-29 tracey int nentries, i, class_flip = 0;
2741 8d4d2453 2020-01-15 tracey
2742 84bf4df2 2020-02-03 stsp *tree_html = NULL;
2743 84bf4df2 2020-02-03 stsp
2744 bcbc97d8 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2745 ec46ccd7 2020-01-15 tracey if (error)
2746 84bf4df2 2020-02-03 stsp return error;
2747 bcbc97d8 2020-01-15 tracey
2748 bcbc97d8 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2749 ec46ccd7 2020-01-15 tracey if (error)
2750 bcbc97d8 2020-01-15 tracey goto done;
2751 bcbc97d8 2020-01-15 tracey
2752 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL)
2753 ec46ccd7 2020-01-15 tracey path = strdup(gw_trans->repo_folder);
2754 84bf4df2 2020-02-03 stsp else {
2755 84bf4df2 2020-02-03 stsp error = got_repo_map_path(&in_repo_path, repo,
2756 84bf4df2 2020-02-03 stsp gw_trans->repo_path, 1);
2757 84bf4df2 2020-02-03 stsp if (error)
2758 84bf4df2 2020-02-03 stsp goto done;
2759 bcbc97d8 2020-01-15 tracey free(path);
2760 bcbc97d8 2020-01-15 tracey path = in_repo_path;
2761 bcbc97d8 2020-01-15 tracey }
2762 bcbc97d8 2020-01-15 tracey
2763 f2f46662 2020-01-23 tracey if (gw_trans->commit == NULL) {
2764 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
2765 ec46ccd7 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2766 ec46ccd7 2020-01-15 tracey if (error)
2767 ec46ccd7 2020-01-15 tracey goto done;
2768 ec46ccd7 2020-01-15 tracey error = got_ref_resolve(&commit_id, repo, head_ref);
2769 84bf4df2 2020-02-03 stsp if (error)
2770 84bf4df2 2020-02-03 stsp goto done;
2771 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
2772 ec46ccd7 2020-01-15 tracey
2773 84bf4df2 2020-02-03 stsp } else {
2774 f2f46662 2020-01-23 tracey error = got_repo_match_object_id(&commit_id, NULL,
2775 f2f46662 2020-01-23 tracey gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2776 84bf4df2 2020-02-03 stsp if (error)
2777 84bf4df2 2020-02-03 stsp goto done;
2778 84bf4df2 2020-02-03 stsp }
2779 f2f46662 2020-01-23 tracey
2780 f2f46662 2020-01-23 tracey error = got_object_id_str(&gw_trans->commit, commit_id);
2781 bcbc97d8 2020-01-15 tracey if (error)
2782 bcbc97d8 2020-01-15 tracey goto done;
2783 bcbc97d8 2020-01-15 tracey
2784 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2785 bcbc97d8 2020-01-15 tracey if (error)
2786 bcbc97d8 2020-01-15 tracey goto done;
2787 bcbc97d8 2020-01-15 tracey
2788 bcbc97d8 2020-01-15 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
2789 bcbc97d8 2020-01-15 tracey if (error)
2790 bcbc97d8 2020-01-15 tracey goto done;
2791 bcbc97d8 2020-01-15 tracey
2792 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
2793 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
2794 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
2795 ec46ccd7 2020-01-15 tracey const char *modestr = "";
2796 84bf4df2 2020-02-03 stsp mode_t mode;
2797 bcbc97d8 2020-01-15 tracey
2798 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
2799 bcbc97d8 2020-01-15 tracey
2800 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2801 bcbc97d8 2020-01-15 tracey if (error)
2802 bcbc97d8 2020-01-15 tracey goto done;
2803 bcbc97d8 2020-01-15 tracey
2804 84bf4df2 2020-02-03 stsp mode = got_tree_entry_get_mode(te);
2805 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
2806 bcbc97d8 2020-01-15 tracey modestr = "$";
2807 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
2808 bcbc97d8 2020-01-15 tracey modestr = "@";
2809 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
2810 bcbc97d8 2020-01-15 tracey modestr = "/";
2811 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
2812 bcbc97d8 2020-01-15 tracey modestr = "*";
2813 c3bcdfd5 2020-01-29 tracey
2814 c3bcdfd5 2020-01-29 tracey if (class_flip == 0) {
2815 84bf4df2 2020-02-03 stsp class = "back_lightgray";
2816 c3bcdfd5 2020-01-29 tracey class_flip = 1;
2817 c3bcdfd5 2020-01-29 tracey } else {
2818 84bf4df2 2020-02-03 stsp class = "back_white";
2819 c3bcdfd5 2020-01-29 tracey class_flip = 0;
2820 c3bcdfd5 2020-01-29 tracey }
2821 bcbc97d8 2020-01-15 tracey
2822 84bf4df2 2020-02-03 stsp if (S_ISDIR(mode)) {
2823 84bf4df2 2020-02-03 stsp if (asprintf(&build_folder, "%s%s%s",
2824 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? "/" : "",
2825 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2826 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te)) == -1) {
2827 84bf4df2 2020-02-03 stsp error = got_error_from_errno(
2828 84bf4df2 2020-02-03 stsp "asprintf");
2829 84bf4df2 2020-02-03 stsp goto done;
2830 ec46ccd7 2020-01-15 tracey }
2831 ec46ccd7 2020-01-15 tracey
2832 88d59c36 2020-01-29 stsp if (asprintf(&url_html, folder_html,
2833 ec46ccd7 2020-01-15 tracey gw_trans->repo_name, gw_trans->action_name,
2834 ec46ccd7 2020-01-15 tracey gw_trans->commit, build_folder,
2835 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
2836 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2837 ec46ccd7 2020-01-15 tracey goto done;
2838 ec46ccd7 2020-01-15 tracey }
2839 84bf4df2 2020-02-03 stsp if (asprintf(&tree_row, tree_line, class, url_html,
2840 84bf4df2 2020-02-03 stsp class) == -1) {
2841 84bf4df2 2020-02-03 stsp error = got_error_from_errno("asprintf");
2842 84bf4df2 2020-02-03 stsp goto done;
2843 84bf4df2 2020-02-03 stsp }
2844 ec46ccd7 2020-01-15 tracey } else {
2845 88d59c36 2020-01-29 stsp if (asprintf(&url_html, file_html, gw_trans->repo_name,
2846 e46f587c 2020-01-29 tracey "blob", gw_trans->commit,
2847 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
2848 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2849 88d59c36 2020-01-29 stsp got_tree_entry_get_name(te), modestr) == -1) {
2850 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2851 ec46ccd7 2020-01-15 tracey goto done;
2852 ec46ccd7 2020-01-15 tracey }
2853 e46f587c 2020-01-29 tracey
2854 e46f587c 2020-01-29 tracey if (asprintf(&tree_row, tree_line_with_navs, class,
2855 84bf4df2 2020-02-03 stsp url_html, class, gw_trans->repo_name, "blob",
2856 84bf4df2 2020-02-03 stsp gw_trans->commit, got_tree_entry_get_name(te),
2857 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2858 84bf4df2 2020-02-03 stsp "blob", gw_trans->repo_name,
2859 84bf4df2 2020-02-03 stsp "blame", gw_trans->commit,
2860 84bf4df2 2020-02-03 stsp got_tree_entry_get_name(te),
2861 84bf4df2 2020-02-03 stsp gw_trans->repo_folder ? gw_trans->repo_folder : "",
2862 84bf4df2 2020-02-03 stsp "blame") == -1) {
2863 e46f587c 2020-01-29 tracey error = got_error_from_errno("asprintf");
2864 e46f587c 2020-01-29 tracey goto done;
2865 e46f587c 2020-01-29 tracey }
2866 ec46ccd7 2020-01-15 tracey }
2867 ec46ccd7 2020-01-15 tracey
2868 bcbc97d8 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tree_row);
2869 ec46ccd7 2020-01-15 tracey if (error)
2870 ec46ccd7 2020-01-15 tracey goto done;
2871 ec46ccd7 2020-01-15 tracey
2872 bcbc97d8 2020-01-15 tracey free(id_str);
2873 84bf4df2 2020-02-03 stsp id_str = NULL;
2874 ec46ccd7 2020-01-15 tracey free(url_html);
2875 84bf4df2 2020-02-03 stsp url_html = NULL;
2876 ec46ccd7 2020-01-15 tracey free(tree_row);
2877 84bf4df2 2020-02-03 stsp tree_row = NULL;
2878 84bf4df2 2020-02-03 stsp free(build_folder);
2879 84bf4df2 2020-02-03 stsp build_folder = NULL;
2880 bcbc97d8 2020-01-15 tracey }
2881 bcbc97d8 2020-01-15 tracey
2882 bcbc97d8 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2883 bcbc97d8 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2884 84bf4df2 2020-02-03 stsp if (error)
2885 84bf4df2 2020-02-03 stsp goto done;
2886 84bf4df2 2020-02-03 stsp *tree_html = strdup(buf_get(diffbuf));
2887 84bf4df2 2020-02-03 stsp if (*tree_html == NULL) {
2888 84bf4df2 2020-02-03 stsp error = got_error_from_errno("strdup");
2889 84bf4df2 2020-02-03 stsp goto done;
2890 84bf4df2 2020-02-03 stsp }
2891 bcbc97d8 2020-01-15 tracey }
2892 bcbc97d8 2020-01-15 tracey done:
2893 bcbc97d8 2020-01-15 tracey if (tree)
2894 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
2895 2e676fc5 2020-01-15 tracey if (repo)
2896 2e676fc5 2020-01-15 tracey got_repo_close(repo);
2897 bcbc97d8 2020-01-15 tracey
2898 84bf4df2 2020-02-03 stsp free(id_str);
2899 84bf4df2 2020-02-03 stsp free(url_html);
2900 84bf4df2 2020-02-03 stsp free(tree_row);
2901 2e676fc5 2020-01-15 tracey free(in_repo_path);
2902 bcbc97d8 2020-01-15 tracey free(tree_id);
2903 bcbc97d8 2020-01-15 tracey free(diffbuf);
2904 84bf4df2 2020-02-03 stsp free(build_folder);
2905 84bf4df2 2020-02-03 stsp return error;
2906 bcbc97d8 2020-01-15 tracey }
2907 bcbc97d8 2020-01-15 tracey
2908 8d4d2453 2020-01-15 tracey static char *
2909 54415d85 2020-01-15 tracey gw_get_repo_heads(struct gw_trans *gw_trans)
2910 8d4d2453 2020-01-15 tracey {
2911 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2912 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2913 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2914 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2915 87f9ebf5 2020-01-15 tracey char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2916 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2917 87f9ebf5 2020-01-15 tracey size_t newsize;
2918 a0f36e03 2020-01-28 stsp
2919 a0f36e03 2020-01-28 stsp SIMPLEQ_INIT(&refs);
2920 8d4d2453 2020-01-15 tracey
2921 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2922 ec46ccd7 2020-01-15 tracey if (error)
2923 6c6c85af 2020-01-15 tracey return NULL;
2924 87f9ebf5 2020-01-15 tracey
2925 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2926 ec46ccd7 2020-01-15 tracey if (error)
2927 87f9ebf5 2020-01-15 tracey goto done;
2928 87f9ebf5 2020-01-15 tracey
2929 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2930 87f9ebf5 2020-01-15 tracey NULL);
2931 87f9ebf5 2020-01-15 tracey if (error)
2932 87f9ebf5 2020-01-15 tracey goto done;
2933 87f9ebf5 2020-01-15 tracey
2934 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2935 87f9ebf5 2020-01-15 tracey char *refname;
2936 87f9ebf5 2020-01-15 tracey
2937 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
2938 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
2939 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2940 87f9ebf5 2020-01-15 tracey goto done;
2941 87f9ebf5 2020-01-15 tracey }
2942 87f9ebf5 2020-01-15 tracey
2943 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
2944 87f9ebf5 2020-01-15 tracey free(refname);
2945 87f9ebf5 2020-01-15 tracey continue;
2946 87f9ebf5 2020-01-15 tracey }
2947 87f9ebf5 2020-01-15 tracey
2948 017d6da3 2020-01-29 tracey error = gw_get_repo_age(&age, gw_trans, gw_trans->gw_dir->path,
2949 017d6da3 2020-01-29 tracey refname, TM_DIFF);
2950 d126c1b7 2020-01-29 stsp if (error)
2951 d126c1b7 2020-01-29 stsp goto done;
2952 87f9ebf5 2020-01-15 tracey
2953 88d59c36 2020-01-29 stsp if (asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2954 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
2955 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
2956 88d59c36 2020-01-29 stsp refname) == -1) {
2957 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2958 87f9ebf5 2020-01-15 tracey goto done;
2959 87f9ebf5 2020-01-15 tracey }
2960 87f9ebf5 2020-01-15 tracey
2961 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
2962 87f9ebf5 2020-01-15 tracey refname += 11;
2963 87f9ebf5 2020-01-15 tracey
2964 88d59c36 2020-01-29 stsp if (asprintf(&head_row, heads_row, age, refname,
2965 88d59c36 2020-01-29 stsp head_navs_disp) == -1) {
2966 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2967 87f9ebf5 2020-01-15 tracey goto done;
2968 87f9ebf5 2020-01-15 tracey }
2969 87f9ebf5 2020-01-15 tracey
2970 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
2971 87f9ebf5 2020-01-15 tracey
2972 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
2973 87f9ebf5 2020-01-15 tracey free(head_row);
2974 87f9ebf5 2020-01-15 tracey }
2975 87f9ebf5 2020-01-15 tracey
2976 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2977 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2978 6c6c85af 2020-01-15 tracey heads = strdup(buf_get(diffbuf));
2979 6c6c85af 2020-01-15 tracey }
2980 87f9ebf5 2020-01-15 tracey done:
2981 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2982 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2983 87f9ebf5 2020-01-15 tracey if (repo)
2984 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2985 87f9ebf5 2020-01-15 tracey if (error)
2986 87f9ebf5 2020-01-15 tracey return NULL;
2987 87f9ebf5 2020-01-15 tracey else
2988 87f9ebf5 2020-01-15 tracey return heads;
2989 8d4d2453 2020-01-15 tracey }
2990 8d4d2453 2020-01-15 tracey
2991 8d4d2453 2020-01-15 tracey static char *
2992 54415d85 2020-01-15 tracey gw_get_got_link(struct gw_trans *gw_trans)
2993 2c251c14 2020-01-15 tracey {
2994 2c251c14 2020-01-15 tracey char *link;
2995 2c251c14 2020-01-15 tracey
2996 88d59c36 2020-01-29 stsp if (asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2997 88d59c36 2020-01-29 stsp gw_trans->gw_conf->got_logo) == -1)
2998 2c251c14 2020-01-15 tracey return NULL;
2999 2c251c14 2020-01-15 tracey
3000 2c251c14 2020-01-15 tracey return link;
3001 2c251c14 2020-01-15 tracey }
3002 2c251c14 2020-01-15 tracey
3003 2c251c14 2020-01-15 tracey static char *
3004 54415d85 2020-01-15 tracey gw_get_site_link(struct gw_trans *gw_trans)
3005 2c251c14 2020-01-15 tracey {
3006 eb89db64 2020-02-03 stsp char *link = NULL, *repo = NULL, *action = NULL;
3007 2c251c14 2020-01-15 tracey
3008 eb89db64 2020-02-03 stsp if (gw_trans->repo_name != NULL &&
3009 eb89db64 2020-02-03 stsp asprintf(&repo, " / <a href='?path=%s&action=summary'>%s</a>",
3010 eb89db64 2020-02-03 stsp gw_trans->repo_name, gw_trans->repo_name) == -1)
3011 eb89db64 2020-02-03 stsp return NULL;
3012 2c251c14 2020-01-15 tracey
3013 eb89db64 2020-02-03 stsp if (gw_trans->action_name != NULL &&
3014 eb89db64 2020-02-03 stsp asprintf(&action, " / %s", gw_trans->action_name) == -1) {
3015 eb89db64 2020-02-03 stsp free(repo);
3016 eb89db64 2020-02-03 stsp return NULL;
3017 eb89db64 2020-02-03 stsp }
3018 2c251c14 2020-01-15 tracey
3019 88d59c36 2020-01-29 stsp if (asprintf(&link, site_link, GOTWEB,
3020 eb89db64 2020-02-03 stsp gw_trans->gw_conf->got_site_link,
3021 eb89db64 2020-02-03 stsp repo ? repo : "", action ? action : "") == -1) {
3022 eb89db64 2020-02-03 stsp free(repo);
3023 eb89db64 2020-02-03 stsp free(action);
3024 2c251c14 2020-01-15 tracey return NULL;
3025 eb89db64 2020-02-03 stsp }
3026 2c251c14 2020-01-15 tracey
3027 eb89db64 2020-02-03 stsp free(repo);
3028 eb89db64 2020-02-03 stsp free(action);
3029 2c251c14 2020-01-15 tracey return link;
3030 2c251c14 2020-01-15 tracey }
3031 2c251c14 2020-01-15 tracey
3032 83eb9a68 2020-02-03 stsp static const struct got_error *
3033 83eb9a68 2020-02-03 stsp gw_colordiff_line(char **colorized_line, char *buf)
3034 ec46ccd7 2020-01-15 tracey {
3035 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
3036 83eb9a68 2020-02-03 stsp char *div_diff_line_div = NULL, *color = NULL;
3037 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
3038 ec46ccd7 2020-01-15 tracey size_t newsize;
3039 ec46ccd7 2020-01-15 tracey
3040 83eb9a68 2020-02-03 stsp *colorized_line = NULL;
3041 83eb9a68 2020-02-03 stsp
3042 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
3043 ec46ccd7 2020-01-15 tracey if (error)
3044 83eb9a68 2020-02-03 stsp return error;
3045 ec46ccd7 2020-01-15 tracey
3046 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
3047 ec46ccd7 2020-01-15 tracey color = "diff_minus";
3048 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "+", 1) == 0)
3049 ec46ccd7 2020-01-15 tracey color = "diff_plus";
3050 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
3051 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
3052 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "@@", 2) == 0)
3053 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
3054 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit +", 8) == 0)
3055 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3056 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "commit -", 8) == 0)
3057 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3058 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob +", 6) == 0)
3059 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3060 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "blob -", 6) == 0)
3061 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3062 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file +", 6) == 0)
3063 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3064 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "file -", 6) == 0)
3065 ec46ccd7 2020-01-15 tracey color = "diff_meta";
3066 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "from:", 5) == 0)
3067 ec46ccd7 2020-01-15 tracey color = "diff_author";
3068 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "via:", 4) == 0)
3069 ec46ccd7 2020-01-15 tracey color = "diff_author";
3070 83eb9a68 2020-02-03 stsp else if (strncmp(buf, "date:", 5) == 0)
3071 ec46ccd7 2020-01-15 tracey color = "diff_date";
3072 ec46ccd7 2020-01-15 tracey
3073 83eb9a68 2020-02-03 stsp if (asprintf(&div_diff_line_div, div_diff_line, color ? color : "")
3074 83eb9a68 2020-02-03 stsp == -1) {
3075 83eb9a68 2020-02-03 stsp error = got_error_from_errno("asprintf");
3076 83eb9a68 2020-02-03 stsp goto done;
3077 83eb9a68 2020-02-03 stsp }
3078 ec46ccd7 2020-01-15 tracey
3079 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_diff_line_div);
3080 ec46ccd7 2020-01-15 tracey if (error)
3081 83eb9a68 2020-02-03 stsp goto done;
3082 ec46ccd7 2020-01-15 tracey
3083 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, buf);
3084 ec46ccd7 2020-01-15 tracey if (error)
3085 83eb9a68 2020-02-03 stsp goto done;
3086 ec46ccd7 2020-01-15 tracey
3087 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
3088 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
3089 83eb9a68 2020-02-03 stsp *colorized_line = strdup(buf_get(diffbuf));
3090 83eb9a68 2020-02-03 stsp if (*colorized_line == NULL)
3091 83eb9a68 2020-02-03 stsp error = got_error_from_errno("strdup");
3092 ec46ccd7 2020-01-15 tracey }
3093 83eb9a68 2020-02-03 stsp done:
3094 ec46ccd7 2020-01-15 tracey free(diffbuf);
3095 ec46ccd7 2020-01-15 tracey free(div_diff_line_div);
3096 83eb9a68 2020-02-03 stsp return error;
3097 ec46ccd7 2020-01-15 tracey }
3098 ec46ccd7 2020-01-15 tracey
3099 070fee27 2020-02-03 stsp /*
3100 070fee27 2020-02-03 stsp * XXX This function should not exist.
3101 070fee27 2020-02-03 stsp * We should let khtml_puts(3) handle HTML escaping.
3102 070fee27 2020-02-03 stsp */
3103 070fee27 2020-02-03 stsp static const struct got_error *
3104 070fee27 2020-02-03 stsp gw_html_escape(char **escaped_html, const char *orig_html)
3105 2c251c14 2020-01-15 tracey {
3106 070fee27 2020-02-03 stsp const struct got_error *error = NULL;
3107 070fee27 2020-02-03 stsp struct escape_pair {
3108 070fee27 2020-02-03 stsp char c;
3109 070fee27 2020-02-03 stsp const char *s;
3110 070fee27 2020-02-03 stsp } esc[] = {
3111 070fee27 2020-02-03 stsp { '>', "&gt;" },
3112 070fee27 2020-02-03 stsp { '<', "&lt;" },
3113 070fee27 2020-02-03 stsp { '&', "&amp;" },
3114 070fee27 2020-02-03 stsp { '"', "&quot;" },
3115 070fee27 2020-02-03 stsp { '\'', "&apos;" },
3116 070fee27 2020-02-03 stsp { '\n', "<br />" },
3117 070fee27 2020-02-03 stsp };
3118 070fee27 2020-02-03 stsp size_t orig_len, len;
3119 070fee27 2020-02-03 stsp int i, j, x;
3120 2c251c14 2020-01-15 tracey
3121 070fee27 2020-02-03 stsp orig_len = strlen(orig_html);
3122 070fee27 2020-02-03 stsp len = orig_len;
3123 070fee27 2020-02-03 stsp for (i = 0; i < orig_len; i++) {
3124 070fee27 2020-02-03 stsp for (j = 0; j < nitems(esc); j++) {
3125 070fee27 2020-02-03 stsp if (orig_html[i] != esc[j].c)
3126 070fee27 2020-02-03 stsp continue;
3127 070fee27 2020-02-03 stsp len += strlen(esc[j].s) - 1 /* escaped char */;
3128 070fee27 2020-02-03 stsp }
3129 070fee27 2020-02-03 stsp }
3130 2c251c14 2020-01-15 tracey
3131 070fee27 2020-02-03 stsp *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
3132 070fee27 2020-02-03 stsp if (*escaped_html == NULL)
3133 070fee27 2020-02-03 stsp return got_error_from_errno("calloc");
3134 070fee27 2020-02-03 stsp
3135 070fee27 2020-02-03 stsp x = 0;
3136 070fee27 2020-02-03 stsp for (i = 0; i < orig_len; i++) {
3137 070fee27 2020-02-03 stsp int escaped = 0;
3138 070fee27 2020-02-03 stsp for (j = 0; j < nitems(esc); j++) {
3139 070fee27 2020-02-03 stsp if (orig_html[i] != esc[j].c)
3140 070fee27 2020-02-03 stsp continue;
3141 2c251c14 2020-01-15 tracey
3142 070fee27 2020-02-03 stsp if (strlcat(*escaped_html, esc[j].s, len + 1)
3143 070fee27 2020-02-03 stsp >= len + 1) {
3144 070fee27 2020-02-03 stsp error = got_error(GOT_ERR_NO_SPACE);
3145 070fee27 2020-02-03 stsp goto done;
3146 070fee27 2020-02-03 stsp }
3147 070fee27 2020-02-03 stsp x += strlen(esc[j].s);
3148 070fee27 2020-02-03 stsp escaped = 1;
3149 2c251c14 2020-01-15 tracey break;
3150 2c251c14 2020-01-15 tracey }
3151 070fee27 2020-02-03 stsp if (!escaped) {
3152 070fee27 2020-02-03 stsp (*escaped_html)[x] = orig_html[i];
3153 070fee27 2020-02-03 stsp x++;
3154 070fee27 2020-02-03 stsp }
3155 2c251c14 2020-01-15 tracey }
3156 070fee27 2020-02-03 stsp done:
3157 070fee27 2020-02-03 stsp if (error) {
3158 070fee27 2020-02-03 stsp free(*escaped_html);
3159 070fee27 2020-02-03 stsp *escaped_html = NULL;
3160 070fee27 2020-02-03 stsp } else {
3161 070fee27 2020-02-03 stsp (*escaped_html)[x] = '\0';
3162 070fee27 2020-02-03 stsp }
3163 070fee27 2020-02-03 stsp return error;
3164 2c251c14 2020-01-15 tracey }
3165 2c251c14 2020-01-15 tracey
3166 2c251c14 2020-01-15 tracey int
3167 c08369d7 2020-01-15 tracey main(int argc, char *argv[])
3168 2c251c14 2020-01-15 tracey {
3169 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
3170 54415d85 2020-01-15 tracey struct gw_trans *gw_trans;
3171 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
3172 2c251c14 2020-01-15 tracey const char *page = "index";
3173 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
3174 c25c2314 2020-01-28 stsp enum kcgi_err kerr;
3175 2c251c14 2020-01-15 tracey
3176 54415d85 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
3177 2c251c14 2020-01-15 tracey errx(1, "malloc");
3178 2c251c14 2020-01-15 tracey
3179 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
3180 2c251c14 2020-01-15 tracey errx(1, "malloc");
3181 2c251c14 2020-01-15 tracey
3182 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
3183 2c251c14 2020-01-15 tracey errx(1, "malloc");
3184 2c251c14 2020-01-15 tracey
3185 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
3186 2c251c14 2020-01-15 tracey errx(1, "malloc");
3187 2c251c14 2020-01-15 tracey
3188 c25c2314 2020-01-28 stsp kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
3189 c25c2314 2020-01-28 stsp if (kerr != KCGI_OK) {
3190 c25c2314 2020-01-28 stsp error = gw_kcgi_error(kerr);
3191 c119608e 2020-01-28 stsp goto done;
3192 c25c2314 2020-01-28 stsp }
3193 2c251c14 2020-01-15 tracey
3194 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
3195 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
3196 4ceb8155 2020-01-15 tracey gw_malloc = 0;
3197 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
3198 c119608e 2020-01-28 stsp goto done;
3199 46b9c89b 2020-01-15 tracey }
3200 46b9c89b 2020-01-15 tracey
3201 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
3202 f2f46662 2020-01-23 tracey TAILQ_INIT(&gw_trans->gw_headers);
3203 2c251c14 2020-01-15 tracey
3204 2c251c14 2020-01-15 tracey gw_trans->page = 0;
3205 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
3206 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
3207 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
3208 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
3209 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
3210 54415d85 2020-01-15 tracey gw_trans->gw_tmpl->key = gw_templs;
3211 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
3212 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
3213 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
3214 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
3215 c119608e 2020-01-28 stsp if (error)
3216 2c251c14 2020-01-15 tracey goto done;
3217 2c251c14 2020-01-15 tracey
3218 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
3219 2c251c14 2020-01-15 tracey if (error)
3220 c119608e 2020-01-28 stsp goto done;
3221 2c251c14 2020-01-15 tracey
3222 017d6da3 2020-01-29 tracey if (gw_trans->action == GW_BLOB)
3223 017d6da3 2020-01-29 tracey error = gw_blob(gw_trans);
3224 017d6da3 2020-01-29 tracey else
3225 017d6da3 2020-01-29 tracey error = gw_display_index(gw_trans);
3226 2c251c14 2020-01-15 tracey done:
3227 c119608e 2020-01-28 stsp if (error) {
3228 c119608e 2020-01-28 stsp gw_trans->mime = KMIME_TEXT_PLAIN;
3229 c119608e 2020-01-28 stsp gw_trans->action = GW_ERR;
3230 6d8d8a26 2020-01-29 stsp gw_display_error(gw_trans, error);
3231 c119608e 2020-01-28 stsp }
3232 2c251c14 2020-01-15 tracey if (gw_malloc) {
3233 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
3234 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
3235 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
3236 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
3237 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
3238 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
3239 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
3240 2c251c14 2020-01-15 tracey free(gw_trans->commit);
3241 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
3242 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
3243 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
3244 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
3245 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
3246 2c251c14 2020-01-15 tracey
3247 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
3248 2c251c14 2020-01-15 tracey free(dir->name);
3249 2c251c14 2020-01-15 tracey free(dir->description);
3250 2c251c14 2020-01-15 tracey free(dir->age);
3251 2c251c14 2020-01-15 tracey free(dir->url);
3252 2c251c14 2020-01-15 tracey free(dir->path);
3253 2c251c14 2020-01-15 tracey free(dir);
3254 2c251c14 2020-01-15 tracey }
3255 2c251c14 2020-01-15 tracey
3256 2c251c14 2020-01-15 tracey }
3257 2c251c14 2020-01-15 tracey
3258 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
3259 d56b34ca 2020-01-29 stsp return 0;
3260 2c251c14 2020-01-15 tracey }