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