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 ec2a9698 2022-09-15 mark TOG_VIEW_HELP
107 ec2a9698 2022-09-15 mark };
108 ec2a9698 2022-09-15 mark
109 ec2a9698 2022-09-15 mark /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
110 ec2a9698 2022-09-15 mark enum tog_keymap_type {
111 ec2a9698 2022-09-15 mark TOG_KEYMAP_KEYS = -2,
112 ec2a9698 2022-09-15 mark TOG_KEYMAP_GLOBAL,
113 ec2a9698 2022-09-15 mark TOG_KEYMAP_DIFF,
114 ec2a9698 2022-09-15 mark TOG_KEYMAP_LOG,
115 ec2a9698 2022-09-15 mark TOG_KEYMAP_BLAME,
116 ec2a9698 2022-09-15 mark TOG_KEYMAP_TREE,
117 ec2a9698 2022-09-15 mark TOG_KEYMAP_REF,
118 ec2a9698 2022-09-15 mark TOG_KEYMAP_HELP
119 d6b05b5b 2018-08-04 stsp };
120 c3e9aa98 2019-05-13 jcs
121 9b058f45 2022-06-30 mark enum tog_view_mode {
122 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
123 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
124 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
125 9b058f45 2022-06-30 mark };
126 9b058f45 2022-06-30 mark
127 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
128 9b058f45 2022-06-30 mark
129 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
130 d6b05b5b 2018-08-04 stsp
131 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
132 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
133 ba4f502b 2018-08-04 stsp struct got_object_id *id;
134 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
135 1a76625f 2018-10-22 stsp int idx;
136 ba4f502b 2018-08-04 stsp };
137 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
138 ba4f502b 2018-08-04 stsp struct commit_queue {
139 ba4f502b 2018-08-04 stsp int ncommits;
140 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
141 6d17833f 2019-11-08 stsp };
142 6d17833f 2019-11-08 stsp
143 f26dddb7 2019-11-08 stsp struct tog_color {
144 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
145 6d17833f 2019-11-08 stsp regex_t regex;
146 6d17833f 2019-11-08 stsp short colorpair;
147 15a087fe 2019-02-21 stsp };
148 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
149 11b20872 2019-11-08 stsp
150 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
151 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
152 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
153 cc488aa7 2022-01-23 stsp
154 cc488aa7 2022-01-23 stsp static const struct got_error *
155 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
156 cc488aa7 2022-01-23 stsp struct got_reference* re2)
157 cc488aa7 2022-01-23 stsp {
158 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
159 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
160 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
161 cc488aa7 2022-01-23 stsp
162 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
163 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
164 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
165 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
166 cc488aa7 2022-01-23 stsp *cmp = -1;
167 cc488aa7 2022-01-23 stsp return NULL;
168 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
169 cc488aa7 2022-01-23 stsp *cmp = 1;
170 cc488aa7 2022-01-23 stsp return NULL;
171 cc488aa7 2022-01-23 stsp }
172 cc488aa7 2022-01-23 stsp
173 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
174 cc488aa7 2022-01-23 stsp return NULL;
175 cc488aa7 2022-01-23 stsp }
176 51a10b52 2020-12-26 stsp
177 11b20872 2019-11-08 stsp static const struct got_error *
178 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
179 51a10b52 2020-12-26 stsp {
180 51a10b52 2020-12-26 stsp const struct got_error *err;
181 51a10b52 2020-12-26 stsp
182 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
183 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
184 7f66531d 2021-11-16 stsp repo);
185 51a10b52 2020-12-26 stsp if (err)
186 51a10b52 2020-12-26 stsp return err;
187 51a10b52 2020-12-26 stsp
188 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
189 51a10b52 2020-12-26 stsp repo);
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp
192 51a10b52 2020-12-26 stsp static void
193 51a10b52 2020-12-26 stsp tog_free_refs(void)
194 51a10b52 2020-12-26 stsp {
195 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
196 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
197 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
198 51a10b52 2020-12-26 stsp }
199 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
200 51a10b52 2020-12-26 stsp }
201 51a10b52 2020-12-26 stsp
202 51a10b52 2020-12-26 stsp static const struct got_error *
203 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
204 11b20872 2019-11-08 stsp int idx, short color)
205 11b20872 2019-11-08 stsp {
206 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
207 11b20872 2019-11-08 stsp struct tog_color *tc;
208 11b20872 2019-11-08 stsp int regerr = 0;
209 11b20872 2019-11-08 stsp
210 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
211 11b20872 2019-11-08 stsp return NULL;
212 11b20872 2019-11-08 stsp
213 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
214 11b20872 2019-11-08 stsp
215 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
216 11b20872 2019-11-08 stsp if (tc == NULL)
217 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
218 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
219 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
220 11b20872 2019-11-08 stsp if (regerr) {
221 11b20872 2019-11-08 stsp static char regerr_msg[512];
222 11b20872 2019-11-08 stsp static char err_msg[512];
223 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
224 11b20872 2019-11-08 stsp sizeof(regerr_msg));
225 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
226 11b20872 2019-11-08 stsp regerr_msg);
227 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
228 11b20872 2019-11-08 stsp free(tc);
229 11b20872 2019-11-08 stsp return err;
230 11b20872 2019-11-08 stsp }
231 11b20872 2019-11-08 stsp tc->colorpair = idx;
232 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
233 11b20872 2019-11-08 stsp return NULL;
234 11b20872 2019-11-08 stsp }
235 11b20872 2019-11-08 stsp
236 11b20872 2019-11-08 stsp static void
237 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
238 11b20872 2019-11-08 stsp {
239 11b20872 2019-11-08 stsp struct tog_color *tc;
240 11b20872 2019-11-08 stsp
241 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
242 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
243 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
244 11b20872 2019-11-08 stsp regfree(&tc->regex);
245 11b20872 2019-11-08 stsp free(tc);
246 11b20872 2019-11-08 stsp }
247 11b20872 2019-11-08 stsp }
248 11b20872 2019-11-08 stsp
249 336075a4 2022-06-25 op static struct tog_color *
250 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
251 11b20872 2019-11-08 stsp {
252 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
253 11b20872 2019-11-08 stsp
254 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
255 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
256 11b20872 2019-11-08 stsp return tc;
257 11b20872 2019-11-08 stsp }
258 11b20872 2019-11-08 stsp
259 11b20872 2019-11-08 stsp return NULL;
260 11b20872 2019-11-08 stsp }
261 11b20872 2019-11-08 stsp
262 11b20872 2019-11-08 stsp static int
263 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
264 11b20872 2019-11-08 stsp {
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
268 11b20872 2019-11-08 stsp return COLOR_CYAN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
270 11b20872 2019-11-08 stsp return COLOR_YELLOW;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
274 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
276 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
278 91b8c405 2020-01-25 stsp return COLOR_CYAN;
279 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
280 11b20872 2019-11-08 stsp return COLOR_GREEN;
281 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
282 11b20872 2019-11-08 stsp return COLOR_GREEN;
283 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
284 11b20872 2019-11-08 stsp return COLOR_CYAN;
285 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
286 11b20872 2019-11-08 stsp return COLOR_YELLOW;
287 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
288 6458efa5 2020-11-24 stsp return COLOR_GREEN;
289 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
290 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
291 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
292 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
293 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
294 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp return -1;
297 11b20872 2019-11-08 stsp }
298 11b20872 2019-11-08 stsp
299 11b20872 2019-11-08 stsp static int
300 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
301 11b20872 2019-11-08 stsp {
302 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp if (val == NULL)
305 11b20872 2019-11-08 stsp return default_color_value(envvar);
306 15a087fe 2019-02-21 stsp
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLACK;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
310 11b20872 2019-11-08 stsp return COLOR_RED;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
312 11b20872 2019-11-08 stsp return COLOR_GREEN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
314 11b20872 2019-11-08 stsp return COLOR_YELLOW;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
316 11b20872 2019-11-08 stsp return COLOR_BLUE;
317 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
318 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
319 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
320 11b20872 2019-11-08 stsp return COLOR_CYAN;
321 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
322 11b20872 2019-11-08 stsp return COLOR_WHITE;
323 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
324 11b20872 2019-11-08 stsp return -1;
325 11b20872 2019-11-08 stsp
326 11b20872 2019-11-08 stsp return default_color_value(envvar);
327 11b20872 2019-11-08 stsp }
328 11b20872 2019-11-08 stsp
329 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
330 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
331 3dbaef42 2020-11-24 stsp const char *label1, *label2;
332 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
333 f9d37699 2022-06-28 stsp int fd1, fd2;
334 c7d5c43c 2022-08-04 mark int lineno;
335 15a087fe 2019-02-21 stsp int first_displayed_line;
336 15a087fe 2019-02-21 stsp int last_displayed_line;
337 15a087fe 2019-02-21 stsp int eof;
338 15a087fe 2019-02-21 stsp int diff_context;
339 3dbaef42 2020-11-24 stsp int ignore_whitespace;
340 64453f7e 2020-11-21 stsp int force_text_diff;
341 15a087fe 2019-02-21 stsp struct got_repository *repo;
342 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
343 fe621944 2020-11-10 stsp size_t nlines;
344 f44b1f58 2020-02-02 tracey int matched_line;
345 f44b1f58 2020-02-02 tracey int selected_line;
346 15a087fe 2019-02-21 stsp
347 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
348 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
349 b01e7d3b 2018-08-04 stsp };
350 b01e7d3b 2018-08-04 stsp
351 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
352 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
353 1a76625f 2018-10-22 stsp
354 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
355 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
356 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
357 1a76625f 2018-10-22 stsp int commits_needed;
358 fb280deb 2021-08-30 stsp int load_all;
359 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
360 568eae95 2022-09-11 mark struct commit_queue *real_commits;
361 1a76625f 2018-10-22 stsp const char *in_repo_path;
362 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
363 1a76625f 2018-10-22 stsp struct got_repository *repo;
364 74467cc8 2022-06-15 stsp int *pack_fds;
365 1a76625f 2018-10-22 stsp int log_complete;
366 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
367 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
368 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
369 13add988 2019-10-15 stsp int *searching;
370 13add988 2019-10-15 stsp int *search_next_done;
371 13add988 2019-10-15 stsp regex_t *regex;
372 568eae95 2022-09-11 mark int *limiting;
373 568eae95 2022-09-11 mark int limit_match;
374 568eae95 2022-09-11 mark regex_t *limit_regex;
375 568eae95 2022-09-11 mark struct commit_queue *limit_commits;
376 1a76625f 2018-10-22 stsp };
377 1a76625f 2018-10-22 stsp
378 1a76625f 2018-10-22 stsp struct tog_log_view_state {
379 568eae95 2022-09-11 mark struct commit_queue *commits;
380 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
381 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
382 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
383 568eae95 2022-09-11 mark struct commit_queue real_commits;
384 b01e7d3b 2018-08-04 stsp int selected;
385 b01e7d3b 2018-08-04 stsp char *in_repo_path;
386 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
387 b672a97a 2020-01-27 stsp int log_branches;
388 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
389 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
390 1a76625f 2018-10-22 stsp sig_atomic_t quit;
391 1a76625f 2018-10-22 stsp pthread_t thread;
392 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
393 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
394 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
395 11b20872 2019-11-08 stsp struct tog_colors colors;
396 10aab77f 2022-07-19 op int use_committer;
397 568eae95 2022-09-11 mark int limit_view;
398 568eae95 2022-09-11 mark regex_t limit_regex;
399 568eae95 2022-09-11 mark struct commit_queue limit_commits;
400 ba4f502b 2018-08-04 stsp };
401 11b20872 2019-11-08 stsp
402 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
403 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
404 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
405 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
406 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
407 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
408 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
409 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
410 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
411 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
412 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
413 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
414 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
415 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
416 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
417 ba4f502b 2018-08-04 stsp
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
420 e9424729 2018-08-04 stsp int nlines;
421 e9424729 2018-08-04 stsp
422 e9424729 2018-08-04 stsp struct tog_view *view;
423 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
424 e9424729 2018-08-04 stsp int *quit;
425 e9424729 2018-08-04 stsp };
426 e9424729 2018-08-04 stsp
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
428 e9424729 2018-08-04 stsp const char *path;
429 e9424729 2018-08-04 stsp struct got_repository *repo;
430 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
431 e9424729 2018-08-04 stsp int *complete;
432 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
433 fc06ba56 2019-08-22 stsp void *cancel_arg;
434 e9424729 2018-08-04 stsp };
435 e9424729 2018-08-04 stsp
436 e9424729 2018-08-04 stsp struct tog_blame {
437 e9424729 2018-08-04 stsp FILE *f;
438 be659d10 2020-11-18 stsp off_t filesize;
439 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
440 6fcac457 2018-11-19 stsp int nlines;
441 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
442 e9424729 2018-08-04 stsp pthread_t thread;
443 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
444 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
445 e9424729 2018-08-04 stsp const char *path;
446 0ae84acc 2022-06-15 tracey int *pack_fds;
447 e9424729 2018-08-04 stsp };
448 e9424729 2018-08-04 stsp
449 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
450 7cbe629d 2018-08-04 stsp int first_displayed_line;
451 7cbe629d 2018-08-04 stsp int last_displayed_line;
452 7cbe629d 2018-08-04 stsp int selected_line;
453 c0f61fa4 2022-07-11 mark int last_diffed_line;
454 7cbe629d 2018-08-04 stsp int blame_complete;
455 e5a0f69f 2018-08-18 stsp int eof;
456 e5a0f69f 2018-08-18 stsp int done;
457 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
458 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
459 e5a0f69f 2018-08-18 stsp char *path;
460 7cbe629d 2018-08-04 stsp struct got_repository *repo;
461 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
462 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
463 e9424729 2018-08-04 stsp struct tog_blame blame;
464 6c4c42e0 2019-06-24 stsp int matched_line;
465 11b20872 2019-11-08 stsp struct tog_colors colors;
466 ad80ab7b 2018-08-04 stsp };
467 ad80ab7b 2018-08-04 stsp
468 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
469 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
470 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
471 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
472 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
473 ad80ab7b 2018-08-04 stsp int selected;
474 ad80ab7b 2018-08-04 stsp };
475 ad80ab7b 2018-08-04 stsp
476 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
477 ad80ab7b 2018-08-04 stsp
478 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
479 ad80ab7b 2018-08-04 stsp char *tree_label;
480 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
481 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
482 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
483 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
484 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
485 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
486 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
487 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
488 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
489 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
490 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
491 6458efa5 2020-11-24 stsp struct tog_colors colors;
492 6458efa5 2020-11-24 stsp };
493 6458efa5 2020-11-24 stsp
494 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
495 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
496 6458efa5 2020-11-24 stsp struct got_reference *ref;
497 6458efa5 2020-11-24 stsp int idx;
498 6458efa5 2020-11-24 stsp };
499 6458efa5 2020-11-24 stsp
500 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
501 6458efa5 2020-11-24 stsp
502 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
503 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
504 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
506 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
507 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
508 6458efa5 2020-11-24 stsp struct got_repository *repo;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
510 bddb1296 2019-11-08 stsp struct tog_colors colors;
511 ec2a9698 2022-09-15 mark };
512 ec2a9698 2022-09-15 mark
513 ec2a9698 2022-09-15 mark struct tog_help_view_state {
514 ec2a9698 2022-09-15 mark FILE *f;
515 ec2a9698 2022-09-15 mark off_t *line_offsets;
516 ec2a9698 2022-09-15 mark size_t nlines;
517 ec2a9698 2022-09-15 mark int lineno;
518 ec2a9698 2022-09-15 mark int first_displayed_line;
519 ec2a9698 2022-09-15 mark int last_displayed_line;
520 ec2a9698 2022-09-15 mark int eof;
521 ec2a9698 2022-09-15 mark int matched_line;
522 ec2a9698 2022-09-15 mark int selected_line;
523 ec2a9698 2022-09-15 mark int all;
524 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
525 ec2a9698 2022-09-15 mark };
526 ec2a9698 2022-09-15 mark
527 ec2a9698 2022-09-15 mark #define GENERATE_HELP \
528 ec2a9698 2022-09-15 mark KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
529 ec2a9698 2022-09-15 mark KEY_("H F1", "Open view-specific help (double tap for all help)"), \
530 ec2a9698 2022-09-15 mark KEY_("k C-p Up", "Move cursor or page up one line"), \
531 ec2a9698 2022-09-15 mark KEY_("j C-n Down", "Move cursor or page down one line"), \
532 ec2a9698 2022-09-15 mark KEY_("C-b b PgUp", "Scroll the view up one page"), \
533 ec2a9698 2022-09-15 mark KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
534 ec2a9698 2022-09-15 mark KEY_("C-u u", "Scroll the view up one half page"), \
535 ec2a9698 2022-09-15 mark KEY_("C-d d", "Scroll the view down one half page"), \
536 0b3f028d 2023-01-09 mark KEY_("g", "Go to line N (default: first line)"), \
537 0b3f028d 2023-01-09 mark KEY_("Home =", "Go to the first line"), \
538 0b3f028d 2023-01-09 mark KEY_("G", "Go to line N (default: last line)"), \
539 0b3f028d 2023-01-09 mark KEY_("End *", "Go to the last line"), \
540 ec2a9698 2022-09-15 mark KEY_("l Right", "Scroll the view right"), \
541 ec2a9698 2022-09-15 mark KEY_("h Left", "Scroll the view left"), \
542 ec2a9698 2022-09-15 mark KEY_("$", "Scroll view to the rightmost position"), \
543 ec2a9698 2022-09-15 mark KEY_("0", "Scroll view to the leftmost position"), \
544 ec2a9698 2022-09-15 mark KEY_("-", "Decrease size of the focussed split"), \
545 ec2a9698 2022-09-15 mark KEY_("+", "Increase size of the focussed split"), \
546 ec2a9698 2022-09-15 mark KEY_("Tab", "Switch focus between views"), \
547 ec2a9698 2022-09-15 mark KEY_("F", "Toggle fullscreen mode"), \
548 ec2a9698 2022-09-15 mark KEY_("/", "Open prompt to enter search term"), \
549 ec2a9698 2022-09-15 mark KEY_("n", "Find next line/token matching the current search term"), \
550 ec2a9698 2022-09-15 mark KEY_("N", "Find previous line/token matching the current search term"),\
551 16a2d4f0 2022-09-19 stsp KEY_("q", "Quit the focussed view; Quit help screen"), \
552 ec2a9698 2022-09-15 mark KEY_("Q", "Quit tog"), \
553 ec2a9698 2022-09-15 mark \
554 fd6badaa 2022-09-23 stsp KEYMAP_("Log view", TOG_KEYMAP_LOG), \
555 ec2a9698 2022-09-15 mark KEY_("< ,", "Move cursor up one commit"), \
556 ec2a9698 2022-09-15 mark KEY_("> .", "Move cursor down one commit"), \
557 ec2a9698 2022-09-15 mark KEY_("Enter", "Open diff view of the selected commit"), \
558 ec2a9698 2022-09-15 mark KEY_("B", "Reload the log view and toggle display of merged commits"), \
559 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
560 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the repository from the selected" \
561 ec2a9698 2022-09-15 mark " commit"), \
562 ec2a9698 2022-09-15 mark KEY_("@", "Toggle between displaying author and committer name"), \
563 ec2a9698 2022-09-15 mark KEY_("&", "Open prompt to enter term to limit commits displayed"), \
564 ec2a9698 2022-09-15 mark KEY_("C-g Backspace", "Cancel current search or log operation"), \
565 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload the log view with new commits in the repository"), \
566 ec2a9698 2022-09-15 mark \
567 fd6badaa 2022-09-23 stsp KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
568 ec2a9698 2022-09-15 mark KEY_("K < ,", "Display diff of next line in the file/log entry"), \
569 ec2a9698 2022-09-15 mark KEY_("J > .", "Display diff of previous line in the file/log entry"), \
570 ec2a9698 2022-09-15 mark KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
571 ec2a9698 2022-09-15 mark KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
572 ec2a9698 2022-09-15 mark " data"), \
573 ec2a9698 2022-09-15 mark KEY_("(", "Go to the previous file in the diff"), \
574 ec2a9698 2022-09-15 mark KEY_(")", "Go to the next file in the diff"), \
575 ec2a9698 2022-09-15 mark KEY_("{", "Go to the previous hunk in the diff"), \
576 ec2a9698 2022-09-15 mark KEY_("}", "Go to the next hunk in the diff"), \
577 ec2a9698 2022-09-15 mark KEY_("[", "Decrease the number of context lines"), \
578 ec2a9698 2022-09-15 mark KEY_("]", "Increase the number of context lines"), \
579 ec2a9698 2022-09-15 mark KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
580 ec2a9698 2022-09-15 mark \
581 fd6badaa 2022-09-23 stsp KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
582 ec2a9698 2022-09-15 mark KEY_("Enter", "Display diff view of the selected line's commit"), \
583 ec2a9698 2022-09-15 mark KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
584 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the currently selected annotated line"), \
585 ec2a9698 2022-09-15 mark KEY_("C", "Reload view with the previously blamed commit"), \
586 ec2a9698 2022-09-15 mark KEY_("c", "Reload view with the version of the file found in the" \
587 ec2a9698 2022-09-15 mark " selected line's commit"), \
588 ec2a9698 2022-09-15 mark KEY_("p", "Reload view with the version of the file found in the" \
589 ec2a9698 2022-09-15 mark " selected line's parent commit"), \
590 ec2a9698 2022-09-15 mark \
591 fd6badaa 2022-09-23 stsp KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
592 ec2a9698 2022-09-15 mark KEY_("Enter", "Enter selected directory or open blame view of the" \
593 ec2a9698 2022-09-15 mark " selected file"), \
594 ec2a9698 2022-09-15 mark KEY_("L", "Open log view for the selected entry"), \
595 ec2a9698 2022-09-15 mark KEY_("R", "Open ref view of all repository references"), \
596 ec2a9698 2022-09-15 mark KEY_("i", "Show object IDs for all tree entries"), \
597 ec2a9698 2022-09-15 mark KEY_("Backspace", "Return to the parent directory"), \
598 ec2a9698 2022-09-15 mark \
599 fd6badaa 2022-09-23 stsp KEYMAP_("Ref view", TOG_KEYMAP_REF), \
600 ec2a9698 2022-09-15 mark KEY_("Enter", "Display log view of the selected reference"), \
601 ec2a9698 2022-09-15 mark KEY_("T", "Display tree view of the selected reference"), \
602 ec2a9698 2022-09-15 mark KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
603 ec2a9698 2022-09-15 mark KEY_("m", "Toggle display of last modified date for each reference"), \
604 ec2a9698 2022-09-15 mark KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
605 ec2a9698 2022-09-15 mark KEY_("C-l", "Reload view with all repository references")
606 ec2a9698 2022-09-15 mark
607 ec2a9698 2022-09-15 mark struct tog_key_map {
608 ec2a9698 2022-09-15 mark const char *keys;
609 ec2a9698 2022-09-15 mark const char *info;
610 ec2a9698 2022-09-15 mark enum tog_keymap_type type;
611 7cbe629d 2018-08-04 stsp };
612 7cbe629d 2018-08-04 stsp
613 669b5ffa 2018-10-07 stsp /*
614 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
615 669b5ffa 2018-10-07 stsp *
616 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
617 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
618 669b5ffa 2018-10-07 stsp * there is enough screen estate.
619 669b5ffa 2018-10-07 stsp *
620 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
621 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
622 669b5ffa 2018-10-07 stsp *
623 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
624 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
625 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
626 669b5ffa 2018-10-07 stsp *
627 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
628 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
629 669b5ffa 2018-10-07 stsp */
630 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
631 669b5ffa 2018-10-07 stsp
632 cc3c9aac 2018-08-01 stsp struct tog_view {
633 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
634 26ed57b2 2018-05-19 stsp WINDOW *window;
635 26ed57b2 2018-05-19 stsp PANEL *panel;
636 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
637 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
638 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
639 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
640 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
641 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
642 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
643 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
644 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
645 9970f7fc 2020-12-03 stsp int dying;
646 669b5ffa 2018-10-07 stsp struct tog_view *parent;
647 669b5ffa 2018-10-07 stsp struct tog_view *child;
648 5dc9f4bc 2018-08-04 stsp
649 e78dc838 2020-12-04 stsp /*
650 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
651 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
652 e78dc838 2020-12-04 stsp * between parent and child.
653 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
654 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
655 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
656 e78dc838 2020-12-04 stsp * situations.
657 e78dc838 2020-12-04 stsp */
658 e78dc838 2020-12-04 stsp int focus_child;
659 e78dc838 2020-12-04 stsp
660 9b058f45 2022-06-30 mark enum tog_view_mode mode;
661 5dc9f4bc 2018-08-04 stsp /* type-specific state */
662 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
663 5dc9f4bc 2018-08-04 stsp union {
664 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
665 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
666 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
667 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
668 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
669 ec2a9698 2022-09-15 mark struct tog_help_view_state help;
670 5dc9f4bc 2018-08-04 stsp } state;
671 e5a0f69f 2018-08-18 stsp
672 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
673 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
674 e78dc838 2020-12-04 stsp struct tog_view *, int);
675 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
676 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
677 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
678 60493ae3 2019-06-20 stsp
679 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
680 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
681 eaf2c13e 2022-09-17 mark void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
682 eaf2c13e 2022-09-17 mark int **, int **, int **, int **);
683 c0c4acc8 2021-01-24 stsp int search_started;
684 60493ae3 2019-06-20 stsp int searching;
685 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
686 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
687 60493ae3 2019-06-20 stsp int search_next_done;
688 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
689 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
690 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
691 1803e47f 2019-06-22 stsp regex_t regex;
692 41605754 2020-11-12 stsp regmatch_t regmatch;
693 cc3c9aac 2018-08-01 stsp };
694 cd0acaa7 2018-05-20 stsp
695 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
696 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
697 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
698 78756c87 2020-11-24 stsp struct got_repository *);
699 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
700 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
701 e78dc838 2020-12-04 stsp struct tog_view *, int);
702 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
703 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
704 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
705 eaf2c13e 2022-09-17 mark static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
706 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
707 ec2a9698 2022-09-15 mark static const struct got_error *search_next_view_match(struct tog_view *);
708 e5a0f69f 2018-08-18 stsp
709 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
710 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
711 78756c87 2020-11-24 stsp const char *, const char *, int);
712 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
713 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
714 e78dc838 2020-12-04 stsp struct tog_view *, int);
715 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
716 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
717 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
718 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
719 e5a0f69f 2018-08-18 stsp
720 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
721 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
722 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
723 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
724 e78dc838 2020-12-04 stsp struct tog_view *, int);
725 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
726 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
727 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
728 eaf2c13e 2022-09-17 mark static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
729 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
730 e5a0f69f 2018-08-18 stsp
731 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
732 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
733 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
734 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
735 e78dc838 2020-12-04 stsp struct tog_view *, int);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
737 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
738 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
739 6458efa5 2020-11-24 stsp
740 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
741 6458efa5 2020-11-24 stsp struct got_repository *);
742 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
743 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
744 e78dc838 2020-12-04 stsp struct tog_view *, int);
745 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
746 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
747 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
748 eaf2c13e 2022-09-17 mark
749 eaf2c13e 2022-09-17 mark static const struct got_error *open_help_view(struct tog_view *,
750 eaf2c13e 2022-09-17 mark struct tog_view *);
751 eaf2c13e 2022-09-17 mark static const struct got_error *show_help_view(struct tog_view *);
752 eaf2c13e 2022-09-17 mark static const struct got_error *input_help_view(struct tog_view **,
753 eaf2c13e 2022-09-17 mark struct tog_view *, int);
754 eaf2c13e 2022-09-17 mark static const struct got_error *reset_help_view(struct tog_view *);
755 eaf2c13e 2022-09-17 mark static const struct got_error* close_help_view(struct tog_view *);
756 eaf2c13e 2022-09-17 mark static const struct got_error *search_start_help_view(struct tog_view *);
757 eaf2c13e 2022-09-17 mark static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
758 eaf2c13e 2022-09-17 mark size_t *, int **, int **, int **, int **);
759 25791caa 2018-10-24 stsp
760 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
761 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
762 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
763 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
764 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
765 25791caa 2018-10-24 stsp
766 25791caa 2018-10-24 stsp static void
767 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
768 25791caa 2018-10-24 stsp {
769 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
770 83baff54 2019-08-12 stsp }
771 83baff54 2019-08-12 stsp
772 83baff54 2019-08-12 stsp static void
773 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
774 83baff54 2019-08-12 stsp {
775 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
776 25791caa 2018-10-24 stsp }
777 26ed57b2 2018-05-19 stsp
778 61266923 2020-01-14 stsp static void
779 61266923 2020-01-14 stsp tog_sigcont(int signo)
780 61266923 2020-01-14 stsp {
781 61266923 2020-01-14 stsp tog_sigcont_received = 1;
782 61266923 2020-01-14 stsp }
783 61266923 2020-01-14 stsp
784 2497f032 2022-05-31 stsp static void
785 2497f032 2022-05-31 stsp tog_sigint(int signo)
786 2497f032 2022-05-31 stsp {
787 2497f032 2022-05-31 stsp tog_sigint_received = 1;
788 2497f032 2022-05-31 stsp }
789 2497f032 2022-05-31 stsp
790 2497f032 2022-05-31 stsp static void
791 2497f032 2022-05-31 stsp tog_sigterm(int signo)
792 2497f032 2022-05-31 stsp {
793 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
794 2497f032 2022-05-31 stsp }
795 2497f032 2022-05-31 stsp
796 2497f032 2022-05-31 stsp static int
797 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
798 2497f032 2022-05-31 stsp {
799 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
800 f044c841 2022-10-24 stsp tog_sigint_received || tog_sigterm_received);
801 2497f032 2022-05-31 stsp }
802 2497f032 2022-05-31 stsp
803 e5a0f69f 2018-08-18 stsp static const struct got_error *
804 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
805 ea5e7bb5 2018-08-01 stsp {
806 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
807 e5a0f69f 2018-08-18 stsp
808 669b5ffa 2018-10-07 stsp if (view->child) {
809 5629093a 2022-07-11 stsp child_err = view_close(view->child);
810 669b5ffa 2018-10-07 stsp view->child = NULL;
811 669b5ffa 2018-10-07 stsp }
812 e5a0f69f 2018-08-18 stsp if (view->close)
813 e5a0f69f 2018-08-18 stsp err = view->close(view);
814 ea5e7bb5 2018-08-01 stsp if (view->panel)
815 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
816 ea5e7bb5 2018-08-01 stsp if (view->window)
817 ea5e7bb5 2018-08-01 stsp delwin(view->window);
818 ea5e7bb5 2018-08-01 stsp free(view);
819 5629093a 2022-07-11 stsp return err ? err : child_err;
820 ea5e7bb5 2018-08-01 stsp }
821 ea5e7bb5 2018-08-01 stsp
822 ea5e7bb5 2018-08-01 stsp static struct tog_view *
823 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
824 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
825 ea5e7bb5 2018-08-01 stsp {
826 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
827 ea5e7bb5 2018-08-01 stsp
828 ea5e7bb5 2018-08-01 stsp if (view == NULL)
829 ea5e7bb5 2018-08-01 stsp return NULL;
830 ea5e7bb5 2018-08-01 stsp
831 d6b05b5b 2018-08-04 stsp view->type = type;
832 f7d12f7e 2018-08-01 stsp view->lines = LINES;
833 f7d12f7e 2018-08-01 stsp view->cols = COLS;
834 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
835 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
836 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
837 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
838 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
839 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
840 96a765a8 2018-08-04 stsp view_close(view);
841 ea5e7bb5 2018-08-01 stsp return NULL;
842 ea5e7bb5 2018-08-01 stsp }
843 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
844 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
845 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
846 96a765a8 2018-08-04 stsp view_close(view);
847 ea5e7bb5 2018-08-01 stsp return NULL;
848 ea5e7bb5 2018-08-01 stsp }
849 ea5e7bb5 2018-08-01 stsp
850 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
851 ea5e7bb5 2018-08-01 stsp return view;
852 cdf1ee82 2018-08-01 stsp }
853 cdf1ee82 2018-08-01 stsp
854 0cf4efb1 2018-09-29 stsp static int
855 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
856 0cf4efb1 2018-09-29 stsp {
857 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
858 0cf4efb1 2018-09-29 stsp return 0;
859 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
860 5c60c32a 2018-10-18 stsp }
861 5c60c32a 2018-10-18 stsp
862 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
863 9b058f45 2022-06-30 mark static int
864 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
865 9b058f45 2022-06-30 mark {
866 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
867 9b058f45 2022-06-30 mark }
868 9b058f45 2022-06-30 mark
869 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
870 5c60c32a 2018-10-18 stsp
871 5c60c32a 2018-10-18 stsp static const struct got_error *
872 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
873 5c60c32a 2018-10-18 stsp {
874 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
875 5c60c32a 2018-10-18 stsp
876 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
877 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
878 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
879 3c1dfe12 2022-07-08 mark else
880 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
881 9b058f45 2022-06-30 mark view->begin_x = 0;
882 571ccd73 2022-07-19 mark } else if (!view->resized) {
883 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
884 3c1dfe12 2022-07-08 mark view->cols > 119)
885 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
886 3c1dfe12 2022-07-08 mark else
887 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
888 9b058f45 2022-06-30 mark view->begin_y = 0;
889 9b058f45 2022-06-30 mark }
890 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
891 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
892 5c60c32a 2018-10-18 stsp view->lines = LINES;
893 5c60c32a 2018-10-18 stsp view->cols = COLS;
894 5c60c32a 2018-10-18 stsp err = view_resize(view);
895 5c60c32a 2018-10-18 stsp if (err)
896 5c60c32a 2018-10-18 stsp return err;
897 5c60c32a 2018-10-18 stsp
898 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
899 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
900 9b058f45 2022-06-30 mark
901 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
902 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
903 5c60c32a 2018-10-18 stsp
904 5c60c32a 2018-10-18 stsp return NULL;
905 5c60c32a 2018-10-18 stsp }
906 5c60c32a 2018-10-18 stsp
907 5c60c32a 2018-10-18 stsp static const struct got_error *
908 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
909 5c60c32a 2018-10-18 stsp {
910 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
911 5c60c32a 2018-10-18 stsp
912 5c60c32a 2018-10-18 stsp view->begin_x = 0;
913 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
914 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
915 5c60c32a 2018-10-18 stsp view->ncols = COLS;
916 5c60c32a 2018-10-18 stsp view->lines = LINES;
917 5c60c32a 2018-10-18 stsp view->cols = COLS;
918 5c60c32a 2018-10-18 stsp err = view_resize(view);
919 5c60c32a 2018-10-18 stsp if (err)
920 5c60c32a 2018-10-18 stsp return err;
921 5c60c32a 2018-10-18 stsp
922 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
923 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
924 5c60c32a 2018-10-18 stsp
925 5c60c32a 2018-10-18 stsp return NULL;
926 0cf4efb1 2018-09-29 stsp }
927 0cf4efb1 2018-09-29 stsp
928 5c60c32a 2018-10-18 stsp static int
929 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
930 5c60c32a 2018-10-18 stsp {
931 5c60c32a 2018-10-18 stsp return view->parent == NULL;
932 5c60c32a 2018-10-18 stsp }
933 5c60c32a 2018-10-18 stsp
934 6131ff18 2022-06-20 mark static int
935 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
936 6131ff18 2022-06-20 mark {
937 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
938 24b9cfdc 2022-06-30 stsp }
939 24b9cfdc 2022-06-30 stsp
940 24b9cfdc 2022-06-30 stsp static int
941 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
942 24b9cfdc 2022-06-30 stsp {
943 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
944 6131ff18 2022-06-20 mark }
945 6131ff18 2022-06-20 mark
946 49b24ee5 2022-07-03 mark static int
947 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
948 49b24ee5 2022-07-03 mark {
949 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
950 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
951 49b24ee5 2022-07-03 mark }
952 49b24ee5 2022-07-03 mark
953 9b058f45 2022-06-30 mark static void
954 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
955 9b058f45 2022-06-30 mark {
956 9b058f45 2022-06-30 mark PANEL *panel;
957 9b058f45 2022-06-30 mark const struct tog_view *view_above;
958 6131ff18 2022-06-20 mark
959 9b058f45 2022-06-30 mark if (view->parent)
960 9b058f45 2022-06-30 mark return view_border(view->parent);
961 9b058f45 2022-06-30 mark
962 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
963 9b058f45 2022-06-30 mark if (panel == NULL)
964 9b058f45 2022-06-30 mark return;
965 9b058f45 2022-06-30 mark
966 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
967 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
968 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
969 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
970 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
971 9b058f45 2022-06-30 mark else
972 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
973 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
974 9b058f45 2022-06-30 mark }
975 9b058f45 2022-06-30 mark
976 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
977 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
978 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
979 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
980 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
981 9b058f45 2022-06-30 mark
982 4d8c2215 2018-08-19 stsp static const struct got_error *
983 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
984 f7d12f7e 2018-08-01 stsp {
985 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
986 9b058f45 2022-06-30 mark int dif, nlines, ncols;
987 f7d12f7e 2018-08-01 stsp
988 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
989 9b058f45 2022-06-30 mark
990 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
991 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
992 0cf4efb1 2018-09-29 stsp else
993 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
994 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
995 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
996 0cf4efb1 2018-09-29 stsp else
997 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
998 6d0fee91 2018-08-01 stsp
999 4dd27a72 2022-06-29 stsp if (view->child) {
1000 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
1001 9b058f45 2022-06-30 mark
1002 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
1003 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
1004 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1005 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
1006 0dbbbe90 2022-06-17 op ncols = COLS;
1007 0dbbbe90 2022-06-17 op
1008 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1009 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1010 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1011 5c60c32a 2018-10-18 stsp else
1012 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1013 5c60c32a 2018-10-18 stsp } else {
1014 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
1015 0dbbbe90 2022-06-17 op
1016 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1017 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1018 9b058f45 2022-06-30 mark }
1019 9b058f45 2022-06-30 mark /*
1020 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
1021 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
1022 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
1023 9b058f45 2022-06-30 mark */
1024 9b058f45 2022-06-30 mark if (hs) {
1025 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
1026 9b058f45 2022-06-30 mark if (err)
1027 9b058f45 2022-06-30 mark return err;
1028 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
1029 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1030 9b058f45 2022-06-30 mark if (err)
1031 9b058f45 2022-06-30 mark return err;
1032 9b058f45 2022-06-30 mark }
1033 9b058f45 2022-06-30 mark view_border(view);
1034 9b058f45 2022-06-30 mark update_panels();
1035 9b058f45 2022-06-30 mark doupdate();
1036 9b058f45 2022-06-30 mark show_panel(view->child->panel);
1037 9b058f45 2022-06-30 mark nlines = view->nlines;
1038 9b058f45 2022-06-30 mark }
1039 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
1040 0dbbbe90 2022-06-17 op ncols = COLS;
1041 669b5ffa 2018-10-07 stsp
1042 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
1043 571ccd73 2022-07-19 mark err = view->resize(view, dif);
1044 571ccd73 2022-07-19 mark if (err)
1045 571ccd73 2022-07-19 mark return err;
1046 571ccd73 2022-07-19 mark }
1047 571ccd73 2022-07-19 mark
1048 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
1049 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
1050 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
1051 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
1052 0dbbbe90 2022-06-17 op wclear(view->window);
1053 0dbbbe90 2022-06-17 op
1054 0dbbbe90 2022-06-17 op view->nlines = nlines;
1055 0dbbbe90 2022-06-17 op view->ncols = ncols;
1056 0dbbbe90 2022-06-17 op view->lines = LINES;
1057 0dbbbe90 2022-06-17 op view->cols = COLS;
1058 0dbbbe90 2022-06-17 op
1059 5c60c32a 2018-10-18 stsp return NULL;
1060 571ccd73 2022-07-19 mark }
1061 571ccd73 2022-07-19 mark
1062 571ccd73 2022-07-19 mark static const struct got_error *
1063 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
1064 571ccd73 2022-07-19 mark {
1065 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
1066 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
1067 6fe51fee 2022-07-22 mark int n = 0;
1068 571ccd73 2022-07-19 mark
1069 6fe51fee 2022-07-22 mark if (s->selected_entry)
1070 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
1071 6fe51fee 2022-07-22 mark
1072 571ccd73 2022-07-19 mark /*
1073 571ccd73 2022-07-19 mark * Request commits to account for the increased
1074 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
1075 571ccd73 2022-07-19 mark */
1076 568eae95 2022-09-11 mark if (s->commits->ncommits < n) {
1077 568eae95 2022-09-11 mark view->nscrolled = n - s->commits->ncommits + increase + 1;
1078 571ccd73 2022-07-19 mark err = request_log_commits(view);
1079 571ccd73 2022-07-19 mark }
1080 571ccd73 2022-07-19 mark
1081 571ccd73 2022-07-19 mark return err;
1082 d9a7ab53 2022-07-11 mark }
1083 d9a7ab53 2022-07-11 mark
1084 d9a7ab53 2022-07-11 mark static void
1085 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
1086 d9a7ab53 2022-07-11 mark {
1087 d9a7ab53 2022-07-11 mark if (n == 0)
1088 d9a7ab53 2022-07-11 mark return;
1089 d9a7ab53 2022-07-11 mark
1090 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
1091 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
1092 d9a7ab53 2022-07-11 mark view->parent->offset += n;
1093 d9a7ab53 2022-07-11 mark else
1094 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
1095 d9a7ab53 2022-07-11 mark } else if (view->offset) {
1096 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
1097 d9a7ab53 2022-07-11 mark view->offset -= n;
1098 d9a7ab53 2022-07-11 mark else
1099 d9a7ab53 2022-07-11 mark view->offset = 0;
1100 d9a7ab53 2022-07-11 mark }
1101 3c1dfe12 2022-07-08 mark }
1102 3c1dfe12 2022-07-08 mark
1103 3c1dfe12 2022-07-08 mark static const struct got_error *
1104 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
1105 3c1dfe12 2022-07-08 mark {
1106 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1107 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
1108 3c1dfe12 2022-07-08 mark
1109 3c1dfe12 2022-07-08 mark if (view->parent)
1110 3c1dfe12 2022-07-08 mark v = view->parent;
1111 3c1dfe12 2022-07-08 mark else
1112 3c1dfe12 2022-07-08 mark v = view;
1113 3c1dfe12 2022-07-08 mark
1114 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
1115 3c1dfe12 2022-07-08 mark return NULL;
1116 3c1dfe12 2022-07-08 mark
1117 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
1118 3c1dfe12 2022-07-08 mark
1119 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1120 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
1121 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1122 3c1dfe12 2022-07-08 mark if (view->parent)
1123 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
1124 3c1dfe12 2022-07-08 mark else
1125 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
1126 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
1127 3c1dfe12 2022-07-08 mark view->count = 0;
1128 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
1129 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
1130 3c1dfe12 2022-07-08 mark view->count = 0;
1131 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
1132 3c1dfe12 2022-07-08 mark }
1133 3c1dfe12 2022-07-08 mark v->ncols = COLS;
1134 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
1135 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
1136 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1137 3c1dfe12 2022-07-08 mark if (err)
1138 3c1dfe12 2022-07-08 mark return err;
1139 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1140 3c1dfe12 2022-07-08 mark } else {
1141 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1142 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1143 3c1dfe12 2022-07-08 mark if (view->parent)
1144 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1145 3c1dfe12 2022-07-08 mark else
1146 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1147 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1148 3c1dfe12 2022-07-08 mark view->count = 0;
1149 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1150 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1151 3c1dfe12 2022-07-08 mark view->count = 0;
1152 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1153 3c1dfe12 2022-07-08 mark }
1154 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1155 3c1dfe12 2022-07-08 mark }
1156 3c1dfe12 2022-07-08 mark
1157 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1158 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1159 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1160 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1161 3c1dfe12 2022-07-08 mark
1162 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1163 3c1dfe12 2022-07-08 mark if (err)
1164 3c1dfe12 2022-07-08 mark return err;
1165 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1166 3c1dfe12 2022-07-08 mark if (err)
1167 3c1dfe12 2022-07-08 mark return err;
1168 3c1dfe12 2022-07-08 mark
1169 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1170 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1171 3c1dfe12 2022-07-08 mark if (err)
1172 3c1dfe12 2022-07-08 mark return err;
1173 3c1dfe12 2022-07-08 mark }
1174 3c1dfe12 2022-07-08 mark
1175 6fe51fee 2022-07-22 mark if (v->resize)
1176 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1177 6fe51fee 2022-07-22 mark else if (v->child->resize)
1178 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1179 3c1dfe12 2022-07-08 mark
1180 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1181 3c1dfe12 2022-07-08 mark
1182 3c1dfe12 2022-07-08 mark return err;
1183 3c1dfe12 2022-07-08 mark }
1184 3c1dfe12 2022-07-08 mark
1185 3c1dfe12 2022-07-08 mark static void
1186 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1187 3c1dfe12 2022-07-08 mark {
1188 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1189 3c1dfe12 2022-07-08 mark
1190 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1191 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1192 669b5ffa 2018-10-07 stsp }
1193 669b5ffa 2018-10-07 stsp
1194 669b5ffa 2018-10-07 stsp static const struct got_error *
1195 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1196 669b5ffa 2018-10-07 stsp {
1197 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1198 669b5ffa 2018-10-07 stsp
1199 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1200 669b5ffa 2018-10-07 stsp return NULL;
1201 669b5ffa 2018-10-07 stsp
1202 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1203 669b5ffa 2018-10-07 stsp view->child = NULL;
1204 669b5ffa 2018-10-07 stsp return err;
1205 669b5ffa 2018-10-07 stsp }
1206 669b5ffa 2018-10-07 stsp
1207 0dbbbe90 2022-06-17 op static const struct got_error *
1208 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1209 669b5ffa 2018-10-07 stsp {
1210 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1211 3c1dfe12 2022-07-08 mark
1212 669b5ffa 2018-10-07 stsp view->child = child;
1213 669b5ffa 2018-10-07 stsp child->parent = view;
1214 0dbbbe90 2022-06-17 op
1215 3c1dfe12 2022-07-08 mark err = view_resize(view);
1216 3c1dfe12 2022-07-08 mark if (err)
1217 3c1dfe12 2022-07-08 mark return err;
1218 3c1dfe12 2022-07-08 mark
1219 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1220 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1221 3c1dfe12 2022-07-08 mark
1222 3c1dfe12 2022-07-08 mark return err;
1223 bfddd0d9 2018-09-29 stsp }
1224 136e2bd4 2022-07-23 mark
1225 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1226 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1227 136e2bd4 2022-07-23 mark
1228 136e2bd4 2022-07-23 mark static const struct got_error *
1229 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1230 136e2bd4 2022-07-23 mark enum tog_view_type request)
1231 136e2bd4 2022-07-23 mark {
1232 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1233 136e2bd4 2022-07-23 mark const struct got_error *err;
1234 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1235 136e2bd4 2022-07-23 mark
1236 136e2bd4 2022-07-23 mark *requested = NULL;
1237 136e2bd4 2022-07-23 mark
1238 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1239 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1240 bfddd0d9 2018-09-29 stsp
1241 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1242 136e2bd4 2022-07-23 mark if (err)
1243 136e2bd4 2022-07-23 mark return err;
1244 136e2bd4 2022-07-23 mark
1245 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1246 94274a8f 2022-09-19 mark request != TOG_VIEW_HELP) {
1247 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1248 136e2bd4 2022-07-23 mark if (err)
1249 136e2bd4 2022-07-23 mark return err;
1250 136e2bd4 2022-07-23 mark }
1251 136e2bd4 2022-07-23 mark
1252 136e2bd4 2022-07-23 mark view->focussed = 0;
1253 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1254 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1255 94274a8f 2022-09-19 mark new_view->nlines = request == TOG_VIEW_HELP ?
1256 94274a8f 2022-09-19 mark view->lines : view->lines - y;
1257 136e2bd4 2022-07-23 mark
1258 94274a8f 2022-09-19 mark if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1259 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1260 136e2bd4 2022-07-23 mark err = view_close_child(view);
1261 136e2bd4 2022-07-23 mark if (err)
1262 136e2bd4 2022-07-23 mark return err;
1263 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1264 136e2bd4 2022-07-23 mark if (err)
1265 136e2bd4 2022-07-23 mark return err;
1266 136e2bd4 2022-07-23 mark view->focus_child = 1;
1267 136e2bd4 2022-07-23 mark } else
1268 136e2bd4 2022-07-23 mark *requested = new_view;
1269 136e2bd4 2022-07-23 mark
1270 136e2bd4 2022-07-23 mark return NULL;
1271 136e2bd4 2022-07-23 mark }
1272 136e2bd4 2022-07-23 mark
1273 34bc9ec9 2019-02-22 stsp static void
1274 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1275 25791caa 2018-10-24 stsp {
1276 25791caa 2018-10-24 stsp int cols, lines;
1277 25791caa 2018-10-24 stsp struct winsize size;
1278 25791caa 2018-10-24 stsp
1279 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1280 25791caa 2018-10-24 stsp cols = 80; /* Default */
1281 25791caa 2018-10-24 stsp lines = 24;
1282 25791caa 2018-10-24 stsp } else {
1283 25791caa 2018-10-24 stsp cols = size.ws_col;
1284 25791caa 2018-10-24 stsp lines = size.ws_row;
1285 25791caa 2018-10-24 stsp }
1286 25791caa 2018-10-24 stsp resize_term(lines, cols);
1287 2b49a8ae 2019-06-22 stsp }
1288 2b49a8ae 2019-06-22 stsp
1289 2b49a8ae 2019-06-22 stsp static const struct got_error *
1290 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1291 2b49a8ae 2019-06-22 stsp {
1292 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1293 9b058f45 2022-06-30 mark struct tog_view *v = view;
1294 2b49a8ae 2019-06-22 stsp char pattern[1024];
1295 2b49a8ae 2019-06-22 stsp int ret;
1296 c0c4acc8 2021-01-24 stsp
1297 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1298 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1299 c0c4acc8 2021-01-24 stsp view->searching = 0;
1300 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1301 c0c4acc8 2021-01-24 stsp }
1302 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1303 2b49a8ae 2019-06-22 stsp
1304 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1305 2b49a8ae 2019-06-22 stsp return NULL;
1306 2b49a8ae 2019-06-22 stsp
1307 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1308 9b058f45 2022-06-30 mark v = view->child;
1309 9d0feb8b 2022-12-27 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1310 9d0feb8b 2022-12-27 mark v = view->parent;
1311 2b49a8ae 2019-06-22 stsp
1312 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1313 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1314 9b058f45 2022-06-30 mark
1315 092a9f9c 2022-12-27 mark nodelay(v->window, FALSE); /* block for search term input */
1316 2b49a8ae 2019-06-22 stsp nocbreak();
1317 2b49a8ae 2019-06-22 stsp echo();
1318 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1319 9b058f45 2022-06-30 mark wrefresh(v->window);
1320 2b49a8ae 2019-06-22 stsp cbreak();
1321 2b49a8ae 2019-06-22 stsp noecho();
1322 092a9f9c 2022-12-27 mark nodelay(v->window, TRUE);
1323 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1324 2b49a8ae 2019-06-22 stsp return NULL;
1325 2b49a8ae 2019-06-22 stsp
1326 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1327 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1328 7c32bd05 2019-06-22 stsp if (err) {
1329 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1330 7c32bd05 2019-06-22 stsp return err;
1331 7c32bd05 2019-06-22 stsp }
1332 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1333 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1334 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1335 2b49a8ae 2019-06-22 stsp view->search_next(view);
1336 2b49a8ae 2019-06-22 stsp }
1337 2b49a8ae 2019-06-22 stsp
1338 2b49a8ae 2019-06-22 stsp return NULL;
1339 d2366e29 2022-07-07 mark }
1340 d2366e29 2022-07-07 mark
1341 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1342 d2366e29 2022-07-07 mark static const struct got_error *
1343 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1344 d2366e29 2022-07-07 mark {
1345 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1346 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1347 d2366e29 2022-07-07 mark
1348 d2366e29 2022-07-07 mark if (view->parent)
1349 d2366e29 2022-07-07 mark v = view->parent;
1350 d2366e29 2022-07-07 mark else
1351 d2366e29 2022-07-07 mark v = view;
1352 d2366e29 2022-07-07 mark
1353 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1354 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1355 7532ccda 2022-07-11 mark else
1356 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1357 d2366e29 2022-07-07 mark
1358 7532ccda 2022-07-11 mark if (!v->child)
1359 7532ccda 2022-07-11 mark return NULL;
1360 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1361 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1362 7532ccda 2022-07-11 mark
1363 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1364 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1365 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1366 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1367 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1368 d2366e29 2022-07-07 mark
1369 7532ccda 2022-07-11 mark
1370 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1371 d2366e29 2022-07-07 mark v->ncols = COLS;
1372 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1373 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1374 d2366e29 2022-07-07 mark
1375 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1376 d2366e29 2022-07-07 mark if (err)
1377 d2366e29 2022-07-07 mark return err;
1378 d2366e29 2022-07-07 mark }
1379 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1380 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1381 d2366e29 2022-07-07 mark v->focus_child = 1;
1382 d2366e29 2022-07-07 mark
1383 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1384 d2366e29 2022-07-07 mark if (err)
1385 d2366e29 2022-07-07 mark return err;
1386 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1387 d2366e29 2022-07-07 mark if (err)
1388 d2366e29 2022-07-07 mark return err;
1389 d2366e29 2022-07-07 mark
1390 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1391 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1392 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1393 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1394 279d2047 2022-07-29 stsp if (err)
1395 279d2047 2022-07-29 stsp return err;
1396 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1397 279d2047 2022-07-29 stsp if (err)
1398 279d2047 2022-07-29 stsp return err;
1399 7532ccda 2022-07-11 mark } else {
1400 7532ccda 2022-07-11 mark offset_selection_up(v);
1401 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1402 d2366e29 2022-07-07 mark }
1403 dff91825 2022-07-22 mark if (v->resize)
1404 dff91825 2022-07-22 mark err = v->resize(v, 0);
1405 dff91825 2022-07-22 mark else if (v->child->resize)
1406 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1407 d2366e29 2022-07-07 mark
1408 d2366e29 2022-07-07 mark return err;
1409 0cf4efb1 2018-09-29 stsp }
1410 6d0fee91 2018-08-01 stsp
1411 640cd7ff 2022-06-22 mark /*
1412 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1413 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1414 640cd7ff 2022-06-22 mark */
1415 640cd7ff 2022-06-22 mark static int
1416 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1417 640cd7ff 2022-06-22 mark {
1418 9b058f45 2022-06-30 mark struct tog_view *v = view;
1419 9b058f45 2022-06-30 mark int x, n = 0;
1420 640cd7ff 2022-06-22 mark
1421 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1422 9b058f45 2022-06-30 mark v = view->child;
1423 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1424 9b058f45 2022-06-30 mark v = view->parent;
1425 9b058f45 2022-06-30 mark
1426 640cd7ff 2022-06-22 mark view->count = 0;
1427 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1428 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1429 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1430 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1431 9b058f45 2022-06-30 mark waddch(v->window, ':');
1432 640cd7ff 2022-06-22 mark
1433 640cd7ff 2022-06-22 mark do {
1434 9b058f45 2022-06-30 mark x = getcurx(v->window);
1435 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1436 9b058f45 2022-06-30 mark waddch(v->window, c);
1437 9b058f45 2022-06-30 mark wrefresh(v->window);
1438 9b058f45 2022-06-30 mark }
1439 9b058f45 2022-06-30 mark
1440 640cd7ff 2022-06-22 mark /*
1441 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1442 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1443 640cd7ff 2022-06-22 mark */
1444 640cd7ff 2022-06-22 mark if (n >= 9999999)
1445 640cd7ff 2022-06-22 mark n = 9999999;
1446 640cd7ff 2022-06-22 mark else
1447 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1448 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1449 640cd7ff 2022-06-22 mark
1450 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1451 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1452 94b80cfa 2022-08-01 mark n = 0;
1453 94b80cfa 2022-08-01 mark c = 0;
1454 94b80cfa 2022-08-01 mark }
1455 94b80cfa 2022-08-01 mark
1456 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1457 640cd7ff 2022-06-22 mark view->count = n;
1458 640cd7ff 2022-06-22 mark
1459 640cd7ff 2022-06-22 mark return c;
1460 640cd7ff 2022-06-22 mark }
1461 640cd7ff 2022-06-22 mark
1462 0cf4efb1 2018-09-29 stsp static const struct got_error *
1463 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1464 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1465 e5a0f69f 2018-08-18 stsp {
1466 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1467 669b5ffa 2018-10-07 stsp struct tog_view *v;
1468 1a76625f 2018-10-22 stsp int ch, errcode;
1469 e5a0f69f 2018-08-18 stsp
1470 e5a0f69f 2018-08-18 stsp *new = NULL;
1471 8f4ed634 2020-03-26 stsp
1472 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1473 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1474 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1475 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1476 640cd7ff 2022-06-22 mark view->count = 0;
1477 640cd7ff 2022-06-22 mark }
1478 e5a0f69f 2018-08-18 stsp
1479 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1480 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1481 82954512 2020-02-03 stsp if (errcode)
1482 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1483 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1484 3da8ef85 2021-09-21 stsp sched_yield();
1485 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1486 82954512 2020-02-03 stsp if (errcode)
1487 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1488 82954512 2020-02-03 stsp "pthread_mutex_lock");
1489 60493ae3 2019-06-20 stsp view->search_next(view);
1490 60493ae3 2019-06-20 stsp return NULL;
1491 60493ae3 2019-06-20 stsp }
1492 60493ae3 2019-06-20 stsp
1493 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1494 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1495 1a76625f 2018-10-22 stsp if (errcode)
1496 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1497 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1498 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1499 a6d37fac 2022-07-03 mark cbreak();
1500 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1501 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1502 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1503 a6d37fac 2022-07-03 mark view->count = 0;
1504 a6d37fac 2022-07-03 mark else
1505 a6d37fac 2022-07-03 mark ch = view->ch;
1506 a6d37fac 2022-07-03 mark } else {
1507 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1508 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1509 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1510 640cd7ff 2022-06-22 mark }
1511 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1512 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1513 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1514 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1515 1a76625f 2018-10-22 stsp if (errcode)
1516 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1517 25791caa 2018-10-24 stsp
1518 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1519 25791caa 2018-10-24 stsp tog_resizeterm();
1520 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1521 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1522 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1523 25791caa 2018-10-24 stsp err = view_resize(v);
1524 25791caa 2018-10-24 stsp if (err)
1525 25791caa 2018-10-24 stsp return err;
1526 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1527 25791caa 2018-10-24 stsp if (err)
1528 25791caa 2018-10-24 stsp return err;
1529 cdfcfb03 2020-12-06 stsp if (v->child) {
1530 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1531 cdfcfb03 2020-12-06 stsp if (err)
1532 cdfcfb03 2020-12-06 stsp return err;
1533 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1534 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1535 cdfcfb03 2020-12-06 stsp if (err)
1536 cdfcfb03 2020-12-06 stsp return err;
1537 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1538 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1539 3c1dfe12 2022-07-08 mark if (err)
1540 3c1dfe12 2022-07-08 mark return err;
1541 3c1dfe12 2022-07-08 mark }
1542 cdfcfb03 2020-12-06 stsp }
1543 25791caa 2018-10-24 stsp }
1544 25791caa 2018-10-24 stsp }
1545 25791caa 2018-10-24 stsp
1546 e5a0f69f 2018-08-18 stsp switch (ch) {
1547 ec2a9698 2022-09-15 mark case '?':
1548 ec2a9698 2022-09-15 mark case 'H':
1549 ec2a9698 2022-09-15 mark case KEY_F(1):
1550 ec2a9698 2022-09-15 mark if (view->type == TOG_VIEW_HELP)
1551 ec2a9698 2022-09-15 mark err = view->reset(view);
1552 ec2a9698 2022-09-15 mark else
1553 ec2a9698 2022-09-15 mark err = view_request_new(new, view, TOG_VIEW_HELP);
1554 ec2a9698 2022-09-15 mark break;
1555 1e37a5c2 2019-05-12 jcs case '\t':
1556 640cd7ff 2022-06-22 mark view->count = 0;
1557 1e37a5c2 2019-05-12 jcs if (view->child) {
1558 e78dc838 2020-12-04 stsp view->focussed = 0;
1559 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1560 e78dc838 2020-12-04 stsp view->focus_child = 1;
1561 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1562 e78dc838 2020-12-04 stsp view->focussed = 0;
1563 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1564 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1565 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1566 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1567 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1568 6fe51fee 2022-07-22 mark 0);
1569 9b058f45 2022-06-30 mark if (err)
1570 9b058f45 2022-06-30 mark return err;
1571 9b058f45 2022-06-30 mark }
1572 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1573 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1574 9b058f45 2022-06-30 mark if (err)
1575 9b058f45 2022-06-30 mark return err;
1576 9b058f45 2022-06-30 mark }
1577 1e37a5c2 2019-05-12 jcs }
1578 1e37a5c2 2019-05-12 jcs break;
1579 1e37a5c2 2019-05-12 jcs case 'q':
1580 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1581 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1582 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1583 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1584 9b058f45 2022-06-30 mark if (err)
1585 9b058f45 2022-06-30 mark break;
1586 9b058f45 2022-06-30 mark }
1587 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1588 9b058f45 2022-06-30 mark }
1589 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1590 9970f7fc 2020-12-03 stsp view->dying = 1;
1591 1e37a5c2 2019-05-12 jcs break;
1592 1e37a5c2 2019-05-12 jcs case 'Q':
1593 1e37a5c2 2019-05-12 jcs *done = 1;
1594 1e37a5c2 2019-05-12 jcs break;
1595 61417565 2022-06-20 mark case 'F':
1596 640cd7ff 2022-06-22 mark view->count = 0;
1597 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1598 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1599 1e37a5c2 2019-05-12 jcs break;
1600 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1601 e78dc838 2020-12-04 stsp view->focussed = 0;
1602 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1603 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1604 3c1dfe12 2022-07-08 mark } else {
1605 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1606 3c1dfe12 2022-07-08 mark if (!err)
1607 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1608 3c1dfe12 2022-07-08 mark }
1609 1e37a5c2 2019-05-12 jcs if (err)
1610 1e37a5c2 2019-05-12 jcs break;
1611 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1612 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1613 1e37a5c2 2019-05-12 jcs } else {
1614 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1615 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1616 e78dc838 2020-12-04 stsp view->focussed = 1;
1617 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1618 1e37a5c2 2019-05-12 jcs } else {
1619 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1620 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1621 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1622 3c1dfe12 2022-07-08 mark if (!err)
1623 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1624 669b5ffa 2018-10-07 stsp }
1625 1e37a5c2 2019-05-12 jcs if (err)
1626 1e37a5c2 2019-05-12 jcs break;
1627 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1628 1e37a5c2 2019-05-12 jcs }
1629 9b058f45 2022-06-30 mark if (err)
1630 9b058f45 2022-06-30 mark break;
1631 6fe51fee 2022-07-22 mark if (view->resize) {
1632 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1633 9b058f45 2022-06-30 mark if (err)
1634 9b058f45 2022-06-30 mark break;
1635 9b058f45 2022-06-30 mark }
1636 9b058f45 2022-06-30 mark if (view->parent)
1637 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1638 9b058f45 2022-06-30 mark if (!err)
1639 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1640 1e37a5c2 2019-05-12 jcs break;
1641 d2366e29 2022-07-07 mark case 'S':
1642 3c1dfe12 2022-07-08 mark view->count = 0;
1643 d2366e29 2022-07-07 mark err = switch_split(view);
1644 3c1dfe12 2022-07-08 mark break;
1645 3c1dfe12 2022-07-08 mark case '-':
1646 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1647 d2366e29 2022-07-07 mark break;
1648 3c1dfe12 2022-07-08 mark case '+':
1649 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1650 3c1dfe12 2022-07-08 mark break;
1651 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1652 60493ae3 2019-06-20 stsp break;
1653 60493ae3 2019-06-20 stsp case '/':
1654 640cd7ff 2022-06-22 mark view->count = 0;
1655 60493ae3 2019-06-20 stsp if (view->search_start)
1656 2b49a8ae 2019-06-22 stsp view_search_start(view);
1657 60493ae3 2019-06-20 stsp else
1658 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1659 1e37a5c2 2019-05-12 jcs break;
1660 b1bf1435 2019-06-21 stsp case 'N':
1661 60493ae3 2019-06-20 stsp case 'n':
1662 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1663 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1664 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1665 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1666 60493ae3 2019-06-20 stsp view->search_next(view);
1667 60493ae3 2019-06-20 stsp } else
1668 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1669 917d79a7 2022-07-01 stsp break;
1670 917d79a7 2022-07-01 stsp case 'A':
1671 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1672 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1673 917d79a7 2022-07-01 stsp else
1674 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1675 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1676 917d79a7 2022-07-01 stsp if (v->reset) {
1677 917d79a7 2022-07-01 stsp err = v->reset(v);
1678 917d79a7 2022-07-01 stsp if (err)
1679 917d79a7 2022-07-01 stsp return err;
1680 917d79a7 2022-07-01 stsp }
1681 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1682 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1683 917d79a7 2022-07-01 stsp if (err)
1684 917d79a7 2022-07-01 stsp return err;
1685 917d79a7 2022-07-01 stsp }
1686 917d79a7 2022-07-01 stsp }
1687 60493ae3 2019-06-20 stsp break;
1688 1e37a5c2 2019-05-12 jcs default:
1689 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1690 1e37a5c2 2019-05-12 jcs break;
1691 e5a0f69f 2018-08-18 stsp }
1692 e5a0f69f 2018-08-18 stsp
1693 e5a0f69f 2018-08-18 stsp return err;
1694 e5a0f69f 2018-08-18 stsp }
1695 e5a0f69f 2018-08-18 stsp
1696 336075a4 2022-06-25 op static int
1697 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1698 a3404814 2018-09-02 stsp {
1699 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1700 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1701 669b5ffa 2018-10-07 stsp return 0;
1702 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1703 669b5ffa 2018-10-07 stsp return 0;
1704 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1705 a3404814 2018-09-02 stsp return 0;
1706 a3404814 2018-09-02 stsp
1707 669b5ffa 2018-10-07 stsp return view->focussed;
1708 a3404814 2018-09-02 stsp }
1709 a3404814 2018-09-02 stsp
1710 bcbd79e2 2018-08-19 stsp static const struct got_error *
1711 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1712 e5a0f69f 2018-08-18 stsp {
1713 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1714 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1715 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1716 d2366e29 2022-07-07 mark char *mode;
1717 fd823528 2018-10-22 stsp int fast_refresh = 10;
1718 1a76625f 2018-10-22 stsp int done = 0, errcode;
1719 e5a0f69f 2018-08-18 stsp
1720 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1721 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1722 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1723 d2366e29 2022-07-07 mark else
1724 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1725 d2366e29 2022-07-07 mark
1726 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1727 1a76625f 2018-10-22 stsp if (errcode)
1728 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1729 1a76625f 2018-10-22 stsp
1730 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1731 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1732 e5a0f69f 2018-08-18 stsp
1733 1004088d 2018-09-29 stsp view->focussed = 1;
1734 878940b7 2018-09-29 stsp err = view->show(view);
1735 0cf4efb1 2018-09-29 stsp if (err)
1736 0cf4efb1 2018-09-29 stsp return err;
1737 0cf4efb1 2018-09-29 stsp update_panels();
1738 0cf4efb1 2018-09-29 stsp doupdate();
1739 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1740 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1741 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1742 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1743 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1744 fd823528 2018-10-22 stsp
1745 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1746 e5a0f69f 2018-08-18 stsp if (err)
1747 e5a0f69f 2018-08-18 stsp break;
1748 9970f7fc 2020-12-03 stsp if (view->dying) {
1749 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1750 669b5ffa 2018-10-07 stsp
1751 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1752 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1753 9970f7fc 2020-12-03 stsp entry);
1754 e78dc838 2020-12-04 stsp else if (view->parent)
1755 669b5ffa 2018-10-07 stsp prev = view->parent;
1756 669b5ffa 2018-10-07 stsp
1757 e78dc838 2020-12-04 stsp if (view->parent) {
1758 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1759 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1760 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1761 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1762 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1763 0dbbbe90 2022-06-17 op if (err)
1764 0dbbbe90 2022-06-17 op break;
1765 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1766 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1767 e78dc838 2020-12-04 stsp } else
1768 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1769 669b5ffa 2018-10-07 stsp
1770 9970f7fc 2020-12-03 stsp err = view_close(view);
1771 fb59748f 2020-12-05 stsp if (err)
1772 e5a0f69f 2018-08-18 stsp goto done;
1773 669b5ffa 2018-10-07 stsp
1774 e78dc838 2020-12-04 stsp view = NULL;
1775 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1776 e78dc838 2020-12-04 stsp if (v->focussed)
1777 e78dc838 2020-12-04 stsp break;
1778 0cf4efb1 2018-09-29 stsp }
1779 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1780 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1781 e78dc838 2020-12-04 stsp if (prev)
1782 e78dc838 2020-12-04 stsp view = prev;
1783 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1784 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1785 e78dc838 2020-12-04 stsp tog_view_list_head);
1786 e78dc838 2020-12-04 stsp }
1787 e78dc838 2020-12-04 stsp if (view) {
1788 e78dc838 2020-12-04 stsp if (view->focus_child) {
1789 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1790 e78dc838 2020-12-04 stsp view = view->child;
1791 e78dc838 2020-12-04 stsp } else
1792 e78dc838 2020-12-04 stsp view->focussed = 1;
1793 e78dc838 2020-12-04 stsp }
1794 e78dc838 2020-12-04 stsp }
1795 e5a0f69f 2018-08-18 stsp }
1796 bcbd79e2 2018-08-19 stsp if (new_view) {
1797 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1798 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1799 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1800 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1801 86c66b02 2018-10-18 stsp continue;
1802 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1803 86c66b02 2018-10-18 stsp err = view_close(v);
1804 86c66b02 2018-10-18 stsp if (err)
1805 86c66b02 2018-10-18 stsp goto done;
1806 86c66b02 2018-10-18 stsp break;
1807 86c66b02 2018-10-18 stsp }
1808 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1809 fed7eaa8 2018-10-24 stsp view = new_view;
1810 0ae84acc 2022-06-15 tracey }
1811 669b5ffa 2018-10-07 stsp if (view) {
1812 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1813 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1814 e78dc838 2020-12-04 stsp view = view->child;
1815 e78dc838 2020-12-04 stsp } else {
1816 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1817 e78dc838 2020-12-04 stsp view = view->parent;
1818 1a76625f 2018-10-22 stsp }
1819 e78dc838 2020-12-04 stsp show_panel(view->panel);
1820 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1821 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1822 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1823 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1824 669b5ffa 2018-10-07 stsp if (err)
1825 1a76625f 2018-10-22 stsp goto done;
1826 669b5ffa 2018-10-07 stsp }
1827 669b5ffa 2018-10-07 stsp err = view->show(view);
1828 0cf4efb1 2018-09-29 stsp if (err)
1829 1a76625f 2018-10-22 stsp goto done;
1830 669b5ffa 2018-10-07 stsp if (view->child) {
1831 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1832 669b5ffa 2018-10-07 stsp if (err)
1833 1a76625f 2018-10-22 stsp goto done;
1834 669b5ffa 2018-10-07 stsp }
1835 1a76625f 2018-10-22 stsp update_panels();
1836 1a76625f 2018-10-22 stsp doupdate();
1837 0cf4efb1 2018-09-29 stsp }
1838 e5a0f69f 2018-08-18 stsp }
1839 e5a0f69f 2018-08-18 stsp done:
1840 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1841 5629093a 2022-07-11 stsp const struct got_error *close_err;
1842 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1843 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1844 5629093a 2022-07-11 stsp close_err = view_close(view);
1845 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1846 5629093a 2022-07-11 stsp err = close_err;
1847 e5a0f69f 2018-08-18 stsp }
1848 1a76625f 2018-10-22 stsp
1849 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1850 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1851 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1852 1a76625f 2018-10-22 stsp
1853 e5a0f69f 2018-08-18 stsp return err;
1854 ea5e7bb5 2018-08-01 stsp }
1855 ea5e7bb5 2018-08-01 stsp
1856 4ed7e80c 2018-05-20 stsp __dead static void
1857 9f7d7167 2018-04-29 stsp usage_log(void)
1858 9f7d7167 2018-04-29 stsp {
1859 80ddbec8 2018-04-29 stsp endwin();
1860 c70c5802 2018-08-01 stsp fprintf(stderr,
1861 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1862 9f7d7167 2018-04-29 stsp getprogname());
1863 9f7d7167 2018-04-29 stsp exit(1);
1864 80ddbec8 2018-04-29 stsp }
1865 80ddbec8 2018-04-29 stsp
1866 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1867 80ddbec8 2018-04-29 stsp static const struct got_error *
1868 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1869 963b370f 2018-05-20 stsp {
1870 00dfcb92 2018-06-11 stsp char *vis = NULL;
1871 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1872 963b370f 2018-05-20 stsp
1873 963b370f 2018-05-20 stsp *ws = NULL;
1874 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1875 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1876 00dfcb92 2018-06-11 stsp int vislen;
1877 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1878 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1879 00dfcb92 2018-06-11 stsp
1880 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1881 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1882 00dfcb92 2018-06-11 stsp if (err)
1883 00dfcb92 2018-06-11 stsp return err;
1884 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1885 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1886 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1887 a7f50699 2018-06-11 stsp goto done;
1888 a7f50699 2018-06-11 stsp }
1889 00dfcb92 2018-06-11 stsp }
1890 963b370f 2018-05-20 stsp
1891 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1892 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1893 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1894 a7f50699 2018-06-11 stsp goto done;
1895 a7f50699 2018-06-11 stsp }
1896 963b370f 2018-05-20 stsp
1897 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1898 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1899 a7f50699 2018-06-11 stsp done:
1900 00dfcb92 2018-06-11 stsp free(vis);
1901 963b370f 2018-05-20 stsp if (err) {
1902 963b370f 2018-05-20 stsp free(*ws);
1903 963b370f 2018-05-20 stsp *ws = NULL;
1904 963b370f 2018-05-20 stsp *wlen = 0;
1905 963b370f 2018-05-20 stsp }
1906 963b370f 2018-05-20 stsp return err;
1907 145b6838 2022-06-16 stsp }
1908 145b6838 2022-06-16 stsp
1909 145b6838 2022-06-16 stsp static const struct got_error *
1910 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1911 145b6838 2022-06-16 stsp {
1912 145b6838 2022-06-16 stsp char *dst;
1913 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1914 145b6838 2022-06-16 stsp
1915 145b6838 2022-06-16 stsp *ptr = NULL;
1916 145b6838 2022-06-16 stsp n = len = strlen(src);
1917 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1918 145b6838 2022-06-16 stsp if (dst == NULL)
1919 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1920 145b6838 2022-06-16 stsp
1921 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1922 145b6838 2022-06-16 stsp const char c = src[idx];
1923 145b6838 2022-06-16 stsp
1924 145b6838 2022-06-16 stsp if (c == '\t') {
1925 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1926 367ddf28 2022-06-16 mark char *p;
1927 367ddf28 2022-06-16 mark
1928 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1929 6e1c41ad 2022-06-16 mark if (p == NULL) {
1930 6e1c41ad 2022-06-16 mark free(dst);
1931 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1932 6e1c41ad 2022-06-16 mark
1933 6e1c41ad 2022-06-16 mark }
1934 6e1c41ad 2022-06-16 mark dst = p;
1935 145b6838 2022-06-16 stsp n += nb;
1936 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1937 145b6838 2022-06-16 stsp sz += nb;
1938 145b6838 2022-06-16 stsp } else
1939 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1940 145b6838 2022-06-16 stsp ++idx;
1941 145b6838 2022-06-16 stsp }
1942 145b6838 2022-06-16 stsp
1943 145b6838 2022-06-16 stsp dst[sz] = '\0';
1944 145b6838 2022-06-16 stsp *ptr = dst;
1945 145b6838 2022-06-16 stsp return NULL;
1946 963b370f 2018-05-20 stsp }
1947 963b370f 2018-05-20 stsp
1948 4e4a9ac8 2022-06-17 op /*
1949 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1950 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1951 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1952 4e4a9ac8 2022-06-17 op * *rcol.
1953 ccda2f4d 2022-06-16 stsp */
1954 4e4a9ac8 2022-06-17 op static int
1955 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1956 4e4a9ac8 2022-06-17 op {
1957 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1958 ccda2f4d 2022-06-16 stsp
1959 4e4a9ac8 2022-06-17 op if (n == 0) {
1960 4e4a9ac8 2022-06-17 op *rcol = cols;
1961 4e4a9ac8 2022-06-17 op return off;
1962 4e4a9ac8 2022-06-17 op }
1963 ccda2f4d 2022-06-16 stsp
1964 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1965 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1966 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1967 4e4a9ac8 2022-06-17 op else
1968 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1969 ccda2f4d 2022-06-16 stsp
1970 4e4a9ac8 2022-06-17 op if (width == -1) {
1971 4e4a9ac8 2022-06-17 op width = 1;
1972 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1973 ccda2f4d 2022-06-16 stsp }
1974 ccda2f4d 2022-06-16 stsp
1975 4e4a9ac8 2022-06-17 op if (cols + width > n)
1976 4e4a9ac8 2022-06-17 op break;
1977 4e4a9ac8 2022-06-17 op cols += width;
1978 ccda2f4d 2022-06-16 stsp }
1979 ccda2f4d 2022-06-16 stsp
1980 4e4a9ac8 2022-06-17 op *rcol = cols;
1981 4e4a9ac8 2022-06-17 op return i;
1982 ccda2f4d 2022-06-16 stsp }
1983 ccda2f4d 2022-06-16 stsp
1984 ccda2f4d 2022-06-16 stsp /*
1985 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1986 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1987 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1988 ccda2f4d 2022-06-16 stsp */
1989 ccda2f4d 2022-06-16 stsp static const struct got_error *
1990 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1991 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1992 ccda2f4d 2022-06-16 stsp {
1993 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1994 4e4a9ac8 2022-06-17 op int cols;
1995 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1996 145b6838 2022-06-16 stsp char *exstr = NULL;
1997 963b370f 2018-05-20 stsp size_t wlen;
1998 4e4a9ac8 2022-06-17 op int i, scrollx;
1999 963b370f 2018-05-20 stsp
2000 963b370f 2018-05-20 stsp *wlinep = NULL;
2001 b700b5d6 2018-07-10 stsp *widthp = 0;
2002 963b370f 2018-05-20 stsp
2003 145b6838 2022-06-16 stsp if (expand) {
2004 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
2005 145b6838 2022-06-16 stsp if (err)
2006 145b6838 2022-06-16 stsp return err;
2007 145b6838 2022-06-16 stsp }
2008 145b6838 2022-06-16 stsp
2009 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2010 145b6838 2022-06-16 stsp free(exstr);
2011 963b370f 2018-05-20 stsp if (err)
2012 963b370f 2018-05-20 stsp return err;
2013 963b370f 2018-05-20 stsp
2014 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2015 ccda2f4d 2022-06-16 stsp
2016 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2017 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2018 3f670bfb 2020-12-10 stsp wlen--;
2019 3f670bfb 2020-12-10 stsp }
2020 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2021 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2022 3f670bfb 2020-12-10 stsp wlen--;
2023 3f670bfb 2020-12-10 stsp }
2024 3f670bfb 2020-12-10 stsp
2025 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2026 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
2027 27a741e5 2019-09-11 stsp
2028 b700b5d6 2018-07-10 stsp if (widthp)
2029 b700b5d6 2018-07-10 stsp *widthp = cols;
2030 ccda2f4d 2022-06-16 stsp if (scrollxp)
2031 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
2032 963b370f 2018-05-20 stsp if (err)
2033 963b370f 2018-05-20 stsp free(wline);
2034 963b370f 2018-05-20 stsp else
2035 963b370f 2018-05-20 stsp *wlinep = wline;
2036 963b370f 2018-05-20 stsp return err;
2037 963b370f 2018-05-20 stsp }
2038 963b370f 2018-05-20 stsp
2039 8b473291 2019-02-21 stsp static const struct got_error*
2040 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2041 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2042 8b473291 2019-02-21 stsp {
2043 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2044 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2045 8b473291 2019-02-21 stsp char *s;
2046 8b473291 2019-02-21 stsp const char *name;
2047 8b473291 2019-02-21 stsp
2048 8b473291 2019-02-21 stsp *refs_str = NULL;
2049 8b473291 2019-02-21 stsp
2050 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2051 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2052 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2053 52b5abe1 2019-08-13 stsp int cmp;
2054 52b5abe1 2019-08-13 stsp
2055 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2056 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2057 8b473291 2019-02-21 stsp continue;
2058 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2059 8b473291 2019-02-21 stsp name += 5;
2060 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
2061 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
2062 7143d404 2019-03-12 stsp continue;
2063 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2064 8b473291 2019-02-21 stsp name += 6;
2065 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2066 8b473291 2019-02-21 stsp name += 8;
2067 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2068 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2069 79cc719f 2020-04-24 stsp continue;
2070 79cc719f 2020-04-24 stsp }
2071 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2072 48cae60d 2020-09-22 stsp if (err)
2073 48cae60d 2020-09-22 stsp break;
2074 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2075 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2076 5d844a1e 2019-08-13 stsp if (err) {
2077 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2078 48cae60d 2020-09-22 stsp free(ref_id);
2079 5d844a1e 2019-08-13 stsp break;
2080 48cae60d 2020-09-22 stsp }
2081 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2082 5d844a1e 2019-08-13 stsp err = NULL;
2083 5d844a1e 2019-08-13 stsp tag = NULL;
2084 5d844a1e 2019-08-13 stsp }
2085 52b5abe1 2019-08-13 stsp }
2086 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2087 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2088 48cae60d 2020-09-22 stsp free(ref_id);
2089 52b5abe1 2019-08-13 stsp if (tag)
2090 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2091 52b5abe1 2019-08-13 stsp if (cmp != 0)
2092 52b5abe1 2019-08-13 stsp continue;
2093 8b473291 2019-02-21 stsp s = *refs_str;
2094 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2095 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2096 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2097 8b473291 2019-02-21 stsp free(s);
2098 8b473291 2019-02-21 stsp *refs_str = NULL;
2099 8b473291 2019-02-21 stsp break;
2100 8b473291 2019-02-21 stsp }
2101 8b473291 2019-02-21 stsp free(s);
2102 8b473291 2019-02-21 stsp }
2103 8b473291 2019-02-21 stsp
2104 8b473291 2019-02-21 stsp return err;
2105 8b473291 2019-02-21 stsp }
2106 8b473291 2019-02-21 stsp
2107 963b370f 2018-05-20 stsp static const struct got_error *
2108 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2109 27a741e5 2019-09-11 stsp int col_tab_align)
2110 5813d178 2019-03-09 stsp {
2111 e6b8b890 2020-12-29 naddy char *smallerthan;
2112 5813d178 2019-03-09 stsp
2113 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2114 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2115 5813d178 2019-03-09 stsp author = smallerthan + 1;
2116 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2117 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
2118 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
2119 5813d178 2019-03-09 stsp }
2120 5813d178 2019-03-09 stsp
2121 5813d178 2019-03-09 stsp static const struct got_error *
2122 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2123 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2124 8fdc79fe 2020-12-01 naddy int author_display_cols)
2125 80ddbec8 2018-04-29 stsp {
2126 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2127 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2128 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2129 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2130 5813d178 2019-03-09 stsp char *author = NULL;
2131 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
2132 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2133 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2134 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
2135 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2136 ccb26ccd 2018-11-05 stsp struct tm tm;
2137 45d799e2 2018-12-23 stsp time_t committer_time;
2138 11b20872 2019-11-08 stsp struct tog_color *tc;
2139 80ddbec8 2018-04-29 stsp
2140 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2141 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2142 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2143 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2144 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2145 b39d25c7 2018-07-10 stsp
2146 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2147 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2148 b39d25c7 2018-07-10 stsp else
2149 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2150 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2151 11b20872 2019-11-08 stsp if (tc)
2152 11b20872 2019-11-08 stsp wattr_on(view->window,
2153 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2154 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2155 11b20872 2019-11-08 stsp if (tc)
2156 11b20872 2019-11-08 stsp wattr_off(view->window,
2157 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2158 27a741e5 2019-09-11 stsp col = limit;
2159 b39d25c7 2018-07-10 stsp if (col > avail)
2160 b39d25c7 2018-07-10 stsp goto done;
2161 6570a66d 2019-11-08 stsp
2162 6570a66d 2019-11-08 stsp if (avail >= 120) {
2163 6570a66d 2019-11-08 stsp char *id_str;
2164 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2165 6570a66d 2019-11-08 stsp if (err)
2166 6570a66d 2019-11-08 stsp goto done;
2167 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2168 11b20872 2019-11-08 stsp if (tc)
2169 11b20872 2019-11-08 stsp wattr_on(view->window,
2170 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2171 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2172 11b20872 2019-11-08 stsp if (tc)
2173 11b20872 2019-11-08 stsp wattr_off(view->window,
2174 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2175 6570a66d 2019-11-08 stsp free(id_str);
2176 6570a66d 2019-11-08 stsp col += 9;
2177 6570a66d 2019-11-08 stsp if (col > avail)
2178 6570a66d 2019-11-08 stsp goto done;
2179 6570a66d 2019-11-08 stsp }
2180 b39d25c7 2018-07-10 stsp
2181 10aab77f 2022-07-19 op if (s->use_committer)
2182 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2183 10aab77f 2022-07-19 op else
2184 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2185 5813d178 2019-03-09 stsp if (author == NULL) {
2186 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2187 80ddbec8 2018-04-29 stsp goto done;
2188 80ddbec8 2018-04-29 stsp }
2189 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2190 bb737323 2018-05-20 stsp if (err)
2191 bb737323 2018-05-20 stsp goto done;
2192 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2193 11b20872 2019-11-08 stsp if (tc)
2194 11b20872 2019-11-08 stsp wattr_on(view->window,
2195 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2196 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2197 bb737323 2018-05-20 stsp col += author_width;
2198 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2199 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2200 bb737323 2018-05-20 stsp col++;
2201 bb737323 2018-05-20 stsp author_width++;
2202 bb737323 2018-05-20 stsp }
2203 f0f62654 2022-09-08 mark if (tc)
2204 f0f62654 2022-09-08 mark wattr_off(view->window,
2205 f0f62654 2022-09-08 mark COLOR_PAIR(tc->colorpair), NULL);
2206 9c2eaf34 2018-05-20 stsp if (col > avail)
2207 9c2eaf34 2018-05-20 stsp goto done;
2208 80ddbec8 2018-04-29 stsp
2209 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2210 5943eee2 2019-08-13 stsp if (err)
2211 6d9fbc00 2018-04-29 stsp goto done;
2212 bb737323 2018-05-20 stsp logmsg = logmsg0;
2213 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2214 bb737323 2018-05-20 stsp logmsg++;
2215 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2216 bb737323 2018-05-20 stsp if (newline)
2217 bb737323 2018-05-20 stsp *newline = '\0';
2218 ccda2f4d 2022-06-16 stsp limit = avail - col;
2219 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2220 4d1f6af3 2022-06-17 op limit--; /* for the border */
2221 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2222 ccda2f4d 2022-06-16 stsp limit, col, 1);
2223 29688b02 2022-06-16 stsp if (err)
2224 29688b02 2022-06-16 stsp goto done;
2225 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2226 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2227 27a741e5 2019-09-11 stsp while (col < avail) {
2228 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2229 bb737323 2018-05-20 stsp col++;
2230 881b2d3e 2018-04-30 stsp }
2231 80ddbec8 2018-04-29 stsp done:
2232 80ddbec8 2018-04-29 stsp free(logmsg0);
2233 bb737323 2018-05-20 stsp free(wlogmsg);
2234 5813d178 2019-03-09 stsp free(author);
2235 bb737323 2018-05-20 stsp free(wauthor);
2236 80ddbec8 2018-04-29 stsp free(line);
2237 80ddbec8 2018-04-29 stsp return err;
2238 80ddbec8 2018-04-29 stsp }
2239 26ed57b2 2018-05-19 stsp
2240 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2241 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2242 899d86c2 2018-05-10 stsp struct got_object_id *id)
2243 80ddbec8 2018-04-29 stsp {
2244 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2245 e15c42de 2022-09-05 op struct got_object_id *dup;
2246 80ddbec8 2018-04-29 stsp
2247 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2248 80ddbec8 2018-04-29 stsp if (entry == NULL)
2249 899d86c2 2018-05-10 stsp return NULL;
2250 99db9666 2018-05-07 stsp
2251 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2252 e15c42de 2022-09-05 op if (dup == NULL) {
2253 e15c42de 2022-09-05 op free(entry);
2254 e15c42de 2022-09-05 op return NULL;
2255 e15c42de 2022-09-05 op }
2256 e15c42de 2022-09-05 op
2257 e15c42de 2022-09-05 op entry->id = dup;
2258 99db9666 2018-05-07 stsp entry->commit = commit;
2259 899d86c2 2018-05-10 stsp return entry;
2260 99db9666 2018-05-07 stsp }
2261 80ddbec8 2018-04-29 stsp
2262 99db9666 2018-05-07 stsp static void
2263 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2264 99db9666 2018-05-07 stsp {
2265 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2266 99db9666 2018-05-07 stsp
2267 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2268 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2269 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2270 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2271 e15c42de 2022-09-05 op free(entry->id);
2272 99db9666 2018-05-07 stsp free(entry);
2273 99db9666 2018-05-07 stsp }
2274 99db9666 2018-05-07 stsp
2275 99db9666 2018-05-07 stsp static void
2276 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2277 99db9666 2018-05-07 stsp {
2278 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2279 99db9666 2018-05-07 stsp pop_commit(commits);
2280 c4972b91 2018-05-07 stsp }
2281 c4972b91 2018-05-07 stsp
2282 c4972b91 2018-05-07 stsp static const struct got_error *
2283 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2284 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2285 13add988 2019-10-15 stsp {
2286 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2287 13add988 2019-10-15 stsp regmatch_t regmatch;
2288 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2289 13add988 2019-10-15 stsp
2290 13add988 2019-10-15 stsp *have_match = 0;
2291 13add988 2019-10-15 stsp
2292 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2293 13add988 2019-10-15 stsp if (err)
2294 13add988 2019-10-15 stsp return err;
2295 13add988 2019-10-15 stsp
2296 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2297 13add988 2019-10-15 stsp if (err)
2298 13add988 2019-10-15 stsp goto done;
2299 13add988 2019-10-15 stsp
2300 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2301 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2302 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2303 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2304 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2305 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2306 13add988 2019-10-15 stsp *have_match = 1;
2307 13add988 2019-10-15 stsp done:
2308 13add988 2019-10-15 stsp free(id_str);
2309 13add988 2019-10-15 stsp free(logmsg);
2310 13add988 2019-10-15 stsp return err;
2311 13add988 2019-10-15 stsp }
2312 13add988 2019-10-15 stsp
2313 13add988 2019-10-15 stsp static const struct got_error *
2314 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2315 c4972b91 2018-05-07 stsp {
2316 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2317 9ba79e04 2018-06-11 stsp
2318 1a76625f 2018-10-22 stsp /*
2319 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2320 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2321 1a76625f 2018-10-22 stsp * while updating the display.
2322 1a76625f 2018-10-22 stsp */
2323 4e0d2870 2020-12-07 naddy do {
2324 d9787ed8 2022-09-10 op struct got_object_id id;
2325 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2326 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2327 568eae95 2022-09-11 mark int limit_match = 0;
2328 1a76625f 2018-10-22 stsp int errcode;
2329 899d86c2 2018-05-10 stsp
2330 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2331 4e0d2870 2020-12-07 naddy NULL, NULL);
2332 d9787ed8 2022-09-10 op if (err)
2333 ecb28ae0 2018-07-16 stsp break;
2334 899d86c2 2018-05-10 stsp
2335 d9787ed8 2022-09-10 op err = got_object_open_as_commit(&commit, a->repo, &id);
2336 9ba79e04 2018-06-11 stsp if (err)
2337 9ba79e04 2018-06-11 stsp break;
2338 d9787ed8 2022-09-10 op entry = alloc_commit_queue_entry(commit, &id);
2339 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2340 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2341 9ba79e04 2018-06-11 stsp break;
2342 9ba79e04 2018-06-11 stsp }
2343 93e45b7c 2018-09-24 stsp
2344 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2345 1a76625f 2018-10-22 stsp if (errcode) {
2346 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2347 13add988 2019-10-15 stsp "pthread_mutex_lock");
2348 1a76625f 2018-10-22 stsp break;
2349 1a76625f 2018-10-22 stsp }
2350 1a76625f 2018-10-22 stsp
2351 568eae95 2022-09-11 mark entry->idx = a->real_commits->ncommits;
2352 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2353 568eae95 2022-09-11 mark a->real_commits->ncommits++;
2354 1a76625f 2018-10-22 stsp
2355 568eae95 2022-09-11 mark if (*a->limiting) {
2356 568eae95 2022-09-11 mark err = match_commit(&limit_match, &id, commit,
2357 568eae95 2022-09-11 mark a->limit_regex);
2358 568eae95 2022-09-11 mark if (err)
2359 568eae95 2022-09-11 mark break;
2360 568eae95 2022-09-11 mark
2361 568eae95 2022-09-11 mark if (limit_match) {
2362 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
2363 568eae95 2022-09-11 mark
2364 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(
2365 568eae95 2022-09-11 mark entry->commit, entry->id);
2366 568eae95 2022-09-11 mark if (matched == NULL) {
2367 568eae95 2022-09-11 mark err = got_error_from_errno(
2368 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
2369 568eae95 2022-09-11 mark break;
2370 568eae95 2022-09-11 mark }
2371 34a842a4 2022-09-18 mark matched->commit = entry->commit;
2372 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
2373 568eae95 2022-09-11 mark
2374 568eae95 2022-09-11 mark matched->idx = a->limit_commits->ncommits;
2375 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&a->limit_commits->head,
2376 568eae95 2022-09-11 mark matched, entry);
2377 568eae95 2022-09-11 mark a->limit_commits->ncommits++;
2378 568eae95 2022-09-11 mark }
2379 568eae95 2022-09-11 mark
2380 568eae95 2022-09-11 mark /*
2381 568eae95 2022-09-11 mark * This is how we signal log_thread() that we
2382 568eae95 2022-09-11 mark * have found a match, and that it should be
2383 568eae95 2022-09-11 mark * counted as a new entry for the view.
2384 568eae95 2022-09-11 mark */
2385 568eae95 2022-09-11 mark a->limit_match = limit_match;
2386 568eae95 2022-09-11 mark }
2387 568eae95 2022-09-11 mark
2388 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2389 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2390 7c1452c1 2020-03-26 stsp int have_match;
2391 d9787ed8 2022-09-10 op err = match_commit(&have_match, &id, commit, a->regex);
2392 7c1452c1 2020-03-26 stsp if (err)
2393 7c1452c1 2020-03-26 stsp break;
2394 568eae95 2022-09-11 mark
2395 568eae95 2022-09-11 mark if (*a->limiting) {
2396 568eae95 2022-09-11 mark if (limit_match && have_match)
2397 568eae95 2022-09-11 mark *a->search_next_done =
2398 568eae95 2022-09-11 mark TOG_SEARCH_HAVE_MORE;
2399 568eae95 2022-09-11 mark } else if (have_match)
2400 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2401 13add988 2019-10-15 stsp }
2402 13add988 2019-10-15 stsp
2403 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2404 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2405 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2406 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2407 7c1452c1 2020-03-26 stsp if (err)
2408 13add988 2019-10-15 stsp break;
2409 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2410 899d86c2 2018-05-10 stsp
2411 9ba79e04 2018-06-11 stsp return err;
2412 0553a4e3 2018-04-30 stsp }
2413 0553a4e3 2018-04-30 stsp
2414 2b779855 2020-12-05 naddy static void
2415 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2416 2b779855 2020-12-05 naddy {
2417 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2418 2b779855 2020-12-05 naddy int ncommits = 0;
2419 2b779855 2020-12-05 naddy
2420 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2421 2b779855 2020-12-05 naddy while (entry) {
2422 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2423 2b779855 2020-12-05 naddy s->selected_entry = entry;
2424 2b779855 2020-12-05 naddy break;
2425 2b779855 2020-12-05 naddy }
2426 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2427 2b779855 2020-12-05 naddy ncommits++;
2428 2b779855 2020-12-05 naddy }
2429 2b779855 2020-12-05 naddy }
2430 2b779855 2020-12-05 naddy
2431 0553a4e3 2018-04-30 stsp static const struct got_error *
2432 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2433 0553a4e3 2018-04-30 stsp {
2434 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2435 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2436 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2437 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2438 60493ae3 2019-06-20 stsp int width;
2439 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2440 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2441 8b473291 2019-02-21 stsp char *refs_str = NULL;
2442 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2443 11b20872 2019-11-08 stsp struct tog_color *tc;
2444 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2445 cbb0c8d7 2022-08-12 mark
2446 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2447 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2448 0553a4e3 2018-04-30 stsp
2449 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2450 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2451 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2452 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2453 1a76625f 2018-10-22 stsp if (err)
2454 ecb28ae0 2018-07-16 stsp return err;
2455 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2456 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2457 d2075bf3 2020-12-25 stsp if (refs) {
2458 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2459 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2460 d2075bf3 2020-12-25 stsp if (err)
2461 d2075bf3 2020-12-25 stsp goto done;
2462 d2075bf3 2020-12-25 stsp }
2463 867c6645 2018-07-10 stsp }
2464 359bfafd 2019-02-22 stsp
2465 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2466 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2467 1a76625f 2018-10-22 stsp
2468 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2469 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2470 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2471 38d166d8 2022-09-23 stsp (view->searching && !view->search_next_done) ?
2472 38d166d8 2022-09-23 stsp "searching..." : "loading...") == -1) {
2473 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2474 8f4ed634 2020-03-26 stsp goto done;
2475 8f4ed634 2020-03-26 stsp }
2476 8f4ed634 2020-03-26 stsp } else {
2477 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2478 568eae95 2022-09-11 mark const char *limit_str = NULL;
2479 f9686aa5 2020-03-27 stsp
2480 f9686aa5 2020-03-27 stsp if (view->searching) {
2481 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2482 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2483 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2484 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2485 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2486 f9686aa5 2020-03-27 stsp search_str = "searching...";
2487 f9686aa5 2020-03-27 stsp }
2488 f9686aa5 2020-03-27 stsp
2489 568eae95 2022-09-11 mark if (s->limit_view && s->commits->ncommits == 0)
2490 568eae95 2022-09-11 mark limit_str = "no matches found";
2491 568eae95 2022-09-11 mark
2492 568eae95 2022-09-11 mark if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2493 568eae95 2022-09-11 mark entry ? entry->idx + 1 : 0, s->commits->ncommits,
2494 568eae95 2022-09-11 mark search_str ? search_str : (refs_str ? refs_str : ""),
2495 568eae95 2022-09-11 mark limit_str ? limit_str : "") == -1) {
2496 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2497 8f4ed634 2020-03-26 stsp goto done;
2498 8f4ed634 2020-03-26 stsp }
2499 8b473291 2019-02-21 stsp }
2500 1a76625f 2018-10-22 stsp
2501 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2502 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2503 87411fa9 2022-06-16 stsp "........................................",
2504 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2505 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2506 1a76625f 2018-10-22 stsp header = NULL;
2507 1a76625f 2018-10-22 stsp goto done;
2508 1a76625f 2018-10-22 stsp }
2509 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2510 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2511 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2512 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2513 1a76625f 2018-10-22 stsp header = NULL;
2514 1a76625f 2018-10-22 stsp goto done;
2515 ecb28ae0 2018-07-16 stsp }
2516 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2517 1a76625f 2018-10-22 stsp if (err)
2518 1a76625f 2018-10-22 stsp goto done;
2519 867c6645 2018-07-10 stsp
2520 2814baeb 2018-08-01 stsp werase(view->window);
2521 867c6645 2018-07-10 stsp
2522 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2523 a3404814 2018-09-02 stsp wstandout(view->window);
2524 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2525 11b20872 2019-11-08 stsp if (tc)
2526 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2527 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2528 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2529 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2530 1a76625f 2018-10-22 stsp width++;
2531 1a76625f 2018-10-22 stsp }
2532 0c6ad1bc 2022-09-09 mark if (tc)
2533 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2534 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2535 a3404814 2018-09-02 stsp wstandend(view->window);
2536 ecb28ae0 2018-07-16 stsp free(wline);
2537 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2538 1a76625f 2018-10-22 stsp goto done;
2539 0553a4e3 2018-04-30 stsp
2540 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2541 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2542 5813d178 2019-03-09 stsp ncommits = 0;
2543 145b6838 2022-06-16 stsp view->maxx = 0;
2544 5813d178 2019-03-09 stsp while (entry) {
2545 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2546 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2547 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2548 5813d178 2019-03-09 stsp int width;
2549 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2550 5813d178 2019-03-09 stsp break;
2551 10aab77f 2022-07-19 op if (s->use_committer)
2552 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2553 10aab77f 2022-07-19 op else
2554 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2555 5813d178 2019-03-09 stsp if (author == NULL) {
2556 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2557 5813d178 2019-03-09 stsp goto done;
2558 5813d178 2019-03-09 stsp }
2559 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2560 27a741e5 2019-09-11 stsp date_display_cols);
2561 5813d178 2019-03-09 stsp if (author_cols < width)
2562 5813d178 2019-03-09 stsp author_cols = width;
2563 5813d178 2019-03-09 stsp free(wauthor);
2564 5813d178 2019-03-09 stsp free(author);
2565 a310d9c3 2022-07-21 florian if (err)
2566 a310d9c3 2022-07-21 florian goto done;
2567 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2568 145b6838 2022-06-16 stsp if (err)
2569 145b6838 2022-06-16 stsp goto done;
2570 145b6838 2022-06-16 stsp msg = msg0;
2571 145b6838 2022-06-16 stsp while (*msg == '\n')
2572 145b6838 2022-06-16 stsp ++msg;
2573 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2574 29688b02 2022-06-16 stsp *eol = '\0';
2575 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2576 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2577 29688b02 2022-06-16 stsp if (err)
2578 29688b02 2022-06-16 stsp goto done;
2579 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2580 145b6838 2022-06-16 stsp free(msg0);
2581 29688b02 2022-06-16 stsp free(wmsg);
2582 7ca04879 2019-10-19 stsp ncommits++;
2583 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2584 5813d178 2019-03-09 stsp }
2585 5813d178 2019-03-09 stsp
2586 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2587 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2588 867c6645 2018-07-10 stsp ncommits = 0;
2589 899d86c2 2018-05-10 stsp while (entry) {
2590 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2591 899d86c2 2018-05-10 stsp break;
2592 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2593 2814baeb 2018-08-01 stsp wstandout(view->window);
2594 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2595 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2596 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2597 2814baeb 2018-08-01 stsp wstandend(view->window);
2598 0553a4e3 2018-04-30 stsp if (err)
2599 60493ae3 2019-06-20 stsp goto done;
2600 0553a4e3 2018-04-30 stsp ncommits++;
2601 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2602 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2603 80ddbec8 2018-04-29 stsp }
2604 80ddbec8 2018-04-29 stsp
2605 9b058f45 2022-06-30 mark view_border(view);
2606 1a76625f 2018-10-22 stsp done:
2607 1a76625f 2018-10-22 stsp free(id_str);
2608 8b473291 2019-02-21 stsp free(refs_str);
2609 1a76625f 2018-10-22 stsp free(ncommits_str);
2610 1a76625f 2018-10-22 stsp free(header);
2611 80ddbec8 2018-04-29 stsp return err;
2612 9f7d7167 2018-04-29 stsp }
2613 07b55e75 2018-05-10 stsp
2614 07b55e75 2018-05-10 stsp static void
2615 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2616 07b55e75 2018-05-10 stsp {
2617 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2618 07b55e75 2018-05-10 stsp int nscrolled = 0;
2619 07b55e75 2018-05-10 stsp
2620 568eae95 2022-09-11 mark entry = TAILQ_FIRST(&s->commits->head);
2621 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2622 07b55e75 2018-05-10 stsp return;
2623 9f7d7167 2018-04-29 stsp
2624 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2625 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2626 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2627 07b55e75 2018-05-10 stsp if (entry) {
2628 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2629 07b55e75 2018-05-10 stsp nscrolled++;
2630 07b55e75 2018-05-10 stsp }
2631 07b55e75 2018-05-10 stsp }
2632 aa075928 2018-05-10 stsp }
2633 aa075928 2018-05-10 stsp
2634 aa075928 2018-05-10 stsp static const struct got_error *
2635 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2636 aa075928 2018-05-10 stsp {
2637 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2638 5e224a3e 2019-02-22 stsp int errcode;
2639 8a42fca8 2019-02-22 stsp
2640 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2641 aa075928 2018-05-10 stsp
2642 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2643 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2644 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2645 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2646 7aafa0d1 2019-02-22 stsp if (errcode)
2647 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2648 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2649 7c1452c1 2020-03-26 stsp
2650 7c1452c1 2020-03-26 stsp /*
2651 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2652 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2653 7c1452c1 2020-03-26 stsp */
2654 7c1452c1 2020-03-26 stsp if (!wait)
2655 7c1452c1 2020-03-26 stsp break;
2656 7c1452c1 2020-03-26 stsp
2657 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2658 ffe38506 2020-12-01 naddy show_log_view(view);
2659 7c1452c1 2020-03-26 stsp update_panels();
2660 7c1452c1 2020-03-26 stsp doupdate();
2661 7c1452c1 2020-03-26 stsp
2662 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2663 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2664 82954512 2020-02-03 stsp if (errcode)
2665 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2666 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2667 82954512 2020-02-03 stsp
2668 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2669 ffe38506 2020-12-01 naddy show_log_view(view);
2670 7c1452c1 2020-03-26 stsp update_panels();
2671 7c1452c1 2020-03-26 stsp doupdate();
2672 5e224a3e 2019-02-22 stsp }
2673 5e224a3e 2019-02-22 stsp
2674 5e224a3e 2019-02-22 stsp return NULL;
2675 5e224a3e 2019-02-22 stsp }
2676 5e224a3e 2019-02-22 stsp
2677 5e224a3e 2019-02-22 stsp static const struct got_error *
2678 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2679 9b058f45 2022-06-30 mark {
2680 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2681 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2682 2525dccb 2022-07-13 mark
2683 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2684 2525dccb 2022-07-13 mark return NULL;
2685 9b058f45 2022-06-30 mark
2686 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2687 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2688 9b058f45 2022-06-30 mark view->nscrolled = 0;
2689 9b058f45 2022-06-30 mark
2690 9b058f45 2022-06-30 mark return err;
2691 9b058f45 2022-06-30 mark }
2692 9b058f45 2022-06-30 mark
2693 9b058f45 2022-06-30 mark static const struct got_error *
2694 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2695 5e224a3e 2019-02-22 stsp {
2696 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2697 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2698 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2699 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2700 5e224a3e 2019-02-22 stsp
2701 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2702 5e224a3e 2019-02-22 stsp return NULL;
2703 5e224a3e 2019-02-22 stsp
2704 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2705 568eae95 2022-09-11 mark if (s->commits->ncommits < ncommits_needed &&
2706 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2707 08ebd0a9 2019-02-22 stsp /*
2708 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2709 08ebd0a9 2019-02-22 stsp */
2710 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2711 568eae95 2022-09-11 mark ncommits_needed - s->commits->ncommits;
2712 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2713 5e224a3e 2019-02-22 stsp if (err)
2714 5e224a3e 2019-02-22 stsp return err;
2715 7aafa0d1 2019-02-22 stsp }
2716 b295e71b 2019-02-22 stsp
2717 7aafa0d1 2019-02-22 stsp do {
2718 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2719 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2720 88048b54 2019-02-21 stsp break;
2721 88048b54 2019-02-21 stsp
2722 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2723 d0407b86 2023-01-10 mark pentry : s->last_displayed_entry;
2724 aa075928 2018-05-10 stsp
2725 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2726 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2727 dd0a52c1 2018-05-20 stsp break;
2728 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2729 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2730 aa075928 2018-05-10 stsp
2731 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2732 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2733 9b058f45 2022-06-30 mark else
2734 9b058f45 2022-06-30 mark view->nscrolled = 0;
2735 9b058f45 2022-06-30 mark
2736 dd0a52c1 2018-05-20 stsp return err;
2737 07b55e75 2018-05-10 stsp }
2738 4a7f7875 2018-05-10 stsp
2739 cd0acaa7 2018-05-20 stsp static const struct got_error *
2740 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2741 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2742 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2743 cd0acaa7 2018-05-20 stsp {
2744 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2745 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2746 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2747 cd0acaa7 2018-05-20 stsp
2748 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2749 15a94983 2018-12-23 stsp if (diff_view == NULL)
2750 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2751 ea5e7bb5 2018-08-01 stsp
2752 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2753 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2754 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2755 e5a0f69f 2018-08-18 stsp if (err == NULL)
2756 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2757 cd0acaa7 2018-05-20 stsp return err;
2758 4a7f7875 2018-05-10 stsp }
2759 4a7f7875 2018-05-10 stsp
2760 80ddbec8 2018-04-29 stsp static const struct got_error *
2761 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2762 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2763 9343a5fb 2018-06-23 stsp {
2764 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2765 941e9f74 2019-05-21 stsp
2766 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2767 941e9f74 2019-05-21 stsp if (parent == NULL)
2768 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2769 941e9f74 2019-05-21 stsp
2770 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2771 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2772 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2773 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2774 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2775 941e9f74 2019-05-21 stsp s->tree = subtree;
2776 941e9f74 2019-05-21 stsp s->selected = 0;
2777 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2778 941e9f74 2019-05-21 stsp return NULL;
2779 941e9f74 2019-05-21 stsp }
2780 941e9f74 2019-05-21 stsp
2781 941e9f74 2019-05-21 stsp static const struct got_error *
2782 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2783 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2784 941e9f74 2019-05-21 stsp {
2785 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2786 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2787 941e9f74 2019-05-21 stsp const char *p;
2788 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2789 9343a5fb 2018-06-23 stsp
2790 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2791 941e9f74 2019-05-21 stsp p = path;
2792 941e9f74 2019-05-21 stsp while (*p) {
2793 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2794 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2795 56e0773d 2019-11-28 stsp char *te_name;
2796 33cbf02b 2020-01-12 stsp
2797 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2798 33cbf02b 2020-01-12 stsp p++;
2799 941e9f74 2019-05-21 stsp
2800 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2801 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2802 941e9f74 2019-05-21 stsp if (slash == NULL)
2803 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2804 33cbf02b 2020-01-12 stsp else
2805 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2806 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2807 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2808 56e0773d 2019-11-28 stsp break;
2809 941e9f74 2019-05-21 stsp }
2810 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2811 56e0773d 2019-11-28 stsp if (te == NULL) {
2812 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2813 56e0773d 2019-11-28 stsp free(te_name);
2814 941e9f74 2019-05-21 stsp break;
2815 941e9f74 2019-05-21 stsp }
2816 56e0773d 2019-11-28 stsp free(te_name);
2817 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2818 941e9f74 2019-05-21 stsp
2819 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2820 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2821 b03c880f 2019-05-21 stsp
2822 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2823 941e9f74 2019-05-21 stsp if (slash)
2824 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2825 941e9f74 2019-05-21 stsp else
2826 941e9f74 2019-05-21 stsp subpath = strdup(path);
2827 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2828 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2829 941e9f74 2019-05-21 stsp break;
2830 941e9f74 2019-05-21 stsp }
2831 941e9f74 2019-05-21 stsp
2832 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2833 941e9f74 2019-05-21 stsp subpath);
2834 941e9f74 2019-05-21 stsp if (err)
2835 941e9f74 2019-05-21 stsp break;
2836 941e9f74 2019-05-21 stsp
2837 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2838 941e9f74 2019-05-21 stsp free(tree_id);
2839 941e9f74 2019-05-21 stsp if (err)
2840 941e9f74 2019-05-21 stsp break;
2841 941e9f74 2019-05-21 stsp
2842 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2843 941e9f74 2019-05-21 stsp if (err) {
2844 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2845 941e9f74 2019-05-21 stsp break;
2846 941e9f74 2019-05-21 stsp }
2847 941e9f74 2019-05-21 stsp if (slash == NULL)
2848 941e9f74 2019-05-21 stsp break;
2849 941e9f74 2019-05-21 stsp free(subpath);
2850 941e9f74 2019-05-21 stsp subpath = NULL;
2851 941e9f74 2019-05-21 stsp p = slash;
2852 941e9f74 2019-05-21 stsp }
2853 941e9f74 2019-05-21 stsp
2854 941e9f74 2019-05-21 stsp free(subpath);
2855 1a76625f 2018-10-22 stsp return err;
2856 61266923 2020-01-14 stsp }
2857 61266923 2020-01-14 stsp
2858 61266923 2020-01-14 stsp static const struct got_error *
2859 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2860 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2861 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2862 55cccc34 2020-02-20 stsp {
2863 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2864 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2865 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2866 55cccc34 2020-02-20 stsp
2867 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2868 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2869 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2870 55cccc34 2020-02-20 stsp
2871 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2872 bc573f3b 2021-07-10 stsp if (err)
2873 55cccc34 2020-02-20 stsp return err;
2874 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2875 55cccc34 2020-02-20 stsp
2876 55cccc34 2020-02-20 stsp *new_view = tree_view;
2877 55cccc34 2020-02-20 stsp
2878 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2879 55cccc34 2020-02-20 stsp return NULL;
2880 55cccc34 2020-02-20 stsp
2881 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2882 55cccc34 2020-02-20 stsp }
2883 55cccc34 2020-02-20 stsp
2884 55cccc34 2020-02-20 stsp static const struct got_error *
2885 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2886 61266923 2020-01-14 stsp {
2887 61266923 2020-01-14 stsp sigset_t sigset;
2888 61266923 2020-01-14 stsp int errcode;
2889 61266923 2020-01-14 stsp
2890 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2891 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2892 61266923 2020-01-14 stsp
2893 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2894 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2895 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2896 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2897 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2898 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2899 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2900 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2901 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2902 61266923 2020-01-14 stsp
2903 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2904 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2905 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2906 61266923 2020-01-14 stsp
2907 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2908 61266923 2020-01-14 stsp if (errcode)
2909 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2910 61266923 2020-01-14 stsp
2911 61266923 2020-01-14 stsp return NULL;
2912 1a76625f 2018-10-22 stsp }
2913 1a76625f 2018-10-22 stsp
2914 1a76625f 2018-10-22 stsp static void *
2915 1a76625f 2018-10-22 stsp log_thread(void *arg)
2916 1a76625f 2018-10-22 stsp {
2917 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2918 1a76625f 2018-10-22 stsp int errcode = 0;
2919 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2920 1a76625f 2018-10-22 stsp int done = 0;
2921 61266923 2020-01-14 stsp
2922 5629093a 2022-07-11 stsp /*
2923 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2924 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2925 5629093a 2022-07-11 stsp */
2926 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2927 5629093a 2022-07-11 stsp if (errcode) {
2928 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2929 61266923 2020-01-14 stsp return (void *)err;
2930 5629093a 2022-07-11 stsp }
2931 1a76625f 2018-10-22 stsp
2932 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2933 5629093a 2022-07-11 stsp if (err) {
2934 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2935 5629093a 2022-07-11 stsp goto done;
2936 5629093a 2022-07-11 stsp }
2937 5629093a 2022-07-11 stsp
2938 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2939 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2940 5629093a 2022-07-11 stsp if (errcode) {
2941 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2942 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2943 5629093a 2022-07-11 stsp goto done;
2944 5629093a 2022-07-11 stsp }
2945 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2946 1a76625f 2018-10-22 stsp if (err) {
2947 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2948 5629093a 2022-07-11 stsp goto done;
2949 1a76625f 2018-10-22 stsp err = NULL;
2950 1a76625f 2018-10-22 stsp done = 1;
2951 568eae95 2022-09-11 mark } else if (a->commits_needed > 0 && !a->load_all) {
2952 568eae95 2022-09-11 mark if (*a->limiting) {
2953 568eae95 2022-09-11 mark if (a->limit_match)
2954 568eae95 2022-09-11 mark a->commits_needed--;
2955 568eae95 2022-09-11 mark } else
2956 568eae95 2022-09-11 mark a->commits_needed--;
2957 568eae95 2022-09-11 mark }
2958 1a76625f 2018-10-22 stsp
2959 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2960 3abe8080 2019-04-10 stsp if (errcode) {
2961 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2962 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2963 5629093a 2022-07-11 stsp goto done;
2964 3abe8080 2019-04-10 stsp } else if (*a->quit)
2965 1a76625f 2018-10-22 stsp done = 1;
2966 568eae95 2022-09-11 mark else if (*a->limiting && *a->first_displayed_entry == NULL) {
2967 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2968 568eae95 2022-09-11 mark TAILQ_FIRST(&a->limit_commits->head);
2969 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2970 568eae95 2022-09-11 mark } else if (*a->first_displayed_entry == NULL) {
2971 568eae95 2022-09-11 mark *a->first_displayed_entry =
2972 568eae95 2022-09-11 mark TAILQ_FIRST(&a->real_commits->head);
2973 568eae95 2022-09-11 mark *a->selected_entry = *a->first_displayed_entry;
2974 1a76625f 2018-10-22 stsp }
2975 1a76625f 2018-10-22 stsp
2976 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2977 7c1452c1 2020-03-26 stsp if (errcode) {
2978 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2979 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2980 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2981 5629093a 2022-07-11 stsp goto done;
2982 7c1452c1 2020-03-26 stsp }
2983 7c1452c1 2020-03-26 stsp
2984 1a76625f 2018-10-22 stsp if (done)
2985 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2986 7c1452c1 2020-03-26 stsp else {
2987 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2988 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2989 7c1452c1 2020-03-26 stsp &tog_mutex);
2990 5629093a 2022-07-11 stsp if (errcode) {
2991 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2992 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2993 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2994 5629093a 2022-07-11 stsp goto done;
2995 5629093a 2022-07-11 stsp }
2996 21355643 2020-12-06 stsp if (*a->quit)
2997 21355643 2020-12-06 stsp done = 1;
2998 7c1452c1 2020-03-26 stsp }
2999 1a76625f 2018-10-22 stsp }
3000 1a76625f 2018-10-22 stsp }
3001 3abe8080 2019-04-10 stsp a->log_complete = 1;
3002 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3003 5629093a 2022-07-11 stsp if (errcode)
3004 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3005 5629093a 2022-07-11 stsp done:
3006 5629093a 2022-07-11 stsp if (err) {
3007 5629093a 2022-07-11 stsp tog_thread_error = 1;
3008 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
3009 5629093a 2022-07-11 stsp }
3010 1a76625f 2018-10-22 stsp return (void *)err;
3011 1a76625f 2018-10-22 stsp }
3012 1a76625f 2018-10-22 stsp
3013 1a76625f 2018-10-22 stsp static const struct got_error *
3014 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3015 1a76625f 2018-10-22 stsp {
3016 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
3017 1a76625f 2018-10-22 stsp int errcode;
3018 1a76625f 2018-10-22 stsp
3019 1a76625f 2018-10-22 stsp if (s->thread) {
3020 1a76625f 2018-10-22 stsp s->quit = 1;
3021 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3022 1a76625f 2018-10-22 stsp if (errcode)
3023 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3024 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3025 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3026 1a76625f 2018-10-22 stsp if (errcode)
3027 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3028 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3029 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
3030 1a76625f 2018-10-22 stsp if (errcode)
3031 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3032 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3033 1a76625f 2018-10-22 stsp if (errcode)
3034 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3035 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3036 1a76625f 2018-10-22 stsp s->thread = NULL;
3037 1a76625f 2018-10-22 stsp }
3038 1a76625f 2018-10-22 stsp
3039 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3040 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3041 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3042 74467cc8 2022-06-15 stsp }
3043 74467cc8 2022-06-15 stsp
3044 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
3045 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
3046 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
3047 74467cc8 2022-06-15 stsp if (err == NULL)
3048 74467cc8 2022-06-15 stsp err = pack_err;
3049 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
3050 1a76625f 2018-10-22 stsp }
3051 1a76625f 2018-10-22 stsp
3052 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3053 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3054 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3055 1a76625f 2018-10-22 stsp }
3056 1a76625f 2018-10-22 stsp
3057 5629093a 2022-07-11 stsp return err ? err : thread_err;
3058 9343a5fb 2018-06-23 stsp }
3059 9343a5fb 2018-06-23 stsp
3060 9343a5fb 2018-06-23 stsp static const struct got_error *
3061 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3062 1a76625f 2018-10-22 stsp {
3063 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3064 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3065 276b94a1 2020-11-13 naddy int errcode;
3066 1a76625f 2018-10-22 stsp
3067 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3068 276b94a1 2020-11-13 naddy
3069 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3070 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3071 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3072 276b94a1 2020-11-13 naddy
3073 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3074 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3075 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3076 276b94a1 2020-11-13 naddy
3077 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3078 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3079 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3080 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3081 1a76625f 2018-10-22 stsp free(s->start_id);
3082 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3083 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3084 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3085 1a76625f 2018-10-22 stsp return err;
3086 1a76625f 2018-10-22 stsp }
3087 1a76625f 2018-10-22 stsp
3088 568eae95 2022-09-11 mark /*
3089 568eae95 2022-09-11 mark * We use two queues to implement the limit feature: first consists of
3090 568eae95 2022-09-11 mark * commits matching the current limit_regex; second is the real queue
3091 568eae95 2022-09-11 mark * of all known commits (real_commits). When the user starts limiting,
3092 568eae95 2022-09-11 mark * we swap queues such that all movement and displaying functionality
3093 568eae95 2022-09-11 mark * works with very slight change.
3094 568eae95 2022-09-11 mark */
3095 1a76625f 2018-10-22 stsp static const struct got_error *
3096 568eae95 2022-09-11 mark limit_log_view(struct tog_view *view)
3097 568eae95 2022-09-11 mark {
3098 568eae95 2022-09-11 mark struct tog_log_view_state *s = &view->state.log;
3099 568eae95 2022-09-11 mark struct commit_queue_entry *entry;
3100 568eae95 2022-09-11 mark struct tog_view *v = view;
3101 568eae95 2022-09-11 mark const struct got_error *err = NULL;
3102 568eae95 2022-09-11 mark char pattern[1024];
3103 568eae95 2022-09-11 mark int ret;
3104 568eae95 2022-09-11 mark
3105 568eae95 2022-09-11 mark if (view_is_hsplit_top(view))
3106 568eae95 2022-09-11 mark v = view->child;
3107 568eae95 2022-09-11 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3108 568eae95 2022-09-11 mark v = view->parent;
3109 568eae95 2022-09-11 mark
3110 568eae95 2022-09-11 mark /* Get the pattern */
3111 568eae95 2022-09-11 mark wmove(v->window, v->nlines - 1, 0);
3112 568eae95 2022-09-11 mark wclrtoeol(v->window);
3113 568eae95 2022-09-11 mark mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3114 568eae95 2022-09-11 mark nodelay(v->window, FALSE);
3115 568eae95 2022-09-11 mark nocbreak();
3116 568eae95 2022-09-11 mark echo();
3117 568eae95 2022-09-11 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
3118 568eae95 2022-09-11 mark cbreak();
3119 568eae95 2022-09-11 mark noecho();
3120 568eae95 2022-09-11 mark nodelay(v->window, TRUE);
3121 568eae95 2022-09-11 mark if (ret == ERR)
3122 568eae95 2022-09-11 mark return NULL;
3123 568eae95 2022-09-11 mark
3124 568eae95 2022-09-11 mark if (*pattern == '\0') {
3125 568eae95 2022-09-11 mark /*
3126 568eae95 2022-09-11 mark * Safety measure for the situation where the user
3127 568eae95 2022-09-11 mark * resets limit without previously limiting anything.
3128 568eae95 2022-09-11 mark */
3129 568eae95 2022-09-11 mark if (!s->limit_view)
3130 568eae95 2022-09-11 mark return NULL;
3131 568eae95 2022-09-11 mark
3132 568eae95 2022-09-11 mark /*
3133 568eae95 2022-09-11 mark * User could have pressed Ctrl+L, which refreshed the
3134 568eae95 2022-09-11 mark * commit queues, it means we can't save previously
3135 568eae95 2022-09-11 mark * (before limit took place) displayed entries,
3136 568eae95 2022-09-11 mark * because they would point to already free'ed memory,
3137 568eae95 2022-09-11 mark * so we are forced to always select first entry of
3138 568eae95 2022-09-11 mark * the queue.
3139 568eae95 2022-09-11 mark */
3140 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3141 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3142 568eae95 2022-09-11 mark s->selected_entry = s->first_displayed_entry;
3143 568eae95 2022-09-11 mark s->selected = 0;
3144 568eae95 2022-09-11 mark s->limit_view = 0;
3145 568eae95 2022-09-11 mark
3146 568eae95 2022-09-11 mark return NULL;
3147 568eae95 2022-09-11 mark }
3148 568eae95 2022-09-11 mark
3149 568eae95 2022-09-11 mark if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3150 568eae95 2022-09-11 mark return NULL;
3151 568eae95 2022-09-11 mark
3152 568eae95 2022-09-11 mark s->limit_view = 1;
3153 568eae95 2022-09-11 mark
3154 568eae95 2022-09-11 mark /* Clear the screen while loading limit view */
3155 568eae95 2022-09-11 mark s->first_displayed_entry = NULL;
3156 568eae95 2022-09-11 mark s->last_displayed_entry = NULL;
3157 568eae95 2022-09-11 mark s->selected_entry = NULL;
3158 568eae95 2022-09-11 mark s->commits = &s->limit_commits;
3159 568eae95 2022-09-11 mark
3160 568eae95 2022-09-11 mark /* Prepare limit queue for new search */
3161 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3162 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3163 568eae95 2022-09-11 mark
3164 568eae95 2022-09-11 mark /* First process commits, which are in queue already */
3165 568eae95 2022-09-11 mark TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3166 568eae95 2022-09-11 mark int have_match = 0;
3167 568eae95 2022-09-11 mark
3168 568eae95 2022-09-11 mark err = match_commit(&have_match, entry->id,
3169 568eae95 2022-09-11 mark entry->commit, &s->limit_regex);
3170 568eae95 2022-09-11 mark if (err)
3171 568eae95 2022-09-11 mark return err;
3172 568eae95 2022-09-11 mark
3173 568eae95 2022-09-11 mark if (have_match) {
3174 568eae95 2022-09-11 mark struct commit_queue_entry *matched;
3175 568eae95 2022-09-11 mark
3176 568eae95 2022-09-11 mark matched = alloc_commit_queue_entry(entry->commit,
3177 568eae95 2022-09-11 mark entry->id);
3178 568eae95 2022-09-11 mark if (matched == NULL) {
3179 568eae95 2022-09-11 mark err = got_error_from_errno(
3180 568eae95 2022-09-11 mark "alloc_commit_queue_entry");
3181 568eae95 2022-09-11 mark break;
3182 568eae95 2022-09-11 mark }
3183 34a842a4 2022-09-18 mark matched->commit = entry->commit;
3184 34a842a4 2022-09-18 mark got_object_commit_retain(entry->commit);
3185 568eae95 2022-09-11 mark
3186 568eae95 2022-09-11 mark matched->idx = s->limit_commits.ncommits;
3187 568eae95 2022-09-11 mark TAILQ_INSERT_TAIL(&s->limit_commits.head,
3188 568eae95 2022-09-11 mark matched, entry);
3189 568eae95 2022-09-11 mark s->limit_commits.ncommits++;
3190 568eae95 2022-09-11 mark }
3191 568eae95 2022-09-11 mark }
3192 568eae95 2022-09-11 mark
3193 568eae95 2022-09-11 mark /* Second process all the commits, until we fill the screen */
3194 568eae95 2022-09-11 mark if (s->limit_commits.ncommits < view->nlines - 1 &&
3195 568eae95 2022-09-11 mark !s->thread_args.log_complete) {
3196 568eae95 2022-09-11 mark s->thread_args.commits_needed +=
3197 568eae95 2022-09-11 mark view->nlines - s->limit_commits.ncommits - 1;
3198 568eae95 2022-09-11 mark err = trigger_log_thread(view, 1);
3199 568eae95 2022-09-11 mark if (err)
3200 568eae95 2022-09-11 mark return err;
3201 568eae95 2022-09-11 mark }
3202 568eae95 2022-09-11 mark
3203 568eae95 2022-09-11 mark s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3204 568eae95 2022-09-11 mark s->selected_entry = TAILQ_FIRST(&s->commits->head);
3205 568eae95 2022-09-11 mark s->selected = 0;
3206 568eae95 2022-09-11 mark
3207 568eae95 2022-09-11 mark return NULL;
3208 568eae95 2022-09-11 mark }
3209 568eae95 2022-09-11 mark
3210 568eae95 2022-09-11 mark static const struct got_error *
3211 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3212 60493ae3 2019-06-20 stsp {
3213 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3214 60493ae3 2019-06-20 stsp
3215 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3216 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3217 60493ae3 2019-06-20 stsp return NULL;
3218 60493ae3 2019-06-20 stsp }
3219 60493ae3 2019-06-20 stsp
3220 60493ae3 2019-06-20 stsp static const struct got_error *
3221 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3222 60493ae3 2019-06-20 stsp {
3223 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3224 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3225 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3226 60493ae3 2019-06-20 stsp
3227 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3228 f9686aa5 2020-03-27 stsp show_log_view(view);
3229 f9686aa5 2020-03-27 stsp update_panels();
3230 f9686aa5 2020-03-27 stsp doupdate();
3231 f9686aa5 2020-03-27 stsp
3232 96e2b566 2019-07-08 stsp if (s->search_entry) {
3233 13add988 2019-10-15 stsp int errcode, ch;
3234 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3235 13add988 2019-10-15 stsp if (errcode)
3236 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3237 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3238 13add988 2019-10-15 stsp ch = wgetch(view->window);
3239 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3240 13add988 2019-10-15 stsp if (errcode)
3241 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3242 13add988 2019-10-15 stsp "pthread_mutex_lock");
3243 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3244 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3245 678cbce5 2019-07-28 stsp return NULL;
3246 678cbce5 2019-07-28 stsp }
3247 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3248 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3249 96e2b566 2019-07-08 stsp else
3250 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3251 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3252 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3253 364ac6fd 2022-06-18 stsp /*
3254 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
3255 f704b35c 2022-06-18 stsp * the position from where we should continue searching
3256 f704b35c 2022-06-18 stsp * might have changed.
3257 364ac6fd 2022-06-18 stsp */
3258 f83ada34 2022-09-13 mark if (view->searching == TOG_SEARCH_FORWARD)
3259 f83ada34 2022-09-13 mark entry = TAILQ_NEXT(s->selected_entry, entry);
3260 f83ada34 2022-09-13 mark else
3261 f83ada34 2022-09-13 mark entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3262 f83ada34 2022-09-13 mark entry);
3263 20be8d96 2019-06-21 stsp } else {
3264 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
3265 20be8d96 2019-06-21 stsp }
3266 60493ae3 2019-06-20 stsp
3267 60493ae3 2019-06-20 stsp while (1) {
3268 13add988 2019-10-15 stsp int have_match = 0;
3269 13add988 2019-10-15 stsp
3270 60493ae3 2019-06-20 stsp if (entry == NULL) {
3271 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3272 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3273 f9967bca 2020-03-27 stsp view->search_next_done =
3274 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3275 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3276 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3277 f801134a 2019-06-25 stsp return NULL;
3278 60493ae3 2019-06-20 stsp }
3279 96e2b566 2019-07-08 stsp /*
3280 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3281 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3282 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3283 96e2b566 2019-07-08 stsp */
3284 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3285 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3286 60493ae3 2019-06-20 stsp }
3287 60493ae3 2019-06-20 stsp
3288 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3289 13add988 2019-10-15 stsp &view->regex);
3290 5943eee2 2019-08-13 stsp if (err)
3291 13add988 2019-10-15 stsp break;
3292 13add988 2019-10-15 stsp if (have_match) {
3293 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3294 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3295 60493ae3 2019-06-20 stsp break;
3296 60493ae3 2019-06-20 stsp }
3297 13add988 2019-10-15 stsp
3298 96e2b566 2019-07-08 stsp s->search_entry = entry;
3299 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3300 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3301 b1bf1435 2019-06-21 stsp else
3302 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3303 60493ae3 2019-06-20 stsp }
3304 60493ae3 2019-06-20 stsp
3305 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3306 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3307 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3308 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3309 60493ae3 2019-06-20 stsp if (err)
3310 60493ae3 2019-06-20 stsp return err;
3311 ead14cbe 2019-06-21 stsp cur++;
3312 ead14cbe 2019-06-21 stsp }
3313 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3314 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3315 60493ae3 2019-06-20 stsp if (err)
3316 60493ae3 2019-06-20 stsp return err;
3317 ead14cbe 2019-06-21 stsp cur--;
3318 60493ae3 2019-06-20 stsp }
3319 60493ae3 2019-06-20 stsp }
3320 60493ae3 2019-06-20 stsp
3321 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3322 96e2b566 2019-07-08 stsp
3323 60493ae3 2019-06-20 stsp return NULL;
3324 60493ae3 2019-06-20 stsp }
3325 60493ae3 2019-06-20 stsp
3326 60493ae3 2019-06-20 stsp static const struct got_error *
3327 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3328 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3329 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3330 80ddbec8 2018-04-29 stsp {
3331 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3332 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3333 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3334 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3335 1a76625f 2018-10-22 stsp int errcode;
3336 80ddbec8 2018-04-29 stsp
3337 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3338 f135c941 2020-02-20 stsp free(s->in_repo_path);
3339 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3340 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3341 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3342 f135c941 2020-02-20 stsp }
3343 ecb28ae0 2018-07-16 stsp
3344 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3345 568eae95 2022-09-11 mark TAILQ_INIT(&s->real_commits.head);
3346 568eae95 2022-09-11 mark s->real_commits.ncommits = 0;
3347 568eae95 2022-09-11 mark s->commits = &s->real_commits;
3348 78756c87 2020-11-24 stsp
3349 568eae95 2022-09-11 mark TAILQ_INIT(&s->limit_commits.head);
3350 568eae95 2022-09-11 mark s->limit_view = 0;
3351 568eae95 2022-09-11 mark s->limit_commits.ncommits = 0;
3352 568eae95 2022-09-11 mark
3353 fb2756b9 2018-08-04 stsp s->repo = repo;
3354 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3355 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3356 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3357 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3358 9cd7cbd1 2020-12-07 stsp goto done;
3359 9cd7cbd1 2020-12-07 stsp }
3360 9cd7cbd1 2020-12-07 stsp }
3361 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3362 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3363 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3364 5036bf37 2018-09-24 stsp goto done;
3365 5036bf37 2018-09-24 stsp }
3366 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3367 3bf00f25 2023-01-02 stsp s->use_committer = 1;
3368 e5a0f69f 2018-08-18 stsp
3369 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3370 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3371 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3372 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3373 11b20872 2019-11-08 stsp if (err)
3374 11b20872 2019-11-08 stsp goto done;
3375 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3376 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3377 11b20872 2019-11-08 stsp if (err) {
3378 11b20872 2019-11-08 stsp free_colors(&s->colors);
3379 11b20872 2019-11-08 stsp goto done;
3380 11b20872 2019-11-08 stsp }
3381 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3382 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3383 11b20872 2019-11-08 stsp if (err) {
3384 11b20872 2019-11-08 stsp free_colors(&s->colors);
3385 11b20872 2019-11-08 stsp goto done;
3386 11b20872 2019-11-08 stsp }
3387 11b20872 2019-11-08 stsp }
3388 11b20872 2019-11-08 stsp
3389 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3390 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3391 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3392 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3393 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3394 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3395 1a76625f 2018-10-22 stsp
3396 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3397 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3398 74467cc8 2022-06-15 stsp if (err)
3399 74467cc8 2022-06-15 stsp goto done;
3400 74467cc8 2022-06-15 stsp }
3401 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3402 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3403 0ae84acc 2022-06-15 tracey if (err)
3404 0ae84acc 2022-06-15 tracey goto done;
3405 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3406 b672a97a 2020-01-27 stsp !s->log_branches);
3407 1a76625f 2018-10-22 stsp if (err)
3408 1a76625f 2018-10-22 stsp goto done;
3409 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3410 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3411 c5b78334 2020-01-12 stsp if (err)
3412 c5b78334 2020-01-12 stsp goto done;
3413 1a76625f 2018-10-22 stsp
3414 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3415 1a76625f 2018-10-22 stsp if (errcode) {
3416 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3417 1a76625f 2018-10-22 stsp goto done;
3418 1a76625f 2018-10-22 stsp }
3419 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3420 7c1452c1 2020-03-26 stsp if (errcode) {
3421 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3422 7c1452c1 2020-03-26 stsp goto done;
3423 7c1452c1 2020-03-26 stsp }
3424 1a76625f 2018-10-22 stsp
3425 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3426 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3427 568eae95 2022-09-11 mark s->thread_args.real_commits = &s->real_commits;
3428 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3429 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3430 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3431 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3432 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3433 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3434 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3435 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3436 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3437 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3438 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3439 568eae95 2022-09-11 mark s->thread_args.limiting = &s->limit_view;
3440 568eae95 2022-09-11 mark s->thread_args.limit_regex = &s->limit_regex;
3441 568eae95 2022-09-11 mark s->thread_args.limit_commits = &s->limit_commits;
3442 ba4f502b 2018-08-04 stsp done:
3443 1a76625f 2018-10-22 stsp if (err)
3444 1a76625f 2018-10-22 stsp close_log_view(view);
3445 ba4f502b 2018-08-04 stsp return err;
3446 ba4f502b 2018-08-04 stsp }
3447 ba4f502b 2018-08-04 stsp
3448 e5a0f69f 2018-08-18 stsp static const struct got_error *
3449 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3450 ba4f502b 2018-08-04 stsp {
3451 f2f6d207 2020-11-24 stsp const struct got_error *err;
3452 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3453 ba4f502b 2018-08-04 stsp
3454 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3455 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3456 2b380cc8 2018-10-24 stsp &s->thread_args);
3457 2b380cc8 2018-10-24 stsp if (errcode)
3458 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3459 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3460 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3461 f2f6d207 2020-11-24 stsp if (err)
3462 f2f6d207 2020-11-24 stsp return err;
3463 f2f6d207 2020-11-24 stsp }
3464 2b380cc8 2018-10-24 stsp }
3465 2b380cc8 2018-10-24 stsp
3466 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3467 b880cc75 2022-06-30 mark }
3468 b880cc75 2022-06-30 mark
3469 b880cc75 2022-06-30 mark static void
3470 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3471 b880cc75 2022-06-30 mark {
3472 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3473 b880cc75 2022-06-30 mark
3474 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3475 b880cc75 2022-06-30 mark return;
3476 568eae95 2022-09-11 mark if (s->selected_entry->idx == 0)
3477 568eae95 2022-09-11 mark view->count = 0;
3478 b880cc75 2022-06-30 mark
3479 568eae95 2022-09-11 mark if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3480 b880cc75 2022-06-30 mark || home)
3481 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3482 b880cc75 2022-06-30 mark
3483 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3484 b880cc75 2022-06-30 mark --s->selected;
3485 b880cc75 2022-06-30 mark else
3486 568eae95 2022-09-11 mark log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3487 b880cc75 2022-06-30 mark
3488 b880cc75 2022-06-30 mark select_commit(s);
3489 b880cc75 2022-06-30 mark return;
3490 b880cc75 2022-06-30 mark }
3491 b880cc75 2022-06-30 mark
3492 b880cc75 2022-06-30 mark static const struct got_error *
3493 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3494 b880cc75 2022-06-30 mark {
3495 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3496 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3497 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3498 b880cc75 2022-06-30 mark
3499 568eae95 2022-09-11 mark if (s->first_displayed_entry == NULL)
3500 568eae95 2022-09-11 mark return NULL;
3501 568eae95 2022-09-11 mark
3502 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3503 568eae95 2022-09-11 mark s->selected_entry->idx >= s->commits->ncommits - 1)
3504 b880cc75 2022-06-30 mark return NULL;
3505 b880cc75 2022-06-30 mark
3506 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3507 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3508 b880cc75 2022-06-30 mark
3509 631e7531 2022-08-12 mark if (!page) {
3510 568eae95 2022-09-11 mark if (s->selected < MIN(eos, s->commits->ncommits - 1))
3511 b880cc75 2022-06-30 mark ++s->selected;
3512 b880cc75 2022-06-30 mark else
3513 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3514 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3515 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3516 631e7531 2022-08-12 mark int n;
3517 631e7531 2022-08-12 mark
3518 631e7531 2022-08-12 mark s->selected = 0;
3519 568eae95 2022-09-11 mark entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3520 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3521 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3522 631e7531 2022-08-12 mark if (entry == NULL)
3523 631e7531 2022-08-12 mark break;
3524 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3525 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3526 631e7531 2022-08-12 mark }
3527 631e7531 2022-08-12 mark if (n > 0)
3528 631e7531 2022-08-12 mark s->selected = n - 1;
3529 b880cc75 2022-06-30 mark } else {
3530 568eae95 2022-09-11 mark if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3531 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3532 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3533 568eae95 2022-09-11 mark s->commits->ncommits - s->selected_entry->idx - 1);
3534 cbb0c8d7 2022-08-12 mark else
3535 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3536 b880cc75 2022-06-30 mark }
3537 b880cc75 2022-06-30 mark if (err)
3538 b880cc75 2022-06-30 mark return err;
3539 b880cc75 2022-06-30 mark
3540 9b058f45 2022-06-30 mark /*
3541 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3542 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3543 9b058f45 2022-06-30 mark */
3544 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3545 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3546 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3547 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3548 374f69dd 2022-08-13 stsp }
3549 9b058f45 2022-06-30 mark
3550 b880cc75 2022-06-30 mark select_commit(s);
3551 b880cc75 2022-06-30 mark
3552 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3553 568eae95 2022-09-11 mark s->selected_entry->idx == s->commits->ncommits - 1)
3554 b880cc75 2022-06-30 mark view->count = 0;
3555 b880cc75 2022-06-30 mark
3556 b880cc75 2022-06-30 mark return NULL;
3557 e5a0f69f 2018-08-18 stsp }
3558 04cc582a 2018-08-01 stsp
3559 9b058f45 2022-06-30 mark static void
3560 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3561 9b058f45 2022-06-30 mark {
3562 76364b2d 2022-07-02 mark *x = 0;
3563 76364b2d 2022-07-02 mark *y = 0;
3564 76364b2d 2022-07-02 mark
3565 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3566 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3567 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3568 7532ccda 2022-07-11 mark else if (view->resized_y)
3569 7532ccda 2022-07-11 mark *y = view->resized_y;
3570 3c1dfe12 2022-07-08 mark else
3571 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3572 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3573 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3574 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3575 7532ccda 2022-07-11 mark else if (view->resized_x)
3576 7532ccda 2022-07-11 mark *x = view->resized_x;
3577 3c1dfe12 2022-07-08 mark else
3578 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3579 3c1dfe12 2022-07-08 mark }
3580 9b058f45 2022-06-30 mark }
3581 9b058f45 2022-06-30 mark
3582 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3583 e5a0f69f 2018-08-18 stsp static const struct got_error *
3584 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3585 9b058f45 2022-06-30 mark {
3586 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3587 9b058f45 2022-06-30 mark
3588 9b058f45 2022-06-30 mark view->nlines = y;
3589 d2366e29 2022-07-07 mark view->ncols = COLS;
3590 9b058f45 2022-06-30 mark err = view_resize(view);
3591 9b058f45 2022-06-30 mark if (err)
3592 9b058f45 2022-06-30 mark return err;
3593 9b058f45 2022-06-30 mark
3594 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3595 9b058f45 2022-06-30 mark
3596 9b058f45 2022-06-30 mark return err;
3597 94b80cfa 2022-08-01 mark }
3598 94b80cfa 2022-08-01 mark
3599 94b80cfa 2022-08-01 mark static const struct got_error *
3600 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3601 94b80cfa 2022-08-01 mark {
3602 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3603 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3604 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3605 374f69dd 2022-08-13 stsp
3606 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3607 374f69dd 2022-08-13 stsp return NULL;
3608 94b80cfa 2022-08-01 mark
3609 94b80cfa 2022-08-01 mark g = view->gline;
3610 94b80cfa 2022-08-01 mark view->gline = 0;
3611 94b80cfa 2022-08-01 mark
3612 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3613 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3614 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3615 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3616 94b80cfa 2022-08-01 mark select_commit(s);
3617 94b80cfa 2022-08-01 mark return NULL;
3618 94b80cfa 2022-08-01 mark }
3619 94b80cfa 2022-08-01 mark
3620 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3621 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3622 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3623 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3624 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3625 94b80cfa 2022-08-01 mark if (err)
3626 94b80cfa 2022-08-01 mark return err;
3627 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3628 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3629 94b80cfa 2022-08-01 mark
3630 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3631 94b80cfa 2022-08-01 mark s->selected = g - 1;
3632 94b80cfa 2022-08-01 mark
3633 94b80cfa 2022-08-01 mark select_commit(s);
3634 94b80cfa 2022-08-01 mark return NULL;
3635 94b80cfa 2022-08-01 mark
3636 9b058f45 2022-06-30 mark }
3637 9b058f45 2022-06-30 mark
3638 9b058f45 2022-06-30 mark static const struct got_error *
3639 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3640 e5a0f69f 2018-08-18 stsp {
3641 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3642 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3643 631e7531 2022-08-12 mark int eos, nscroll;
3644 80ddbec8 2018-04-29 stsp
3645 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3646 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3647 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3648 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3649 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3650 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3651 fb280deb 2021-08-30 stsp }
3652 11edf34c 2022-08-12 mark if (err)
3653 11edf34c 2022-08-12 mark return err;
3654 528dedf3 2021-08-30 stsp }
3655 0dca135e 2022-06-30 mark
3656 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3657 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3658 0dca135e 2022-06-30 mark --eos; /* border */
3659 94b80cfa 2022-08-01 mark
3660 94b80cfa 2022-08-01 mark if (view->gline)
3661 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3662 0dca135e 2022-06-30 mark
3663 528dedf3 2021-08-30 stsp switch (ch) {
3664 568eae95 2022-09-11 mark case '&':
3665 568eae95 2022-09-11 mark err = limit_log_view(view);
3666 568eae95 2022-09-11 mark break;
3667 1e37a5c2 2019-05-12 jcs case 'q':
3668 1e37a5c2 2019-05-12 jcs s->quit = 1;
3669 145b6838 2022-06-16 stsp break;
3670 145b6838 2022-06-16 stsp case '0':
3671 145b6838 2022-06-16 stsp view->x = 0;
3672 145b6838 2022-06-16 stsp break;
3673 145b6838 2022-06-16 stsp case '$':
3674 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3675 640cd7ff 2022-06-22 mark view->count = 0;
3676 145b6838 2022-06-16 stsp break;
3677 145b6838 2022-06-16 stsp case KEY_RIGHT:
3678 145b6838 2022-06-16 stsp case 'l':
3679 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3680 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3681 640cd7ff 2022-06-22 mark else
3682 640cd7ff 2022-06-22 mark view->count = 0;
3683 1e37a5c2 2019-05-12 jcs break;
3684 145b6838 2022-06-16 stsp case KEY_LEFT:
3685 145b6838 2022-06-16 stsp case 'h':
3686 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3687 640cd7ff 2022-06-22 mark if (view->x <= 0)
3688 640cd7ff 2022-06-22 mark view->count = 0;
3689 145b6838 2022-06-16 stsp break;
3690 1e37a5c2 2019-05-12 jcs case 'k':
3691 1e37a5c2 2019-05-12 jcs case KEY_UP:
3692 1e37a5c2 2019-05-12 jcs case '<':
3693 1e37a5c2 2019-05-12 jcs case ',':
3694 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3695 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3696 912a3f79 2021-08-30 j break;
3697 912a3f79 2021-08-30 j case 'g':
3698 0b3f028d 2023-01-09 mark case '=':
3699 912a3f79 2021-08-30 j case KEY_HOME:
3700 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3701 640cd7ff 2022-06-22 mark view->count = 0;
3702 1e37a5c2 2019-05-12 jcs break;
3703 83cc4199 2022-06-13 stsp case CTRL('u'):
3704 33c3719a 2022-06-15 stsp case 'u':
3705 83cc4199 2022-06-13 stsp nscroll /= 2;
3706 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3707 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3708 a4292ac5 2019-05-12 jcs case CTRL('b'):
3709 61417565 2022-06-20 mark case 'b':
3710 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3711 1e37a5c2 2019-05-12 jcs break;
3712 1e37a5c2 2019-05-12 jcs case 'j':
3713 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3714 1e37a5c2 2019-05-12 jcs case '>':
3715 1e37a5c2 2019-05-12 jcs case '.':
3716 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3717 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3718 912a3f79 2021-08-30 j break;
3719 10aab77f 2022-07-19 op case '@':
3720 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3721 10aab77f 2022-07-19 op break;
3722 912a3f79 2021-08-30 j case 'G':
3723 0b3f028d 2023-01-09 mark case '*':
3724 912a3f79 2021-08-30 j case KEY_END: {
3725 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3726 912a3f79 2021-08-30 j * traverse them all. */
3727 640cd7ff 2022-06-22 mark view->count = 0;
3728 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3729 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3730 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3731 568eae95 2022-09-11 mark err = log_move_cursor_down(view, s->commits->ncommits);
3732 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3733 1e37a5c2 2019-05-12 jcs break;
3734 912a3f79 2021-08-30 j }
3735 80b7e8da 2022-06-11 stsp case CTRL('d'):
3736 33c3719a 2022-06-15 stsp case 'd':
3737 83cc4199 2022-06-13 stsp nscroll /= 2;
3738 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3739 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3740 61417565 2022-06-20 mark case CTRL('f'):
3741 48bb96f0 2022-06-20 naddy case 'f':
3742 b880cc75 2022-06-30 mark case ' ':
3743 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3744 1e37a5c2 2019-05-12 jcs break;
3745 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3746 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3747 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3748 568eae95 2022-09-11 mark if (s->selected > s->commits->ncommits - 1)
3749 568eae95 2022-09-11 mark s->selected = s->commits->ncommits - 1;
3750 2b779855 2020-12-05 naddy select_commit(s);
3751 568eae95 2022-09-11 mark if (s->commits->ncommits < view->nlines - 1 &&
3752 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3753 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3754 568eae95 2022-09-11 mark s->commits->ncommits;
3755 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3756 0bf7f153 2020-12-02 naddy }
3757 1e37a5c2 2019-05-12 jcs break;
3758 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3759 49b24ee5 2022-07-03 mark case '\r':
3760 640cd7ff 2022-06-22 mark view->count = 0;
3761 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3762 e5a0f69f 2018-08-18 stsp break;
3763 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3764 1e37a5c2 2019-05-12 jcs break;
3765 5e98fb33 2022-07-22 mark case 'T':
3766 640cd7ff 2022-06-22 mark view->count = 0;
3767 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3768 5036bf37 2018-09-24 stsp break;
3769 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3770 1e37a5c2 2019-05-12 jcs break;
3771 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3772 21355643 2020-12-06 stsp case CTRL('l'):
3773 21355643 2020-12-06 stsp case 'B':
3774 640cd7ff 2022-06-22 mark view->count = 0;
3775 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3776 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3777 1e37a5c2 2019-05-12 jcs break;
3778 21355643 2020-12-06 stsp err = stop_log_thread(s);
3779 74cfe85e 2020-10-20 stsp if (err)
3780 74cfe85e 2020-10-20 stsp return err;
3781 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3782 21355643 2020-12-06 stsp char *parent_path;
3783 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3784 21355643 2020-12-06 stsp if (err)
3785 21355643 2020-12-06 stsp return err;
3786 21355643 2020-12-06 stsp free(s->in_repo_path);
3787 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3788 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3789 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3790 21355643 2020-12-06 stsp struct got_object_id *start_id;
3791 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3792 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3793 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3794 ea371198 2022-11-17 stsp if (err) {
3795 ea371198 2022-11-17 stsp if (s->head_ref_name == NULL ||
3796 ea371198 2022-11-17 stsp err->code != GOT_ERR_NOT_REF)
3797 ea371198 2022-11-17 stsp return err;
3798 ea371198 2022-11-17 stsp /* Try to cope with deleted references. */
3799 ea371198 2022-11-17 stsp free(s->head_ref_name);
3800 ea371198 2022-11-17 stsp s->head_ref_name = NULL;
3801 ea371198 2022-11-17 stsp err = got_repo_match_object_id(&start_id,
3802 ea371198 2022-11-17 stsp NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3803 ea371198 2022-11-17 stsp &tog_refs, s->repo);
3804 ea371198 2022-11-17 stsp if (err)
3805 ea371198 2022-11-17 stsp return err;
3806 ea371198 2022-11-17 stsp }
3807 21355643 2020-12-06 stsp free(s->start_id);
3808 21355643 2020-12-06 stsp s->start_id = start_id;
3809 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3810 21355643 2020-12-06 stsp } else /* 'B' */
3811 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3812 21355643 2020-12-06 stsp
3813 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3814 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3815 b0dd8d27 2022-06-16 stsp if (err)
3816 b0dd8d27 2022-06-16 stsp return err;
3817 b0dd8d27 2022-06-16 stsp }
3818 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3819 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3820 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3821 74cfe85e 2020-10-20 stsp if (err)
3822 21355643 2020-12-06 stsp return err;
3823 51a10b52 2020-12-26 stsp tog_free_refs();
3824 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3825 ca51c541 2020-12-07 stsp if (err)
3826 ca51c541 2020-12-07 stsp return err;
3827 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3828 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3829 d01904d4 2019-06-25 stsp if (err)
3830 d01904d4 2019-06-25 stsp return err;
3831 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3832 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3833 b672a97a 2020-01-27 stsp if (err)
3834 b672a97a 2020-01-27 stsp return err;
3835 568eae95 2022-09-11 mark free_commits(&s->real_commits);
3836 568eae95 2022-09-11 mark free_commits(&s->limit_commits);
3837 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3838 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3839 21355643 2020-12-06 stsp s->selected_entry = NULL;
3840 21355643 2020-12-06 stsp s->selected = 0;
3841 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3842 21355643 2020-12-06 stsp s->quit = 0;
3843 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3844 dfee752e 2022-06-18 op s->matched_entry = NULL;
3845 dfee752e 2022-06-18 op s->search_entry = NULL;
3846 01a7bcaf 2022-07-22 mark view->offset = 0;
3847 d01904d4 2019-06-25 stsp break;
3848 5e98fb33 2022-07-22 mark case 'R':
3849 640cd7ff 2022-06-22 mark view->count = 0;
3850 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3851 6458efa5 2020-11-24 stsp break;
3852 1e37a5c2 2019-05-12 jcs default:
3853 640cd7ff 2022-06-22 mark view->count = 0;
3854 1e37a5c2 2019-05-12 jcs break;
3855 899d86c2 2018-05-10 stsp }
3856 e5a0f69f 2018-08-18 stsp
3857 80ddbec8 2018-04-29 stsp return err;
3858 80ddbec8 2018-04-29 stsp }
3859 80ddbec8 2018-04-29 stsp
3860 4ed7e80c 2018-05-20 stsp static const struct got_error *
3861 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3862 c2db6724 2019-01-04 stsp {
3863 c2db6724 2019-01-04 stsp const struct got_error *error;
3864 c2db6724 2019-01-04 stsp
3865 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3866 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3867 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3868 37c06ea4 2019-07-15 stsp #endif
3869 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3870 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3871 c2db6724 2019-01-04 stsp
3872 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3873 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3874 c2db6724 2019-01-04 stsp
3875 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3876 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3877 c2db6724 2019-01-04 stsp
3878 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3879 c2db6724 2019-01-04 stsp if (error != NULL)
3880 c2db6724 2019-01-04 stsp return error;
3881 c2db6724 2019-01-04 stsp
3882 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3883 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3884 c2db6724 2019-01-04 stsp
3885 c2db6724 2019-01-04 stsp return NULL;
3886 c2db6724 2019-01-04 stsp }
3887 c2db6724 2019-01-04 stsp
3888 a915003a 2019-02-05 stsp static void
3889 a915003a 2019-02-05 stsp init_curses(void)
3890 a915003a 2019-02-05 stsp {
3891 2497f032 2022-05-31 stsp /*
3892 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3893 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3894 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3895 2497f032 2022-05-31 stsp */
3896 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3897 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3898 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3899 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3900 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3901 2497f032 2022-05-31 stsp
3902 a915003a 2019-02-05 stsp initscr();
3903 a915003a 2019-02-05 stsp cbreak();
3904 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3905 a915003a 2019-02-05 stsp noecho();
3906 a915003a 2019-02-05 stsp nonl();
3907 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3908 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3909 a915003a 2019-02-05 stsp curs_set(0);
3910 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3911 6d17833f 2019-11-08 stsp start_color();
3912 6d17833f 2019-11-08 stsp use_default_colors();
3913 6d17833f 2019-11-08 stsp }
3914 a915003a 2019-02-05 stsp }
3915 a915003a 2019-02-05 stsp
3916 c2db6724 2019-01-04 stsp static const struct got_error *
3917 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3918 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3919 f135c941 2020-02-20 stsp {
3920 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3921 f135c941 2020-02-20 stsp
3922 f135c941 2020-02-20 stsp if (argc == 0) {
3923 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3924 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3925 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3926 f135c941 2020-02-20 stsp return NULL;
3927 f135c941 2020-02-20 stsp }
3928 f135c941 2020-02-20 stsp
3929 f135c941 2020-02-20 stsp if (worktree) {
3930 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3931 bfd61697 2020-11-14 stsp char *p;
3932 f135c941 2020-02-20 stsp
3933 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3934 f135c941 2020-02-20 stsp if (err)
3935 f135c941 2020-02-20 stsp return err;
3936 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3937 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3938 bfd61697 2020-11-14 stsp p) == -1) {
3939 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3940 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3941 f135c941 2020-02-20 stsp }
3942 f135c941 2020-02-20 stsp free(p);
3943 f135c941 2020-02-20 stsp } else
3944 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3945 f135c941 2020-02-20 stsp
3946 f135c941 2020-02-20 stsp return err;
3947 f135c941 2020-02-20 stsp }
3948 f135c941 2020-02-20 stsp
3949 f135c941 2020-02-20 stsp static const struct got_error *
3950 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3951 9f7d7167 2018-04-29 stsp {
3952 80ddbec8 2018-04-29 stsp const struct got_error *error;
3953 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3954 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3955 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3956 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3957 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3958 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3959 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3960 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3961 04cc582a 2018-08-01 stsp struct tog_view *view;
3962 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3963 80ddbec8 2018-04-29 stsp
3964 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3965 80ddbec8 2018-04-29 stsp switch (ch) {
3966 b672a97a 2020-01-27 stsp case 'b':
3967 b672a97a 2020-01-27 stsp log_branches = 1;
3968 b672a97a 2020-01-27 stsp break;
3969 80ddbec8 2018-04-29 stsp case 'c':
3970 80ddbec8 2018-04-29 stsp start_commit = optarg;
3971 80ddbec8 2018-04-29 stsp break;
3972 ecb28ae0 2018-07-16 stsp case 'r':
3973 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3974 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3975 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3976 9ba1d308 2019-10-21 stsp optarg);
3977 ecb28ae0 2018-07-16 stsp break;
3978 80ddbec8 2018-04-29 stsp default:
3979 17020d27 2019-03-07 stsp usage_log();
3980 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3981 80ddbec8 2018-04-29 stsp }
3982 80ddbec8 2018-04-29 stsp }
3983 80ddbec8 2018-04-29 stsp
3984 80ddbec8 2018-04-29 stsp argc -= optind;
3985 80ddbec8 2018-04-29 stsp argv += optind;
3986 80ddbec8 2018-04-29 stsp
3987 f135c941 2020-02-20 stsp if (argc > 1)
3988 f135c941 2020-02-20 stsp usage_log();
3989 963f97a1 2019-03-18 stsp
3990 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3991 0ae84acc 2022-06-15 tracey if (error != NULL)
3992 0ae84acc 2022-06-15 tracey goto done;
3993 0ae84acc 2022-06-15 tracey
3994 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3995 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3996 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3997 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3998 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3999 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4000 c156c7a4 2020-12-18 stsp goto done;
4001 a1fbf39a 2019-08-11 stsp if (worktree)
4002 6962eb72 2020-02-20 stsp repo_path =
4003 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4004 a1fbf39a 2019-08-11 stsp else
4005 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4006 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4007 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4008 c156c7a4 2020-12-18 stsp goto done;
4009 c156c7a4 2020-12-18 stsp }
4010 ecb28ae0 2018-07-16 stsp }
4011 ecb28ae0 2018-07-16 stsp
4012 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4013 80ddbec8 2018-04-29 stsp if (error != NULL)
4014 ecb28ae0 2018-07-16 stsp goto done;
4015 80ddbec8 2018-04-29 stsp
4016 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4017 f135c941 2020-02-20 stsp repo, worktree);
4018 f135c941 2020-02-20 stsp if (error)
4019 f135c941 2020-02-20 stsp goto done;
4020 f135c941 2020-02-20 stsp
4021 f135c941 2020-02-20 stsp init_curses();
4022 f135c941 2020-02-20 stsp
4023 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4024 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4025 c02c541e 2019-03-29 stsp if (error)
4026 c02c541e 2019-03-29 stsp goto done;
4027 c02c541e 2019-03-29 stsp
4028 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4029 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4030 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4031 87670572 2020-12-26 naddy if (error)
4032 87670572 2020-12-26 naddy goto done;
4033 87670572 2020-12-26 naddy }
4034 51a10b52 2020-12-26 stsp
4035 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4036 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4037 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4038 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4039 d8f38dc4 2020-12-05 stsp if (error)
4040 d8f38dc4 2020-12-05 stsp goto done;
4041 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4042 d8f38dc4 2020-12-05 stsp } else {
4043 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4044 d8f38dc4 2020-12-05 stsp if (error == NULL)
4045 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4046 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4047 d8f38dc4 2020-12-05 stsp goto done;
4048 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4049 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4050 d8f38dc4 2020-12-05 stsp if (error)
4051 d8f38dc4 2020-12-05 stsp goto done;
4052 d8f38dc4 2020-12-05 stsp }
4053 8b473291 2019-02-21 stsp
4054 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4055 04cc582a 2018-08-01 stsp if (view == NULL) {
4056 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4057 04cc582a 2018-08-01 stsp goto done;
4058 04cc582a 2018-08-01 stsp }
4059 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4060 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4061 ba4f502b 2018-08-04 stsp if (error)
4062 ba4f502b 2018-08-04 stsp goto done;
4063 2fc00ff4 2019-08-31 stsp if (worktree) {
4064 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4065 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4066 2fc00ff4 2019-08-31 stsp worktree = NULL;
4067 2fc00ff4 2019-08-31 stsp }
4068 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4069 ecb28ae0 2018-07-16 stsp done:
4070 f135c941 2020-02-20 stsp free(in_repo_path);
4071 ecb28ae0 2018-07-16 stsp free(repo_path);
4072 ecb28ae0 2018-07-16 stsp free(cwd);
4073 899d86c2 2018-05-10 stsp free(start_id);
4074 d8f38dc4 2020-12-05 stsp free(label);
4075 d8f38dc4 2020-12-05 stsp if (ref)
4076 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4077 1d0f4054 2021-06-17 stsp if (repo) {
4078 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4079 1d0f4054 2021-06-17 stsp if (error == NULL)
4080 1d0f4054 2021-06-17 stsp error = close_err;
4081 1d0f4054 2021-06-17 stsp }
4082 ec142235 2019-03-07 stsp if (worktree)
4083 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4084 0ae84acc 2022-06-15 tracey if (pack_fds) {
4085 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4086 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4087 0ae84acc 2022-06-15 tracey if (error == NULL)
4088 0ae84acc 2022-06-15 tracey error = pack_err;
4089 0ae84acc 2022-06-15 tracey }
4090 51a10b52 2020-12-26 stsp tog_free_refs();
4091 80ddbec8 2018-04-29 stsp return error;
4092 9f7d7167 2018-04-29 stsp }
4093 9f7d7167 2018-04-29 stsp
4094 4ed7e80c 2018-05-20 stsp __dead static void
4095 9f7d7167 2018-04-29 stsp usage_diff(void)
4096 9f7d7167 2018-04-29 stsp {
4097 80ddbec8 2018-04-29 stsp endwin();
4098 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4099 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
4100 9f7d7167 2018-04-29 stsp exit(1);
4101 b304db33 2018-05-20 stsp }
4102 b304db33 2018-05-20 stsp
4103 6d17833f 2019-11-08 stsp static int
4104 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4105 41605754 2020-11-12 stsp regmatch_t *regmatch)
4106 6d17833f 2019-11-08 stsp {
4107 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4108 6d17833f 2019-11-08 stsp }
4109 6d17833f 2019-11-08 stsp
4110 336075a4 2022-06-25 op static struct tog_color *
4111 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4112 6d17833f 2019-11-08 stsp {
4113 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4114 6d17833f 2019-11-08 stsp
4115 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4116 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4117 6d17833f 2019-11-08 stsp return tc;
4118 6d17833f 2019-11-08 stsp }
4119 6d17833f 2019-11-08 stsp
4120 6d17833f 2019-11-08 stsp return NULL;
4121 6d17833f 2019-11-08 stsp }
4122 6d17833f 2019-11-08 stsp
4123 4ed7e80c 2018-05-20 stsp static const struct got_error *
4124 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4125 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
4126 41605754 2020-11-12 stsp {
4127 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4128 44a87665 2022-06-16 stsp char *exstr = NULL;
4129 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
4130 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
4131 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
4132 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4133 41605754 2020-11-12 stsp
4134 41605754 2020-11-12 stsp *wtotal = 0;
4135 1853e0f4 2022-06-16 stsp
4136 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
4137 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
4138 41605754 2020-11-12 stsp
4139 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
4140 44a87665 2022-06-16 stsp if (err)
4141 44a87665 2022-06-16 stsp return err;
4142 44a87665 2022-06-16 stsp
4143 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
4144 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
4145 44a87665 2022-06-16 stsp if (seg0 == NULL) {
4146 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
4147 44a87665 2022-06-16 stsp goto done;
4148 44a87665 2022-06-16 stsp }
4149 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
4150 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
4151 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4152 1853e0f4 2022-06-16 stsp goto done;
4153 1853e0f4 2022-06-16 stsp }
4154 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
4155 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
4156 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
4157 1853e0f4 2022-06-16 stsp goto done;
4158 1853e0f4 2022-06-16 stsp }
4159 145b6838 2022-06-16 stsp
4160 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
4161 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4162 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4163 1853e0f4 2022-06-16 stsp if (err)
4164 1853e0f4 2022-06-16 stsp goto done;
4165 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
4166 145b6838 2022-06-16 stsp if (n) {
4167 1853e0f4 2022-06-16 stsp free(wline);
4168 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4169 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4170 1853e0f4 2022-06-16 stsp if (err)
4171 1853e0f4 2022-06-16 stsp goto done;
4172 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4173 1853e0f4 2022-06-16 stsp wlimit -= width;
4174 1853e0f4 2022-06-16 stsp *wtotal += width;
4175 41605754 2020-11-12 stsp }
4176 41605754 2020-11-12 stsp
4177 41605754 2020-11-12 stsp if (wlimit > 0) {
4178 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
4179 1853e0f4 2022-06-16 stsp size_t wlen;
4180 1853e0f4 2022-06-16 stsp
4181 1853e0f4 2022-06-16 stsp free(wline);
4182 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4183 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4184 1853e0f4 2022-06-16 stsp if (err)
4185 1853e0f4 2022-06-16 stsp goto done;
4186 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
4187 1853e0f4 2022-06-16 stsp while (i < wlen) {
4188 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
4189 1853e0f4 2022-06-16 stsp if (width == -1) {
4190 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
4191 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
4192 1853e0f4 2022-06-16 stsp goto done;
4193 1853e0f4 2022-06-16 stsp }
4194 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
4195 1853e0f4 2022-06-16 stsp break;
4196 61417565 2022-06-20 mark w += width;
4197 1853e0f4 2022-06-16 stsp i++;
4198 41605754 2020-11-12 stsp }
4199 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
4200 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
4201 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
4202 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
4203 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
4204 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
4205 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
4206 41605754 2020-11-12 stsp }
4207 41605754 2020-11-12 stsp }
4208 41605754 2020-11-12 stsp
4209 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
4210 1853e0f4 2022-06-16 stsp free(wline);
4211 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
4212 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
4213 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
4214 1853e0f4 2022-06-16 stsp col_tab_align, 1);
4215 1853e0f4 2022-06-16 stsp if (err)
4216 1853e0f4 2022-06-16 stsp goto done;
4217 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
4218 1853e0f4 2022-06-16 stsp } else {
4219 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
4220 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
4221 1853e0f4 2022-06-16 stsp if (err)
4222 1853e0f4 2022-06-16 stsp goto done;
4223 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
4224 1853e0f4 2022-06-16 stsp }
4225 1853e0f4 2022-06-16 stsp *wtotal += width2;
4226 41605754 2020-11-12 stsp }
4227 1853e0f4 2022-06-16 stsp done:
4228 145b6838 2022-06-16 stsp free(wline);
4229 44a87665 2022-06-16 stsp free(exstr);
4230 1853e0f4 2022-06-16 stsp free(seg0);
4231 1853e0f4 2022-06-16 stsp free(seg1);
4232 1853e0f4 2022-06-16 stsp free(seg2);
4233 1853e0f4 2022-06-16 stsp return err;
4234 41605754 2020-11-12 stsp }
4235 94b80cfa 2022-08-01 mark
4236 94b80cfa 2022-08-01 mark static int
4237 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
4238 94b80cfa 2022-08-01 mark {
4239 94b80cfa 2022-08-01 mark FILE *f = NULL;
4240 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
4241 94b80cfa 2022-08-01 mark
4242 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
4243 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
4244 ec2a9698 2022-09-15 mark
4245 ec2a9698 2022-09-15 mark first = &s->first_displayed_line;
4246 ec2a9698 2022-09-15 mark selected = first;
4247 ec2a9698 2022-09-15 mark eof = &s->eof;
4248 ec2a9698 2022-09-15 mark f = s->f;
4249 ec2a9698 2022-09-15 mark } else if (view->type == TOG_VIEW_HELP) {
4250 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
4251 41605754 2020-11-12 stsp
4252 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4253 94b80cfa 2022-08-01 mark selected = first;
4254 94b80cfa 2022-08-01 mark eof = &s->eof;
4255 94b80cfa 2022-08-01 mark f = s->f;
4256 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
4257 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
4258 94b80cfa 2022-08-01 mark
4259 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
4260 94b80cfa 2022-08-01 mark selected = &s->selected_line;
4261 94b80cfa 2022-08-01 mark eof = &s->eof;
4262 94b80cfa 2022-08-01 mark f = s->blame.f;
4263 94b80cfa 2022-08-01 mark } else
4264 94b80cfa 2022-08-01 mark return 0;
4265 94b80cfa 2022-08-01 mark
4266 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
4267 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
4268 94b80cfa 2022-08-01 mark return 0;
4269 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4270 94b80cfa 2022-08-01 mark rewind(f);
4271 94b80cfa 2022-08-01 mark *eof = 0;
4272 94b80cfa 2022-08-01 mark *first = 1;
4273 94b80cfa 2022-08-01 mark *lineno = 0;
4274 94b80cfa 2022-08-01 mark *nprinted = 0;
4275 94b80cfa 2022-08-01 mark return 0;
4276 94b80cfa 2022-08-01 mark }
4277 94b80cfa 2022-08-01 mark
4278 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
4279 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
4280 94b80cfa 2022-08-01 mark view->gline = 0;
4281 94b80cfa 2022-08-01 mark
4282 94b80cfa 2022-08-01 mark return 1;
4283 94b80cfa 2022-08-01 mark }
4284 94b80cfa 2022-08-01 mark
4285 41605754 2020-11-12 stsp static const struct got_error *
4286 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4287 26ed57b2 2018-05-19 stsp {
4288 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4289 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4290 61e69b96 2018-05-20 stsp const struct got_error *err;
4291 c7d5c43c 2022-08-04 mark int nprinted = 0;
4292 b304db33 2018-05-20 stsp char *line;
4293 826082fe 2020-12-10 stsp size_t linesize = 0;
4294 826082fe 2020-12-10 stsp ssize_t linelen;
4295 61e69b96 2018-05-20 stsp wchar_t *wline;
4296 e0b650dd 2018-05-20 stsp int width;
4297 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4298 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4299 fe621944 2020-11-10 stsp off_t line_offset;
4300 26ed57b2 2018-05-19 stsp
4301 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
4302 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
4303 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4304 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4305 fe621944 2020-11-10 stsp
4306 f7d12f7e 2018-08-01 stsp werase(view->window);
4307 a3404814 2018-09-02 stsp
4308 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
4309 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
4310 94b80cfa 2022-08-01 mark
4311 a3404814 2018-09-02 stsp if (header) {
4312 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4313 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
4314 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
4315 94b80cfa 2022-08-01 mark
4316 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4317 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4318 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4319 ccda2f4d 2022-06-16 stsp 0, 0);
4320 135a2da0 2020-11-11 stsp free(line);
4321 135a2da0 2020-11-11 stsp if (err)
4322 a3404814 2018-09-02 stsp return err;
4323 a3404814 2018-09-02 stsp
4324 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4325 a3404814 2018-09-02 stsp wstandout(view->window);
4326 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4327 e54cc94a 2020-11-11 stsp free(wline);
4328 e54cc94a 2020-11-11 stsp wline = NULL;
4329 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
4330 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
4331 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4332 a3404814 2018-09-02 stsp wstandend(view->window);
4333 26ed57b2 2018-05-19 stsp
4334 a3404814 2018-09-02 stsp if (max_lines <= 1)
4335 a3404814 2018-09-02 stsp return NULL;
4336 a3404814 2018-09-02 stsp max_lines--;
4337 a3404814 2018-09-02 stsp }
4338 a3404814 2018-09-02 stsp
4339 89f1a395 2020-12-01 naddy s->eof = 0;
4340 145b6838 2022-06-16 stsp view->maxx = 0;
4341 826082fe 2020-12-10 stsp line = NULL;
4342 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4343 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
4344 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
4345 6a5ff2d4 2022-08-06 mark
4346 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4347 826082fe 2020-12-10 stsp if (linelen == -1) {
4348 826082fe 2020-12-10 stsp if (feof(s->f)) {
4349 826082fe 2020-12-10 stsp s->eof = 1;
4350 826082fe 2020-12-10 stsp break;
4351 826082fe 2020-12-10 stsp }
4352 826082fe 2020-12-10 stsp free(line);
4353 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4354 61e69b96 2018-05-20 stsp }
4355 145b6838 2022-06-16 stsp
4356 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4357 94b80cfa 2022-08-01 mark continue;
4358 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4359 94b80cfa 2022-08-01 mark continue;
4360 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4361 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4362 94b80cfa 2022-08-01 mark
4363 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4364 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4365 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4366 1853e0f4 2022-06-16 stsp if (err) {
4367 1853e0f4 2022-06-16 stsp free(line);
4368 1853e0f4 2022-06-16 stsp return err;
4369 1853e0f4 2022-06-16 stsp }
4370 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4371 1853e0f4 2022-06-16 stsp free(wline);
4372 1853e0f4 2022-06-16 stsp wline = NULL;
4373 1853e0f4 2022-06-16 stsp
4374 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4375 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4376 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4377 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4378 94b80cfa 2022-08-01 mark if (attr)
4379 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4380 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4381 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4382 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4383 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4384 41605754 2020-11-12 stsp if (err) {
4385 41605754 2020-11-12 stsp free(line);
4386 41605754 2020-11-12 stsp return err;
4387 41605754 2020-11-12 stsp }
4388 41605754 2020-11-12 stsp } else {
4389 969c159c 2022-06-16 stsp int skip;
4390 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4391 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4392 969c159c 2022-06-16 stsp if (err) {
4393 969c159c 2022-06-16 stsp free(line);
4394 969c159c 2022-06-16 stsp return err;
4395 969c159c 2022-06-16 stsp }
4396 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4397 969c159c 2022-06-16 stsp free(wline);
4398 41605754 2020-11-12 stsp wline = NULL;
4399 41605754 2020-11-12 stsp }
4400 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4401 94b80cfa 2022-08-01 mark /* highlight full gline length */
4402 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4403 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4404 94b80cfa 2022-08-01 mark } else {
4405 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4406 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4407 94b80cfa 2022-08-01 mark }
4408 94b80cfa 2022-08-01 mark if (attr)
4409 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4410 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4411 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4412 826082fe 2020-12-10 stsp }
4413 826082fe 2020-12-10 stsp free(line);
4414 fe621944 2020-11-10 stsp if (nprinted >= 1)
4415 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4416 89f1a395 2020-12-01 naddy (nprinted - 1);
4417 fe621944 2020-11-10 stsp else
4418 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4419 26ed57b2 2018-05-19 stsp
4420 9b058f45 2022-06-30 mark view_border(view);
4421 c3e9aa98 2019-05-13 jcs
4422 89f1a395 2020-12-01 naddy if (s->eof) {
4423 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4424 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4425 c3e9aa98 2019-05-13 jcs nprinted++;
4426 c3e9aa98 2019-05-13 jcs }
4427 c3e9aa98 2019-05-13 jcs
4428 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4429 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4430 c3e9aa98 2019-05-13 jcs if (err) {
4431 c3e9aa98 2019-05-13 jcs return err;
4432 c3e9aa98 2019-05-13 jcs }
4433 26ed57b2 2018-05-19 stsp
4434 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4435 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4436 e54cc94a 2020-11-11 stsp free(wline);
4437 e54cc94a 2020-11-11 stsp wline = NULL;
4438 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4439 c3e9aa98 2019-05-13 jcs }
4440 c3e9aa98 2019-05-13 jcs
4441 26ed57b2 2018-05-19 stsp return NULL;
4442 abd2672a 2018-12-23 stsp }
4443 abd2672a 2018-12-23 stsp
4444 abd2672a 2018-12-23 stsp static char *
4445 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4446 abd2672a 2018-12-23 stsp {
4447 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4448 09867e48 2019-08-13 stsp char *p, *s;
4449 09867e48 2019-08-13 stsp
4450 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4451 09867e48 2019-08-13 stsp if (tm == NULL)
4452 09867e48 2019-08-13 stsp return NULL;
4453 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4454 09867e48 2019-08-13 stsp if (s == NULL)
4455 09867e48 2019-08-13 stsp return NULL;
4456 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4457 abd2672a 2018-12-23 stsp if (p)
4458 abd2672a 2018-12-23 stsp *p = '\0';
4459 abd2672a 2018-12-23 stsp return s;
4460 9f7d7167 2018-04-29 stsp }
4461 9f7d7167 2018-04-29 stsp
4462 4ed7e80c 2018-05-20 stsp static const struct got_error *
4463 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4464 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4465 abd2672a 2018-12-23 stsp {
4466 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4467 fe621944 2020-11-10 stsp
4468 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4469 fe621944 2020-11-10 stsp if (p == NULL)
4470 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4471 c7d5c43c 2022-08-04 mark *lines = p;
4472 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4473 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4474 fe621944 2020-11-10 stsp (*nlines)++;
4475 c7d5c43c 2022-08-04 mark
4476 fe621944 2020-11-10 stsp return NULL;
4477 fe621944 2020-11-10 stsp }
4478 fe621944 2020-11-10 stsp
4479 fe621944 2020-11-10 stsp static const struct got_error *
4480 1f3405c9 2023-01-17 mark cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4481 1f3405c9 2023-01-17 mark struct got_diff_line *s_lines, size_t s_nlines)
4482 1f3405c9 2023-01-17 mark {
4483 1f3405c9 2023-01-17 mark struct got_diff_line *p;
4484 1f3405c9 2023-01-17 mark char buf[BUFSIZ];
4485 1f3405c9 2023-01-17 mark size_t i, r;
4486 1f3405c9 2023-01-17 mark
4487 1f3405c9 2023-01-17 mark if (fseeko(src, 0L, SEEK_SET) == -1)
4488 1f3405c9 2023-01-17 mark return got_error_from_errno("fseeko");
4489 1f3405c9 2023-01-17 mark
4490 1f3405c9 2023-01-17 mark for (;;) {
4491 1f3405c9 2023-01-17 mark r = fread(buf, 1, sizeof(buf), src);
4492 1f3405c9 2023-01-17 mark if (r == 0) {
4493 1f3405c9 2023-01-17 mark if (ferror(src))
4494 1f3405c9 2023-01-17 mark return got_error_from_errno("fread");
4495 1f3405c9 2023-01-17 mark if (feof(src))
4496 1f3405c9 2023-01-17 mark break;
4497 1f3405c9 2023-01-17 mark }
4498 1f3405c9 2023-01-17 mark if (fwrite(buf, 1, r, dst) != r)
4499 1f3405c9 2023-01-17 mark return got_ferror(dst, GOT_ERR_IO);
4500 1f3405c9 2023-01-17 mark }
4501 1f3405c9 2023-01-17 mark
4502 1f3405c9 2023-01-17 mark /*
4503 1f3405c9 2023-01-17 mark * The diff driver initialises the first line at offset zero when the
4504 1f3405c9 2023-01-17 mark * array isn't prepopulated, skip it; we already have it in *d_lines.
4505 1f3405c9 2023-01-17 mark */
4506 1f3405c9 2023-01-17 mark for (i = 1; i < s_nlines; ++i)
4507 1f3405c9 2023-01-17 mark s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4508 1f3405c9 2023-01-17 mark
4509 1f3405c9 2023-01-17 mark --s_nlines;
4510 1f3405c9 2023-01-17 mark
4511 1f3405c9 2023-01-17 mark p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4512 1f3405c9 2023-01-17 mark if (p == NULL) {
4513 1f3405c9 2023-01-17 mark /* d_lines is freed in close_diff_view() */
4514 1f3405c9 2023-01-17 mark return got_error_from_errno("reallocarray");
4515 1f3405c9 2023-01-17 mark }
4516 1f3405c9 2023-01-17 mark
4517 1f3405c9 2023-01-17 mark *d_lines = p;
4518 1f3405c9 2023-01-17 mark
4519 1f3405c9 2023-01-17 mark memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4520 1f3405c9 2023-01-17 mark *d_nlines += s_nlines;
4521 1f3405c9 2023-01-17 mark
4522 1f3405c9 2023-01-17 mark return NULL;
4523 1f3405c9 2023-01-17 mark }
4524 1f3405c9 2023-01-17 mark
4525 1f3405c9 2023-01-17 mark static const struct got_error *
4526 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4527 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4528 5191b70b 2023-01-07 mark struct got_repository *repo, int ignore_ws, int force_text_diff,
4529 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg *dsa, FILE *outfile)
4530 fe621944 2020-11-10 stsp {
4531 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4532 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4533 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4534 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4535 45d799e2 2018-12-23 stsp time_t committer_time;
4536 45d799e2 2018-12-23 stsp const char *author, *committer;
4537 8b473291 2019-02-21 stsp char *refs_str = NULL;
4538 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4539 fe621944 2020-11-10 stsp off_t outoff = 0;
4540 fe621944 2020-11-10 stsp int n;
4541 abd2672a 2018-12-23 stsp
4542 8b473291 2019-02-21 stsp if (refs) {
4543 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4544 8b473291 2019-02-21 stsp if (err)
4545 8b473291 2019-02-21 stsp return err;
4546 8b473291 2019-02-21 stsp }
4547 8b473291 2019-02-21 stsp
4548 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4549 abd2672a 2018-12-23 stsp if (err)
4550 abd2672a 2018-12-23 stsp return err;
4551 abd2672a 2018-12-23 stsp
4552 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4553 15a94983 2018-12-23 stsp if (err) {
4554 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4555 15a94983 2018-12-23 stsp goto done;
4556 15a94983 2018-12-23 stsp }
4557 abd2672a 2018-12-23 stsp
4558 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4559 fe621944 2020-11-10 stsp if (err)
4560 fe621944 2020-11-10 stsp goto done;
4561 fe621944 2020-11-10 stsp
4562 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4563 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4564 fe621944 2020-11-10 stsp if (n < 0) {
4565 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4566 abd2672a 2018-12-23 stsp goto done;
4567 abd2672a 2018-12-23 stsp }
4568 fe621944 2020-11-10 stsp outoff += n;
4569 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4570 fe621944 2020-11-10 stsp if (err)
4571 fe621944 2020-11-10 stsp goto done;
4572 fe621944 2020-11-10 stsp
4573 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4574 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4575 fe621944 2020-11-10 stsp if (n < 0) {
4576 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4577 abd2672a 2018-12-23 stsp goto done;
4578 abd2672a 2018-12-23 stsp }
4579 fe621944 2020-11-10 stsp outoff += n;
4580 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4581 fe621944 2020-11-10 stsp if (err)
4582 fe621944 2020-11-10 stsp goto done;
4583 fe621944 2020-11-10 stsp
4584 1a99e0b4 2023-01-07 stsp author = got_object_commit_get_author(commit);
4585 1a99e0b4 2023-01-07 stsp committer = got_object_commit_get_committer(commit);
4586 1a99e0b4 2023-01-07 stsp if (strcmp(author, committer) != 0) {
4587 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "via: %s\n", committer);
4588 fe621944 2020-11-10 stsp if (n < 0) {
4589 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4590 fe621944 2020-11-10 stsp goto done;
4591 fe621944 2020-11-10 stsp }
4592 fe621944 2020-11-10 stsp outoff += n;
4593 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4594 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_AUTHOR);
4595 fe621944 2020-11-10 stsp if (err)
4596 fe621944 2020-11-10 stsp goto done;
4597 abd2672a 2018-12-23 stsp }
4598 1a99e0b4 2023-01-07 stsp committer_time = got_object_commit_get_committer_time(commit);
4599 1a99e0b4 2023-01-07 stsp datestr = get_datestr(&committer_time, datebuf);
4600 1a99e0b4 2023-01-07 stsp if (datestr) {
4601 1a99e0b4 2023-01-07 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4602 fe621944 2020-11-10 stsp if (n < 0) {
4603 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4604 fe621944 2020-11-10 stsp goto done;
4605 fe621944 2020-11-10 stsp }
4606 fe621944 2020-11-10 stsp outoff += n;
4607 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4608 1a99e0b4 2023-01-07 stsp GOT_DIFF_LINE_DATE);
4609 fe621944 2020-11-10 stsp if (err)
4610 fe621944 2020-11-10 stsp goto done;
4611 abd2672a 2018-12-23 stsp }
4612 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4613 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4614 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4615 9f98ca05 2021-09-24 stsp int pn = 1;
4616 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4617 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4618 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4619 9f98ca05 2021-09-24 stsp if (err)
4620 9f98ca05 2021-09-24 stsp goto done;
4621 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4622 9f98ca05 2021-09-24 stsp if (n < 0) {
4623 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4624 9f98ca05 2021-09-24 stsp goto done;
4625 9f98ca05 2021-09-24 stsp }
4626 9f98ca05 2021-09-24 stsp outoff += n;
4627 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4628 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4629 9f98ca05 2021-09-24 stsp if (err)
4630 9f98ca05 2021-09-24 stsp goto done;
4631 9f98ca05 2021-09-24 stsp free(id_str);
4632 9f98ca05 2021-09-24 stsp id_str = NULL;
4633 9f98ca05 2021-09-24 stsp }
4634 9f98ca05 2021-09-24 stsp }
4635 9f98ca05 2021-09-24 stsp
4636 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4637 5943eee2 2019-08-13 stsp if (err)
4638 5943eee2 2019-08-13 stsp goto done;
4639 fe621944 2020-11-10 stsp s = logmsg;
4640 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4641 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4642 fe621944 2020-11-10 stsp if (n < 0) {
4643 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4644 fe621944 2020-11-10 stsp goto done;
4645 fe621944 2020-11-10 stsp }
4646 fe621944 2020-11-10 stsp outoff += n;
4647 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4648 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4649 fe621944 2020-11-10 stsp if (err)
4650 fe621944 2020-11-10 stsp goto done;
4651 abd2672a 2018-12-23 stsp }
4652 fe621944 2020-11-10 stsp
4653 1f3405c9 2023-01-17 mark TAILQ_FOREACH(pe, dsa->paths, entry) {
4654 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4655 1f3405c9 2023-01-17 mark int pad = dsa->max_path_len - pe->path_len + 1;
4656 5191b70b 2023-01-07 mark
4657 5191b70b 2023-01-07 mark n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4658 1f3405c9 2023-01-17 mark pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4659 1f3405c9 2023-01-17 mark dsa->rm_cols + 1, cp->rm);
4660 fe621944 2020-11-10 stsp if (n < 0) {
4661 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4662 fe621944 2020-11-10 stsp goto done;
4663 fe621944 2020-11-10 stsp }
4664 fe621944 2020-11-10 stsp outoff += n;
4665 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4666 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4667 fe621944 2020-11-10 stsp if (err)
4668 fe621944 2020-11-10 stsp goto done;
4669 0208f208 2020-05-05 stsp }
4670 fe621944 2020-11-10 stsp
4671 0208f208 2020-05-05 stsp fputc('\n', outfile);
4672 fe621944 2020-11-10 stsp outoff++;
4673 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4674 5191b70b 2023-01-07 mark if (err)
4675 5191b70b 2023-01-07 mark goto done;
4676 5191b70b 2023-01-07 mark
4677 5191b70b 2023-01-07 mark n = fprintf(outfile,
4678 65dedee0 2023-01-15 mark "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4679 1f3405c9 2023-01-17 mark dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4680 1f3405c9 2023-01-17 mark dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4681 5191b70b 2023-01-07 mark if (n < 0) {
4682 5191b70b 2023-01-07 mark err = got_error_from_errno("fprintf");
4683 5191b70b 2023-01-07 mark goto done;
4684 5191b70b 2023-01-07 mark }
4685 5191b70b 2023-01-07 mark outoff += n;
4686 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4687 5191b70b 2023-01-07 mark if (err)
4688 5191b70b 2023-01-07 mark goto done;
4689 5191b70b 2023-01-07 mark
4690 5191b70b 2023-01-07 mark fputc('\n', outfile);
4691 5191b70b 2023-01-07 mark outoff++;
4692 5191b70b 2023-01-07 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4693 abd2672a 2018-12-23 stsp done:
4694 abd2672a 2018-12-23 stsp free(id_str);
4695 5943eee2 2019-08-13 stsp free(logmsg);
4696 8b473291 2019-02-21 stsp free(refs_str);
4697 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4698 7510f233 2020-08-09 stsp if (err) {
4699 c7d5c43c 2022-08-04 mark free(*lines);
4700 c7d5c43c 2022-08-04 mark *lines = NULL;
4701 ae6a6978 2020-08-09 stsp *nlines = 0;
4702 7510f233 2020-08-09 stsp }
4703 fe621944 2020-11-10 stsp return err;
4704 abd2672a 2018-12-23 stsp }
4705 abd2672a 2018-12-23 stsp
4706 abd2672a 2018-12-23 stsp static const struct got_error *
4707 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4708 26ed57b2 2018-05-19 stsp {
4709 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4710 1f3405c9 2023-01-17 mark FILE *f = NULL, *tmp_diff_file = NULL;
4711 15a94983 2018-12-23 stsp int obj_type;
4712 1f3405c9 2023-01-17 mark struct got_diff_line *lines = NULL;
4713 1f3405c9 2023-01-17 mark struct got_pathlist_head changed_paths;
4714 fe621944 2020-11-10 stsp
4715 1f3405c9 2023-01-17 mark TAILQ_INIT(&changed_paths);
4716 1f3405c9 2023-01-17 mark
4717 c7d5c43c 2022-08-04 mark free(s->lines);
4718 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4719 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4720 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4721 fe621944 2020-11-10 stsp s->nlines = 0;
4722 26ed57b2 2018-05-19 stsp
4723 511a516b 2018-05-19 stsp f = got_opentemp();
4724 48ae06ee 2018-10-18 stsp if (f == NULL) {
4725 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4726 48ae06ee 2018-10-18 stsp goto done;
4727 48ae06ee 2018-10-18 stsp }
4728 1f3405c9 2023-01-17 mark tmp_diff_file = got_opentemp();
4729 1f3405c9 2023-01-17 mark if (tmp_diff_file == NULL) {
4730 1f3405c9 2023-01-17 mark err = got_error_from_errno("got_opentemp");
4731 1f3405c9 2023-01-17 mark goto done;
4732 1f3405c9 2023-01-17 mark }
4733 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4734 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4735 fb43ecf1 2019-02-11 stsp goto done;
4736 fb43ecf1 2019-02-11 stsp }
4737 48ae06ee 2018-10-18 stsp s->f = f;
4738 26ed57b2 2018-05-19 stsp
4739 15a94983 2018-12-23 stsp if (s->id1)
4740 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4741 15a94983 2018-12-23 stsp else
4742 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4743 15a94983 2018-12-23 stsp if (err)
4744 15a94983 2018-12-23 stsp goto done;
4745 15a94983 2018-12-23 stsp
4746 15a94983 2018-12-23 stsp switch (obj_type) {
4747 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4748 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4749 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4750 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4751 1f3405c9 2023-01-17 mark s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4752 a76e88e5 2023-01-10 mark s->f);
4753 26ed57b2 2018-05-19 stsp break;
4754 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4755 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4756 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4757 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4758 1f3405c9 2023-01-17 mark s->force_text_diff, NULL, s->repo, s->f);
4759 26ed57b2 2018-05-19 stsp break;
4760 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4761 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4762 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4763 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4764 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4765 1f3405c9 2023-01-17 mark size_t nlines = 0;
4766 1f3405c9 2023-01-17 mark struct got_diffstat_cb_arg dsa = {
4767 1f3405c9 2023-01-17 mark 0, 0, 0, 0, 0, 0,
4768 1f3405c9 2023-01-17 mark &changed_paths,
4769 1f3405c9 2023-01-17 mark s->ignore_whitespace,
4770 1f3405c9 2023-01-17 mark s->force_text_diff,
4771 1f3405c9 2023-01-17 mark tog_diff_algo
4772 1f3405c9 2023-01-17 mark };
4773 abd2672a 2018-12-23 stsp
4774 1f3405c9 2023-01-17 mark lines = malloc(sizeof(*lines));
4775 1f3405c9 2023-01-17 mark if (lines == NULL) {
4776 1f3405c9 2023-01-17 mark err = got_error_from_errno("malloc");
4777 1f3405c9 2023-01-17 mark goto done;
4778 1f3405c9 2023-01-17 mark }
4779 1f3405c9 2023-01-17 mark
4780 1f3405c9 2023-01-17 mark /* build diff first in tmp file then append to commit info */
4781 1f3405c9 2023-01-17 mark err = got_diff_objects_as_commits(&lines, &nlines,
4782 1f3405c9 2023-01-17 mark s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4783 1f3405c9 2023-01-17 mark tog_diff_algo, s->diff_context, s->ignore_whitespace,
4784 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4785 1f3405c9 2023-01-17 mark if (err)
4786 1f3405c9 2023-01-17 mark break;
4787 1f3405c9 2023-01-17 mark
4788 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4789 abd2672a 2018-12-23 stsp if (err)
4790 3ffacbe1 2020-02-02 tracey goto done;
4791 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4792 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4793 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4794 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4795 5191b70b 2023-01-07 mark refs, s->repo, s->ignore_whitespace,
4796 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4797 f44b1f58 2020-02-02 tracey if (err)
4798 f44b1f58 2020-02-02 tracey goto done;
4799 f44b1f58 2020-02-02 tracey } else {
4800 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4801 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4802 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4803 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4804 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4805 5191b70b 2023-01-07 mark s->ignore_whitespace,
4806 1f3405c9 2023-01-17 mark s->force_text_diff, &dsa, s->f);
4807 f44b1f58 2020-02-02 tracey if (err)
4808 f44b1f58 2020-02-02 tracey goto done;
4809 f5404e4e 2020-02-02 tracey break;
4810 15a087fe 2019-02-21 stsp }
4811 abd2672a 2018-12-23 stsp }
4812 abd2672a 2018-12-23 stsp }
4813 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4814 abd2672a 2018-12-23 stsp
4815 1f3405c9 2023-01-17 mark err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4816 1f3405c9 2023-01-17 mark lines, nlines);
4817 26ed57b2 2018-05-19 stsp break;
4818 abd2672a 2018-12-23 stsp }
4819 26ed57b2 2018-05-19 stsp default:
4820 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4821 48ae06ee 2018-10-18 stsp break;
4822 26ed57b2 2018-05-19 stsp }
4823 48ae06ee 2018-10-18 stsp done:
4824 1f3405c9 2023-01-17 mark free(lines);
4825 1f3405c9 2023-01-17 mark got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4826 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4827 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4828 1f3405c9 2023-01-17 mark if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4829 1f3405c9 2023-01-17 mark err = got_error_from_errno("fclose");
4830 48ae06ee 2018-10-18 stsp return err;
4831 48ae06ee 2018-10-18 stsp }
4832 26ed57b2 2018-05-19 stsp
4833 f5215bb9 2019-02-22 stsp static void
4834 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4835 f5215bb9 2019-02-22 stsp {
4836 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4837 f5215bb9 2019-02-22 stsp update_panels();
4838 f5215bb9 2019-02-22 stsp doupdate();
4839 f44b1f58 2020-02-02 tracey }
4840 f44b1f58 2020-02-02 tracey
4841 f44b1f58 2020-02-02 tracey static const struct got_error *
4842 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4843 f44b1f58 2020-02-02 tracey {
4844 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4845 f44b1f58 2020-02-02 tracey
4846 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4847 f44b1f58 2020-02-02 tracey return NULL;
4848 f44b1f58 2020-02-02 tracey }
4849 f44b1f58 2020-02-02 tracey
4850 eaf2c13e 2022-09-17 mark static void
4851 eaf2c13e 2022-09-17 mark search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4852 ec2a9698 2022-09-15 mark size_t *nlines, int **first, int **last, int **match, int **selected)
4853 f44b1f58 2020-02-02 tracey {
4854 eaf2c13e 2022-09-17 mark struct tog_diff_view_state *s = &view->state.diff;
4855 ec2a9698 2022-09-15 mark
4856 eaf2c13e 2022-09-17 mark *f = s->f;
4857 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
4858 eaf2c13e 2022-09-17 mark *line_offsets = NULL;
4859 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
4860 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
4861 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
4862 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
4863 ec2a9698 2022-09-15 mark }
4864 ec2a9698 2022-09-15 mark
4865 ec2a9698 2022-09-15 mark static const struct got_error *
4866 ec2a9698 2022-09-15 mark search_next_view_match(struct tog_view *view)
4867 ec2a9698 2022-09-15 mark {
4868 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4869 ec2a9698 2022-09-15 mark FILE *f;
4870 f44b1f58 2020-02-02 tracey int lineno;
4871 cb713507 2022-06-16 stsp char *line = NULL;
4872 826082fe 2020-12-10 stsp size_t linesize = 0;
4873 826082fe 2020-12-10 stsp ssize_t linelen;
4874 ec2a9698 2022-09-15 mark off_t *line_offsets;
4875 ec2a9698 2022-09-15 mark size_t nlines = 0;
4876 ec2a9698 2022-09-15 mark int *first, *last, *match, *selected;
4877 f44b1f58 2020-02-02 tracey
4878 eaf2c13e 2022-09-17 mark if (!view->search_setup)
4879 eaf2c13e 2022-09-17 mark return got_error_msg(GOT_ERR_NOT_IMPL,
4880 eaf2c13e 2022-09-17 mark "view search not supported");
4881 eaf2c13e 2022-09-17 mark view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4882 ec2a9698 2022-09-15 mark &match, &selected);
4883 ec2a9698 2022-09-15 mark
4884 f44b1f58 2020-02-02 tracey if (!view->searching) {
4885 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4886 f44b1f58 2020-02-02 tracey return NULL;
4887 f44b1f58 2020-02-02 tracey }
4888 f44b1f58 2020-02-02 tracey
4889 ec2a9698 2022-09-15 mark if (*match) {
4890 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4891 ec2a9698 2022-09-15 mark lineno = *match + 1;
4892 f44b1f58 2020-02-02 tracey else
4893 ec2a9698 2022-09-15 mark lineno = *match - 1;
4894 487cd7d2 2021-12-17 stsp } else
4895 ec2a9698 2022-09-15 mark lineno = *first - 1 + *selected;
4896 f44b1f58 2020-02-02 tracey
4897 f44b1f58 2020-02-02 tracey while (1) {
4898 f44b1f58 2020-02-02 tracey off_t offset;
4899 f44b1f58 2020-02-02 tracey
4900 ec2a9698 2022-09-15 mark if (lineno <= 0 || lineno > nlines) {
4901 ec2a9698 2022-09-15 mark if (*match == 0) {
4902 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4903 f44b1f58 2020-02-02 tracey break;
4904 f44b1f58 2020-02-02 tracey }
4905 f44b1f58 2020-02-02 tracey
4906 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4907 f44b1f58 2020-02-02 tracey lineno = 1;
4908 f44b1f58 2020-02-02 tracey else
4909 ec2a9698 2022-09-15 mark lineno = nlines;
4910 f44b1f58 2020-02-02 tracey }
4911 f44b1f58 2020-02-02 tracey
4912 ec2a9698 2022-09-15 mark offset = view->type == TOG_VIEW_DIFF ?
4913 ec2a9698 2022-09-15 mark view->state.diff.lines[lineno - 1].offset :
4914 ec2a9698 2022-09-15 mark line_offsets[lineno - 1];
4915 ec2a9698 2022-09-15 mark if (fseeko(f, offset, SEEK_SET) != 0) {
4916 f44b1f58 2020-02-02 tracey free(line);
4917 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4918 f44b1f58 2020-02-02 tracey }
4919 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesize, f);
4920 cb713507 2022-06-16 stsp if (linelen != -1) {
4921 cb713507 2022-06-16 stsp char *exstr;
4922 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4923 cb713507 2022-06-16 stsp if (err)
4924 cb713507 2022-06-16 stsp break;
4925 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4926 cb713507 2022-06-16 stsp &view->regmatch)) {
4927 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4928 ec2a9698 2022-09-15 mark *match = lineno;
4929 cb713507 2022-06-16 stsp free(exstr);
4930 cb713507 2022-06-16 stsp break;
4931 cb713507 2022-06-16 stsp }
4932 cb713507 2022-06-16 stsp free(exstr);
4933 f44b1f58 2020-02-02 tracey }
4934 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4935 f44b1f58 2020-02-02 tracey lineno++;
4936 f44b1f58 2020-02-02 tracey else
4937 f44b1f58 2020-02-02 tracey lineno--;
4938 f44b1f58 2020-02-02 tracey }
4939 826082fe 2020-12-10 stsp free(line);
4940 f44b1f58 2020-02-02 tracey
4941 ec2a9698 2022-09-15 mark if (*match) {
4942 ec2a9698 2022-09-15 mark *first = *match;
4943 ec2a9698 2022-09-15 mark *selected = 1;
4944 f44b1f58 2020-02-02 tracey }
4945 f44b1f58 2020-02-02 tracey
4946 145b6838 2022-06-16 stsp return err;
4947 f5215bb9 2019-02-22 stsp }
4948 f5215bb9 2019-02-22 stsp
4949 48ae06ee 2018-10-18 stsp static const struct got_error *
4950 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4951 b72706c3 2022-06-01 stsp {
4952 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4953 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4954 b72706c3 2022-06-01 stsp
4955 b72706c3 2022-06-01 stsp free(s->id1);
4956 b72706c3 2022-06-01 stsp s->id1 = NULL;
4957 b72706c3 2022-06-01 stsp free(s->id2);
4958 b72706c3 2022-06-01 stsp s->id2 = NULL;
4959 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4960 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4961 b72706c3 2022-06-01 stsp s->f = NULL;
4962 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4963 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4964 b72706c3 2022-06-01 stsp s->f1 = NULL;
4965 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4966 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4967 b72706c3 2022-06-01 stsp s->f2 = NULL;
4968 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4969 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4970 f9d37699 2022-06-28 stsp s->fd1 = -1;
4971 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4972 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4973 f9d37699 2022-06-28 stsp s->fd2 = -1;
4974 c7d5c43c 2022-08-04 mark free(s->lines);
4975 c7d5c43c 2022-08-04 mark s->lines = NULL;
4976 b72706c3 2022-06-01 stsp s->nlines = 0;
4977 b72706c3 2022-06-01 stsp return err;
4978 b72706c3 2022-06-01 stsp }
4979 b72706c3 2022-06-01 stsp
4980 b72706c3 2022-06-01 stsp static const struct got_error *
4981 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4982 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4983 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4984 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4985 48ae06ee 2018-10-18 stsp {
4986 48ae06ee 2018-10-18 stsp const struct got_error *err;
4987 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4988 5dc9f4bc 2018-08-04 stsp
4989 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4990 f9d37699 2022-06-28 stsp s->fd1 = -1;
4991 f9d37699 2022-06-28 stsp s->fd2 = -1;
4992 b72706c3 2022-06-01 stsp
4993 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4994 15a94983 2018-12-23 stsp int type1, type2;
4995 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4996 15a94983 2018-12-23 stsp if (err)
4997 15a94983 2018-12-23 stsp return err;
4998 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4999 15a94983 2018-12-23 stsp if (err)
5000 15a94983 2018-12-23 stsp return err;
5001 15a94983 2018-12-23 stsp
5002 15a94983 2018-12-23 stsp if (type1 != type2)
5003 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5004 15a94983 2018-12-23 stsp }
5005 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5006 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5007 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5008 f44b1f58 2020-02-02 tracey s->repo = repo;
5009 f44b1f58 2020-02-02 tracey s->id1 = id1;
5010 f44b1f58 2020-02-02 tracey s->id2 = id2;
5011 3dbaef42 2020-11-24 stsp s->label1 = label1;
5012 3dbaef42 2020-11-24 stsp s->label2 = label2;
5013 48ae06ee 2018-10-18 stsp
5014 15a94983 2018-12-23 stsp if (id1) {
5015 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5016 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5017 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5018 48ae06ee 2018-10-18 stsp } else
5019 5465d566 2020-02-01 tracey s->id1 = NULL;
5020 48ae06ee 2018-10-18 stsp
5021 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5022 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5023 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
5024 b72706c3 2022-06-01 stsp goto done;
5025 48ae06ee 2018-10-18 stsp }
5026 b72706c3 2022-06-01 stsp
5027 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
5028 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
5029 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
5030 a00719e9 2022-06-17 stsp goto done;
5031 a00719e9 2022-06-17 stsp }
5032 a00719e9 2022-06-17 stsp
5033 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
5034 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
5035 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
5036 b72706c3 2022-06-01 stsp goto done;
5037 b72706c3 2022-06-01 stsp }
5038 b72706c3 2022-06-01 stsp
5039 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
5040 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
5041 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5042 f9d37699 2022-06-28 stsp goto done;
5043 f9d37699 2022-06-28 stsp }
5044 f9d37699 2022-06-28 stsp
5045 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
5046 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
5047 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
5048 f9d37699 2022-06-28 stsp goto done;
5049 f9d37699 2022-06-28 stsp }
5050 f9d37699 2022-06-28 stsp
5051 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5052 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5053 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5054 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
5055 5465d566 2020-02-01 tracey s->repo = repo;
5056 6d17833f 2019-11-08 stsp
5057 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5058 6a5ff2d4 2022-08-06 mark int rc;
5059 6d17833f 2019-11-08 stsp
5060 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
5061 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5062 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5063 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
5064 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5065 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5066 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
5067 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5068 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5069 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
5070 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5071 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5072 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
5073 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5074 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5075 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5076 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5077 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5078 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5079 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
5080 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5081 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5082 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
5083 6a5ff2d4 2022-08-06 mark if (rc != ERR)
5084 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
5085 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
5086 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
5087 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
5088 b72706c3 2022-06-01 stsp goto done;
5089 6a5ff2d4 2022-08-06 mark }
5090 6d17833f 2019-11-08 stsp }
5091 5dc9f4bc 2018-08-04 stsp
5092 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5093 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
5094 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
5095 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5096 f5215bb9 2019-02-22 stsp
5097 5465d566 2020-02-01 tracey err = create_diff(s);
5098 48ae06ee 2018-10-18 stsp
5099 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5100 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5101 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
5102 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5103 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5104 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_diff_view;
5105 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
5106 b72706c3 2022-06-01 stsp done:
5107 b72706c3 2022-06-01 stsp if (err)
5108 b72706c3 2022-06-01 stsp close_diff_view(view);
5109 e5a0f69f 2018-08-18 stsp return err;
5110 5dc9f4bc 2018-08-04 stsp }
5111 5dc9f4bc 2018-08-04 stsp
5112 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5113 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5114 5dc9f4bc 2018-08-04 stsp {
5115 a3404814 2018-09-02 stsp const struct got_error *err;
5116 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5117 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5118 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5119 a3404814 2018-09-02 stsp
5120 a3404814 2018-09-02 stsp if (s->id1) {
5121 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5122 a3404814 2018-09-02 stsp if (err)
5123 a3404814 2018-09-02 stsp return err;
5124 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
5125 3dbaef42 2020-11-24 stsp } else
5126 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5127 3dbaef42 2020-11-24 stsp
5128 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5129 a3404814 2018-09-02 stsp if (err)
5130 a3404814 2018-09-02 stsp return err;
5131 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
5132 26ed57b2 2018-05-19 stsp
5133 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5134 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5135 a3404814 2018-09-02 stsp free(id_str1);
5136 a3404814 2018-09-02 stsp free(id_str2);
5137 a3404814 2018-09-02 stsp return err;
5138 a3404814 2018-09-02 stsp }
5139 a3404814 2018-09-02 stsp free(id_str1);
5140 a3404814 2018-09-02 stsp free(id_str2);
5141 a3404814 2018-09-02 stsp
5142 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5143 267bb3b8 2021-08-01 stsp free(header);
5144 267bb3b8 2021-08-01 stsp return err;
5145 15a087fe 2019-02-21 stsp }
5146 15a087fe 2019-02-21 stsp
5147 15a087fe 2019-02-21 stsp static const struct got_error *
5148 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5149 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5150 15a087fe 2019-02-21 stsp {
5151 d7a04538 2019-02-21 stsp const struct got_error *err;
5152 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5153 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5154 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5155 15a087fe 2019-02-21 stsp
5156 15a087fe 2019-02-21 stsp free(s->id2);
5157 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5158 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5159 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5160 15a087fe 2019-02-21 stsp
5161 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5162 d7a04538 2019-02-21 stsp if (err)
5163 d7a04538 2019-02-21 stsp return err;
5164 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5165 15a087fe 2019-02-21 stsp free(s->id1);
5166 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5167 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5168 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5169 15a087fe 2019-02-21 stsp return NULL;
5170 0cf4efb1 2018-09-29 stsp }
5171 0cf4efb1 2018-09-29 stsp
5172 0cf4efb1 2018-09-29 stsp static const struct got_error *
5173 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
5174 917d79a7 2022-07-01 stsp {
5175 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
5176 917d79a7 2022-07-01 stsp
5177 917d79a7 2022-07-01 stsp view->count = 0;
5178 917d79a7 2022-07-01 stsp wclear(view->window);
5179 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
5180 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
5181 917d79a7 2022-07-01 stsp s->matched_line = 0;
5182 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
5183 917d79a7 2022-07-01 stsp return create_diff(s);
5184 c7d5c43c 2022-08-04 mark }
5185 c7d5c43c 2022-08-04 mark
5186 c7d5c43c 2022-08-04 mark static void
5187 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5188 c7d5c43c 2022-08-04 mark {
5189 c7d5c43c 2022-08-04 mark int start, i;
5190 c7d5c43c 2022-08-04 mark
5191 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
5192 c7d5c43c 2022-08-04 mark
5193 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5194 c7d5c43c 2022-08-04 mark if (i == 0)
5195 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
5196 c7d5c43c 2022-08-04 mark if (--i == start)
5197 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5198 c7d5c43c 2022-08-04 mark }
5199 c7d5c43c 2022-08-04 mark
5200 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5201 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5202 917d79a7 2022-07-01 stsp }
5203 917d79a7 2022-07-01 stsp
5204 c7d5c43c 2022-08-04 mark static void
5205 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5206 c7d5c43c 2022-08-04 mark {
5207 c7d5c43c 2022-08-04 mark int start, i;
5208 c7d5c43c 2022-08-04 mark
5209 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
5210 c7d5c43c 2022-08-04 mark
5211 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
5212 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
5213 c7d5c43c 2022-08-04 mark i = 0;
5214 c7d5c43c 2022-08-04 mark if (++i == start)
5215 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
5216 c7d5c43c 2022-08-04 mark }
5217 c7d5c43c 2022-08-04 mark
5218 c7d5c43c 2022-08-04 mark s->selected_line = 1;
5219 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
5220 c7d5c43c 2022-08-04 mark }
5221 c7d5c43c 2022-08-04 mark
5222 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5223 c0f61fa4 2022-07-11 mark int, int, int);
5224 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5225 c0f61fa4 2022-07-11 mark int, int);
5226 c0f61fa4 2022-07-11 mark
5227 917d79a7 2022-07-01 stsp static const struct got_error *
5228 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5229 e5a0f69f 2018-08-18 stsp {
5230 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5231 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5232 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5233 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5234 826082fe 2020-12-10 stsp char *line = NULL;
5235 826082fe 2020-12-10 stsp size_t linesize = 0;
5236 826082fe 2020-12-10 stsp ssize_t linelen;
5237 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
5238 e5a0f69f 2018-08-18 stsp
5239 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
5240 c7d5c43c 2022-08-04 mark
5241 e5a0f69f 2018-08-18 stsp switch (ch) {
5242 145b6838 2022-06-16 stsp case '0':
5243 145b6838 2022-06-16 stsp view->x = 0;
5244 145b6838 2022-06-16 stsp break;
5245 145b6838 2022-06-16 stsp case '$':
5246 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5247 640cd7ff 2022-06-22 mark view->count = 0;
5248 145b6838 2022-06-16 stsp break;
5249 145b6838 2022-06-16 stsp case KEY_RIGHT:
5250 145b6838 2022-06-16 stsp case 'l':
5251 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5252 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5253 640cd7ff 2022-06-22 mark else
5254 640cd7ff 2022-06-22 mark view->count = 0;
5255 145b6838 2022-06-16 stsp break;
5256 145b6838 2022-06-16 stsp case KEY_LEFT:
5257 145b6838 2022-06-16 stsp case 'h':
5258 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5259 640cd7ff 2022-06-22 mark if (view->x <= 0)
5260 640cd7ff 2022-06-22 mark view->count = 0;
5261 145b6838 2022-06-16 stsp break;
5262 64453f7e 2020-11-21 stsp case 'a':
5263 3dbaef42 2020-11-24 stsp case 'w':
5264 3dbaef42 2020-11-24 stsp if (ch == 'a')
5265 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5266 5191b70b 2023-01-07 mark else if (ch == 'w')
5267 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5268 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
5269 912a3f79 2021-08-30 j break;
5270 912a3f79 2021-08-30 j case 'g':
5271 912a3f79 2021-08-30 j case KEY_HOME:
5272 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5273 640cd7ff 2022-06-22 mark view->count = 0;
5274 64453f7e 2020-11-21 stsp break;
5275 912a3f79 2021-08-30 j case 'G':
5276 912a3f79 2021-08-30 j case KEY_END:
5277 640cd7ff 2022-06-22 mark view->count = 0;
5278 912a3f79 2021-08-30 j if (s->eof)
5279 912a3f79 2021-08-30 j break;
5280 912a3f79 2021-08-30 j
5281 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5282 912a3f79 2021-08-30 j s->eof = 1;
5283 912a3f79 2021-08-30 j break;
5284 1e37a5c2 2019-05-12 jcs case 'k':
5285 1e37a5c2 2019-05-12 jcs case KEY_UP:
5286 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5287 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5288 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5289 640cd7ff 2022-06-22 mark else
5290 640cd7ff 2022-06-22 mark view->count = 0;
5291 1e37a5c2 2019-05-12 jcs break;
5292 83cc4199 2022-06-13 stsp case CTRL('u'):
5293 33c3719a 2022-06-15 stsp case 'u':
5294 83cc4199 2022-06-13 stsp nscroll /= 2;
5295 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5296 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5297 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5298 61417565 2022-06-20 mark case 'b':
5299 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
5300 640cd7ff 2022-06-22 mark view->count = 0;
5301 26ed57b2 2018-05-19 stsp break;
5302 640cd7ff 2022-06-22 mark }
5303 1e37a5c2 2019-05-12 jcs i = 0;
5304 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
5305 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5306 1e37a5c2 2019-05-12 jcs break;
5307 1e37a5c2 2019-05-12 jcs case 'j':
5308 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5309 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5310 1e37a5c2 2019-05-12 jcs if (!s->eof)
5311 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5312 640cd7ff 2022-06-22 mark else
5313 640cd7ff 2022-06-22 mark view->count = 0;
5314 1e37a5c2 2019-05-12 jcs break;
5315 83cc4199 2022-06-13 stsp case CTRL('d'):
5316 33c3719a 2022-06-15 stsp case 'd':
5317 83cc4199 2022-06-13 stsp nscroll /= 2;
5318 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5319 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5320 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5321 61417565 2022-06-20 mark case 'f':
5322 1e37a5c2 2019-05-12 jcs case ' ':
5323 640cd7ff 2022-06-22 mark if (s->eof) {
5324 640cd7ff 2022-06-22 mark view->count = 0;
5325 1e37a5c2 2019-05-12 jcs break;
5326 640cd7ff 2022-06-22 mark }
5327 1e37a5c2 2019-05-12 jcs i = 0;
5328 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
5329 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5330 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5331 826082fe 2020-12-10 stsp if (linelen == -1) {
5332 826082fe 2020-12-10 stsp if (feof(s->f)) {
5333 826082fe 2020-12-10 stsp s->eof = 1;
5334 826082fe 2020-12-10 stsp } else
5335 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5336 34bc9ec9 2019-02-22 stsp break;
5337 826082fe 2020-12-10 stsp }
5338 1e37a5c2 2019-05-12 jcs }
5339 826082fe 2020-12-10 stsp free(line);
5340 1e37a5c2 2019-05-12 jcs break;
5341 c7d5c43c 2022-08-04 mark case '(':
5342 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5343 c7d5c43c 2022-08-04 mark break;
5344 c7d5c43c 2022-08-04 mark case ')':
5345 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5346 c7d5c43c 2022-08-04 mark break;
5347 c7d5c43c 2022-08-04 mark case '{':
5348 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5349 c7d5c43c 2022-08-04 mark break;
5350 c7d5c43c 2022-08-04 mark case '}':
5351 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
5352 c7d5c43c 2022-08-04 mark break;
5353 1e37a5c2 2019-05-12 jcs case '[':
5354 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5355 1e37a5c2 2019-05-12 jcs s->diff_context--;
5356 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5357 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5358 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5359 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5360 27829c9e 2020-11-21 stsp s->nlines) {
5361 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5362 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5363 27829c9e 2020-11-21 stsp }
5364 640cd7ff 2022-06-22 mark } else
5365 640cd7ff 2022-06-22 mark view->count = 0;
5366 1e37a5c2 2019-05-12 jcs break;
5367 1e37a5c2 2019-05-12 jcs case ']':
5368 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5369 1e37a5c2 2019-05-12 jcs s->diff_context++;
5370 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5371 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5372 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5373 640cd7ff 2022-06-22 mark } else
5374 640cd7ff 2022-06-22 mark view->count = 0;
5375 1e37a5c2 2019-05-12 jcs break;
5376 1e37a5c2 2019-05-12 jcs case '<':
5377 1e37a5c2 2019-05-12 jcs case ',':
5378 2b3e6702 2022-07-20 mark case 'K':
5379 c0f61fa4 2022-07-11 mark up = 1;
5380 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
5381 c0f61fa4 2022-07-11 mark case '>':
5382 c0f61fa4 2022-07-11 mark case '.':
5383 2b3e6702 2022-07-20 mark case 'J':
5384 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
5385 640cd7ff 2022-06-22 mark view->count = 0;
5386 48ae06ee 2018-10-18 stsp break;
5387 640cd7ff 2022-06-22 mark }
5388 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
5389 6524637e 2019-02-21 stsp
5390 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
5391 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
5392 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
5393 15a087fe 2019-02-21 stsp
5394 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
5395 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5396 c0f61fa4 2022-07-11 mark if (err)
5397 c0f61fa4 2022-07-11 mark break;
5398 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5399 15a087fe 2019-02-21 stsp
5400 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
5401 c0f61fa4 2022-07-11 mark break;
5402 15a087fe 2019-02-21 stsp
5403 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
5404 c0f61fa4 2022-07-11 mark if (err)
5405 c0f61fa4 2022-07-11 mark break;
5406 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5407 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
5408 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
5409 5e224a3e 2019-02-22 stsp
5410 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
5411 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
5412 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
5413 c0f61fa4 2022-07-11 mark
5414 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
5415 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
5416 c0f61fa4 2022-07-11 mark if (err)
5417 c0f61fa4 2022-07-11 mark break;
5418 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
5419 5a8b5076 2020-12-05 stsp
5420 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
5421 c0f61fa4 2022-07-11 mark break;
5422 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5423 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5424 c0f61fa4 2022-07-11 mark bs->selected_line);
5425 c0f61fa4 2022-07-11 mark if (id == NULL)
5426 c0f61fa4 2022-07-11 mark break;
5427 15a087fe 2019-02-21 stsp
5428 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5429 c0f61fa4 2022-07-11 mark break;
5430 15a087fe 2019-02-21 stsp
5431 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5432 c0f61fa4 2022-07-11 mark if (err)
5433 c0f61fa4 2022-07-11 mark break;
5434 c0f61fa4 2022-07-11 mark }
5435 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5436 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5437 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5438 145b6838 2022-06-16 stsp view->x = 0;
5439 1e37a5c2 2019-05-12 jcs
5440 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5441 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5442 1e37a5c2 2019-05-12 jcs break;
5443 1e37a5c2 2019-05-12 jcs default:
5444 640cd7ff 2022-06-22 mark view->count = 0;
5445 1e37a5c2 2019-05-12 jcs break;
5446 26ed57b2 2018-05-19 stsp }
5447 e5a0f69f 2018-08-18 stsp
5448 bcbd79e2 2018-08-19 stsp return err;
5449 26ed57b2 2018-05-19 stsp }
5450 26ed57b2 2018-05-19 stsp
5451 4ed7e80c 2018-05-20 stsp static const struct got_error *
5452 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5453 9f7d7167 2018-04-29 stsp {
5454 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5455 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5456 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5457 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5458 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5459 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5460 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5461 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5462 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5463 3dbaef42 2020-11-24 stsp const char *errstr;
5464 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5465 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5466 70ac5f84 2019-03-28 stsp
5467 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5468 26ed57b2 2018-05-19 stsp switch (ch) {
5469 64453f7e 2020-11-21 stsp case 'a':
5470 64453f7e 2020-11-21 stsp force_text_diff = 1;
5471 3dbaef42 2020-11-24 stsp break;
5472 3dbaef42 2020-11-24 stsp case 'C':
5473 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5474 3dbaef42 2020-11-24 stsp &errstr);
5475 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5476 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5477 5a20d08d 2022-02-09 op errstr, errstr);
5478 64453f7e 2020-11-21 stsp break;
5479 09b5bff8 2020-02-23 naddy case 'r':
5480 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5481 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5482 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5483 09b5bff8 2020-02-23 naddy optarg);
5484 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5485 09b5bff8 2020-02-23 naddy break;
5486 3dbaef42 2020-11-24 stsp case 'w':
5487 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5488 3dbaef42 2020-11-24 stsp break;
5489 26ed57b2 2018-05-19 stsp default:
5490 17020d27 2019-03-07 stsp usage_diff();
5491 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5492 26ed57b2 2018-05-19 stsp }
5493 26ed57b2 2018-05-19 stsp }
5494 26ed57b2 2018-05-19 stsp
5495 26ed57b2 2018-05-19 stsp argc -= optind;
5496 26ed57b2 2018-05-19 stsp argv += optind;
5497 26ed57b2 2018-05-19 stsp
5498 26ed57b2 2018-05-19 stsp if (argc == 0) {
5499 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5500 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5501 15a94983 2018-12-23 stsp id_str1 = argv[0];
5502 15a94983 2018-12-23 stsp id_str2 = argv[1];
5503 26ed57b2 2018-05-19 stsp } else
5504 26ed57b2 2018-05-19 stsp usage_diff();
5505 eb6600df 2019-01-04 stsp
5506 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5507 0ae84acc 2022-06-15 tracey if (error)
5508 0ae84acc 2022-06-15 tracey goto done;
5509 0ae84acc 2022-06-15 tracey
5510 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5511 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5512 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5513 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5514 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5515 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5516 c156c7a4 2020-12-18 stsp goto done;
5517 a273ac94 2020-02-23 naddy if (worktree)
5518 a273ac94 2020-02-23 naddy repo_path =
5519 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5520 a273ac94 2020-02-23 naddy else
5521 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5522 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5523 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5524 c156c7a4 2020-12-18 stsp goto done;
5525 c156c7a4 2020-12-18 stsp }
5526 a273ac94 2020-02-23 naddy }
5527 a273ac94 2020-02-23 naddy
5528 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5529 eb6600df 2019-01-04 stsp if (error)
5530 eb6600df 2019-01-04 stsp goto done;
5531 26ed57b2 2018-05-19 stsp
5532 a273ac94 2020-02-23 naddy init_curses();
5533 a273ac94 2020-02-23 naddy
5534 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5535 51a10b52 2020-12-26 stsp if (error)
5536 51a10b52 2020-12-26 stsp goto done;
5537 51a10b52 2020-12-26 stsp
5538 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5539 26ed57b2 2018-05-19 stsp if (error)
5540 26ed57b2 2018-05-19 stsp goto done;
5541 26ed57b2 2018-05-19 stsp
5542 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5543 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5544 26ed57b2 2018-05-19 stsp if (error)
5545 26ed57b2 2018-05-19 stsp goto done;
5546 26ed57b2 2018-05-19 stsp
5547 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5548 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5549 26ed57b2 2018-05-19 stsp if (error)
5550 26ed57b2 2018-05-19 stsp goto done;
5551 26ed57b2 2018-05-19 stsp
5552 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5553 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5554 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5555 ea5e7bb5 2018-08-01 stsp goto done;
5556 ea5e7bb5 2018-08-01 stsp }
5557 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5558 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5559 5dc9f4bc 2018-08-04 stsp if (error)
5560 5dc9f4bc 2018-08-04 stsp goto done;
5561 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5562 26ed57b2 2018-05-19 stsp done:
5563 3dbaef42 2020-11-24 stsp free(label1);
5564 3dbaef42 2020-11-24 stsp free(label2);
5565 c02c541e 2019-03-29 stsp free(repo_path);
5566 a273ac94 2020-02-23 naddy free(cwd);
5567 1d0f4054 2021-06-17 stsp if (repo) {
5568 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5569 1d0f4054 2021-06-17 stsp if (error == NULL)
5570 1d0f4054 2021-06-17 stsp error = close_err;
5571 1d0f4054 2021-06-17 stsp }
5572 a273ac94 2020-02-23 naddy if (worktree)
5573 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5574 0ae84acc 2022-06-15 tracey if (pack_fds) {
5575 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5576 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5577 0ae84acc 2022-06-15 tracey if (error == NULL)
5578 0ae84acc 2022-06-15 tracey error = pack_err;
5579 0ae84acc 2022-06-15 tracey }
5580 51a10b52 2020-12-26 stsp tog_free_refs();
5581 26ed57b2 2018-05-19 stsp return error;
5582 9f7d7167 2018-04-29 stsp }
5583 9f7d7167 2018-04-29 stsp
5584 4ed7e80c 2018-05-20 stsp __dead static void
5585 9f7d7167 2018-04-29 stsp usage_blame(void)
5586 9f7d7167 2018-04-29 stsp {
5587 80ddbec8 2018-04-29 stsp endwin();
5588 87411fa9 2022-06-16 stsp fprintf(stderr,
5589 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5590 9f7d7167 2018-04-29 stsp getprogname());
5591 9f7d7167 2018-04-29 stsp exit(1);
5592 9f7d7167 2018-04-29 stsp }
5593 84451b3e 2018-07-10 stsp
5594 84451b3e 2018-07-10 stsp struct tog_blame_line {
5595 84451b3e 2018-07-10 stsp int annotated;
5596 84451b3e 2018-07-10 stsp struct got_object_id *id;
5597 84451b3e 2018-07-10 stsp };
5598 9f7d7167 2018-04-29 stsp
5599 4ed7e80c 2018-05-20 stsp static const struct got_error *
5600 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5601 84451b3e 2018-07-10 stsp {
5602 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5603 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5604 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5605 84451b3e 2018-07-10 stsp const struct got_error *err;
5606 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5607 826082fe 2020-12-10 stsp char *line = NULL;
5608 826082fe 2020-12-10 stsp size_t linesize = 0;
5609 826082fe 2020-12-10 stsp ssize_t linelen;
5610 84451b3e 2018-07-10 stsp wchar_t *wline;
5611 27a741e5 2019-09-11 stsp int width;
5612 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5613 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5614 ab089a2a 2018-07-12 stsp char *id_str;
5615 11b20872 2019-11-08 stsp struct tog_color *tc;
5616 ab089a2a 2018-07-12 stsp
5617 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5618 ab089a2a 2018-07-12 stsp if (err)
5619 ab089a2a 2018-07-12 stsp return err;
5620 84451b3e 2018-07-10 stsp
5621 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5622 f7d12f7e 2018-08-01 stsp werase(view->window);
5623 84451b3e 2018-07-10 stsp
5624 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5625 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5626 ab089a2a 2018-07-12 stsp free(id_str);
5627 ab089a2a 2018-07-12 stsp return err;
5628 ab089a2a 2018-07-12 stsp }
5629 ab089a2a 2018-07-12 stsp
5630 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5631 ab089a2a 2018-07-12 stsp free(line);
5632 2550e4c3 2018-07-13 stsp line = NULL;
5633 1cae65b4 2019-09-22 stsp if (err)
5634 1cae65b4 2019-09-22 stsp return err;
5635 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5636 a3404814 2018-09-02 stsp wstandout(view->window);
5637 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5638 11b20872 2019-11-08 stsp if (tc)
5639 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5640 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5641 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
5642 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
5643 11b20872 2019-11-08 stsp if (tc)
5644 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5645 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5646 a3404814 2018-09-02 stsp wstandend(view->window);
5647 2550e4c3 2018-07-13 stsp free(wline);
5648 2550e4c3 2018-07-13 stsp wline = NULL;
5649 ab089a2a 2018-07-12 stsp
5650 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5651 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5652 94b80cfa 2022-08-01 mark
5653 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5654 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5655 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5656 ab089a2a 2018-07-12 stsp free(id_str);
5657 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5658 ab089a2a 2018-07-12 stsp }
5659 ab089a2a 2018-07-12 stsp free(id_str);
5660 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5661 3f60a8ef 2018-07-10 stsp free(line);
5662 2550e4c3 2018-07-13 stsp line = NULL;
5663 3f60a8ef 2018-07-10 stsp if (err)
5664 3f60a8ef 2018-07-10 stsp return err;
5665 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5666 2550e4c3 2018-07-13 stsp free(wline);
5667 2550e4c3 2018-07-13 stsp wline = NULL;
5668 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5669 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5670 3f60a8ef 2018-07-10 stsp
5671 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5672 145b6838 2022-06-16 stsp view->maxx = 0;
5673 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5674 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5675 826082fe 2020-12-10 stsp if (linelen == -1) {
5676 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5677 826082fe 2020-12-10 stsp s->eof = 1;
5678 826082fe 2020-12-10 stsp break;
5679 826082fe 2020-12-10 stsp }
5680 84451b3e 2018-07-10 stsp free(line);
5681 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5682 84451b3e 2018-07-10 stsp }
5683 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5684 94b80cfa 2022-08-01 mark continue;
5685 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5686 826082fe 2020-12-10 stsp continue;
5687 1853e0f4 2022-06-16 stsp
5688 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5689 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5690 1853e0f4 2022-06-16 stsp if (err) {
5691 1853e0f4 2022-06-16 stsp free(line);
5692 1853e0f4 2022-06-16 stsp return err;
5693 1853e0f4 2022-06-16 stsp }
5694 1853e0f4 2022-06-16 stsp free(wline);
5695 1853e0f4 2022-06-16 stsp wline = NULL;
5696 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5697 84451b3e 2018-07-10 stsp
5698 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5699 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5700 b700b5d6 2018-07-10 stsp
5701 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5702 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5703 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5704 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5705 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5706 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5707 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5708 8d0fe45a 2019-08-12 stsp char *id_str;
5709 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5710 87411fa9 2022-06-16 stsp blame_line->id);
5711 8d0fe45a 2019-08-12 stsp if (err) {
5712 8d0fe45a 2019-08-12 stsp free(line);
5713 8d0fe45a 2019-08-12 stsp return err;
5714 8d0fe45a 2019-08-12 stsp }
5715 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5716 11b20872 2019-11-08 stsp if (tc)
5717 11b20872 2019-11-08 stsp wattr_on(view->window,
5718 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5719 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5720 11b20872 2019-11-08 stsp if (tc)
5721 11b20872 2019-11-08 stsp wattr_off(view->window,
5722 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5723 8d0fe45a 2019-08-12 stsp free(id_str);
5724 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5725 8d0fe45a 2019-08-12 stsp } else {
5726 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5727 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5728 84451b3e 2018-07-10 stsp }
5729 ee41ec32 2018-07-10 stsp } else {
5730 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5731 ee41ec32 2018-07-10 stsp prev_id = NULL;
5732 ee41ec32 2018-07-10 stsp }
5733 84451b3e 2018-07-10 stsp
5734 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5735 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5736 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5737 27a741e5 2019-09-11 stsp
5738 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5739 41605754 2020-11-12 stsp width = 9;
5740 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5741 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5742 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5743 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5744 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5745 41605754 2020-11-12 stsp if (err) {
5746 41605754 2020-11-12 stsp free(line);
5747 41605754 2020-11-12 stsp return err;
5748 41605754 2020-11-12 stsp }
5749 41605754 2020-11-12 stsp width += 9;
5750 41605754 2020-11-12 stsp } else {
5751 4cb9d869 2022-06-16 stsp int skip;
5752 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5753 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5754 4cb9d869 2022-06-16 stsp if (err) {
5755 4cb9d869 2022-06-16 stsp free(line);
5756 4cb9d869 2022-06-16 stsp return err;
5757 145b6838 2022-06-16 stsp }
5758 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5759 a75b3e2d 2022-06-16 stsp width += 9;
5760 41605754 2020-11-12 stsp free(wline);
5761 41605754 2020-11-12 stsp wline = NULL;
5762 41605754 2020-11-12 stsp }
5763 41605754 2020-11-12 stsp
5764 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5765 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5766 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5767 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5768 84451b3e 2018-07-10 stsp }
5769 826082fe 2020-12-10 stsp free(line);
5770 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5771 84451b3e 2018-07-10 stsp
5772 9b058f45 2022-06-30 mark view_border(view);
5773 84451b3e 2018-07-10 stsp
5774 84451b3e 2018-07-10 stsp return NULL;
5775 84451b3e 2018-07-10 stsp }
5776 84451b3e 2018-07-10 stsp
5777 84451b3e 2018-07-10 stsp static const struct got_error *
5778 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5779 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5780 84451b3e 2018-07-10 stsp {
5781 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5782 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5783 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5784 1a76625f 2018-10-22 stsp int errcode;
5785 84451b3e 2018-07-10 stsp
5786 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5787 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5788 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5789 84451b3e 2018-07-10 stsp
5790 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5791 1a76625f 2018-10-22 stsp if (errcode)
5792 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5793 84451b3e 2018-07-10 stsp
5794 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5795 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5796 d68a0a7d 2018-07-10 stsp goto done;
5797 d68a0a7d 2018-07-10 stsp }
5798 d68a0a7d 2018-07-10 stsp
5799 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5800 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5801 d68a0a7d 2018-07-10 stsp
5802 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5803 d68a0a7d 2018-07-10 stsp if (line->annotated)
5804 d68a0a7d 2018-07-10 stsp goto done;
5805 d68a0a7d 2018-07-10 stsp
5806 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5807 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5808 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5809 84451b3e 2018-07-10 stsp goto done;
5810 84451b3e 2018-07-10 stsp }
5811 84451b3e 2018-07-10 stsp line->annotated = 1;
5812 84451b3e 2018-07-10 stsp done:
5813 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5814 1a76625f 2018-10-22 stsp if (errcode)
5815 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5816 84451b3e 2018-07-10 stsp return err;
5817 84451b3e 2018-07-10 stsp }
5818 84451b3e 2018-07-10 stsp
5819 84451b3e 2018-07-10 stsp static void *
5820 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5821 84451b3e 2018-07-10 stsp {
5822 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5823 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5824 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5825 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5826 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5827 1b484788 2022-06-28 tracey
5828 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5829 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5830 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5831 e6e73e55 2022-06-30 tracey
5832 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5833 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5834 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5835 e6e73e55 2022-06-30 tracey goto done;
5836 e6e73e55 2022-06-30 tracey }
5837 18430de3 2018-07-10 stsp
5838 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5839 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5840 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5841 e6e73e55 2022-06-30 tracey goto done;
5842 e6e73e55 2022-06-30 tracey }
5843 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5844 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5845 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5846 e6e73e55 2022-06-30 tracey goto done;
5847 e6e73e55 2022-06-30 tracey }
5848 e6e73e55 2022-06-30 tracey
5849 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5850 61266923 2020-01-14 stsp if (err)
5851 e6e73e55 2022-06-30 tracey goto done;
5852 61266923 2020-01-14 stsp
5853 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5854 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5855 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5856 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5857 fc06ba56 2019-08-22 stsp err = NULL;
5858 18430de3 2018-07-10 stsp
5859 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5860 e6e73e55 2022-06-30 tracey if (errcode) {
5861 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5862 e6e73e55 2022-06-30 tracey goto done;
5863 e6e73e55 2022-06-30 tracey }
5864 18430de3 2018-07-10 stsp
5865 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5866 1d0f4054 2021-06-17 stsp if (err == NULL)
5867 1d0f4054 2021-06-17 stsp err = close_err;
5868 c9beca56 2018-07-22 stsp ta->repo = NULL;
5869 c9beca56 2018-07-22 stsp *ta->complete = 1;
5870 18430de3 2018-07-10 stsp
5871 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5872 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5873 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5874 18430de3 2018-07-10 stsp
5875 e6e73e55 2022-06-30 tracey done:
5876 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5877 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5878 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5879 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5880 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5881 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5882 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5883 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5884 1b484788 2022-06-28 tracey
5885 18430de3 2018-07-10 stsp return (void *)err;
5886 84451b3e 2018-07-10 stsp }
5887 84451b3e 2018-07-10 stsp
5888 245d91c1 2018-07-12 stsp static struct got_object_id *
5889 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5890 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5891 245d91c1 2018-07-12 stsp {
5892 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5893 8d0fe45a 2019-08-12 stsp
5894 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5895 8d0fe45a 2019-08-12 stsp return NULL;
5896 b880a918 2018-07-10 stsp
5897 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5898 c0f61fa4 2022-07-11 mark if (!line->annotated)
5899 c0f61fa4 2022-07-11 mark return NULL;
5900 c0f61fa4 2022-07-11 mark
5901 c0f61fa4 2022-07-11 mark return line->id;
5902 c0f61fa4 2022-07-11 mark }
5903 c0f61fa4 2022-07-11 mark
5904 c0f61fa4 2022-07-11 mark static struct got_object_id *
5905 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5906 c0f61fa4 2022-07-11 mark int lineno)
5907 c0f61fa4 2022-07-11 mark {
5908 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5909 c0f61fa4 2022-07-11 mark
5910 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5911 c0f61fa4 2022-07-11 mark return NULL;
5912 c0f61fa4 2022-07-11 mark
5913 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5914 245d91c1 2018-07-12 stsp if (!line->annotated)
5915 245d91c1 2018-07-12 stsp return NULL;
5916 245d91c1 2018-07-12 stsp
5917 245d91c1 2018-07-12 stsp return line->id;
5918 b880a918 2018-07-10 stsp }
5919 245d91c1 2018-07-12 stsp
5920 b880a918 2018-07-10 stsp static const struct got_error *
5921 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5922 a70480e0 2018-06-23 stsp {
5923 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5924 245d91c1 2018-07-12 stsp int i;
5925 245d91c1 2018-07-12 stsp
5926 245d91c1 2018-07-12 stsp if (blame->thread) {
5927 1a76625f 2018-10-22 stsp int errcode;
5928 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5929 1a76625f 2018-10-22 stsp if (errcode)
5930 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5931 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5932 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5933 1a76625f 2018-10-22 stsp if (errcode)
5934 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5935 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5936 1a76625f 2018-10-22 stsp if (errcode)
5937 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5938 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5939 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5940 245d91c1 2018-07-12 stsp err = NULL;
5941 245d91c1 2018-07-12 stsp blame->thread = NULL;
5942 245d91c1 2018-07-12 stsp }
5943 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5944 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5945 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5946 1d0f4054 2021-06-17 stsp if (err == NULL)
5947 1d0f4054 2021-06-17 stsp err = close_err;
5948 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5949 245d91c1 2018-07-12 stsp }
5950 245d91c1 2018-07-12 stsp if (blame->f) {
5951 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5952 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5953 245d91c1 2018-07-12 stsp blame->f = NULL;
5954 245d91c1 2018-07-12 stsp }
5955 57670559 2018-12-23 stsp if (blame->lines) {
5956 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5957 57670559 2018-12-23 stsp free(blame->lines[i].id);
5958 57670559 2018-12-23 stsp free(blame->lines);
5959 57670559 2018-12-23 stsp blame->lines = NULL;
5960 57670559 2018-12-23 stsp }
5961 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5962 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5963 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5964 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5965 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5966 0ae84acc 2022-06-15 tracey if (err == NULL)
5967 0ae84acc 2022-06-15 tracey err = pack_err;
5968 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5969 0ae84acc 2022-06-15 tracey }
5970 245d91c1 2018-07-12 stsp return err;
5971 245d91c1 2018-07-12 stsp }
5972 245d91c1 2018-07-12 stsp
5973 245d91c1 2018-07-12 stsp static const struct got_error *
5974 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5975 fc06ba56 2019-08-22 stsp {
5976 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5977 fc06ba56 2019-08-22 stsp int *done = arg;
5978 fc06ba56 2019-08-22 stsp int errcode;
5979 fc06ba56 2019-08-22 stsp
5980 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5981 fc06ba56 2019-08-22 stsp if (errcode)
5982 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5983 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5984 fc06ba56 2019-08-22 stsp
5985 fc06ba56 2019-08-22 stsp if (*done)
5986 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5987 fc06ba56 2019-08-22 stsp
5988 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5989 fc06ba56 2019-08-22 stsp if (errcode)
5990 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5991 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5992 fc06ba56 2019-08-22 stsp
5993 fc06ba56 2019-08-22 stsp return err;
5994 fc06ba56 2019-08-22 stsp }
5995 fc06ba56 2019-08-22 stsp
5996 fc06ba56 2019-08-22 stsp static const struct got_error *
5997 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5998 245d91c1 2018-07-12 stsp {
5999 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6000 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6001 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6002 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6003 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6004 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6005 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6006 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
6007 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6008 a70480e0 2018-06-23 stsp
6009 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
6010 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
6011 27d434c2 2018-09-15 stsp if (err)
6012 15a94983 2018-12-23 stsp return err;
6013 eb81bc23 2022-06-28 tracey
6014 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
6015 eb81bc23 2022-06-28 tracey if (fd == -1) {
6016 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
6017 eb81bc23 2022-06-28 tracey goto done;
6018 eb81bc23 2022-06-28 tracey }
6019 a44927cc 2022-04-07 stsp
6020 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6021 a44927cc 2022-04-07 stsp if (err)
6022 a44927cc 2022-04-07 stsp goto done;
6023 27d434c2 2018-09-15 stsp
6024 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6025 84451b3e 2018-07-10 stsp if (err)
6026 84451b3e 2018-07-10 stsp goto done;
6027 27d434c2 2018-09-15 stsp
6028 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6029 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6030 84451b3e 2018-07-10 stsp goto done;
6031 84451b3e 2018-07-10 stsp }
6032 a70480e0 2018-06-23 stsp
6033 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6034 a70480e0 2018-06-23 stsp if (err)
6035 a70480e0 2018-06-23 stsp goto done;
6036 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6037 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6038 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6039 84451b3e 2018-07-10 stsp goto done;
6040 84451b3e 2018-07-10 stsp }
6041 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6042 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6043 1fddf795 2021-01-20 stsp if (err)
6044 1fddf795 2021-01-20 stsp goto done;
6045 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6046 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6047 84451b3e 2018-07-10 stsp goto done;
6048 1fddf795 2021-01-20 stsp }
6049 b02560ec 2019-08-19 stsp
6050 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6051 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6052 b02560ec 2019-08-19 stsp blame->nlines--;
6053 a70480e0 2018-06-23 stsp
6054 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6055 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6056 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6057 84451b3e 2018-07-10 stsp goto done;
6058 84451b3e 2018-07-10 stsp }
6059 a70480e0 2018-06-23 stsp
6060 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
6061 bd24772e 2018-07-11 stsp if (err)
6062 bd24772e 2018-07-11 stsp goto done;
6063 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6064 0ae84acc 2022-06-15 tracey pack_fds);
6065 0ae84acc 2022-06-15 tracey if (err)
6066 0ae84acc 2022-06-15 tracey goto done;
6067 bd24772e 2018-07-11 stsp
6068 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
6069 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6070 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6071 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6072 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6073 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6074 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6075 245d91c1 2018-07-12 stsp goto done;
6076 245d91c1 2018-07-12 stsp }
6077 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6078 245d91c1 2018-07-12 stsp
6079 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6080 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6081 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6082 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6083 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6084 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6085 a5388363 2020-12-01 naddy s->blame_complete = 0;
6086 f5a09613 2020-12-13 naddy
6087 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6088 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6089 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6090 f5a09613 2020-12-13 naddy s->selected_line = 1;
6091 f5a09613 2020-12-13 naddy }
6092 67b5eae1 2021-12-27 naddy s->matched_line = 0;
6093 245d91c1 2018-07-12 stsp
6094 245d91c1 2018-07-12 stsp done:
6095 a44927cc 2022-04-07 stsp if (commit)
6096 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6097 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
6098 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
6099 245d91c1 2018-07-12 stsp if (blob)
6100 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6101 27d434c2 2018-09-15 stsp free(obj_id);
6102 245d91c1 2018-07-12 stsp if (err)
6103 245d91c1 2018-07-12 stsp stop_blame(blame);
6104 245d91c1 2018-07-12 stsp return err;
6105 245d91c1 2018-07-12 stsp }
6106 245d91c1 2018-07-12 stsp
6107 245d91c1 2018-07-12 stsp static const struct got_error *
6108 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6109 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6110 245d91c1 2018-07-12 stsp {
6111 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6112 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6113 dbc6a6b6 2018-07-12 stsp
6114 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6115 245d91c1 2018-07-12 stsp
6116 c4843652 2019-08-12 stsp s->path = strdup(path);
6117 c4843652 2019-08-12 stsp if (s->path == NULL)
6118 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6119 c4843652 2019-08-12 stsp
6120 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6121 c4843652 2019-08-12 stsp if (err) {
6122 c4843652 2019-08-12 stsp free(s->path);
6123 7cbe629d 2018-08-04 stsp return err;
6124 c4843652 2019-08-12 stsp }
6125 245d91c1 2018-07-12 stsp
6126 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6127 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6128 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6129 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6130 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6131 fb2756b9 2018-08-04 stsp s->repo = repo;
6132 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6133 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6134 7cbe629d 2018-08-04 stsp
6135 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6136 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6137 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6138 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6139 11b20872 2019-11-08 stsp if (err)
6140 11b20872 2019-11-08 stsp return err;
6141 11b20872 2019-11-08 stsp }
6142 11b20872 2019-11-08 stsp
6143 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6144 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6145 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
6146 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6147 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6148 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_blame_view;
6149 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
6150 e5a0f69f 2018-08-18 stsp
6151 a5388363 2020-12-01 naddy return run_blame(view);
6152 7cbe629d 2018-08-04 stsp }
6153 7cbe629d 2018-08-04 stsp
6154 e5a0f69f 2018-08-18 stsp static const struct got_error *
6155 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6156 7cbe629d 2018-08-04 stsp {
6157 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6158 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6159 7cbe629d 2018-08-04 stsp
6160 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6161 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6162 e5a0f69f 2018-08-18 stsp
6163 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6164 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6165 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6166 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6167 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6168 7cbe629d 2018-08-04 stsp }
6169 e5a0f69f 2018-08-18 stsp
6170 e5a0f69f 2018-08-18 stsp free(s->path);
6171 11b20872 2019-11-08 stsp free_colors(&s->colors);
6172 e5a0f69f 2018-08-18 stsp return err;
6173 7cbe629d 2018-08-04 stsp }
6174 7cbe629d 2018-08-04 stsp
6175 7cbe629d 2018-08-04 stsp static const struct got_error *
6176 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6177 6c4c42e0 2019-06-24 stsp {
6178 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6179 6c4c42e0 2019-06-24 stsp
6180 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6181 6c4c42e0 2019-06-24 stsp return NULL;
6182 eaf2c13e 2022-09-17 mark }
6183 eaf2c13e 2022-09-17 mark
6184 eaf2c13e 2022-09-17 mark static void
6185 eaf2c13e 2022-09-17 mark search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6186 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
6187 eaf2c13e 2022-09-17 mark {
6188 eaf2c13e 2022-09-17 mark struct tog_blame_view_state *s = &view->state.blame;
6189 eaf2c13e 2022-09-17 mark
6190 eaf2c13e 2022-09-17 mark *f = s->blame.f;
6191 eaf2c13e 2022-09-17 mark *nlines = s->blame.nlines;
6192 eaf2c13e 2022-09-17 mark *line_offsets = s->blame.line_offsets;
6193 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
6194 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
6195 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
6196 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
6197 6c4c42e0 2019-06-24 stsp }
6198 6c4c42e0 2019-06-24 stsp
6199 6c4c42e0 2019-06-24 stsp static const struct got_error *
6200 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6201 7cbe629d 2018-08-04 stsp {
6202 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6203 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6204 2b380cc8 2018-10-24 stsp int errcode;
6205 2b380cc8 2018-10-24 stsp
6206 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
6207 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6208 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6209 2b380cc8 2018-10-24 stsp if (errcode)
6210 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6211 51fe7530 2019-08-19 stsp
6212 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6213 2b380cc8 2018-10-24 stsp }
6214 e5a0f69f 2018-08-18 stsp
6215 51fe7530 2019-08-19 stsp if (s->blame_complete)
6216 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6217 51fe7530 2019-08-19 stsp
6218 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6219 e5a0f69f 2018-08-18 stsp
6220 9b058f45 2022-06-30 mark view_border(view);
6221 e5a0f69f 2018-08-18 stsp return err;
6222 e5a0f69f 2018-08-18 stsp }
6223 e5a0f69f 2018-08-18 stsp
6224 e5a0f69f 2018-08-18 stsp static const struct got_error *
6225 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6226 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
6227 05f04cdf 2022-07-20 mark {
6228 05f04cdf 2022-07-20 mark struct tog_view *log_view;
6229 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
6230 05f04cdf 2022-07-20 mark
6231 05f04cdf 2022-07-20 mark *new_view = NULL;
6232 05f04cdf 2022-07-20 mark
6233 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6234 05f04cdf 2022-07-20 mark if (log_view == NULL)
6235 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
6236 05f04cdf 2022-07-20 mark
6237 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6238 05f04cdf 2022-07-20 mark if (err)
6239 05f04cdf 2022-07-20 mark view_close(log_view);
6240 05f04cdf 2022-07-20 mark else
6241 05f04cdf 2022-07-20 mark *new_view = log_view;
6242 05f04cdf 2022-07-20 mark
6243 05f04cdf 2022-07-20 mark return err;
6244 05f04cdf 2022-07-20 mark }
6245 05f04cdf 2022-07-20 mark
6246 05f04cdf 2022-07-20 mark static const struct got_error *
6247 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6248 e5a0f69f 2018-08-18 stsp {
6249 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6250 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
6251 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6252 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
6253 9b058f45 2022-06-30 mark
6254 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
6255 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6256 9b058f45 2022-06-30 mark --eos; /* border */
6257 7cbe629d 2018-08-04 stsp
6258 e5a0f69f 2018-08-18 stsp switch (ch) {
6259 145b6838 2022-06-16 stsp case '0':
6260 145b6838 2022-06-16 stsp view->x = 0;
6261 145b6838 2022-06-16 stsp break;
6262 145b6838 2022-06-16 stsp case '$':
6263 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
6264 640cd7ff 2022-06-22 mark view->count = 0;
6265 145b6838 2022-06-16 stsp break;
6266 145b6838 2022-06-16 stsp case KEY_RIGHT:
6267 145b6838 2022-06-16 stsp case 'l':
6268 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
6269 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
6270 640cd7ff 2022-06-22 mark else
6271 640cd7ff 2022-06-22 mark view->count = 0;
6272 145b6838 2022-06-16 stsp break;
6273 145b6838 2022-06-16 stsp case KEY_LEFT:
6274 145b6838 2022-06-16 stsp case 'h':
6275 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
6276 640cd7ff 2022-06-22 mark if (view->x <= 0)
6277 640cd7ff 2022-06-22 mark view->count = 0;
6278 145b6838 2022-06-16 stsp break;
6279 1e37a5c2 2019-05-12 jcs case 'q':
6280 1e37a5c2 2019-05-12 jcs s->done = 1;
6281 4deef56f 2021-09-02 naddy break;
6282 4deef56f 2021-09-02 naddy case 'g':
6283 4deef56f 2021-09-02 naddy case KEY_HOME:
6284 4deef56f 2021-09-02 naddy s->selected_line = 1;
6285 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6286 640cd7ff 2022-06-22 mark view->count = 0;
6287 4deef56f 2021-09-02 naddy break;
6288 4deef56f 2021-09-02 naddy case 'G':
6289 4deef56f 2021-09-02 naddy case KEY_END:
6290 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
6291 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6292 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6293 4deef56f 2021-09-02 naddy } else {
6294 9b058f45 2022-06-30 mark s->selected_line = eos;
6295 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
6296 4deef56f 2021-09-02 naddy }
6297 640cd7ff 2022-06-22 mark view->count = 0;
6298 1e37a5c2 2019-05-12 jcs break;
6299 1e37a5c2 2019-05-12 jcs case 'k':
6300 1e37a5c2 2019-05-12 jcs case KEY_UP:
6301 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6302 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6303 1e37a5c2 2019-05-12 jcs s->selected_line--;
6304 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6305 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6306 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6307 640cd7ff 2022-06-22 mark else
6308 640cd7ff 2022-06-22 mark view->count = 0;
6309 1e37a5c2 2019-05-12 jcs break;
6310 83cc4199 2022-06-13 stsp case CTRL('u'):
6311 33c3719a 2022-06-15 stsp case 'u':
6312 83cc4199 2022-06-13 stsp nscroll /= 2;
6313 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6314 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6315 ea025d1d 2020-02-22 naddy case CTRL('b'):
6316 61417565 2022-06-20 mark case 'b':
6317 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6318 640cd7ff 2022-06-22 mark if (view->count > 1)
6319 640cd7ff 2022-06-22 mark nscroll += nscroll;
6320 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
6321 640cd7ff 2022-06-22 mark view->count = 0;
6322 e5a0f69f 2018-08-18 stsp break;
6323 1e37a5c2 2019-05-12 jcs }
6324 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
6325 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
6326 1e37a5c2 2019-05-12 jcs else
6327 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6328 1e37a5c2 2019-05-12 jcs break;
6329 1e37a5c2 2019-05-12 jcs case 'j':
6330 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6331 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6332 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
6333 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6334 1e37a5c2 2019-05-12 jcs s->selected_line++;
6335 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6336 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6337 640cd7ff 2022-06-22 mark else
6338 640cd7ff 2022-06-22 mark view->count = 0;
6339 1e37a5c2 2019-05-12 jcs break;
6340 61417565 2022-06-20 mark case 'c':
6341 1e37a5c2 2019-05-12 jcs case 'p': {
6342 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6343 640cd7ff 2022-06-22 mark
6344 640cd7ff 2022-06-22 mark view->count = 0;
6345 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6346 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6347 1e37a5c2 2019-05-12 jcs if (id == NULL)
6348 e5a0f69f 2018-08-18 stsp break;
6349 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6350 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
6351 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6352 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6353 1e37a5c2 2019-05-12 jcs int obj_type;
6354 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6355 1e37a5c2 2019-05-12 jcs s->repo, id);
6356 e5a0f69f 2018-08-18 stsp if (err)
6357 e5a0f69f 2018-08-18 stsp break;
6358 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6359 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6360 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6361 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6362 e5a0f69f 2018-08-18 stsp break;
6363 e5a0f69f 2018-08-18 stsp }
6364 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6365 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6366 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6367 a44927cc 2022-04-07 stsp if (err)
6368 a44927cc 2022-04-07 stsp break;
6369 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6370 a44927cc 2022-04-07 stsp pcommit, s->path);
6371 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6372 e5a0f69f 2018-08-18 stsp if (err) {
6373 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6374 1e37a5c2 2019-05-12 jcs err = NULL;
6375 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6376 e5a0f69f 2018-08-18 stsp break;
6377 e5a0f69f 2018-08-18 stsp }
6378 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6379 1e37a5c2 2019-05-12 jcs blob_id);
6380 1e37a5c2 2019-05-12 jcs free(blob_id);
6381 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6382 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6383 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6384 e5a0f69f 2018-08-18 stsp break;
6385 1e37a5c2 2019-05-12 jcs }
6386 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6387 d7b5a0e8 2022-04-20 stsp &pid->id);
6388 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6389 1e37a5c2 2019-05-12 jcs } else {
6390 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6391 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6392 1e37a5c2 2019-05-12 jcs break;
6393 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6394 1e37a5c2 2019-05-12 jcs id);
6395 1e37a5c2 2019-05-12 jcs }
6396 1e37a5c2 2019-05-12 jcs if (err)
6397 e5a0f69f 2018-08-18 stsp break;
6398 1e37a5c2 2019-05-12 jcs s->done = 1;
6399 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6400 1e37a5c2 2019-05-12 jcs s->done = 0;
6401 1e37a5c2 2019-05-12 jcs if (thread_err)
6402 1e37a5c2 2019-05-12 jcs break;
6403 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6404 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6405 a5388363 2020-12-01 naddy err = run_blame(view);
6406 1e37a5c2 2019-05-12 jcs if (err)
6407 1e37a5c2 2019-05-12 jcs break;
6408 1e37a5c2 2019-05-12 jcs break;
6409 1e37a5c2 2019-05-12 jcs }
6410 61417565 2022-06-20 mark case 'C': {
6411 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6412 640cd7ff 2022-06-22 mark
6413 640cd7ff 2022-06-22 mark view->count = 0;
6414 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6415 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6416 1e37a5c2 2019-05-12 jcs break;
6417 1e37a5c2 2019-05-12 jcs s->done = 1;
6418 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6419 1e37a5c2 2019-05-12 jcs s->done = 0;
6420 1e37a5c2 2019-05-12 jcs if (thread_err)
6421 1e37a5c2 2019-05-12 jcs break;
6422 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6423 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6424 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6425 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6426 a5388363 2020-12-01 naddy err = run_blame(view);
6427 1e37a5c2 2019-05-12 jcs if (err)
6428 1e37a5c2 2019-05-12 jcs break;
6429 1e37a5c2 2019-05-12 jcs break;
6430 1e37a5c2 2019-05-12 jcs }
6431 136e2bd4 2022-07-23 mark case 'L':
6432 05f04cdf 2022-07-20 mark view->count = 0;
6433 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6434 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6435 136e2bd4 2022-07-23 mark if (s->id_to_log)
6436 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6437 05f04cdf 2022-07-20 mark break;
6438 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6439 1e37a5c2 2019-05-12 jcs case '\r': {
6440 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6441 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6442 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6443 640cd7ff 2022-06-22 mark
6444 640cd7ff 2022-06-22 mark view->count = 0;
6445 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6446 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6447 1e37a5c2 2019-05-12 jcs if (id == NULL)
6448 1e37a5c2 2019-05-12 jcs break;
6449 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6450 1e37a5c2 2019-05-12 jcs if (err)
6451 1e37a5c2 2019-05-12 jcs break;
6452 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6453 c0f61fa4 2022-07-11 mark if (*new_view) {
6454 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6455 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6456 c0f61fa4 2022-07-11 mark if (err)
6457 c0f61fa4 2022-07-11 mark break;
6458 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6459 c0f61fa4 2022-07-11 mark } else {
6460 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6461 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6462 9b058f45 2022-06-30 mark
6463 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6464 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6465 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6466 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6467 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6468 c0f61fa4 2022-07-11 mark break;
6469 c0f61fa4 2022-07-11 mark }
6470 15a94983 2018-12-23 stsp }
6471 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6472 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6473 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6474 1e37a5c2 2019-05-12 jcs if (err) {
6475 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6476 1e37a5c2 2019-05-12 jcs break;
6477 1e37a5c2 2019-05-12 jcs }
6478 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6479 c0f61fa4 2022-07-11 mark s->selected_line;
6480 c0f61fa4 2022-07-11 mark if (*new_view)
6481 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6482 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6483 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6484 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6485 9b058f45 2022-06-30 mark if (err)
6486 9b058f45 2022-06-30 mark break;
6487 9b058f45 2022-06-30 mark }
6488 9b058f45 2022-06-30 mark
6489 e78dc838 2020-12-04 stsp view->focussed = 0;
6490 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6491 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6492 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6493 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6494 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6495 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6496 1e37a5c2 2019-05-12 jcs if (err)
6497 34bc9ec9 2019-02-22 stsp break;
6498 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6499 0dbbbe90 2022-06-17 op if (err)
6500 0dbbbe90 2022-06-17 op break;
6501 e78dc838 2020-12-04 stsp view->focus_child = 1;
6502 1e37a5c2 2019-05-12 jcs } else
6503 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6504 1e37a5c2 2019-05-12 jcs if (err)
6505 e5a0f69f 2018-08-18 stsp break;
6506 1e37a5c2 2019-05-12 jcs break;
6507 1e37a5c2 2019-05-12 jcs }
6508 83cc4199 2022-06-13 stsp case CTRL('d'):
6509 33c3719a 2022-06-15 stsp case 'd':
6510 83cc4199 2022-06-13 stsp nscroll /= 2;
6511 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6512 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6513 ea025d1d 2020-02-22 naddy case CTRL('f'):
6514 61417565 2022-06-20 mark case 'f':
6515 1e37a5c2 2019-05-12 jcs case ' ':
6516 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6517 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6518 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6519 640cd7ff 2022-06-22 mark view->count = 0;
6520 e5a0f69f 2018-08-18 stsp break;
6521 1e37a5c2 2019-05-12 jcs }
6522 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6523 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6524 83cc4199 2022-06-13 stsp s->selected_line +=
6525 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6526 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6527 1e37a5c2 2019-05-12 jcs }
6528 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6529 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6530 1e37a5c2 2019-05-12 jcs else
6531 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6532 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6533 1e37a5c2 2019-05-12 jcs break;
6534 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6535 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6536 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6537 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6538 1e37a5c2 2019-05-12 jcs }
6539 1e37a5c2 2019-05-12 jcs break;
6540 1e37a5c2 2019-05-12 jcs default:
6541 640cd7ff 2022-06-22 mark view->count = 0;
6542 1e37a5c2 2019-05-12 jcs break;
6543 a70480e0 2018-06-23 stsp }
6544 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6545 917d79a7 2022-07-01 stsp }
6546 917d79a7 2022-07-01 stsp
6547 917d79a7 2022-07-01 stsp static const struct got_error *
6548 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6549 917d79a7 2022-07-01 stsp {
6550 917d79a7 2022-07-01 stsp const struct got_error *err;
6551 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6552 917d79a7 2022-07-01 stsp
6553 917d79a7 2022-07-01 stsp view->count = 0;
6554 917d79a7 2022-07-01 stsp s->done = 1;
6555 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6556 917d79a7 2022-07-01 stsp s->done = 0;
6557 917d79a7 2022-07-01 stsp if (err)
6558 917d79a7 2022-07-01 stsp return err;
6559 917d79a7 2022-07-01 stsp return run_blame(view);
6560 a70480e0 2018-06-23 stsp }
6561 a70480e0 2018-06-23 stsp
6562 a70480e0 2018-06-23 stsp static const struct got_error *
6563 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6564 9f7d7167 2018-04-29 stsp {
6565 a70480e0 2018-06-23 stsp const struct got_error *error;
6566 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6567 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6568 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6569 0587e10c 2020-07-23 stsp char *link_target = NULL;
6570 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6571 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6572 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6573 a70480e0 2018-06-23 stsp int ch;
6574 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6575 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6576 a70480e0 2018-06-23 stsp
6577 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6578 a70480e0 2018-06-23 stsp switch (ch) {
6579 a70480e0 2018-06-23 stsp case 'c':
6580 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6581 a70480e0 2018-06-23 stsp break;
6582 69069811 2018-08-02 stsp case 'r':
6583 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6584 69069811 2018-08-02 stsp if (repo_path == NULL)
6585 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6586 9ba1d308 2019-10-21 stsp optarg);
6587 69069811 2018-08-02 stsp break;
6588 a70480e0 2018-06-23 stsp default:
6589 17020d27 2019-03-07 stsp usage_blame();
6590 a70480e0 2018-06-23 stsp /* NOTREACHED */
6591 a70480e0 2018-06-23 stsp }
6592 a70480e0 2018-06-23 stsp }
6593 a70480e0 2018-06-23 stsp
6594 a70480e0 2018-06-23 stsp argc -= optind;
6595 a70480e0 2018-06-23 stsp argv += optind;
6596 a70480e0 2018-06-23 stsp
6597 f135c941 2020-02-20 stsp if (argc != 1)
6598 a70480e0 2018-06-23 stsp usage_blame();
6599 6962eb72 2020-02-20 stsp
6600 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6601 0ae84acc 2022-06-15 tracey if (error != NULL)
6602 0ae84acc 2022-06-15 tracey goto done;
6603 0ae84acc 2022-06-15 tracey
6604 69069811 2018-08-02 stsp if (repo_path == NULL) {
6605 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6606 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6607 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6608 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6609 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6610 c156c7a4 2020-12-18 stsp goto done;
6611 f135c941 2020-02-20 stsp if (worktree)
6612 eb41ed75 2019-02-05 stsp repo_path =
6613 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6614 f135c941 2020-02-20 stsp else
6615 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6616 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6617 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6618 c156c7a4 2020-12-18 stsp goto done;
6619 c156c7a4 2020-12-18 stsp }
6620 f135c941 2020-02-20 stsp }
6621 a915003a 2019-02-05 stsp
6622 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6623 c02c541e 2019-03-29 stsp if (error != NULL)
6624 8e94dd5b 2019-01-04 stsp goto done;
6625 69069811 2018-08-02 stsp
6626 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6627 f135c941 2020-02-20 stsp worktree);
6628 c02c541e 2019-03-29 stsp if (error)
6629 92205607 2019-01-04 stsp goto done;
6630 69069811 2018-08-02 stsp
6631 f135c941 2020-02-20 stsp init_curses();
6632 f135c941 2020-02-20 stsp
6633 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6634 51a10b52 2020-12-26 stsp if (error)
6635 51a10b52 2020-12-26 stsp goto done;
6636 51a10b52 2020-12-26 stsp
6637 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6638 eb41ed75 2019-02-05 stsp if (error)
6639 69069811 2018-08-02 stsp goto done;
6640 a70480e0 2018-06-23 stsp
6641 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6642 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6643 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6644 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6645 a70480e0 2018-06-23 stsp if (error != NULL)
6646 66b4983c 2018-06-23 stsp goto done;
6647 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6648 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6649 a70480e0 2018-06-23 stsp } else {
6650 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6651 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6652 a70480e0 2018-06-23 stsp }
6653 a19e88aa 2018-06-23 stsp if (error != NULL)
6654 8b473291 2019-02-21 stsp goto done;
6655 8b473291 2019-02-21 stsp
6656 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6657 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6658 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6659 e1cd8fed 2018-08-01 stsp goto done;
6660 e1cd8fed 2018-08-01 stsp }
6661 0587e10c 2020-07-23 stsp
6662 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6663 a44927cc 2022-04-07 stsp if (error)
6664 a44927cc 2022-04-07 stsp goto done;
6665 a44927cc 2022-04-07 stsp
6666 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6667 a44927cc 2022-04-07 stsp commit, repo);
6668 7cbe629d 2018-08-04 stsp if (error)
6669 7cbe629d 2018-08-04 stsp goto done;
6670 0587e10c 2020-07-23 stsp
6671 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6672 78756c87 2020-11-24 stsp commit_id, repo);
6673 0587e10c 2020-07-23 stsp if (error)
6674 0587e10c 2020-07-23 stsp goto done;
6675 12314ad4 2019-08-31 stsp if (worktree) {
6676 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6677 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6678 12314ad4 2019-08-31 stsp worktree = NULL;
6679 12314ad4 2019-08-31 stsp }
6680 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6681 a70480e0 2018-06-23 stsp done:
6682 69069811 2018-08-02 stsp free(repo_path);
6683 f135c941 2020-02-20 stsp free(in_repo_path);
6684 0587e10c 2020-07-23 stsp free(link_target);
6685 69069811 2018-08-02 stsp free(cwd);
6686 a70480e0 2018-06-23 stsp free(commit_id);
6687 a44927cc 2022-04-07 stsp if (commit)
6688 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6689 eb41ed75 2019-02-05 stsp if (worktree)
6690 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6691 1d0f4054 2021-06-17 stsp if (repo) {
6692 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6693 1d0f4054 2021-06-17 stsp if (error == NULL)
6694 1d0f4054 2021-06-17 stsp error = close_err;
6695 1d0f4054 2021-06-17 stsp }
6696 0ae84acc 2022-06-15 tracey if (pack_fds) {
6697 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6698 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6699 0ae84acc 2022-06-15 tracey if (error == NULL)
6700 0ae84acc 2022-06-15 tracey error = pack_err;
6701 0ae84acc 2022-06-15 tracey }
6702 51a10b52 2020-12-26 stsp tog_free_refs();
6703 a70480e0 2018-06-23 stsp return error;
6704 ffd1d5e5 2018-06-23 stsp }
6705 ffd1d5e5 2018-06-23 stsp
6706 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6707 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6708 ffd1d5e5 2018-06-23 stsp {
6709 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6710 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6711 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6712 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6713 0c6ad1bc 2022-09-09 mark char *index = NULL;
6714 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6715 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6716 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6717 ffd1d5e5 2018-06-23 stsp
6718 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6719 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6720 9b058f45 2022-06-30 mark --limit; /* border */
6721 ffd1d5e5 2018-06-23 stsp
6722 f7d12f7e 2018-08-01 stsp werase(view->window);
6723 ffd1d5e5 2018-06-23 stsp
6724 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6725 ffd1d5e5 2018-06-23 stsp return NULL;
6726 ffd1d5e5 2018-06-23 stsp
6727 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6728 ccda2f4d 2022-06-16 stsp 0, 0);
6729 ffd1d5e5 2018-06-23 stsp if (err)
6730 ffd1d5e5 2018-06-23 stsp return err;
6731 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6732 a3404814 2018-09-02 stsp wstandout(view->window);
6733 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6734 11b20872 2019-11-08 stsp if (tc)
6735 0c6ad1bc 2022-09-09 mark wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6736 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6737 0c6ad1bc 2022-09-09 mark free(wline);
6738 0c6ad1bc 2022-09-09 mark wline = NULL;
6739 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
6740 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
6741 11b20872 2019-11-08 stsp if (tc)
6742 0c6ad1bc 2022-09-09 mark wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6743 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6744 a3404814 2018-09-02 stsp wstandend(view->window);
6745 0c6ad1bc 2022-09-09 mark if (--limit <= 0)
6746 0c6ad1bc 2022-09-09 mark return NULL;
6747 94b80cfa 2022-08-01 mark
6748 df68a56b 2022-08-12 mark i += s->selected;
6749 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6750 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6751 df68a56b 2022-08-12 mark if (s->tree != s->root)
6752 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6753 94b80cfa 2022-08-01 mark }
6754 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6755 0c6ad1bc 2022-09-09 mark if (asprintf(&index, "[%d/%d] %s",
6756 0c6ad1bc 2022-09-09 mark i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6757 0c6ad1bc 2022-09-09 mark return got_error_from_errno("asprintf");
6758 0c6ad1bc 2022-09-09 mark err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6759 0c6ad1bc 2022-09-09 mark free(index);
6760 ce52c690 2018-06-23 stsp if (err)
6761 ce52c690 2018-06-23 stsp return err;
6762 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6763 2550e4c3 2018-07-13 stsp free(wline);
6764 2550e4c3 2018-07-13 stsp wline = NULL;
6765 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6766 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6767 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6768 ffd1d5e5 2018-06-23 stsp return NULL;
6769 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6770 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6771 a1eca9bb 2018-06-23 stsp return NULL;
6772 ffd1d5e5 2018-06-23 stsp
6773 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6774 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6775 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6776 0cf4efb1 2018-09-29 stsp if (view->focussed)
6777 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6778 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6779 ffd1d5e5 2018-06-23 stsp }
6780 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6781 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6782 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6783 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6784 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6785 ffd1d5e5 2018-06-23 stsp return NULL;
6786 ffd1d5e5 2018-06-23 stsp n = 1;
6787 ffd1d5e5 2018-06-23 stsp } else {
6788 ffd1d5e5 2018-06-23 stsp n = 0;
6789 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6790 ffd1d5e5 2018-06-23 stsp }
6791 ffd1d5e5 2018-06-23 stsp
6792 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6793 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6794 848d6979 2019-08-12 stsp const char *modestr = "";
6795 56e0773d 2019-11-28 stsp mode_t mode;
6796 1d13200f 2018-07-12 stsp
6797 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6798 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6799 56e0773d 2019-11-28 stsp
6800 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6801 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6802 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6803 1d13200f 2018-07-12 stsp if (err)
6804 638f9024 2019-05-13 stsp return got_error_from_errno(
6805 230a42bd 2019-05-11 jcs "got_object_id_str");
6806 1d13200f 2018-07-12 stsp }
6807 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6808 63c5ca5d 2019-08-24 stsp modestr = "$";
6809 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6810 0d6c6ee3 2020-05-20 stsp int i;
6811 0d6c6ee3 2020-05-20 stsp
6812 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6813 d86d3b18 2020-12-01 naddy te, s->repo);
6814 0d6c6ee3 2020-05-20 stsp if (err) {
6815 0d6c6ee3 2020-05-20 stsp free(id_str);
6816 0d6c6ee3 2020-05-20 stsp return err;
6817 0d6c6ee3 2020-05-20 stsp }
6818 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6819 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6820 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6821 0d6c6ee3 2020-05-20 stsp }
6822 848d6979 2019-08-12 stsp modestr = "@";
6823 0d6c6ee3 2020-05-20 stsp }
6824 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6825 848d6979 2019-08-12 stsp modestr = "/";
6826 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6827 848d6979 2019-08-12 stsp modestr = "*";
6828 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6829 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6830 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6831 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6832 1d13200f 2018-07-12 stsp free(id_str);
6833 0d6c6ee3 2020-05-20 stsp free(link_target);
6834 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6835 1d13200f 2018-07-12 stsp }
6836 1d13200f 2018-07-12 stsp free(id_str);
6837 0d6c6ee3 2020-05-20 stsp free(link_target);
6838 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6839 ccda2f4d 2022-06-16 stsp 0, 0);
6840 ffd1d5e5 2018-06-23 stsp if (err) {
6841 ffd1d5e5 2018-06-23 stsp free(line);
6842 ffd1d5e5 2018-06-23 stsp break;
6843 ffd1d5e5 2018-06-23 stsp }
6844 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6845 0cf4efb1 2018-09-29 stsp if (view->focussed)
6846 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6847 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6848 ffd1d5e5 2018-06-23 stsp }
6849 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6850 f26dddb7 2019-11-08 stsp if (tc)
6851 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6852 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6853 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6854 f26dddb7 2019-11-08 stsp if (tc)
6855 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6856 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6857 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6858 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6859 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6860 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6861 ffd1d5e5 2018-06-23 stsp free(line);
6862 2550e4c3 2018-07-13 stsp free(wline);
6863 2550e4c3 2018-07-13 stsp wline = NULL;
6864 ffd1d5e5 2018-06-23 stsp n++;
6865 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6866 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6867 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6868 ffd1d5e5 2018-06-23 stsp break;
6869 ffd1d5e5 2018-06-23 stsp }
6870 ffd1d5e5 2018-06-23 stsp
6871 ffd1d5e5 2018-06-23 stsp return err;
6872 ffd1d5e5 2018-06-23 stsp }
6873 ffd1d5e5 2018-06-23 stsp
6874 ffd1d5e5 2018-06-23 stsp static void
6875 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6876 ffd1d5e5 2018-06-23 stsp {
6877 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6878 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6879 fa86c4bf 2020-11-29 stsp int i = 0;
6880 ffd1d5e5 2018-06-23 stsp
6881 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6882 ffd1d5e5 2018-06-23 stsp return;
6883 ffd1d5e5 2018-06-23 stsp
6884 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6885 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6886 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6887 fa86c4bf 2020-11-29 stsp if (!isroot)
6888 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6889 fa86c4bf 2020-11-29 stsp break;
6890 fa86c4bf 2020-11-29 stsp }
6891 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6892 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6893 ffd1d5e5 2018-06-23 stsp }
6894 ffd1d5e5 2018-06-23 stsp }
6895 ffd1d5e5 2018-06-23 stsp
6896 9b058f45 2022-06-30 mark static const struct got_error *
6897 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6898 ffd1d5e5 2018-06-23 stsp {
6899 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6900 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6901 ffd1d5e5 2018-06-23 stsp int n = 0;
6902 ffd1d5e5 2018-06-23 stsp
6903 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6904 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6905 694d3271 2020-12-01 naddy s->first_displayed_entry);
6906 694d3271 2020-12-01 naddy else
6907 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6908 56e0773d 2019-11-28 stsp
6909 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6910 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6911 94b80cfa 2022-08-01 mark if (last) {
6912 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6913 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6914 94b80cfa 2022-08-01 mark }
6915 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6916 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6917 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6918 768394f3 2019-01-24 stsp }
6919 ffd1d5e5 2018-06-23 stsp }
6920 9b058f45 2022-06-30 mark
6921 9b058f45 2022-06-30 mark return NULL;
6922 ffd1d5e5 2018-06-23 stsp }
6923 ffd1d5e5 2018-06-23 stsp
6924 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6925 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6926 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6927 ffd1d5e5 2018-06-23 stsp {
6928 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6929 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6930 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6931 ffd1d5e5 2018-06-23 stsp
6932 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6933 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6934 56e0773d 2019-11-28 stsp + 1 /* slash */;
6935 ce52c690 2018-06-23 stsp if (te)
6936 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6937 ce52c690 2018-06-23 stsp
6938 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6939 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6940 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6941 ffd1d5e5 2018-06-23 stsp
6942 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6943 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6944 d9765a41 2018-06-23 stsp while (pt) {
6945 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6946 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6947 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6948 cb2ebc8a 2018-06-23 stsp goto done;
6949 cb2ebc8a 2018-06-23 stsp }
6950 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6951 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6952 cb2ebc8a 2018-06-23 stsp goto done;
6953 cb2ebc8a 2018-06-23 stsp }
6954 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6955 ffd1d5e5 2018-06-23 stsp }
6956 ce52c690 2018-06-23 stsp if (te) {
6957 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6958 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6959 ce52c690 2018-06-23 stsp goto done;
6960 ce52c690 2018-06-23 stsp }
6961 cb2ebc8a 2018-06-23 stsp }
6962 ce52c690 2018-06-23 stsp done:
6963 ce52c690 2018-06-23 stsp if (err) {
6964 ce52c690 2018-06-23 stsp free(*path);
6965 ce52c690 2018-06-23 stsp *path = NULL;
6966 ce52c690 2018-06-23 stsp }
6967 ce52c690 2018-06-23 stsp return err;
6968 ce52c690 2018-06-23 stsp }
6969 ce52c690 2018-06-23 stsp
6970 ce52c690 2018-06-23 stsp static const struct got_error *
6971 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6972 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6973 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6974 ce52c690 2018-06-23 stsp {
6975 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6976 ce52c690 2018-06-23 stsp char *path;
6977 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6978 a0de39f3 2019-08-09 stsp
6979 a0de39f3 2019-08-09 stsp *new_view = NULL;
6980 69efd4c4 2018-07-18 stsp
6981 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6982 ce52c690 2018-06-23 stsp if (err)
6983 ce52c690 2018-06-23 stsp return err;
6984 ffd1d5e5 2018-06-23 stsp
6985 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6986 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6987 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6988 83ce39e3 2019-08-12 stsp goto done;
6989 83ce39e3 2019-08-12 stsp }
6990 cdf1ee82 2018-08-01 stsp
6991 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6992 e5a0f69f 2018-08-18 stsp if (err) {
6993 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6994 fc06ba56 2019-08-22 stsp err = NULL;
6995 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6996 e5a0f69f 2018-08-18 stsp } else
6997 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6998 83ce39e3 2019-08-12 stsp done:
6999 83ce39e3 2019-08-12 stsp free(path);
7000 69efd4c4 2018-07-18 stsp return err;
7001 69efd4c4 2018-07-18 stsp }
7002 69efd4c4 2018-07-18 stsp
7003 69efd4c4 2018-07-18 stsp static const struct got_error *
7004 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7005 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7006 69efd4c4 2018-07-18 stsp {
7007 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7008 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7009 69efd4c4 2018-07-18 stsp char *path;
7010 a0de39f3 2019-08-09 stsp
7011 a0de39f3 2019-08-09 stsp *new_view = NULL;
7012 69efd4c4 2018-07-18 stsp
7013 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7014 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7015 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7016 e5a0f69f 2018-08-18 stsp
7017 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7018 69efd4c4 2018-07-18 stsp if (err)
7019 69efd4c4 2018-07-18 stsp return err;
7020 69efd4c4 2018-07-18 stsp
7021 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7022 4e97c21c 2020-12-06 stsp path, 0);
7023 ba4f502b 2018-08-04 stsp if (err)
7024 e5a0f69f 2018-08-18 stsp view_close(log_view);
7025 e5a0f69f 2018-08-18 stsp else
7026 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7027 cb2ebc8a 2018-06-23 stsp free(path);
7028 cb2ebc8a 2018-06-23 stsp return err;
7029 ffd1d5e5 2018-06-23 stsp }
7030 ffd1d5e5 2018-06-23 stsp
7031 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7032 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7033 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7034 ffd1d5e5 2018-06-23 stsp {
7035 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7036 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7037 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7038 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7039 ffd1d5e5 2018-06-23 stsp
7040 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7041 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7042 bc573f3b 2021-07-10 stsp
7043 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7044 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7045 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7046 ffd1d5e5 2018-06-23 stsp
7047 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7048 bc573f3b 2021-07-10 stsp if (err)
7049 bc573f3b 2021-07-10 stsp goto done;
7050 bc573f3b 2021-07-10 stsp
7051 bc573f3b 2021-07-10 stsp /*
7052 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7053 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7054 bc573f3b 2021-07-10 stsp * closed on demand.
7055 bc573f3b 2021-07-10 stsp */
7056 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7057 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7058 bc573f3b 2021-07-10 stsp if (err)
7059 bc573f3b 2021-07-10 stsp goto done;
7060 bc573f3b 2021-07-10 stsp s->tree = s->root;
7061 bc573f3b 2021-07-10 stsp
7062 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7063 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7064 ffd1d5e5 2018-06-23 stsp goto done;
7065 ffd1d5e5 2018-06-23 stsp
7066 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7067 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7068 ffd1d5e5 2018-06-23 stsp goto done;
7069 ffd1d5e5 2018-06-23 stsp }
7070 ffd1d5e5 2018-06-23 stsp
7071 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7072 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7073 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7074 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7075 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7076 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7077 9cd7cbd1 2020-12-07 stsp goto done;
7078 9cd7cbd1 2020-12-07 stsp }
7079 9cd7cbd1 2020-12-07 stsp }
7080 fb2756b9 2018-08-04 stsp s->repo = repo;
7081 c0b01bdb 2019-11-08 stsp
7082 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7083 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7084 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7085 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7086 c0b01bdb 2019-11-08 stsp if (err)
7087 c0b01bdb 2019-11-08 stsp goto done;
7088 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7089 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7090 bc573f3b 2021-07-10 stsp if (err)
7091 c0b01bdb 2019-11-08 stsp goto done;
7092 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7093 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7094 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7095 bc573f3b 2021-07-10 stsp if (err)
7096 c0b01bdb 2019-11-08 stsp goto done;
7097 e5a0f69f 2018-08-18 stsp
7098 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7099 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7100 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7101 bc573f3b 2021-07-10 stsp if (err)
7102 11b20872 2019-11-08 stsp goto done;
7103 11b20872 2019-11-08 stsp
7104 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7105 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7106 bc573f3b 2021-07-10 stsp if (err)
7107 c0b01bdb 2019-11-08 stsp goto done;
7108 c0b01bdb 2019-11-08 stsp }
7109 c0b01bdb 2019-11-08 stsp
7110 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7111 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7112 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7113 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7114 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7115 ad80ab7b 2018-08-04 stsp done:
7116 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7117 bc573f3b 2021-07-10 stsp if (commit)
7118 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7119 bc573f3b 2021-07-10 stsp if (err)
7120 bc573f3b 2021-07-10 stsp close_tree_view(view);
7121 ad80ab7b 2018-08-04 stsp return err;
7122 ad80ab7b 2018-08-04 stsp }
7123 ad80ab7b 2018-08-04 stsp
7124 e5a0f69f 2018-08-18 stsp static const struct got_error *
7125 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7126 ad80ab7b 2018-08-04 stsp {
7127 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7128 ad80ab7b 2018-08-04 stsp
7129 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7130 fb2756b9 2018-08-04 stsp free(s->tree_label);
7131 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7132 6484ec90 2018-09-29 stsp free(s->commit_id);
7133 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7134 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7135 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7136 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7137 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7138 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7139 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7140 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7141 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7142 ad80ab7b 2018-08-04 stsp free(parent);
7143 ad80ab7b 2018-08-04 stsp
7144 ad80ab7b 2018-08-04 stsp }
7145 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7146 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7147 bc573f3b 2021-07-10 stsp if (s->root)
7148 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7149 7c32bd05 2019-06-22 stsp return NULL;
7150 7c32bd05 2019-06-22 stsp }
7151 7c32bd05 2019-06-22 stsp
7152 7c32bd05 2019-06-22 stsp static const struct got_error *
7153 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7154 7c32bd05 2019-06-22 stsp {
7155 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7156 7c32bd05 2019-06-22 stsp
7157 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7158 7c32bd05 2019-06-22 stsp return NULL;
7159 7c32bd05 2019-06-22 stsp }
7160 7c32bd05 2019-06-22 stsp
7161 7c32bd05 2019-06-22 stsp static int
7162 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7163 7c32bd05 2019-06-22 stsp {
7164 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7165 7c32bd05 2019-06-22 stsp
7166 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7167 56e0773d 2019-11-28 stsp 0) == 0;
7168 7c32bd05 2019-06-22 stsp }
7169 7c32bd05 2019-06-22 stsp
7170 7c32bd05 2019-06-22 stsp static const struct got_error *
7171 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7172 7c32bd05 2019-06-22 stsp {
7173 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7174 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7175 7c32bd05 2019-06-22 stsp
7176 7c32bd05 2019-06-22 stsp if (!view->searching) {
7177 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7178 7c32bd05 2019-06-22 stsp return NULL;
7179 7c32bd05 2019-06-22 stsp }
7180 7c32bd05 2019-06-22 stsp
7181 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7182 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7183 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7184 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7185 56e0773d 2019-11-28 stsp s->selected_entry);
7186 7c32bd05 2019-06-22 stsp else
7187 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7188 56e0773d 2019-11-28 stsp } else {
7189 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7190 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7191 56e0773d 2019-11-28 stsp else
7192 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7193 56e0773d 2019-11-28 stsp s->selected_entry);
7194 7c32bd05 2019-06-22 stsp }
7195 7c32bd05 2019-06-22 stsp } else {
7196 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7197 487cd7d2 2021-12-17 stsp te = s->selected_entry;
7198 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7199 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7200 56e0773d 2019-11-28 stsp else
7201 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7202 7c32bd05 2019-06-22 stsp }
7203 7c32bd05 2019-06-22 stsp
7204 7c32bd05 2019-06-22 stsp while (1) {
7205 56e0773d 2019-11-28 stsp if (te == NULL) {
7206 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7207 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7208 ac66afb8 2019-06-24 stsp return NULL;
7209 ac66afb8 2019-06-24 stsp }
7210 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7211 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7212 56e0773d 2019-11-28 stsp else
7213 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7214 7c32bd05 2019-06-22 stsp }
7215 7c32bd05 2019-06-22 stsp
7216 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7217 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7218 56e0773d 2019-11-28 stsp s->matched_entry = te;
7219 7c32bd05 2019-06-22 stsp break;
7220 7c32bd05 2019-06-22 stsp }
7221 7c32bd05 2019-06-22 stsp
7222 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7223 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7224 56e0773d 2019-11-28 stsp else
7225 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7226 7c32bd05 2019-06-22 stsp }
7227 e5a0f69f 2018-08-18 stsp
7228 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7229 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7230 7c32bd05 2019-06-22 stsp s->selected = 0;
7231 7c32bd05 2019-06-22 stsp }
7232 7c32bd05 2019-06-22 stsp
7233 e5a0f69f 2018-08-18 stsp return NULL;
7234 ad80ab7b 2018-08-04 stsp }
7235 ad80ab7b 2018-08-04 stsp
7236 ad80ab7b 2018-08-04 stsp static const struct got_error *
7237 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7238 ad80ab7b 2018-08-04 stsp {
7239 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7240 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7241 e5a0f69f 2018-08-18 stsp char *parent_path;
7242 ad80ab7b 2018-08-04 stsp
7243 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7244 e5a0f69f 2018-08-18 stsp if (err)
7245 e5a0f69f 2018-08-18 stsp return err;
7246 ffd1d5e5 2018-06-23 stsp
7247 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7248 e5a0f69f 2018-08-18 stsp free(parent_path);
7249 669b5ffa 2018-10-07 stsp
7250 9b058f45 2022-06-30 mark view_border(view);
7251 e5a0f69f 2018-08-18 stsp return err;
7252 94b80cfa 2022-08-01 mark }
7253 94b80cfa 2022-08-01 mark
7254 94b80cfa 2022-08-01 mark static const struct got_error *
7255 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
7256 94b80cfa 2022-08-01 mark {
7257 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7258 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
7259 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
7260 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
7261 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
7262 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
7263 94b80cfa 2022-08-01 mark
7264 94b80cfa 2022-08-01 mark g = view->gline;
7265 94b80cfa 2022-08-01 mark view->gline = 0;
7266 94b80cfa 2022-08-01 mark
7267 94b80cfa 2022-08-01 mark if (g == 0)
7268 94b80cfa 2022-08-01 mark g = 1;
7269 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
7270 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7271 94b80cfa 2022-08-01 mark
7272 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
7273 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
7274 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
7275 94b80cfa 2022-08-01 mark
7276 94b80cfa 2022-08-01 mark if (*fte != NULL) {
7277 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7278 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
7279 94b80cfa 2022-08-01 mark }
7280 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
7281 94b80cfa 2022-08-01 mark last += off;
7282 94b80cfa 2022-08-01 mark
7283 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
7284 94b80cfa 2022-08-01 mark s->selected = g - first;
7285 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
7286 94b80cfa 2022-08-01 mark }
7287 94b80cfa 2022-08-01 mark
7288 94b80cfa 2022-08-01 mark if (*ste != NULL) {
7289 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
7290 94b80cfa 2022-08-01 mark i += off;
7291 94b80cfa 2022-08-01 mark }
7292 94b80cfa 2022-08-01 mark
7293 94b80cfa 2022-08-01 mark if (i < g) {
7294 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
7295 94b80cfa 2022-08-01 mark if (err)
7296 94b80cfa 2022-08-01 mark return err;
7297 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
7298 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
7299 94b80cfa 2022-08-01 mark first + s->selected < g &&
7300 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
7301 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
7302 94b80cfa 2022-08-01 mark first += off;
7303 94b80cfa 2022-08-01 mark s->selected = g - first;
7304 94b80cfa 2022-08-01 mark }
7305 94b80cfa 2022-08-01 mark } else if (i > g)
7306 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
7307 94b80cfa 2022-08-01 mark
7308 94b80cfa 2022-08-01 mark if (g < nlines &&
7309 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7310 94b80cfa 2022-08-01 mark s->selected = g - 1;
7311 94b80cfa 2022-08-01 mark
7312 94b80cfa 2022-08-01 mark return NULL;
7313 e5a0f69f 2018-08-18 stsp }
7314 ce52c690 2018-06-23 stsp
7315 e5a0f69f 2018-08-18 stsp static const struct got_error *
7316 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7317 e5a0f69f 2018-08-18 stsp {
7318 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7319 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7320 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7321 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
7322 ffd1d5e5 2018-06-23 stsp
7323 94b80cfa 2022-08-01 mark if (view->gline)
7324 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
7325 94b80cfa 2022-08-01 mark
7326 e5a0f69f 2018-08-18 stsp switch (ch) {
7327 1e37a5c2 2019-05-12 jcs case 'i':
7328 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7329 640cd7ff 2022-06-22 mark view->count = 0;
7330 1e37a5c2 2019-05-12 jcs break;
7331 5e98fb33 2022-07-22 mark case 'L':
7332 640cd7ff 2022-06-22 mark view->count = 0;
7333 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7334 ffd1d5e5 2018-06-23 stsp break;
7335 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7336 152c1c93 2020-11-29 stsp break;
7337 5e98fb33 2022-07-22 mark case 'R':
7338 640cd7ff 2022-06-22 mark view->count = 0;
7339 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
7340 e4526bf5 2021-09-03 naddy break;
7341 e4526bf5 2021-09-03 naddy case 'g':
7342 0b3f028d 2023-01-09 mark case '=':
7343 e4526bf5 2021-09-03 naddy case KEY_HOME:
7344 e4526bf5 2021-09-03 naddy s->selected = 0;
7345 640cd7ff 2022-06-22 mark view->count = 0;
7346 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7347 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7348 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7349 e4526bf5 2021-09-03 naddy else
7350 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7351 1e37a5c2 2019-05-12 jcs break;
7352 e4526bf5 2021-09-03 naddy case 'G':
7353 0b3f028d 2023-01-09 mark case '*':
7354 9b058f45 2022-06-30 mark case KEY_END: {
7355 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
7356 9b058f45 2022-06-30 mark
7357 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7358 9b058f45 2022-06-30 mark --eos; /* border */
7359 e4526bf5 2021-09-03 naddy s->selected = 0;
7360 640cd7ff 2022-06-22 mark view->count = 0;
7361 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7362 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7363 e4526bf5 2021-09-03 naddy if (te == NULL) {
7364 ad055527 2022-07-16 mark if (s->tree != s->root) {
7365 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7366 e4526bf5 2021-09-03 naddy n++;
7367 e4526bf5 2021-09-03 naddy }
7368 e4526bf5 2021-09-03 naddy break;
7369 e4526bf5 2021-09-03 naddy }
7370 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7371 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7372 e4526bf5 2021-09-03 naddy }
7373 e4526bf5 2021-09-03 naddy if (n > 0)
7374 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7375 e4526bf5 2021-09-03 naddy break;
7376 9b058f45 2022-06-30 mark }
7377 1e37a5c2 2019-05-12 jcs case 'k':
7378 1e37a5c2 2019-05-12 jcs case KEY_UP:
7379 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7380 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7381 1e37a5c2 2019-05-12 jcs s->selected--;
7382 fa86c4bf 2020-11-29 stsp break;
7383 1e37a5c2 2019-05-12 jcs }
7384 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7385 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7386 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7387 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7388 640cd7ff 2022-06-22 mark view->count = 0;
7389 1e37a5c2 2019-05-12 jcs break;
7390 83cc4199 2022-06-13 stsp case CTRL('u'):
7391 33c3719a 2022-06-15 stsp case 'u':
7392 83cc4199 2022-06-13 stsp nscroll /= 2;
7393 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7394 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7395 ea025d1d 2020-02-22 naddy case CTRL('b'):
7396 61417565 2022-06-20 mark case 'b':
7397 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7398 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7399 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7400 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7401 fa86c4bf 2020-11-29 stsp } else {
7402 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7403 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7404 fa86c4bf 2020-11-29 stsp }
7405 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7406 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7407 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7408 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7409 640cd7ff 2022-06-22 mark view->count = 0;
7410 1e37a5c2 2019-05-12 jcs break;
7411 1e37a5c2 2019-05-12 jcs case 'j':
7412 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7413 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7414 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7415 1e37a5c2 2019-05-12 jcs s->selected++;
7416 1e37a5c2 2019-05-12 jcs break;
7417 1e37a5c2 2019-05-12 jcs }
7418 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7419 640cd7ff 2022-06-22 mark == NULL) {
7420 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7421 640cd7ff 2022-06-22 mark view->count = 0;
7422 1e37a5c2 2019-05-12 jcs break;
7423 640cd7ff 2022-06-22 mark }
7424 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7425 1e37a5c2 2019-05-12 jcs break;
7426 83cc4199 2022-06-13 stsp case CTRL('d'):
7427 33c3719a 2022-06-15 stsp case 'd':
7428 83cc4199 2022-06-13 stsp nscroll /= 2;
7429 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7430 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7431 ea025d1d 2020-02-22 naddy case CTRL('f'):
7432 61417565 2022-06-20 mark case 'f':
7433 48bb96f0 2022-06-20 naddy case ' ':
7434 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7435 1e37a5c2 2019-05-12 jcs == NULL) {
7436 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7437 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7438 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7439 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7440 640cd7ff 2022-06-22 mark else
7441 640cd7ff 2022-06-22 mark view->count = 0;
7442 1e37a5c2 2019-05-12 jcs break;
7443 1e37a5c2 2019-05-12 jcs }
7444 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7445 1e37a5c2 2019-05-12 jcs break;
7446 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7447 1e37a5c2 2019-05-12 jcs case '\r':
7448 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7449 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7450 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7451 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7452 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7453 640cd7ff 2022-06-22 mark view->count = 0;
7454 1e37a5c2 2019-05-12 jcs break;
7455 640cd7ff 2022-06-22 mark }
7456 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7457 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7458 1e37a5c2 2019-05-12 jcs entry);
7459 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7460 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7461 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7462 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7463 1e37a5c2 2019-05-12 jcs s->selected_entry =
7464 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7465 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7466 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7467 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7468 9b058f45 2022-06-30 mark if (err)
7469 9b058f45 2022-06-30 mark break;
7470 9b058f45 2022-06-30 mark }
7471 1e37a5c2 2019-05-12 jcs free(parent);
7472 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7473 56e0773d 2019-11-28 stsp s->selected_entry))) {
7474 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7475 640cd7ff 2022-06-22 mark view->count = 0;
7476 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7477 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7478 1e37a5c2 2019-05-12 jcs if (err)
7479 1e37a5c2 2019-05-12 jcs break;
7480 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7481 941e9f74 2019-05-21 stsp if (err) {
7482 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7483 1e37a5c2 2019-05-12 jcs break;
7484 1e37a5c2 2019-05-12 jcs }
7485 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7486 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7487 1e37a5c2 2019-05-12 jcs break;
7488 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7489 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7490 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7491 640cd7ff 2022-06-22 mark view->count = 0;
7492 1e37a5c2 2019-05-12 jcs break;
7493 1e37a5c2 2019-05-12 jcs default:
7494 640cd7ff 2022-06-22 mark view->count = 0;
7495 1e37a5c2 2019-05-12 jcs break;
7496 ffd1d5e5 2018-06-23 stsp }
7497 e5a0f69f 2018-08-18 stsp
7498 ffd1d5e5 2018-06-23 stsp return err;
7499 9f7d7167 2018-04-29 stsp }
7500 9f7d7167 2018-04-29 stsp
7501 ffd1d5e5 2018-06-23 stsp __dead static void
7502 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7503 ffd1d5e5 2018-06-23 stsp {
7504 ffd1d5e5 2018-06-23 stsp endwin();
7505 87411fa9 2022-06-16 stsp fprintf(stderr,
7506 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7507 ffd1d5e5 2018-06-23 stsp getprogname());
7508 ffd1d5e5 2018-06-23 stsp exit(1);
7509 ffd1d5e5 2018-06-23 stsp }
7510 ffd1d5e5 2018-06-23 stsp
7511 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7512 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7513 ffd1d5e5 2018-06-23 stsp {
7514 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7515 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7516 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7517 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7518 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7519 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7520 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7521 4e97c21c 2020-12-06 stsp char *label = NULL;
7522 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7523 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7524 ffd1d5e5 2018-06-23 stsp int ch;
7525 5221c383 2018-08-01 stsp struct tog_view *view;
7526 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7527 70ac5f84 2019-03-28 stsp
7528 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7529 ffd1d5e5 2018-06-23 stsp switch (ch) {
7530 ffd1d5e5 2018-06-23 stsp case 'c':
7531 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7532 74283ab8 2020-02-07 stsp break;
7533 74283ab8 2020-02-07 stsp case 'r':
7534 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7535 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7536 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7537 74283ab8 2020-02-07 stsp optarg);
7538 ffd1d5e5 2018-06-23 stsp break;
7539 ffd1d5e5 2018-06-23 stsp default:
7540 e99e2d15 2020-11-24 naddy usage_tree();
7541 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7542 ffd1d5e5 2018-06-23 stsp }
7543 ffd1d5e5 2018-06-23 stsp }
7544 ffd1d5e5 2018-06-23 stsp
7545 ffd1d5e5 2018-06-23 stsp argc -= optind;
7546 ffd1d5e5 2018-06-23 stsp argv += optind;
7547 ffd1d5e5 2018-06-23 stsp
7548 55cccc34 2020-02-20 stsp if (argc > 1)
7549 e99e2d15 2020-11-24 naddy usage_tree();
7550 0ae84acc 2022-06-15 tracey
7551 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7552 0ae84acc 2022-06-15 tracey if (error != NULL)
7553 0ae84acc 2022-06-15 tracey goto done;
7554 74283ab8 2020-02-07 stsp
7555 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7556 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7557 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7558 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7559 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7560 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7561 c156c7a4 2020-12-18 stsp goto done;
7562 55cccc34 2020-02-20 stsp if (worktree)
7563 52185f70 2019-02-05 stsp repo_path =
7564 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7565 55cccc34 2020-02-20 stsp else
7566 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7567 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7568 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7569 c156c7a4 2020-12-18 stsp goto done;
7570 c156c7a4 2020-12-18 stsp }
7571 55cccc34 2020-02-20 stsp }
7572 a915003a 2019-02-05 stsp
7573 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7574 c02c541e 2019-03-29 stsp if (error != NULL)
7575 52185f70 2019-02-05 stsp goto done;
7576 d188b9a6 2019-01-04 stsp
7577 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7578 55cccc34 2020-02-20 stsp repo, worktree);
7579 55cccc34 2020-02-20 stsp if (error)
7580 55cccc34 2020-02-20 stsp goto done;
7581 55cccc34 2020-02-20 stsp
7582 55cccc34 2020-02-20 stsp init_curses();
7583 55cccc34 2020-02-20 stsp
7584 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7585 c02c541e 2019-03-29 stsp if (error)
7586 52185f70 2019-02-05 stsp goto done;
7587 ffd1d5e5 2018-06-23 stsp
7588 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7589 51a10b52 2020-12-26 stsp if (error)
7590 51a10b52 2020-12-26 stsp goto done;
7591 51a10b52 2020-12-26 stsp
7592 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7593 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7594 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7595 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7596 4e97c21c 2020-12-06 stsp if (error)
7597 4e97c21c 2020-12-06 stsp goto done;
7598 4e97c21c 2020-12-06 stsp head_ref_name = label;
7599 4e97c21c 2020-12-06 stsp } else {
7600 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7601 4e97c21c 2020-12-06 stsp if (error == NULL)
7602 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7603 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7604 4e97c21c 2020-12-06 stsp goto done;
7605 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7606 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7607 4e97c21c 2020-12-06 stsp if (error)
7608 4e97c21c 2020-12-06 stsp goto done;
7609 4e97c21c 2020-12-06 stsp }
7610 ffd1d5e5 2018-06-23 stsp
7611 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7612 a44927cc 2022-04-07 stsp if (error)
7613 a44927cc 2022-04-07 stsp goto done;
7614 a44927cc 2022-04-07 stsp
7615 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7616 5221c383 2018-08-01 stsp if (view == NULL) {
7617 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7618 5221c383 2018-08-01 stsp goto done;
7619 5221c383 2018-08-01 stsp }
7620 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7621 ad80ab7b 2018-08-04 stsp if (error)
7622 ad80ab7b 2018-08-04 stsp goto done;
7623 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7624 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7625 d91faf3b 2020-12-01 naddy in_repo_path);
7626 55cccc34 2020-02-20 stsp if (error)
7627 55cccc34 2020-02-20 stsp goto done;
7628 55cccc34 2020-02-20 stsp }
7629 55cccc34 2020-02-20 stsp
7630 55cccc34 2020-02-20 stsp if (worktree) {
7631 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7632 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7633 55cccc34 2020-02-20 stsp worktree = NULL;
7634 55cccc34 2020-02-20 stsp }
7635 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7636 ffd1d5e5 2018-06-23 stsp done:
7637 52185f70 2019-02-05 stsp free(repo_path);
7638 e4a0e26d 2020-02-20 stsp free(cwd);
7639 ffd1d5e5 2018-06-23 stsp free(commit_id);
7640 4e97c21c 2020-12-06 stsp free(label);
7641 486cd271 2020-12-06 stsp if (ref)
7642 486cd271 2020-12-06 stsp got_ref_close(ref);
7643 1d0f4054 2021-06-17 stsp if (repo) {
7644 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7645 1d0f4054 2021-06-17 stsp if (error == NULL)
7646 1d0f4054 2021-06-17 stsp error = close_err;
7647 1d0f4054 2021-06-17 stsp }
7648 0ae84acc 2022-06-15 tracey if (pack_fds) {
7649 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7650 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7651 0ae84acc 2022-06-15 tracey if (error == NULL)
7652 0ae84acc 2022-06-15 tracey error = pack_err;
7653 0ae84acc 2022-06-15 tracey }
7654 51a10b52 2020-12-26 stsp tog_free_refs();
7655 ffd1d5e5 2018-06-23 stsp return error;
7656 6458efa5 2020-11-24 stsp }
7657 6458efa5 2020-11-24 stsp
7658 6458efa5 2020-11-24 stsp static const struct got_error *
7659 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7660 6458efa5 2020-11-24 stsp {
7661 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7662 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7663 6458efa5 2020-11-24 stsp
7664 6458efa5 2020-11-24 stsp s->nrefs = 0;
7665 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7666 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7667 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7668 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7669 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7670 6458efa5 2020-11-24 stsp continue;
7671 6458efa5 2020-11-24 stsp
7672 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7673 6458efa5 2020-11-24 stsp if (re == NULL)
7674 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7675 6458efa5 2020-11-24 stsp
7676 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7677 8924d611 2020-12-26 stsp if (re->ref == NULL)
7678 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7679 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7680 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7681 6458efa5 2020-11-24 stsp }
7682 6458efa5 2020-11-24 stsp
7683 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7684 6458efa5 2020-11-24 stsp return NULL;
7685 6458efa5 2020-11-24 stsp }
7686 6458efa5 2020-11-24 stsp
7687 336075a4 2022-06-25 op static void
7688 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7689 6458efa5 2020-11-24 stsp {
7690 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7691 6458efa5 2020-11-24 stsp
7692 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7693 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7694 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7695 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7696 6458efa5 2020-11-24 stsp free(re);
7697 6458efa5 2020-11-24 stsp }
7698 6458efa5 2020-11-24 stsp }
7699 6458efa5 2020-11-24 stsp
7700 6458efa5 2020-11-24 stsp static const struct got_error *
7701 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7702 6458efa5 2020-11-24 stsp {
7703 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7704 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7705 6458efa5 2020-11-24 stsp
7706 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7707 6458efa5 2020-11-24 stsp s->repo = repo;
7708 6458efa5 2020-11-24 stsp
7709 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7710 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7711 6458efa5 2020-11-24 stsp
7712 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7713 6458efa5 2020-11-24 stsp if (err)
7714 6458efa5 2020-11-24 stsp return err;
7715 34ba6917 2020-11-30 stsp
7716 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7717 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7718 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7719 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7720 6458efa5 2020-11-24 stsp if (err)
7721 6458efa5 2020-11-24 stsp goto done;
7722 6458efa5 2020-11-24 stsp
7723 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7724 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7725 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7726 6458efa5 2020-11-24 stsp if (err)
7727 6458efa5 2020-11-24 stsp goto done;
7728 6458efa5 2020-11-24 stsp
7729 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7730 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7731 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7732 cc488aa7 2022-01-23 stsp if (err)
7733 cc488aa7 2022-01-23 stsp goto done;
7734 cc488aa7 2022-01-23 stsp
7735 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7736 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7737 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7738 6458efa5 2020-11-24 stsp if (err)
7739 6458efa5 2020-11-24 stsp goto done;
7740 6458efa5 2020-11-24 stsp }
7741 6458efa5 2020-11-24 stsp
7742 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7743 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7744 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7745 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7746 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7747 6458efa5 2020-11-24 stsp done:
7748 6458efa5 2020-11-24 stsp if (err)
7749 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7750 6458efa5 2020-11-24 stsp return err;
7751 6458efa5 2020-11-24 stsp }
7752 6458efa5 2020-11-24 stsp
7753 6458efa5 2020-11-24 stsp static const struct got_error *
7754 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7755 6458efa5 2020-11-24 stsp {
7756 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7757 6458efa5 2020-11-24 stsp
7758 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7759 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7760 6458efa5 2020-11-24 stsp
7761 6458efa5 2020-11-24 stsp return NULL;
7762 9f7d7167 2018-04-29 stsp }
7763 ce5b7c56 2019-07-09 stsp
7764 6458efa5 2020-11-24 stsp static const struct got_error *
7765 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7766 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7767 6458efa5 2020-11-24 stsp {
7768 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7769 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7770 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7771 6458efa5 2020-11-24 stsp int obj_type;
7772 6458efa5 2020-11-24 stsp
7773 c42c9805 2020-11-24 stsp *commit_id = NULL;
7774 6458efa5 2020-11-24 stsp
7775 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7776 6458efa5 2020-11-24 stsp if (err)
7777 6458efa5 2020-11-24 stsp return err;
7778 6458efa5 2020-11-24 stsp
7779 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7780 6458efa5 2020-11-24 stsp if (err)
7781 6458efa5 2020-11-24 stsp goto done;
7782 6458efa5 2020-11-24 stsp
7783 6458efa5 2020-11-24 stsp switch (obj_type) {
7784 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7785 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7786 6458efa5 2020-11-24 stsp break;
7787 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7788 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7789 6458efa5 2020-11-24 stsp if (err)
7790 6458efa5 2020-11-24 stsp goto done;
7791 c42c9805 2020-11-24 stsp free(obj_id);
7792 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7793 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7794 c42c9805 2020-11-24 stsp if (err)
7795 6458efa5 2020-11-24 stsp goto done;
7796 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7797 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7798 c42c9805 2020-11-24 stsp goto done;
7799 c42c9805 2020-11-24 stsp }
7800 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7801 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7802 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7803 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7804 c42c9805 2020-11-24 stsp goto done;
7805 c42c9805 2020-11-24 stsp }
7806 6458efa5 2020-11-24 stsp break;
7807 6458efa5 2020-11-24 stsp default:
7808 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7809 c42c9805 2020-11-24 stsp break;
7810 6458efa5 2020-11-24 stsp }
7811 6458efa5 2020-11-24 stsp
7812 c42c9805 2020-11-24 stsp done:
7813 c42c9805 2020-11-24 stsp if (tag)
7814 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7815 c42c9805 2020-11-24 stsp if (err) {
7816 c42c9805 2020-11-24 stsp free(*commit_id);
7817 c42c9805 2020-11-24 stsp *commit_id = NULL;
7818 c42c9805 2020-11-24 stsp }
7819 c42c9805 2020-11-24 stsp return err;
7820 c42c9805 2020-11-24 stsp }
7821 c42c9805 2020-11-24 stsp
7822 c42c9805 2020-11-24 stsp static const struct got_error *
7823 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7824 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7825 c42c9805 2020-11-24 stsp {
7826 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7827 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7828 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7829 c42c9805 2020-11-24 stsp
7830 c42c9805 2020-11-24 stsp *new_view = NULL;
7831 c42c9805 2020-11-24 stsp
7832 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7833 c42c9805 2020-11-24 stsp if (err) {
7834 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7835 c42c9805 2020-11-24 stsp return err;
7836 c42c9805 2020-11-24 stsp else
7837 c42c9805 2020-11-24 stsp return NULL;
7838 c42c9805 2020-11-24 stsp }
7839 c42c9805 2020-11-24 stsp
7840 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7841 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7842 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7843 6458efa5 2020-11-24 stsp goto done;
7844 6458efa5 2020-11-24 stsp }
7845 6458efa5 2020-11-24 stsp
7846 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7847 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7848 6458efa5 2020-11-24 stsp done:
7849 6458efa5 2020-11-24 stsp if (err)
7850 6458efa5 2020-11-24 stsp view_close(log_view);
7851 6458efa5 2020-11-24 stsp else
7852 6458efa5 2020-11-24 stsp *new_view = log_view;
7853 c42c9805 2020-11-24 stsp free(commit_id);
7854 6458efa5 2020-11-24 stsp return err;
7855 6458efa5 2020-11-24 stsp }
7856 6458efa5 2020-11-24 stsp
7857 ce5b7c56 2019-07-09 stsp static void
7858 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7859 6458efa5 2020-11-24 stsp {
7860 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7861 34ba6917 2020-11-30 stsp int i = 0;
7862 6458efa5 2020-11-24 stsp
7863 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7864 6458efa5 2020-11-24 stsp return;
7865 6458efa5 2020-11-24 stsp
7866 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7867 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7868 34ba6917 2020-11-30 stsp if (re == NULL)
7869 34ba6917 2020-11-30 stsp break;
7870 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7871 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7872 6458efa5 2020-11-24 stsp }
7873 6458efa5 2020-11-24 stsp }
7874 6458efa5 2020-11-24 stsp
7875 9b058f45 2022-06-30 mark static const struct got_error *
7876 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7877 6458efa5 2020-11-24 stsp {
7878 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7879 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7880 6458efa5 2020-11-24 stsp int n = 0;
7881 6458efa5 2020-11-24 stsp
7882 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7883 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7884 6458efa5 2020-11-24 stsp else
7885 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7886 6458efa5 2020-11-24 stsp
7887 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7888 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7889 94b80cfa 2022-08-01 mark if (last) {
7890 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7891 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7892 94b80cfa 2022-08-01 mark }
7893 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7894 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7895 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7896 6458efa5 2020-11-24 stsp }
7897 6458efa5 2020-11-24 stsp }
7898 9b058f45 2022-06-30 mark
7899 9b058f45 2022-06-30 mark return NULL;
7900 6458efa5 2020-11-24 stsp }
7901 6458efa5 2020-11-24 stsp
7902 6458efa5 2020-11-24 stsp static const struct got_error *
7903 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7904 6458efa5 2020-11-24 stsp {
7905 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7906 6458efa5 2020-11-24 stsp
7907 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7908 6458efa5 2020-11-24 stsp return NULL;
7909 6458efa5 2020-11-24 stsp }
7910 6458efa5 2020-11-24 stsp
7911 6458efa5 2020-11-24 stsp static int
7912 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7913 6458efa5 2020-11-24 stsp {
7914 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7915 6458efa5 2020-11-24 stsp
7916 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7917 6458efa5 2020-11-24 stsp 0) == 0;
7918 6458efa5 2020-11-24 stsp }
7919 6458efa5 2020-11-24 stsp
7920 6458efa5 2020-11-24 stsp static const struct got_error *
7921 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7922 6458efa5 2020-11-24 stsp {
7923 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7924 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7925 6458efa5 2020-11-24 stsp
7926 6458efa5 2020-11-24 stsp if (!view->searching) {
7927 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7928 6458efa5 2020-11-24 stsp return NULL;
7929 6458efa5 2020-11-24 stsp }
7930 6458efa5 2020-11-24 stsp
7931 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7932 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7933 6458efa5 2020-11-24 stsp if (s->selected_entry)
7934 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7935 6458efa5 2020-11-24 stsp else
7936 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7937 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7938 6458efa5 2020-11-24 stsp } else {
7939 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7940 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7941 6458efa5 2020-11-24 stsp else
7942 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7943 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7944 6458efa5 2020-11-24 stsp }
7945 6458efa5 2020-11-24 stsp } else {
7946 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7947 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7948 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7949 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7950 6458efa5 2020-11-24 stsp else
7951 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7952 6458efa5 2020-11-24 stsp }
7953 6458efa5 2020-11-24 stsp
7954 6458efa5 2020-11-24 stsp while (1) {
7955 6458efa5 2020-11-24 stsp if (re == NULL) {
7956 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7957 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7958 6458efa5 2020-11-24 stsp return NULL;
7959 6458efa5 2020-11-24 stsp }
7960 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7961 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7962 6458efa5 2020-11-24 stsp else
7963 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7964 6458efa5 2020-11-24 stsp }
7965 6458efa5 2020-11-24 stsp
7966 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7967 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7968 6458efa5 2020-11-24 stsp s->matched_entry = re;
7969 6458efa5 2020-11-24 stsp break;
7970 6458efa5 2020-11-24 stsp }
7971 6458efa5 2020-11-24 stsp
7972 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7973 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7974 6458efa5 2020-11-24 stsp else
7975 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7976 6458efa5 2020-11-24 stsp }
7977 6458efa5 2020-11-24 stsp
7978 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7979 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7980 6458efa5 2020-11-24 stsp s->selected = 0;
7981 6458efa5 2020-11-24 stsp }
7982 6458efa5 2020-11-24 stsp
7983 6458efa5 2020-11-24 stsp return NULL;
7984 6458efa5 2020-11-24 stsp }
7985 6458efa5 2020-11-24 stsp
7986 6458efa5 2020-11-24 stsp static const struct got_error *
7987 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7988 6458efa5 2020-11-24 stsp {
7989 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7990 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7991 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7992 6458efa5 2020-11-24 stsp char *line = NULL;
7993 6458efa5 2020-11-24 stsp wchar_t *wline;
7994 6458efa5 2020-11-24 stsp struct tog_color *tc;
7995 6458efa5 2020-11-24 stsp int width, n;
7996 6458efa5 2020-11-24 stsp int limit = view->nlines;
7997 6458efa5 2020-11-24 stsp
7998 6458efa5 2020-11-24 stsp werase(view->window);
7999 6458efa5 2020-11-24 stsp
8000 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8001 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
8002 9b058f45 2022-06-30 mark --limit; /* border */
8003 6458efa5 2020-11-24 stsp
8004 6458efa5 2020-11-24 stsp if (limit == 0)
8005 6458efa5 2020-11-24 stsp return NULL;
8006 6458efa5 2020-11-24 stsp
8007 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8008 6458efa5 2020-11-24 stsp
8009 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8010 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8011 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8012 6458efa5 2020-11-24 stsp
8013 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8014 6458efa5 2020-11-24 stsp if (err) {
8015 6458efa5 2020-11-24 stsp free(line);
8016 6458efa5 2020-11-24 stsp return err;
8017 6458efa5 2020-11-24 stsp }
8018 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8019 6458efa5 2020-11-24 stsp wstandout(view->window);
8020 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8021 0c6ad1bc 2022-09-09 mark while (width++ < view->ncols)
8022 0c6ad1bc 2022-09-09 mark waddch(view->window, ' ');
8023 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8024 6458efa5 2020-11-24 stsp wstandend(view->window);
8025 6458efa5 2020-11-24 stsp free(wline);
8026 6458efa5 2020-11-24 stsp wline = NULL;
8027 6458efa5 2020-11-24 stsp free(line);
8028 6458efa5 2020-11-24 stsp line = NULL;
8029 6458efa5 2020-11-24 stsp if (--limit <= 0)
8030 6458efa5 2020-11-24 stsp return NULL;
8031 6458efa5 2020-11-24 stsp
8032 6458efa5 2020-11-24 stsp n = 0;
8033 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8034 6458efa5 2020-11-24 stsp char *line = NULL;
8035 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8036 6458efa5 2020-11-24 stsp
8037 b4996bee 2022-06-16 stsp if (s->show_date) {
8038 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
8039 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
8040 b4996bee 2022-06-16 stsp struct got_object_id *id;
8041 b4996bee 2022-06-16 stsp struct tm tm;
8042 b4996bee 2022-06-16 stsp time_t t;
8043 b4996bee 2022-06-16 stsp
8044 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8045 b4996bee 2022-06-16 stsp if (err)
8046 b4996bee 2022-06-16 stsp return err;
8047 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
8048 b4996bee 2022-06-16 stsp if (err) {
8049 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
8050 b4996bee 2022-06-16 stsp free(id);
8051 b4996bee 2022-06-16 stsp return err;
8052 b4996bee 2022-06-16 stsp }
8053 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
8054 b4996bee 2022-06-16 stsp id);
8055 b4996bee 2022-06-16 stsp if (err) {
8056 b4996bee 2022-06-16 stsp free(id);
8057 b4996bee 2022-06-16 stsp return err;
8058 b4996bee 2022-06-16 stsp }
8059 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
8060 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
8061 b4996bee 2022-06-16 stsp } else {
8062 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
8063 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
8064 b4996bee 2022-06-16 stsp }
8065 b4996bee 2022-06-16 stsp free(id);
8066 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
8067 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
8068 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8069 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
8070 b4996bee 2022-06-16 stsp }
8071 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8072 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
8073 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
8074 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8075 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8076 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8077 6458efa5 2020-11-24 stsp struct got_object_id *id;
8078 6458efa5 2020-11-24 stsp char *id_str;
8079 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8080 6458efa5 2020-11-24 stsp if (err)
8081 6458efa5 2020-11-24 stsp return err;
8082 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8083 6458efa5 2020-11-24 stsp if (err) {
8084 6458efa5 2020-11-24 stsp free(id);
8085 6458efa5 2020-11-24 stsp return err;
8086 6458efa5 2020-11-24 stsp }
8087 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8088 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8089 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8090 6458efa5 2020-11-24 stsp free(id);
8091 6458efa5 2020-11-24 stsp free(id_str);
8092 6458efa5 2020-11-24 stsp return err;
8093 6458efa5 2020-11-24 stsp }
8094 6458efa5 2020-11-24 stsp free(id);
8095 6458efa5 2020-11-24 stsp free(id_str);
8096 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8097 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
8098 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
8099 6458efa5 2020-11-24 stsp
8100 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8101 ccda2f4d 2022-06-16 stsp 0, 0);
8102 6458efa5 2020-11-24 stsp if (err) {
8103 6458efa5 2020-11-24 stsp free(line);
8104 6458efa5 2020-11-24 stsp return err;
8105 6458efa5 2020-11-24 stsp }
8106 6458efa5 2020-11-24 stsp if (n == s->selected) {
8107 6458efa5 2020-11-24 stsp if (view->focussed)
8108 6458efa5 2020-11-24 stsp wstandout(view->window);
8109 6458efa5 2020-11-24 stsp s->selected_entry = re;
8110 6458efa5 2020-11-24 stsp }
8111 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8112 6458efa5 2020-11-24 stsp if (tc)
8113 6458efa5 2020-11-24 stsp wattr_on(view->window,
8114 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8115 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8116 6458efa5 2020-11-24 stsp if (tc)
8117 6458efa5 2020-11-24 stsp wattr_off(view->window,
8118 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8119 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8120 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8121 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8122 6458efa5 2020-11-24 stsp wstandend(view->window);
8123 6458efa5 2020-11-24 stsp free(line);
8124 6458efa5 2020-11-24 stsp free(wline);
8125 6458efa5 2020-11-24 stsp wline = NULL;
8126 6458efa5 2020-11-24 stsp n++;
8127 6458efa5 2020-11-24 stsp s->ndisplayed++;
8128 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8129 6458efa5 2020-11-24 stsp
8130 6458efa5 2020-11-24 stsp limit--;
8131 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8132 6458efa5 2020-11-24 stsp }
8133 6458efa5 2020-11-24 stsp
8134 9b058f45 2022-06-30 mark view_border(view);
8135 6458efa5 2020-11-24 stsp return err;
8136 6458efa5 2020-11-24 stsp }
8137 6458efa5 2020-11-24 stsp
8138 6458efa5 2020-11-24 stsp static const struct got_error *
8139 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8140 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8141 c42c9805 2020-11-24 stsp {
8142 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8143 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8144 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8145 c42c9805 2020-11-24 stsp
8146 c42c9805 2020-11-24 stsp *new_view = NULL;
8147 c42c9805 2020-11-24 stsp
8148 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8149 c42c9805 2020-11-24 stsp if (err) {
8150 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8151 c42c9805 2020-11-24 stsp return err;
8152 c42c9805 2020-11-24 stsp else
8153 c42c9805 2020-11-24 stsp return NULL;
8154 c42c9805 2020-11-24 stsp }
8155 c42c9805 2020-11-24 stsp
8156 c42c9805 2020-11-24 stsp
8157 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8158 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8159 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8160 c42c9805 2020-11-24 stsp goto done;
8161 c42c9805 2020-11-24 stsp }
8162 c42c9805 2020-11-24 stsp
8163 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8164 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8165 c42c9805 2020-11-24 stsp if (err)
8166 c42c9805 2020-11-24 stsp goto done;
8167 c42c9805 2020-11-24 stsp
8168 c42c9805 2020-11-24 stsp *new_view = tree_view;
8169 c42c9805 2020-11-24 stsp done:
8170 c42c9805 2020-11-24 stsp free(commit_id);
8171 c42c9805 2020-11-24 stsp return err;
8172 94b80cfa 2022-08-01 mark }
8173 94b80cfa 2022-08-01 mark
8174 94b80cfa 2022-08-01 mark static const struct got_error *
8175 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
8176 94b80cfa 2022-08-01 mark {
8177 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
8178 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
8179 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
8180 94b80cfa 2022-08-01 mark
8181 94b80cfa 2022-08-01 mark g = view->gline;
8182 94b80cfa 2022-08-01 mark view->gline = 0;
8183 94b80cfa 2022-08-01 mark
8184 94b80cfa 2022-08-01 mark if (g == 0)
8185 94b80cfa 2022-08-01 mark g = 1;
8186 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
8187 94b80cfa 2022-08-01 mark g = s->nrefs;
8188 94b80cfa 2022-08-01 mark
8189 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
8190 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
8191 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
8192 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8193 94b80cfa 2022-08-01 mark return NULL;
8194 94b80cfa 2022-08-01 mark }
8195 94b80cfa 2022-08-01 mark
8196 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
8197 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
8198 94b80cfa 2022-08-01 mark if (err)
8199 94b80cfa 2022-08-01 mark return err;
8200 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8201 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
8202 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
8203 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
8204 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
8205 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
8206 94b80cfa 2022-08-01 mark
8207 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
8208 94b80cfa 2022-08-01 mark s->selected = g - 1;
8209 94b80cfa 2022-08-01 mark
8210 94b80cfa 2022-08-01 mark return NULL;
8211 94b80cfa 2022-08-01 mark
8212 c42c9805 2020-11-24 stsp }
8213 94b80cfa 2022-08-01 mark
8214 c42c9805 2020-11-24 stsp static const struct got_error *
8215 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8216 6458efa5 2020-11-24 stsp {
8217 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8218 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8219 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8220 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
8221 6458efa5 2020-11-24 stsp
8222 94b80cfa 2022-08-01 mark if (view->gline)
8223 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
8224 94b80cfa 2022-08-01 mark
8225 6458efa5 2020-11-24 stsp switch (ch) {
8226 6458efa5 2020-11-24 stsp case 'i':
8227 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8228 640cd7ff 2022-06-22 mark view->count = 0;
8229 7f66531d 2021-11-16 stsp break;
8230 b4996bee 2022-06-16 stsp case 'm':
8231 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
8232 640cd7ff 2022-06-22 mark view->count = 0;
8233 b4996bee 2022-06-16 stsp break;
8234 07a065fe 2021-11-20 stsp case 'o':
8235 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
8236 640cd7ff 2022-06-22 mark view->count = 0;
8237 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8238 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
8239 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
8240 50617b77 2021-11-20 stsp if (err)
8241 50617b77 2021-11-20 stsp break;
8242 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
8243 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
8244 50617b77 2021-11-20 stsp &tog_refs, s->repo);
8245 7f66531d 2021-11-16 stsp if (err)
8246 7f66531d 2021-11-16 stsp break;
8247 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
8248 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
8249 6458efa5 2020-11-24 stsp break;
8250 6458efa5 2020-11-24 stsp case KEY_ENTER:
8251 6458efa5 2020-11-24 stsp case '\r':
8252 640cd7ff 2022-06-22 mark view->count = 0;
8253 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8254 9b058f45 2022-06-30 mark break;
8255 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
8256 6458efa5 2020-11-24 stsp break;
8257 5e98fb33 2022-07-22 mark case 'T':
8258 640cd7ff 2022-06-22 mark view->count = 0;
8259 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8260 c42c9805 2020-11-24 stsp break;
8261 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
8262 c42c9805 2020-11-24 stsp break;
8263 e4526bf5 2021-09-03 naddy case 'g':
8264 0b3f028d 2023-01-09 mark case '=':
8265 e4526bf5 2021-09-03 naddy case KEY_HOME:
8266 e4526bf5 2021-09-03 naddy s->selected = 0;
8267 640cd7ff 2022-06-22 mark view->count = 0;
8268 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8269 e4526bf5 2021-09-03 naddy break;
8270 e4526bf5 2021-09-03 naddy case 'G':
8271 0b3f028d 2023-01-09 mark case '*':
8272 9b058f45 2022-06-30 mark case KEY_END: {
8273 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
8274 9b058f45 2022-06-30 mark
8275 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
8276 9b058f45 2022-06-30 mark --eos; /* border */
8277 e4526bf5 2021-09-03 naddy s->selected = 0;
8278 640cd7ff 2022-06-22 mark view->count = 0;
8279 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8280 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
8281 e4526bf5 2021-09-03 naddy if (re == NULL)
8282 e4526bf5 2021-09-03 naddy break;
8283 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8284 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8285 e4526bf5 2021-09-03 naddy }
8286 e4526bf5 2021-09-03 naddy if (n > 0)
8287 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8288 e4526bf5 2021-09-03 naddy break;
8289 9b058f45 2022-06-30 mark }
8290 6458efa5 2020-11-24 stsp case 'k':
8291 6458efa5 2020-11-24 stsp case KEY_UP:
8292 02ffd0d5 2021-10-17 stsp case CTRL('p'):
8293 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8294 6458efa5 2020-11-24 stsp s->selected--;
8295 6458efa5 2020-11-24 stsp break;
8296 34ba6917 2020-11-30 stsp }
8297 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8298 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8299 640cd7ff 2022-06-22 mark view->count = 0;
8300 6458efa5 2020-11-24 stsp break;
8301 83cc4199 2022-06-13 stsp case CTRL('u'):
8302 33c3719a 2022-06-15 stsp case 'u':
8303 83cc4199 2022-06-13 stsp nscroll /= 2;
8304 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8305 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8306 6458efa5 2020-11-24 stsp case CTRL('b'):
8307 61417565 2022-06-20 mark case 'b':
8308 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8309 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
8310 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
8311 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
8312 640cd7ff 2022-06-22 mark view->count = 0;
8313 6458efa5 2020-11-24 stsp break;
8314 6458efa5 2020-11-24 stsp case 'j':
8315 6458efa5 2020-11-24 stsp case KEY_DOWN:
8316 02ffd0d5 2021-10-17 stsp case CTRL('n'):
8317 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8318 6458efa5 2020-11-24 stsp s->selected++;
8319 6458efa5 2020-11-24 stsp break;
8320 6458efa5 2020-11-24 stsp }
8321 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8322 6458efa5 2020-11-24 stsp /* can't scroll any further */
8323 640cd7ff 2022-06-22 mark view->count = 0;
8324 6458efa5 2020-11-24 stsp break;
8325 640cd7ff 2022-06-22 mark }
8326 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
8327 6458efa5 2020-11-24 stsp break;
8328 83cc4199 2022-06-13 stsp case CTRL('d'):
8329 33c3719a 2022-06-15 stsp case 'd':
8330 83cc4199 2022-06-13 stsp nscroll /= 2;
8331 83cc4199 2022-06-13 stsp /* FALL THROUGH */
8332 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8333 6458efa5 2020-11-24 stsp case CTRL('f'):
8334 61417565 2022-06-20 mark case 'f':
8335 48bb96f0 2022-06-20 naddy case ' ':
8336 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8337 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8338 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8339 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
8340 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
8341 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
8342 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
8343 640cd7ff 2022-06-22 mark view->count = 0;
8344 6458efa5 2020-11-24 stsp break;
8345 6458efa5 2020-11-24 stsp }
8346 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
8347 6458efa5 2020-11-24 stsp break;
8348 6458efa5 2020-11-24 stsp case CTRL('l'):
8349 640cd7ff 2022-06-22 mark view->count = 0;
8350 8924d611 2020-12-26 stsp tog_free_refs();
8351 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
8352 8924d611 2020-12-26 stsp if (err)
8353 8924d611 2020-12-26 stsp break;
8354 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8355 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8356 6458efa5 2020-11-24 stsp break;
8357 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8358 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8359 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8360 6458efa5 2020-11-24 stsp break;
8361 6458efa5 2020-11-24 stsp default:
8362 640cd7ff 2022-06-22 mark view->count = 0;
8363 6458efa5 2020-11-24 stsp break;
8364 6458efa5 2020-11-24 stsp }
8365 6458efa5 2020-11-24 stsp
8366 6458efa5 2020-11-24 stsp return err;
8367 6458efa5 2020-11-24 stsp }
8368 6458efa5 2020-11-24 stsp
8369 6458efa5 2020-11-24 stsp __dead static void
8370 6458efa5 2020-11-24 stsp usage_ref(void)
8371 6458efa5 2020-11-24 stsp {
8372 6458efa5 2020-11-24 stsp endwin();
8373 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8374 6458efa5 2020-11-24 stsp getprogname());
8375 6458efa5 2020-11-24 stsp exit(1);
8376 6458efa5 2020-11-24 stsp }
8377 6458efa5 2020-11-24 stsp
8378 6458efa5 2020-11-24 stsp static const struct got_error *
8379 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8380 6458efa5 2020-11-24 stsp {
8381 6458efa5 2020-11-24 stsp const struct got_error *error;
8382 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8383 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8384 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8385 6458efa5 2020-11-24 stsp int ch;
8386 6458efa5 2020-11-24 stsp struct tog_view *view;
8387 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8388 6458efa5 2020-11-24 stsp
8389 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8390 6458efa5 2020-11-24 stsp switch (ch) {
8391 6458efa5 2020-11-24 stsp case 'r':
8392 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8393 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8394 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8395 6458efa5 2020-11-24 stsp optarg);
8396 6458efa5 2020-11-24 stsp break;
8397 6458efa5 2020-11-24 stsp default:
8398 e99e2d15 2020-11-24 naddy usage_ref();
8399 6458efa5 2020-11-24 stsp /* NOTREACHED */
8400 6458efa5 2020-11-24 stsp }
8401 6458efa5 2020-11-24 stsp }
8402 6458efa5 2020-11-24 stsp
8403 6458efa5 2020-11-24 stsp argc -= optind;
8404 6458efa5 2020-11-24 stsp argv += optind;
8405 6458efa5 2020-11-24 stsp
8406 6458efa5 2020-11-24 stsp if (argc > 1)
8407 e99e2d15 2020-11-24 naddy usage_ref();
8408 0ae84acc 2022-06-15 tracey
8409 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8410 0ae84acc 2022-06-15 tracey if (error != NULL)
8411 0ae84acc 2022-06-15 tracey goto done;
8412 6458efa5 2020-11-24 stsp
8413 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8414 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8415 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8416 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8417 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8418 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8419 c156c7a4 2020-12-18 stsp goto done;
8420 6458efa5 2020-11-24 stsp if (worktree)
8421 6458efa5 2020-11-24 stsp repo_path =
8422 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8423 6458efa5 2020-11-24 stsp else
8424 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8425 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8426 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8427 c156c7a4 2020-12-18 stsp goto done;
8428 c156c7a4 2020-12-18 stsp }
8429 6458efa5 2020-11-24 stsp }
8430 6458efa5 2020-11-24 stsp
8431 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8432 6458efa5 2020-11-24 stsp if (error != NULL)
8433 6458efa5 2020-11-24 stsp goto done;
8434 6458efa5 2020-11-24 stsp
8435 6458efa5 2020-11-24 stsp init_curses();
8436 6458efa5 2020-11-24 stsp
8437 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8438 51a10b52 2020-12-26 stsp if (error)
8439 51a10b52 2020-12-26 stsp goto done;
8440 51a10b52 2020-12-26 stsp
8441 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8442 6458efa5 2020-11-24 stsp if (error)
8443 6458efa5 2020-11-24 stsp goto done;
8444 6458efa5 2020-11-24 stsp
8445 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8446 6458efa5 2020-11-24 stsp if (view == NULL) {
8447 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8448 6458efa5 2020-11-24 stsp goto done;
8449 6458efa5 2020-11-24 stsp }
8450 6458efa5 2020-11-24 stsp
8451 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8452 6458efa5 2020-11-24 stsp if (error)
8453 6458efa5 2020-11-24 stsp goto done;
8454 6458efa5 2020-11-24 stsp
8455 6458efa5 2020-11-24 stsp if (worktree) {
8456 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8457 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8458 6458efa5 2020-11-24 stsp worktree = NULL;
8459 6458efa5 2020-11-24 stsp }
8460 6458efa5 2020-11-24 stsp error = view_loop(view);
8461 6458efa5 2020-11-24 stsp done:
8462 6458efa5 2020-11-24 stsp free(repo_path);
8463 6458efa5 2020-11-24 stsp free(cwd);
8464 1d0f4054 2021-06-17 stsp if (repo) {
8465 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8466 1d0f4054 2021-06-17 stsp if (close_err)
8467 1d0f4054 2021-06-17 stsp error = close_err;
8468 1d0f4054 2021-06-17 stsp }
8469 0ae84acc 2022-06-15 tracey if (pack_fds) {
8470 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8471 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8472 0ae84acc 2022-06-15 tracey if (error == NULL)
8473 0ae84acc 2022-06-15 tracey error = pack_err;
8474 0ae84acc 2022-06-15 tracey }
8475 51a10b52 2020-12-26 stsp tog_free_refs();
8476 6458efa5 2020-11-24 stsp return error;
8477 6458efa5 2020-11-24 stsp }
8478 6458efa5 2020-11-24 stsp
8479 ec2a9698 2022-09-15 mark static const struct got_error*
8480 ec2a9698 2022-09-15 mark win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8481 ec2a9698 2022-09-15 mark const char *str)
8482 ec2a9698 2022-09-15 mark {
8483 ec2a9698 2022-09-15 mark size_t len;
8484 ec2a9698 2022-09-15 mark
8485 ec2a9698 2022-09-15 mark if (win == NULL)
8486 ec2a9698 2022-09-15 mark win = stdscr;
8487 ec2a9698 2022-09-15 mark
8488 ec2a9698 2022-09-15 mark len = strlen(str);
8489 ec2a9698 2022-09-15 mark x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8490 ec2a9698 2022-09-15 mark
8491 ec2a9698 2022-09-15 mark if (focus)
8492 ec2a9698 2022-09-15 mark wstandout(win);
8493 ec2a9698 2022-09-15 mark if (mvwprintw(win, y, x, "%s", str) == ERR)
8494 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8495 ec2a9698 2022-09-15 mark if (focus)
8496 ec2a9698 2022-09-15 mark wstandend(win);
8497 ec2a9698 2022-09-15 mark
8498 ec2a9698 2022-09-15 mark return NULL;
8499 ec2a9698 2022-09-15 mark }
8500 ec2a9698 2022-09-15 mark
8501 136e2bd4 2022-07-23 mark static const struct got_error *
8502 ec2a9698 2022-09-15 mark add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8503 ec2a9698 2022-09-15 mark {
8504 ec2a9698 2022-09-15 mark off_t *p;
8505 ec2a9698 2022-09-15 mark
8506 ec2a9698 2022-09-15 mark p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8507 ec2a9698 2022-09-15 mark if (p == NULL) {
8508 ec2a9698 2022-09-15 mark free(*line_offsets);
8509 ec2a9698 2022-09-15 mark *line_offsets = NULL;
8510 ec2a9698 2022-09-15 mark return got_error_from_errno("reallocarray");
8511 ec2a9698 2022-09-15 mark }
8512 ec2a9698 2022-09-15 mark
8513 ec2a9698 2022-09-15 mark *line_offsets = p;
8514 ec2a9698 2022-09-15 mark (*line_offsets)[*nlines] = off;
8515 ec2a9698 2022-09-15 mark ++(*nlines);
8516 ec2a9698 2022-09-15 mark return NULL;
8517 ec2a9698 2022-09-15 mark }
8518 ec2a9698 2022-09-15 mark
8519 ec2a9698 2022-09-15 mark static const struct got_error *
8520 ec2a9698 2022-09-15 mark max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8521 ec2a9698 2022-09-15 mark {
8522 ec2a9698 2022-09-15 mark *ret = 0;
8523 ec2a9698 2022-09-15 mark
8524 ec2a9698 2022-09-15 mark for (;n > 0; --n, ++km) {
8525 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8526 ec2a9698 2022-09-15 mark size_t len = 1;
8527 ec2a9698 2022-09-15 mark
8528 ec2a9698 2022-09-15 mark if (km->keys == NULL)
8529 ec2a9698 2022-09-15 mark continue;
8530 ec2a9698 2022-09-15 mark
8531 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8532 ec2a9698 2022-09-15 mark if (t0 == NULL)
8533 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8534 ec2a9698 2022-09-15 mark
8535 ec2a9698 2022-09-15 mark len += strlen(t);
8536 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL)
8537 ec2a9698 2022-09-15 mark len += strlen(k) > 1 ? 2 : 0;
8538 ec2a9698 2022-09-15 mark free(t0);
8539 ec2a9698 2022-09-15 mark *ret = MAX(*ret, len);
8540 ec2a9698 2022-09-15 mark }
8541 ec2a9698 2022-09-15 mark
8542 ec2a9698 2022-09-15 mark return NULL;
8543 ec2a9698 2022-09-15 mark }
8544 ec2a9698 2022-09-15 mark
8545 ec2a9698 2022-09-15 mark /*
8546 ec2a9698 2022-09-15 mark * Write keymap section headers, keys, and key info in km to f.
8547 ec2a9698 2022-09-15 mark * Save line offset to *off. If terminal has UTF8 encoding enabled,
8548 ec2a9698 2022-09-15 mark * wrap control and symbolic keys in guillemets, else use <>.
8549 ec2a9698 2022-09-15 mark */
8550 ec2a9698 2022-09-15 mark static const struct got_error *
8551 ec2a9698 2022-09-15 mark format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8552 ec2a9698 2022-09-15 mark {
8553 ec2a9698 2022-09-15 mark int n, len = width;
8554 ec2a9698 2022-09-15 mark
8555 ec2a9698 2022-09-15 mark if (km->keys) {
8556 1681ba87 2022-09-18 mark static const char *u8_glyph[] = {
8557 1681ba87 2022-09-18 mark "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8558 1681ba87 2022-09-18 mark "\xe2\x80\xba" /* U+203A (utf8 >) */
8559 1681ba87 2022-09-18 mark };
8560 ec2a9698 2022-09-15 mark char *t0, *t, *k;
8561 ec2a9698 2022-09-15 mark int cs, s, first = 1;
8562 ec2a9698 2022-09-15 mark
8563 ec2a9698 2022-09-15 mark cs = got_locale_is_utf8();
8564 ec2a9698 2022-09-15 mark
8565 ec2a9698 2022-09-15 mark t = t0 = strdup(km->keys);
8566 ec2a9698 2022-09-15 mark if (t0 == NULL)
8567 ec2a9698 2022-09-15 mark return got_error_from_errno("strdup");
8568 ec2a9698 2022-09-15 mark
8569 ec2a9698 2022-09-15 mark len = strlen(km->keys);
8570 ec2a9698 2022-09-15 mark while ((k = strsep(&t, " ")) != NULL) {
8571 ec2a9698 2022-09-15 mark s = strlen(k) > 1; /* control or symbolic key */
8572 ec2a9698 2022-09-15 mark n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8573 1681ba87 2022-09-18 mark cs && s ? u8_glyph[0] : s ? "<" : "", k,
8574 1681ba87 2022-09-18 mark cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8575 ec2a9698 2022-09-15 mark if (n < 0) {
8576 ec2a9698 2022-09-15 mark free(t0);
8577 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8578 ec2a9698 2022-09-15 mark }
8579 ec2a9698 2022-09-15 mark first = 0;
8580 ec2a9698 2022-09-15 mark len += s ? 2 : 0;
8581 ec2a9698 2022-09-15 mark *off += n;
8582 ec2a9698 2022-09-15 mark }
8583 ec2a9698 2022-09-15 mark free(t0);
8584 ec2a9698 2022-09-15 mark }
8585 ec2a9698 2022-09-15 mark n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8586 ec2a9698 2022-09-15 mark if (n < 0)
8587 ec2a9698 2022-09-15 mark return got_error_from_errno("fprintf");
8588 ec2a9698 2022-09-15 mark *off += n;
8589 ec2a9698 2022-09-15 mark
8590 ec2a9698 2022-09-15 mark return NULL;
8591 ec2a9698 2022-09-15 mark }
8592 ec2a9698 2022-09-15 mark
8593 ec2a9698 2022-09-15 mark static const struct got_error *
8594 ec2a9698 2022-09-15 mark format_help(struct tog_help_view_state *s)
8595 ec2a9698 2022-09-15 mark {
8596 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8597 ec2a9698 2022-09-15 mark off_t off = 0;
8598 ec2a9698 2022-09-15 mark int i, max, n, show = s->all;
8599 ec2a9698 2022-09-15 mark static const struct tog_key_map km[] = {
8600 ec2a9698 2022-09-15 mark #define KEYMAP_(info, type) { NULL, (info), type }
8601 ec2a9698 2022-09-15 mark #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8602 ec2a9698 2022-09-15 mark GENERATE_HELP
8603 ec2a9698 2022-09-15 mark #undef KEYMAP_
8604 ec2a9698 2022-09-15 mark #undef KEY_
8605 ec2a9698 2022-09-15 mark };
8606 ec2a9698 2022-09-15 mark
8607 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8608 ec2a9698 2022-09-15 mark if (err)
8609 ec2a9698 2022-09-15 mark return err;
8610 ec2a9698 2022-09-15 mark
8611 ec2a9698 2022-09-15 mark n = nitems(km);
8612 ec2a9698 2022-09-15 mark err = max_key_str(&max, km, n);
8613 ec2a9698 2022-09-15 mark if (err)
8614 ec2a9698 2022-09-15 mark return err;
8615 ec2a9698 2022-09-15 mark
8616 ec2a9698 2022-09-15 mark for (i = 0; i < n; ++i) {
8617 ec2a9698 2022-09-15 mark if (km[i].keys == NULL) {
8618 ec2a9698 2022-09-15 mark show = s->all;
8619 ec2a9698 2022-09-15 mark if (km[i].type == TOG_KEYMAP_GLOBAL ||
8620 ec2a9698 2022-09-15 mark km[i].type == s->type || s->all)
8621 ec2a9698 2022-09-15 mark show = 1;
8622 ec2a9698 2022-09-15 mark }
8623 ec2a9698 2022-09-15 mark if (show) {
8624 ec2a9698 2022-09-15 mark err = format_help_line(&off, s->f, &km[i], max);
8625 ec2a9698 2022-09-15 mark if (err)
8626 ec2a9698 2022-09-15 mark return err;
8627 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8628 ec2a9698 2022-09-15 mark if (err)
8629 ec2a9698 2022-09-15 mark return err;
8630 ec2a9698 2022-09-15 mark }
8631 ec2a9698 2022-09-15 mark }
8632 ec2a9698 2022-09-15 mark fputc('\n', s->f);
8633 ec2a9698 2022-09-15 mark ++off;
8634 ec2a9698 2022-09-15 mark err = add_line_offset(&s->line_offsets, &s->nlines, off);
8635 ec2a9698 2022-09-15 mark return err;
8636 ec2a9698 2022-09-15 mark }
8637 ec2a9698 2022-09-15 mark
8638 ec2a9698 2022-09-15 mark static const struct got_error *
8639 ec2a9698 2022-09-15 mark create_help(struct tog_help_view_state *s)
8640 ec2a9698 2022-09-15 mark {
8641 ec2a9698 2022-09-15 mark FILE *f;
8642 ec2a9698 2022-09-15 mark const struct got_error *err;
8643 ec2a9698 2022-09-15 mark
8644 ec2a9698 2022-09-15 mark free(s->line_offsets);
8645 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8646 ec2a9698 2022-09-15 mark s->nlines = 0;
8647 ec2a9698 2022-09-15 mark
8648 ec2a9698 2022-09-15 mark f = got_opentemp();
8649 ec2a9698 2022-09-15 mark if (f == NULL)
8650 ec2a9698 2022-09-15 mark return got_error_from_errno("got_opentemp");
8651 ec2a9698 2022-09-15 mark s->f = f;
8652 ec2a9698 2022-09-15 mark
8653 ec2a9698 2022-09-15 mark err = format_help(s);
8654 ec2a9698 2022-09-15 mark if (err)
8655 ec2a9698 2022-09-15 mark return err;
8656 ec2a9698 2022-09-15 mark
8657 ec2a9698 2022-09-15 mark if (s->f && fflush(s->f) != 0)
8658 ec2a9698 2022-09-15 mark return got_error_from_errno("fflush");
8659 ec2a9698 2022-09-15 mark
8660 ec2a9698 2022-09-15 mark return NULL;
8661 ec2a9698 2022-09-15 mark }
8662 ec2a9698 2022-09-15 mark
8663 ec2a9698 2022-09-15 mark static const struct got_error *
8664 ec2a9698 2022-09-15 mark search_start_help_view(struct tog_view *view)
8665 ec2a9698 2022-09-15 mark {
8666 ec2a9698 2022-09-15 mark view->state.help.matched_line = 0;
8667 ec2a9698 2022-09-15 mark return NULL;
8668 ec2a9698 2022-09-15 mark }
8669 ec2a9698 2022-09-15 mark
8670 eaf2c13e 2022-09-17 mark static void
8671 eaf2c13e 2022-09-17 mark search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8672 eaf2c13e 2022-09-17 mark size_t *nlines, int **first, int **last, int **match, int **selected)
8673 eaf2c13e 2022-09-17 mark {
8674 eaf2c13e 2022-09-17 mark struct tog_help_view_state *s = &view->state.help;
8675 eaf2c13e 2022-09-17 mark
8676 eaf2c13e 2022-09-17 mark *f = s->f;
8677 eaf2c13e 2022-09-17 mark *nlines = s->nlines;
8678 eaf2c13e 2022-09-17 mark *line_offsets = s->line_offsets;
8679 eaf2c13e 2022-09-17 mark *match = &s->matched_line;
8680 eaf2c13e 2022-09-17 mark *first = &s->first_displayed_line;
8681 eaf2c13e 2022-09-17 mark *last = &s->last_displayed_line;
8682 eaf2c13e 2022-09-17 mark *selected = &s->selected_line;
8683 eaf2c13e 2022-09-17 mark }
8684 eaf2c13e 2022-09-17 mark
8685 ec2a9698 2022-09-15 mark static const struct got_error *
8686 ec2a9698 2022-09-15 mark show_help_view(struct tog_view *view)
8687 ec2a9698 2022-09-15 mark {
8688 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8689 ec2a9698 2022-09-15 mark const struct got_error *err;
8690 ec2a9698 2022-09-15 mark regmatch_t *regmatch = &view->regmatch;
8691 ec2a9698 2022-09-15 mark wchar_t *wline;
8692 ec2a9698 2022-09-15 mark char *line;
8693 ec2a9698 2022-09-15 mark ssize_t linelen;
8694 ec2a9698 2022-09-15 mark size_t linesz = 0;
8695 ec2a9698 2022-09-15 mark int width, nprinted = 0, rc = 0;
8696 ec2a9698 2022-09-15 mark int eos = view->nlines;
8697 ec2a9698 2022-09-15 mark
8698 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8699 ec2a9698 2022-09-15 mark --eos; /* account for border */
8700 ec2a9698 2022-09-15 mark
8701 ec2a9698 2022-09-15 mark s->lineno = 0;
8702 ec2a9698 2022-09-15 mark rewind(s->f);
8703 ec2a9698 2022-09-15 mark werase(view->window);
8704 ec2a9698 2022-09-15 mark
8705 ec2a9698 2022-09-15 mark if (view->gline > s->nlines - 1)
8706 ec2a9698 2022-09-15 mark view->gline = s->nlines - 1;
8707 ec2a9698 2022-09-15 mark
8708 ec2a9698 2022-09-15 mark err = win_draw_center(view->window, 0, 0, view->ncols,
8709 16a2d4f0 2022-09-19 stsp view_needs_focus_indication(view),
8710 12c07a25 2022-09-19 stsp "tog help (press q to return to tog)");
8711 ec2a9698 2022-09-15 mark if (err)
8712 ec2a9698 2022-09-15 mark return err;
8713 ec2a9698 2022-09-15 mark if (eos <= 1)
8714 ec2a9698 2022-09-15 mark return NULL;
8715 ec2a9698 2022-09-15 mark waddstr(view->window, "\n\n");
8716 ec2a9698 2022-09-15 mark eos -= 2;
8717 ec2a9698 2022-09-15 mark
8718 ec2a9698 2022-09-15 mark s->eof = 0;
8719 ec2a9698 2022-09-15 mark view->maxx = 0;
8720 ec2a9698 2022-09-15 mark line = NULL;
8721 ec2a9698 2022-09-15 mark while (eos > 0 && nprinted < eos) {
8722 ec2a9698 2022-09-15 mark attr_t attr = 0;
8723 ec2a9698 2022-09-15 mark
8724 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8725 ec2a9698 2022-09-15 mark if (linelen == -1) {
8726 ec2a9698 2022-09-15 mark if (!feof(s->f)) {
8727 ec2a9698 2022-09-15 mark free(line);
8728 ec2a9698 2022-09-15 mark return got_ferror(s->f, GOT_ERR_IO);
8729 ec2a9698 2022-09-15 mark }
8730 ec2a9698 2022-09-15 mark s->eof = 1;
8731 ec2a9698 2022-09-15 mark break;
8732 ec2a9698 2022-09-15 mark }
8733 ec2a9698 2022-09-15 mark if (++s->lineno < s->first_displayed_line)
8734 ec2a9698 2022-09-15 mark continue;
8735 ec2a9698 2022-09-15 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8736 ec2a9698 2022-09-15 mark continue;
8737 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline)
8738 ec2a9698 2022-09-15 mark attr = A_STANDOUT;
8739 ec2a9698 2022-09-15 mark
8740 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8741 ec2a9698 2022-09-15 mark view->x ? 1 : 0);
8742 ec2a9698 2022-09-15 mark if (err) {
8743 ec2a9698 2022-09-15 mark free(line);
8744 ec2a9698 2022-09-15 mark return err;
8745 ec2a9698 2022-09-15 mark }
8746 ec2a9698 2022-09-15 mark view->maxx = MAX(view->maxx, width);
8747 ec2a9698 2022-09-15 mark free(wline);
8748 ec2a9698 2022-09-15 mark wline = NULL;
8749 ec2a9698 2022-09-15 mark
8750 ec2a9698 2022-09-15 mark if (attr)
8751 ec2a9698 2022-09-15 mark wattron(view->window, attr);
8752 ec2a9698 2022-09-15 mark if (s->first_displayed_line + nprinted == s->matched_line &&
8753 ec2a9698 2022-09-15 mark regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8754 ec2a9698 2022-09-15 mark err = add_matched_line(&width, line, view->ncols - 1, 0,
8755 ec2a9698 2022-09-15 mark view->window, view->x, regmatch);
8756 ec2a9698 2022-09-15 mark if (err) {
8757 ec2a9698 2022-09-15 mark free(line);
8758 ec2a9698 2022-09-15 mark return err;
8759 ec2a9698 2022-09-15 mark }
8760 ec2a9698 2022-09-15 mark } else {
8761 ec2a9698 2022-09-15 mark int skip;
8762 ec2a9698 2022-09-15 mark
8763 ec2a9698 2022-09-15 mark err = format_line(&wline, &width, &skip, line,
8764 ec2a9698 2022-09-15 mark view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8765 ec2a9698 2022-09-15 mark if (err) {
8766 ec2a9698 2022-09-15 mark free(line);
8767 ec2a9698 2022-09-15 mark return err;
8768 ec2a9698 2022-09-15 mark }
8769 ec2a9698 2022-09-15 mark rc = waddwstr(view->window, &wline[skip]);
8770 ec2a9698 2022-09-15 mark free(wline);
8771 ec2a9698 2022-09-15 mark wline = NULL;
8772 ec2a9698 2022-09-15 mark if (rc == ERR)
8773 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "waddwstr");
8774 ec2a9698 2022-09-15 mark }
8775 ec2a9698 2022-09-15 mark if (s->lineno == view->hiline) {
8776 ec2a9698 2022-09-15 mark while (width++ < view->ncols)
8777 ec2a9698 2022-09-15 mark waddch(view->window, ' ');
8778 ec2a9698 2022-09-15 mark } else {
8779 ec2a9698 2022-09-15 mark if (width <= view->ncols)
8780 ec2a9698 2022-09-15 mark waddch(view->window, '\n');
8781 ec2a9698 2022-09-15 mark }
8782 ec2a9698 2022-09-15 mark if (attr)
8783 ec2a9698 2022-09-15 mark wattroff(view->window, attr);
8784 ec2a9698 2022-09-15 mark if (++nprinted == 1)
8785 ec2a9698 2022-09-15 mark s->first_displayed_line = s->lineno;
8786 ec2a9698 2022-09-15 mark }
8787 ec2a9698 2022-09-15 mark free(line);
8788 ec2a9698 2022-09-15 mark if (nprinted > 0)
8789 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8790 ec2a9698 2022-09-15 mark else
8791 ec2a9698 2022-09-15 mark s->last_displayed_line = s->first_displayed_line;
8792 ec2a9698 2022-09-15 mark
8793 ec2a9698 2022-09-15 mark view_border(view);
8794 ec2a9698 2022-09-15 mark
8795 ec2a9698 2022-09-15 mark if (s->eof) {
8796 ec2a9698 2022-09-15 mark rc = waddnstr(view->window,
8797 ec2a9698 2022-09-15 mark "See the tog(1) manual page for full documentation",
8798 ec2a9698 2022-09-15 mark view->ncols - 1);
8799 ec2a9698 2022-09-15 mark if (rc == ERR)
8800 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8801 ec2a9698 2022-09-15 mark } else {
8802 ec2a9698 2022-09-15 mark wmove(view->window, view->nlines - 1, 0);
8803 ec2a9698 2022-09-15 mark wclrtoeol(view->window);
8804 ec2a9698 2022-09-15 mark wstandout(view->window);
8805 ec2a9698 2022-09-15 mark rc = waddnstr(view->window, "scroll down for more...",
8806 ec2a9698 2022-09-15 mark view->ncols - 1);
8807 ec2a9698 2022-09-15 mark if (rc == ERR)
8808 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8809 ec2a9698 2022-09-15 mark if (getcurx(view->window) < view->ncols - 6) {
8810 ec2a9698 2022-09-15 mark rc = wprintw(view->window, "[%.0f%%]",
8811 ec2a9698 2022-09-15 mark 100.00 * s->last_displayed_line / s->nlines);
8812 ec2a9698 2022-09-15 mark if (rc == ERR)
8813 ec2a9698 2022-09-15 mark return got_error_msg(GOT_ERR_IO, "wprintw");
8814 ec2a9698 2022-09-15 mark }
8815 ec2a9698 2022-09-15 mark wstandend(view->window);
8816 ec2a9698 2022-09-15 mark }
8817 ec2a9698 2022-09-15 mark
8818 ec2a9698 2022-09-15 mark return NULL;
8819 ec2a9698 2022-09-15 mark }
8820 ec2a9698 2022-09-15 mark
8821 ec2a9698 2022-09-15 mark static const struct got_error *
8822 ec2a9698 2022-09-15 mark input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8823 ec2a9698 2022-09-15 mark {
8824 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8825 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8826 ec2a9698 2022-09-15 mark char *line = NULL;
8827 ec2a9698 2022-09-15 mark ssize_t linelen;
8828 ec2a9698 2022-09-15 mark size_t linesz = 0;
8829 ec2a9698 2022-09-15 mark int eos, nscroll;
8830 ec2a9698 2022-09-15 mark
8831 ec2a9698 2022-09-15 mark eos = nscroll = view->nlines;
8832 ec2a9698 2022-09-15 mark if (view_is_hsplit_top(view))
8833 ec2a9698 2022-09-15 mark --eos; /* border */
8834 ec2a9698 2022-09-15 mark
8835 ec2a9698 2022-09-15 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
8836 ec2a9698 2022-09-15 mark
8837 ec2a9698 2022-09-15 mark switch (ch) {
8838 ec2a9698 2022-09-15 mark case '0':
8839 ec2a9698 2022-09-15 mark view->x = 0;
8840 ec2a9698 2022-09-15 mark break;
8841 ec2a9698 2022-09-15 mark case '$':
8842 ec2a9698 2022-09-15 mark view->x = MAX(view->maxx - view->ncols / 3, 0);
8843 ec2a9698 2022-09-15 mark view->count = 0;
8844 ec2a9698 2022-09-15 mark break;
8845 ec2a9698 2022-09-15 mark case KEY_RIGHT:
8846 ec2a9698 2022-09-15 mark case 'l':
8847 ec2a9698 2022-09-15 mark if (view->x + view->ncols / 3 < view->maxx)
8848 ec2a9698 2022-09-15 mark view->x += 2;
8849 ec2a9698 2022-09-15 mark else
8850 ec2a9698 2022-09-15 mark view->count = 0;
8851 ec2a9698 2022-09-15 mark break;
8852 ec2a9698 2022-09-15 mark case KEY_LEFT:
8853 ec2a9698 2022-09-15 mark case 'h':
8854 ec2a9698 2022-09-15 mark view->x -= MIN(view->x, 2);
8855 ec2a9698 2022-09-15 mark if (view->x <= 0)
8856 ec2a9698 2022-09-15 mark view->count = 0;
8857 ec2a9698 2022-09-15 mark break;
8858 ec2a9698 2022-09-15 mark case 'g':
8859 ec2a9698 2022-09-15 mark case KEY_HOME:
8860 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8861 ec2a9698 2022-09-15 mark view->count = 0;
8862 ec2a9698 2022-09-15 mark break;
8863 ec2a9698 2022-09-15 mark case 'G':
8864 ec2a9698 2022-09-15 mark case KEY_END:
8865 ec2a9698 2022-09-15 mark view->count = 0;
8866 ec2a9698 2022-09-15 mark if (s->eof)
8867 ec2a9698 2022-09-15 mark break;
8868 ec2a9698 2022-09-15 mark s->first_displayed_line = (s->nlines - eos) + 3;
8869 ec2a9698 2022-09-15 mark s->eof = 1;
8870 ec2a9698 2022-09-15 mark break;
8871 ec2a9698 2022-09-15 mark case 'k':
8872 ec2a9698 2022-09-15 mark case KEY_UP:
8873 ec2a9698 2022-09-15 mark if (s->first_displayed_line > 1)
8874 ec2a9698 2022-09-15 mark --s->first_displayed_line;
8875 ec2a9698 2022-09-15 mark else
8876 ec2a9698 2022-09-15 mark view->count = 0;
8877 ec2a9698 2022-09-15 mark break;
8878 ec2a9698 2022-09-15 mark case CTRL('u'):
8879 ec2a9698 2022-09-15 mark case 'u':
8880 ec2a9698 2022-09-15 mark nscroll /= 2;
8881 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8882 ec2a9698 2022-09-15 mark case KEY_PPAGE:
8883 ec2a9698 2022-09-15 mark case CTRL('b'):
8884 ec2a9698 2022-09-15 mark case 'b':
8885 ec2a9698 2022-09-15 mark if (s->first_displayed_line == 1) {
8886 ec2a9698 2022-09-15 mark view->count = 0;
8887 ec2a9698 2022-09-15 mark break;
8888 ec2a9698 2022-09-15 mark }
8889 ec2a9698 2022-09-15 mark while (--nscroll > 0 && s->first_displayed_line > 1)
8890 ec2a9698 2022-09-15 mark s->first_displayed_line--;
8891 ec2a9698 2022-09-15 mark break;
8892 ec2a9698 2022-09-15 mark case 'j':
8893 ec2a9698 2022-09-15 mark case KEY_DOWN:
8894 ec2a9698 2022-09-15 mark case CTRL('n'):
8895 ec2a9698 2022-09-15 mark if (!s->eof)
8896 ec2a9698 2022-09-15 mark ++s->first_displayed_line;
8897 ec2a9698 2022-09-15 mark else
8898 ec2a9698 2022-09-15 mark view->count = 0;
8899 ec2a9698 2022-09-15 mark break;
8900 ec2a9698 2022-09-15 mark case CTRL('d'):
8901 ec2a9698 2022-09-15 mark case 'd':
8902 ec2a9698 2022-09-15 mark nscroll /= 2;
8903 ec2a9698 2022-09-15 mark /* FALL THROUGH */
8904 ec2a9698 2022-09-15 mark case KEY_NPAGE:
8905 ec2a9698 2022-09-15 mark case CTRL('f'):
8906 ec2a9698 2022-09-15 mark case 'f':
8907 ec2a9698 2022-09-15 mark case ' ':
8908 ec2a9698 2022-09-15 mark if (s->eof) {
8909 ec2a9698 2022-09-15 mark view->count = 0;
8910 ec2a9698 2022-09-15 mark break;
8911 ec2a9698 2022-09-15 mark }
8912 ec2a9698 2022-09-15 mark while (!s->eof && --nscroll > 0) {
8913 ec2a9698 2022-09-15 mark linelen = getline(&line, &linesz, s->f);
8914 ec2a9698 2022-09-15 mark s->first_displayed_line++;
8915 ec2a9698 2022-09-15 mark if (linelen == -1) {
8916 ec2a9698 2022-09-15 mark if (feof(s->f))
8917 ec2a9698 2022-09-15 mark s->eof = 1;
8918 ec2a9698 2022-09-15 mark else
8919 ec2a9698 2022-09-15 mark err = got_ferror(s->f, GOT_ERR_IO);
8920 ec2a9698 2022-09-15 mark break;
8921 ec2a9698 2022-09-15 mark }
8922 ec2a9698 2022-09-15 mark }
8923 ec2a9698 2022-09-15 mark free(line);
8924 ec2a9698 2022-09-15 mark break;
8925 ec2a9698 2022-09-15 mark default:
8926 ec2a9698 2022-09-15 mark view->count = 0;
8927 ec2a9698 2022-09-15 mark break;
8928 ec2a9698 2022-09-15 mark }
8929 ec2a9698 2022-09-15 mark
8930 ec2a9698 2022-09-15 mark return err;
8931 ec2a9698 2022-09-15 mark }
8932 ec2a9698 2022-09-15 mark
8933 ec2a9698 2022-09-15 mark static const struct got_error *
8934 ec2a9698 2022-09-15 mark close_help_view(struct tog_view *view)
8935 ec2a9698 2022-09-15 mark {
8936 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8937 ec2a9698 2022-09-15 mark
8938 ec2a9698 2022-09-15 mark free(s->line_offsets);
8939 ec2a9698 2022-09-15 mark s->line_offsets = NULL;
8940 ec2a9698 2022-09-15 mark if (fclose(s->f) == EOF)
8941 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8942 ec2a9698 2022-09-15 mark
8943 ec2a9698 2022-09-15 mark return NULL;
8944 ec2a9698 2022-09-15 mark }
8945 ec2a9698 2022-09-15 mark
8946 ec2a9698 2022-09-15 mark static const struct got_error *
8947 ec2a9698 2022-09-15 mark reset_help_view(struct tog_view *view)
8948 ec2a9698 2022-09-15 mark {
8949 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8950 ec2a9698 2022-09-15 mark
8951 ec2a9698 2022-09-15 mark
8952 ec2a9698 2022-09-15 mark if (s->f && fclose(s->f) == EOF)
8953 ec2a9698 2022-09-15 mark return got_error_from_errno("fclose");
8954 ec2a9698 2022-09-15 mark
8955 ec2a9698 2022-09-15 mark wclear(view->window);
8956 ec2a9698 2022-09-15 mark view->count = 0;
8957 ec2a9698 2022-09-15 mark view->x = 0;
8958 ec2a9698 2022-09-15 mark s->all = !s->all;
8959 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8960 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8961 ec2a9698 2022-09-15 mark s->matched_line = 0;
8962 ec2a9698 2022-09-15 mark
8963 ec2a9698 2022-09-15 mark return create_help(s);
8964 ec2a9698 2022-09-15 mark }
8965 ec2a9698 2022-09-15 mark
8966 ec2a9698 2022-09-15 mark static const struct got_error *
8967 ec2a9698 2022-09-15 mark open_help_view(struct tog_view *view, struct tog_view *parent)
8968 ec2a9698 2022-09-15 mark {
8969 ec2a9698 2022-09-15 mark const struct got_error *err = NULL;
8970 ec2a9698 2022-09-15 mark struct tog_help_view_state *s = &view->state.help;
8971 ec2a9698 2022-09-15 mark
8972 ec2a9698 2022-09-15 mark s->type = (enum tog_keymap_type)parent->type;
8973 ec2a9698 2022-09-15 mark s->first_displayed_line = 1;
8974 ec2a9698 2022-09-15 mark s->last_displayed_line = view->nlines;
8975 ec2a9698 2022-09-15 mark s->selected_line = 1;
8976 ec2a9698 2022-09-15 mark
8977 ec2a9698 2022-09-15 mark view->show = show_help_view;
8978 ec2a9698 2022-09-15 mark view->input = input_help_view;
8979 ec2a9698 2022-09-15 mark view->reset = reset_help_view;
8980 ec2a9698 2022-09-15 mark view->close = close_help_view;
8981 ec2a9698 2022-09-15 mark view->search_start = search_start_help_view;
8982 eaf2c13e 2022-09-17 mark view->search_setup = search_setup_help_view;
8983 ec2a9698 2022-09-15 mark view->search_next = search_next_view_match;
8984 ec2a9698 2022-09-15 mark
8985 ec2a9698 2022-09-15 mark err = create_help(s);
8986 ec2a9698 2022-09-15 mark return err;
8987 ec2a9698 2022-09-15 mark }
8988 ec2a9698 2022-09-15 mark
8989 ec2a9698 2022-09-15 mark static const struct got_error *
8990 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8991 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8992 136e2bd4 2022-07-23 mark {
8993 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8994 136e2bd4 2022-07-23 mark
8995 136e2bd4 2022-07-23 mark *new_view = NULL;
8996 136e2bd4 2022-07-23 mark
8997 136e2bd4 2022-07-23 mark switch (request) {
8998 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8999 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
9000 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
9001 136e2bd4 2022-07-23 mark
9002 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
9003 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
9004 136e2bd4 2022-07-23 mark view, s->repo);
9005 136e2bd4 2022-07-23 mark } else
9006 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9007 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9008 136e2bd4 2022-07-23 mark break;
9009 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
9010 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
9011 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
9012 136e2bd4 2022-07-23 mark
9013 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
9014 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
9015 136e2bd4 2022-07-23 mark s->repo);
9016 136e2bd4 2022-07-23 mark } else
9017 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9018 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9019 136e2bd4 2022-07-23 mark break;
9020 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
9021 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
9022 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
9023 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
9024 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9025 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
9026 136e2bd4 2022-07-23 mark &view->state.tree);
9027 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9028 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
9029 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9030 136e2bd4 2022-07-23 mark view->state.ref.repo);
9031 136e2bd4 2022-07-23 mark else
9032 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9033 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9034 136e2bd4 2022-07-23 mark break;
9035 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
9036 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9037 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
9038 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
9039 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
9040 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
9041 136e2bd4 2022-07-23 mark view->state.log.repo);
9042 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
9043 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
9044 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
9045 136e2bd4 2022-07-23 mark view->state.ref.repo);
9046 136e2bd4 2022-07-23 mark else
9047 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
9048 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9049 136e2bd4 2022-07-23 mark break;
9050 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
9051 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9052 136e2bd4 2022-07-23 mark if (*new_view == NULL)
9053 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
9054 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
9055 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
9056 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
9057 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
9058 136e2bd4 2022-07-23 mark else
9059 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
9060 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
9061 136e2bd4 2022-07-23 mark if (err)
9062 136e2bd4 2022-07-23 mark view_close(*new_view);
9063 136e2bd4 2022-07-23 mark break;
9064 ec2a9698 2022-09-15 mark case TOG_VIEW_HELP:
9065 94274a8f 2022-09-19 mark *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9066 ec2a9698 2022-09-15 mark if (*new_view == NULL)
9067 ec2a9698 2022-09-15 mark return got_error_from_errno("view_open");
9068 ec2a9698 2022-09-15 mark err = open_help_view(*new_view, view);
9069 ec2a9698 2022-09-15 mark if (err)
9070 ec2a9698 2022-09-15 mark view_close(*new_view);
9071 ec2a9698 2022-09-15 mark break;
9072 136e2bd4 2022-07-23 mark default:
9073 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9074 136e2bd4 2022-07-23 mark }
9075 136e2bd4 2022-07-23 mark
9076 136e2bd4 2022-07-23 mark return err;
9077 136e2bd4 2022-07-23 mark }
9078 136e2bd4 2022-07-23 mark
9079 9b058f45 2022-06-30 mark /*
9080 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
9081 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
9082 9b058f45 2022-06-30 mark */
9083 6458efa5 2020-11-24 stsp static void
9084 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
9085 9b058f45 2022-06-30 mark {
9086 9b058f45 2022-06-30 mark switch (view->type) {
9087 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9088 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9089 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
9090 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
9091 9b058f45 2022-06-30 mark 1);
9092 9b058f45 2022-06-30 mark break;
9093 9b058f45 2022-06-30 mark }
9094 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
9095 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
9096 9b058f45 2022-06-30 mark else
9097 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
9098 9b058f45 2022-06-30 mark s->selected_line += view->offset;
9099 9b058f45 2022-06-30 mark break;
9100 9b058f45 2022-06-30 mark }
9101 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
9102 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
9103 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
9104 9b058f45 2022-06-30 mark break;
9105 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
9106 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
9107 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
9108 9b058f45 2022-06-30 mark break;
9109 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
9110 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
9111 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
9112 9b058f45 2022-06-30 mark break;
9113 9b058f45 2022-06-30 mark default:
9114 9b058f45 2022-06-30 mark break;
9115 9b058f45 2022-06-30 mark }
9116 9b058f45 2022-06-30 mark
9117 9b058f45 2022-06-30 mark view->offset = 0;
9118 9b058f45 2022-06-30 mark }
9119 9b058f45 2022-06-30 mark
9120 9b058f45 2022-06-30 mark /*
9121 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
9122 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
9123 9b058f45 2022-06-30 mark */
9124 9b058f45 2022-06-30 mark static const struct got_error *
9125 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
9126 9b058f45 2022-06-30 mark {
9127 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
9128 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
9129 9b058f45 2022-06-30 mark int *selected = NULL;
9130 9b058f45 2022-06-30 mark int header, offset;
9131 9b058f45 2022-06-30 mark
9132 9b058f45 2022-06-30 mark switch (view->type) {
9133 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
9134 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
9135 9b058f45 2022-06-30 mark header = 3;
9136 9b058f45 2022-06-30 mark scrolld = NULL;
9137 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
9138 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
9139 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
9140 9b058f45 2022-06-30 mark s->selected_line -= offset;
9141 9b058f45 2022-06-30 mark view->offset = offset;
9142 9b058f45 2022-06-30 mark }
9143 9b058f45 2022-06-30 mark break;
9144 9b058f45 2022-06-30 mark }
9145 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
9146 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
9147 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
9148 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
9149 9b058f45 2022-06-30 mark selected = &s->selected;
9150 9b058f45 2022-06-30 mark break;
9151 9b058f45 2022-06-30 mark }
9152 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
9153 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
9154 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
9155 9b058f45 2022-06-30 mark header = 3;
9156 9b058f45 2022-06-30 mark selected = &s->selected;
9157 9b058f45 2022-06-30 mark break;
9158 9b058f45 2022-06-30 mark }
9159 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
9160 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
9161 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
9162 9b058f45 2022-06-30 mark header = 5;
9163 9b058f45 2022-06-30 mark selected = &s->selected;
9164 9b058f45 2022-06-30 mark break;
9165 9b058f45 2022-06-30 mark }
9166 9b058f45 2022-06-30 mark default:
9167 9b058f45 2022-06-30 mark selected = NULL;
9168 9b058f45 2022-06-30 mark scrolld = NULL;
9169 9b058f45 2022-06-30 mark header = 0;
9170 9b058f45 2022-06-30 mark break;
9171 9b058f45 2022-06-30 mark }
9172 9b058f45 2022-06-30 mark
9173 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
9174 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
9175 9b058f45 2022-06-30 mark view->offset = offset;
9176 9b058f45 2022-06-30 mark if (scrolld && offset) {
9177 9b058f45 2022-06-30 mark err = scrolld(view, offset);
9178 9b058f45 2022-06-30 mark *selected -= offset;
9179 9b058f45 2022-06-30 mark }
9180 9b058f45 2022-06-30 mark }
9181 9b058f45 2022-06-30 mark
9182 9b058f45 2022-06-30 mark return err;
9183 9b058f45 2022-06-30 mark }
9184 9b058f45 2022-06-30 mark
9185 9b058f45 2022-06-30 mark static void
9186 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9187 ce5b7c56 2019-07-09 stsp {
9188 6059809a 2020-12-17 stsp size_t i;
9189 9f7d7167 2018-04-29 stsp
9190 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9191 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9192 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
9193 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9194 ce5b7c56 2019-07-09 stsp }
9195 6879ba42 2020-10-01 naddy fputc('\n', fp);
9196 ce5b7c56 2019-07-09 stsp }
9197 ce5b7c56 2019-07-09 stsp
9198 4ed7e80c 2018-05-20 stsp __dead static void
9199 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9200 9f7d7167 2018-04-29 stsp {
9201 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9202 6879ba42 2020-10-01 naddy
9203 6a0a1bd4 2022-10-04 op fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9204 6879ba42 2020-10-01 naddy getprogname());
9205 6879ba42 2020-10-01 naddy if (hflag) {
9206 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9207 6879ba42 2020-10-01 naddy list_commands(fp);
9208 ee85c5e8 2020-02-29 stsp }
9209 6879ba42 2020-10-01 naddy exit(status);
9210 9f7d7167 2018-04-29 stsp }
9211 9f7d7167 2018-04-29 stsp
9212 c2301be8 2018-04-30 stsp static char **
9213 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9214 c2301be8 2018-04-30 stsp {
9215 ee85c5e8 2020-02-29 stsp va_list ap;
9216 c2301be8 2018-04-30 stsp char **argv;
9217 ee85c5e8 2020-02-29 stsp int i;
9218 c2301be8 2018-04-30 stsp
9219 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9220 ee85c5e8 2020-02-29 stsp
9221 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9222 c2301be8 2018-04-30 stsp if (argv == NULL)
9223 c2301be8 2018-04-30 stsp err(1, "calloc");
9224 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9225 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9226 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9227 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9228 c2301be8 2018-04-30 stsp }
9229 c2301be8 2018-04-30 stsp
9230 ee85c5e8 2020-02-29 stsp va_end(ap);
9231 c2301be8 2018-04-30 stsp return argv;
9232 ee85c5e8 2020-02-29 stsp }
9233 ee85c5e8 2020-02-29 stsp
9234 ee85c5e8 2020-02-29 stsp /*
9235 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9236 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9237 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9238 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9239 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9240 ee85c5e8 2020-02-29 stsp */
9241 ee85c5e8 2020-02-29 stsp static const struct got_error *
9242 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9243 ee85c5e8 2020-02-29 stsp {
9244 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9245 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9246 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9247 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9248 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9249 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
9250 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9251 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9252 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
9253 84de9106 2020-12-26 stsp
9254 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9255 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9256 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9257 ee85c5e8 2020-02-29 stsp
9258 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
9259 0ae84acc 2022-06-15 tracey if (error != NULL)
9260 0ae84acc 2022-06-15 tracey goto done;
9261 0ae84acc 2022-06-15 tracey
9262 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9263 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9264 ee85c5e8 2020-02-29 stsp goto done;
9265 ee85c5e8 2020-02-29 stsp
9266 ee85c5e8 2020-02-29 stsp if (worktree)
9267 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9268 ee85c5e8 2020-02-29 stsp else
9269 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9270 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9271 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9272 ee85c5e8 2020-02-29 stsp goto done;
9273 ee85c5e8 2020-02-29 stsp }
9274 ee85c5e8 2020-02-29 stsp
9275 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9276 ee85c5e8 2020-02-29 stsp if (error != NULL)
9277 ee85c5e8 2020-02-29 stsp goto done;
9278 ee85c5e8 2020-02-29 stsp
9279 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9280 ee85c5e8 2020-02-29 stsp repo, worktree);
9281 ee85c5e8 2020-02-29 stsp if (error)
9282 ee85c5e8 2020-02-29 stsp goto done;
9283 ee85c5e8 2020-02-29 stsp
9284 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
9285 84de9106 2020-12-26 stsp if (error)
9286 84de9106 2020-12-26 stsp goto done;
9287 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9288 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9289 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9290 ee85c5e8 2020-02-29 stsp if (error)
9291 ee85c5e8 2020-02-29 stsp goto done;
9292 ee85c5e8 2020-02-29 stsp
9293 ee85c5e8 2020-02-29 stsp if (worktree) {
9294 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9295 ee85c5e8 2020-02-29 stsp worktree = NULL;
9296 ee85c5e8 2020-02-29 stsp }
9297 ee85c5e8 2020-02-29 stsp
9298 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9299 a44927cc 2022-04-07 stsp if (error)
9300 a44927cc 2022-04-07 stsp goto done;
9301 a44927cc 2022-04-07 stsp
9302 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9303 ee85c5e8 2020-02-29 stsp if (error) {
9304 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9305 ee85c5e8 2020-02-29 stsp goto done;
9306 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9307 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9308 6879ba42 2020-10-01 naddy usage(1, 1);
9309 ee85c5e8 2020-02-29 stsp /* not reached */
9310 ee85c5e8 2020-02-29 stsp }
9311 ee85c5e8 2020-02-29 stsp
9312 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9313 ee85c5e8 2020-02-29 stsp if (error)
9314 ee85c5e8 2020-02-29 stsp goto done;
9315 ee85c5e8 2020-02-29 stsp
9316 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9317 ee85c5e8 2020-02-29 stsp argc = 4;
9318 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9319 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9320 ee85c5e8 2020-02-29 stsp done:
9321 1d0f4054 2021-06-17 stsp if (repo) {
9322 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9323 1d0f4054 2021-06-17 stsp if (error == NULL)
9324 1d0f4054 2021-06-17 stsp error = close_err;
9325 1d0f4054 2021-06-17 stsp }
9326 a44927cc 2022-04-07 stsp if (commit)
9327 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
9328 ee85c5e8 2020-02-29 stsp if (worktree)
9329 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9330 0ae84acc 2022-06-15 tracey if (pack_fds) {
9331 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
9332 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
9333 0ae84acc 2022-06-15 tracey if (error == NULL)
9334 0ae84acc 2022-06-15 tracey error = pack_err;
9335 0ae84acc 2022-06-15 tracey }
9336 ee85c5e8 2020-02-29 stsp free(id);
9337 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9338 ee85c5e8 2020-02-29 stsp free(commit_id);
9339 ee85c5e8 2020-02-29 stsp free(cwd);
9340 ee85c5e8 2020-02-29 stsp free(repo_path);
9341 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9342 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9343 ee85c5e8 2020-02-29 stsp int i;
9344 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9345 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9346 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9347 ee85c5e8 2020-02-29 stsp }
9348 87670572 2020-12-26 naddy tog_free_refs();
9349 ee85c5e8 2020-02-29 stsp return error;
9350 c2301be8 2018-04-30 stsp }
9351 c2301be8 2018-04-30 stsp
9352 9f7d7167 2018-04-29 stsp int
9353 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9354 9f7d7167 2018-04-29 stsp {
9355 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9356 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
9357 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9358 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9359 3e166534 2022-02-16 naddy static const struct option longopts[] = {
9360 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9361 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9362 83cd27f8 2020-01-13 stsp };
9363 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
9364 9f7d7167 2018-04-29 stsp
9365 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
9366 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
9367 3264c09c 2022-09-06 mark
9368 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9369 9f7d7167 2018-04-29 stsp
9370 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9371 9f7d7167 2018-04-29 stsp switch (ch) {
9372 9f7d7167 2018-04-29 stsp case 'h':
9373 9f7d7167 2018-04-29 stsp hflag = 1;
9374 9f7d7167 2018-04-29 stsp break;
9375 53ccebc2 2019-07-30 stsp case 'V':
9376 53ccebc2 2019-07-30 stsp Vflag = 1;
9377 53ccebc2 2019-07-30 stsp break;
9378 9f7d7167 2018-04-29 stsp default:
9379 6879ba42 2020-10-01 naddy usage(hflag, 1);
9380 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9381 9f7d7167 2018-04-29 stsp }
9382 9f7d7167 2018-04-29 stsp }
9383 9f7d7167 2018-04-29 stsp
9384 9f7d7167 2018-04-29 stsp argc -= optind;
9385 9f7d7167 2018-04-29 stsp argv += optind;
9386 9814e6a3 2020-09-27 naddy optind = 1;
9387 c2301be8 2018-04-30 stsp optreset = 1;
9388 9f7d7167 2018-04-29 stsp
9389 53ccebc2 2019-07-30 stsp if (Vflag) {
9390 53ccebc2 2019-07-30 stsp got_version_print_str();
9391 6879ba42 2020-10-01 naddy return 0;
9392 53ccebc2 2019-07-30 stsp }
9393 4010e238 2020-12-04 stsp
9394 4010e238 2020-12-04 stsp #ifndef PROFILE
9395 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9396 4010e238 2020-12-04 stsp NULL) == -1)
9397 4010e238 2020-12-04 stsp err(1, "pledge");
9398 4010e238 2020-12-04 stsp #endif
9399 53ccebc2 2019-07-30 stsp
9400 c2301be8 2018-04-30 stsp if (argc == 0) {
9401 f29d3e89 2018-06-23 stsp if (hflag)
9402 6879ba42 2020-10-01 naddy usage(hflag, 0);
9403 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9404 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9405 c2301be8 2018-04-30 stsp argc = 1;
9406 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9407 c2301be8 2018-04-30 stsp } else {
9408 6059809a 2020-12-17 stsp size_t i;
9409 9f7d7167 2018-04-29 stsp
9410 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9411 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9412 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9413 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9414 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9415 9f7d7167 2018-04-29 stsp break;
9416 9f7d7167 2018-04-29 stsp }
9417 9f7d7167 2018-04-29 stsp }
9418 ee85c5e8 2020-02-29 stsp }
9419 3642c4c6 2019-07-09 stsp
9420 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9421 917d79a7 2022-07-01 stsp if (diff_algo_str) {
9422 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
9423 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9424 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
9425 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9426 917d79a7 2022-07-01 stsp }
9427 917d79a7 2022-07-01 stsp
9428 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9429 ee85c5e8 2020-02-29 stsp if (argc != 1)
9430 6879ba42 2020-10-01 naddy usage(0, 1);
9431 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9432 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9433 ee85c5e8 2020-02-29 stsp } else {
9434 ee85c5e8 2020-02-29 stsp if (hflag)
9435 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9436 ee85c5e8 2020-02-29 stsp else
9437 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9438 9f7d7167 2018-04-29 stsp }
9439 9f7d7167 2018-04-29 stsp
9440 9f7d7167 2018-04-29 stsp endwin();
9441 b46c1e04 2020-09-20 naddy putchar('\n');
9442 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9443 a2f4a359 2020-02-28 stsp int i;
9444 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9445 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9446 a2f4a359 2020-02-28 stsp free(cmd_argv);
9447 a2f4a359 2020-02-28 stsp }
9448 a2f4a359 2020-02-28 stsp
9449 e45c294c 2022-10-24 stsp if (error && error->code != GOT_ERR_CANCELLED &&
9450 e45c294c 2022-10-24 stsp error->code != GOT_ERR_EOF &&
9451 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
9452 e45c294c 2022-10-24 stsp error->code != GOT_ERR_PRIVSEP_PIPE &&
9453 e45c294c 2022-10-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9454 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9455 9f7d7167 2018-04-29 stsp return 0;
9456 9f7d7167 2018-04-29 stsp }