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