Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 9f7d7167 2018-04-29 stsp
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 9f7d7167 2018-04-29 stsp
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 d6b05b5b 2018-08-04 stsp };
107 c3e9aa98 2019-05-13 jcs
108 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
109 d6b05b5b 2018-08-04 stsp
110 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
111 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
112 ba4f502b 2018-08-04 stsp struct got_object_id *id;
113 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
114 1a76625f 2018-10-22 stsp int idx;
115 ba4f502b 2018-08-04 stsp };
116 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 ba4f502b 2018-08-04 stsp struct commit_queue {
118 ba4f502b 2018-08-04 stsp int ncommits;
119 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
120 6d17833f 2019-11-08 stsp };
121 6d17833f 2019-11-08 stsp
122 f26dddb7 2019-11-08 stsp struct tog_color {
123 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
124 6d17833f 2019-11-08 stsp regex_t regex;
125 6d17833f 2019-11-08 stsp short colorpair;
126 15a087fe 2019-02-21 stsp };
127 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
128 11b20872 2019-11-08 stsp
129 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
131 cc488aa7 2022-01-23 stsp
132 cc488aa7 2022-01-23 stsp static const struct got_error *
133 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 cc488aa7 2022-01-23 stsp struct got_reference* re2)
135 cc488aa7 2022-01-23 stsp {
136 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
137 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
138 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
139 cc488aa7 2022-01-23 stsp
140 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
141 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
144 cc488aa7 2022-01-23 stsp *cmp = -1;
145 cc488aa7 2022-01-23 stsp return NULL;
146 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
147 cc488aa7 2022-01-23 stsp *cmp = 1;
148 cc488aa7 2022-01-23 stsp return NULL;
149 cc488aa7 2022-01-23 stsp }
150 cc488aa7 2022-01-23 stsp
151 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 cc488aa7 2022-01-23 stsp return NULL;
153 cc488aa7 2022-01-23 stsp }
154 51a10b52 2020-12-26 stsp
155 11b20872 2019-11-08 stsp static const struct got_error *
156 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
157 51a10b52 2020-12-26 stsp {
158 51a10b52 2020-12-26 stsp const struct got_error *err;
159 51a10b52 2020-12-26 stsp
160 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 7f66531d 2021-11-16 stsp repo);
163 51a10b52 2020-12-26 stsp if (err)
164 51a10b52 2020-12-26 stsp return err;
165 51a10b52 2020-12-26 stsp
166 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 51a10b52 2020-12-26 stsp repo);
168 51a10b52 2020-12-26 stsp }
169 51a10b52 2020-12-26 stsp
170 51a10b52 2020-12-26 stsp static void
171 51a10b52 2020-12-26 stsp tog_free_refs(void)
172 51a10b52 2020-12-26 stsp {
173 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
174 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
175 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
176 51a10b52 2020-12-26 stsp }
177 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
178 51a10b52 2020-12-26 stsp }
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp static const struct got_error *
181 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
182 11b20872 2019-11-08 stsp int idx, short color)
183 11b20872 2019-11-08 stsp {
184 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
185 11b20872 2019-11-08 stsp struct tog_color *tc;
186 11b20872 2019-11-08 stsp int regerr = 0;
187 11b20872 2019-11-08 stsp
188 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
189 11b20872 2019-11-08 stsp return NULL;
190 11b20872 2019-11-08 stsp
191 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
192 11b20872 2019-11-08 stsp
193 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
194 11b20872 2019-11-08 stsp if (tc == NULL)
195 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
196 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
197 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 11b20872 2019-11-08 stsp if (regerr) {
199 11b20872 2019-11-08 stsp static char regerr_msg[512];
200 11b20872 2019-11-08 stsp static char err_msg[512];
201 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
202 11b20872 2019-11-08 stsp sizeof(regerr_msg));
203 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 11b20872 2019-11-08 stsp regerr_msg);
205 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 11b20872 2019-11-08 stsp free(tc);
207 11b20872 2019-11-08 stsp return err;
208 11b20872 2019-11-08 stsp }
209 11b20872 2019-11-08 stsp tc->colorpair = idx;
210 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
211 11b20872 2019-11-08 stsp return NULL;
212 11b20872 2019-11-08 stsp }
213 11b20872 2019-11-08 stsp
214 11b20872 2019-11-08 stsp static void
215 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
216 11b20872 2019-11-08 stsp {
217 11b20872 2019-11-08 stsp struct tog_color *tc;
218 11b20872 2019-11-08 stsp
219 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
220 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
221 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
222 11b20872 2019-11-08 stsp regfree(&tc->regex);
223 11b20872 2019-11-08 stsp free(tc);
224 11b20872 2019-11-08 stsp }
225 11b20872 2019-11-08 stsp }
226 11b20872 2019-11-08 stsp
227 11b20872 2019-11-08 stsp struct tog_color *
228 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
229 11b20872 2019-11-08 stsp {
230 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
231 11b20872 2019-11-08 stsp
232 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
233 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
234 11b20872 2019-11-08 stsp return tc;
235 11b20872 2019-11-08 stsp }
236 11b20872 2019-11-08 stsp
237 11b20872 2019-11-08 stsp return NULL;
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp
240 11b20872 2019-11-08 stsp static int
241 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
242 11b20872 2019-11-08 stsp {
243 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
245 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 11b20872 2019-11-08 stsp return COLOR_CYAN;
247 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 11b20872 2019-11-08 stsp return COLOR_YELLOW;
249 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 11b20872 2019-11-08 stsp return COLOR_GREEN;
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 91b8c405 2020-01-25 stsp return COLOR_CYAN;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 11b20872 2019-11-08 stsp return COLOR_GREEN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 11b20872 2019-11-08 stsp return COLOR_CYAN;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 6458efa5 2020-11-24 stsp return COLOR_GREEN;
267 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
269 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
271 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
273 11b20872 2019-11-08 stsp
274 11b20872 2019-11-08 stsp return -1;
275 11b20872 2019-11-08 stsp }
276 11b20872 2019-11-08 stsp
277 11b20872 2019-11-08 stsp static int
278 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
279 11b20872 2019-11-08 stsp {
280 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
281 11b20872 2019-11-08 stsp
282 11b20872 2019-11-08 stsp if (val == NULL)
283 11b20872 2019-11-08 stsp return default_color_value(envvar);
284 15a087fe 2019-02-21 stsp
285 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
286 11b20872 2019-11-08 stsp return COLOR_BLACK;
287 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
288 11b20872 2019-11-08 stsp return COLOR_RED;
289 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
290 11b20872 2019-11-08 stsp return COLOR_GREEN;
291 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
292 11b20872 2019-11-08 stsp return COLOR_YELLOW;
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLUE;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
296 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
298 11b20872 2019-11-08 stsp return COLOR_CYAN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
300 11b20872 2019-11-08 stsp return COLOR_WHITE;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
302 11b20872 2019-11-08 stsp return -1;
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp return default_color_value(envvar);
305 11b20872 2019-11-08 stsp }
306 11b20872 2019-11-08 stsp
307 11b20872 2019-11-08 stsp
308 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
309 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
310 3dbaef42 2020-11-24 stsp const char *label1, *label2;
311 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
312 15a087fe 2019-02-21 stsp int first_displayed_line;
313 15a087fe 2019-02-21 stsp int last_displayed_line;
314 15a087fe 2019-02-21 stsp int eof;
315 15a087fe 2019-02-21 stsp int diff_context;
316 3dbaef42 2020-11-24 stsp int ignore_whitespace;
317 64453f7e 2020-11-21 stsp int force_text_diff;
318 15a087fe 2019-02-21 stsp struct got_repository *repo;
319 bddb1296 2019-11-08 stsp struct tog_colors colors;
320 fe621944 2020-11-10 stsp size_t nlines;
321 f44b1f58 2020-02-02 tracey off_t *line_offsets;
322 f44b1f58 2020-02-02 tracey int matched_line;
323 f44b1f58 2020-02-02 tracey int selected_line;
324 15a087fe 2019-02-21 stsp
325 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
326 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
327 b01e7d3b 2018-08-04 stsp };
328 b01e7d3b 2018-08-04 stsp
329 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
330 1a76625f 2018-10-22 stsp
331 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
332 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
333 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
334 1a76625f 2018-10-22 stsp int commits_needed;
335 fb280deb 2021-08-30 stsp int load_all;
336 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
337 1a76625f 2018-10-22 stsp struct commit_queue *commits;
338 1a76625f 2018-10-22 stsp const char *in_repo_path;
339 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
340 1a76625f 2018-10-22 stsp struct got_repository *repo;
341 74467cc8 2022-06-15 stsp int *pack_fds;
342 1a76625f 2018-10-22 stsp int log_complete;
343 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
344 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
345 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
346 13add988 2019-10-15 stsp int *searching;
347 13add988 2019-10-15 stsp int *search_next_done;
348 13add988 2019-10-15 stsp regex_t *regex;
349 1a76625f 2018-10-22 stsp };
350 1a76625f 2018-10-22 stsp
351 1a76625f 2018-10-22 stsp struct tog_log_view_state {
352 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
353 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
354 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
355 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
356 b01e7d3b 2018-08-04 stsp int selected;
357 b01e7d3b 2018-08-04 stsp char *in_repo_path;
358 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
359 b672a97a 2020-01-27 stsp int log_branches;
360 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
361 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
362 1a76625f 2018-10-22 stsp sig_atomic_t quit;
363 1a76625f 2018-10-22 stsp pthread_t thread;
364 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
365 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
366 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
367 11b20872 2019-11-08 stsp struct tog_colors colors;
368 ba4f502b 2018-08-04 stsp };
369 11b20872 2019-11-08 stsp
370 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
371 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
372 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
374 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
375 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
376 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
377 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
378 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
379 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
380 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
381 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
382 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
383 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
384 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
385 ba4f502b 2018-08-04 stsp
386 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
387 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
388 e9424729 2018-08-04 stsp int nlines;
389 e9424729 2018-08-04 stsp
390 e9424729 2018-08-04 stsp struct tog_view *view;
391 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
392 e9424729 2018-08-04 stsp int *quit;
393 e9424729 2018-08-04 stsp };
394 e9424729 2018-08-04 stsp
395 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
396 e9424729 2018-08-04 stsp const char *path;
397 e9424729 2018-08-04 stsp struct got_repository *repo;
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
399 e9424729 2018-08-04 stsp int *complete;
400 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
401 fc06ba56 2019-08-22 stsp void *cancel_arg;
402 e9424729 2018-08-04 stsp };
403 e9424729 2018-08-04 stsp
404 e9424729 2018-08-04 stsp struct tog_blame {
405 e9424729 2018-08-04 stsp FILE *f;
406 be659d10 2020-11-18 stsp off_t filesize;
407 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
408 6fcac457 2018-11-19 stsp int nlines;
409 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
410 e9424729 2018-08-04 stsp pthread_t thread;
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
412 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
413 e9424729 2018-08-04 stsp const char *path;
414 0ae84acc 2022-06-15 tracey int *pack_fds;
415 e9424729 2018-08-04 stsp };
416 e9424729 2018-08-04 stsp
417 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
418 7cbe629d 2018-08-04 stsp int first_displayed_line;
419 7cbe629d 2018-08-04 stsp int last_displayed_line;
420 7cbe629d 2018-08-04 stsp int selected_line;
421 7cbe629d 2018-08-04 stsp int blame_complete;
422 e5a0f69f 2018-08-18 stsp int eof;
423 e5a0f69f 2018-08-18 stsp int done;
424 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
425 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
426 e5a0f69f 2018-08-18 stsp char *path;
427 7cbe629d 2018-08-04 stsp struct got_repository *repo;
428 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp struct tog_blame blame;
430 6c4c42e0 2019-06-24 stsp int matched_line;
431 11b20872 2019-11-08 stsp struct tog_colors colors;
432 ad80ab7b 2018-08-04 stsp };
433 ad80ab7b 2018-08-04 stsp
434 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
435 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
436 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
437 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
438 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
439 ad80ab7b 2018-08-04 stsp int selected;
440 ad80ab7b 2018-08-04 stsp };
441 ad80ab7b 2018-08-04 stsp
442 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
443 ad80ab7b 2018-08-04 stsp
444 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
445 ad80ab7b 2018-08-04 stsp char *tree_label;
446 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
447 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
448 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
453 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
455 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
456 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
457 6458efa5 2020-11-24 stsp struct tog_colors colors;
458 6458efa5 2020-11-24 stsp };
459 6458efa5 2020-11-24 stsp
460 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
461 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
462 6458efa5 2020-11-24 stsp struct got_reference *ref;
463 6458efa5 2020-11-24 stsp int idx;
464 6458efa5 2020-11-24 stsp };
465 6458efa5 2020-11-24 stsp
466 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
467 6458efa5 2020-11-24 stsp
468 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
469 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
471 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
472 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
473 7f66531d 2021-11-16 stsp int nrefs, ndisplayed, selected, show_ids, sort_by_date;
474 6458efa5 2020-11-24 stsp struct got_repository *repo;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
476 bddb1296 2019-11-08 stsp struct tog_colors colors;
477 7cbe629d 2018-08-04 stsp };
478 7cbe629d 2018-08-04 stsp
479 669b5ffa 2018-10-07 stsp /*
480 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
481 669b5ffa 2018-10-07 stsp *
482 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
483 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
484 669b5ffa 2018-10-07 stsp * there is enough screen estate.
485 669b5ffa 2018-10-07 stsp *
486 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
487 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
488 669b5ffa 2018-10-07 stsp *
489 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
490 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
491 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
492 669b5ffa 2018-10-07 stsp *
493 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
494 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
495 669b5ffa 2018-10-07 stsp */
496 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
497 669b5ffa 2018-10-07 stsp
498 cc3c9aac 2018-08-01 stsp struct tog_view {
499 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
500 26ed57b2 2018-05-19 stsp WINDOW *window;
501 26ed57b2 2018-05-19 stsp PANEL *panel;
502 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
503 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
504 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
505 9970f7fc 2020-12-03 stsp int dying;
506 669b5ffa 2018-10-07 stsp struct tog_view *parent;
507 669b5ffa 2018-10-07 stsp struct tog_view *child;
508 5dc9f4bc 2018-08-04 stsp
509 e78dc838 2020-12-04 stsp /*
510 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
511 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
512 e78dc838 2020-12-04 stsp * between parent and child.
513 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
514 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
515 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
516 e78dc838 2020-12-04 stsp * situations.
517 e78dc838 2020-12-04 stsp */
518 e78dc838 2020-12-04 stsp int focus_child;
519 e78dc838 2020-12-04 stsp
520 5dc9f4bc 2018-08-04 stsp /* type-specific state */
521 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
522 5dc9f4bc 2018-08-04 stsp union {
523 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
524 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
525 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
526 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
527 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
528 5dc9f4bc 2018-08-04 stsp } state;
529 e5a0f69f 2018-08-18 stsp
530 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
531 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
532 e78dc838 2020-12-04 stsp struct tog_view *, int);
533 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
534 60493ae3 2019-06-20 stsp
535 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
536 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
537 c0c4acc8 2021-01-24 stsp int search_started;
538 60493ae3 2019-06-20 stsp int searching;
539 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
540 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
541 60493ae3 2019-06-20 stsp int search_next_done;
542 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
543 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
544 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
545 1803e47f 2019-06-22 stsp regex_t regex;
546 41605754 2020-11-12 stsp regmatch_t regmatch;
547 cc3c9aac 2018-08-01 stsp };
548 cd0acaa7 2018-05-20 stsp
549 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
550 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
551 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
552 78756c87 2020-11-24 stsp struct got_repository *);
553 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
554 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
555 e78dc838 2020-12-04 stsp struct tog_view *, int);
556 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
557 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
558 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
559 e5a0f69f 2018-08-18 stsp
560 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
561 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
562 78756c87 2020-11-24 stsp const char *, const char *, int);
563 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
565 e78dc838 2020-12-04 stsp struct tog_view *, int);
566 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
567 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
568 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp
570 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
571 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
572 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
573 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
574 e78dc838 2020-12-04 stsp struct tog_view *, int);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
576 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
577 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp
579 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
580 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
581 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
583 e78dc838 2020-12-04 stsp struct tog_view *, int);
584 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
585 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
586 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
587 6458efa5 2020-11-24 stsp
588 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
589 6458efa5 2020-11-24 stsp struct got_repository *);
590 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
591 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
592 e78dc838 2020-12-04 stsp struct tog_view *, int);
593 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
594 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
595 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
596 25791caa 2018-10-24 stsp
597 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
598 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
599 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
600 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
601 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
602 25791caa 2018-10-24 stsp
603 25791caa 2018-10-24 stsp static void
604 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
605 25791caa 2018-10-24 stsp {
606 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
607 83baff54 2019-08-12 stsp }
608 83baff54 2019-08-12 stsp
609 83baff54 2019-08-12 stsp static void
610 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
611 83baff54 2019-08-12 stsp {
612 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
613 25791caa 2018-10-24 stsp }
614 26ed57b2 2018-05-19 stsp
615 61266923 2020-01-14 stsp static void
616 61266923 2020-01-14 stsp tog_sigcont(int signo)
617 61266923 2020-01-14 stsp {
618 61266923 2020-01-14 stsp tog_sigcont_received = 1;
619 61266923 2020-01-14 stsp }
620 61266923 2020-01-14 stsp
621 2497f032 2022-05-31 stsp static void
622 2497f032 2022-05-31 stsp tog_sigint(int signo)
623 2497f032 2022-05-31 stsp {
624 2497f032 2022-05-31 stsp tog_sigint_received = 1;
625 2497f032 2022-05-31 stsp }
626 2497f032 2022-05-31 stsp
627 2497f032 2022-05-31 stsp static void
628 2497f032 2022-05-31 stsp tog_sigterm(int signo)
629 2497f032 2022-05-31 stsp {
630 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
631 2497f032 2022-05-31 stsp }
632 2497f032 2022-05-31 stsp
633 2497f032 2022-05-31 stsp static int
634 2497f032 2022-05-31 stsp tog_fatal_signal_received()
635 2497f032 2022-05-31 stsp {
636 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
637 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
638 2497f032 2022-05-31 stsp }
639 2497f032 2022-05-31 stsp
640 2497f032 2022-05-31 stsp
641 e5a0f69f 2018-08-18 stsp static const struct got_error *
642 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
643 ea5e7bb5 2018-08-01 stsp {
644 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
645 e5a0f69f 2018-08-18 stsp
646 669b5ffa 2018-10-07 stsp if (view->child) {
647 669b5ffa 2018-10-07 stsp view_close(view->child);
648 669b5ffa 2018-10-07 stsp view->child = NULL;
649 669b5ffa 2018-10-07 stsp }
650 e5a0f69f 2018-08-18 stsp if (view->close)
651 e5a0f69f 2018-08-18 stsp err = view->close(view);
652 ea5e7bb5 2018-08-01 stsp if (view->panel)
653 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
654 ea5e7bb5 2018-08-01 stsp if (view->window)
655 ea5e7bb5 2018-08-01 stsp delwin(view->window);
656 ea5e7bb5 2018-08-01 stsp free(view);
657 e5a0f69f 2018-08-18 stsp return err;
658 ea5e7bb5 2018-08-01 stsp }
659 ea5e7bb5 2018-08-01 stsp
660 ea5e7bb5 2018-08-01 stsp static struct tog_view *
661 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
662 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
663 ea5e7bb5 2018-08-01 stsp {
664 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
665 ea5e7bb5 2018-08-01 stsp
666 ea5e7bb5 2018-08-01 stsp if (view == NULL)
667 ea5e7bb5 2018-08-01 stsp return NULL;
668 ea5e7bb5 2018-08-01 stsp
669 d6b05b5b 2018-08-04 stsp view->type = type;
670 f7d12f7e 2018-08-01 stsp view->lines = LINES;
671 f7d12f7e 2018-08-01 stsp view->cols = COLS;
672 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
673 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
674 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
675 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
676 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
677 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
678 96a765a8 2018-08-04 stsp view_close(view);
679 ea5e7bb5 2018-08-01 stsp return NULL;
680 ea5e7bb5 2018-08-01 stsp }
681 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
682 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
683 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
684 96a765a8 2018-08-04 stsp view_close(view);
685 ea5e7bb5 2018-08-01 stsp return NULL;
686 ea5e7bb5 2018-08-01 stsp }
687 ea5e7bb5 2018-08-01 stsp
688 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
689 ea5e7bb5 2018-08-01 stsp return view;
690 cdf1ee82 2018-08-01 stsp }
691 cdf1ee82 2018-08-01 stsp
692 0cf4efb1 2018-09-29 stsp static int
693 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
694 0cf4efb1 2018-09-29 stsp {
695 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
696 0cf4efb1 2018-09-29 stsp return 0;
697 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
698 5c60c32a 2018-10-18 stsp }
699 5c60c32a 2018-10-18 stsp
700 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
701 5c60c32a 2018-10-18 stsp
702 5c60c32a 2018-10-18 stsp static const struct got_error *
703 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
704 5c60c32a 2018-10-18 stsp {
705 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
706 5c60c32a 2018-10-18 stsp
707 5c60c32a 2018-10-18 stsp view->begin_y = 0;
708 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
709 5c60c32a 2018-10-18 stsp view->nlines = LINES;
710 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
711 5c60c32a 2018-10-18 stsp view->lines = LINES;
712 5c60c32a 2018-10-18 stsp view->cols = COLS;
713 5c60c32a 2018-10-18 stsp err = view_resize(view);
714 5c60c32a 2018-10-18 stsp if (err)
715 5c60c32a 2018-10-18 stsp return err;
716 5c60c32a 2018-10-18 stsp
717 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
718 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
719 5c60c32a 2018-10-18 stsp
720 5c60c32a 2018-10-18 stsp return NULL;
721 5c60c32a 2018-10-18 stsp }
722 5c60c32a 2018-10-18 stsp
723 5c60c32a 2018-10-18 stsp static const struct got_error *
724 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
725 5c60c32a 2018-10-18 stsp {
726 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
727 5c60c32a 2018-10-18 stsp
728 5c60c32a 2018-10-18 stsp view->begin_x = 0;
729 5c60c32a 2018-10-18 stsp view->begin_y = 0;
730 5c60c32a 2018-10-18 stsp view->nlines = LINES;
731 5c60c32a 2018-10-18 stsp view->ncols = COLS;
732 5c60c32a 2018-10-18 stsp view->lines = LINES;
733 5c60c32a 2018-10-18 stsp view->cols = COLS;
734 5c60c32a 2018-10-18 stsp err = view_resize(view);
735 5c60c32a 2018-10-18 stsp if (err)
736 5c60c32a 2018-10-18 stsp return err;
737 5c60c32a 2018-10-18 stsp
738 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
739 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
740 5c60c32a 2018-10-18 stsp
741 5c60c32a 2018-10-18 stsp return NULL;
742 0cf4efb1 2018-09-29 stsp }
743 0cf4efb1 2018-09-29 stsp
744 5c60c32a 2018-10-18 stsp static int
745 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
746 5c60c32a 2018-10-18 stsp {
747 5c60c32a 2018-10-18 stsp return view->parent == NULL;
748 5c60c32a 2018-10-18 stsp }
749 5c60c32a 2018-10-18 stsp
750 4d8c2215 2018-08-19 stsp static const struct got_error *
751 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
752 f7d12f7e 2018-08-01 stsp {
753 f7d12f7e 2018-08-01 stsp int nlines, ncols;
754 f7d12f7e 2018-08-01 stsp
755 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
756 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
757 0cf4efb1 2018-09-29 stsp else
758 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
759 f7d12f7e 2018-08-01 stsp
760 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
761 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
762 0cf4efb1 2018-09-29 stsp else
763 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
764 f7d12f7e 2018-08-01 stsp
765 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
766 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
767 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
768 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
769 25791caa 2018-10-24 stsp wclear(view->window);
770 f7d12f7e 2018-08-01 stsp
771 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
772 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
773 0cf4efb1 2018-09-29 stsp view->lines = LINES;
774 0cf4efb1 2018-09-29 stsp view->cols = COLS;
775 6d0fee91 2018-08-01 stsp
776 6e3e5d9c 2018-10-18 stsp if (view->child) {
777 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
778 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
779 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
780 5c60c32a 2018-10-18 stsp if (view->child->focussed)
781 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
782 5c60c32a 2018-10-18 stsp else
783 5c60c32a 2018-10-18 stsp show_panel(view->panel);
784 5c60c32a 2018-10-18 stsp } else {
785 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
786 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
787 5c60c32a 2018-10-18 stsp }
788 5c60c32a 2018-10-18 stsp }
789 669b5ffa 2018-10-07 stsp
790 5c60c32a 2018-10-18 stsp return NULL;
791 669b5ffa 2018-10-07 stsp }
792 669b5ffa 2018-10-07 stsp
793 669b5ffa 2018-10-07 stsp static const struct got_error *
794 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
795 669b5ffa 2018-10-07 stsp {
796 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
797 669b5ffa 2018-10-07 stsp
798 669b5ffa 2018-10-07 stsp if (view->child == NULL)
799 669b5ffa 2018-10-07 stsp return NULL;
800 669b5ffa 2018-10-07 stsp
801 669b5ffa 2018-10-07 stsp err = view_close(view->child);
802 669b5ffa 2018-10-07 stsp view->child = NULL;
803 669b5ffa 2018-10-07 stsp return err;
804 669b5ffa 2018-10-07 stsp }
805 669b5ffa 2018-10-07 stsp
806 72a9cb46 2020-12-03 stsp static void
807 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
808 669b5ffa 2018-10-07 stsp {
809 669b5ffa 2018-10-07 stsp view->child = child;
810 669b5ffa 2018-10-07 stsp child->parent = view;
811 bfddd0d9 2018-09-29 stsp }
812 bfddd0d9 2018-09-29 stsp
813 bfddd0d9 2018-09-29 stsp static int
814 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
815 bfddd0d9 2018-09-29 stsp {
816 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
817 34bc9ec9 2019-02-22 stsp }
818 34bc9ec9 2019-02-22 stsp
819 34bc9ec9 2019-02-22 stsp static void
820 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
821 25791caa 2018-10-24 stsp {
822 25791caa 2018-10-24 stsp int cols, lines;
823 25791caa 2018-10-24 stsp struct winsize size;
824 25791caa 2018-10-24 stsp
825 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
826 25791caa 2018-10-24 stsp cols = 80; /* Default */
827 25791caa 2018-10-24 stsp lines = 24;
828 25791caa 2018-10-24 stsp } else {
829 25791caa 2018-10-24 stsp cols = size.ws_col;
830 25791caa 2018-10-24 stsp lines = size.ws_row;
831 25791caa 2018-10-24 stsp }
832 25791caa 2018-10-24 stsp resize_term(lines, cols);
833 2b49a8ae 2019-06-22 stsp }
834 2b49a8ae 2019-06-22 stsp
835 2b49a8ae 2019-06-22 stsp static const struct got_error *
836 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
837 2b49a8ae 2019-06-22 stsp {
838 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
839 2b49a8ae 2019-06-22 stsp char pattern[1024];
840 2b49a8ae 2019-06-22 stsp int ret;
841 c0c4acc8 2021-01-24 stsp
842 c0c4acc8 2021-01-24 stsp if (view->search_started) {
843 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
844 c0c4acc8 2021-01-24 stsp view->searching = 0;
845 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
846 c0c4acc8 2021-01-24 stsp }
847 c0c4acc8 2021-01-24 stsp view->search_started = 0;
848 2b49a8ae 2019-06-22 stsp
849 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
850 2b49a8ae 2019-06-22 stsp return NULL;
851 2b49a8ae 2019-06-22 stsp
852 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
853 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
854 2b49a8ae 2019-06-22 stsp
855 2b49a8ae 2019-06-22 stsp nocbreak();
856 2b49a8ae 2019-06-22 stsp echo();
857 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
858 2b49a8ae 2019-06-22 stsp cbreak();
859 2b49a8ae 2019-06-22 stsp noecho();
860 2b49a8ae 2019-06-22 stsp if (ret == ERR)
861 2b49a8ae 2019-06-22 stsp return NULL;
862 2b49a8ae 2019-06-22 stsp
863 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
864 7c32bd05 2019-06-22 stsp err = view->search_start(view);
865 7c32bd05 2019-06-22 stsp if (err) {
866 7c32bd05 2019-06-22 stsp regfree(&view->regex);
867 7c32bd05 2019-06-22 stsp return err;
868 7c32bd05 2019-06-22 stsp }
869 c0c4acc8 2021-01-24 stsp view->search_started = 1;
870 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
871 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
872 2b49a8ae 2019-06-22 stsp view->search_next(view);
873 2b49a8ae 2019-06-22 stsp }
874 2b49a8ae 2019-06-22 stsp
875 2b49a8ae 2019-06-22 stsp return NULL;
876 0cf4efb1 2018-09-29 stsp }
877 6d0fee91 2018-08-01 stsp
878 0cf4efb1 2018-09-29 stsp static const struct got_error *
879 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
880 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
881 e5a0f69f 2018-08-18 stsp {
882 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
883 669b5ffa 2018-10-07 stsp struct tog_view *v;
884 1a76625f 2018-10-22 stsp int ch, errcode;
885 e5a0f69f 2018-08-18 stsp
886 e5a0f69f 2018-08-18 stsp *new = NULL;
887 8f4ed634 2020-03-26 stsp
888 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
889 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
890 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
891 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
892 e5a0f69f 2018-08-18 stsp
893 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
894 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
895 82954512 2020-02-03 stsp if (errcode)
896 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
897 82954512 2020-02-03 stsp "pthread_mutex_unlock");
898 3da8ef85 2021-09-21 stsp sched_yield();
899 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
900 82954512 2020-02-03 stsp if (errcode)
901 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
902 82954512 2020-02-03 stsp "pthread_mutex_lock");
903 60493ae3 2019-06-20 stsp view->search_next(view);
904 60493ae3 2019-06-20 stsp return NULL;
905 60493ae3 2019-06-20 stsp }
906 60493ae3 2019-06-20 stsp
907 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
908 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
909 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
910 1a76625f 2018-10-22 stsp if (errcode)
911 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
912 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
913 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
914 1a76625f 2018-10-22 stsp if (errcode)
915 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
916 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
917 25791caa 2018-10-24 stsp
918 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
919 25791caa 2018-10-24 stsp tog_resizeterm();
920 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
921 61266923 2020-01-14 stsp tog_sigcont_received = 0;
922 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
923 25791caa 2018-10-24 stsp err = view_resize(v);
924 25791caa 2018-10-24 stsp if (err)
925 25791caa 2018-10-24 stsp return err;
926 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
927 25791caa 2018-10-24 stsp if (err)
928 25791caa 2018-10-24 stsp return err;
929 cdfcfb03 2020-12-06 stsp if (v->child) {
930 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
931 cdfcfb03 2020-12-06 stsp if (err)
932 cdfcfb03 2020-12-06 stsp return err;
933 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
934 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
935 cdfcfb03 2020-12-06 stsp if (err)
936 cdfcfb03 2020-12-06 stsp return err;
937 cdfcfb03 2020-12-06 stsp }
938 25791caa 2018-10-24 stsp }
939 25791caa 2018-10-24 stsp }
940 25791caa 2018-10-24 stsp
941 e5a0f69f 2018-08-18 stsp switch (ch) {
942 1e37a5c2 2019-05-12 jcs case '\t':
943 1e37a5c2 2019-05-12 jcs if (view->child) {
944 e78dc838 2020-12-04 stsp view->focussed = 0;
945 e78dc838 2020-12-04 stsp view->child->focussed = 1;
946 e78dc838 2020-12-04 stsp view->focus_child = 1;
947 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
948 e78dc838 2020-12-04 stsp view->focussed = 0;
949 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
950 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
951 1e37a5c2 2019-05-12 jcs }
952 1e37a5c2 2019-05-12 jcs break;
953 1e37a5c2 2019-05-12 jcs case 'q':
954 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
955 9970f7fc 2020-12-03 stsp view->dying = 1;
956 1e37a5c2 2019-05-12 jcs break;
957 1e37a5c2 2019-05-12 jcs case 'Q':
958 1e37a5c2 2019-05-12 jcs *done = 1;
959 1e37a5c2 2019-05-12 jcs break;
960 1e37a5c2 2019-05-12 jcs case 'f':
961 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
962 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
963 1e37a5c2 2019-05-12 jcs break;
964 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
965 e78dc838 2020-12-04 stsp view->focussed = 0;
966 e78dc838 2020-12-04 stsp view->child->focussed = 1;
967 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
968 1e37a5c2 2019-05-12 jcs } else
969 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
970 1e37a5c2 2019-05-12 jcs if (err)
971 1e37a5c2 2019-05-12 jcs break;
972 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
973 9970f7fc 2020-12-03 stsp KEY_RESIZE);
974 1e37a5c2 2019-05-12 jcs } else {
975 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
976 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
977 e78dc838 2020-12-04 stsp view->focussed = 1;
978 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
979 1e37a5c2 2019-05-12 jcs } else {
980 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
981 669b5ffa 2018-10-07 stsp }
982 1e37a5c2 2019-05-12 jcs if (err)
983 1e37a5c2 2019-05-12 jcs break;
984 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
985 1e37a5c2 2019-05-12 jcs }
986 1e37a5c2 2019-05-12 jcs break;
987 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
988 60493ae3 2019-06-20 stsp break;
989 60493ae3 2019-06-20 stsp case '/':
990 60493ae3 2019-06-20 stsp if (view->search_start)
991 2b49a8ae 2019-06-22 stsp view_search_start(view);
992 60493ae3 2019-06-20 stsp else
993 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
994 1e37a5c2 2019-05-12 jcs break;
995 b1bf1435 2019-06-21 stsp case 'N':
996 60493ae3 2019-06-20 stsp case 'n':
997 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
998 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
999 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1000 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1001 60493ae3 2019-06-20 stsp view->search_next(view);
1002 60493ae3 2019-06-20 stsp } else
1003 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1004 60493ae3 2019-06-20 stsp break;
1005 1e37a5c2 2019-05-12 jcs default:
1006 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1007 1e37a5c2 2019-05-12 jcs break;
1008 e5a0f69f 2018-08-18 stsp }
1009 e5a0f69f 2018-08-18 stsp
1010 e5a0f69f 2018-08-18 stsp return err;
1011 e5a0f69f 2018-08-18 stsp }
1012 e5a0f69f 2018-08-18 stsp
1013 1a57306a 2018-09-02 stsp void
1014 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1015 1a57306a 2018-09-02 stsp {
1016 0cf4efb1 2018-09-29 stsp PANEL *panel;
1017 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1018 0cf4efb1 2018-09-29 stsp
1019 669b5ffa 2018-10-07 stsp if (view->parent)
1020 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1021 669b5ffa 2018-10-07 stsp
1022 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1023 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1024 1a57306a 2018-09-02 stsp return;
1025 1a57306a 2018-09-02 stsp
1026 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1027 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1028 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1029 bcbd79e2 2018-08-19 stsp }
1030 bcbd79e2 2018-08-19 stsp
1031 a3404814 2018-09-02 stsp int
1032 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1033 a3404814 2018-09-02 stsp {
1034 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1035 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1036 669b5ffa 2018-10-07 stsp return 0;
1037 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1038 669b5ffa 2018-10-07 stsp return 0;
1039 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1040 a3404814 2018-09-02 stsp return 0;
1041 a3404814 2018-09-02 stsp
1042 669b5ffa 2018-10-07 stsp return view->focussed;
1043 a3404814 2018-09-02 stsp }
1044 a3404814 2018-09-02 stsp
1045 bcbd79e2 2018-08-19 stsp static const struct got_error *
1046 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1047 e5a0f69f 2018-08-18 stsp {
1048 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1049 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1050 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1051 fd823528 2018-10-22 stsp int fast_refresh = 10;
1052 1a76625f 2018-10-22 stsp int done = 0, errcode;
1053 e5a0f69f 2018-08-18 stsp
1054 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1055 1a76625f 2018-10-22 stsp if (errcode)
1056 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1057 1a76625f 2018-10-22 stsp
1058 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1059 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1060 e5a0f69f 2018-08-18 stsp
1061 1004088d 2018-09-29 stsp view->focussed = 1;
1062 878940b7 2018-09-29 stsp err = view->show(view);
1063 0cf4efb1 2018-09-29 stsp if (err)
1064 0cf4efb1 2018-09-29 stsp return err;
1065 0cf4efb1 2018-09-29 stsp update_panels();
1066 0cf4efb1 2018-09-29 stsp doupdate();
1067 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1068 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1069 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1070 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1071 fd823528 2018-10-22 stsp
1072 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1073 e5a0f69f 2018-08-18 stsp if (err)
1074 e5a0f69f 2018-08-18 stsp break;
1075 9970f7fc 2020-12-03 stsp if (view->dying) {
1076 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1077 669b5ffa 2018-10-07 stsp
1078 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1079 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1080 9970f7fc 2020-12-03 stsp entry);
1081 e78dc838 2020-12-04 stsp else if (view->parent)
1082 669b5ffa 2018-10-07 stsp prev = view->parent;
1083 669b5ffa 2018-10-07 stsp
1084 e78dc838 2020-12-04 stsp if (view->parent) {
1085 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1086 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1087 e78dc838 2020-12-04 stsp } else
1088 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1089 669b5ffa 2018-10-07 stsp
1090 9970f7fc 2020-12-03 stsp err = view_close(view);
1091 fb59748f 2020-12-05 stsp if (err)
1092 e5a0f69f 2018-08-18 stsp goto done;
1093 669b5ffa 2018-10-07 stsp
1094 e78dc838 2020-12-04 stsp view = NULL;
1095 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1096 e78dc838 2020-12-04 stsp if (v->focussed)
1097 e78dc838 2020-12-04 stsp break;
1098 0cf4efb1 2018-09-29 stsp }
1099 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1100 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1101 e78dc838 2020-12-04 stsp if (prev)
1102 e78dc838 2020-12-04 stsp view = prev;
1103 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1104 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1105 e78dc838 2020-12-04 stsp tog_view_list_head);
1106 e78dc838 2020-12-04 stsp }
1107 e78dc838 2020-12-04 stsp if (view) {
1108 e78dc838 2020-12-04 stsp if (view->focus_child) {
1109 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1110 e78dc838 2020-12-04 stsp view = view->child;
1111 e78dc838 2020-12-04 stsp } else
1112 e78dc838 2020-12-04 stsp view->focussed = 1;
1113 e78dc838 2020-12-04 stsp }
1114 e78dc838 2020-12-04 stsp }
1115 e5a0f69f 2018-08-18 stsp }
1116 bcbd79e2 2018-08-19 stsp if (new_view) {
1117 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1118 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1119 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1120 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1121 86c66b02 2018-10-18 stsp continue;
1122 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1123 86c66b02 2018-10-18 stsp err = view_close(v);
1124 86c66b02 2018-10-18 stsp if (err)
1125 86c66b02 2018-10-18 stsp goto done;
1126 86c66b02 2018-10-18 stsp break;
1127 86c66b02 2018-10-18 stsp }
1128 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1129 fed7eaa8 2018-10-24 stsp view = new_view;
1130 0ae84acc 2022-06-15 tracey }
1131 669b5ffa 2018-10-07 stsp if (view) {
1132 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1133 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1134 e78dc838 2020-12-04 stsp view = view->child;
1135 e78dc838 2020-12-04 stsp } else {
1136 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1137 e78dc838 2020-12-04 stsp view = view->parent;
1138 1a76625f 2018-10-22 stsp }
1139 e78dc838 2020-12-04 stsp show_panel(view->panel);
1140 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1141 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1142 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1143 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1144 669b5ffa 2018-10-07 stsp if (err)
1145 1a76625f 2018-10-22 stsp goto done;
1146 669b5ffa 2018-10-07 stsp }
1147 669b5ffa 2018-10-07 stsp err = view->show(view);
1148 0cf4efb1 2018-09-29 stsp if (err)
1149 1a76625f 2018-10-22 stsp goto done;
1150 669b5ffa 2018-10-07 stsp if (view->child) {
1151 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1152 669b5ffa 2018-10-07 stsp if (err)
1153 1a76625f 2018-10-22 stsp goto done;
1154 669b5ffa 2018-10-07 stsp }
1155 1a76625f 2018-10-22 stsp update_panels();
1156 1a76625f 2018-10-22 stsp doupdate();
1157 0cf4efb1 2018-09-29 stsp }
1158 e5a0f69f 2018-08-18 stsp }
1159 e5a0f69f 2018-08-18 stsp done:
1160 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1161 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1162 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1163 e5a0f69f 2018-08-18 stsp view_close(view);
1164 e5a0f69f 2018-08-18 stsp }
1165 1a76625f 2018-10-22 stsp
1166 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1167 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1168 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1169 1a76625f 2018-10-22 stsp
1170 e5a0f69f 2018-08-18 stsp return err;
1171 ea5e7bb5 2018-08-01 stsp }
1172 ea5e7bb5 2018-08-01 stsp
1173 4ed7e80c 2018-05-20 stsp __dead static void
1174 9f7d7167 2018-04-29 stsp usage_log(void)
1175 9f7d7167 2018-04-29 stsp {
1176 80ddbec8 2018-04-29 stsp endwin();
1177 c70c5802 2018-08-01 stsp fprintf(stderr,
1178 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1179 9f7d7167 2018-04-29 stsp getprogname());
1180 9f7d7167 2018-04-29 stsp exit(1);
1181 80ddbec8 2018-04-29 stsp }
1182 80ddbec8 2018-04-29 stsp
1183 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1184 80ddbec8 2018-04-29 stsp static const struct got_error *
1185 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1186 963b370f 2018-05-20 stsp {
1187 00dfcb92 2018-06-11 stsp char *vis = NULL;
1188 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1189 963b370f 2018-05-20 stsp
1190 963b370f 2018-05-20 stsp *ws = NULL;
1191 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1192 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1193 00dfcb92 2018-06-11 stsp int vislen;
1194 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1195 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1196 00dfcb92 2018-06-11 stsp
1197 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1198 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1199 00dfcb92 2018-06-11 stsp if (err)
1200 00dfcb92 2018-06-11 stsp return err;
1201 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1202 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1203 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1204 a7f50699 2018-06-11 stsp goto done;
1205 a7f50699 2018-06-11 stsp }
1206 00dfcb92 2018-06-11 stsp }
1207 963b370f 2018-05-20 stsp
1208 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1209 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1210 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1211 a7f50699 2018-06-11 stsp goto done;
1212 a7f50699 2018-06-11 stsp }
1213 963b370f 2018-05-20 stsp
1214 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1215 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1216 a7f50699 2018-06-11 stsp done:
1217 00dfcb92 2018-06-11 stsp free(vis);
1218 963b370f 2018-05-20 stsp if (err) {
1219 963b370f 2018-05-20 stsp free(*ws);
1220 963b370f 2018-05-20 stsp *ws = NULL;
1221 963b370f 2018-05-20 stsp *wlen = 0;
1222 963b370f 2018-05-20 stsp }
1223 963b370f 2018-05-20 stsp return err;
1224 963b370f 2018-05-20 stsp }
1225 963b370f 2018-05-20 stsp
1226 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1227 963b370f 2018-05-20 stsp static const struct got_error *
1228 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1229 27a741e5 2019-09-11 stsp int col_tab_align)
1230 963b370f 2018-05-20 stsp {
1231 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1232 963b370f 2018-05-20 stsp int cols = 0;
1233 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1234 963b370f 2018-05-20 stsp size_t wlen;
1235 963b370f 2018-05-20 stsp int i;
1236 963b370f 2018-05-20 stsp
1237 963b370f 2018-05-20 stsp *wlinep = NULL;
1238 b700b5d6 2018-07-10 stsp *widthp = 0;
1239 963b370f 2018-05-20 stsp
1240 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1241 963b370f 2018-05-20 stsp if (err)
1242 963b370f 2018-05-20 stsp return err;
1243 963b370f 2018-05-20 stsp
1244 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1245 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1246 3f670bfb 2020-12-10 stsp wlen--;
1247 3f670bfb 2020-12-10 stsp }
1248 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1249 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1250 3f670bfb 2020-12-10 stsp wlen--;
1251 3f670bfb 2020-12-10 stsp }
1252 3f670bfb 2020-12-10 stsp
1253 963b370f 2018-05-20 stsp i = 0;
1254 27a741e5 2019-09-11 stsp while (i < wlen) {
1255 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1256 27a741e5 2019-09-11 stsp
1257 27a741e5 2019-09-11 stsp if (width == 0) {
1258 b700b5d6 2018-07-10 stsp i++;
1259 27a741e5 2019-09-11 stsp continue;
1260 27a741e5 2019-09-11 stsp }
1261 27a741e5 2019-09-11 stsp
1262 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1263 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1264 27a741e5 2019-09-11 stsp break;
1265 27a741e5 2019-09-11 stsp cols += width;
1266 3c1f04f1 2018-09-13 stsp i++;
1267 27a741e5 2019-09-11 stsp } else if (width == -1) {
1268 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1269 27a741e5 2019-09-11 stsp width = TABSIZE -
1270 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1271 748d5cab 2020-12-14 naddy } else {
1272 748d5cab 2020-12-14 naddy width = 1;
1273 748d5cab 2020-12-14 naddy wline[i] = L'.';
1274 27a741e5 2019-09-11 stsp }
1275 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1276 748d5cab 2020-12-14 naddy break;
1277 748d5cab 2020-12-14 naddy cols += width;
1278 b700b5d6 2018-07-10 stsp i++;
1279 27a741e5 2019-09-11 stsp } else {
1280 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1281 963b370f 2018-05-20 stsp goto done;
1282 963b370f 2018-05-20 stsp }
1283 963b370f 2018-05-20 stsp }
1284 963b370f 2018-05-20 stsp wline[i] = L'\0';
1285 b700b5d6 2018-07-10 stsp if (widthp)
1286 b700b5d6 2018-07-10 stsp *widthp = cols;
1287 963b370f 2018-05-20 stsp done:
1288 963b370f 2018-05-20 stsp if (err)
1289 963b370f 2018-05-20 stsp free(wline);
1290 963b370f 2018-05-20 stsp else
1291 963b370f 2018-05-20 stsp *wlinep = wline;
1292 963b370f 2018-05-20 stsp return err;
1293 963b370f 2018-05-20 stsp }
1294 963b370f 2018-05-20 stsp
1295 8b473291 2019-02-21 stsp static const struct got_error*
1296 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1297 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1298 8b473291 2019-02-21 stsp {
1299 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1300 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1301 8b473291 2019-02-21 stsp char *s;
1302 8b473291 2019-02-21 stsp const char *name;
1303 8b473291 2019-02-21 stsp
1304 8b473291 2019-02-21 stsp *refs_str = NULL;
1305 8b473291 2019-02-21 stsp
1306 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1307 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1308 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1309 52b5abe1 2019-08-13 stsp int cmp;
1310 52b5abe1 2019-08-13 stsp
1311 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1312 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1313 8b473291 2019-02-21 stsp continue;
1314 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1315 8b473291 2019-02-21 stsp name += 5;
1316 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1317 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1318 7143d404 2019-03-12 stsp continue;
1319 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1320 8b473291 2019-02-21 stsp name += 6;
1321 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1322 8b473291 2019-02-21 stsp name += 8;
1323 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1324 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1325 79cc719f 2020-04-24 stsp continue;
1326 79cc719f 2020-04-24 stsp }
1327 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1328 48cae60d 2020-09-22 stsp if (err)
1329 48cae60d 2020-09-22 stsp break;
1330 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1331 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1332 5d844a1e 2019-08-13 stsp if (err) {
1333 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1334 48cae60d 2020-09-22 stsp free(ref_id);
1335 5d844a1e 2019-08-13 stsp break;
1336 48cae60d 2020-09-22 stsp }
1337 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1338 5d844a1e 2019-08-13 stsp err = NULL;
1339 5d844a1e 2019-08-13 stsp tag = NULL;
1340 5d844a1e 2019-08-13 stsp }
1341 52b5abe1 2019-08-13 stsp }
1342 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1343 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1344 48cae60d 2020-09-22 stsp free(ref_id);
1345 52b5abe1 2019-08-13 stsp if (tag)
1346 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1347 52b5abe1 2019-08-13 stsp if (cmp != 0)
1348 52b5abe1 2019-08-13 stsp continue;
1349 8b473291 2019-02-21 stsp s = *refs_str;
1350 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1351 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1352 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1353 8b473291 2019-02-21 stsp free(s);
1354 8b473291 2019-02-21 stsp *refs_str = NULL;
1355 8b473291 2019-02-21 stsp break;
1356 8b473291 2019-02-21 stsp }
1357 8b473291 2019-02-21 stsp free(s);
1358 8b473291 2019-02-21 stsp }
1359 8b473291 2019-02-21 stsp
1360 8b473291 2019-02-21 stsp return err;
1361 8b473291 2019-02-21 stsp }
1362 8b473291 2019-02-21 stsp
1363 963b370f 2018-05-20 stsp static const struct got_error *
1364 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1365 27a741e5 2019-09-11 stsp int col_tab_align)
1366 5813d178 2019-03-09 stsp {
1367 e6b8b890 2020-12-29 naddy char *smallerthan;
1368 5813d178 2019-03-09 stsp
1369 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1370 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1371 5813d178 2019-03-09 stsp author = smallerthan + 1;
1372 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1373 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1374 5813d178 2019-03-09 stsp }
1375 5813d178 2019-03-09 stsp
1376 5813d178 2019-03-09 stsp static const struct got_error *
1377 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1378 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1379 8fdc79fe 2020-12-01 naddy int author_display_cols)
1380 80ddbec8 2018-04-29 stsp {
1381 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1382 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1383 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1384 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1385 5813d178 2019-03-09 stsp char *author = NULL;
1386 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1387 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1388 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1389 bb737323 2018-05-20 stsp int col, limit;
1390 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1391 ccb26ccd 2018-11-05 stsp struct tm tm;
1392 45d799e2 2018-12-23 stsp time_t committer_time;
1393 11b20872 2019-11-08 stsp struct tog_color *tc;
1394 80ddbec8 2018-04-29 stsp
1395 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1396 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1397 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1398 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1399 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1400 b39d25c7 2018-07-10 stsp
1401 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1402 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1403 b39d25c7 2018-07-10 stsp else
1404 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1405 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1406 11b20872 2019-11-08 stsp if (tc)
1407 11b20872 2019-11-08 stsp wattr_on(view->window,
1408 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1409 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1410 11b20872 2019-11-08 stsp if (tc)
1411 11b20872 2019-11-08 stsp wattr_off(view->window,
1412 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1413 27a741e5 2019-09-11 stsp col = limit;
1414 b39d25c7 2018-07-10 stsp if (col > avail)
1415 b39d25c7 2018-07-10 stsp goto done;
1416 6570a66d 2019-11-08 stsp
1417 6570a66d 2019-11-08 stsp if (avail >= 120) {
1418 6570a66d 2019-11-08 stsp char *id_str;
1419 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1420 6570a66d 2019-11-08 stsp if (err)
1421 6570a66d 2019-11-08 stsp goto done;
1422 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1423 11b20872 2019-11-08 stsp if (tc)
1424 11b20872 2019-11-08 stsp wattr_on(view->window,
1425 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1426 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1427 11b20872 2019-11-08 stsp if (tc)
1428 11b20872 2019-11-08 stsp wattr_off(view->window,
1429 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1430 6570a66d 2019-11-08 stsp free(id_str);
1431 6570a66d 2019-11-08 stsp col += 9;
1432 6570a66d 2019-11-08 stsp if (col > avail)
1433 6570a66d 2019-11-08 stsp goto done;
1434 6570a66d 2019-11-08 stsp }
1435 b39d25c7 2018-07-10 stsp
1436 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1437 5813d178 2019-03-09 stsp if (author == NULL) {
1438 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1439 80ddbec8 2018-04-29 stsp goto done;
1440 80ddbec8 2018-04-29 stsp }
1441 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1442 bb737323 2018-05-20 stsp if (err)
1443 bb737323 2018-05-20 stsp goto done;
1444 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1445 11b20872 2019-11-08 stsp if (tc)
1446 11b20872 2019-11-08 stsp wattr_on(view->window,
1447 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1448 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1449 11b20872 2019-11-08 stsp if (tc)
1450 11b20872 2019-11-08 stsp wattr_off(view->window,
1451 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1452 bb737323 2018-05-20 stsp col += author_width;
1453 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1454 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1455 bb737323 2018-05-20 stsp col++;
1456 bb737323 2018-05-20 stsp author_width++;
1457 bb737323 2018-05-20 stsp }
1458 9c2eaf34 2018-05-20 stsp if (col > avail)
1459 9c2eaf34 2018-05-20 stsp goto done;
1460 80ddbec8 2018-04-29 stsp
1461 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1462 5943eee2 2019-08-13 stsp if (err)
1463 6d9fbc00 2018-04-29 stsp goto done;
1464 bb737323 2018-05-20 stsp logmsg = logmsg0;
1465 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1466 bb737323 2018-05-20 stsp logmsg++;
1467 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1468 bb737323 2018-05-20 stsp if (newline)
1469 bb737323 2018-05-20 stsp *newline = '\0';
1470 bb737323 2018-05-20 stsp limit = avail - col;
1471 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1472 bb737323 2018-05-20 stsp if (err)
1473 bb737323 2018-05-20 stsp goto done;
1474 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1475 bb737323 2018-05-20 stsp col += logmsg_width;
1476 27a741e5 2019-09-11 stsp while (col < avail) {
1477 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1478 bb737323 2018-05-20 stsp col++;
1479 881b2d3e 2018-04-30 stsp }
1480 80ddbec8 2018-04-29 stsp done:
1481 80ddbec8 2018-04-29 stsp free(logmsg0);
1482 bb737323 2018-05-20 stsp free(wlogmsg);
1483 5813d178 2019-03-09 stsp free(author);
1484 bb737323 2018-05-20 stsp free(wauthor);
1485 80ddbec8 2018-04-29 stsp free(line);
1486 80ddbec8 2018-04-29 stsp return err;
1487 80ddbec8 2018-04-29 stsp }
1488 26ed57b2 2018-05-19 stsp
1489 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1490 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1491 899d86c2 2018-05-10 stsp struct got_object_id *id)
1492 80ddbec8 2018-04-29 stsp {
1493 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1494 80ddbec8 2018-04-29 stsp
1495 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1496 80ddbec8 2018-04-29 stsp if (entry == NULL)
1497 899d86c2 2018-05-10 stsp return NULL;
1498 99db9666 2018-05-07 stsp
1499 899d86c2 2018-05-10 stsp entry->id = id;
1500 99db9666 2018-05-07 stsp entry->commit = commit;
1501 899d86c2 2018-05-10 stsp return entry;
1502 99db9666 2018-05-07 stsp }
1503 80ddbec8 2018-04-29 stsp
1504 99db9666 2018-05-07 stsp static void
1505 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1506 99db9666 2018-05-07 stsp {
1507 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1508 99db9666 2018-05-07 stsp
1509 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1510 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1511 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1512 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1513 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1514 99db9666 2018-05-07 stsp free(entry);
1515 99db9666 2018-05-07 stsp }
1516 99db9666 2018-05-07 stsp
1517 99db9666 2018-05-07 stsp static void
1518 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1519 99db9666 2018-05-07 stsp {
1520 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1521 99db9666 2018-05-07 stsp pop_commit(commits);
1522 c4972b91 2018-05-07 stsp }
1523 c4972b91 2018-05-07 stsp
1524 c4972b91 2018-05-07 stsp static const struct got_error *
1525 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1526 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1527 13add988 2019-10-15 stsp {
1528 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1529 13add988 2019-10-15 stsp regmatch_t regmatch;
1530 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1531 13add988 2019-10-15 stsp
1532 13add988 2019-10-15 stsp *have_match = 0;
1533 13add988 2019-10-15 stsp
1534 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1535 13add988 2019-10-15 stsp if (err)
1536 13add988 2019-10-15 stsp return err;
1537 13add988 2019-10-15 stsp
1538 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1539 13add988 2019-10-15 stsp if (err)
1540 13add988 2019-10-15 stsp goto done;
1541 13add988 2019-10-15 stsp
1542 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1543 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1544 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1545 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1546 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1547 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1548 13add988 2019-10-15 stsp *have_match = 1;
1549 13add988 2019-10-15 stsp done:
1550 13add988 2019-10-15 stsp free(id_str);
1551 13add988 2019-10-15 stsp free(logmsg);
1552 13add988 2019-10-15 stsp return err;
1553 13add988 2019-10-15 stsp }
1554 13add988 2019-10-15 stsp
1555 13add988 2019-10-15 stsp static const struct got_error *
1556 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1557 c4972b91 2018-05-07 stsp {
1558 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1559 9ba79e04 2018-06-11 stsp
1560 1a76625f 2018-10-22 stsp /*
1561 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1562 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1563 1a76625f 2018-10-22 stsp * while updating the display.
1564 1a76625f 2018-10-22 stsp */
1565 4e0d2870 2020-12-07 naddy do {
1566 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1567 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1568 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1569 1a76625f 2018-10-22 stsp int errcode;
1570 899d86c2 2018-05-10 stsp
1571 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1572 4e0d2870 2020-12-07 naddy NULL, NULL);
1573 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1574 ecb28ae0 2018-07-16 stsp break;
1575 899d86c2 2018-05-10 stsp
1576 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1577 9ba79e04 2018-06-11 stsp if (err)
1578 9ba79e04 2018-06-11 stsp break;
1579 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1580 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1581 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1582 9ba79e04 2018-06-11 stsp break;
1583 9ba79e04 2018-06-11 stsp }
1584 93e45b7c 2018-09-24 stsp
1585 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1586 1a76625f 2018-10-22 stsp if (errcode) {
1587 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1588 13add988 2019-10-15 stsp "pthread_mutex_lock");
1589 1a76625f 2018-10-22 stsp break;
1590 1a76625f 2018-10-22 stsp }
1591 1a76625f 2018-10-22 stsp
1592 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1593 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1594 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1595 1a76625f 2018-10-22 stsp
1596 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1597 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1598 7c1452c1 2020-03-26 stsp int have_match;
1599 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1600 7c1452c1 2020-03-26 stsp if (err)
1601 7c1452c1 2020-03-26 stsp break;
1602 7c1452c1 2020-03-26 stsp if (have_match)
1603 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1604 13add988 2019-10-15 stsp }
1605 13add988 2019-10-15 stsp
1606 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1607 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1608 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1609 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1610 7c1452c1 2020-03-26 stsp if (err)
1611 13add988 2019-10-15 stsp break;
1612 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1613 899d86c2 2018-05-10 stsp
1614 9ba79e04 2018-06-11 stsp return err;
1615 0553a4e3 2018-04-30 stsp }
1616 0553a4e3 2018-04-30 stsp
1617 2b779855 2020-12-05 naddy static void
1618 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1619 2b779855 2020-12-05 naddy {
1620 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1621 2b779855 2020-12-05 naddy int ncommits = 0;
1622 2b779855 2020-12-05 naddy
1623 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1624 2b779855 2020-12-05 naddy while (entry) {
1625 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1626 2b779855 2020-12-05 naddy s->selected_entry = entry;
1627 2b779855 2020-12-05 naddy break;
1628 2b779855 2020-12-05 naddy }
1629 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1630 2b779855 2020-12-05 naddy ncommits++;
1631 2b779855 2020-12-05 naddy }
1632 2b779855 2020-12-05 naddy }
1633 2b779855 2020-12-05 naddy
1634 0553a4e3 2018-04-30 stsp static const struct got_error *
1635 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1636 0553a4e3 2018-04-30 stsp {
1637 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1638 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1639 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1640 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1641 60493ae3 2019-06-20 stsp int width;
1642 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1643 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1644 8b473291 2019-02-21 stsp char *refs_str = NULL;
1645 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1646 11b20872 2019-11-08 stsp struct tog_color *tc;
1647 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1648 0553a4e3 2018-04-30 stsp
1649 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1650 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1651 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1652 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1653 1a76625f 2018-10-22 stsp if (err)
1654 ecb28ae0 2018-07-16 stsp return err;
1655 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1656 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1657 d2075bf3 2020-12-25 stsp if (refs) {
1658 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1659 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1660 d2075bf3 2020-12-25 stsp if (err)
1661 d2075bf3 2020-12-25 stsp goto done;
1662 d2075bf3 2020-12-25 stsp }
1663 867c6645 2018-07-10 stsp }
1664 359bfafd 2019-02-22 stsp
1665 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1666 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1667 1a76625f 2018-10-22 stsp
1668 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1669 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1670 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1671 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1672 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1673 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1674 8f4ed634 2020-03-26 stsp goto done;
1675 8f4ed634 2020-03-26 stsp }
1676 8f4ed634 2020-03-26 stsp } else {
1677 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1678 f9686aa5 2020-03-27 stsp
1679 f9686aa5 2020-03-27 stsp if (view->searching) {
1680 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1681 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1682 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1683 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1684 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1685 f9686aa5 2020-03-27 stsp search_str = "searching...";
1686 f9686aa5 2020-03-27 stsp }
1687 f9686aa5 2020-03-27 stsp
1688 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1689 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1690 f9686aa5 2020-03-27 stsp search_str ? search_str :
1691 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1692 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1693 8f4ed634 2020-03-26 stsp goto done;
1694 8f4ed634 2020-03-26 stsp }
1695 8b473291 2019-02-21 stsp }
1696 1a76625f 2018-10-22 stsp
1697 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1698 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1699 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1700 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1701 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1702 1a76625f 2018-10-22 stsp header = NULL;
1703 1a76625f 2018-10-22 stsp goto done;
1704 1a76625f 2018-10-22 stsp }
1705 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1706 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1707 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1708 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1709 1a76625f 2018-10-22 stsp header = NULL;
1710 1a76625f 2018-10-22 stsp goto done;
1711 ecb28ae0 2018-07-16 stsp }
1712 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1713 1a76625f 2018-10-22 stsp if (err)
1714 1a76625f 2018-10-22 stsp goto done;
1715 867c6645 2018-07-10 stsp
1716 2814baeb 2018-08-01 stsp werase(view->window);
1717 867c6645 2018-07-10 stsp
1718 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1719 a3404814 2018-09-02 stsp wstandout(view->window);
1720 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1721 11b20872 2019-11-08 stsp if (tc)
1722 11b20872 2019-11-08 stsp wattr_on(view->window,
1723 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1724 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1725 11b20872 2019-11-08 stsp if (tc)
1726 11b20872 2019-11-08 stsp wattr_off(view->window,
1727 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1728 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1729 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1730 1a76625f 2018-10-22 stsp width++;
1731 1a76625f 2018-10-22 stsp }
1732 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1733 a3404814 2018-09-02 stsp wstandend(view->window);
1734 ecb28ae0 2018-07-16 stsp free(wline);
1735 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1736 1a76625f 2018-10-22 stsp goto done;
1737 0553a4e3 2018-04-30 stsp
1738 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1739 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1740 5813d178 2019-03-09 stsp ncommits = 0;
1741 5813d178 2019-03-09 stsp while (entry) {
1742 5813d178 2019-03-09 stsp char *author;
1743 5813d178 2019-03-09 stsp wchar_t *wauthor;
1744 5813d178 2019-03-09 stsp int width;
1745 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1746 5813d178 2019-03-09 stsp break;
1747 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1748 5813d178 2019-03-09 stsp if (author == NULL) {
1749 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1750 5813d178 2019-03-09 stsp goto done;
1751 5813d178 2019-03-09 stsp }
1752 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1753 27a741e5 2019-09-11 stsp date_display_cols);
1754 5813d178 2019-03-09 stsp if (author_cols < width)
1755 5813d178 2019-03-09 stsp author_cols = width;
1756 5813d178 2019-03-09 stsp free(wauthor);
1757 5813d178 2019-03-09 stsp free(author);
1758 7ca04879 2019-10-19 stsp ncommits++;
1759 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1760 5813d178 2019-03-09 stsp }
1761 5813d178 2019-03-09 stsp
1762 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1763 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1764 867c6645 2018-07-10 stsp ncommits = 0;
1765 899d86c2 2018-05-10 stsp while (entry) {
1766 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1767 899d86c2 2018-05-10 stsp break;
1768 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1769 2814baeb 2018-08-01 stsp wstandout(view->window);
1770 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1771 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1772 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1773 2814baeb 2018-08-01 stsp wstandend(view->window);
1774 0553a4e3 2018-04-30 stsp if (err)
1775 60493ae3 2019-06-20 stsp goto done;
1776 0553a4e3 2018-04-30 stsp ncommits++;
1777 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1778 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1779 80ddbec8 2018-04-29 stsp }
1780 80ddbec8 2018-04-29 stsp
1781 1a57306a 2018-09-02 stsp view_vborder(view);
1782 1a76625f 2018-10-22 stsp done:
1783 1a76625f 2018-10-22 stsp free(id_str);
1784 8b473291 2019-02-21 stsp free(refs_str);
1785 1a76625f 2018-10-22 stsp free(ncommits_str);
1786 1a76625f 2018-10-22 stsp free(header);
1787 80ddbec8 2018-04-29 stsp return err;
1788 9f7d7167 2018-04-29 stsp }
1789 07b55e75 2018-05-10 stsp
1790 07b55e75 2018-05-10 stsp static void
1791 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1792 07b55e75 2018-05-10 stsp {
1793 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1794 07b55e75 2018-05-10 stsp int nscrolled = 0;
1795 07b55e75 2018-05-10 stsp
1796 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1797 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1798 07b55e75 2018-05-10 stsp return;
1799 9f7d7167 2018-04-29 stsp
1800 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1801 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1802 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1803 07b55e75 2018-05-10 stsp if (entry) {
1804 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1805 07b55e75 2018-05-10 stsp nscrolled++;
1806 07b55e75 2018-05-10 stsp }
1807 07b55e75 2018-05-10 stsp }
1808 aa075928 2018-05-10 stsp }
1809 aa075928 2018-05-10 stsp
1810 aa075928 2018-05-10 stsp static const struct got_error *
1811 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1812 aa075928 2018-05-10 stsp {
1813 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1814 5e224a3e 2019-02-22 stsp int errcode;
1815 8a42fca8 2019-02-22 stsp
1816 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1817 aa075928 2018-05-10 stsp
1818 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1819 ffe38506 2020-12-01 naddy if (ta->log_complete)
1820 5e224a3e 2019-02-22 stsp break;
1821 b295e71b 2019-02-22 stsp
1822 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1823 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1824 7aafa0d1 2019-02-22 stsp if (errcode)
1825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1826 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1827 7c1452c1 2020-03-26 stsp
1828 7c1452c1 2020-03-26 stsp /*
1829 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1830 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1831 7c1452c1 2020-03-26 stsp */
1832 7c1452c1 2020-03-26 stsp if (!wait)
1833 7c1452c1 2020-03-26 stsp break;
1834 7c1452c1 2020-03-26 stsp
1835 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1836 ffe38506 2020-12-01 naddy show_log_view(view);
1837 7c1452c1 2020-03-26 stsp update_panels();
1838 7c1452c1 2020-03-26 stsp doupdate();
1839 7c1452c1 2020-03-26 stsp
1840 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1841 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1842 82954512 2020-02-03 stsp if (errcode)
1843 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1844 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1845 82954512 2020-02-03 stsp
1846 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1847 ffe38506 2020-12-01 naddy show_log_view(view);
1848 7c1452c1 2020-03-26 stsp update_panels();
1849 7c1452c1 2020-03-26 stsp doupdate();
1850 5e224a3e 2019-02-22 stsp }
1851 5e224a3e 2019-02-22 stsp
1852 5e224a3e 2019-02-22 stsp return NULL;
1853 5e224a3e 2019-02-22 stsp }
1854 5e224a3e 2019-02-22 stsp
1855 5e224a3e 2019-02-22 stsp static const struct got_error *
1856 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1857 5e224a3e 2019-02-22 stsp {
1858 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1859 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1860 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1861 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1862 5e224a3e 2019-02-22 stsp
1863 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1864 5e224a3e 2019-02-22 stsp return NULL;
1865 5e224a3e 2019-02-22 stsp
1866 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1867 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1868 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1869 08ebd0a9 2019-02-22 stsp /*
1870 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1871 08ebd0a9 2019-02-22 stsp */
1872 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1873 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1874 5e224a3e 2019-02-22 stsp if (err)
1875 5e224a3e 2019-02-22 stsp return err;
1876 7aafa0d1 2019-02-22 stsp }
1877 b295e71b 2019-02-22 stsp
1878 7aafa0d1 2019-02-22 stsp do {
1879 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1880 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1881 88048b54 2019-02-21 stsp break;
1882 88048b54 2019-02-21 stsp
1883 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1884 aa075928 2018-05-10 stsp
1885 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1886 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1887 dd0a52c1 2018-05-20 stsp break;
1888 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1889 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1890 aa075928 2018-05-10 stsp
1891 dd0a52c1 2018-05-20 stsp return err;
1892 07b55e75 2018-05-10 stsp }
1893 4a7f7875 2018-05-10 stsp
1894 cd0acaa7 2018-05-20 stsp static const struct got_error *
1895 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1896 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1897 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1898 cd0acaa7 2018-05-20 stsp {
1899 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1900 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1901 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1902 cd0acaa7 2018-05-20 stsp
1903 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1904 15a94983 2018-12-23 stsp if (diff_view == NULL)
1905 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1906 ea5e7bb5 2018-08-01 stsp
1907 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1908 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1909 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1910 e5a0f69f 2018-08-18 stsp if (err == NULL)
1911 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1912 cd0acaa7 2018-05-20 stsp return err;
1913 4a7f7875 2018-05-10 stsp }
1914 4a7f7875 2018-05-10 stsp
1915 80ddbec8 2018-04-29 stsp static const struct got_error *
1916 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1917 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1918 9343a5fb 2018-06-23 stsp {
1919 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1920 941e9f74 2019-05-21 stsp
1921 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1922 941e9f74 2019-05-21 stsp if (parent == NULL)
1923 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1924 941e9f74 2019-05-21 stsp
1925 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1926 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1927 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1928 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1929 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1930 941e9f74 2019-05-21 stsp s->tree = subtree;
1931 941e9f74 2019-05-21 stsp s->selected = 0;
1932 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1933 941e9f74 2019-05-21 stsp return NULL;
1934 941e9f74 2019-05-21 stsp }
1935 941e9f74 2019-05-21 stsp
1936 941e9f74 2019-05-21 stsp static const struct got_error *
1937 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1938 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
1939 941e9f74 2019-05-21 stsp {
1940 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1941 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1942 941e9f74 2019-05-21 stsp const char *p;
1943 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1944 9343a5fb 2018-06-23 stsp
1945 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1946 941e9f74 2019-05-21 stsp p = path;
1947 941e9f74 2019-05-21 stsp while (*p) {
1948 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1949 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1950 56e0773d 2019-11-28 stsp char *te_name;
1951 33cbf02b 2020-01-12 stsp
1952 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1953 33cbf02b 2020-01-12 stsp p++;
1954 941e9f74 2019-05-21 stsp
1955 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1956 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1957 941e9f74 2019-05-21 stsp if (slash == NULL)
1958 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1959 33cbf02b 2020-01-12 stsp else
1960 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1961 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1962 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1963 56e0773d 2019-11-28 stsp break;
1964 941e9f74 2019-05-21 stsp }
1965 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1966 56e0773d 2019-11-28 stsp if (te == NULL) {
1967 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1968 56e0773d 2019-11-28 stsp free(te_name);
1969 941e9f74 2019-05-21 stsp break;
1970 941e9f74 2019-05-21 stsp }
1971 56e0773d 2019-11-28 stsp free(te_name);
1972 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1973 941e9f74 2019-05-21 stsp
1974 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1975 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1976 b03c880f 2019-05-21 stsp
1977 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1978 941e9f74 2019-05-21 stsp if (slash)
1979 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1980 941e9f74 2019-05-21 stsp else
1981 941e9f74 2019-05-21 stsp subpath = strdup(path);
1982 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1983 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1984 941e9f74 2019-05-21 stsp break;
1985 941e9f74 2019-05-21 stsp }
1986 941e9f74 2019-05-21 stsp
1987 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
1988 941e9f74 2019-05-21 stsp subpath);
1989 941e9f74 2019-05-21 stsp if (err)
1990 941e9f74 2019-05-21 stsp break;
1991 941e9f74 2019-05-21 stsp
1992 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1993 941e9f74 2019-05-21 stsp free(tree_id);
1994 941e9f74 2019-05-21 stsp if (err)
1995 941e9f74 2019-05-21 stsp break;
1996 941e9f74 2019-05-21 stsp
1997 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1998 941e9f74 2019-05-21 stsp if (err) {
1999 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2000 941e9f74 2019-05-21 stsp break;
2001 941e9f74 2019-05-21 stsp }
2002 941e9f74 2019-05-21 stsp if (slash == NULL)
2003 941e9f74 2019-05-21 stsp break;
2004 941e9f74 2019-05-21 stsp free(subpath);
2005 941e9f74 2019-05-21 stsp subpath = NULL;
2006 941e9f74 2019-05-21 stsp p = slash;
2007 941e9f74 2019-05-21 stsp }
2008 941e9f74 2019-05-21 stsp
2009 941e9f74 2019-05-21 stsp free(subpath);
2010 1a76625f 2018-10-22 stsp return err;
2011 61266923 2020-01-14 stsp }
2012 61266923 2020-01-14 stsp
2013 61266923 2020-01-14 stsp static const struct got_error *
2014 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2015 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2016 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2017 55cccc34 2020-02-20 stsp {
2018 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2019 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2020 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2021 55cccc34 2020-02-20 stsp
2022 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2023 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2024 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2025 55cccc34 2020-02-20 stsp
2026 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2027 bc573f3b 2021-07-10 stsp if (err)
2028 55cccc34 2020-02-20 stsp return err;
2029 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2030 55cccc34 2020-02-20 stsp
2031 55cccc34 2020-02-20 stsp *new_view = tree_view;
2032 55cccc34 2020-02-20 stsp
2033 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2034 55cccc34 2020-02-20 stsp return NULL;
2035 55cccc34 2020-02-20 stsp
2036 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2037 55cccc34 2020-02-20 stsp }
2038 55cccc34 2020-02-20 stsp
2039 55cccc34 2020-02-20 stsp static const struct got_error *
2040 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2041 61266923 2020-01-14 stsp {
2042 61266923 2020-01-14 stsp sigset_t sigset;
2043 61266923 2020-01-14 stsp int errcode;
2044 61266923 2020-01-14 stsp
2045 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2046 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2047 61266923 2020-01-14 stsp
2048 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2049 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2050 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2051 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2052 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2053 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2054 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2055 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2056 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2057 61266923 2020-01-14 stsp
2058 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2059 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2060 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2061 61266923 2020-01-14 stsp
2062 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2063 61266923 2020-01-14 stsp if (errcode)
2064 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2065 61266923 2020-01-14 stsp
2066 61266923 2020-01-14 stsp return NULL;
2067 1a76625f 2018-10-22 stsp }
2068 1a76625f 2018-10-22 stsp
2069 1a76625f 2018-10-22 stsp static void *
2070 1a76625f 2018-10-22 stsp log_thread(void *arg)
2071 1a76625f 2018-10-22 stsp {
2072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2073 1a76625f 2018-10-22 stsp int errcode = 0;
2074 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2075 1a76625f 2018-10-22 stsp int done = 0;
2076 61266923 2020-01-14 stsp
2077 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2078 61266923 2020-01-14 stsp if (err)
2079 61266923 2020-01-14 stsp return (void *)err;
2080 1a76625f 2018-10-22 stsp
2081 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2082 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2083 1a76625f 2018-10-22 stsp if (err) {
2084 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2085 1a76625f 2018-10-22 stsp return (void *)err;
2086 1a76625f 2018-10-22 stsp err = NULL;
2087 1a76625f 2018-10-22 stsp done = 1;
2088 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2089 1a76625f 2018-10-22 stsp a->commits_needed--;
2090 1a76625f 2018-10-22 stsp
2091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2092 3abe8080 2019-04-10 stsp if (errcode) {
2093 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2094 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2095 3abe8080 2019-04-10 stsp break;
2096 3abe8080 2019-04-10 stsp } else if (*a->quit)
2097 1a76625f 2018-10-22 stsp done = 1;
2098 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2099 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2100 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2101 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2102 1a76625f 2018-10-22 stsp }
2103 1a76625f 2018-10-22 stsp
2104 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2105 7c1452c1 2020-03-26 stsp if (errcode) {
2106 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2107 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2108 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2109 7c1452c1 2020-03-26 stsp break;
2110 7c1452c1 2020-03-26 stsp }
2111 7c1452c1 2020-03-26 stsp
2112 1a76625f 2018-10-22 stsp if (done)
2113 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2114 7c1452c1 2020-03-26 stsp else {
2115 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2116 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2117 7c1452c1 2020-03-26 stsp &tog_mutex);
2118 7c1452c1 2020-03-26 stsp if (errcode)
2119 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2120 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2121 21355643 2020-12-06 stsp if (*a->quit)
2122 21355643 2020-12-06 stsp done = 1;
2123 7c1452c1 2020-03-26 stsp }
2124 1a76625f 2018-10-22 stsp }
2125 1a76625f 2018-10-22 stsp
2126 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2127 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2128 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2129 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2130 1a76625f 2018-10-22 stsp }
2131 3abe8080 2019-04-10 stsp a->log_complete = 1;
2132 1a76625f 2018-10-22 stsp return (void *)err;
2133 1a76625f 2018-10-22 stsp }
2134 1a76625f 2018-10-22 stsp
2135 1a76625f 2018-10-22 stsp static const struct got_error *
2136 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2137 1a76625f 2018-10-22 stsp {
2138 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2139 1a76625f 2018-10-22 stsp int errcode;
2140 1a76625f 2018-10-22 stsp
2141 1a76625f 2018-10-22 stsp if (s->thread) {
2142 1a76625f 2018-10-22 stsp s->quit = 1;
2143 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2144 1a76625f 2018-10-22 stsp if (errcode)
2145 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2146 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2147 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2148 1a76625f 2018-10-22 stsp if (errcode)
2149 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2150 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2151 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2152 1a76625f 2018-10-22 stsp if (errcode)
2153 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2155 1a76625f 2018-10-22 stsp if (errcode)
2156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2157 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2158 1a76625f 2018-10-22 stsp s->thread = NULL;
2159 1a76625f 2018-10-22 stsp }
2160 1a76625f 2018-10-22 stsp
2161 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2162 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2163 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2164 74467cc8 2022-06-15 stsp }
2165 74467cc8 2022-06-15 stsp
2166 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2167 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2168 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2169 74467cc8 2022-06-15 stsp if (err == NULL)
2170 74467cc8 2022-06-15 stsp err = pack_err;
2171 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2172 1a76625f 2018-10-22 stsp }
2173 1a76625f 2018-10-22 stsp
2174 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2175 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2176 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2177 1a76625f 2018-10-22 stsp }
2178 1a76625f 2018-10-22 stsp
2179 9343a5fb 2018-06-23 stsp return err;
2180 9343a5fb 2018-06-23 stsp }
2181 9343a5fb 2018-06-23 stsp
2182 9343a5fb 2018-06-23 stsp static const struct got_error *
2183 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2184 1a76625f 2018-10-22 stsp {
2185 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2186 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2187 276b94a1 2020-11-13 naddy int errcode;
2188 1a76625f 2018-10-22 stsp
2189 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2190 276b94a1 2020-11-13 naddy
2191 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2192 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2193 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2194 276b94a1 2020-11-13 naddy
2195 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2196 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2197 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2198 276b94a1 2020-11-13 naddy
2199 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2200 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2201 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2202 1a76625f 2018-10-22 stsp free(s->start_id);
2203 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2204 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2205 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2206 1a76625f 2018-10-22 stsp return err;
2207 1a76625f 2018-10-22 stsp }
2208 1a76625f 2018-10-22 stsp
2209 1a76625f 2018-10-22 stsp static const struct got_error *
2210 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2211 60493ae3 2019-06-20 stsp {
2212 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2213 60493ae3 2019-06-20 stsp
2214 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2215 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2216 60493ae3 2019-06-20 stsp return NULL;
2217 60493ae3 2019-06-20 stsp }
2218 60493ae3 2019-06-20 stsp
2219 60493ae3 2019-06-20 stsp static const struct got_error *
2220 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2221 60493ae3 2019-06-20 stsp {
2222 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2223 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2224 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2225 60493ae3 2019-06-20 stsp
2226 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2227 f9686aa5 2020-03-27 stsp show_log_view(view);
2228 f9686aa5 2020-03-27 stsp update_panels();
2229 f9686aa5 2020-03-27 stsp doupdate();
2230 f9686aa5 2020-03-27 stsp
2231 96e2b566 2019-07-08 stsp if (s->search_entry) {
2232 13add988 2019-10-15 stsp int errcode, ch;
2233 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2234 13add988 2019-10-15 stsp if (errcode)
2235 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2236 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2237 13add988 2019-10-15 stsp ch = wgetch(view->window);
2238 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2239 13add988 2019-10-15 stsp if (errcode)
2240 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2241 13add988 2019-10-15 stsp "pthread_mutex_lock");
2242 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2243 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2244 678cbce5 2019-07-28 stsp return NULL;
2245 678cbce5 2019-07-28 stsp }
2246 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2247 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2248 96e2b566 2019-07-08 stsp else
2249 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2250 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2251 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2252 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2253 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2254 b1bf1435 2019-06-21 stsp else
2255 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2256 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2257 20be8d96 2019-06-21 stsp } else {
2258 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2259 20be8d96 2019-06-21 stsp }
2260 60493ae3 2019-06-20 stsp
2261 60493ae3 2019-06-20 stsp while (1) {
2262 13add988 2019-10-15 stsp int have_match = 0;
2263 13add988 2019-10-15 stsp
2264 60493ae3 2019-06-20 stsp if (entry == NULL) {
2265 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2266 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2267 f9967bca 2020-03-27 stsp view->search_next_done =
2268 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2269 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2270 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2271 f801134a 2019-06-25 stsp return NULL;
2272 60493ae3 2019-06-20 stsp }
2273 96e2b566 2019-07-08 stsp /*
2274 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2275 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2276 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2277 96e2b566 2019-07-08 stsp */
2278 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2279 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2280 60493ae3 2019-06-20 stsp }
2281 60493ae3 2019-06-20 stsp
2282 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2283 13add988 2019-10-15 stsp &view->regex);
2284 5943eee2 2019-08-13 stsp if (err)
2285 13add988 2019-10-15 stsp break;
2286 13add988 2019-10-15 stsp if (have_match) {
2287 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2288 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2289 60493ae3 2019-06-20 stsp break;
2290 60493ae3 2019-06-20 stsp }
2291 13add988 2019-10-15 stsp
2292 96e2b566 2019-07-08 stsp s->search_entry = entry;
2293 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2294 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2295 b1bf1435 2019-06-21 stsp else
2296 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2297 60493ae3 2019-06-20 stsp }
2298 60493ae3 2019-06-20 stsp
2299 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2300 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2301 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2302 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2303 60493ae3 2019-06-20 stsp if (err)
2304 60493ae3 2019-06-20 stsp return err;
2305 ead14cbe 2019-06-21 stsp cur++;
2306 ead14cbe 2019-06-21 stsp }
2307 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2308 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2309 60493ae3 2019-06-20 stsp if (err)
2310 60493ae3 2019-06-20 stsp return err;
2311 ead14cbe 2019-06-21 stsp cur--;
2312 60493ae3 2019-06-20 stsp }
2313 60493ae3 2019-06-20 stsp }
2314 60493ae3 2019-06-20 stsp
2315 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2316 96e2b566 2019-07-08 stsp
2317 60493ae3 2019-06-20 stsp return NULL;
2318 60493ae3 2019-06-20 stsp }
2319 60493ae3 2019-06-20 stsp
2320 60493ae3 2019-06-20 stsp static const struct got_error *
2321 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2322 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2323 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2324 80ddbec8 2018-04-29 stsp {
2325 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2326 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2327 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2328 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2329 1a76625f 2018-10-22 stsp int errcode;
2330 80ddbec8 2018-04-29 stsp
2331 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2332 f135c941 2020-02-20 stsp free(s->in_repo_path);
2333 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2334 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2335 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2336 f135c941 2020-02-20 stsp }
2337 ecb28ae0 2018-07-16 stsp
2338 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2339 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2340 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2341 78756c87 2020-11-24 stsp
2342 fb2756b9 2018-08-04 stsp s->repo = repo;
2343 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2344 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2345 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2346 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2347 9cd7cbd1 2020-12-07 stsp goto done;
2348 9cd7cbd1 2020-12-07 stsp }
2349 9cd7cbd1 2020-12-07 stsp }
2350 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2351 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2353 5036bf37 2018-09-24 stsp goto done;
2354 5036bf37 2018-09-24 stsp }
2355 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2356 e5a0f69f 2018-08-18 stsp
2357 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2358 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2359 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2360 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2361 11b20872 2019-11-08 stsp if (err)
2362 11b20872 2019-11-08 stsp goto done;
2363 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2364 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2365 11b20872 2019-11-08 stsp if (err) {
2366 11b20872 2019-11-08 stsp free_colors(&s->colors);
2367 11b20872 2019-11-08 stsp goto done;
2368 11b20872 2019-11-08 stsp }
2369 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2370 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2371 11b20872 2019-11-08 stsp if (err) {
2372 11b20872 2019-11-08 stsp free_colors(&s->colors);
2373 11b20872 2019-11-08 stsp goto done;
2374 11b20872 2019-11-08 stsp }
2375 11b20872 2019-11-08 stsp }
2376 11b20872 2019-11-08 stsp
2377 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2378 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2379 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2380 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2381 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2382 1a76625f 2018-10-22 stsp
2383 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2384 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2385 74467cc8 2022-06-15 stsp if (err)
2386 74467cc8 2022-06-15 stsp goto done;
2387 74467cc8 2022-06-15 stsp }
2388 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2389 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2390 0ae84acc 2022-06-15 tracey if (err)
2391 0ae84acc 2022-06-15 tracey goto done;
2392 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2393 b672a97a 2020-01-27 stsp !s->log_branches);
2394 1a76625f 2018-10-22 stsp if (err)
2395 1a76625f 2018-10-22 stsp goto done;
2396 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2397 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2398 c5b78334 2020-01-12 stsp if (err)
2399 c5b78334 2020-01-12 stsp goto done;
2400 1a76625f 2018-10-22 stsp
2401 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2402 1a76625f 2018-10-22 stsp if (errcode) {
2403 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2404 1a76625f 2018-10-22 stsp goto done;
2405 1a76625f 2018-10-22 stsp }
2406 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2407 7c1452c1 2020-03-26 stsp if (errcode) {
2408 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2409 7c1452c1 2020-03-26 stsp goto done;
2410 7c1452c1 2020-03-26 stsp }
2411 1a76625f 2018-10-22 stsp
2412 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2413 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2414 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2415 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2416 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2417 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2418 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2419 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2420 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2421 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2422 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2423 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2424 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2425 ba4f502b 2018-08-04 stsp done:
2426 1a76625f 2018-10-22 stsp if (err)
2427 1a76625f 2018-10-22 stsp close_log_view(view);
2428 ba4f502b 2018-08-04 stsp return err;
2429 ba4f502b 2018-08-04 stsp }
2430 ba4f502b 2018-08-04 stsp
2431 e5a0f69f 2018-08-18 stsp static const struct got_error *
2432 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2433 ba4f502b 2018-08-04 stsp {
2434 f2f6d207 2020-11-24 stsp const struct got_error *err;
2435 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2436 ba4f502b 2018-08-04 stsp
2437 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2438 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2439 2b380cc8 2018-10-24 stsp &s->thread_args);
2440 2b380cc8 2018-10-24 stsp if (errcode)
2441 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2442 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2443 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2444 f2f6d207 2020-11-24 stsp if (err)
2445 f2f6d207 2020-11-24 stsp return err;
2446 f2f6d207 2020-11-24 stsp }
2447 2b380cc8 2018-10-24 stsp }
2448 2b380cc8 2018-10-24 stsp
2449 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2450 e5a0f69f 2018-08-18 stsp }
2451 04cc582a 2018-08-01 stsp
2452 e5a0f69f 2018-08-18 stsp static const struct got_error *
2453 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2454 e5a0f69f 2018-08-18 stsp {
2455 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2456 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2457 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2458 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2459 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2460 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
2461 80ddbec8 2018-04-29 stsp
2462 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2463 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2464 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2465 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2466 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2467 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2468 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2469 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2470 fb280deb 2021-08-30 stsp select_commit(s);
2471 fb280deb 2021-08-30 stsp }
2472 528dedf3 2021-08-30 stsp return NULL;
2473 528dedf3 2021-08-30 stsp }
2474 528dedf3 2021-08-30 stsp
2475 528dedf3 2021-08-30 stsp switch (ch) {
2476 1e37a5c2 2019-05-12 jcs case 'q':
2477 1e37a5c2 2019-05-12 jcs s->quit = 1;
2478 1e37a5c2 2019-05-12 jcs break;
2479 1e37a5c2 2019-05-12 jcs case 'k':
2480 1e37a5c2 2019-05-12 jcs case KEY_UP:
2481 1e37a5c2 2019-05-12 jcs case '<':
2482 1e37a5c2 2019-05-12 jcs case ',':
2483 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2484 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2485 1a76625f 2018-10-22 stsp break;
2486 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2487 1e37a5c2 2019-05-12 jcs s->selected--;
2488 1144d21a 2019-06-21 stsp else
2489 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2490 912a3f79 2021-08-30 j select_commit(s);
2491 912a3f79 2021-08-30 j break;
2492 912a3f79 2021-08-30 j case 'g':
2493 912a3f79 2021-08-30 j case KEY_HOME:
2494 912a3f79 2021-08-30 j s->selected = 0;
2495 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2496 2b779855 2020-12-05 naddy select_commit(s);
2497 1e37a5c2 2019-05-12 jcs break;
2498 83cc4199 2022-06-13 stsp case CTRL('u'):
2499 33c3719a 2022-06-15 stsp case 'u':
2500 83cc4199 2022-06-13 stsp nscroll /= 2;
2501 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2502 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2503 a4292ac5 2019-05-12 jcs case CTRL('b'):
2504 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2505 e5a0f69f 2018-08-18 stsp break;
2506 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2507 83cc4199 2022-06-13 stsp s->selected = MAX(0, s->selected - nscroll - 1);
2508 2b779855 2020-12-05 naddy else
2509 83cc4199 2022-06-13 stsp log_scroll_up(s, nscroll);
2510 2b779855 2020-12-05 naddy select_commit(s);
2511 1e37a5c2 2019-05-12 jcs break;
2512 1e37a5c2 2019-05-12 jcs case 'j':
2513 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2514 1e37a5c2 2019-05-12 jcs case '>':
2515 1e37a5c2 2019-05-12 jcs case '.':
2516 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2517 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2518 e5a0f69f 2018-08-18 stsp break;
2519 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2520 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2521 1e37a5c2 2019-05-12 jcs s->selected++;
2522 2b779855 2020-12-05 naddy else {
2523 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2524 2b779855 2020-12-05 naddy if (err)
2525 2b779855 2020-12-05 naddy break;
2526 912a3f79 2021-08-30 j }
2527 912a3f79 2021-08-30 j select_commit(s);
2528 912a3f79 2021-08-30 j break;
2529 912a3f79 2021-08-30 j case 'G':
2530 912a3f79 2021-08-30 j case KEY_END: {
2531 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2532 912a3f79 2021-08-30 j * traverse them all. */
2533 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2534 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2535 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2536 80ddbec8 2018-04-29 stsp }
2537 912a3f79 2021-08-30 j
2538 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2539 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2540 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2541 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2542 f3bc9f1d 2021-09-05 naddy break;
2543 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2544 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2545 f3bc9f1d 2021-09-05 naddy }
2546 f3bc9f1d 2021-09-05 naddy if (n > 0)
2547 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2548 2b779855 2020-12-05 naddy select_commit(s);
2549 1e37a5c2 2019-05-12 jcs break;
2550 912a3f79 2021-08-30 j }
2551 80b7e8da 2022-06-11 stsp case CTRL('d'):
2552 33c3719a 2022-06-15 stsp case 'd':
2553 83cc4199 2022-06-13 stsp nscroll /= 2;
2554 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2555 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2556 83cc4199 2022-06-13 stsp case CTRL('f'): {
2557 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2558 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2559 1e37a5c2 2019-05-12 jcs if (first == NULL)
2560 e5a0f69f 2018-08-18 stsp break;
2561 83cc4199 2022-06-13 stsp err = log_scroll_down(view, nscroll);
2562 1cae65b4 2019-09-22 stsp if (err)
2563 1cae65b4 2019-09-22 stsp break;
2564 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2565 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2566 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2567 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2568 83cc4199 2022-06-13 stsp s->selected += MIN(s->last_displayed_entry->idx -
2569 83cc4199 2022-06-13 stsp s->selected_entry->idx, nscroll + 1);
2570 1e37a5c2 2019-05-12 jcs }
2571 2b779855 2020-12-05 naddy select_commit(s);
2572 1e37a5c2 2019-05-12 jcs break;
2573 1e37a5c2 2019-05-12 jcs }
2574 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2575 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2576 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2577 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2578 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2579 2b779855 2020-12-05 naddy select_commit(s);
2580 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2581 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2582 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2583 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2584 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2585 0bf7f153 2020-12-02 naddy }
2586 1e37a5c2 2019-05-12 jcs break;
2587 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2588 87c7274c 2019-05-12 jcs case ' ':
2589 1e37a5c2 2019-05-12 jcs case '\r':
2590 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2591 e5a0f69f 2018-08-18 stsp break;
2592 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2593 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2594 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2595 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2596 78756c87 2020-11-24 stsp view, s->repo);
2597 1e37a5c2 2019-05-12 jcs if (err)
2598 1e37a5c2 2019-05-12 jcs break;
2599 e78dc838 2020-12-04 stsp view->focussed = 0;
2600 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2601 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2602 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2603 f7013a22 2018-10-24 stsp if (err)
2604 1e37a5c2 2019-05-12 jcs return err;
2605 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2606 e78dc838 2020-12-04 stsp view->focus_child = 1;
2607 1e37a5c2 2019-05-12 jcs } else
2608 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2609 1e37a5c2 2019-05-12 jcs break;
2610 1e37a5c2 2019-05-12 jcs case 't':
2611 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2612 5036bf37 2018-09-24 stsp break;
2613 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2614 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2615 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2616 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2617 4e97c21c 2020-12-06 stsp s->repo);
2618 1e37a5c2 2019-05-12 jcs if (err)
2619 e5a0f69f 2018-08-18 stsp break;
2620 e78dc838 2020-12-04 stsp view->focussed = 0;
2621 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2622 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2623 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2624 1e37a5c2 2019-05-12 jcs if (err)
2625 1e37a5c2 2019-05-12 jcs return err;
2626 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2627 e78dc838 2020-12-04 stsp view->focus_child = 1;
2628 1e37a5c2 2019-05-12 jcs } else
2629 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2630 1e37a5c2 2019-05-12 jcs break;
2631 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2632 21355643 2020-12-06 stsp case CTRL('l'):
2633 21355643 2020-12-06 stsp case 'B':
2634 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2635 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2636 1e37a5c2 2019-05-12 jcs break;
2637 21355643 2020-12-06 stsp err = stop_log_thread(s);
2638 74cfe85e 2020-10-20 stsp if (err)
2639 74cfe85e 2020-10-20 stsp return err;
2640 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2641 21355643 2020-12-06 stsp char *parent_path;
2642 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2643 21355643 2020-12-06 stsp if (err)
2644 21355643 2020-12-06 stsp return err;
2645 21355643 2020-12-06 stsp free(s->in_repo_path);
2646 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2647 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2648 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2649 21355643 2020-12-06 stsp struct got_object_id *start_id;
2650 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2651 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2652 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2653 21355643 2020-12-06 stsp if (err)
2654 21355643 2020-12-06 stsp return err;
2655 21355643 2020-12-06 stsp free(s->start_id);
2656 21355643 2020-12-06 stsp s->start_id = start_id;
2657 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2658 21355643 2020-12-06 stsp } else /* 'B' */
2659 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2660 21355643 2020-12-06 stsp
2661 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
2662 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
2663 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2664 74cfe85e 2020-10-20 stsp if (err)
2665 21355643 2020-12-06 stsp return err;
2666 51a10b52 2020-12-26 stsp tog_free_refs();
2667 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
2668 ca51c541 2020-12-07 stsp if (err)
2669 ca51c541 2020-12-07 stsp return err;
2670 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2671 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2672 d01904d4 2019-06-25 stsp if (err)
2673 d01904d4 2019-06-25 stsp return err;
2674 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2675 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2676 b672a97a 2020-01-27 stsp if (err)
2677 b672a97a 2020-01-27 stsp return err;
2678 21355643 2020-12-06 stsp free_commits(&s->commits);
2679 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2680 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2681 21355643 2020-12-06 stsp s->selected_entry = NULL;
2682 21355643 2020-12-06 stsp s->selected = 0;
2683 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2684 21355643 2020-12-06 stsp s->quit = 0;
2685 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2686 d01904d4 2019-06-25 stsp break;
2687 6458efa5 2020-11-24 stsp case 'r':
2688 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2689 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2690 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2691 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2692 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2693 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2694 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2695 6458efa5 2020-11-24 stsp if (err) {
2696 6458efa5 2020-11-24 stsp view_close(ref_view);
2697 6458efa5 2020-11-24 stsp return err;
2698 6458efa5 2020-11-24 stsp }
2699 e78dc838 2020-12-04 stsp view->focussed = 0;
2700 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2701 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2702 6458efa5 2020-11-24 stsp err = view_close_child(view);
2703 6458efa5 2020-11-24 stsp if (err)
2704 6458efa5 2020-11-24 stsp return err;
2705 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2706 e78dc838 2020-12-04 stsp view->focus_child = 1;
2707 6458efa5 2020-11-24 stsp } else
2708 6458efa5 2020-11-24 stsp *new_view = ref_view;
2709 6458efa5 2020-11-24 stsp break;
2710 1e37a5c2 2019-05-12 jcs default:
2711 1e37a5c2 2019-05-12 jcs break;
2712 899d86c2 2018-05-10 stsp }
2713 e5a0f69f 2018-08-18 stsp
2714 80ddbec8 2018-04-29 stsp return err;
2715 80ddbec8 2018-04-29 stsp }
2716 80ddbec8 2018-04-29 stsp
2717 4ed7e80c 2018-05-20 stsp static const struct got_error *
2718 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2719 c2db6724 2019-01-04 stsp {
2720 c2db6724 2019-01-04 stsp const struct got_error *error;
2721 c2db6724 2019-01-04 stsp
2722 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2723 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2724 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2725 37c06ea4 2019-07-15 stsp #endif
2726 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2727 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2728 c2db6724 2019-01-04 stsp
2729 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2730 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2731 c2db6724 2019-01-04 stsp
2732 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2733 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2734 c2db6724 2019-01-04 stsp
2735 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2736 c2db6724 2019-01-04 stsp if (error != NULL)
2737 c2db6724 2019-01-04 stsp return error;
2738 c2db6724 2019-01-04 stsp
2739 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2740 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2741 c2db6724 2019-01-04 stsp
2742 c2db6724 2019-01-04 stsp return NULL;
2743 c2db6724 2019-01-04 stsp }
2744 c2db6724 2019-01-04 stsp
2745 a915003a 2019-02-05 stsp static void
2746 a915003a 2019-02-05 stsp init_curses(void)
2747 a915003a 2019-02-05 stsp {
2748 2497f032 2022-05-31 stsp /*
2749 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
2750 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
2751 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
2752 2497f032 2022-05-31 stsp */
2753 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
2754 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
2755 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
2756 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
2757 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
2758 2497f032 2022-05-31 stsp
2759 a915003a 2019-02-05 stsp initscr();
2760 a915003a 2019-02-05 stsp cbreak();
2761 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2762 a915003a 2019-02-05 stsp noecho();
2763 a915003a 2019-02-05 stsp nonl();
2764 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2765 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2766 a915003a 2019-02-05 stsp curs_set(0);
2767 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2768 6d17833f 2019-11-08 stsp start_color();
2769 6d17833f 2019-11-08 stsp use_default_colors();
2770 6d17833f 2019-11-08 stsp }
2771 a915003a 2019-02-05 stsp }
2772 a915003a 2019-02-05 stsp
2773 c2db6724 2019-01-04 stsp static const struct got_error *
2774 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2775 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2776 f135c941 2020-02-20 stsp {
2777 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2778 f135c941 2020-02-20 stsp
2779 f135c941 2020-02-20 stsp if (argc == 0) {
2780 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2781 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2782 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2783 f135c941 2020-02-20 stsp return NULL;
2784 f135c941 2020-02-20 stsp }
2785 f135c941 2020-02-20 stsp
2786 f135c941 2020-02-20 stsp if (worktree) {
2787 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2788 bfd61697 2020-11-14 stsp char *p;
2789 f135c941 2020-02-20 stsp
2790 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2791 f135c941 2020-02-20 stsp if (err)
2792 f135c941 2020-02-20 stsp return err;
2793 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2794 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2795 bfd61697 2020-11-14 stsp p) == -1) {
2796 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2797 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2798 f135c941 2020-02-20 stsp }
2799 f135c941 2020-02-20 stsp free(p);
2800 f135c941 2020-02-20 stsp } else
2801 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2802 f135c941 2020-02-20 stsp
2803 f135c941 2020-02-20 stsp return err;
2804 f135c941 2020-02-20 stsp }
2805 f135c941 2020-02-20 stsp
2806 f135c941 2020-02-20 stsp static const struct got_error *
2807 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2808 9f7d7167 2018-04-29 stsp {
2809 80ddbec8 2018-04-29 stsp const struct got_error *error;
2810 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2811 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2812 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2813 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2814 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2815 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2816 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2817 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2818 04cc582a 2018-08-01 stsp struct tog_view *view;
2819 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
2820 80ddbec8 2018-04-29 stsp
2821 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2822 80ddbec8 2018-04-29 stsp switch (ch) {
2823 b672a97a 2020-01-27 stsp case 'b':
2824 b672a97a 2020-01-27 stsp log_branches = 1;
2825 b672a97a 2020-01-27 stsp break;
2826 80ddbec8 2018-04-29 stsp case 'c':
2827 80ddbec8 2018-04-29 stsp start_commit = optarg;
2828 80ddbec8 2018-04-29 stsp break;
2829 ecb28ae0 2018-07-16 stsp case 'r':
2830 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2831 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2832 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2833 9ba1d308 2019-10-21 stsp optarg);
2834 ecb28ae0 2018-07-16 stsp break;
2835 80ddbec8 2018-04-29 stsp default:
2836 17020d27 2019-03-07 stsp usage_log();
2837 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2838 80ddbec8 2018-04-29 stsp }
2839 80ddbec8 2018-04-29 stsp }
2840 80ddbec8 2018-04-29 stsp
2841 80ddbec8 2018-04-29 stsp argc -= optind;
2842 80ddbec8 2018-04-29 stsp argv += optind;
2843 80ddbec8 2018-04-29 stsp
2844 f135c941 2020-02-20 stsp if (argc > 1)
2845 f135c941 2020-02-20 stsp usage_log();
2846 963f97a1 2019-03-18 stsp
2847 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
2848 0ae84acc 2022-06-15 tracey if (error != NULL)
2849 0ae84acc 2022-06-15 tracey goto done;
2850 0ae84acc 2022-06-15 tracey
2851 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2852 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2853 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2854 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2855 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2856 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2857 c156c7a4 2020-12-18 stsp goto done;
2858 a1fbf39a 2019-08-11 stsp if (worktree)
2859 6962eb72 2020-02-20 stsp repo_path =
2860 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2861 a1fbf39a 2019-08-11 stsp else
2862 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2863 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2864 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2865 c156c7a4 2020-12-18 stsp goto done;
2866 c156c7a4 2020-12-18 stsp }
2867 ecb28ae0 2018-07-16 stsp }
2868 ecb28ae0 2018-07-16 stsp
2869 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2870 80ddbec8 2018-04-29 stsp if (error != NULL)
2871 ecb28ae0 2018-07-16 stsp goto done;
2872 80ddbec8 2018-04-29 stsp
2873 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2874 f135c941 2020-02-20 stsp repo, worktree);
2875 f135c941 2020-02-20 stsp if (error)
2876 f135c941 2020-02-20 stsp goto done;
2877 f135c941 2020-02-20 stsp
2878 f135c941 2020-02-20 stsp init_curses();
2879 f135c941 2020-02-20 stsp
2880 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2881 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2882 c02c541e 2019-03-29 stsp if (error)
2883 c02c541e 2019-03-29 stsp goto done;
2884 c02c541e 2019-03-29 stsp
2885 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2886 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2887 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
2888 87670572 2020-12-26 naddy if (error)
2889 87670572 2020-12-26 naddy goto done;
2890 87670572 2020-12-26 naddy }
2891 51a10b52 2020-12-26 stsp
2892 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2893 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2894 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2895 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2896 d8f38dc4 2020-12-05 stsp if (error)
2897 d8f38dc4 2020-12-05 stsp goto done;
2898 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2899 d8f38dc4 2020-12-05 stsp } else {
2900 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2901 d8f38dc4 2020-12-05 stsp if (error == NULL)
2902 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2903 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2904 d8f38dc4 2020-12-05 stsp goto done;
2905 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2906 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2907 d8f38dc4 2020-12-05 stsp if (error)
2908 d8f38dc4 2020-12-05 stsp goto done;
2909 d8f38dc4 2020-12-05 stsp }
2910 8b473291 2019-02-21 stsp
2911 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2912 04cc582a 2018-08-01 stsp if (view == NULL) {
2913 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2914 04cc582a 2018-08-01 stsp goto done;
2915 04cc582a 2018-08-01 stsp }
2916 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2917 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2918 ba4f502b 2018-08-04 stsp if (error)
2919 ba4f502b 2018-08-04 stsp goto done;
2920 2fc00ff4 2019-08-31 stsp if (worktree) {
2921 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2922 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2923 2fc00ff4 2019-08-31 stsp worktree = NULL;
2924 2fc00ff4 2019-08-31 stsp }
2925 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2926 ecb28ae0 2018-07-16 stsp done:
2927 f135c941 2020-02-20 stsp free(in_repo_path);
2928 ecb28ae0 2018-07-16 stsp free(repo_path);
2929 ecb28ae0 2018-07-16 stsp free(cwd);
2930 899d86c2 2018-05-10 stsp free(start_id);
2931 d8f38dc4 2020-12-05 stsp free(label);
2932 d8f38dc4 2020-12-05 stsp if (ref)
2933 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2934 1d0f4054 2021-06-17 stsp if (repo) {
2935 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2936 1d0f4054 2021-06-17 stsp if (error == NULL)
2937 1d0f4054 2021-06-17 stsp error = close_err;
2938 1d0f4054 2021-06-17 stsp }
2939 ec142235 2019-03-07 stsp if (worktree)
2940 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2941 0ae84acc 2022-06-15 tracey if (pack_fds) {
2942 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
2943 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
2944 0ae84acc 2022-06-15 tracey if (error == NULL)
2945 0ae84acc 2022-06-15 tracey error = pack_err;
2946 0ae84acc 2022-06-15 tracey }
2947 51a10b52 2020-12-26 stsp tog_free_refs();
2948 80ddbec8 2018-04-29 stsp return error;
2949 9f7d7167 2018-04-29 stsp }
2950 9f7d7167 2018-04-29 stsp
2951 4ed7e80c 2018-05-20 stsp __dead static void
2952 9f7d7167 2018-04-29 stsp usage_diff(void)
2953 9f7d7167 2018-04-29 stsp {
2954 80ddbec8 2018-04-29 stsp endwin();
2955 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2956 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2957 9f7d7167 2018-04-29 stsp exit(1);
2958 b304db33 2018-05-20 stsp }
2959 b304db33 2018-05-20 stsp
2960 6d17833f 2019-11-08 stsp static int
2961 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2962 41605754 2020-11-12 stsp regmatch_t *regmatch)
2963 6d17833f 2019-11-08 stsp {
2964 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2965 6d17833f 2019-11-08 stsp }
2966 6d17833f 2019-11-08 stsp
2967 f26dddb7 2019-11-08 stsp struct tog_color *
2968 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2969 6d17833f 2019-11-08 stsp {
2970 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2971 6d17833f 2019-11-08 stsp
2972 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2973 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2974 6d17833f 2019-11-08 stsp return tc;
2975 6d17833f 2019-11-08 stsp }
2976 6d17833f 2019-11-08 stsp
2977 6d17833f 2019-11-08 stsp return NULL;
2978 6d17833f 2019-11-08 stsp }
2979 6d17833f 2019-11-08 stsp
2980 4ed7e80c 2018-05-20 stsp static const struct got_error *
2981 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2982 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2983 41605754 2020-11-12 stsp {
2984 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2985 41605754 2020-11-12 stsp wchar_t *wline;
2986 41605754 2020-11-12 stsp int width;
2987 41605754 2020-11-12 stsp char *s;
2988 41605754 2020-11-12 stsp
2989 41605754 2020-11-12 stsp *wtotal = 0;
2990 41605754 2020-11-12 stsp
2991 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2992 41605754 2020-11-12 stsp if (s == NULL)
2993 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2994 41605754 2020-11-12 stsp
2995 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2996 41605754 2020-11-12 stsp if (err) {
2997 41605754 2020-11-12 stsp free(s);
2998 41605754 2020-11-12 stsp return err;
2999 41605754 2020-11-12 stsp }
3000 41605754 2020-11-12 stsp waddwstr(window, wline);
3001 41605754 2020-11-12 stsp free(wline);
3002 41605754 2020-11-12 stsp free(s);
3003 41605754 2020-11-12 stsp wlimit -= width;
3004 41605754 2020-11-12 stsp *wtotal += width;
3005 41605754 2020-11-12 stsp
3006 41605754 2020-11-12 stsp if (wlimit > 0) {
3007 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
3008 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
3009 41605754 2020-11-12 stsp if (s == NULL) {
3010 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
3011 41605754 2020-11-12 stsp free(s);
3012 41605754 2020-11-12 stsp return err;
3013 41605754 2020-11-12 stsp }
3014 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
3015 41605754 2020-11-12 stsp if (err) {
3016 41605754 2020-11-12 stsp free(s);
3017 41605754 2020-11-12 stsp return err;
3018 41605754 2020-11-12 stsp }
3019 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
3020 41605754 2020-11-12 stsp waddwstr(window, wline);
3021 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
3022 41605754 2020-11-12 stsp free(wline);
3023 41605754 2020-11-12 stsp free(s);
3024 41605754 2020-11-12 stsp wlimit -= width;
3025 41605754 2020-11-12 stsp *wtotal += width;
3026 41605754 2020-11-12 stsp }
3027 41605754 2020-11-12 stsp
3028 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3029 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3030 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3031 41605754 2020-11-12 stsp if (err)
3032 41605754 2020-11-12 stsp return err;
3033 41605754 2020-11-12 stsp waddwstr(window, wline);
3034 41605754 2020-11-12 stsp free(wline);
3035 41605754 2020-11-12 stsp *wtotal += width;
3036 41605754 2020-11-12 stsp }
3037 41605754 2020-11-12 stsp
3038 41605754 2020-11-12 stsp return NULL;
3039 41605754 2020-11-12 stsp }
3040 41605754 2020-11-12 stsp
3041 41605754 2020-11-12 stsp static const struct got_error *
3042 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3043 26ed57b2 2018-05-19 stsp {
3044 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3045 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3046 61e69b96 2018-05-20 stsp const struct got_error *err;
3047 fe621944 2020-11-10 stsp int nprinted = 0;
3048 b304db33 2018-05-20 stsp char *line;
3049 826082fe 2020-12-10 stsp size_t linesize = 0;
3050 826082fe 2020-12-10 stsp ssize_t linelen;
3051 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3052 61e69b96 2018-05-20 stsp wchar_t *wline;
3053 e0b650dd 2018-05-20 stsp int width;
3054 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3055 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3056 fe621944 2020-11-10 stsp off_t line_offset;
3057 26ed57b2 2018-05-19 stsp
3058 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3059 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3060 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3061 fe621944 2020-11-10 stsp
3062 f7d12f7e 2018-08-01 stsp werase(view->window);
3063 a3404814 2018-09-02 stsp
3064 a3404814 2018-09-02 stsp if (header) {
3065 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3066 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3067 135a2da0 2020-11-11 stsp header) == -1)
3068 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3069 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3070 135a2da0 2020-11-11 stsp free(line);
3071 135a2da0 2020-11-11 stsp if (err)
3072 a3404814 2018-09-02 stsp return err;
3073 a3404814 2018-09-02 stsp
3074 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3075 a3404814 2018-09-02 stsp wstandout(view->window);
3076 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3077 e54cc94a 2020-11-11 stsp free(wline);
3078 e54cc94a 2020-11-11 stsp wline = NULL;
3079 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3080 a3404814 2018-09-02 stsp wstandend(view->window);
3081 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3082 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3083 26ed57b2 2018-05-19 stsp
3084 a3404814 2018-09-02 stsp if (max_lines <= 1)
3085 a3404814 2018-09-02 stsp return NULL;
3086 a3404814 2018-09-02 stsp max_lines--;
3087 a3404814 2018-09-02 stsp }
3088 a3404814 2018-09-02 stsp
3089 89f1a395 2020-12-01 naddy s->eof = 0;
3090 826082fe 2020-12-10 stsp line = NULL;
3091 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3092 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3093 826082fe 2020-12-10 stsp if (linelen == -1) {
3094 826082fe 2020-12-10 stsp if (feof(s->f)) {
3095 826082fe 2020-12-10 stsp s->eof = 1;
3096 826082fe 2020-12-10 stsp break;
3097 826082fe 2020-12-10 stsp }
3098 826082fe 2020-12-10 stsp free(line);
3099 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3100 61e69b96 2018-05-20 stsp }
3101 6d17833f 2019-11-08 stsp
3102 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3103 f26dddb7 2019-11-08 stsp if (tc)
3104 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3105 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3106 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3107 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3108 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3109 41605754 2020-11-12 stsp view->window, regmatch);
3110 41605754 2020-11-12 stsp if (err) {
3111 41605754 2020-11-12 stsp free(line);
3112 41605754 2020-11-12 stsp return err;
3113 41605754 2020-11-12 stsp }
3114 41605754 2020-11-12 stsp } else {
3115 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3116 41605754 2020-11-12 stsp if (err) {
3117 41605754 2020-11-12 stsp free(line);
3118 41605754 2020-11-12 stsp return err;
3119 41605754 2020-11-12 stsp }
3120 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3121 41605754 2020-11-12 stsp free(wline);
3122 41605754 2020-11-12 stsp wline = NULL;
3123 41605754 2020-11-12 stsp }
3124 f26dddb7 2019-11-08 stsp if (tc)
3125 6d17833f 2019-11-08 stsp wattr_off(view->window,
3126 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3127 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3128 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3129 fe621944 2020-11-10 stsp nprinted++;
3130 826082fe 2020-12-10 stsp }
3131 826082fe 2020-12-10 stsp free(line);
3132 fe621944 2020-11-10 stsp if (nprinted >= 1)
3133 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3134 89f1a395 2020-12-01 naddy (nprinted - 1);
3135 fe621944 2020-11-10 stsp else
3136 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3137 26ed57b2 2018-05-19 stsp
3138 1a57306a 2018-09-02 stsp view_vborder(view);
3139 c3e9aa98 2019-05-13 jcs
3140 89f1a395 2020-12-01 naddy if (s->eof) {
3141 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3142 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3143 c3e9aa98 2019-05-13 jcs nprinted++;
3144 c3e9aa98 2019-05-13 jcs }
3145 c3e9aa98 2019-05-13 jcs
3146 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3147 c3e9aa98 2019-05-13 jcs if (err) {
3148 c3e9aa98 2019-05-13 jcs return err;
3149 c3e9aa98 2019-05-13 jcs }
3150 26ed57b2 2018-05-19 stsp
3151 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3152 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3153 e54cc94a 2020-11-11 stsp free(wline);
3154 e54cc94a 2020-11-11 stsp wline = NULL;
3155 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3156 c3e9aa98 2019-05-13 jcs }
3157 c3e9aa98 2019-05-13 jcs
3158 26ed57b2 2018-05-19 stsp return NULL;
3159 abd2672a 2018-12-23 stsp }
3160 abd2672a 2018-12-23 stsp
3161 abd2672a 2018-12-23 stsp static char *
3162 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3163 abd2672a 2018-12-23 stsp {
3164 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3165 09867e48 2019-08-13 stsp char *p, *s;
3166 09867e48 2019-08-13 stsp
3167 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3168 09867e48 2019-08-13 stsp if (tm == NULL)
3169 09867e48 2019-08-13 stsp return NULL;
3170 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3171 09867e48 2019-08-13 stsp if (s == NULL)
3172 09867e48 2019-08-13 stsp return NULL;
3173 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3174 abd2672a 2018-12-23 stsp if (p)
3175 abd2672a 2018-12-23 stsp *p = '\0';
3176 abd2672a 2018-12-23 stsp return s;
3177 9f7d7167 2018-04-29 stsp }
3178 9f7d7167 2018-04-29 stsp
3179 4ed7e80c 2018-05-20 stsp static const struct got_error *
3180 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3181 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3182 0208f208 2020-05-05 stsp {
3183 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3184 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3185 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3186 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3187 0208f208 2020-05-05 stsp
3188 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3189 0208f208 2020-05-05 stsp if (qid != NULL) {
3190 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3191 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3192 d7b5a0e8 2022-04-20 stsp &qid->id);
3193 0208f208 2020-05-05 stsp if (err)
3194 0208f208 2020-05-05 stsp return err;
3195 0208f208 2020-05-05 stsp
3196 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3197 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3198 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3199 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3200 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3201 aa8b5dd0 2021-08-01 stsp }
3202 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3203 0208f208 2020-05-05 stsp
3204 0208f208 2020-05-05 stsp }
3205 0208f208 2020-05-05 stsp
3206 0208f208 2020-05-05 stsp if (tree_id1) {
3207 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3208 0208f208 2020-05-05 stsp if (err)
3209 0208f208 2020-05-05 stsp goto done;
3210 0208f208 2020-05-05 stsp }
3211 0208f208 2020-05-05 stsp
3212 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3213 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3214 0208f208 2020-05-05 stsp if (err)
3215 0208f208 2020-05-05 stsp goto done;
3216 0208f208 2020-05-05 stsp
3217 b72706c3 2022-06-01 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3218 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3219 0208f208 2020-05-05 stsp done:
3220 0208f208 2020-05-05 stsp if (tree1)
3221 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3222 0208f208 2020-05-05 stsp if (tree2)
3223 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3224 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3225 0208f208 2020-05-05 stsp return err;
3226 0208f208 2020-05-05 stsp }
3227 0208f208 2020-05-05 stsp
3228 0208f208 2020-05-05 stsp static const struct got_error *
3229 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3230 abd2672a 2018-12-23 stsp {
3231 fe621944 2020-11-10 stsp off_t *p;
3232 fe621944 2020-11-10 stsp
3233 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3234 fe621944 2020-11-10 stsp if (p == NULL)
3235 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3236 fe621944 2020-11-10 stsp *line_offsets = p;
3237 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3238 fe621944 2020-11-10 stsp (*nlines)++;
3239 fe621944 2020-11-10 stsp return NULL;
3240 fe621944 2020-11-10 stsp }
3241 fe621944 2020-11-10 stsp
3242 fe621944 2020-11-10 stsp static const struct got_error *
3243 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3244 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3245 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3246 fe621944 2020-11-10 stsp {
3247 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3248 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3249 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3250 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3251 45d799e2 2018-12-23 stsp time_t committer_time;
3252 45d799e2 2018-12-23 stsp const char *author, *committer;
3253 8b473291 2019-02-21 stsp char *refs_str = NULL;
3254 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3255 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3256 fe621944 2020-11-10 stsp off_t outoff = 0;
3257 fe621944 2020-11-10 stsp int n;
3258 abd2672a 2018-12-23 stsp
3259 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3260 0208f208 2020-05-05 stsp
3261 8b473291 2019-02-21 stsp if (refs) {
3262 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3263 8b473291 2019-02-21 stsp if (err)
3264 8b473291 2019-02-21 stsp return err;
3265 8b473291 2019-02-21 stsp }
3266 8b473291 2019-02-21 stsp
3267 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3268 abd2672a 2018-12-23 stsp if (err)
3269 abd2672a 2018-12-23 stsp return err;
3270 abd2672a 2018-12-23 stsp
3271 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3272 15a94983 2018-12-23 stsp if (err) {
3273 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3274 15a94983 2018-12-23 stsp goto done;
3275 15a94983 2018-12-23 stsp }
3276 abd2672a 2018-12-23 stsp
3277 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3278 fe621944 2020-11-10 stsp if (err)
3279 fe621944 2020-11-10 stsp goto done;
3280 fe621944 2020-11-10 stsp
3281 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3282 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3283 fe621944 2020-11-10 stsp if (n < 0) {
3284 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3285 abd2672a 2018-12-23 stsp goto done;
3286 abd2672a 2018-12-23 stsp }
3287 fe621944 2020-11-10 stsp outoff += n;
3288 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3289 fe621944 2020-11-10 stsp if (err)
3290 fe621944 2020-11-10 stsp goto done;
3291 fe621944 2020-11-10 stsp
3292 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3293 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3294 fe621944 2020-11-10 stsp if (n < 0) {
3295 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3296 abd2672a 2018-12-23 stsp goto done;
3297 abd2672a 2018-12-23 stsp }
3298 fe621944 2020-11-10 stsp outoff += n;
3299 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3300 fe621944 2020-11-10 stsp if (err)
3301 fe621944 2020-11-10 stsp goto done;
3302 fe621944 2020-11-10 stsp
3303 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3304 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3305 fe621944 2020-11-10 stsp if (datestr) {
3306 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3307 fe621944 2020-11-10 stsp if (n < 0) {
3308 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3309 fe621944 2020-11-10 stsp goto done;
3310 fe621944 2020-11-10 stsp }
3311 fe621944 2020-11-10 stsp outoff += n;
3312 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3313 fe621944 2020-11-10 stsp if (err)
3314 fe621944 2020-11-10 stsp goto done;
3315 abd2672a 2018-12-23 stsp }
3316 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3317 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3318 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3319 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3320 fe621944 2020-11-10 stsp if (n < 0) {
3321 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3322 fe621944 2020-11-10 stsp goto done;
3323 fe621944 2020-11-10 stsp }
3324 fe621944 2020-11-10 stsp outoff += n;
3325 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3326 fe621944 2020-11-10 stsp if (err)
3327 fe621944 2020-11-10 stsp goto done;
3328 abd2672a 2018-12-23 stsp }
3329 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3330 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3331 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3332 9f98ca05 2021-09-24 stsp int pn = 1;
3333 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3334 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3335 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3336 9f98ca05 2021-09-24 stsp if (err)
3337 9f98ca05 2021-09-24 stsp goto done;
3338 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3339 9f98ca05 2021-09-24 stsp if (n < 0) {
3340 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3341 9f98ca05 2021-09-24 stsp goto done;
3342 9f98ca05 2021-09-24 stsp }
3343 9f98ca05 2021-09-24 stsp outoff += n;
3344 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3345 9f98ca05 2021-09-24 stsp if (err)
3346 9f98ca05 2021-09-24 stsp goto done;
3347 9f98ca05 2021-09-24 stsp free(id_str);
3348 9f98ca05 2021-09-24 stsp id_str = NULL;
3349 9f98ca05 2021-09-24 stsp }
3350 9f98ca05 2021-09-24 stsp }
3351 9f98ca05 2021-09-24 stsp
3352 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3353 5943eee2 2019-08-13 stsp if (err)
3354 5943eee2 2019-08-13 stsp goto done;
3355 fe621944 2020-11-10 stsp s = logmsg;
3356 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3357 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3358 fe621944 2020-11-10 stsp if (n < 0) {
3359 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3360 fe621944 2020-11-10 stsp goto done;
3361 fe621944 2020-11-10 stsp }
3362 fe621944 2020-11-10 stsp outoff += n;
3363 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3364 fe621944 2020-11-10 stsp if (err)
3365 fe621944 2020-11-10 stsp goto done;
3366 abd2672a 2018-12-23 stsp }
3367 fe621944 2020-11-10 stsp
3368 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3369 0208f208 2020-05-05 stsp if (err)
3370 0208f208 2020-05-05 stsp goto done;
3371 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3372 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3373 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3374 fe621944 2020-11-10 stsp if (n < 0) {
3375 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3376 fe621944 2020-11-10 stsp goto done;
3377 fe621944 2020-11-10 stsp }
3378 fe621944 2020-11-10 stsp outoff += n;
3379 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3380 fe621944 2020-11-10 stsp if (err)
3381 fe621944 2020-11-10 stsp goto done;
3382 0208f208 2020-05-05 stsp free((char *)pe->path);
3383 0208f208 2020-05-05 stsp free(pe->data);
3384 0208f208 2020-05-05 stsp }
3385 fe621944 2020-11-10 stsp
3386 0208f208 2020-05-05 stsp fputc('\n', outfile);
3387 fe621944 2020-11-10 stsp outoff++;
3388 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3389 abd2672a 2018-12-23 stsp done:
3390 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3391 abd2672a 2018-12-23 stsp free(id_str);
3392 5943eee2 2019-08-13 stsp free(logmsg);
3393 8b473291 2019-02-21 stsp free(refs_str);
3394 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3395 7510f233 2020-08-09 stsp if (err) {
3396 ae6a6978 2020-08-09 stsp free(*line_offsets);
3397 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3398 ae6a6978 2020-08-09 stsp *nlines = 0;
3399 7510f233 2020-08-09 stsp }
3400 fe621944 2020-11-10 stsp return err;
3401 abd2672a 2018-12-23 stsp }
3402 abd2672a 2018-12-23 stsp
3403 abd2672a 2018-12-23 stsp static const struct got_error *
3404 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3405 26ed57b2 2018-05-19 stsp {
3406 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3407 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3408 15a94983 2018-12-23 stsp int obj_type;
3409 fe621944 2020-11-10 stsp
3410 fe621944 2020-11-10 stsp free(s->line_offsets);
3411 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3412 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3413 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3414 fe621944 2020-11-10 stsp s->nlines = 0;
3415 26ed57b2 2018-05-19 stsp
3416 511a516b 2018-05-19 stsp f = got_opentemp();
3417 48ae06ee 2018-10-18 stsp if (f == NULL) {
3418 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3419 48ae06ee 2018-10-18 stsp goto done;
3420 48ae06ee 2018-10-18 stsp }
3421 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3422 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3423 fb43ecf1 2019-02-11 stsp goto done;
3424 fb43ecf1 2019-02-11 stsp }
3425 48ae06ee 2018-10-18 stsp s->f = f;
3426 26ed57b2 2018-05-19 stsp
3427 15a94983 2018-12-23 stsp if (s->id1)
3428 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3429 15a94983 2018-12-23 stsp else
3430 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3431 15a94983 2018-12-23 stsp if (err)
3432 15a94983 2018-12-23 stsp goto done;
3433 15a94983 2018-12-23 stsp
3434 15a94983 2018-12-23 stsp switch (obj_type) {
3435 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3436 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3437 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3438 b72706c3 2022-06-01 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3439 b72706c3 2022-06-01 stsp s->repo, s->f);
3440 26ed57b2 2018-05-19 stsp break;
3441 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3442 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3443 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3444 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3445 26ed57b2 2018-05-19 stsp break;
3446 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3447 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3448 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3449 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3450 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3451 abd2672a 2018-12-23 stsp
3452 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3453 abd2672a 2018-12-23 stsp if (err)
3454 3ffacbe1 2020-02-02 tracey goto done;
3455 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3456 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3457 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3458 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3459 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3460 f44b1f58 2020-02-02 tracey if (err)
3461 f44b1f58 2020-02-02 tracey goto done;
3462 f44b1f58 2020-02-02 tracey } else {
3463 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3464 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3465 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3466 fe621944 2020-11-10 stsp err = write_commit_info(
3467 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3468 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3469 f44b1f58 2020-02-02 tracey if (err)
3470 f44b1f58 2020-02-02 tracey goto done;
3471 f5404e4e 2020-02-02 tracey break;
3472 15a087fe 2019-02-21 stsp }
3473 abd2672a 2018-12-23 stsp }
3474 abd2672a 2018-12-23 stsp }
3475 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3476 abd2672a 2018-12-23 stsp
3477 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3478 b72706c3 2022-06-01 stsp s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3479 b72706c3 2022-06-01 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3480 26ed57b2 2018-05-19 stsp break;
3481 abd2672a 2018-12-23 stsp }
3482 26ed57b2 2018-05-19 stsp default:
3483 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3484 48ae06ee 2018-10-18 stsp break;
3485 26ed57b2 2018-05-19 stsp }
3486 f44b1f58 2020-02-02 tracey if (err)
3487 f44b1f58 2020-02-02 tracey goto done;
3488 48ae06ee 2018-10-18 stsp done:
3489 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3490 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3491 48ae06ee 2018-10-18 stsp return err;
3492 48ae06ee 2018-10-18 stsp }
3493 26ed57b2 2018-05-19 stsp
3494 f5215bb9 2019-02-22 stsp static void
3495 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3496 f5215bb9 2019-02-22 stsp {
3497 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3498 f5215bb9 2019-02-22 stsp update_panels();
3499 f5215bb9 2019-02-22 stsp doupdate();
3500 f44b1f58 2020-02-02 tracey }
3501 f44b1f58 2020-02-02 tracey
3502 f44b1f58 2020-02-02 tracey static const struct got_error *
3503 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3504 f44b1f58 2020-02-02 tracey {
3505 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3506 f44b1f58 2020-02-02 tracey
3507 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3508 f44b1f58 2020-02-02 tracey return NULL;
3509 f44b1f58 2020-02-02 tracey }
3510 f44b1f58 2020-02-02 tracey
3511 f44b1f58 2020-02-02 tracey static const struct got_error *
3512 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3513 f44b1f58 2020-02-02 tracey {
3514 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3515 f44b1f58 2020-02-02 tracey int lineno;
3516 826082fe 2020-12-10 stsp char *line = NULL;
3517 826082fe 2020-12-10 stsp size_t linesize = 0;
3518 826082fe 2020-12-10 stsp ssize_t linelen;
3519 f44b1f58 2020-02-02 tracey
3520 f44b1f58 2020-02-02 tracey if (!view->searching) {
3521 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3522 f44b1f58 2020-02-02 tracey return NULL;
3523 f44b1f58 2020-02-02 tracey }
3524 f44b1f58 2020-02-02 tracey
3525 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3526 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3527 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3528 f44b1f58 2020-02-02 tracey else
3529 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3530 487cd7d2 2021-12-17 stsp } else
3531 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
3532 f44b1f58 2020-02-02 tracey
3533 f44b1f58 2020-02-02 tracey while (1) {
3534 f44b1f58 2020-02-02 tracey off_t offset;
3535 f44b1f58 2020-02-02 tracey
3536 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3537 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3538 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3539 f44b1f58 2020-02-02 tracey break;
3540 f44b1f58 2020-02-02 tracey }
3541 f44b1f58 2020-02-02 tracey
3542 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3543 f44b1f58 2020-02-02 tracey lineno = 1;
3544 f44b1f58 2020-02-02 tracey else
3545 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3546 f44b1f58 2020-02-02 tracey }
3547 f44b1f58 2020-02-02 tracey
3548 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3549 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3550 f44b1f58 2020-02-02 tracey free(line);
3551 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3552 f44b1f58 2020-02-02 tracey }
3553 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3554 826082fe 2020-12-10 stsp if (linelen != -1 &&
3555 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3556 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3557 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3558 f44b1f58 2020-02-02 tracey break;
3559 f44b1f58 2020-02-02 tracey }
3560 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3561 f44b1f58 2020-02-02 tracey lineno++;
3562 f44b1f58 2020-02-02 tracey else
3563 f44b1f58 2020-02-02 tracey lineno--;
3564 f44b1f58 2020-02-02 tracey }
3565 826082fe 2020-12-10 stsp free(line);
3566 f44b1f58 2020-02-02 tracey
3567 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3568 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3569 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3570 f44b1f58 2020-02-02 tracey }
3571 f44b1f58 2020-02-02 tracey
3572 f44b1f58 2020-02-02 tracey return NULL;
3573 f5215bb9 2019-02-22 stsp }
3574 f5215bb9 2019-02-22 stsp
3575 48ae06ee 2018-10-18 stsp static const struct got_error *
3576 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
3577 b72706c3 2022-06-01 stsp {
3578 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
3579 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
3580 b72706c3 2022-06-01 stsp
3581 b72706c3 2022-06-01 stsp free(s->id1);
3582 b72706c3 2022-06-01 stsp s->id1 = NULL;
3583 b72706c3 2022-06-01 stsp free(s->id2);
3584 b72706c3 2022-06-01 stsp s->id2 = NULL;
3585 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
3586 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3587 b72706c3 2022-06-01 stsp s->f = NULL;
3588 b72706c3 2022-06-01 stsp if (s->f1 && fclose(s->f1) == EOF)
3589 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3590 b72706c3 2022-06-01 stsp s->f1 = NULL;
3591 b72706c3 2022-06-01 stsp if (s->f2 && fclose(s->f2) == EOF)
3592 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
3593 b72706c3 2022-06-01 stsp s->f2 = NULL;
3594 b72706c3 2022-06-01 stsp free_colors(&s->colors);
3595 b72706c3 2022-06-01 stsp free(s->line_offsets);
3596 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
3597 b72706c3 2022-06-01 stsp s->nlines = 0;
3598 b72706c3 2022-06-01 stsp return err;
3599 b72706c3 2022-06-01 stsp }
3600 b72706c3 2022-06-01 stsp
3601 b72706c3 2022-06-01 stsp static const struct got_error *
3602 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3603 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3604 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3605 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3606 48ae06ee 2018-10-18 stsp {
3607 48ae06ee 2018-10-18 stsp const struct got_error *err;
3608 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3609 5dc9f4bc 2018-08-04 stsp
3610 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
3611 b72706c3 2022-06-01 stsp
3612 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3613 15a94983 2018-12-23 stsp int type1, type2;
3614 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3615 15a94983 2018-12-23 stsp if (err)
3616 15a94983 2018-12-23 stsp return err;
3617 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3618 15a94983 2018-12-23 stsp if (err)
3619 15a94983 2018-12-23 stsp return err;
3620 15a94983 2018-12-23 stsp
3621 15a94983 2018-12-23 stsp if (type1 != type2)
3622 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3623 15a94983 2018-12-23 stsp }
3624 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3625 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3626 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3627 f44b1f58 2020-02-02 tracey s->repo = repo;
3628 f44b1f58 2020-02-02 tracey s->id1 = id1;
3629 f44b1f58 2020-02-02 tracey s->id2 = id2;
3630 3dbaef42 2020-11-24 stsp s->label1 = label1;
3631 3dbaef42 2020-11-24 stsp s->label2 = label2;
3632 48ae06ee 2018-10-18 stsp
3633 15a94983 2018-12-23 stsp if (id1) {
3634 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3635 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3636 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3637 b72706c3 2022-06-01 stsp s->f1 = got_opentemp();
3638 b72706c3 2022-06-01 stsp if (s->f1 == NULL) {
3639 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3640 b72706c3 2022-06-01 stsp goto done;
3641 b72706c3 2022-06-01 stsp }
3642 48ae06ee 2018-10-18 stsp } else
3643 5465d566 2020-02-01 tracey s->id1 = NULL;
3644 48ae06ee 2018-10-18 stsp
3645 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3646 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3647 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
3648 b72706c3 2022-06-01 stsp goto done;
3649 48ae06ee 2018-10-18 stsp }
3650 b72706c3 2022-06-01 stsp
3651 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
3652 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
3653 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
3654 b72706c3 2022-06-01 stsp goto done;
3655 b72706c3 2022-06-01 stsp }
3656 b72706c3 2022-06-01 stsp
3657 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3658 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3659 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3660 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3661 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3662 5465d566 2020-02-01 tracey s->log_view = log_view;
3663 5465d566 2020-02-01 tracey s->repo = repo;
3664 6d17833f 2019-11-08 stsp
3665 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3666 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3667 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3668 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3669 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3670 6d17833f 2019-11-08 stsp if (err)
3671 b72706c3 2022-06-01 stsp goto done;
3672 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3673 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3674 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3675 b72706c3 2022-06-01 stsp if (err)
3676 b72706c3 2022-06-01 stsp goto done;
3677 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3678 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3679 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3680 b72706c3 2022-06-01 stsp if (err)
3681 b72706c3 2022-06-01 stsp goto done;
3682 6d17833f 2019-11-08 stsp
3683 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3684 9f98ca05 2021-09-24 stsp "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3685 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3686 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3687 b72706c3 2022-06-01 stsp if (err)
3688 b72706c3 2022-06-01 stsp goto done;
3689 11b20872 2019-11-08 stsp
3690 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3691 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3692 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3693 b72706c3 2022-06-01 stsp if (err)
3694 b72706c3 2022-06-01 stsp goto done;
3695 11b20872 2019-11-08 stsp
3696 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3697 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3698 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3699 b72706c3 2022-06-01 stsp if (err)
3700 b72706c3 2022-06-01 stsp goto done;
3701 6d17833f 2019-11-08 stsp }
3702 5dc9f4bc 2018-08-04 stsp
3703 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3704 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3705 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3706 f5215bb9 2019-02-22 stsp
3707 5465d566 2020-02-01 tracey err = create_diff(s);
3708 48ae06ee 2018-10-18 stsp
3709 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3710 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3711 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3712 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3713 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3714 b72706c3 2022-06-01 stsp done:
3715 b72706c3 2022-06-01 stsp if (err)
3716 b72706c3 2022-06-01 stsp close_diff_view(view);
3717 e5a0f69f 2018-08-18 stsp return err;
3718 5dc9f4bc 2018-08-04 stsp }
3719 5dc9f4bc 2018-08-04 stsp
3720 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3721 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3722 5dc9f4bc 2018-08-04 stsp {
3723 a3404814 2018-09-02 stsp const struct got_error *err;
3724 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3725 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3726 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3727 a3404814 2018-09-02 stsp
3728 a3404814 2018-09-02 stsp if (s->id1) {
3729 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3730 a3404814 2018-09-02 stsp if (err)
3731 a3404814 2018-09-02 stsp return err;
3732 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3733 3dbaef42 2020-11-24 stsp } else
3734 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3735 3dbaef42 2020-11-24 stsp
3736 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3737 a3404814 2018-09-02 stsp if (err)
3738 a3404814 2018-09-02 stsp return err;
3739 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3740 26ed57b2 2018-05-19 stsp
3741 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3742 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3743 a3404814 2018-09-02 stsp free(id_str1);
3744 a3404814 2018-09-02 stsp free(id_str2);
3745 a3404814 2018-09-02 stsp return err;
3746 a3404814 2018-09-02 stsp }
3747 a3404814 2018-09-02 stsp free(id_str1);
3748 a3404814 2018-09-02 stsp free(id_str2);
3749 a3404814 2018-09-02 stsp
3750 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3751 267bb3b8 2021-08-01 stsp free(header);
3752 267bb3b8 2021-08-01 stsp return err;
3753 15a087fe 2019-02-21 stsp }
3754 15a087fe 2019-02-21 stsp
3755 15a087fe 2019-02-21 stsp static const struct got_error *
3756 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3757 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3758 15a087fe 2019-02-21 stsp {
3759 d7a04538 2019-02-21 stsp const struct got_error *err;
3760 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3761 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3762 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3763 15a087fe 2019-02-21 stsp
3764 15a087fe 2019-02-21 stsp free(s->id2);
3765 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3766 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3767 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3768 15a087fe 2019-02-21 stsp
3769 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3770 d7a04538 2019-02-21 stsp if (err)
3771 d7a04538 2019-02-21 stsp return err;
3772 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3773 15a087fe 2019-02-21 stsp free(s->id1);
3774 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3775 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3776 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3777 15a087fe 2019-02-21 stsp return NULL;
3778 0cf4efb1 2018-09-29 stsp }
3779 0cf4efb1 2018-09-29 stsp
3780 0cf4efb1 2018-09-29 stsp static const struct got_error *
3781 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3782 e5a0f69f 2018-08-18 stsp {
3783 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3784 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3785 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3786 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3787 826082fe 2020-12-10 stsp char *line = NULL;
3788 826082fe 2020-12-10 stsp size_t linesize = 0;
3789 826082fe 2020-12-10 stsp ssize_t linelen;
3790 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
3791 e5a0f69f 2018-08-18 stsp
3792 e5a0f69f 2018-08-18 stsp switch (ch) {
3793 64453f7e 2020-11-21 stsp case 'a':
3794 3dbaef42 2020-11-24 stsp case 'w':
3795 3dbaef42 2020-11-24 stsp if (ch == 'a')
3796 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3797 3dbaef42 2020-11-24 stsp if (ch == 'w')
3798 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3799 64453f7e 2020-11-21 stsp wclear(view->window);
3800 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3801 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3802 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3803 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3804 64453f7e 2020-11-21 stsp err = create_diff(s);
3805 912a3f79 2021-08-30 j break;
3806 912a3f79 2021-08-30 j case 'g':
3807 912a3f79 2021-08-30 j case KEY_HOME:
3808 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3809 64453f7e 2020-11-21 stsp break;
3810 912a3f79 2021-08-30 j case 'G':
3811 912a3f79 2021-08-30 j case KEY_END:
3812 912a3f79 2021-08-30 j if (s->eof)
3813 912a3f79 2021-08-30 j break;
3814 912a3f79 2021-08-30 j
3815 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3816 912a3f79 2021-08-30 j s->eof = 1;
3817 912a3f79 2021-08-30 j break;
3818 1e37a5c2 2019-05-12 jcs case 'k':
3819 1e37a5c2 2019-05-12 jcs case KEY_UP:
3820 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3821 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3822 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3823 1e37a5c2 2019-05-12 jcs break;
3824 83cc4199 2022-06-13 stsp case CTRL('u'):
3825 33c3719a 2022-06-15 stsp case 'u':
3826 83cc4199 2022-06-13 stsp nscroll /= 2;
3827 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3828 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3829 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3830 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3831 26ed57b2 2018-05-19 stsp break;
3832 1e37a5c2 2019-05-12 jcs i = 0;
3833 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
3834 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3835 1e37a5c2 2019-05-12 jcs break;
3836 1e37a5c2 2019-05-12 jcs case 'j':
3837 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3838 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3839 1e37a5c2 2019-05-12 jcs if (!s->eof)
3840 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3841 1e37a5c2 2019-05-12 jcs break;
3842 83cc4199 2022-06-13 stsp case CTRL('d'):
3843 33c3719a 2022-06-15 stsp case 'd':
3844 83cc4199 2022-06-13 stsp nscroll /= 2;
3845 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3846 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3847 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3848 1e37a5c2 2019-05-12 jcs case ' ':
3849 00ba99a7 2019-05-12 jcs if (s->eof)
3850 1e37a5c2 2019-05-12 jcs break;
3851 1e37a5c2 2019-05-12 jcs i = 0;
3852 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
3853 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3854 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3855 826082fe 2020-12-10 stsp if (linelen == -1) {
3856 826082fe 2020-12-10 stsp if (feof(s->f)) {
3857 826082fe 2020-12-10 stsp s->eof = 1;
3858 826082fe 2020-12-10 stsp } else
3859 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3860 34bc9ec9 2019-02-22 stsp break;
3861 826082fe 2020-12-10 stsp }
3862 1e37a5c2 2019-05-12 jcs }
3863 826082fe 2020-12-10 stsp free(line);
3864 1e37a5c2 2019-05-12 jcs break;
3865 1e37a5c2 2019-05-12 jcs case '[':
3866 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3867 1e37a5c2 2019-05-12 jcs s->diff_context--;
3868 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3869 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3870 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3871 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3872 27829c9e 2020-11-21 stsp s->nlines) {
3873 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3874 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3875 27829c9e 2020-11-21 stsp }
3876 1e37a5c2 2019-05-12 jcs }
3877 1e37a5c2 2019-05-12 jcs break;
3878 1e37a5c2 2019-05-12 jcs case ']':
3879 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3880 1e37a5c2 2019-05-12 jcs s->diff_context++;
3881 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3882 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3883 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3884 1e37a5c2 2019-05-12 jcs }
3885 1e37a5c2 2019-05-12 jcs break;
3886 1e37a5c2 2019-05-12 jcs case '<':
3887 1e37a5c2 2019-05-12 jcs case ',':
3888 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3889 48ae06ee 2018-10-18 stsp break;
3890 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3891 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3892 6524637e 2019-02-21 stsp
3893 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3894 1e37a5c2 2019-05-12 jcs if (err)
3895 1e37a5c2 2019-05-12 jcs break;
3896 15a087fe 2019-02-21 stsp
3897 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3898 5a8b5076 2020-12-05 stsp break;
3899 5a8b5076 2020-12-05 stsp
3900 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3901 1e37a5c2 2019-05-12 jcs if (err)
3902 1e37a5c2 2019-05-12 jcs break;
3903 15a087fe 2019-02-21 stsp
3904 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3905 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3906 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3907 15a087fe 2019-02-21 stsp
3908 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3909 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3910 1e37a5c2 2019-05-12 jcs break;
3911 1e37a5c2 2019-05-12 jcs case '>':
3912 1e37a5c2 2019-05-12 jcs case '.':
3913 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3914 15a087fe 2019-02-21 stsp break;
3915 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3916 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3917 5e224a3e 2019-02-22 stsp
3918 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3919 1e37a5c2 2019-05-12 jcs if (err)
3920 5a8b5076 2020-12-05 stsp break;
3921 5a8b5076 2020-12-05 stsp
3922 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3923 1e37a5c2 2019-05-12 jcs break;
3924 15a087fe 2019-02-21 stsp
3925 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3926 1e37a5c2 2019-05-12 jcs if (err)
3927 1e37a5c2 2019-05-12 jcs break;
3928 15a087fe 2019-02-21 stsp
3929 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3930 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3931 67b5eae1 2021-12-27 naddy s->matched_line = 0;
3932 1e37a5c2 2019-05-12 jcs
3933 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3934 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3935 1e37a5c2 2019-05-12 jcs break;
3936 1e37a5c2 2019-05-12 jcs default:
3937 1e37a5c2 2019-05-12 jcs break;
3938 26ed57b2 2018-05-19 stsp }
3939 e5a0f69f 2018-08-18 stsp
3940 bcbd79e2 2018-08-19 stsp return err;
3941 26ed57b2 2018-05-19 stsp }
3942 26ed57b2 2018-05-19 stsp
3943 4ed7e80c 2018-05-20 stsp static const struct got_error *
3944 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3945 9f7d7167 2018-04-29 stsp {
3946 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3947 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3948 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3949 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3950 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3951 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3952 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3953 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3954 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3955 3dbaef42 2020-11-24 stsp const char *errstr;
3956 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3957 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3958 70ac5f84 2019-03-28 stsp
3959 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3960 26ed57b2 2018-05-19 stsp switch (ch) {
3961 64453f7e 2020-11-21 stsp case 'a':
3962 64453f7e 2020-11-21 stsp force_text_diff = 1;
3963 3dbaef42 2020-11-24 stsp break;
3964 3dbaef42 2020-11-24 stsp case 'C':
3965 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3966 3dbaef42 2020-11-24 stsp &errstr);
3967 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3968 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
3969 5a20d08d 2022-02-09 op errstr, errstr);
3970 64453f7e 2020-11-21 stsp break;
3971 09b5bff8 2020-02-23 naddy case 'r':
3972 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3973 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3974 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3975 09b5bff8 2020-02-23 naddy optarg);
3976 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3977 09b5bff8 2020-02-23 naddy break;
3978 3dbaef42 2020-11-24 stsp case 'w':
3979 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3980 3dbaef42 2020-11-24 stsp break;
3981 26ed57b2 2018-05-19 stsp default:
3982 17020d27 2019-03-07 stsp usage_diff();
3983 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3984 26ed57b2 2018-05-19 stsp }
3985 26ed57b2 2018-05-19 stsp }
3986 26ed57b2 2018-05-19 stsp
3987 26ed57b2 2018-05-19 stsp argc -= optind;
3988 26ed57b2 2018-05-19 stsp argv += optind;
3989 26ed57b2 2018-05-19 stsp
3990 26ed57b2 2018-05-19 stsp if (argc == 0) {
3991 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3992 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3993 15a94983 2018-12-23 stsp id_str1 = argv[0];
3994 15a94983 2018-12-23 stsp id_str2 = argv[1];
3995 26ed57b2 2018-05-19 stsp } else
3996 26ed57b2 2018-05-19 stsp usage_diff();
3997 eb6600df 2019-01-04 stsp
3998 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3999 0ae84acc 2022-06-15 tracey if (error)
4000 0ae84acc 2022-06-15 tracey goto done;
4001 0ae84acc 2022-06-15 tracey
4002 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4003 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4004 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4005 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4006 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4007 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4008 c156c7a4 2020-12-18 stsp goto done;
4009 a273ac94 2020-02-23 naddy if (worktree)
4010 a273ac94 2020-02-23 naddy repo_path =
4011 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4012 a273ac94 2020-02-23 naddy else
4013 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4014 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4015 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4016 c156c7a4 2020-12-18 stsp goto done;
4017 c156c7a4 2020-12-18 stsp }
4018 a273ac94 2020-02-23 naddy }
4019 a273ac94 2020-02-23 naddy
4020 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4021 eb6600df 2019-01-04 stsp if (error)
4022 eb6600df 2019-01-04 stsp goto done;
4023 26ed57b2 2018-05-19 stsp
4024 a273ac94 2020-02-23 naddy init_curses();
4025 a273ac94 2020-02-23 naddy
4026 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4027 51a10b52 2020-12-26 stsp if (error)
4028 51a10b52 2020-12-26 stsp goto done;
4029 51a10b52 2020-12-26 stsp
4030 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4031 26ed57b2 2018-05-19 stsp if (error)
4032 26ed57b2 2018-05-19 stsp goto done;
4033 26ed57b2 2018-05-19 stsp
4034 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4035 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4036 26ed57b2 2018-05-19 stsp if (error)
4037 26ed57b2 2018-05-19 stsp goto done;
4038 26ed57b2 2018-05-19 stsp
4039 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4040 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4041 26ed57b2 2018-05-19 stsp if (error)
4042 26ed57b2 2018-05-19 stsp goto done;
4043 26ed57b2 2018-05-19 stsp
4044 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4045 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4046 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4047 ea5e7bb5 2018-08-01 stsp goto done;
4048 ea5e7bb5 2018-08-01 stsp }
4049 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4050 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4051 5dc9f4bc 2018-08-04 stsp if (error)
4052 5dc9f4bc 2018-08-04 stsp goto done;
4053 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4054 26ed57b2 2018-05-19 stsp done:
4055 3dbaef42 2020-11-24 stsp free(label1);
4056 3dbaef42 2020-11-24 stsp free(label2);
4057 c02c541e 2019-03-29 stsp free(repo_path);
4058 a273ac94 2020-02-23 naddy free(cwd);
4059 1d0f4054 2021-06-17 stsp if (repo) {
4060 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4061 1d0f4054 2021-06-17 stsp if (error == NULL)
4062 1d0f4054 2021-06-17 stsp error = close_err;
4063 1d0f4054 2021-06-17 stsp }
4064 a273ac94 2020-02-23 naddy if (worktree)
4065 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4066 0ae84acc 2022-06-15 tracey if (pack_fds) {
4067 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4068 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4069 0ae84acc 2022-06-15 tracey if (error == NULL)
4070 0ae84acc 2022-06-15 tracey error = pack_err;
4071 0ae84acc 2022-06-15 tracey }
4072 51a10b52 2020-12-26 stsp tog_free_refs();
4073 26ed57b2 2018-05-19 stsp return error;
4074 9f7d7167 2018-04-29 stsp }
4075 9f7d7167 2018-04-29 stsp
4076 4ed7e80c 2018-05-20 stsp __dead static void
4077 9f7d7167 2018-04-29 stsp usage_blame(void)
4078 9f7d7167 2018-04-29 stsp {
4079 80ddbec8 2018-04-29 stsp endwin();
4080 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4081 9f7d7167 2018-04-29 stsp getprogname());
4082 9f7d7167 2018-04-29 stsp exit(1);
4083 9f7d7167 2018-04-29 stsp }
4084 84451b3e 2018-07-10 stsp
4085 84451b3e 2018-07-10 stsp struct tog_blame_line {
4086 84451b3e 2018-07-10 stsp int annotated;
4087 84451b3e 2018-07-10 stsp struct got_object_id *id;
4088 84451b3e 2018-07-10 stsp };
4089 9f7d7167 2018-04-29 stsp
4090 4ed7e80c 2018-05-20 stsp static const struct got_error *
4091 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4092 84451b3e 2018-07-10 stsp {
4093 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4094 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4095 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4096 84451b3e 2018-07-10 stsp const struct got_error *err;
4097 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4098 826082fe 2020-12-10 stsp char *line = NULL;
4099 826082fe 2020-12-10 stsp size_t linesize = 0;
4100 826082fe 2020-12-10 stsp ssize_t linelen;
4101 84451b3e 2018-07-10 stsp wchar_t *wline;
4102 27a741e5 2019-09-11 stsp int width;
4103 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4104 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4105 ab089a2a 2018-07-12 stsp char *id_str;
4106 11b20872 2019-11-08 stsp struct tog_color *tc;
4107 ab089a2a 2018-07-12 stsp
4108 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4109 ab089a2a 2018-07-12 stsp if (err)
4110 ab089a2a 2018-07-12 stsp return err;
4111 84451b3e 2018-07-10 stsp
4112 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4113 f7d12f7e 2018-08-01 stsp werase(view->window);
4114 84451b3e 2018-07-10 stsp
4115 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4116 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4117 ab089a2a 2018-07-12 stsp free(id_str);
4118 ab089a2a 2018-07-12 stsp return err;
4119 ab089a2a 2018-07-12 stsp }
4120 ab089a2a 2018-07-12 stsp
4121 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4122 ab089a2a 2018-07-12 stsp free(line);
4123 2550e4c3 2018-07-13 stsp line = NULL;
4124 1cae65b4 2019-09-22 stsp if (err)
4125 1cae65b4 2019-09-22 stsp return err;
4126 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4127 a3404814 2018-09-02 stsp wstandout(view->window);
4128 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4129 11b20872 2019-11-08 stsp if (tc)
4130 11b20872 2019-11-08 stsp wattr_on(view->window,
4131 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4132 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4133 11b20872 2019-11-08 stsp if (tc)
4134 11b20872 2019-11-08 stsp wattr_off(view->window,
4135 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4136 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4137 a3404814 2018-09-02 stsp wstandend(view->window);
4138 2550e4c3 2018-07-13 stsp free(wline);
4139 2550e4c3 2018-07-13 stsp wline = NULL;
4140 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4141 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4142 ab089a2a 2018-07-12 stsp
4143 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4144 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4145 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4146 ab089a2a 2018-07-12 stsp free(id_str);
4147 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4148 ab089a2a 2018-07-12 stsp }
4149 ab089a2a 2018-07-12 stsp free(id_str);
4150 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4151 3f60a8ef 2018-07-10 stsp free(line);
4152 2550e4c3 2018-07-13 stsp line = NULL;
4153 3f60a8ef 2018-07-10 stsp if (err)
4154 3f60a8ef 2018-07-10 stsp return err;
4155 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4156 2550e4c3 2018-07-13 stsp free(wline);
4157 2550e4c3 2018-07-13 stsp wline = NULL;
4158 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4159 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4160 3f60a8ef 2018-07-10 stsp
4161 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4162 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4163 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4164 826082fe 2020-12-10 stsp if (linelen == -1) {
4165 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4166 826082fe 2020-12-10 stsp s->eof = 1;
4167 826082fe 2020-12-10 stsp break;
4168 826082fe 2020-12-10 stsp }
4169 84451b3e 2018-07-10 stsp free(line);
4170 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4171 84451b3e 2018-07-10 stsp }
4172 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4173 826082fe 2020-12-10 stsp continue;
4174 84451b3e 2018-07-10 stsp
4175 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4176 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4177 b700b5d6 2018-07-10 stsp
4178 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4179 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4180 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4181 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4182 27a741e5 2019-09-11 stsp !(view->focussed &&
4183 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4184 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4185 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4186 8d0fe45a 2019-08-12 stsp char *id_str;
4187 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4188 8d0fe45a 2019-08-12 stsp if (err) {
4189 8d0fe45a 2019-08-12 stsp free(line);
4190 8d0fe45a 2019-08-12 stsp return err;
4191 8d0fe45a 2019-08-12 stsp }
4192 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4193 11b20872 2019-11-08 stsp if (tc)
4194 11b20872 2019-11-08 stsp wattr_on(view->window,
4195 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4196 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4197 11b20872 2019-11-08 stsp if (tc)
4198 11b20872 2019-11-08 stsp wattr_off(view->window,
4199 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4200 8d0fe45a 2019-08-12 stsp free(id_str);
4201 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4202 8d0fe45a 2019-08-12 stsp } else {
4203 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4204 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4205 84451b3e 2018-07-10 stsp }
4206 ee41ec32 2018-07-10 stsp } else {
4207 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4208 ee41ec32 2018-07-10 stsp prev_id = NULL;
4209 ee41ec32 2018-07-10 stsp }
4210 84451b3e 2018-07-10 stsp
4211 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4212 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4213 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4214 27a741e5 2019-09-11 stsp
4215 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4216 41605754 2020-11-12 stsp width = 9;
4217 41605754 2020-11-12 stsp wline = wcsdup(L"");
4218 41605754 2020-11-12 stsp if (wline == NULL) {
4219 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4220 41605754 2020-11-12 stsp free(line);
4221 41605754 2020-11-12 stsp return err;
4222 41605754 2020-11-12 stsp }
4223 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4224 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4225 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4226 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4227 41605754 2020-11-12 stsp view->window, regmatch);
4228 41605754 2020-11-12 stsp if (err) {
4229 41605754 2020-11-12 stsp free(line);
4230 41605754 2020-11-12 stsp return err;
4231 41605754 2020-11-12 stsp }
4232 41605754 2020-11-12 stsp width += 9;
4233 41605754 2020-11-12 stsp } else {
4234 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4235 41605754 2020-11-12 stsp view->ncols - 9, 9);
4236 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4237 41605754 2020-11-12 stsp free(wline);
4238 41605754 2020-11-12 stsp wline = NULL;
4239 41605754 2020-11-12 stsp width += 9;
4240 41605754 2020-11-12 stsp }
4241 41605754 2020-11-12 stsp
4242 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4243 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4244 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4245 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4246 84451b3e 2018-07-10 stsp }
4247 826082fe 2020-12-10 stsp free(line);
4248 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4249 84451b3e 2018-07-10 stsp
4250 1a57306a 2018-09-02 stsp view_vborder(view);
4251 84451b3e 2018-07-10 stsp
4252 84451b3e 2018-07-10 stsp return NULL;
4253 84451b3e 2018-07-10 stsp }
4254 84451b3e 2018-07-10 stsp
4255 84451b3e 2018-07-10 stsp static const struct got_error *
4256 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4257 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4258 84451b3e 2018-07-10 stsp {
4259 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4260 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4261 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4262 1a76625f 2018-10-22 stsp int errcode;
4263 84451b3e 2018-07-10 stsp
4264 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4265 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4266 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4267 84451b3e 2018-07-10 stsp
4268 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4269 1a76625f 2018-10-22 stsp if (errcode)
4270 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4271 84451b3e 2018-07-10 stsp
4272 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4273 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4274 d68a0a7d 2018-07-10 stsp goto done;
4275 d68a0a7d 2018-07-10 stsp }
4276 d68a0a7d 2018-07-10 stsp
4277 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4278 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4279 d68a0a7d 2018-07-10 stsp
4280 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4281 d68a0a7d 2018-07-10 stsp if (line->annotated)
4282 d68a0a7d 2018-07-10 stsp goto done;
4283 d68a0a7d 2018-07-10 stsp
4284 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4285 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4286 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4287 84451b3e 2018-07-10 stsp goto done;
4288 84451b3e 2018-07-10 stsp }
4289 84451b3e 2018-07-10 stsp line->annotated = 1;
4290 84451b3e 2018-07-10 stsp done:
4291 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4292 1a76625f 2018-10-22 stsp if (errcode)
4293 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4294 84451b3e 2018-07-10 stsp return err;
4295 84451b3e 2018-07-10 stsp }
4296 84451b3e 2018-07-10 stsp
4297 84451b3e 2018-07-10 stsp static void *
4298 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4299 84451b3e 2018-07-10 stsp {
4300 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4301 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4302 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4303 1a76625f 2018-10-22 stsp int errcode;
4304 18430de3 2018-07-10 stsp
4305 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4306 61266923 2020-01-14 stsp if (err)
4307 61266923 2020-01-14 stsp return (void *)err;
4308 61266923 2020-01-14 stsp
4309 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4310 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4311 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4312 fc06ba56 2019-08-22 stsp err = NULL;
4313 18430de3 2018-07-10 stsp
4314 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4315 1a76625f 2018-10-22 stsp if (errcode)
4316 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4317 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4318 18430de3 2018-07-10 stsp
4319 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4320 1d0f4054 2021-06-17 stsp if (err == NULL)
4321 1d0f4054 2021-06-17 stsp err = close_err;
4322 c9beca56 2018-07-22 stsp ta->repo = NULL;
4323 c9beca56 2018-07-22 stsp *ta->complete = 1;
4324 18430de3 2018-07-10 stsp
4325 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4326 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4327 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4328 18430de3 2018-07-10 stsp
4329 18430de3 2018-07-10 stsp return (void *)err;
4330 84451b3e 2018-07-10 stsp }
4331 84451b3e 2018-07-10 stsp
4332 245d91c1 2018-07-12 stsp static struct got_object_id *
4333 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4334 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4335 245d91c1 2018-07-12 stsp {
4336 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4337 8d0fe45a 2019-08-12 stsp
4338 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4339 8d0fe45a 2019-08-12 stsp return NULL;
4340 b880a918 2018-07-10 stsp
4341 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4342 245d91c1 2018-07-12 stsp if (!line->annotated)
4343 245d91c1 2018-07-12 stsp return NULL;
4344 245d91c1 2018-07-12 stsp
4345 245d91c1 2018-07-12 stsp return line->id;
4346 b880a918 2018-07-10 stsp }
4347 245d91c1 2018-07-12 stsp
4348 b880a918 2018-07-10 stsp static const struct got_error *
4349 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4350 a70480e0 2018-06-23 stsp {
4351 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4352 245d91c1 2018-07-12 stsp int i;
4353 245d91c1 2018-07-12 stsp
4354 245d91c1 2018-07-12 stsp if (blame->thread) {
4355 1a76625f 2018-10-22 stsp int errcode;
4356 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4357 1a76625f 2018-10-22 stsp if (errcode)
4358 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4359 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4360 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4361 1a76625f 2018-10-22 stsp if (errcode)
4362 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4363 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4364 1a76625f 2018-10-22 stsp if (errcode)
4365 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4366 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4367 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4368 245d91c1 2018-07-12 stsp err = NULL;
4369 245d91c1 2018-07-12 stsp blame->thread = NULL;
4370 245d91c1 2018-07-12 stsp }
4371 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4372 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4373 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4374 1d0f4054 2021-06-17 stsp if (err == NULL)
4375 1d0f4054 2021-06-17 stsp err = close_err;
4376 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4377 245d91c1 2018-07-12 stsp }
4378 245d91c1 2018-07-12 stsp if (blame->f) {
4379 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4380 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4381 245d91c1 2018-07-12 stsp blame->f = NULL;
4382 245d91c1 2018-07-12 stsp }
4383 57670559 2018-12-23 stsp if (blame->lines) {
4384 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4385 57670559 2018-12-23 stsp free(blame->lines[i].id);
4386 57670559 2018-12-23 stsp free(blame->lines);
4387 57670559 2018-12-23 stsp blame->lines = NULL;
4388 57670559 2018-12-23 stsp }
4389 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4390 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4391 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
4392 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4393 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
4394 0ae84acc 2022-06-15 tracey if (err == NULL)
4395 0ae84acc 2022-06-15 tracey err = pack_err;
4396 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
4397 0ae84acc 2022-06-15 tracey }
4398 245d91c1 2018-07-12 stsp return err;
4399 245d91c1 2018-07-12 stsp }
4400 245d91c1 2018-07-12 stsp
4401 245d91c1 2018-07-12 stsp static const struct got_error *
4402 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4403 fc06ba56 2019-08-22 stsp {
4404 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4405 fc06ba56 2019-08-22 stsp int *done = arg;
4406 fc06ba56 2019-08-22 stsp int errcode;
4407 fc06ba56 2019-08-22 stsp
4408 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4409 fc06ba56 2019-08-22 stsp if (errcode)
4410 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4411 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4412 fc06ba56 2019-08-22 stsp
4413 fc06ba56 2019-08-22 stsp if (*done)
4414 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4415 fc06ba56 2019-08-22 stsp
4416 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4417 fc06ba56 2019-08-22 stsp if (errcode)
4418 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4419 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4420 fc06ba56 2019-08-22 stsp
4421 fc06ba56 2019-08-22 stsp return err;
4422 fc06ba56 2019-08-22 stsp }
4423 fc06ba56 2019-08-22 stsp
4424 fc06ba56 2019-08-22 stsp static const struct got_error *
4425 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4426 245d91c1 2018-07-12 stsp {
4427 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4428 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4429 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4430 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4431 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4432 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4433 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4434 15a94983 2018-12-23 stsp int obj_type;
4435 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4436 a70480e0 2018-06-23 stsp
4437 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
4438 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
4439 27d434c2 2018-09-15 stsp if (err)
4440 15a94983 2018-12-23 stsp return err;
4441 a44927cc 2022-04-07 stsp
4442 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4443 a44927cc 2022-04-07 stsp if (err)
4444 a44927cc 2022-04-07 stsp goto done;
4445 27d434c2 2018-09-15 stsp
4446 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4447 84451b3e 2018-07-10 stsp if (err)
4448 84451b3e 2018-07-10 stsp goto done;
4449 27d434c2 2018-09-15 stsp
4450 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4451 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4452 84451b3e 2018-07-10 stsp goto done;
4453 84451b3e 2018-07-10 stsp }
4454 a70480e0 2018-06-23 stsp
4455 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4456 a70480e0 2018-06-23 stsp if (err)
4457 a70480e0 2018-06-23 stsp goto done;
4458 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4459 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4460 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4461 84451b3e 2018-07-10 stsp goto done;
4462 84451b3e 2018-07-10 stsp }
4463 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4464 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4465 1fddf795 2021-01-20 stsp if (err)
4466 1fddf795 2021-01-20 stsp goto done;
4467 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4468 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4469 84451b3e 2018-07-10 stsp goto done;
4470 1fddf795 2021-01-20 stsp }
4471 b02560ec 2019-08-19 stsp
4472 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4473 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4474 b02560ec 2019-08-19 stsp blame->nlines--;
4475 a70480e0 2018-06-23 stsp
4476 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4477 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4478 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4479 84451b3e 2018-07-10 stsp goto done;
4480 84451b3e 2018-07-10 stsp }
4481 a70480e0 2018-06-23 stsp
4482 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
4483 bd24772e 2018-07-11 stsp if (err)
4484 bd24772e 2018-07-11 stsp goto done;
4485 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4486 0ae84acc 2022-06-15 tracey pack_fds);
4487 0ae84acc 2022-06-15 tracey if (err)
4488 0ae84acc 2022-06-15 tracey goto done;
4489 bd24772e 2018-07-11 stsp
4490 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
4491 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4492 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4493 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4494 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4495 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4496 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4497 245d91c1 2018-07-12 stsp goto done;
4498 245d91c1 2018-07-12 stsp }
4499 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4500 245d91c1 2018-07-12 stsp
4501 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4502 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4503 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4504 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4505 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4506 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4507 a5388363 2020-12-01 naddy s->blame_complete = 0;
4508 f5a09613 2020-12-13 naddy
4509 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4510 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4511 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4512 f5a09613 2020-12-13 naddy s->selected_line = 1;
4513 f5a09613 2020-12-13 naddy }
4514 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4515 245d91c1 2018-07-12 stsp
4516 245d91c1 2018-07-12 stsp done:
4517 a44927cc 2022-04-07 stsp if (commit)
4518 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
4519 245d91c1 2018-07-12 stsp if (blob)
4520 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4521 27d434c2 2018-09-15 stsp free(obj_id);
4522 245d91c1 2018-07-12 stsp if (err)
4523 245d91c1 2018-07-12 stsp stop_blame(blame);
4524 245d91c1 2018-07-12 stsp return err;
4525 245d91c1 2018-07-12 stsp }
4526 245d91c1 2018-07-12 stsp
4527 245d91c1 2018-07-12 stsp static const struct got_error *
4528 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4529 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4530 245d91c1 2018-07-12 stsp {
4531 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4532 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4533 dbc6a6b6 2018-07-12 stsp
4534 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4535 245d91c1 2018-07-12 stsp
4536 c4843652 2019-08-12 stsp s->path = strdup(path);
4537 c4843652 2019-08-12 stsp if (s->path == NULL)
4538 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4539 c4843652 2019-08-12 stsp
4540 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4541 c4843652 2019-08-12 stsp if (err) {
4542 c4843652 2019-08-12 stsp free(s->path);
4543 7cbe629d 2018-08-04 stsp return err;
4544 c4843652 2019-08-12 stsp }
4545 245d91c1 2018-07-12 stsp
4546 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4547 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4548 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4549 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4550 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4551 fb2756b9 2018-08-04 stsp s->repo = repo;
4552 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4553 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4554 7cbe629d 2018-08-04 stsp
4555 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4556 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4558 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4559 11b20872 2019-11-08 stsp if (err)
4560 11b20872 2019-11-08 stsp return err;
4561 11b20872 2019-11-08 stsp }
4562 11b20872 2019-11-08 stsp
4563 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4564 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4565 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4566 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4567 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4568 e5a0f69f 2018-08-18 stsp
4569 a5388363 2020-12-01 naddy return run_blame(view);
4570 7cbe629d 2018-08-04 stsp }
4571 7cbe629d 2018-08-04 stsp
4572 e5a0f69f 2018-08-18 stsp static const struct got_error *
4573 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4574 7cbe629d 2018-08-04 stsp {
4575 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4576 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4577 7cbe629d 2018-08-04 stsp
4578 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4579 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4580 e5a0f69f 2018-08-18 stsp
4581 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4582 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4583 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4584 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4585 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4586 7cbe629d 2018-08-04 stsp }
4587 e5a0f69f 2018-08-18 stsp
4588 e5a0f69f 2018-08-18 stsp free(s->path);
4589 11b20872 2019-11-08 stsp free_colors(&s->colors);
4590 e5a0f69f 2018-08-18 stsp return err;
4591 7cbe629d 2018-08-04 stsp }
4592 7cbe629d 2018-08-04 stsp
4593 7cbe629d 2018-08-04 stsp static const struct got_error *
4594 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4595 6c4c42e0 2019-06-24 stsp {
4596 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4597 6c4c42e0 2019-06-24 stsp
4598 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4599 6c4c42e0 2019-06-24 stsp return NULL;
4600 6c4c42e0 2019-06-24 stsp }
4601 6c4c42e0 2019-06-24 stsp
4602 6c4c42e0 2019-06-24 stsp static const struct got_error *
4603 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4604 6c4c42e0 2019-06-24 stsp {
4605 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4606 6c4c42e0 2019-06-24 stsp int lineno;
4607 826082fe 2020-12-10 stsp char *line = NULL;
4608 826082fe 2020-12-10 stsp size_t linesize = 0;
4609 826082fe 2020-12-10 stsp ssize_t linelen;
4610 6c4c42e0 2019-06-24 stsp
4611 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4612 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4613 6c4c42e0 2019-06-24 stsp return NULL;
4614 6c4c42e0 2019-06-24 stsp }
4615 6c4c42e0 2019-06-24 stsp
4616 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4617 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4618 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4619 6c4c42e0 2019-06-24 stsp else
4620 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4621 487cd7d2 2021-12-17 stsp } else
4622 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
4623 6c4c42e0 2019-06-24 stsp
4624 6c4c42e0 2019-06-24 stsp while (1) {
4625 6c4c42e0 2019-06-24 stsp off_t offset;
4626 6c4c42e0 2019-06-24 stsp
4627 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4628 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4629 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4630 6c4c42e0 2019-06-24 stsp break;
4631 6c4c42e0 2019-06-24 stsp }
4632 2246482e 2019-06-25 stsp
4633 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4634 6c4c42e0 2019-06-24 stsp lineno = 1;
4635 6c4c42e0 2019-06-24 stsp else
4636 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4637 6c4c42e0 2019-06-24 stsp }
4638 6c4c42e0 2019-06-24 stsp
4639 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4640 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4641 6c4c42e0 2019-06-24 stsp free(line);
4642 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4643 6c4c42e0 2019-06-24 stsp }
4644 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4645 826082fe 2020-12-10 stsp if (linelen != -1 &&
4646 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4647 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4648 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4649 6c4c42e0 2019-06-24 stsp break;
4650 6c4c42e0 2019-06-24 stsp }
4651 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4652 6c4c42e0 2019-06-24 stsp lineno++;
4653 6c4c42e0 2019-06-24 stsp else
4654 6c4c42e0 2019-06-24 stsp lineno--;
4655 6c4c42e0 2019-06-24 stsp }
4656 826082fe 2020-12-10 stsp free(line);
4657 6c4c42e0 2019-06-24 stsp
4658 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4659 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4660 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4661 6c4c42e0 2019-06-24 stsp }
4662 6c4c42e0 2019-06-24 stsp
4663 6c4c42e0 2019-06-24 stsp return NULL;
4664 6c4c42e0 2019-06-24 stsp }
4665 6c4c42e0 2019-06-24 stsp
4666 6c4c42e0 2019-06-24 stsp static const struct got_error *
4667 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4668 7cbe629d 2018-08-04 stsp {
4669 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4670 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4671 2b380cc8 2018-10-24 stsp int errcode;
4672 2b380cc8 2018-10-24 stsp
4673 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
4674 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4675 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4676 2b380cc8 2018-10-24 stsp if (errcode)
4677 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4678 51fe7530 2019-08-19 stsp
4679 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4680 2b380cc8 2018-10-24 stsp }
4681 e5a0f69f 2018-08-18 stsp
4682 51fe7530 2019-08-19 stsp if (s->blame_complete)
4683 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4684 51fe7530 2019-08-19 stsp
4685 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4686 e5a0f69f 2018-08-18 stsp
4687 669b5ffa 2018-10-07 stsp view_vborder(view);
4688 e5a0f69f 2018-08-18 stsp return err;
4689 e5a0f69f 2018-08-18 stsp }
4690 e5a0f69f 2018-08-18 stsp
4691 e5a0f69f 2018-08-18 stsp static const struct got_error *
4692 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4693 e5a0f69f 2018-08-18 stsp {
4694 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4695 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4696 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4697 83cc4199 2022-06-13 stsp int begin_x = 0, nscroll = view->nlines - 2;
4698 7cbe629d 2018-08-04 stsp
4699 e5a0f69f 2018-08-18 stsp switch (ch) {
4700 1e37a5c2 2019-05-12 jcs case 'q':
4701 1e37a5c2 2019-05-12 jcs s->done = 1;
4702 4deef56f 2021-09-02 naddy break;
4703 4deef56f 2021-09-02 naddy case 'g':
4704 4deef56f 2021-09-02 naddy case KEY_HOME:
4705 4deef56f 2021-09-02 naddy s->selected_line = 1;
4706 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4707 4deef56f 2021-09-02 naddy break;
4708 4deef56f 2021-09-02 naddy case 'G':
4709 4deef56f 2021-09-02 naddy case KEY_END:
4710 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4711 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4712 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4713 4deef56f 2021-09-02 naddy } else {
4714 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4715 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4716 4deef56f 2021-09-02 naddy (view->nlines - 3);
4717 4deef56f 2021-09-02 naddy }
4718 1e37a5c2 2019-05-12 jcs break;
4719 1e37a5c2 2019-05-12 jcs case 'k':
4720 1e37a5c2 2019-05-12 jcs case KEY_UP:
4721 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4722 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4723 1e37a5c2 2019-05-12 jcs s->selected_line--;
4724 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4725 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4726 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4727 1e37a5c2 2019-05-12 jcs break;
4728 83cc4199 2022-06-13 stsp case CTRL('u'):
4729 33c3719a 2022-06-15 stsp case 'u':
4730 83cc4199 2022-06-13 stsp nscroll /= 2;
4731 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4732 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4733 ea025d1d 2020-02-22 naddy case CTRL('b'):
4734 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4735 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
4736 e5a0f69f 2018-08-18 stsp break;
4737 1e37a5c2 2019-05-12 jcs }
4738 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
4739 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
4740 1e37a5c2 2019-05-12 jcs else
4741 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4742 1e37a5c2 2019-05-12 jcs break;
4743 1e37a5c2 2019-05-12 jcs case 'j':
4744 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4745 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4746 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4747 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4748 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4749 1e37a5c2 2019-05-12 jcs s->selected_line++;
4750 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4751 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4752 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4753 1e37a5c2 2019-05-12 jcs break;
4754 1e37a5c2 2019-05-12 jcs case 'b':
4755 1e37a5c2 2019-05-12 jcs case 'p': {
4756 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4757 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4758 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4759 1e37a5c2 2019-05-12 jcs if (id == NULL)
4760 e5a0f69f 2018-08-18 stsp break;
4761 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4762 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
4763 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4764 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4765 1e37a5c2 2019-05-12 jcs int obj_type;
4766 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4767 1e37a5c2 2019-05-12 jcs s->repo, id);
4768 e5a0f69f 2018-08-18 stsp if (err)
4769 e5a0f69f 2018-08-18 stsp break;
4770 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4771 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4772 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4773 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4774 e5a0f69f 2018-08-18 stsp break;
4775 e5a0f69f 2018-08-18 stsp }
4776 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4777 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
4778 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
4779 a44927cc 2022-04-07 stsp if (err)
4780 a44927cc 2022-04-07 stsp break;
4781 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4782 a44927cc 2022-04-07 stsp pcommit, s->path);
4783 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
4784 e5a0f69f 2018-08-18 stsp if (err) {
4785 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4786 1e37a5c2 2019-05-12 jcs err = NULL;
4787 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4788 e5a0f69f 2018-08-18 stsp break;
4789 e5a0f69f 2018-08-18 stsp }
4790 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4791 1e37a5c2 2019-05-12 jcs blob_id);
4792 1e37a5c2 2019-05-12 jcs free(blob_id);
4793 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4794 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4795 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4796 e5a0f69f 2018-08-18 stsp break;
4797 1e37a5c2 2019-05-12 jcs }
4798 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4799 d7b5a0e8 2022-04-20 stsp &pid->id);
4800 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4801 1e37a5c2 2019-05-12 jcs } else {
4802 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4803 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
4804 1e37a5c2 2019-05-12 jcs break;
4805 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4806 1e37a5c2 2019-05-12 jcs id);
4807 1e37a5c2 2019-05-12 jcs }
4808 1e37a5c2 2019-05-12 jcs if (err)
4809 e5a0f69f 2018-08-18 stsp break;
4810 1e37a5c2 2019-05-12 jcs s->done = 1;
4811 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4812 1e37a5c2 2019-05-12 jcs s->done = 0;
4813 1e37a5c2 2019-05-12 jcs if (thread_err)
4814 1e37a5c2 2019-05-12 jcs break;
4815 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4816 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4817 a5388363 2020-12-01 naddy err = run_blame(view);
4818 1e37a5c2 2019-05-12 jcs if (err)
4819 1e37a5c2 2019-05-12 jcs break;
4820 1e37a5c2 2019-05-12 jcs break;
4821 1e37a5c2 2019-05-12 jcs }
4822 1e37a5c2 2019-05-12 jcs case 'B': {
4823 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4824 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4825 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
4826 1e37a5c2 2019-05-12 jcs break;
4827 1e37a5c2 2019-05-12 jcs s->done = 1;
4828 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4829 1e37a5c2 2019-05-12 jcs s->done = 0;
4830 1e37a5c2 2019-05-12 jcs if (thread_err)
4831 1e37a5c2 2019-05-12 jcs break;
4832 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4833 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4834 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4835 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4836 a5388363 2020-12-01 naddy err = run_blame(view);
4837 1e37a5c2 2019-05-12 jcs if (err)
4838 1e37a5c2 2019-05-12 jcs break;
4839 1e37a5c2 2019-05-12 jcs break;
4840 1e37a5c2 2019-05-12 jcs }
4841 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4842 1e37a5c2 2019-05-12 jcs case '\r': {
4843 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4844 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4845 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4846 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4847 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4848 1e37a5c2 2019-05-12 jcs if (id == NULL)
4849 1e37a5c2 2019-05-12 jcs break;
4850 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4851 1e37a5c2 2019-05-12 jcs if (err)
4852 1e37a5c2 2019-05-12 jcs break;
4853 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4854 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4855 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4856 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4857 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4858 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4859 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4860 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4861 1e37a5c2 2019-05-12 jcs break;
4862 15a94983 2018-12-23 stsp }
4863 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4864 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4865 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4866 1e37a5c2 2019-05-12 jcs if (err) {
4867 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4868 1e37a5c2 2019-05-12 jcs break;
4869 1e37a5c2 2019-05-12 jcs }
4870 e78dc838 2020-12-04 stsp view->focussed = 0;
4871 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4872 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4873 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4874 1e37a5c2 2019-05-12 jcs if (err)
4875 34bc9ec9 2019-02-22 stsp break;
4876 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4877 e78dc838 2020-12-04 stsp view->focus_child = 1;
4878 1e37a5c2 2019-05-12 jcs } else
4879 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4880 1e37a5c2 2019-05-12 jcs if (err)
4881 e5a0f69f 2018-08-18 stsp break;
4882 1e37a5c2 2019-05-12 jcs break;
4883 1e37a5c2 2019-05-12 jcs }
4884 83cc4199 2022-06-13 stsp case CTRL('d'):
4885 33c3719a 2022-06-15 stsp case 'd':
4886 83cc4199 2022-06-13 stsp nscroll /= 2;
4887 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4888 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4889 ea025d1d 2020-02-22 naddy case CTRL('f'):
4890 1e37a5c2 2019-05-12 jcs case ' ':
4891 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4892 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4893 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4894 e5a0f69f 2018-08-18 stsp break;
4895 1e37a5c2 2019-05-12 jcs }
4896 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4897 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4898 83cc4199 2022-06-13 stsp s->selected_line +=
4899 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
4900 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
4901 1e37a5c2 2019-05-12 jcs }
4902 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
4903 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
4904 1e37a5c2 2019-05-12 jcs else
4905 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4906 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
4907 1e37a5c2 2019-05-12 jcs break;
4908 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4909 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4910 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4911 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4912 1e37a5c2 2019-05-12 jcs }
4913 1e37a5c2 2019-05-12 jcs break;
4914 1e37a5c2 2019-05-12 jcs default:
4915 1e37a5c2 2019-05-12 jcs break;
4916 a70480e0 2018-06-23 stsp }
4917 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4918 a70480e0 2018-06-23 stsp }
4919 a70480e0 2018-06-23 stsp
4920 a70480e0 2018-06-23 stsp static const struct got_error *
4921 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4922 9f7d7167 2018-04-29 stsp {
4923 a70480e0 2018-06-23 stsp const struct got_error *error;
4924 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4925 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4926 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4927 0587e10c 2020-07-23 stsp char *link_target = NULL;
4928 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4929 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
4930 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4931 a70480e0 2018-06-23 stsp int ch;
4932 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4933 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4934 a70480e0 2018-06-23 stsp
4935 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4936 a70480e0 2018-06-23 stsp switch (ch) {
4937 a70480e0 2018-06-23 stsp case 'c':
4938 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4939 a70480e0 2018-06-23 stsp break;
4940 69069811 2018-08-02 stsp case 'r':
4941 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4942 69069811 2018-08-02 stsp if (repo_path == NULL)
4943 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4944 9ba1d308 2019-10-21 stsp optarg);
4945 69069811 2018-08-02 stsp break;
4946 a70480e0 2018-06-23 stsp default:
4947 17020d27 2019-03-07 stsp usage_blame();
4948 a70480e0 2018-06-23 stsp /* NOTREACHED */
4949 a70480e0 2018-06-23 stsp }
4950 a70480e0 2018-06-23 stsp }
4951 a70480e0 2018-06-23 stsp
4952 a70480e0 2018-06-23 stsp argc -= optind;
4953 a70480e0 2018-06-23 stsp argv += optind;
4954 a70480e0 2018-06-23 stsp
4955 f135c941 2020-02-20 stsp if (argc != 1)
4956 a70480e0 2018-06-23 stsp usage_blame();
4957 6962eb72 2020-02-20 stsp
4958 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4959 0ae84acc 2022-06-15 tracey if (error != NULL)
4960 0ae84acc 2022-06-15 tracey goto done;
4961 0ae84acc 2022-06-15 tracey
4962 69069811 2018-08-02 stsp if (repo_path == NULL) {
4963 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4964 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4965 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4966 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4967 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4968 c156c7a4 2020-12-18 stsp goto done;
4969 f135c941 2020-02-20 stsp if (worktree)
4970 eb41ed75 2019-02-05 stsp repo_path =
4971 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4972 f135c941 2020-02-20 stsp else
4973 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4974 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4975 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4976 c156c7a4 2020-12-18 stsp goto done;
4977 c156c7a4 2020-12-18 stsp }
4978 f135c941 2020-02-20 stsp }
4979 a915003a 2019-02-05 stsp
4980 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4981 c02c541e 2019-03-29 stsp if (error != NULL)
4982 8e94dd5b 2019-01-04 stsp goto done;
4983 69069811 2018-08-02 stsp
4984 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4985 f135c941 2020-02-20 stsp worktree);
4986 c02c541e 2019-03-29 stsp if (error)
4987 92205607 2019-01-04 stsp goto done;
4988 69069811 2018-08-02 stsp
4989 f135c941 2020-02-20 stsp init_curses();
4990 f135c941 2020-02-20 stsp
4991 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4992 51a10b52 2020-12-26 stsp if (error)
4993 51a10b52 2020-12-26 stsp goto done;
4994 51a10b52 2020-12-26 stsp
4995 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4996 eb41ed75 2019-02-05 stsp if (error)
4997 69069811 2018-08-02 stsp goto done;
4998 a70480e0 2018-06-23 stsp
4999 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5000 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5001 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5002 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5003 a70480e0 2018-06-23 stsp if (error != NULL)
5004 66b4983c 2018-06-23 stsp goto done;
5005 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5006 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5007 a70480e0 2018-06-23 stsp } else {
5008 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5009 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5010 a70480e0 2018-06-23 stsp }
5011 a19e88aa 2018-06-23 stsp if (error != NULL)
5012 8b473291 2019-02-21 stsp goto done;
5013 8b473291 2019-02-21 stsp
5014 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5015 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5016 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5017 e1cd8fed 2018-08-01 stsp goto done;
5018 e1cd8fed 2018-08-01 stsp }
5019 0587e10c 2020-07-23 stsp
5020 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5021 a44927cc 2022-04-07 stsp if (error)
5022 a44927cc 2022-04-07 stsp goto done;
5023 a44927cc 2022-04-07 stsp
5024 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5025 a44927cc 2022-04-07 stsp commit, repo);
5026 7cbe629d 2018-08-04 stsp if (error)
5027 7cbe629d 2018-08-04 stsp goto done;
5028 0587e10c 2020-07-23 stsp
5029 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5030 78756c87 2020-11-24 stsp commit_id, repo);
5031 0587e10c 2020-07-23 stsp if (error)
5032 0587e10c 2020-07-23 stsp goto done;
5033 12314ad4 2019-08-31 stsp if (worktree) {
5034 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5035 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5036 12314ad4 2019-08-31 stsp worktree = NULL;
5037 12314ad4 2019-08-31 stsp }
5038 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5039 a70480e0 2018-06-23 stsp done:
5040 69069811 2018-08-02 stsp free(repo_path);
5041 f135c941 2020-02-20 stsp free(in_repo_path);
5042 0587e10c 2020-07-23 stsp free(link_target);
5043 69069811 2018-08-02 stsp free(cwd);
5044 a70480e0 2018-06-23 stsp free(commit_id);
5045 a44927cc 2022-04-07 stsp if (commit)
5046 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5047 eb41ed75 2019-02-05 stsp if (worktree)
5048 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5049 1d0f4054 2021-06-17 stsp if (repo) {
5050 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5051 1d0f4054 2021-06-17 stsp if (error == NULL)
5052 1d0f4054 2021-06-17 stsp error = close_err;
5053 1d0f4054 2021-06-17 stsp }
5054 0ae84acc 2022-06-15 tracey if (pack_fds) {
5055 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5056 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5057 0ae84acc 2022-06-15 tracey if (error == NULL)
5058 0ae84acc 2022-06-15 tracey error = pack_err;
5059 0ae84acc 2022-06-15 tracey }
5060 51a10b52 2020-12-26 stsp tog_free_refs();
5061 a70480e0 2018-06-23 stsp return error;
5062 ffd1d5e5 2018-06-23 stsp }
5063 ffd1d5e5 2018-06-23 stsp
5064 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5065 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5066 ffd1d5e5 2018-06-23 stsp {
5067 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5068 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5069 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5070 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5071 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5072 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5073 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5074 ffd1d5e5 2018-06-23 stsp
5075 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5076 ffd1d5e5 2018-06-23 stsp
5077 f7d12f7e 2018-08-01 stsp werase(view->window);
5078 ffd1d5e5 2018-06-23 stsp
5079 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5080 ffd1d5e5 2018-06-23 stsp return NULL;
5081 ffd1d5e5 2018-06-23 stsp
5082 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5083 ffd1d5e5 2018-06-23 stsp if (err)
5084 ffd1d5e5 2018-06-23 stsp return err;
5085 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5086 a3404814 2018-09-02 stsp wstandout(view->window);
5087 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5088 11b20872 2019-11-08 stsp if (tc)
5089 11b20872 2019-11-08 stsp wattr_on(view->window,
5090 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5092 11b20872 2019-11-08 stsp if (tc)
5093 11b20872 2019-11-08 stsp wattr_off(view->window,
5094 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5095 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5096 a3404814 2018-09-02 stsp wstandend(view->window);
5097 2550e4c3 2018-07-13 stsp free(wline);
5098 2550e4c3 2018-07-13 stsp wline = NULL;
5099 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5100 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5101 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5102 ffd1d5e5 2018-06-23 stsp return NULL;
5103 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5104 ce52c690 2018-06-23 stsp if (err)
5105 ce52c690 2018-06-23 stsp return err;
5106 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5107 2550e4c3 2018-07-13 stsp free(wline);
5108 2550e4c3 2018-07-13 stsp wline = NULL;
5109 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5110 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5111 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5112 ffd1d5e5 2018-06-23 stsp return NULL;
5113 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5114 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5115 a1eca9bb 2018-06-23 stsp return NULL;
5116 ffd1d5e5 2018-06-23 stsp
5117 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5118 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5119 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5120 0cf4efb1 2018-09-29 stsp if (view->focussed)
5121 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5122 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5123 ffd1d5e5 2018-06-23 stsp }
5124 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5125 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5126 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5127 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5128 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5129 ffd1d5e5 2018-06-23 stsp return NULL;
5130 ffd1d5e5 2018-06-23 stsp n = 1;
5131 ffd1d5e5 2018-06-23 stsp } else {
5132 ffd1d5e5 2018-06-23 stsp n = 0;
5133 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5134 ffd1d5e5 2018-06-23 stsp }
5135 ffd1d5e5 2018-06-23 stsp
5136 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5137 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5138 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5139 848d6979 2019-08-12 stsp const char *modestr = "";
5140 56e0773d 2019-11-28 stsp mode_t mode;
5141 1d13200f 2018-07-12 stsp
5142 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5143 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5144 56e0773d 2019-11-28 stsp
5145 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5146 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5147 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5148 1d13200f 2018-07-12 stsp if (err)
5149 638f9024 2019-05-13 stsp return got_error_from_errno(
5150 230a42bd 2019-05-11 jcs "got_object_id_str");
5151 1d13200f 2018-07-12 stsp }
5152 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5153 63c5ca5d 2019-08-24 stsp modestr = "$";
5154 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5155 0d6c6ee3 2020-05-20 stsp int i;
5156 0d6c6ee3 2020-05-20 stsp
5157 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5158 d86d3b18 2020-12-01 naddy te, s->repo);
5159 0d6c6ee3 2020-05-20 stsp if (err) {
5160 0d6c6ee3 2020-05-20 stsp free(id_str);
5161 0d6c6ee3 2020-05-20 stsp return err;
5162 0d6c6ee3 2020-05-20 stsp }
5163 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5164 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5165 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5166 0d6c6ee3 2020-05-20 stsp }
5167 848d6979 2019-08-12 stsp modestr = "@";
5168 0d6c6ee3 2020-05-20 stsp }
5169 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5170 848d6979 2019-08-12 stsp modestr = "/";
5171 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5172 848d6979 2019-08-12 stsp modestr = "*";
5173 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5174 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5175 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5176 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5177 1d13200f 2018-07-12 stsp free(id_str);
5178 0d6c6ee3 2020-05-20 stsp free(link_target);
5179 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5180 1d13200f 2018-07-12 stsp }
5181 1d13200f 2018-07-12 stsp free(id_str);
5182 0d6c6ee3 2020-05-20 stsp free(link_target);
5183 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5184 ffd1d5e5 2018-06-23 stsp if (err) {
5185 ffd1d5e5 2018-06-23 stsp free(line);
5186 ffd1d5e5 2018-06-23 stsp break;
5187 ffd1d5e5 2018-06-23 stsp }
5188 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5189 0cf4efb1 2018-09-29 stsp if (view->focussed)
5190 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5191 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5192 ffd1d5e5 2018-06-23 stsp }
5193 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5194 f26dddb7 2019-11-08 stsp if (tc)
5195 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5196 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5197 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5198 f26dddb7 2019-11-08 stsp if (tc)
5199 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5200 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5201 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5202 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5203 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5204 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5205 ffd1d5e5 2018-06-23 stsp free(line);
5206 2550e4c3 2018-07-13 stsp free(wline);
5207 2550e4c3 2018-07-13 stsp wline = NULL;
5208 ffd1d5e5 2018-06-23 stsp n++;
5209 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5210 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5211 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5212 ffd1d5e5 2018-06-23 stsp break;
5213 ffd1d5e5 2018-06-23 stsp }
5214 ffd1d5e5 2018-06-23 stsp
5215 ffd1d5e5 2018-06-23 stsp return err;
5216 ffd1d5e5 2018-06-23 stsp }
5217 ffd1d5e5 2018-06-23 stsp
5218 ffd1d5e5 2018-06-23 stsp static void
5219 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5220 ffd1d5e5 2018-06-23 stsp {
5221 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5222 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5223 fa86c4bf 2020-11-29 stsp int i = 0;
5224 ffd1d5e5 2018-06-23 stsp
5225 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5226 ffd1d5e5 2018-06-23 stsp return;
5227 ffd1d5e5 2018-06-23 stsp
5228 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5229 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5230 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5231 fa86c4bf 2020-11-29 stsp if (!isroot)
5232 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5233 fa86c4bf 2020-11-29 stsp break;
5234 fa86c4bf 2020-11-29 stsp }
5235 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5236 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5237 ffd1d5e5 2018-06-23 stsp }
5238 ffd1d5e5 2018-06-23 stsp }
5239 ffd1d5e5 2018-06-23 stsp
5240 fa86c4bf 2020-11-29 stsp static void
5241 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5242 ffd1d5e5 2018-06-23 stsp {
5243 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5244 ffd1d5e5 2018-06-23 stsp int n = 0;
5245 ffd1d5e5 2018-06-23 stsp
5246 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5247 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5248 694d3271 2020-12-01 naddy s->first_displayed_entry);
5249 694d3271 2020-12-01 naddy else
5250 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5251 56e0773d 2019-11-28 stsp
5252 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5253 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5254 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5255 768394f3 2019-01-24 stsp if (last) {
5256 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5257 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5258 768394f3 2019-01-24 stsp }
5259 ffd1d5e5 2018-06-23 stsp }
5260 ffd1d5e5 2018-06-23 stsp }
5261 ffd1d5e5 2018-06-23 stsp
5262 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5263 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5264 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5265 ffd1d5e5 2018-06-23 stsp {
5266 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5267 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5268 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5269 ffd1d5e5 2018-06-23 stsp
5270 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5271 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5272 56e0773d 2019-11-28 stsp + 1 /* slash */;
5273 ce52c690 2018-06-23 stsp if (te)
5274 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5275 ce52c690 2018-06-23 stsp
5276 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5277 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5278 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5279 ffd1d5e5 2018-06-23 stsp
5280 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5281 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5282 d9765a41 2018-06-23 stsp while (pt) {
5283 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5284 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5285 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5286 cb2ebc8a 2018-06-23 stsp goto done;
5287 cb2ebc8a 2018-06-23 stsp }
5288 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5289 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5290 cb2ebc8a 2018-06-23 stsp goto done;
5291 cb2ebc8a 2018-06-23 stsp }
5292 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5293 ffd1d5e5 2018-06-23 stsp }
5294 ce52c690 2018-06-23 stsp if (te) {
5295 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5296 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5297 ce52c690 2018-06-23 stsp goto done;
5298 ce52c690 2018-06-23 stsp }
5299 cb2ebc8a 2018-06-23 stsp }
5300 ce52c690 2018-06-23 stsp done:
5301 ce52c690 2018-06-23 stsp if (err) {
5302 ce52c690 2018-06-23 stsp free(*path);
5303 ce52c690 2018-06-23 stsp *path = NULL;
5304 ce52c690 2018-06-23 stsp }
5305 ce52c690 2018-06-23 stsp return err;
5306 ce52c690 2018-06-23 stsp }
5307 ce52c690 2018-06-23 stsp
5308 ce52c690 2018-06-23 stsp static const struct got_error *
5309 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5310 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5311 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5312 ce52c690 2018-06-23 stsp {
5313 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5314 ce52c690 2018-06-23 stsp char *path;
5315 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5316 a0de39f3 2019-08-09 stsp
5317 a0de39f3 2019-08-09 stsp *new_view = NULL;
5318 69efd4c4 2018-07-18 stsp
5319 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5320 ce52c690 2018-06-23 stsp if (err)
5321 ce52c690 2018-06-23 stsp return err;
5322 ffd1d5e5 2018-06-23 stsp
5323 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5324 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5325 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5326 83ce39e3 2019-08-12 stsp goto done;
5327 83ce39e3 2019-08-12 stsp }
5328 cdf1ee82 2018-08-01 stsp
5329 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5330 e5a0f69f 2018-08-18 stsp if (err) {
5331 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5332 fc06ba56 2019-08-22 stsp err = NULL;
5333 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5334 e5a0f69f 2018-08-18 stsp } else
5335 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5336 83ce39e3 2019-08-12 stsp done:
5337 83ce39e3 2019-08-12 stsp free(path);
5338 69efd4c4 2018-07-18 stsp return err;
5339 69efd4c4 2018-07-18 stsp }
5340 69efd4c4 2018-07-18 stsp
5341 69efd4c4 2018-07-18 stsp static const struct got_error *
5342 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5343 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5344 69efd4c4 2018-07-18 stsp {
5345 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5346 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5347 69efd4c4 2018-07-18 stsp char *path;
5348 a0de39f3 2019-08-09 stsp
5349 a0de39f3 2019-08-09 stsp *new_view = NULL;
5350 69efd4c4 2018-07-18 stsp
5351 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5352 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5353 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5354 e5a0f69f 2018-08-18 stsp
5355 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5356 69efd4c4 2018-07-18 stsp if (err)
5357 69efd4c4 2018-07-18 stsp return err;
5358 69efd4c4 2018-07-18 stsp
5359 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5360 4e97c21c 2020-12-06 stsp path, 0);
5361 ba4f502b 2018-08-04 stsp if (err)
5362 e5a0f69f 2018-08-18 stsp view_close(log_view);
5363 e5a0f69f 2018-08-18 stsp else
5364 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5365 cb2ebc8a 2018-06-23 stsp free(path);
5366 cb2ebc8a 2018-06-23 stsp return err;
5367 ffd1d5e5 2018-06-23 stsp }
5368 ffd1d5e5 2018-06-23 stsp
5369 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5370 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5371 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5372 ffd1d5e5 2018-06-23 stsp {
5373 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5374 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5375 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5376 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5377 ffd1d5e5 2018-06-23 stsp
5378 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5379 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5380 bc573f3b 2021-07-10 stsp
5381 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5382 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5383 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5384 ffd1d5e5 2018-06-23 stsp
5385 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5386 bc573f3b 2021-07-10 stsp if (err)
5387 bc573f3b 2021-07-10 stsp goto done;
5388 bc573f3b 2021-07-10 stsp
5389 bc573f3b 2021-07-10 stsp /*
5390 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5391 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5392 bc573f3b 2021-07-10 stsp * closed on demand.
5393 bc573f3b 2021-07-10 stsp */
5394 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5395 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5396 bc573f3b 2021-07-10 stsp if (err)
5397 bc573f3b 2021-07-10 stsp goto done;
5398 bc573f3b 2021-07-10 stsp s->tree = s->root;
5399 bc573f3b 2021-07-10 stsp
5400 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5401 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5402 ffd1d5e5 2018-06-23 stsp goto done;
5403 ffd1d5e5 2018-06-23 stsp
5404 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5405 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5406 ffd1d5e5 2018-06-23 stsp goto done;
5407 ffd1d5e5 2018-06-23 stsp }
5408 ffd1d5e5 2018-06-23 stsp
5409 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5410 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5411 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5412 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5413 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5414 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5415 9cd7cbd1 2020-12-07 stsp goto done;
5416 9cd7cbd1 2020-12-07 stsp }
5417 9cd7cbd1 2020-12-07 stsp }
5418 fb2756b9 2018-08-04 stsp s->repo = repo;
5419 c0b01bdb 2019-11-08 stsp
5420 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5421 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5422 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5423 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5424 c0b01bdb 2019-11-08 stsp if (err)
5425 c0b01bdb 2019-11-08 stsp goto done;
5426 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5427 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5428 bc573f3b 2021-07-10 stsp if (err)
5429 c0b01bdb 2019-11-08 stsp goto done;
5430 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5431 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5432 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5433 bc573f3b 2021-07-10 stsp if (err)
5434 c0b01bdb 2019-11-08 stsp goto done;
5435 e5a0f69f 2018-08-18 stsp
5436 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5437 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5438 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5439 bc573f3b 2021-07-10 stsp if (err)
5440 11b20872 2019-11-08 stsp goto done;
5441 11b20872 2019-11-08 stsp
5442 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5443 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5444 bc573f3b 2021-07-10 stsp if (err)
5445 c0b01bdb 2019-11-08 stsp goto done;
5446 c0b01bdb 2019-11-08 stsp }
5447 c0b01bdb 2019-11-08 stsp
5448 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5449 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5450 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5451 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5452 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5453 ad80ab7b 2018-08-04 stsp done:
5454 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5455 bc573f3b 2021-07-10 stsp if (commit)
5456 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5457 bc573f3b 2021-07-10 stsp if (err)
5458 bc573f3b 2021-07-10 stsp close_tree_view(view);
5459 ad80ab7b 2018-08-04 stsp return err;
5460 ad80ab7b 2018-08-04 stsp }
5461 ad80ab7b 2018-08-04 stsp
5462 e5a0f69f 2018-08-18 stsp static const struct got_error *
5463 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5464 ad80ab7b 2018-08-04 stsp {
5465 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5466 ad80ab7b 2018-08-04 stsp
5467 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5468 fb2756b9 2018-08-04 stsp free(s->tree_label);
5469 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5470 6484ec90 2018-09-29 stsp free(s->commit_id);
5471 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5472 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5473 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5474 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5475 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5476 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5477 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5478 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5479 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5480 ad80ab7b 2018-08-04 stsp free(parent);
5481 ad80ab7b 2018-08-04 stsp
5482 ad80ab7b 2018-08-04 stsp }
5483 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5484 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5485 bc573f3b 2021-07-10 stsp if (s->root)
5486 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5487 7c32bd05 2019-06-22 stsp return NULL;
5488 7c32bd05 2019-06-22 stsp }
5489 7c32bd05 2019-06-22 stsp
5490 7c32bd05 2019-06-22 stsp static const struct got_error *
5491 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5492 7c32bd05 2019-06-22 stsp {
5493 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5494 7c32bd05 2019-06-22 stsp
5495 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5496 7c32bd05 2019-06-22 stsp return NULL;
5497 7c32bd05 2019-06-22 stsp }
5498 7c32bd05 2019-06-22 stsp
5499 7c32bd05 2019-06-22 stsp static int
5500 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5501 7c32bd05 2019-06-22 stsp {
5502 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5503 7c32bd05 2019-06-22 stsp
5504 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5505 56e0773d 2019-11-28 stsp 0) == 0;
5506 7c32bd05 2019-06-22 stsp }
5507 7c32bd05 2019-06-22 stsp
5508 7c32bd05 2019-06-22 stsp static const struct got_error *
5509 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5510 7c32bd05 2019-06-22 stsp {
5511 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5512 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5513 7c32bd05 2019-06-22 stsp
5514 7c32bd05 2019-06-22 stsp if (!view->searching) {
5515 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5516 7c32bd05 2019-06-22 stsp return NULL;
5517 7c32bd05 2019-06-22 stsp }
5518 7c32bd05 2019-06-22 stsp
5519 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5520 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5521 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5522 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5523 56e0773d 2019-11-28 stsp s->selected_entry);
5524 7c32bd05 2019-06-22 stsp else
5525 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5526 56e0773d 2019-11-28 stsp } else {
5527 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5528 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5529 56e0773d 2019-11-28 stsp else
5530 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5531 56e0773d 2019-11-28 stsp s->selected_entry);
5532 7c32bd05 2019-06-22 stsp }
5533 7c32bd05 2019-06-22 stsp } else {
5534 487cd7d2 2021-12-17 stsp if (s->selected_entry)
5535 487cd7d2 2021-12-17 stsp te = s->selected_entry;
5536 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
5537 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5538 56e0773d 2019-11-28 stsp else
5539 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5540 7c32bd05 2019-06-22 stsp }
5541 7c32bd05 2019-06-22 stsp
5542 7c32bd05 2019-06-22 stsp while (1) {
5543 56e0773d 2019-11-28 stsp if (te == NULL) {
5544 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5545 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5546 ac66afb8 2019-06-24 stsp return NULL;
5547 ac66afb8 2019-06-24 stsp }
5548 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5549 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5550 56e0773d 2019-11-28 stsp else
5551 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5552 7c32bd05 2019-06-22 stsp }
5553 7c32bd05 2019-06-22 stsp
5554 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5555 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5556 56e0773d 2019-11-28 stsp s->matched_entry = te;
5557 7c32bd05 2019-06-22 stsp break;
5558 7c32bd05 2019-06-22 stsp }
5559 7c32bd05 2019-06-22 stsp
5560 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5561 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5562 56e0773d 2019-11-28 stsp else
5563 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5564 7c32bd05 2019-06-22 stsp }
5565 e5a0f69f 2018-08-18 stsp
5566 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5567 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5568 7c32bd05 2019-06-22 stsp s->selected = 0;
5569 7c32bd05 2019-06-22 stsp }
5570 7c32bd05 2019-06-22 stsp
5571 e5a0f69f 2018-08-18 stsp return NULL;
5572 ad80ab7b 2018-08-04 stsp }
5573 ad80ab7b 2018-08-04 stsp
5574 ad80ab7b 2018-08-04 stsp static const struct got_error *
5575 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5576 ad80ab7b 2018-08-04 stsp {
5577 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5578 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5579 e5a0f69f 2018-08-18 stsp char *parent_path;
5580 ad80ab7b 2018-08-04 stsp
5581 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5582 e5a0f69f 2018-08-18 stsp if (err)
5583 e5a0f69f 2018-08-18 stsp return err;
5584 ffd1d5e5 2018-06-23 stsp
5585 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5586 e5a0f69f 2018-08-18 stsp free(parent_path);
5587 669b5ffa 2018-10-07 stsp
5588 669b5ffa 2018-10-07 stsp view_vborder(view);
5589 e5a0f69f 2018-08-18 stsp return err;
5590 e5a0f69f 2018-08-18 stsp }
5591 ce52c690 2018-06-23 stsp
5592 e5a0f69f 2018-08-18 stsp static const struct got_error *
5593 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5594 e5a0f69f 2018-08-18 stsp {
5595 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5596 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5597 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5598 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5599 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
5600 ffd1d5e5 2018-06-23 stsp
5601 e5a0f69f 2018-08-18 stsp switch (ch) {
5602 1e37a5c2 2019-05-12 jcs case 'i':
5603 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5604 1e37a5c2 2019-05-12 jcs break;
5605 1e37a5c2 2019-05-12 jcs case 'l':
5606 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5607 ffd1d5e5 2018-06-23 stsp break;
5608 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5609 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5610 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5611 e78dc838 2020-12-04 stsp view->focussed = 0;
5612 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5613 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5614 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5615 1e37a5c2 2019-05-12 jcs if (err)
5616 1e37a5c2 2019-05-12 jcs return err;
5617 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5618 e78dc838 2020-12-04 stsp view->focus_child = 1;
5619 1e37a5c2 2019-05-12 jcs } else
5620 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5621 152c1c93 2020-11-29 stsp break;
5622 152c1c93 2020-11-29 stsp case 'r':
5623 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5624 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5625 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5626 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5627 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5628 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5629 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5630 152c1c93 2020-11-29 stsp if (err) {
5631 152c1c93 2020-11-29 stsp view_close(ref_view);
5632 152c1c93 2020-11-29 stsp return err;
5633 152c1c93 2020-11-29 stsp }
5634 e78dc838 2020-12-04 stsp view->focussed = 0;
5635 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5636 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5637 152c1c93 2020-11-29 stsp err = view_close_child(view);
5638 152c1c93 2020-11-29 stsp if (err)
5639 152c1c93 2020-11-29 stsp return err;
5640 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5641 e78dc838 2020-12-04 stsp view->focus_child = 1;
5642 152c1c93 2020-11-29 stsp } else
5643 152c1c93 2020-11-29 stsp *new_view = ref_view;
5644 e4526bf5 2021-09-03 naddy break;
5645 e4526bf5 2021-09-03 naddy case 'g':
5646 e4526bf5 2021-09-03 naddy case KEY_HOME:
5647 e4526bf5 2021-09-03 naddy s->selected = 0;
5648 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5649 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5650 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5651 e4526bf5 2021-09-03 naddy else
5652 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5653 1e37a5c2 2019-05-12 jcs break;
5654 e4526bf5 2021-09-03 naddy case 'G':
5655 e4526bf5 2021-09-03 naddy case KEY_END:
5656 e4526bf5 2021-09-03 naddy s->selected = 0;
5657 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5658 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5659 e4526bf5 2021-09-03 naddy if (te == NULL) {
5660 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5661 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5662 e4526bf5 2021-09-03 naddy n++;
5663 e4526bf5 2021-09-03 naddy }
5664 e4526bf5 2021-09-03 naddy break;
5665 e4526bf5 2021-09-03 naddy }
5666 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5667 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5668 e4526bf5 2021-09-03 naddy }
5669 e4526bf5 2021-09-03 naddy if (n > 0)
5670 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5671 e4526bf5 2021-09-03 naddy break;
5672 1e37a5c2 2019-05-12 jcs case 'k':
5673 1e37a5c2 2019-05-12 jcs case KEY_UP:
5674 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5675 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5676 1e37a5c2 2019-05-12 jcs s->selected--;
5677 fa86c4bf 2020-11-29 stsp break;
5678 1e37a5c2 2019-05-12 jcs }
5679 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5680 1e37a5c2 2019-05-12 jcs break;
5681 83cc4199 2022-06-13 stsp case CTRL('u'):
5682 33c3719a 2022-06-15 stsp case 'u':
5683 83cc4199 2022-06-13 stsp nscroll /= 2;
5684 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5685 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5686 ea025d1d 2020-02-22 naddy case CTRL('b'):
5687 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5688 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5689 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5690 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5691 fa86c4bf 2020-11-29 stsp } else {
5692 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5693 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
5694 fa86c4bf 2020-11-29 stsp }
5695 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
5696 1e37a5c2 2019-05-12 jcs break;
5697 1e37a5c2 2019-05-12 jcs case 'j':
5698 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5699 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5700 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5701 1e37a5c2 2019-05-12 jcs s->selected++;
5702 1e37a5c2 2019-05-12 jcs break;
5703 1e37a5c2 2019-05-12 jcs }
5704 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5705 56e0773d 2019-11-28 stsp == NULL)
5706 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5707 1e37a5c2 2019-05-12 jcs break;
5708 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5709 1e37a5c2 2019-05-12 jcs break;
5710 83cc4199 2022-06-13 stsp case CTRL('d'):
5711 33c3719a 2022-06-15 stsp case 'd':
5712 83cc4199 2022-06-13 stsp nscroll /= 2;
5713 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5714 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5715 ea025d1d 2020-02-22 naddy case CTRL('f'):
5716 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5717 1e37a5c2 2019-05-12 jcs == NULL) {
5718 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5719 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5720 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
5721 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
5722 1e37a5c2 2019-05-12 jcs break;
5723 1e37a5c2 2019-05-12 jcs }
5724 83cc4199 2022-06-13 stsp tree_scroll_down(s, nscroll);
5725 1e37a5c2 2019-05-12 jcs break;
5726 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5727 1e37a5c2 2019-05-12 jcs case '\r':
5728 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5729 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5730 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5731 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5732 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5733 1e37a5c2 2019-05-12 jcs break;
5734 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5735 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5736 1e37a5c2 2019-05-12 jcs entry);
5737 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5738 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5739 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5740 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5741 1e37a5c2 2019-05-12 jcs s->selected_entry =
5742 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5743 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5744 1e37a5c2 2019-05-12 jcs free(parent);
5745 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5746 56e0773d 2019-11-28 stsp s->selected_entry))) {
5747 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5748 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5749 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5750 1e37a5c2 2019-05-12 jcs if (err)
5751 1e37a5c2 2019-05-12 jcs break;
5752 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5753 941e9f74 2019-05-21 stsp if (err) {
5754 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5755 1e37a5c2 2019-05-12 jcs break;
5756 1e37a5c2 2019-05-12 jcs }
5757 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5758 56e0773d 2019-11-28 stsp s->selected_entry))) {
5759 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5760 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5761 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5762 1e37a5c2 2019-05-12 jcs
5763 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5764 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5765 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5766 1e37a5c2 2019-05-12 jcs if (err)
5767 1e37a5c2 2019-05-12 jcs break;
5768 e78dc838 2020-12-04 stsp view->focussed = 0;
5769 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5770 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5771 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5772 669b5ffa 2018-10-07 stsp if (err)
5773 669b5ffa 2018-10-07 stsp return err;
5774 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5775 e78dc838 2020-12-04 stsp view->focus_child = 1;
5776 669b5ffa 2018-10-07 stsp } else
5777 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5778 1e37a5c2 2019-05-12 jcs }
5779 1e37a5c2 2019-05-12 jcs break;
5780 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5781 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5782 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5783 1e37a5c2 2019-05-12 jcs break;
5784 1e37a5c2 2019-05-12 jcs default:
5785 1e37a5c2 2019-05-12 jcs break;
5786 ffd1d5e5 2018-06-23 stsp }
5787 e5a0f69f 2018-08-18 stsp
5788 ffd1d5e5 2018-06-23 stsp return err;
5789 9f7d7167 2018-04-29 stsp }
5790 9f7d7167 2018-04-29 stsp
5791 ffd1d5e5 2018-06-23 stsp __dead static void
5792 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5793 ffd1d5e5 2018-06-23 stsp {
5794 ffd1d5e5 2018-06-23 stsp endwin();
5795 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5796 ffd1d5e5 2018-06-23 stsp getprogname());
5797 ffd1d5e5 2018-06-23 stsp exit(1);
5798 ffd1d5e5 2018-06-23 stsp }
5799 ffd1d5e5 2018-06-23 stsp
5800 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5801 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5802 ffd1d5e5 2018-06-23 stsp {
5803 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5804 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5805 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5806 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5807 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5808 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5809 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5810 4e97c21c 2020-12-06 stsp char *label = NULL;
5811 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5812 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5813 ffd1d5e5 2018-06-23 stsp int ch;
5814 5221c383 2018-08-01 stsp struct tog_view *view;
5815 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5816 70ac5f84 2019-03-28 stsp
5817 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5818 ffd1d5e5 2018-06-23 stsp switch (ch) {
5819 ffd1d5e5 2018-06-23 stsp case 'c':
5820 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5821 74283ab8 2020-02-07 stsp break;
5822 74283ab8 2020-02-07 stsp case 'r':
5823 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5824 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5825 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5826 74283ab8 2020-02-07 stsp optarg);
5827 ffd1d5e5 2018-06-23 stsp break;
5828 ffd1d5e5 2018-06-23 stsp default:
5829 e99e2d15 2020-11-24 naddy usage_tree();
5830 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5831 ffd1d5e5 2018-06-23 stsp }
5832 ffd1d5e5 2018-06-23 stsp }
5833 ffd1d5e5 2018-06-23 stsp
5834 ffd1d5e5 2018-06-23 stsp argc -= optind;
5835 ffd1d5e5 2018-06-23 stsp argv += optind;
5836 ffd1d5e5 2018-06-23 stsp
5837 55cccc34 2020-02-20 stsp if (argc > 1)
5838 e99e2d15 2020-11-24 naddy usage_tree();
5839 0ae84acc 2022-06-15 tracey
5840 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5841 0ae84acc 2022-06-15 tracey if (error != NULL)
5842 0ae84acc 2022-06-15 tracey goto done;
5843 74283ab8 2020-02-07 stsp
5844 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5845 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5846 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5847 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5848 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5849 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5850 c156c7a4 2020-12-18 stsp goto done;
5851 55cccc34 2020-02-20 stsp if (worktree)
5852 52185f70 2019-02-05 stsp repo_path =
5853 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5854 55cccc34 2020-02-20 stsp else
5855 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5856 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5857 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5858 c156c7a4 2020-12-18 stsp goto done;
5859 c156c7a4 2020-12-18 stsp }
5860 55cccc34 2020-02-20 stsp }
5861 a915003a 2019-02-05 stsp
5862 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5863 c02c541e 2019-03-29 stsp if (error != NULL)
5864 52185f70 2019-02-05 stsp goto done;
5865 d188b9a6 2019-01-04 stsp
5866 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5867 55cccc34 2020-02-20 stsp repo, worktree);
5868 55cccc34 2020-02-20 stsp if (error)
5869 55cccc34 2020-02-20 stsp goto done;
5870 55cccc34 2020-02-20 stsp
5871 55cccc34 2020-02-20 stsp init_curses();
5872 55cccc34 2020-02-20 stsp
5873 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5874 c02c541e 2019-03-29 stsp if (error)
5875 52185f70 2019-02-05 stsp goto done;
5876 ffd1d5e5 2018-06-23 stsp
5877 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5878 51a10b52 2020-12-26 stsp if (error)
5879 51a10b52 2020-12-26 stsp goto done;
5880 51a10b52 2020-12-26 stsp
5881 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5882 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5883 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5884 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5885 4e97c21c 2020-12-06 stsp if (error)
5886 4e97c21c 2020-12-06 stsp goto done;
5887 4e97c21c 2020-12-06 stsp head_ref_name = label;
5888 4e97c21c 2020-12-06 stsp } else {
5889 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5890 4e97c21c 2020-12-06 stsp if (error == NULL)
5891 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5892 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5893 4e97c21c 2020-12-06 stsp goto done;
5894 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5895 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5896 4e97c21c 2020-12-06 stsp if (error)
5897 4e97c21c 2020-12-06 stsp goto done;
5898 4e97c21c 2020-12-06 stsp }
5899 ffd1d5e5 2018-06-23 stsp
5900 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5901 a44927cc 2022-04-07 stsp if (error)
5902 a44927cc 2022-04-07 stsp goto done;
5903 a44927cc 2022-04-07 stsp
5904 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5905 5221c383 2018-08-01 stsp if (view == NULL) {
5906 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5907 5221c383 2018-08-01 stsp goto done;
5908 5221c383 2018-08-01 stsp }
5909 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5910 ad80ab7b 2018-08-04 stsp if (error)
5911 ad80ab7b 2018-08-04 stsp goto done;
5912 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5913 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
5914 d91faf3b 2020-12-01 naddy in_repo_path);
5915 55cccc34 2020-02-20 stsp if (error)
5916 55cccc34 2020-02-20 stsp goto done;
5917 55cccc34 2020-02-20 stsp }
5918 55cccc34 2020-02-20 stsp
5919 55cccc34 2020-02-20 stsp if (worktree) {
5920 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5921 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5922 55cccc34 2020-02-20 stsp worktree = NULL;
5923 55cccc34 2020-02-20 stsp }
5924 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5925 ffd1d5e5 2018-06-23 stsp done:
5926 52185f70 2019-02-05 stsp free(repo_path);
5927 e4a0e26d 2020-02-20 stsp free(cwd);
5928 ffd1d5e5 2018-06-23 stsp free(commit_id);
5929 4e97c21c 2020-12-06 stsp free(label);
5930 486cd271 2020-12-06 stsp if (ref)
5931 486cd271 2020-12-06 stsp got_ref_close(ref);
5932 1d0f4054 2021-06-17 stsp if (repo) {
5933 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5934 1d0f4054 2021-06-17 stsp if (error == NULL)
5935 1d0f4054 2021-06-17 stsp error = close_err;
5936 1d0f4054 2021-06-17 stsp }
5937 0ae84acc 2022-06-15 tracey if (pack_fds) {
5938 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5939 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5940 0ae84acc 2022-06-15 tracey if (error == NULL)
5941 0ae84acc 2022-06-15 tracey error = pack_err;
5942 0ae84acc 2022-06-15 tracey }
5943 51a10b52 2020-12-26 stsp tog_free_refs();
5944 ffd1d5e5 2018-06-23 stsp return error;
5945 6458efa5 2020-11-24 stsp }
5946 6458efa5 2020-11-24 stsp
5947 6458efa5 2020-11-24 stsp static const struct got_error *
5948 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5949 6458efa5 2020-11-24 stsp {
5950 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5951 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5952 6458efa5 2020-11-24 stsp
5953 6458efa5 2020-11-24 stsp s->nrefs = 0;
5954 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5955 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
5956 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
5957 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
5958 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
5959 6458efa5 2020-11-24 stsp continue;
5960 6458efa5 2020-11-24 stsp
5961 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5962 6458efa5 2020-11-24 stsp if (re == NULL)
5963 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5964 6458efa5 2020-11-24 stsp
5965 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5966 8924d611 2020-12-26 stsp if (re->ref == NULL)
5967 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5968 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5969 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5970 6458efa5 2020-11-24 stsp }
5971 6458efa5 2020-11-24 stsp
5972 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5973 6458efa5 2020-11-24 stsp return NULL;
5974 6458efa5 2020-11-24 stsp }
5975 6458efa5 2020-11-24 stsp
5976 6458efa5 2020-11-24 stsp void
5977 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5978 6458efa5 2020-11-24 stsp {
5979 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5980 6458efa5 2020-11-24 stsp
5981 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5982 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5983 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5984 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5985 6458efa5 2020-11-24 stsp free(re);
5986 6458efa5 2020-11-24 stsp }
5987 6458efa5 2020-11-24 stsp }
5988 6458efa5 2020-11-24 stsp
5989 6458efa5 2020-11-24 stsp static const struct got_error *
5990 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5991 6458efa5 2020-11-24 stsp {
5992 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5993 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5994 6458efa5 2020-11-24 stsp
5995 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5996 6458efa5 2020-11-24 stsp s->repo = repo;
5997 6458efa5 2020-11-24 stsp
5998 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5999 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6000 6458efa5 2020-11-24 stsp
6001 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6002 6458efa5 2020-11-24 stsp if (err)
6003 6458efa5 2020-11-24 stsp return err;
6004 34ba6917 2020-11-30 stsp
6005 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6006 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6007 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6008 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6009 6458efa5 2020-11-24 stsp if (err)
6010 6458efa5 2020-11-24 stsp goto done;
6011 6458efa5 2020-11-24 stsp
6012 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6013 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6014 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6015 6458efa5 2020-11-24 stsp if (err)
6016 6458efa5 2020-11-24 stsp goto done;
6017 6458efa5 2020-11-24 stsp
6018 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6019 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6020 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6021 cc488aa7 2022-01-23 stsp if (err)
6022 cc488aa7 2022-01-23 stsp goto done;
6023 cc488aa7 2022-01-23 stsp
6024 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6025 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6026 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6027 6458efa5 2020-11-24 stsp if (err)
6028 6458efa5 2020-11-24 stsp goto done;
6029 6458efa5 2020-11-24 stsp }
6030 6458efa5 2020-11-24 stsp
6031 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6032 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6033 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6034 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6035 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6036 6458efa5 2020-11-24 stsp done:
6037 6458efa5 2020-11-24 stsp if (err)
6038 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6039 6458efa5 2020-11-24 stsp return err;
6040 6458efa5 2020-11-24 stsp }
6041 6458efa5 2020-11-24 stsp
6042 6458efa5 2020-11-24 stsp static const struct got_error *
6043 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6044 6458efa5 2020-11-24 stsp {
6045 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6046 6458efa5 2020-11-24 stsp
6047 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6048 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6049 6458efa5 2020-11-24 stsp
6050 6458efa5 2020-11-24 stsp return NULL;
6051 9f7d7167 2018-04-29 stsp }
6052 ce5b7c56 2019-07-09 stsp
6053 6458efa5 2020-11-24 stsp static const struct got_error *
6054 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6055 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6056 6458efa5 2020-11-24 stsp {
6057 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6058 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6059 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6060 6458efa5 2020-11-24 stsp int obj_type;
6061 6458efa5 2020-11-24 stsp
6062 c42c9805 2020-11-24 stsp *commit_id = NULL;
6063 6458efa5 2020-11-24 stsp
6064 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6065 6458efa5 2020-11-24 stsp if (err)
6066 6458efa5 2020-11-24 stsp return err;
6067 6458efa5 2020-11-24 stsp
6068 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6069 6458efa5 2020-11-24 stsp if (err)
6070 6458efa5 2020-11-24 stsp goto done;
6071 6458efa5 2020-11-24 stsp
6072 6458efa5 2020-11-24 stsp switch (obj_type) {
6073 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6074 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6075 6458efa5 2020-11-24 stsp break;
6076 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6077 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6078 6458efa5 2020-11-24 stsp if (err)
6079 6458efa5 2020-11-24 stsp goto done;
6080 c42c9805 2020-11-24 stsp free(obj_id);
6081 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6082 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6083 c42c9805 2020-11-24 stsp if (err)
6084 6458efa5 2020-11-24 stsp goto done;
6085 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6086 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6087 c42c9805 2020-11-24 stsp goto done;
6088 c42c9805 2020-11-24 stsp }
6089 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6090 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6091 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6092 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6093 c42c9805 2020-11-24 stsp goto done;
6094 c42c9805 2020-11-24 stsp }
6095 6458efa5 2020-11-24 stsp break;
6096 6458efa5 2020-11-24 stsp default:
6097 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6098 c42c9805 2020-11-24 stsp break;
6099 6458efa5 2020-11-24 stsp }
6100 6458efa5 2020-11-24 stsp
6101 c42c9805 2020-11-24 stsp done:
6102 c42c9805 2020-11-24 stsp if (tag)
6103 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6104 c42c9805 2020-11-24 stsp if (err) {
6105 c42c9805 2020-11-24 stsp free(*commit_id);
6106 c42c9805 2020-11-24 stsp *commit_id = NULL;
6107 c42c9805 2020-11-24 stsp }
6108 c42c9805 2020-11-24 stsp return err;
6109 c42c9805 2020-11-24 stsp }
6110 c42c9805 2020-11-24 stsp
6111 c42c9805 2020-11-24 stsp static const struct got_error *
6112 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6113 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6114 c42c9805 2020-11-24 stsp {
6115 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6116 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6117 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6118 c42c9805 2020-11-24 stsp
6119 c42c9805 2020-11-24 stsp *new_view = NULL;
6120 c42c9805 2020-11-24 stsp
6121 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6122 c42c9805 2020-11-24 stsp if (err) {
6123 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6124 c42c9805 2020-11-24 stsp return err;
6125 c42c9805 2020-11-24 stsp else
6126 c42c9805 2020-11-24 stsp return NULL;
6127 c42c9805 2020-11-24 stsp }
6128 c42c9805 2020-11-24 stsp
6129 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6130 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6131 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6132 6458efa5 2020-11-24 stsp goto done;
6133 6458efa5 2020-11-24 stsp }
6134 6458efa5 2020-11-24 stsp
6135 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6136 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6137 6458efa5 2020-11-24 stsp done:
6138 6458efa5 2020-11-24 stsp if (err)
6139 6458efa5 2020-11-24 stsp view_close(log_view);
6140 6458efa5 2020-11-24 stsp else
6141 6458efa5 2020-11-24 stsp *new_view = log_view;
6142 c42c9805 2020-11-24 stsp free(commit_id);
6143 6458efa5 2020-11-24 stsp return err;
6144 6458efa5 2020-11-24 stsp }
6145 6458efa5 2020-11-24 stsp
6146 ce5b7c56 2019-07-09 stsp static void
6147 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6148 6458efa5 2020-11-24 stsp {
6149 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6150 34ba6917 2020-11-30 stsp int i = 0;
6151 6458efa5 2020-11-24 stsp
6152 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6153 6458efa5 2020-11-24 stsp return;
6154 6458efa5 2020-11-24 stsp
6155 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6156 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6157 34ba6917 2020-11-30 stsp if (re == NULL)
6158 34ba6917 2020-11-30 stsp break;
6159 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6160 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6161 6458efa5 2020-11-24 stsp }
6162 6458efa5 2020-11-24 stsp }
6163 6458efa5 2020-11-24 stsp
6164 34ba6917 2020-11-30 stsp static void
6165 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6166 6458efa5 2020-11-24 stsp {
6167 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6168 6458efa5 2020-11-24 stsp int n = 0;
6169 6458efa5 2020-11-24 stsp
6170 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6171 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6172 6458efa5 2020-11-24 stsp else
6173 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6174 6458efa5 2020-11-24 stsp
6175 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6176 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6177 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6178 6458efa5 2020-11-24 stsp if (last) {
6179 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6180 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6181 6458efa5 2020-11-24 stsp }
6182 6458efa5 2020-11-24 stsp }
6183 6458efa5 2020-11-24 stsp }
6184 6458efa5 2020-11-24 stsp
6185 6458efa5 2020-11-24 stsp static const struct got_error *
6186 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6187 6458efa5 2020-11-24 stsp {
6188 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6189 6458efa5 2020-11-24 stsp
6190 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6191 6458efa5 2020-11-24 stsp return NULL;
6192 6458efa5 2020-11-24 stsp }
6193 6458efa5 2020-11-24 stsp
6194 6458efa5 2020-11-24 stsp static int
6195 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6196 6458efa5 2020-11-24 stsp {
6197 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6198 6458efa5 2020-11-24 stsp
6199 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6200 6458efa5 2020-11-24 stsp 0) == 0;
6201 6458efa5 2020-11-24 stsp }
6202 6458efa5 2020-11-24 stsp
6203 6458efa5 2020-11-24 stsp static const struct got_error *
6204 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6205 6458efa5 2020-11-24 stsp {
6206 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6207 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6208 6458efa5 2020-11-24 stsp
6209 6458efa5 2020-11-24 stsp if (!view->searching) {
6210 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6211 6458efa5 2020-11-24 stsp return NULL;
6212 6458efa5 2020-11-24 stsp }
6213 6458efa5 2020-11-24 stsp
6214 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6215 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6216 6458efa5 2020-11-24 stsp if (s->selected_entry)
6217 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6218 6458efa5 2020-11-24 stsp else
6219 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6220 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6221 6458efa5 2020-11-24 stsp } else {
6222 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6223 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6224 6458efa5 2020-11-24 stsp else
6225 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6226 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6227 6458efa5 2020-11-24 stsp }
6228 6458efa5 2020-11-24 stsp } else {
6229 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6230 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6231 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6232 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6233 6458efa5 2020-11-24 stsp else
6234 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6235 6458efa5 2020-11-24 stsp }
6236 6458efa5 2020-11-24 stsp
6237 6458efa5 2020-11-24 stsp while (1) {
6238 6458efa5 2020-11-24 stsp if (re == NULL) {
6239 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6240 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6241 6458efa5 2020-11-24 stsp return NULL;
6242 6458efa5 2020-11-24 stsp }
6243 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6244 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6245 6458efa5 2020-11-24 stsp else
6246 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6247 6458efa5 2020-11-24 stsp }
6248 6458efa5 2020-11-24 stsp
6249 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6250 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6251 6458efa5 2020-11-24 stsp s->matched_entry = re;
6252 6458efa5 2020-11-24 stsp break;
6253 6458efa5 2020-11-24 stsp }
6254 6458efa5 2020-11-24 stsp
6255 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6256 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6257 6458efa5 2020-11-24 stsp else
6258 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6259 6458efa5 2020-11-24 stsp }
6260 6458efa5 2020-11-24 stsp
6261 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6262 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6263 6458efa5 2020-11-24 stsp s->selected = 0;
6264 6458efa5 2020-11-24 stsp }
6265 6458efa5 2020-11-24 stsp
6266 6458efa5 2020-11-24 stsp return NULL;
6267 6458efa5 2020-11-24 stsp }
6268 6458efa5 2020-11-24 stsp
6269 6458efa5 2020-11-24 stsp static const struct got_error *
6270 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6271 6458efa5 2020-11-24 stsp {
6272 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6273 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6274 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6275 6458efa5 2020-11-24 stsp char *line = NULL;
6276 6458efa5 2020-11-24 stsp wchar_t *wline;
6277 6458efa5 2020-11-24 stsp struct tog_color *tc;
6278 6458efa5 2020-11-24 stsp int width, n;
6279 6458efa5 2020-11-24 stsp int limit = view->nlines;
6280 6458efa5 2020-11-24 stsp
6281 6458efa5 2020-11-24 stsp werase(view->window);
6282 6458efa5 2020-11-24 stsp
6283 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6284 6458efa5 2020-11-24 stsp
6285 6458efa5 2020-11-24 stsp if (limit == 0)
6286 6458efa5 2020-11-24 stsp return NULL;
6287 6458efa5 2020-11-24 stsp
6288 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6289 6458efa5 2020-11-24 stsp
6290 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6291 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6292 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6293 6458efa5 2020-11-24 stsp
6294 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6295 6458efa5 2020-11-24 stsp if (err) {
6296 6458efa5 2020-11-24 stsp free(line);
6297 6458efa5 2020-11-24 stsp return err;
6298 6458efa5 2020-11-24 stsp }
6299 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6300 6458efa5 2020-11-24 stsp wstandout(view->window);
6301 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6302 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6303 6458efa5 2020-11-24 stsp wstandend(view->window);
6304 6458efa5 2020-11-24 stsp free(wline);
6305 6458efa5 2020-11-24 stsp wline = NULL;
6306 6458efa5 2020-11-24 stsp free(line);
6307 6458efa5 2020-11-24 stsp line = NULL;
6308 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6309 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6310 6458efa5 2020-11-24 stsp if (--limit <= 0)
6311 6458efa5 2020-11-24 stsp return NULL;
6312 6458efa5 2020-11-24 stsp
6313 6458efa5 2020-11-24 stsp n = 0;
6314 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6315 6458efa5 2020-11-24 stsp char *line = NULL;
6316 6458efa5 2020-11-24 stsp
6317 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6318 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6319 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6320 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6321 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6322 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6323 6458efa5 2020-11-24 stsp struct got_object_id *id;
6324 6458efa5 2020-11-24 stsp char *id_str;
6325 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6326 6458efa5 2020-11-24 stsp if (err)
6327 6458efa5 2020-11-24 stsp return err;
6328 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6329 6458efa5 2020-11-24 stsp if (err) {
6330 6458efa5 2020-11-24 stsp free(id);
6331 6458efa5 2020-11-24 stsp return err;
6332 6458efa5 2020-11-24 stsp }
6333 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6334 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6335 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6336 6458efa5 2020-11-24 stsp free(id);
6337 6458efa5 2020-11-24 stsp free(id_str);
6338 6458efa5 2020-11-24 stsp return err;
6339 6458efa5 2020-11-24 stsp }
6340 6458efa5 2020-11-24 stsp free(id);
6341 6458efa5 2020-11-24 stsp free(id_str);
6342 6458efa5 2020-11-24 stsp } else {
6343 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6344 6458efa5 2020-11-24 stsp if (line == NULL)
6345 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6346 6458efa5 2020-11-24 stsp }
6347 6458efa5 2020-11-24 stsp
6348 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6349 6458efa5 2020-11-24 stsp if (err) {
6350 6458efa5 2020-11-24 stsp free(line);
6351 6458efa5 2020-11-24 stsp return err;
6352 6458efa5 2020-11-24 stsp }
6353 6458efa5 2020-11-24 stsp if (n == s->selected) {
6354 6458efa5 2020-11-24 stsp if (view->focussed)
6355 6458efa5 2020-11-24 stsp wstandout(view->window);
6356 6458efa5 2020-11-24 stsp s->selected_entry = re;
6357 6458efa5 2020-11-24 stsp }
6358 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6359 6458efa5 2020-11-24 stsp if (tc)
6360 6458efa5 2020-11-24 stsp wattr_on(view->window,
6361 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6362 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6363 6458efa5 2020-11-24 stsp if (tc)
6364 6458efa5 2020-11-24 stsp wattr_off(view->window,
6365 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6366 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6367 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6368 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6369 6458efa5 2020-11-24 stsp wstandend(view->window);
6370 6458efa5 2020-11-24 stsp free(line);
6371 6458efa5 2020-11-24 stsp free(wline);
6372 6458efa5 2020-11-24 stsp wline = NULL;
6373 6458efa5 2020-11-24 stsp n++;
6374 6458efa5 2020-11-24 stsp s->ndisplayed++;
6375 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6376 6458efa5 2020-11-24 stsp
6377 6458efa5 2020-11-24 stsp limit--;
6378 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6379 6458efa5 2020-11-24 stsp }
6380 6458efa5 2020-11-24 stsp
6381 6458efa5 2020-11-24 stsp view_vborder(view);
6382 6458efa5 2020-11-24 stsp return err;
6383 6458efa5 2020-11-24 stsp }
6384 6458efa5 2020-11-24 stsp
6385 6458efa5 2020-11-24 stsp static const struct got_error *
6386 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6387 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6388 c42c9805 2020-11-24 stsp {
6389 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6390 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6391 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6392 c42c9805 2020-11-24 stsp
6393 c42c9805 2020-11-24 stsp *new_view = NULL;
6394 c42c9805 2020-11-24 stsp
6395 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6396 c42c9805 2020-11-24 stsp if (err) {
6397 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6398 c42c9805 2020-11-24 stsp return err;
6399 c42c9805 2020-11-24 stsp else
6400 c42c9805 2020-11-24 stsp return NULL;
6401 c42c9805 2020-11-24 stsp }
6402 c42c9805 2020-11-24 stsp
6403 c42c9805 2020-11-24 stsp
6404 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6405 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6406 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6407 c42c9805 2020-11-24 stsp goto done;
6408 c42c9805 2020-11-24 stsp }
6409 c42c9805 2020-11-24 stsp
6410 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6411 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6412 c42c9805 2020-11-24 stsp if (err)
6413 c42c9805 2020-11-24 stsp goto done;
6414 c42c9805 2020-11-24 stsp
6415 c42c9805 2020-11-24 stsp *new_view = tree_view;
6416 c42c9805 2020-11-24 stsp done:
6417 c42c9805 2020-11-24 stsp free(commit_id);
6418 c42c9805 2020-11-24 stsp return err;
6419 c42c9805 2020-11-24 stsp }
6420 c42c9805 2020-11-24 stsp static const struct got_error *
6421 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6422 6458efa5 2020-11-24 stsp {
6423 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6424 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6425 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6426 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6427 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 1;
6428 6458efa5 2020-11-24 stsp
6429 6458efa5 2020-11-24 stsp switch (ch) {
6430 6458efa5 2020-11-24 stsp case 'i':
6431 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6432 7f66531d 2021-11-16 stsp break;
6433 07a065fe 2021-11-20 stsp case 'o':
6434 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
6435 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6436 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
6437 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
6438 50617b77 2021-11-20 stsp if (err)
6439 50617b77 2021-11-20 stsp break;
6440 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
6441 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
6442 50617b77 2021-11-20 stsp &tog_refs, s->repo);
6443 7f66531d 2021-11-16 stsp if (err)
6444 7f66531d 2021-11-16 stsp break;
6445 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
6446 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
6447 6458efa5 2020-11-24 stsp break;
6448 6458efa5 2020-11-24 stsp case KEY_ENTER:
6449 6458efa5 2020-11-24 stsp case '\r':
6450 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6451 6458efa5 2020-11-24 stsp break;
6452 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6453 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6454 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6455 6458efa5 2020-11-24 stsp s->repo);
6456 e78dc838 2020-12-04 stsp view->focussed = 0;
6457 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6458 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6459 6458efa5 2020-11-24 stsp err = view_close_child(view);
6460 6458efa5 2020-11-24 stsp if (err)
6461 6458efa5 2020-11-24 stsp return err;
6462 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6463 e78dc838 2020-12-04 stsp view->focus_child = 1;
6464 6458efa5 2020-11-24 stsp } else
6465 6458efa5 2020-11-24 stsp *new_view = log_view;
6466 6458efa5 2020-11-24 stsp break;
6467 c42c9805 2020-11-24 stsp case 't':
6468 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6469 c42c9805 2020-11-24 stsp break;
6470 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6471 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6472 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6473 c42c9805 2020-11-24 stsp s->repo);
6474 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6475 c42c9805 2020-11-24 stsp break;
6476 e78dc838 2020-12-04 stsp view->focussed = 0;
6477 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6478 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6479 c42c9805 2020-11-24 stsp err = view_close_child(view);
6480 c42c9805 2020-11-24 stsp if (err)
6481 c42c9805 2020-11-24 stsp return err;
6482 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6483 e78dc838 2020-12-04 stsp view->focus_child = 1;
6484 c42c9805 2020-11-24 stsp } else
6485 c42c9805 2020-11-24 stsp *new_view = tree_view;
6486 c42c9805 2020-11-24 stsp break;
6487 e4526bf5 2021-09-03 naddy case 'g':
6488 e4526bf5 2021-09-03 naddy case KEY_HOME:
6489 e4526bf5 2021-09-03 naddy s->selected = 0;
6490 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6491 e4526bf5 2021-09-03 naddy break;
6492 e4526bf5 2021-09-03 naddy case 'G':
6493 e4526bf5 2021-09-03 naddy case KEY_END:
6494 e4526bf5 2021-09-03 naddy s->selected = 0;
6495 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6496 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6497 e4526bf5 2021-09-03 naddy if (re == NULL)
6498 e4526bf5 2021-09-03 naddy break;
6499 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6500 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6501 e4526bf5 2021-09-03 naddy }
6502 e4526bf5 2021-09-03 naddy if (n > 0)
6503 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6504 e4526bf5 2021-09-03 naddy break;
6505 6458efa5 2020-11-24 stsp case 'k':
6506 6458efa5 2020-11-24 stsp case KEY_UP:
6507 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6508 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6509 6458efa5 2020-11-24 stsp s->selected--;
6510 6458efa5 2020-11-24 stsp break;
6511 34ba6917 2020-11-30 stsp }
6512 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6513 6458efa5 2020-11-24 stsp break;
6514 83cc4199 2022-06-13 stsp case CTRL('u'):
6515 33c3719a 2022-06-15 stsp case 'u':
6516 83cc4199 2022-06-13 stsp nscroll /= 2;
6517 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6518 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6519 6458efa5 2020-11-24 stsp case CTRL('b'):
6520 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6521 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
6522 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
6523 6458efa5 2020-11-24 stsp break;
6524 6458efa5 2020-11-24 stsp case 'j':
6525 6458efa5 2020-11-24 stsp case KEY_DOWN:
6526 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6527 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6528 6458efa5 2020-11-24 stsp s->selected++;
6529 6458efa5 2020-11-24 stsp break;
6530 6458efa5 2020-11-24 stsp }
6531 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6532 6458efa5 2020-11-24 stsp /* can't scroll any further */
6533 6458efa5 2020-11-24 stsp break;
6534 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6535 6458efa5 2020-11-24 stsp break;
6536 83cc4199 2022-06-13 stsp case CTRL('d'):
6537 33c3719a 2022-06-15 stsp case 'd':
6538 83cc4199 2022-06-13 stsp nscroll /= 2;
6539 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6540 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6541 6458efa5 2020-11-24 stsp case CTRL('f'):
6542 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6543 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6544 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6545 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6546 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6547 6458efa5 2020-11-24 stsp break;
6548 6458efa5 2020-11-24 stsp }
6549 83cc4199 2022-06-13 stsp ref_scroll_down(s, nscroll);
6550 6458efa5 2020-11-24 stsp break;
6551 6458efa5 2020-11-24 stsp case CTRL('l'):
6552 8924d611 2020-12-26 stsp tog_free_refs();
6553 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
6554 8924d611 2020-12-26 stsp if (err)
6555 8924d611 2020-12-26 stsp break;
6556 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6557 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6558 6458efa5 2020-11-24 stsp break;
6559 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6560 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6561 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6562 6458efa5 2020-11-24 stsp break;
6563 6458efa5 2020-11-24 stsp default:
6564 6458efa5 2020-11-24 stsp break;
6565 6458efa5 2020-11-24 stsp }
6566 6458efa5 2020-11-24 stsp
6567 6458efa5 2020-11-24 stsp return err;
6568 6458efa5 2020-11-24 stsp }
6569 6458efa5 2020-11-24 stsp
6570 6458efa5 2020-11-24 stsp __dead static void
6571 6458efa5 2020-11-24 stsp usage_ref(void)
6572 6458efa5 2020-11-24 stsp {
6573 6458efa5 2020-11-24 stsp endwin();
6574 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6575 6458efa5 2020-11-24 stsp getprogname());
6576 6458efa5 2020-11-24 stsp exit(1);
6577 6458efa5 2020-11-24 stsp }
6578 6458efa5 2020-11-24 stsp
6579 6458efa5 2020-11-24 stsp static const struct got_error *
6580 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6581 6458efa5 2020-11-24 stsp {
6582 6458efa5 2020-11-24 stsp const struct got_error *error;
6583 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6584 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6585 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6586 6458efa5 2020-11-24 stsp int ch;
6587 6458efa5 2020-11-24 stsp struct tog_view *view;
6588 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6589 6458efa5 2020-11-24 stsp
6590 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6591 6458efa5 2020-11-24 stsp switch (ch) {
6592 6458efa5 2020-11-24 stsp case 'r':
6593 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6594 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6595 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6596 6458efa5 2020-11-24 stsp optarg);
6597 6458efa5 2020-11-24 stsp break;
6598 6458efa5 2020-11-24 stsp default:
6599 e99e2d15 2020-11-24 naddy usage_ref();
6600 6458efa5 2020-11-24 stsp /* NOTREACHED */
6601 6458efa5 2020-11-24 stsp }
6602 6458efa5 2020-11-24 stsp }
6603 6458efa5 2020-11-24 stsp
6604 6458efa5 2020-11-24 stsp argc -= optind;
6605 6458efa5 2020-11-24 stsp argv += optind;
6606 6458efa5 2020-11-24 stsp
6607 6458efa5 2020-11-24 stsp if (argc > 1)
6608 e99e2d15 2020-11-24 naddy usage_ref();
6609 0ae84acc 2022-06-15 tracey
6610 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6611 0ae84acc 2022-06-15 tracey if (error != NULL)
6612 0ae84acc 2022-06-15 tracey goto done;
6613 6458efa5 2020-11-24 stsp
6614 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6615 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6616 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6617 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6618 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6619 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6620 c156c7a4 2020-12-18 stsp goto done;
6621 6458efa5 2020-11-24 stsp if (worktree)
6622 6458efa5 2020-11-24 stsp repo_path =
6623 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6624 6458efa5 2020-11-24 stsp else
6625 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6626 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6627 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6628 c156c7a4 2020-12-18 stsp goto done;
6629 c156c7a4 2020-12-18 stsp }
6630 6458efa5 2020-11-24 stsp }
6631 6458efa5 2020-11-24 stsp
6632 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6633 6458efa5 2020-11-24 stsp if (error != NULL)
6634 6458efa5 2020-11-24 stsp goto done;
6635 6458efa5 2020-11-24 stsp
6636 6458efa5 2020-11-24 stsp init_curses();
6637 6458efa5 2020-11-24 stsp
6638 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6639 51a10b52 2020-12-26 stsp if (error)
6640 51a10b52 2020-12-26 stsp goto done;
6641 51a10b52 2020-12-26 stsp
6642 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6643 6458efa5 2020-11-24 stsp if (error)
6644 6458efa5 2020-11-24 stsp goto done;
6645 6458efa5 2020-11-24 stsp
6646 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6647 6458efa5 2020-11-24 stsp if (view == NULL) {
6648 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6649 6458efa5 2020-11-24 stsp goto done;
6650 6458efa5 2020-11-24 stsp }
6651 6458efa5 2020-11-24 stsp
6652 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6653 6458efa5 2020-11-24 stsp if (error)
6654 6458efa5 2020-11-24 stsp goto done;
6655 6458efa5 2020-11-24 stsp
6656 6458efa5 2020-11-24 stsp if (worktree) {
6657 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6658 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6659 6458efa5 2020-11-24 stsp worktree = NULL;
6660 6458efa5 2020-11-24 stsp }
6661 6458efa5 2020-11-24 stsp error = view_loop(view);
6662 6458efa5 2020-11-24 stsp done:
6663 6458efa5 2020-11-24 stsp free(repo_path);
6664 6458efa5 2020-11-24 stsp free(cwd);
6665 1d0f4054 2021-06-17 stsp if (repo) {
6666 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6667 1d0f4054 2021-06-17 stsp if (close_err)
6668 1d0f4054 2021-06-17 stsp error = close_err;
6669 1d0f4054 2021-06-17 stsp }
6670 0ae84acc 2022-06-15 tracey if (pack_fds) {
6671 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6672 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6673 0ae84acc 2022-06-15 tracey if (error == NULL)
6674 0ae84acc 2022-06-15 tracey error = pack_err;
6675 0ae84acc 2022-06-15 tracey }
6676 51a10b52 2020-12-26 stsp tog_free_refs();
6677 6458efa5 2020-11-24 stsp return error;
6678 6458efa5 2020-11-24 stsp }
6679 6458efa5 2020-11-24 stsp
6680 6458efa5 2020-11-24 stsp static void
6681 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6682 ce5b7c56 2019-07-09 stsp {
6683 6059809a 2020-12-17 stsp size_t i;
6684 9f7d7167 2018-04-29 stsp
6685 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6686 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6687 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
6688 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6689 ce5b7c56 2019-07-09 stsp }
6690 6879ba42 2020-10-01 naddy fputc('\n', fp);
6691 ce5b7c56 2019-07-09 stsp }
6692 ce5b7c56 2019-07-09 stsp
6693 4ed7e80c 2018-05-20 stsp __dead static void
6694 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6695 9f7d7167 2018-04-29 stsp {
6696 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6697 6879ba42 2020-10-01 naddy
6698 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6699 6879ba42 2020-10-01 naddy getprogname());
6700 6879ba42 2020-10-01 naddy if (hflag) {
6701 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6702 6879ba42 2020-10-01 naddy list_commands(fp);
6703 ee85c5e8 2020-02-29 stsp }
6704 6879ba42 2020-10-01 naddy exit(status);
6705 9f7d7167 2018-04-29 stsp }
6706 9f7d7167 2018-04-29 stsp
6707 c2301be8 2018-04-30 stsp static char **
6708 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6709 c2301be8 2018-04-30 stsp {
6710 ee85c5e8 2020-02-29 stsp va_list ap;
6711 c2301be8 2018-04-30 stsp char **argv;
6712 ee85c5e8 2020-02-29 stsp int i;
6713 c2301be8 2018-04-30 stsp
6714 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6715 ee85c5e8 2020-02-29 stsp
6716 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6717 c2301be8 2018-04-30 stsp if (argv == NULL)
6718 c2301be8 2018-04-30 stsp err(1, "calloc");
6719 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6720 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6721 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6722 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6723 c2301be8 2018-04-30 stsp }
6724 c2301be8 2018-04-30 stsp
6725 ee85c5e8 2020-02-29 stsp va_end(ap);
6726 c2301be8 2018-04-30 stsp return argv;
6727 ee85c5e8 2020-02-29 stsp }
6728 ee85c5e8 2020-02-29 stsp
6729 ee85c5e8 2020-02-29 stsp /*
6730 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6731 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6732 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6733 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6734 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6735 ee85c5e8 2020-02-29 stsp */
6736 ee85c5e8 2020-02-29 stsp static const struct got_error *
6737 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6738 ee85c5e8 2020-02-29 stsp {
6739 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6740 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6741 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6742 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6743 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6744 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6745 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6746 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6747 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6748 84de9106 2020-12-26 stsp
6749 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6750 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6751 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6752 ee85c5e8 2020-02-29 stsp
6753 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6754 0ae84acc 2022-06-15 tracey if (error != NULL)
6755 0ae84acc 2022-06-15 tracey goto done;
6756 0ae84acc 2022-06-15 tracey
6757 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6758 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6759 ee85c5e8 2020-02-29 stsp goto done;
6760 ee85c5e8 2020-02-29 stsp
6761 ee85c5e8 2020-02-29 stsp if (worktree)
6762 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6763 ee85c5e8 2020-02-29 stsp else
6764 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6765 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6766 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6767 ee85c5e8 2020-02-29 stsp goto done;
6768 ee85c5e8 2020-02-29 stsp }
6769 ee85c5e8 2020-02-29 stsp
6770 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6771 ee85c5e8 2020-02-29 stsp if (error != NULL)
6772 ee85c5e8 2020-02-29 stsp goto done;
6773 ee85c5e8 2020-02-29 stsp
6774 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6775 ee85c5e8 2020-02-29 stsp repo, worktree);
6776 ee85c5e8 2020-02-29 stsp if (error)
6777 ee85c5e8 2020-02-29 stsp goto done;
6778 ee85c5e8 2020-02-29 stsp
6779 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6780 84de9106 2020-12-26 stsp if (error)
6781 84de9106 2020-12-26 stsp goto done;
6782 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6783 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6784 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6785 ee85c5e8 2020-02-29 stsp if (error)
6786 ee85c5e8 2020-02-29 stsp goto done;
6787 ee85c5e8 2020-02-29 stsp
6788 ee85c5e8 2020-02-29 stsp if (worktree) {
6789 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6790 ee85c5e8 2020-02-29 stsp worktree = NULL;
6791 ee85c5e8 2020-02-29 stsp }
6792 ee85c5e8 2020-02-29 stsp
6793 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6794 a44927cc 2022-04-07 stsp if (error)
6795 a44927cc 2022-04-07 stsp goto done;
6796 a44927cc 2022-04-07 stsp
6797 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6798 ee85c5e8 2020-02-29 stsp if (error) {
6799 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6800 ee85c5e8 2020-02-29 stsp goto done;
6801 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6802 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6803 6879ba42 2020-10-01 naddy usage(1, 1);
6804 ee85c5e8 2020-02-29 stsp /* not reached */
6805 ee85c5e8 2020-02-29 stsp }
6806 ee85c5e8 2020-02-29 stsp
6807 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6808 1d0f4054 2021-06-17 stsp if (error == NULL)
6809 1d0f4054 2021-06-17 stsp error = close_err;
6810 ee85c5e8 2020-02-29 stsp repo = NULL;
6811 ee85c5e8 2020-02-29 stsp
6812 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6813 ee85c5e8 2020-02-29 stsp if (error)
6814 ee85c5e8 2020-02-29 stsp goto done;
6815 ee85c5e8 2020-02-29 stsp
6816 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6817 ee85c5e8 2020-02-29 stsp argc = 4;
6818 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6819 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6820 ee85c5e8 2020-02-29 stsp done:
6821 1d0f4054 2021-06-17 stsp if (repo) {
6822 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6823 1d0f4054 2021-06-17 stsp if (error == NULL)
6824 1d0f4054 2021-06-17 stsp error = close_err;
6825 1d0f4054 2021-06-17 stsp }
6826 a44927cc 2022-04-07 stsp if (commit)
6827 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6828 ee85c5e8 2020-02-29 stsp if (worktree)
6829 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6830 0ae84acc 2022-06-15 tracey if (pack_fds) {
6831 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6832 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6833 0ae84acc 2022-06-15 tracey if (error == NULL)
6834 0ae84acc 2022-06-15 tracey error = pack_err;
6835 0ae84acc 2022-06-15 tracey }
6836 ee85c5e8 2020-02-29 stsp free(id);
6837 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6838 ee85c5e8 2020-02-29 stsp free(commit_id);
6839 ee85c5e8 2020-02-29 stsp free(cwd);
6840 ee85c5e8 2020-02-29 stsp free(repo_path);
6841 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6842 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6843 ee85c5e8 2020-02-29 stsp int i;
6844 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6845 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6846 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6847 ee85c5e8 2020-02-29 stsp }
6848 87670572 2020-12-26 naddy tog_free_refs();
6849 ee85c5e8 2020-02-29 stsp return error;
6850 c2301be8 2018-04-30 stsp }
6851 c2301be8 2018-04-30 stsp
6852 9f7d7167 2018-04-29 stsp int
6853 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6854 9f7d7167 2018-04-29 stsp {
6855 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6856 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
6857 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6858 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6859 3e166534 2022-02-16 naddy static const struct option longopts[] = {
6860 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6861 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6862 83cd27f8 2020-01-13 stsp };
6863 9f7d7167 2018-04-29 stsp
6864 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6865 9f7d7167 2018-04-29 stsp
6866 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6867 9f7d7167 2018-04-29 stsp switch (ch) {
6868 9f7d7167 2018-04-29 stsp case 'h':
6869 9f7d7167 2018-04-29 stsp hflag = 1;
6870 9f7d7167 2018-04-29 stsp break;
6871 53ccebc2 2019-07-30 stsp case 'V':
6872 53ccebc2 2019-07-30 stsp Vflag = 1;
6873 53ccebc2 2019-07-30 stsp break;
6874 9f7d7167 2018-04-29 stsp default:
6875 6879ba42 2020-10-01 naddy usage(hflag, 1);
6876 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6877 9f7d7167 2018-04-29 stsp }
6878 9f7d7167 2018-04-29 stsp }
6879 9f7d7167 2018-04-29 stsp
6880 9f7d7167 2018-04-29 stsp argc -= optind;
6881 9f7d7167 2018-04-29 stsp argv += optind;
6882 9814e6a3 2020-09-27 naddy optind = 1;
6883 c2301be8 2018-04-30 stsp optreset = 1;
6884 9f7d7167 2018-04-29 stsp
6885 53ccebc2 2019-07-30 stsp if (Vflag) {
6886 53ccebc2 2019-07-30 stsp got_version_print_str();
6887 6879ba42 2020-10-01 naddy return 0;
6888 53ccebc2 2019-07-30 stsp }
6889 4010e238 2020-12-04 stsp
6890 4010e238 2020-12-04 stsp #ifndef PROFILE
6891 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6892 4010e238 2020-12-04 stsp NULL) == -1)
6893 4010e238 2020-12-04 stsp err(1, "pledge");
6894 4010e238 2020-12-04 stsp #endif
6895 53ccebc2 2019-07-30 stsp
6896 c2301be8 2018-04-30 stsp if (argc == 0) {
6897 f29d3e89 2018-06-23 stsp if (hflag)
6898 6879ba42 2020-10-01 naddy usage(hflag, 0);
6899 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6900 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6901 c2301be8 2018-04-30 stsp argc = 1;
6902 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6903 c2301be8 2018-04-30 stsp } else {
6904 6059809a 2020-12-17 stsp size_t i;
6905 9f7d7167 2018-04-29 stsp
6906 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6907 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6908 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6909 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6910 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6911 9f7d7167 2018-04-29 stsp break;
6912 9f7d7167 2018-04-29 stsp }
6913 9f7d7167 2018-04-29 stsp }
6914 ee85c5e8 2020-02-29 stsp }
6915 3642c4c6 2019-07-09 stsp
6916 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6917 ee85c5e8 2020-02-29 stsp if (argc != 1)
6918 6879ba42 2020-10-01 naddy usage(0, 1);
6919 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6920 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6921 ee85c5e8 2020-02-29 stsp } else {
6922 ee85c5e8 2020-02-29 stsp if (hflag)
6923 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6924 ee85c5e8 2020-02-29 stsp else
6925 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6926 9f7d7167 2018-04-29 stsp }
6927 9f7d7167 2018-04-29 stsp
6928 9f7d7167 2018-04-29 stsp endwin();
6929 b46c1e04 2020-09-20 naddy putchar('\n');
6930 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6931 a2f4a359 2020-02-28 stsp int i;
6932 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6933 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6934 a2f4a359 2020-02-28 stsp free(cmd_argv);
6935 a2f4a359 2020-02-28 stsp }
6936 a2f4a359 2020-02-28 stsp
6937 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6938 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6939 9f7d7167 2018-04-29 stsp return 0;
6940 9f7d7167 2018-04-29 stsp }