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