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