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 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 9f7d7167 2018-04-29 stsp
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
21 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
22 80ddbec8 2018-04-29 stsp
23 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
24 31120ada 2018-04-30 stsp #include <errno.h>
25 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
26 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
27 def2f970 2021-09-28 thomas #endif
28 9f7d7167 2018-04-29 stsp #include <curses.h>
29 9f7d7167 2018-04-29 stsp #include <panel.h>
30 9f7d7167 2018-04-29 stsp #include <locale.h>
31 61266923 2020-01-14 stsp #include <signal.h>
32 9f7d7167 2018-04-29 stsp #include <stdlib.h>
33 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
34 26ed57b2 2018-05-19 stsp #include <stdio.h>
35 9f7d7167 2018-04-29 stsp #include <getopt.h>
36 9f7d7167 2018-04-29 stsp #include <string.h>
37 9f7d7167 2018-04-29 stsp #include <err.h>
38 80ddbec8 2018-04-29 stsp #include <unistd.h>
39 26ed57b2 2018-05-19 stsp #include <limits.h>
40 61e69b96 2018-05-20 stsp #include <wchar.h>
41 788c352e 2018-06-16 stsp #include <time.h>
42 84451b3e 2018-07-10 stsp #include <pthread.h>
43 5036bf37 2018-09-24 stsp #include <libgen.h>
44 60493ae3 2019-06-20 stsp #include <regex.h>
45 3da8ef85 2021-09-21 stsp #include <sched.h>
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
112 fc2737d5 2022-09-15 thomas };
113 fc2737d5 2022-09-15 thomas
114 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
116 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
124 d6b05b5b 2018-08-04 stsp };
125 c3e9aa98 2019-05-13 jcs
126 a5d43cac 2022-07-01 thomas enum tog_view_mode {
127 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
130 a5d43cac 2022-07-01 thomas };
131 a5d43cac 2022-07-01 thomas
132 87a675e0 2023-04-22 thomas #define HSPLIT_SCALE 0.3f /* default horizontal split scale */
133 a5d43cac 2022-07-01 thomas
134 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
135 d6b05b5b 2018-08-04 stsp
136 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
137 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
138 ba4f502b 2018-08-04 stsp struct got_object_id *id;
139 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
140 1a76625f 2018-10-22 stsp int idx;
141 ba4f502b 2018-08-04 stsp };
142 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 ba4f502b 2018-08-04 stsp struct commit_queue {
144 ba4f502b 2018-08-04 stsp int ncommits;
145 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
146 6d17833f 2019-11-08 stsp };
147 6d17833f 2019-11-08 stsp
148 f26dddb7 2019-11-08 stsp struct tog_color {
149 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
150 6d17833f 2019-11-08 stsp regex_t regex;
151 6d17833f 2019-11-08 stsp short colorpair;
152 15a087fe 2019-02-21 stsp };
153 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
154 11b20872 2019-11-08 stsp
155 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
157 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
158 2183bbf6 2022-01-23 thomas
159 2183bbf6 2022-01-23 thomas static const struct got_error *
160 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 2183bbf6 2022-01-23 thomas struct got_reference* re2)
162 2183bbf6 2022-01-23 thomas {
163 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
164 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
165 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
166 2183bbf6 2022-01-23 thomas
167 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
168 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
171 2183bbf6 2022-01-23 thomas *cmp = -1;
172 2183bbf6 2022-01-23 thomas return NULL;
173 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
174 2183bbf6 2022-01-23 thomas *cmp = 1;
175 2183bbf6 2022-01-23 thomas return NULL;
176 2183bbf6 2022-01-23 thomas }
177 2183bbf6 2022-01-23 thomas
178 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 2183bbf6 2022-01-23 thomas return NULL;
180 2183bbf6 2022-01-23 thomas }
181 51a10b52 2020-12-26 stsp
182 11b20872 2019-11-08 stsp static const struct got_error *
183 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
184 51a10b52 2020-12-26 stsp {
185 51a10b52 2020-12-26 stsp const struct got_error *err;
186 51a10b52 2020-12-26 stsp
187 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 3bfadbd4 2021-11-20 thomas repo);
190 51a10b52 2020-12-26 stsp if (err)
191 51a10b52 2020-12-26 stsp return err;
192 51a10b52 2020-12-26 stsp
193 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
194 51a10b52 2020-12-26 stsp repo);
195 51a10b52 2020-12-26 stsp }
196 51a10b52 2020-12-26 stsp
197 51a10b52 2020-12-26 stsp static void
198 51a10b52 2020-12-26 stsp tog_free_refs(void)
199 51a10b52 2020-12-26 stsp {
200 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
201 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
202 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
203 51a10b52 2020-12-26 stsp }
204 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
205 51a10b52 2020-12-26 stsp }
206 51a10b52 2020-12-26 stsp
207 51a10b52 2020-12-26 stsp static const struct got_error *
208 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
209 11b20872 2019-11-08 stsp int idx, short color)
210 11b20872 2019-11-08 stsp {
211 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
212 11b20872 2019-11-08 stsp struct tog_color *tc;
213 11b20872 2019-11-08 stsp int regerr = 0;
214 11b20872 2019-11-08 stsp
215 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
216 11b20872 2019-11-08 stsp return NULL;
217 11b20872 2019-11-08 stsp
218 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
219 11b20872 2019-11-08 stsp
220 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
221 11b20872 2019-11-08 stsp if (tc == NULL)
222 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
223 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
224 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 11b20872 2019-11-08 stsp if (regerr) {
226 11b20872 2019-11-08 stsp static char regerr_msg[512];
227 11b20872 2019-11-08 stsp static char err_msg[512];
228 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
229 11b20872 2019-11-08 stsp sizeof(regerr_msg));
230 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 11b20872 2019-11-08 stsp regerr_msg);
232 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 11b20872 2019-11-08 stsp free(tc);
234 11b20872 2019-11-08 stsp return err;
235 11b20872 2019-11-08 stsp }
236 11b20872 2019-11-08 stsp tc->colorpair = idx;
237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
238 11b20872 2019-11-08 stsp return NULL;
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 11b20872 2019-11-08 stsp static void
242 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
247 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
248 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
249 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 11b20872 2019-11-08 stsp free(tc);
251 11b20872 2019-11-08 stsp }
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 ef20f542 2022-06-26 thomas static struct tog_color *
255 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
258 11b20872 2019-11-08 stsp
259 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
260 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
261 11b20872 2019-11-08 stsp return tc;
262 11b20872 2019-11-08 stsp }
263 11b20872 2019-11-08 stsp
264 11b20872 2019-11-08 stsp return NULL;
265 11b20872 2019-11-08 stsp }
266 11b20872 2019-11-08 stsp
267 11b20872 2019-11-08 stsp static int
268 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
269 11b20872 2019-11-08 stsp {
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 11b20872 2019-11-08 stsp return COLOR_CYAN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 11b20872 2019-11-08 stsp return COLOR_YELLOW;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 11b20872 2019-11-08 stsp return COLOR_GREEN;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 91b8c405 2020-01-25 stsp return COLOR_CYAN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_GREEN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 11b20872 2019-11-08 stsp return COLOR_GREEN;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 11b20872 2019-11-08 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_YELLOW;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_GREEN;
294 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
296 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
298 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
300 11b20872 2019-11-08 stsp
301 11b20872 2019-11-08 stsp return -1;
302 11b20872 2019-11-08 stsp }
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp static int
305 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
306 11b20872 2019-11-08 stsp {
307 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
308 11b20872 2019-11-08 stsp
309 11b20872 2019-11-08 stsp if (val == NULL)
310 11b20872 2019-11-08 stsp return default_color_value(envvar);
311 15a087fe 2019-02-21 stsp
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
313 11b20872 2019-11-08 stsp return COLOR_BLACK;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
315 11b20872 2019-11-08 stsp return COLOR_RED;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
317 11b20872 2019-11-08 stsp return COLOR_GREEN;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
319 11b20872 2019-11-08 stsp return COLOR_YELLOW;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
321 11b20872 2019-11-08 stsp return COLOR_BLUE;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
323 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
325 11b20872 2019-11-08 stsp return COLOR_CYAN;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
327 11b20872 2019-11-08 stsp return COLOR_WHITE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
329 11b20872 2019-11-08 stsp return -1;
330 11b20872 2019-11-08 stsp
331 11b20872 2019-11-08 stsp return default_color_value(envvar);
332 11b20872 2019-11-08 stsp }
333 11b20872 2019-11-08 stsp
334 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
335 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
336 3dbaef42 2020-11-24 stsp const char *label1, *label2;
337 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
338 19a6a6b5 2022-07-01 thomas int fd1, fd2;
339 82c78e96 2022-08-06 thomas int lineno;
340 15a087fe 2019-02-21 stsp int first_displayed_line;
341 15a087fe 2019-02-21 stsp int last_displayed_line;
342 15a087fe 2019-02-21 stsp int eof;
343 15a087fe 2019-02-21 stsp int diff_context;
344 3dbaef42 2020-11-24 stsp int ignore_whitespace;
345 64453f7e 2020-11-21 stsp int force_text_diff;
346 15a087fe 2019-02-21 stsp struct got_repository *repo;
347 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
348 fe621944 2020-11-10 stsp size_t nlines;
349 f44b1f58 2020-02-02 tracey int matched_line;
350 f44b1f58 2020-02-02 tracey int selected_line;
351 15a087fe 2019-02-21 stsp
352 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
353 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
354 b01e7d3b 2018-08-04 stsp };
355 b01e7d3b 2018-08-04 stsp
356 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
358 1a76625f 2018-10-22 stsp
359 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
360 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
361 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
362 1a76625f 2018-10-22 stsp int commits_needed;
363 fb280deb 2021-08-30 stsp int load_all;
364 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
365 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
366 1a76625f 2018-10-22 stsp const char *in_repo_path;
367 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp struct got_repository *repo;
369 1af5eddf 2022-06-23 thomas int *pack_fds;
370 1a76625f 2018-10-22 stsp int log_complete;
371 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
372 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
373 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
374 13add988 2019-10-15 stsp int *searching;
375 13add988 2019-10-15 stsp int *search_next_done;
376 13add988 2019-10-15 stsp regex_t *regex;
377 7e8004ba 2022-09-11 thomas int *limiting;
378 7e8004ba 2022-09-11 thomas int limit_match;
379 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
380 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
381 1a76625f 2018-10-22 stsp };
382 1a76625f 2018-10-22 stsp
383 1a76625f 2018-10-22 stsp struct tog_log_view_state {
384 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
385 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
386 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
387 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
388 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
389 b01e7d3b 2018-08-04 stsp int selected;
390 b01e7d3b 2018-08-04 stsp char *in_repo_path;
391 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
392 b672a97a 2020-01-27 stsp int log_branches;
393 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
394 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
395 1a76625f 2018-10-22 stsp sig_atomic_t quit;
396 1a76625f 2018-10-22 stsp pthread_t thread;
397 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
398 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
399 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
401 f69c5a46 2022-07-19 thomas int use_committer;
402 7e8004ba 2022-09-11 thomas int limit_view;
403 7e8004ba 2022-09-11 thomas regex_t limit_regex;
404 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
405 ba4f502b 2018-08-04 stsp };
406 11b20872 2019-11-08 stsp
407 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
408 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
409 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
411 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
412 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
413 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
414 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
415 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
416 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
417 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
418 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
419 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
420 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
421 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
422 ba4f502b 2018-08-04 stsp
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
425 e9424729 2018-08-04 stsp int nlines;
426 e9424729 2018-08-04 stsp
427 e9424729 2018-08-04 stsp struct tog_view *view;
428 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp int *quit;
430 e9424729 2018-08-04 stsp };
431 e9424729 2018-08-04 stsp
432 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
433 e9424729 2018-08-04 stsp const char *path;
434 e9424729 2018-08-04 stsp struct got_repository *repo;
435 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
436 e9424729 2018-08-04 stsp int *complete;
437 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
438 fc06ba56 2019-08-22 stsp void *cancel_arg;
439 1134ebde 2023-04-22 thomas pthread_cond_t blame_complete;
440 e9424729 2018-08-04 stsp };
441 e9424729 2018-08-04 stsp
442 e9424729 2018-08-04 stsp struct tog_blame {
443 e9424729 2018-08-04 stsp FILE *f;
444 be659d10 2020-11-18 stsp off_t filesize;
445 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
446 6fcac457 2018-11-19 stsp int nlines;
447 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
448 e9424729 2018-08-04 stsp pthread_t thread;
449 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
450 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
451 e9424729 2018-08-04 stsp const char *path;
452 7cd52833 2022-06-23 thomas int *pack_fds;
453 e9424729 2018-08-04 stsp };
454 e9424729 2018-08-04 stsp
455 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
456 7cbe629d 2018-08-04 stsp int first_displayed_line;
457 7cbe629d 2018-08-04 stsp int last_displayed_line;
458 7cbe629d 2018-08-04 stsp int selected_line;
459 4fc71f3b 2022-07-12 thomas int last_diffed_line;
460 7cbe629d 2018-08-04 stsp int blame_complete;
461 e5a0f69f 2018-08-18 stsp int eof;
462 e5a0f69f 2018-08-18 stsp int done;
463 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
464 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
465 e5a0f69f 2018-08-18 stsp char *path;
466 7cbe629d 2018-08-04 stsp struct got_repository *repo;
467 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
468 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
469 e9424729 2018-08-04 stsp struct tog_blame blame;
470 6c4c42e0 2019-06-24 stsp int matched_line;
471 11b20872 2019-11-08 stsp struct tog_colors colors;
472 ad80ab7b 2018-08-04 stsp };
473 ad80ab7b 2018-08-04 stsp
474 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
475 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
476 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
478 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
479 ad80ab7b 2018-08-04 stsp int selected;
480 ad80ab7b 2018-08-04 stsp };
481 ad80ab7b 2018-08-04 stsp
482 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 ad80ab7b 2018-08-04 stsp
484 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
485 ad80ab7b 2018-08-04 stsp char *tree_label;
486 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
487 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
488 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
489 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
490 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
491 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
492 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
493 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
494 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
495 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
496 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
497 6458efa5 2020-11-24 stsp struct tog_colors colors;
498 6458efa5 2020-11-24 stsp };
499 6458efa5 2020-11-24 stsp
500 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
501 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
502 6458efa5 2020-11-24 stsp struct got_reference *ref;
503 6458efa5 2020-11-24 stsp int idx;
504 6458efa5 2020-11-24 stsp };
505 6458efa5 2020-11-24 stsp
506 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 6458efa5 2020-11-24 stsp
508 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
509 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
512 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
513 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
514 6458efa5 2020-11-24 stsp struct got_repository *repo;
515 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
516 bddb1296 2019-11-08 stsp struct tog_colors colors;
517 fc2737d5 2022-09-15 thomas };
518 fc2737d5 2022-09-15 thomas
519 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
520 fc2737d5 2022-09-15 thomas FILE *f;
521 fc2737d5 2022-09-15 thomas off_t *line_offsets;
522 fc2737d5 2022-09-15 thomas size_t nlines;
523 fc2737d5 2022-09-15 thomas int lineno;
524 fc2737d5 2022-09-15 thomas int first_displayed_line;
525 fc2737d5 2022-09-15 thomas int last_displayed_line;
526 fc2737d5 2022-09-15 thomas int eof;
527 fc2737d5 2022-09-15 thomas int matched_line;
528 fc2737d5 2022-09-15 thomas int selected_line;
529 fc2737d5 2022-09-15 thomas int all;
530 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
531 fc2737d5 2022-09-15 thomas };
532 fc2737d5 2022-09-15 thomas
533 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
534 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
535 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
536 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
537 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
538 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
539 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
540 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
541 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
542 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
543 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
544 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
545 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
546 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
547 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
548 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
550 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
552 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
553 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
554 c282f787 2023-04-22 thomas KEY_("S", "Switch split-screen layout"), \
555 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
556 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
557 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
558 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
559 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
560 fc2737d5 2022-09-15 thomas \
561 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
562 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
564 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
565 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
566 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
567 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
568 fc2737d5 2022-09-15 thomas " commit"), \
569 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
570 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
571 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
572 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
573 fc2737d5 2022-09-15 thomas \
574 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
575 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
576 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
577 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
578 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
579 fc2737d5 2022-09-15 thomas " data"), \
580 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
583 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
584 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
585 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
586 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
587 fc2737d5 2022-09-15 thomas \
588 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
589 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
590 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
591 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
592 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's commit"), \
595 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
596 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
597 fc2737d5 2022-09-15 thomas \
598 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
599 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
600 fc2737d5 2022-09-15 thomas " selected file"), \
601 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
602 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
603 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
604 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
605 fc2737d5 2022-09-15 thomas \
606 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
607 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
608 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
610 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
611 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
612 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
613 fc2737d5 2022-09-15 thomas
614 fc2737d5 2022-09-15 thomas struct tog_key_map {
615 fc2737d5 2022-09-15 thomas const char *keys;
616 fc2737d5 2022-09-15 thomas const char *info;
617 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
618 7cbe629d 2018-08-04 stsp };
619 7cbe629d 2018-08-04 stsp
620 b85a3496 2023-04-14 thomas /* curses io for tog regress */
621 b85a3496 2023-04-14 thomas struct tog_io {
622 b85a3496 2023-04-14 thomas FILE *cin;
623 b85a3496 2023-04-14 thomas FILE *cout;
624 b85a3496 2023-04-14 thomas FILE *f;
625 5b3a801d 2023-04-22 thomas FILE *sdump;
626 8e778ade 2023-04-22 thomas int wait_for_ui;
627 557d3365 2023-04-14 thomas } tog_io;
628 557d3365 2023-04-14 thomas static int using_mock_io;
629 b85a3496 2023-04-14 thomas
630 13bc6832 2023-04-22 thomas #define TOG_KEY_SCRDUMP SHRT_MIN
631 b85a3496 2023-04-14 thomas
632 669b5ffa 2018-10-07 stsp /*
633 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
634 669b5ffa 2018-10-07 stsp *
635 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
636 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
637 669b5ffa 2018-10-07 stsp * there is enough screen estate.
638 669b5ffa 2018-10-07 stsp *
639 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
640 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
641 669b5ffa 2018-10-07 stsp *
642 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
643 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
644 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
645 669b5ffa 2018-10-07 stsp *
646 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
647 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
648 669b5ffa 2018-10-07 stsp */
649 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
650 669b5ffa 2018-10-07 stsp
651 cc3c9aac 2018-08-01 stsp struct tog_view {
652 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
653 26ed57b2 2018-05-19 stsp WINDOW *window;
654 26ed57b2 2018-05-19 stsp PANEL *panel;
655 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
656 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
657 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
658 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
659 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
660 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
661 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
662 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
663 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
664 9970f7fc 2020-12-03 stsp int dying;
665 669b5ffa 2018-10-07 stsp struct tog_view *parent;
666 669b5ffa 2018-10-07 stsp struct tog_view *child;
667 5dc9f4bc 2018-08-04 stsp
668 e78dc838 2020-12-04 stsp /*
669 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
670 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
671 e78dc838 2020-12-04 stsp * between parent and child.
672 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
673 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
674 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
675 e78dc838 2020-12-04 stsp * situations.
676 e78dc838 2020-12-04 stsp */
677 e78dc838 2020-12-04 stsp int focus_child;
678 e78dc838 2020-12-04 stsp
679 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
680 5dc9f4bc 2018-08-04 stsp /* type-specific state */
681 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
682 5dc9f4bc 2018-08-04 stsp union {
683 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
684 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
685 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
686 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
687 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
688 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
689 5dc9f4bc 2018-08-04 stsp } state;
690 e5a0f69f 2018-08-18 stsp
691 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
692 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
693 e78dc838 2020-12-04 stsp struct tog_view *, int);
694 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
695 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
696 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
697 60493ae3 2019-06-20 stsp
698 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
699 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
700 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
701 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
702 c0c4acc8 2021-01-24 stsp int search_started;
703 60493ae3 2019-06-20 stsp int searching;
704 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
705 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
706 60493ae3 2019-06-20 stsp int search_next_done;
707 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
708 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
709 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
710 1803e47f 2019-06-22 stsp regex_t regex;
711 41605754 2020-11-12 stsp regmatch_t regmatch;
712 f2d06bef 2023-01-23 thomas const char *action;
713 cc3c9aac 2018-08-01 stsp };
714 cd0acaa7 2018-05-20 stsp
715 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
716 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
717 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
718 78756c87 2020-11-24 stsp struct got_repository *);
719 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
720 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
721 e78dc838 2020-12-04 stsp struct tog_view *, int);
722 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
723 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
724 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
725 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
726 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
727 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
728 e5a0f69f 2018-08-18 stsp
729 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
730 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
731 78756c87 2020-11-24 stsp const char *, const char *, int);
732 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
733 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
734 e78dc838 2020-12-04 stsp struct tog_view *, int);
735 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
736 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
737 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
738 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
739 e5a0f69f 2018-08-18 stsp
740 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
741 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
742 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
743 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
744 e78dc838 2020-12-04 stsp struct tog_view *, int);
745 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
746 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
747 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
748 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
749 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
750 e5a0f69f 2018-08-18 stsp
751 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
752 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
753 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
754 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
755 e78dc838 2020-12-04 stsp struct tog_view *, int);
756 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
757 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
758 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
759 6458efa5 2020-11-24 stsp
760 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
761 6458efa5 2020-11-24 stsp struct got_repository *);
762 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
763 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
764 e78dc838 2020-12-04 stsp struct tog_view *, int);
765 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
766 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
767 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
768 2a7d3cd7 2022-09-17 thomas
769 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
770 2a7d3cd7 2022-09-17 thomas struct tog_view *);
771 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
772 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
773 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
774 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
775 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
776 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
777 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
778 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
779 25791caa 2018-10-24 stsp
780 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
781 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
782 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
783 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
784 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
785 25791caa 2018-10-24 stsp
786 25791caa 2018-10-24 stsp static void
787 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
788 25791caa 2018-10-24 stsp {
789 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
790 83baff54 2019-08-12 stsp }
791 83baff54 2019-08-12 stsp
792 83baff54 2019-08-12 stsp static void
793 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
794 83baff54 2019-08-12 stsp {
795 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
796 25791caa 2018-10-24 stsp }
797 26ed57b2 2018-05-19 stsp
798 61266923 2020-01-14 stsp static void
799 61266923 2020-01-14 stsp tog_sigcont(int signo)
800 61266923 2020-01-14 stsp {
801 61266923 2020-01-14 stsp tog_sigcont_received = 1;
802 61266923 2020-01-14 stsp }
803 61266923 2020-01-14 stsp
804 296152d1 2022-05-31 thomas static void
805 296152d1 2022-05-31 thomas tog_sigint(int signo)
806 296152d1 2022-05-31 thomas {
807 296152d1 2022-05-31 thomas tog_sigint_received = 1;
808 296152d1 2022-05-31 thomas }
809 296152d1 2022-05-31 thomas
810 296152d1 2022-05-31 thomas static void
811 296152d1 2022-05-31 thomas tog_sigterm(int signo)
812 296152d1 2022-05-31 thomas {
813 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
814 296152d1 2022-05-31 thomas }
815 296152d1 2022-05-31 thomas
816 296152d1 2022-05-31 thomas static int
817 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
818 296152d1 2022-05-31 thomas {
819 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
820 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
821 296152d1 2022-05-31 thomas }
822 296152d1 2022-05-31 thomas
823 e5a0f69f 2018-08-18 stsp static const struct got_error *
824 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
825 ea5e7bb5 2018-08-01 stsp {
826 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
827 e5a0f69f 2018-08-18 stsp
828 669b5ffa 2018-10-07 stsp if (view->child) {
829 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
830 669b5ffa 2018-10-07 stsp view->child = NULL;
831 669b5ffa 2018-10-07 stsp }
832 e5a0f69f 2018-08-18 stsp if (view->close)
833 e5a0f69f 2018-08-18 stsp err = view->close(view);
834 ea5e7bb5 2018-08-01 stsp if (view->panel)
835 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
836 ea5e7bb5 2018-08-01 stsp if (view->window)
837 ea5e7bb5 2018-08-01 stsp delwin(view->window);
838 ea5e7bb5 2018-08-01 stsp free(view);
839 f2d749db 2022-07-12 thomas return err ? err : child_err;
840 ea5e7bb5 2018-08-01 stsp }
841 ea5e7bb5 2018-08-01 stsp
842 ea5e7bb5 2018-08-01 stsp static struct tog_view *
843 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
844 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
845 ea5e7bb5 2018-08-01 stsp {
846 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
847 ea5e7bb5 2018-08-01 stsp
848 ea5e7bb5 2018-08-01 stsp if (view == NULL)
849 ea5e7bb5 2018-08-01 stsp return NULL;
850 ea5e7bb5 2018-08-01 stsp
851 d6b05b5b 2018-08-04 stsp view->type = type;
852 f7d12f7e 2018-08-01 stsp view->lines = LINES;
853 f7d12f7e 2018-08-01 stsp view->cols = COLS;
854 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
855 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
856 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
857 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
858 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
859 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
860 96a765a8 2018-08-04 stsp view_close(view);
861 ea5e7bb5 2018-08-01 stsp return NULL;
862 ea5e7bb5 2018-08-01 stsp }
863 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
864 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
865 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
866 96a765a8 2018-08-04 stsp view_close(view);
867 ea5e7bb5 2018-08-01 stsp return NULL;
868 ea5e7bb5 2018-08-01 stsp }
869 ea5e7bb5 2018-08-01 stsp
870 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
871 ea5e7bb5 2018-08-01 stsp return view;
872 cdf1ee82 2018-08-01 stsp }
873 cdf1ee82 2018-08-01 stsp
874 0cf4efb1 2018-09-29 stsp static int
875 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
876 0cf4efb1 2018-09-29 stsp {
877 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
878 0cf4efb1 2018-09-29 stsp return 0;
879 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
880 5c60c32a 2018-10-18 stsp }
881 5c60c32a 2018-10-18 stsp
882 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
883 a5d43cac 2022-07-01 thomas static int
884 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
885 a5d43cac 2022-07-01 thomas {
886 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
887 a5d43cac 2022-07-01 thomas }
888 a5d43cac 2022-07-01 thomas
889 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
890 5c60c32a 2018-10-18 stsp
891 5c60c32a 2018-10-18 stsp static const struct got_error *
892 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
893 5c60c32a 2018-10-18 stsp {
894 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
895 5c60c32a 2018-10-18 stsp
896 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
897 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
898 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
899 53d2bdd3 2022-07-10 thomas else
900 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
901 a5d43cac 2022-07-01 thomas view->begin_x = 0;
902 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
903 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
904 53d2bdd3 2022-07-10 thomas view->cols > 119)
905 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
906 53d2bdd3 2022-07-10 thomas else
907 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
908 a5d43cac 2022-07-01 thomas view->begin_y = 0;
909 a5d43cac 2022-07-01 thomas }
910 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
911 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
912 5c60c32a 2018-10-18 stsp view->lines = LINES;
913 5c60c32a 2018-10-18 stsp view->cols = COLS;
914 5c60c32a 2018-10-18 stsp err = view_resize(view);
915 5c60c32a 2018-10-18 stsp if (err)
916 5c60c32a 2018-10-18 stsp return err;
917 5c60c32a 2018-10-18 stsp
918 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
919 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
920 a5d43cac 2022-07-01 thomas
921 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
922 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
923 5c60c32a 2018-10-18 stsp
924 5c60c32a 2018-10-18 stsp return NULL;
925 5c60c32a 2018-10-18 stsp }
926 5c60c32a 2018-10-18 stsp
927 5c60c32a 2018-10-18 stsp static const struct got_error *
928 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
929 5c60c32a 2018-10-18 stsp {
930 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
931 5c60c32a 2018-10-18 stsp
932 5c60c32a 2018-10-18 stsp view->begin_x = 0;
933 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
934 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
935 5c60c32a 2018-10-18 stsp view->ncols = COLS;
936 5c60c32a 2018-10-18 stsp view->lines = LINES;
937 5c60c32a 2018-10-18 stsp view->cols = COLS;
938 5c60c32a 2018-10-18 stsp err = view_resize(view);
939 5c60c32a 2018-10-18 stsp if (err)
940 5c60c32a 2018-10-18 stsp return err;
941 5c60c32a 2018-10-18 stsp
942 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
943 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
944 5c60c32a 2018-10-18 stsp
945 5c60c32a 2018-10-18 stsp return NULL;
946 0cf4efb1 2018-09-29 stsp }
947 0cf4efb1 2018-09-29 stsp
948 5c60c32a 2018-10-18 stsp static int
949 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
950 5c60c32a 2018-10-18 stsp {
951 5c60c32a 2018-10-18 stsp return view->parent == NULL;
952 5c60c32a 2018-10-18 stsp }
953 5c60c32a 2018-10-18 stsp
954 b65b3ea0 2022-06-23 thomas static int
955 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
956 b65b3ea0 2022-06-23 thomas {
957 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
958 e44940c3 2022-07-01 thomas }
959 e44940c3 2022-07-01 thomas
960 e44940c3 2022-07-01 thomas static int
961 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
962 e44940c3 2022-07-01 thomas {
963 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
964 b65b3ea0 2022-06-23 thomas }
965 b65b3ea0 2022-06-23 thomas
966 444d5325 2022-07-03 thomas static int
967 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
968 444d5325 2022-07-03 thomas {
969 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
970 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
971 444d5325 2022-07-03 thomas }
972 444d5325 2022-07-03 thomas
973 a5d43cac 2022-07-01 thomas static void
974 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
975 a5d43cac 2022-07-01 thomas {
976 a5d43cac 2022-07-01 thomas PANEL *panel;
977 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
978 b65b3ea0 2022-06-23 thomas
979 a5d43cac 2022-07-01 thomas if (view->parent)
980 a5d43cac 2022-07-01 thomas return view_border(view->parent);
981 a5d43cac 2022-07-01 thomas
982 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
983 a5d43cac 2022-07-01 thomas if (panel == NULL)
984 a5d43cac 2022-07-01 thomas return;
985 a5d43cac 2022-07-01 thomas
986 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
987 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
988 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
989 88e8b64f 2023-04-22 thomas view->begin_x, ACS_HLINE, view->ncols);
990 a5d43cac 2022-07-01 thomas else
991 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
992 88e8b64f 2023-04-22 thomas ACS_VLINE, view->nlines);
993 a5d43cac 2022-07-01 thomas }
994 a5d43cac 2022-07-01 thomas
995 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
996 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
997 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
998 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
999 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
1000 a5d43cac 2022-07-01 thomas
1001 4d8c2215 2018-08-19 stsp static const struct got_error *
1002 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1003 f7d12f7e 2018-08-01 stsp {
1004 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
1005 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
1006 f7d12f7e 2018-08-01 stsp
1007 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
1008 a5d43cac 2022-07-01 thomas
1009 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1010 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1011 0cf4efb1 2018-09-29 stsp else
1012 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1013 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1014 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1015 0cf4efb1 2018-09-29 stsp else
1016 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1017 6d0fee91 2018-08-01 stsp
1018 4918811f 2022-07-01 thomas if (view->child) {
1019 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1020 a5d43cac 2022-07-01 thomas
1021 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1022 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1023 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1024 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1025 40236d76 2022-06-23 thomas ncols = COLS;
1026 40236d76 2022-06-23 thomas
1027 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1028 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1029 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1030 5c60c32a 2018-10-18 stsp else
1031 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1032 5c60c32a 2018-10-18 stsp } else {
1033 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1034 40236d76 2022-06-23 thomas
1035 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1036 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1037 a5d43cac 2022-07-01 thomas }
1038 a5d43cac 2022-07-01 thomas /*
1039 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1040 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1041 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1042 a5d43cac 2022-07-01 thomas */
1043 a5d43cac 2022-07-01 thomas if (hs) {
1044 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1045 a5d43cac 2022-07-01 thomas if (err)
1046 a5d43cac 2022-07-01 thomas return err;
1047 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1048 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1049 a5d43cac 2022-07-01 thomas if (err)
1050 a5d43cac 2022-07-01 thomas return err;
1051 a5d43cac 2022-07-01 thomas }
1052 a5d43cac 2022-07-01 thomas view_border(view);
1053 a5d43cac 2022-07-01 thomas update_panels();
1054 a5d43cac 2022-07-01 thomas doupdate();
1055 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1056 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1057 a5d43cac 2022-07-01 thomas }
1058 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1059 40236d76 2022-06-23 thomas ncols = COLS;
1060 669b5ffa 2018-10-07 stsp
1061 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1062 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1063 ea0bff04 2022-07-19 thomas if (err)
1064 ea0bff04 2022-07-19 thomas return err;
1065 ea0bff04 2022-07-19 thomas }
1066 ea0bff04 2022-07-19 thomas
1067 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1068 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1069 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1070 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1071 40236d76 2022-06-23 thomas wclear(view->window);
1072 40236d76 2022-06-23 thomas
1073 40236d76 2022-06-23 thomas view->nlines = nlines;
1074 40236d76 2022-06-23 thomas view->ncols = ncols;
1075 40236d76 2022-06-23 thomas view->lines = LINES;
1076 40236d76 2022-06-23 thomas view->cols = COLS;
1077 40236d76 2022-06-23 thomas
1078 5c60c32a 2018-10-18 stsp return NULL;
1079 ea0bff04 2022-07-19 thomas }
1080 ea0bff04 2022-07-19 thomas
1081 ea0bff04 2022-07-19 thomas static const struct got_error *
1082 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1083 ea0bff04 2022-07-19 thomas {
1084 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1085 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1086 3a0139e8 2022-07-22 thomas int n = 0;
1087 ea0bff04 2022-07-19 thomas
1088 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1089 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1090 3a0139e8 2022-07-22 thomas
1091 ea0bff04 2022-07-19 thomas /*
1092 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1093 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1094 ea0bff04 2022-07-19 thomas */
1095 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1096 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1097 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1098 ea0bff04 2022-07-19 thomas }
1099 ea0bff04 2022-07-19 thomas
1100 ea0bff04 2022-07-19 thomas return err;
1101 97cb21cd 2022-07-12 thomas }
1102 97cb21cd 2022-07-12 thomas
1103 97cb21cd 2022-07-12 thomas static void
1104 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1105 97cb21cd 2022-07-12 thomas {
1106 97cb21cd 2022-07-12 thomas if (n == 0)
1107 97cb21cd 2022-07-12 thomas return;
1108 97cb21cd 2022-07-12 thomas
1109 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1110 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1111 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1112 97cb21cd 2022-07-12 thomas else
1113 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1114 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1115 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1116 97cb21cd 2022-07-12 thomas view->offset -= n;
1117 97cb21cd 2022-07-12 thomas else
1118 97cb21cd 2022-07-12 thomas view->offset = 0;
1119 97cb21cd 2022-07-12 thomas }
1120 53d2bdd3 2022-07-10 thomas }
1121 53d2bdd3 2022-07-10 thomas
1122 53d2bdd3 2022-07-10 thomas static const struct got_error *
1123 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1124 53d2bdd3 2022-07-10 thomas {
1125 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1126 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1127 53d2bdd3 2022-07-10 thomas
1128 53d2bdd3 2022-07-10 thomas if (view->parent)
1129 53d2bdd3 2022-07-10 thomas v = view->parent;
1130 53d2bdd3 2022-07-10 thomas else
1131 53d2bdd3 2022-07-10 thomas v = view;
1132 53d2bdd3 2022-07-10 thomas
1133 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1134 53d2bdd3 2022-07-10 thomas return NULL;
1135 53d2bdd3 2022-07-10 thomas
1136 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1137 53d2bdd3 2022-07-10 thomas
1138 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1139 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1140 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1141 53d2bdd3 2022-07-10 thomas if (view->parent)
1142 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1143 53d2bdd3 2022-07-10 thomas else
1144 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1145 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1146 53d2bdd3 2022-07-10 thomas view->count = 0;
1147 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1148 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1149 53d2bdd3 2022-07-10 thomas view->count = 0;
1150 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1151 53d2bdd3 2022-07-10 thomas }
1152 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1153 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1154 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1155 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1156 53d2bdd3 2022-07-10 thomas if (err)
1157 53d2bdd3 2022-07-10 thomas return err;
1158 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1159 53d2bdd3 2022-07-10 thomas } else {
1160 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1161 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1162 53d2bdd3 2022-07-10 thomas if (view->parent)
1163 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1164 53d2bdd3 2022-07-10 thomas else
1165 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1166 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1167 53d2bdd3 2022-07-10 thomas view->count = 0;
1168 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1169 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1170 53d2bdd3 2022-07-10 thomas view->count = 0;
1171 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1172 53d2bdd3 2022-07-10 thomas }
1173 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1174 53d2bdd3 2022-07-10 thomas }
1175 53d2bdd3 2022-07-10 thomas
1176 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1177 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1178 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1179 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1180 53d2bdd3 2022-07-10 thomas
1181 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1182 53d2bdd3 2022-07-10 thomas if (err)
1183 53d2bdd3 2022-07-10 thomas return err;
1184 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1185 53d2bdd3 2022-07-10 thomas if (err)
1186 53d2bdd3 2022-07-10 thomas return err;
1187 53d2bdd3 2022-07-10 thomas
1188 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1189 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1190 53d2bdd3 2022-07-10 thomas if (err)
1191 53d2bdd3 2022-07-10 thomas return err;
1192 53d2bdd3 2022-07-10 thomas }
1193 53d2bdd3 2022-07-10 thomas
1194 3a0139e8 2022-07-22 thomas if (v->resize)
1195 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1196 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1197 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1198 53d2bdd3 2022-07-10 thomas
1199 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1200 53d2bdd3 2022-07-10 thomas
1201 53d2bdd3 2022-07-10 thomas return err;
1202 53d2bdd3 2022-07-10 thomas }
1203 53d2bdd3 2022-07-10 thomas
1204 53d2bdd3 2022-07-10 thomas static void
1205 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1206 53d2bdd3 2022-07-10 thomas {
1207 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1208 53d2bdd3 2022-07-10 thomas
1209 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1210 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1211 669b5ffa 2018-10-07 stsp }
1212 669b5ffa 2018-10-07 stsp
1213 669b5ffa 2018-10-07 stsp static const struct got_error *
1214 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1215 669b5ffa 2018-10-07 stsp {
1216 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1217 669b5ffa 2018-10-07 stsp
1218 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1219 669b5ffa 2018-10-07 stsp return NULL;
1220 669b5ffa 2018-10-07 stsp
1221 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1222 669b5ffa 2018-10-07 stsp view->child = NULL;
1223 669b5ffa 2018-10-07 stsp return err;
1224 669b5ffa 2018-10-07 stsp }
1225 669b5ffa 2018-10-07 stsp
1226 40236d76 2022-06-23 thomas static const struct got_error *
1227 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1228 669b5ffa 2018-10-07 stsp {
1229 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1230 53d2bdd3 2022-07-10 thomas
1231 669b5ffa 2018-10-07 stsp view->child = child;
1232 669b5ffa 2018-10-07 stsp child->parent = view;
1233 40236d76 2022-06-23 thomas
1234 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1235 53d2bdd3 2022-07-10 thomas if (err)
1236 53d2bdd3 2022-07-10 thomas return err;
1237 53d2bdd3 2022-07-10 thomas
1238 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1239 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1240 53d2bdd3 2022-07-10 thomas
1241 53d2bdd3 2022-07-10 thomas return err;
1242 bfddd0d9 2018-09-29 stsp }
1243 2a31b33b 2022-07-23 thomas
1244 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1245 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1246 2a31b33b 2022-07-23 thomas
1247 2a31b33b 2022-07-23 thomas static const struct got_error *
1248 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1249 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1250 2a31b33b 2022-07-23 thomas {
1251 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1252 2a31b33b 2022-07-23 thomas const struct got_error *err;
1253 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1254 2a31b33b 2022-07-23 thomas
1255 2a31b33b 2022-07-23 thomas *requested = NULL;
1256 2a31b33b 2022-07-23 thomas
1257 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1258 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1259 bfddd0d9 2018-09-29 stsp
1260 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1261 2a31b33b 2022-07-23 thomas if (err)
1262 2a31b33b 2022-07-23 thomas return err;
1263 2a31b33b 2022-07-23 thomas
1264 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1265 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1266 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1267 2a31b33b 2022-07-23 thomas if (err)
1268 2a31b33b 2022-07-23 thomas return err;
1269 2a31b33b 2022-07-23 thomas }
1270 2a31b33b 2022-07-23 thomas
1271 2a31b33b 2022-07-23 thomas view->focussed = 0;
1272 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1273 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1274 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1275 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1276 2a31b33b 2022-07-23 thomas
1277 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1278 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1279 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1280 2a31b33b 2022-07-23 thomas if (err)
1281 2a31b33b 2022-07-23 thomas return err;
1282 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1283 2a31b33b 2022-07-23 thomas if (err)
1284 2a31b33b 2022-07-23 thomas return err;
1285 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1286 2a31b33b 2022-07-23 thomas } else
1287 2a31b33b 2022-07-23 thomas *requested = new_view;
1288 2a31b33b 2022-07-23 thomas
1289 2a31b33b 2022-07-23 thomas return NULL;
1290 2a31b33b 2022-07-23 thomas }
1291 2a31b33b 2022-07-23 thomas
1292 34bc9ec9 2019-02-22 stsp static void
1293 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1294 25791caa 2018-10-24 stsp {
1295 25791caa 2018-10-24 stsp int cols, lines;
1296 25791caa 2018-10-24 stsp struct winsize size;
1297 25791caa 2018-10-24 stsp
1298 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1299 25791caa 2018-10-24 stsp cols = 80; /* Default */
1300 25791caa 2018-10-24 stsp lines = 24;
1301 25791caa 2018-10-24 stsp } else {
1302 25791caa 2018-10-24 stsp cols = size.ws_col;
1303 25791caa 2018-10-24 stsp lines = size.ws_row;
1304 25791caa 2018-10-24 stsp }
1305 25791caa 2018-10-24 stsp resize_term(lines, cols);
1306 2b49a8ae 2019-06-22 stsp }
1307 2b49a8ae 2019-06-22 stsp
1308 2b49a8ae 2019-06-22 stsp static const struct got_error *
1309 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1310 2b49a8ae 2019-06-22 stsp {
1311 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1312 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1313 2b49a8ae 2019-06-22 stsp char pattern[1024];
1314 2b49a8ae 2019-06-22 stsp int ret;
1315 c0c4acc8 2021-01-24 stsp
1316 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1317 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1318 c0c4acc8 2021-01-24 stsp view->searching = 0;
1319 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1320 c0c4acc8 2021-01-24 stsp }
1321 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1322 2b49a8ae 2019-06-22 stsp
1323 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1324 2b49a8ae 2019-06-22 stsp return NULL;
1325 2b49a8ae 2019-06-22 stsp
1326 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1327 a5d43cac 2022-07-01 thomas v = view->child;
1328 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1329 d07291c6 2022-12-30 thomas v = view->parent;
1330 2b49a8ae 2019-06-22 stsp
1331 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1332 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1333 a5d43cac 2022-07-01 thomas
1334 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1335 2b49a8ae 2019-06-22 stsp nocbreak();
1336 2b49a8ae 2019-06-22 stsp echo();
1337 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1338 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1339 2b49a8ae 2019-06-22 stsp cbreak();
1340 2b49a8ae 2019-06-22 stsp noecho();
1341 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1342 557d3365 2023-04-14 thomas if (!fast_refresh && !using_mock_io)
1343 f54d892e 2023-02-17 thomas halfdelay(10);
1344 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1345 2b49a8ae 2019-06-22 stsp return NULL;
1346 2b49a8ae 2019-06-22 stsp
1347 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1348 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1349 7c32bd05 2019-06-22 stsp if (err) {
1350 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1351 7c32bd05 2019-06-22 stsp return err;
1352 7c32bd05 2019-06-22 stsp }
1353 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1354 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1355 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1356 2b49a8ae 2019-06-22 stsp view->search_next(view);
1357 2b49a8ae 2019-06-22 stsp }
1358 2b49a8ae 2019-06-22 stsp
1359 2b49a8ae 2019-06-22 stsp return NULL;
1360 64486692 2022-07-07 thomas }
1361 64486692 2022-07-07 thomas
1362 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1363 64486692 2022-07-07 thomas static const struct got_error *
1364 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1365 64486692 2022-07-07 thomas {
1366 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1367 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1368 64486692 2022-07-07 thomas
1369 64486692 2022-07-07 thomas if (view->parent)
1370 64486692 2022-07-07 thomas v = view->parent;
1371 64486692 2022-07-07 thomas else
1372 64486692 2022-07-07 thomas v = view;
1373 64486692 2022-07-07 thomas
1374 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1375 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1376 ddbc4d37 2022-07-12 thomas else
1377 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1378 64486692 2022-07-07 thomas
1379 ddbc4d37 2022-07-12 thomas if (!v->child)
1380 ddbc4d37 2022-07-12 thomas return NULL;
1381 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1382 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1383 ddbc4d37 2022-07-12 thomas
1384 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1385 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1386 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1387 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1388 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1389 64486692 2022-07-07 thomas
1390 ddbc4d37 2022-07-12 thomas
1391 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1392 64486692 2022-07-07 thomas v->ncols = COLS;
1393 64486692 2022-07-07 thomas v->child->ncols = COLS;
1394 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1395 64486692 2022-07-07 thomas
1396 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1397 64486692 2022-07-07 thomas if (err)
1398 64486692 2022-07-07 thomas return err;
1399 64486692 2022-07-07 thomas }
1400 64486692 2022-07-07 thomas v->child->mode = v->mode;
1401 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1402 64486692 2022-07-07 thomas v->focus_child = 1;
1403 64486692 2022-07-07 thomas
1404 64486692 2022-07-07 thomas err = view_fullscreen(v);
1405 64486692 2022-07-07 thomas if (err)
1406 64486692 2022-07-07 thomas return err;
1407 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1408 64486692 2022-07-07 thomas if (err)
1409 64486692 2022-07-07 thomas return err;
1410 64486692 2022-07-07 thomas
1411 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1412 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1413 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1414 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1415 cf1fe301 2022-07-29 thomas if (err)
1416 cf1fe301 2022-07-29 thomas return err;
1417 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1418 cf1fe301 2022-07-29 thomas if (err)
1419 cf1fe301 2022-07-29 thomas return err;
1420 ddbc4d37 2022-07-12 thomas } else {
1421 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1422 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1423 64486692 2022-07-07 thomas }
1424 f4e6231a 2022-07-22 thomas if (v->resize)
1425 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1426 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1427 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1428 64486692 2022-07-07 thomas
1429 64486692 2022-07-07 thomas return err;
1430 0cf4efb1 2018-09-29 stsp }
1431 6d0fee91 2018-08-01 stsp
1432 07b0611c 2022-06-23 thomas /*
1433 b85a3496 2023-04-14 thomas * Strip trailing whitespace from str starting at byte *n;
1434 b85a3496 2023-04-14 thomas * if *n < 0, use strlen(str). Return new str length in *n.
1435 b85a3496 2023-04-14 thomas */
1436 b85a3496 2023-04-14 thomas static void
1437 b85a3496 2023-04-14 thomas strip_trailing_ws(char *str, int *n)
1438 b85a3496 2023-04-14 thomas {
1439 b85a3496 2023-04-14 thomas size_t x = *n;
1440 b85a3496 2023-04-14 thomas
1441 b85a3496 2023-04-14 thomas if (str == NULL || *str == '\0')
1442 b85a3496 2023-04-14 thomas return;
1443 b85a3496 2023-04-14 thomas
1444 b85a3496 2023-04-14 thomas if (x < 0)
1445 b85a3496 2023-04-14 thomas x = strlen(str);
1446 b85a3496 2023-04-14 thomas
1447 b85a3496 2023-04-14 thomas while (x-- > 0 && isspace((unsigned char)str[x]))
1448 b85a3496 2023-04-14 thomas str[x] = '\0';
1449 b85a3496 2023-04-14 thomas
1450 b85a3496 2023-04-14 thomas *n = x + 1;
1451 b85a3496 2023-04-14 thomas }
1452 b85a3496 2023-04-14 thomas
1453 b85a3496 2023-04-14 thomas /*
1454 b85a3496 2023-04-14 thomas * Extract visible substring of line y from the curses screen
1455 13bc6832 2023-04-22 thomas * and strip trailing whitespace. If vline is set, overwrite
1456 13bc6832 2023-04-22 thomas * line[vline] with '|' because the ACS_VLINE character is
1457 13bc6832 2023-04-22 thomas * written out as 'x'. Write the line to file f.
1458 b85a3496 2023-04-14 thomas */
1459 b85a3496 2023-04-14 thomas static const struct got_error *
1460 b85a3496 2023-04-14 thomas view_write_line(FILE *f, int y, int vline)
1461 b85a3496 2023-04-14 thomas {
1462 b85a3496 2023-04-14 thomas char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1463 b85a3496 2023-04-14 thomas int r, w;
1464 b85a3496 2023-04-14 thomas
1465 b85a3496 2023-04-14 thomas r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1466 b85a3496 2023-04-14 thomas if (r == ERR)
1467 b85a3496 2023-04-14 thomas return got_error_fmt(GOT_ERR_RANGE,
1468 b85a3496 2023-04-14 thomas "failed to extract line %d", y);
1469 b85a3496 2023-04-14 thomas
1470 b85a3496 2023-04-14 thomas /*
1471 b85a3496 2023-04-14 thomas * In some views, lines are padded with blanks to COLS width.
1472 b85a3496 2023-04-14 thomas * Strip them so we can diff without the -b flag when testing.
1473 b85a3496 2023-04-14 thomas */
1474 b85a3496 2023-04-14 thomas strip_trailing_ws(line, &r);
1475 b85a3496 2023-04-14 thomas
1476 13bc6832 2023-04-22 thomas if (vline > 0)
1477 b85a3496 2023-04-14 thomas line[vline] = '|';
1478 b85a3496 2023-04-14 thomas
1479 b85a3496 2023-04-14 thomas w = fprintf(f, "%s\n", line);
1480 b85a3496 2023-04-14 thomas if (w != r + 1) /* \n */
1481 b85a3496 2023-04-14 thomas return got_ferror(f, GOT_ERR_IO);
1482 b85a3496 2023-04-14 thomas
1483 b85a3496 2023-04-14 thomas return NULL;
1484 b85a3496 2023-04-14 thomas }
1485 b85a3496 2023-04-14 thomas
1486 b85a3496 2023-04-14 thomas /*
1487 b85a3496 2023-04-14 thomas * Capture the visible curses screen by writing each line to the
1488 b85a3496 2023-04-14 thomas * file at the path set via the TOG_SCR_DUMP environment variable.
1489 b85a3496 2023-04-14 thomas */
1490 b85a3496 2023-04-14 thomas static const struct got_error *
1491 b85a3496 2023-04-14 thomas screendump(struct tog_view *view)
1492 b85a3496 2023-04-14 thomas {
1493 b85a3496 2023-04-14 thomas const struct got_error *err;
1494 b85a3496 2023-04-14 thomas int i;
1495 b85a3496 2023-04-14 thomas
1496 5b3a801d 2023-04-22 thomas err = got_opentemp_truncate(tog_io.sdump);
1497 5b3a801d 2023-04-22 thomas if (err)
1498 5b3a801d 2023-04-22 thomas return err;
1499 b85a3496 2023-04-14 thomas
1500 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_x) ||
1501 b85a3496 2023-04-14 thomas (view->parent && view->begin_x)) {
1502 b85a3496 2023-04-14 thomas int ncols = view->child ? view->ncols : view->parent->ncols;
1503 b85a3496 2023-04-14 thomas
1504 b85a3496 2023-04-14 thomas /* vertical splitscreen */
1505 b85a3496 2023-04-14 thomas for (i = 0; i < view->nlines; ++i) {
1506 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, ncols - 1);
1507 b85a3496 2023-04-14 thomas if (err)
1508 b85a3496 2023-04-14 thomas goto done;
1509 b85a3496 2023-04-14 thomas }
1510 b85a3496 2023-04-14 thomas } else {
1511 b85a3496 2023-04-14 thomas int hline = 0;
1512 b85a3496 2023-04-14 thomas
1513 b85a3496 2023-04-14 thomas /* fullscreen or horizontal splitscreen */
1514 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_y) ||
1515 b85a3496 2023-04-14 thomas (view->parent && view->begin_y)) /* hsplit */
1516 b85a3496 2023-04-14 thomas hline = view->child ?
1517 b85a3496 2023-04-14 thomas view->child->begin_y : view->begin_y;
1518 b85a3496 2023-04-14 thomas
1519 b85a3496 2023-04-14 thomas for (i = 0; i < view->lines; i++) {
1520 13bc6832 2023-04-22 thomas if (hline && i == hline - 1) {
1521 b85a3496 2023-04-14 thomas int c;
1522 b85a3496 2023-04-14 thomas
1523 b85a3496 2023-04-14 thomas /* ACS_HLINE writes out as 'q', overwrite it */
1524 b85a3496 2023-04-14 thomas for (c = 0; c < view->cols; ++c)
1525 5b3a801d 2023-04-22 thomas fputc('-', tog_io.sdump);
1526 5b3a801d 2023-04-22 thomas fputc('\n', tog_io.sdump);
1527 b85a3496 2023-04-14 thomas continue;
1528 b85a3496 2023-04-14 thomas }
1529 b85a3496 2023-04-14 thomas
1530 5b3a801d 2023-04-22 thomas err = view_write_line(tog_io.sdump, i, 0);
1531 b85a3496 2023-04-14 thomas if (err)
1532 b85a3496 2023-04-14 thomas goto done;
1533 b85a3496 2023-04-14 thomas }
1534 b85a3496 2023-04-14 thomas }
1535 b85a3496 2023-04-14 thomas
1536 b85a3496 2023-04-14 thomas done:
1537 b85a3496 2023-04-14 thomas return err;
1538 b85a3496 2023-04-14 thomas }
1539 b85a3496 2023-04-14 thomas
1540 b85a3496 2023-04-14 thomas /*
1541 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1542 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1543 07b0611c 2022-06-23 thomas */
1544 07b0611c 2022-06-23 thomas static int
1545 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1546 07b0611c 2022-06-23 thomas {
1547 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1548 a5d43cac 2022-07-01 thomas int x, n = 0;
1549 07b0611c 2022-06-23 thomas
1550 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1551 a5d43cac 2022-07-01 thomas v = view->child;
1552 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1553 a5d43cac 2022-07-01 thomas v = view->parent;
1554 a5d43cac 2022-07-01 thomas
1555 07b0611c 2022-06-23 thomas view->count = 0;
1556 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1557 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1558 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1559 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1560 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1561 07b0611c 2022-06-23 thomas
1562 07b0611c 2022-06-23 thomas do {
1563 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1564 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1565 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1566 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1567 a5d43cac 2022-07-01 thomas }
1568 a5d43cac 2022-07-01 thomas
1569 07b0611c 2022-06-23 thomas /*
1570 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1571 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1572 07b0611c 2022-06-23 thomas */
1573 07b0611c 2022-06-23 thomas if (n >= 9999999)
1574 07b0611c 2022-06-23 thomas n = 9999999;
1575 07b0611c 2022-06-23 thomas else
1576 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1577 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1578 07b0611c 2022-06-23 thomas
1579 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1580 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1581 07dd3ed3 2022-08-06 thomas n = 0;
1582 07dd3ed3 2022-08-06 thomas c = 0;
1583 07dd3ed3 2022-08-06 thomas }
1584 07dd3ed3 2022-08-06 thomas
1585 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1586 07b0611c 2022-06-23 thomas view->count = n;
1587 07b0611c 2022-06-23 thomas
1588 07b0611c 2022-06-23 thomas return c;
1589 f2d06bef 2023-01-23 thomas }
1590 f2d06bef 2023-01-23 thomas
1591 f2d06bef 2023-01-23 thomas static void
1592 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1593 f2d06bef 2023-01-23 thomas {
1594 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1595 f2d06bef 2023-01-23 thomas
1596 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1597 f2d06bef 2023-01-23 thomas v = view->child;
1598 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1599 f2d06bef 2023-01-23 thomas v = view->parent;
1600 f2d06bef 2023-01-23 thomas
1601 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1602 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1603 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1604 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1605 f2d06bef 2023-01-23 thomas
1606 f2d06bef 2023-01-23 thomas /*
1607 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1608 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1609 f2d06bef 2023-01-23 thomas */
1610 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1611 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1612 f2d06bef 2023-01-23 thomas view->action = NULL;
1613 f2d06bef 2023-01-23 thomas } else
1614 f2d06bef 2023-01-23 thomas view->action = NULL;
1615 07b0611c 2022-06-23 thomas }
1616 07b0611c 2022-06-23 thomas
1617 b85a3496 2023-04-14 thomas /*
1618 b85a3496 2023-04-14 thomas * Read the next line from the test script and assign
1619 b85a3496 2023-04-14 thomas * key instruction to *ch. If at EOF, set the *done flag.
1620 b85a3496 2023-04-14 thomas */
1621 0cf4efb1 2018-09-29 stsp static const struct got_error *
1622 a184c764 2023-04-22 thomas tog_read_script_key(FILE *script, struct tog_view *view, int *ch, int *done)
1623 b85a3496 2023-04-14 thomas {
1624 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1625 b85a3496 2023-04-14 thomas char *line = NULL;
1626 b85a3496 2023-04-14 thomas size_t linesz = 0;
1627 8e778ade 2023-04-22 thomas
1628 a184c764 2023-04-22 thomas if (view->count && --view->count) {
1629 a184c764 2023-04-22 thomas *ch = view->ch;
1630 a184c764 2023-04-22 thomas return NULL;
1631 a184c764 2023-04-22 thomas } else
1632 a184c764 2023-04-22 thomas *ch = -1;
1633 b85a3496 2023-04-14 thomas
1634 b85a3496 2023-04-14 thomas if (getline(&line, &linesz, script) == -1) {
1635 b85a3496 2023-04-14 thomas if (feof(script)) {
1636 b85a3496 2023-04-14 thomas *done = 1;
1637 b85a3496 2023-04-14 thomas goto done;
1638 b85a3496 2023-04-14 thomas } else {
1639 b85a3496 2023-04-14 thomas err = got_ferror(script, GOT_ERR_IO);
1640 b85a3496 2023-04-14 thomas goto done;
1641 b85a3496 2023-04-14 thomas }
1642 8e778ade 2023-04-22 thomas }
1643 1134ebde 2023-04-22 thomas
1644 8e778ade 2023-04-22 thomas if (strncasecmp(line, "WAIT_FOR_UI", 11) == 0)
1645 8e778ade 2023-04-22 thomas tog_io.wait_for_ui = 1;
1646 8e778ade 2023-04-22 thomas else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1647 b85a3496 2023-04-14 thomas *ch = KEY_ENTER;
1648 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1649 b85a3496 2023-04-14 thomas *ch = KEY_RIGHT;
1650 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1651 b85a3496 2023-04-14 thomas *ch = KEY_LEFT;
1652 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1653 b85a3496 2023-04-14 thomas *ch = KEY_DOWN;
1654 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_UP", 6) == 0)
1655 b85a3496 2023-04-14 thomas *ch = KEY_UP;
1656 52c5094b 2023-04-28 thomas else if (strncasecmp(line, "TAB", 3) == 0)
1657 52c5094b 2023-04-28 thomas *ch = '\t';
1658 13bc6832 2023-04-22 thomas else if (strncasecmp(line, "SCREENDUMP", 10) == 0)
1659 b85a3496 2023-04-14 thomas *ch = TOG_KEY_SCRDUMP;
1660 a184c764 2023-04-22 thomas else if (isdigit((unsigned char)*line)) {
1661 a184c764 2023-04-22 thomas char *t = line;
1662 a184c764 2023-04-22 thomas
1663 a184c764 2023-04-22 thomas while (isdigit((unsigned char)*t))
1664 a184c764 2023-04-22 thomas ++t;
1665 a184c764 2023-04-22 thomas view->ch = *ch = *t;
1666 a184c764 2023-04-22 thomas *t = '\0';
1667 a184c764 2023-04-22 thomas /* ignore error, view->count is 0 if instruction is invalid */
1668 a184c764 2023-04-22 thomas view->count = strtonum(line, 0, INT_MAX, NULL);
1669 a184c764 2023-04-22 thomas } else
1670 b85a3496 2023-04-14 thomas *ch = *line;
1671 b85a3496 2023-04-14 thomas
1672 b85a3496 2023-04-14 thomas done:
1673 b85a3496 2023-04-14 thomas free(line);
1674 b85a3496 2023-04-14 thomas return err;
1675 b85a3496 2023-04-14 thomas }
1676 b85a3496 2023-04-14 thomas
1677 b85a3496 2023-04-14 thomas static const struct got_error *
1678 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1679 557d3365 2023-04-14 thomas struct tog_view_list_head *views, int fast_refresh)
1680 e5a0f69f 2018-08-18 stsp {
1681 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1682 669b5ffa 2018-10-07 stsp struct tog_view *v;
1683 1a76625f 2018-10-22 stsp int ch, errcode;
1684 e5a0f69f 2018-08-18 stsp
1685 e5a0f69f 2018-08-18 stsp *new = NULL;
1686 f2d06bef 2023-01-23 thomas
1687 f2d06bef 2023-01-23 thomas if (view->action)
1688 f2d06bef 2023-01-23 thomas action_report(view);
1689 8f4ed634 2020-03-26 stsp
1690 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1691 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1692 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1693 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1694 07b0611c 2022-06-23 thomas view->count = 0;
1695 07b0611c 2022-06-23 thomas }
1696 e5a0f69f 2018-08-18 stsp
1697 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1698 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1699 82954512 2020-02-03 stsp if (errcode)
1700 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1701 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1702 3da8ef85 2021-09-21 stsp sched_yield();
1703 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1704 82954512 2020-02-03 stsp if (errcode)
1705 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1706 82954512 2020-02-03 stsp "pthread_mutex_lock");
1707 60493ae3 2019-06-20 stsp view->search_next(view);
1708 60493ae3 2019-06-20 stsp return NULL;
1709 60493ae3 2019-06-20 stsp }
1710 60493ae3 2019-06-20 stsp
1711 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1712 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1713 1a76625f 2018-10-22 stsp if (errcode)
1714 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1715 b85a3496 2023-04-14 thomas
1716 557d3365 2023-04-14 thomas if (using_mock_io) {
1717 a184c764 2023-04-22 thomas err = tog_read_script_key(tog_io.f, view, &ch, done);
1718 9d9cab5e 2023-04-22 thomas if (err) {
1719 9d9cab5e 2023-04-22 thomas errcode = pthread_mutex_lock(&tog_mutex);
1720 b85a3496 2023-04-14 thomas return err;
1721 9d9cab5e 2023-04-22 thomas }
1722 b85a3496 2023-04-14 thomas } else if (view->count && --view->count) {
1723 634cb454 2022-07-03 thomas cbreak();
1724 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1725 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1726 b85a3496 2023-04-14 thomas /* let C-g or backspace abort unfinished count */
1727 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1728 634cb454 2022-07-03 thomas view->count = 0;
1729 634cb454 2022-07-03 thomas else
1730 634cb454 2022-07-03 thomas ch = view->ch;
1731 634cb454 2022-07-03 thomas } else {
1732 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1733 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1734 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1735 07b0611c 2022-06-23 thomas }
1736 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1737 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1738 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1739 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1740 1a76625f 2018-10-22 stsp if (errcode)
1741 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1742 25791caa 2018-10-24 stsp
1743 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1744 25791caa 2018-10-24 stsp tog_resizeterm();
1745 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1746 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1747 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1748 25791caa 2018-10-24 stsp err = view_resize(v);
1749 25791caa 2018-10-24 stsp if (err)
1750 25791caa 2018-10-24 stsp return err;
1751 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1752 25791caa 2018-10-24 stsp if (err)
1753 25791caa 2018-10-24 stsp return err;
1754 cdfcfb03 2020-12-06 stsp if (v->child) {
1755 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1756 cdfcfb03 2020-12-06 stsp if (err)
1757 cdfcfb03 2020-12-06 stsp return err;
1758 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1759 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1760 cdfcfb03 2020-12-06 stsp if (err)
1761 cdfcfb03 2020-12-06 stsp return err;
1762 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1763 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1764 53d2bdd3 2022-07-10 thomas if (err)
1765 53d2bdd3 2022-07-10 thomas return err;
1766 53d2bdd3 2022-07-10 thomas }
1767 cdfcfb03 2020-12-06 stsp }
1768 25791caa 2018-10-24 stsp }
1769 25791caa 2018-10-24 stsp }
1770 25791caa 2018-10-24 stsp
1771 e5a0f69f 2018-08-18 stsp switch (ch) {
1772 fc2737d5 2022-09-15 thomas case '?':
1773 fc2737d5 2022-09-15 thomas case 'H':
1774 fc2737d5 2022-09-15 thomas case KEY_F(1):
1775 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1776 fc2737d5 2022-09-15 thomas err = view->reset(view);
1777 fc2737d5 2022-09-15 thomas else
1778 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1779 fc2737d5 2022-09-15 thomas break;
1780 1e37a5c2 2019-05-12 jcs case '\t':
1781 07b0611c 2022-06-23 thomas view->count = 0;
1782 1e37a5c2 2019-05-12 jcs if (view->child) {
1783 e78dc838 2020-12-04 stsp view->focussed = 0;
1784 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1785 e78dc838 2020-12-04 stsp view->focus_child = 1;
1786 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1787 e78dc838 2020-12-04 stsp view->focussed = 0;
1788 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1789 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1790 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1791 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1792 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1793 3a0139e8 2022-07-22 thomas 0);
1794 a5d43cac 2022-07-01 thomas if (err)
1795 a5d43cac 2022-07-01 thomas return err;
1796 a5d43cac 2022-07-01 thomas }
1797 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1798 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1799 a5d43cac 2022-07-01 thomas if (err)
1800 a5d43cac 2022-07-01 thomas return err;
1801 a5d43cac 2022-07-01 thomas }
1802 1e37a5c2 2019-05-12 jcs }
1803 1e37a5c2 2019-05-12 jcs break;
1804 1e37a5c2 2019-05-12 jcs case 'q':
1805 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1806 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1807 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1808 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1809 a5d43cac 2022-07-01 thomas if (err)
1810 a5d43cac 2022-07-01 thomas break;
1811 a5d43cac 2022-07-01 thomas }
1812 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1813 a5d43cac 2022-07-01 thomas }
1814 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1815 9970f7fc 2020-12-03 stsp view->dying = 1;
1816 1e37a5c2 2019-05-12 jcs break;
1817 1e37a5c2 2019-05-12 jcs case 'Q':
1818 1e37a5c2 2019-05-12 jcs *done = 1;
1819 1e37a5c2 2019-05-12 jcs break;
1820 1c5e5faa 2022-06-23 thomas case 'F':
1821 07b0611c 2022-06-23 thomas view->count = 0;
1822 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1823 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1824 1e37a5c2 2019-05-12 jcs break;
1825 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1826 e78dc838 2020-12-04 stsp view->focussed = 0;
1827 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1828 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1829 53d2bdd3 2022-07-10 thomas } else {
1830 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1831 53d2bdd3 2022-07-10 thomas if (!err)
1832 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1833 53d2bdd3 2022-07-10 thomas }
1834 1e37a5c2 2019-05-12 jcs if (err)
1835 1e37a5c2 2019-05-12 jcs break;
1836 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1837 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1838 1e37a5c2 2019-05-12 jcs } else {
1839 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1840 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1841 e78dc838 2020-12-04 stsp view->focussed = 1;
1842 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1843 1e37a5c2 2019-05-12 jcs } else {
1844 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1845 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1846 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1847 53d2bdd3 2022-07-10 thomas if (!err)
1848 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1849 669b5ffa 2018-10-07 stsp }
1850 1e37a5c2 2019-05-12 jcs if (err)
1851 1e37a5c2 2019-05-12 jcs break;
1852 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1853 a5d43cac 2022-07-01 thomas }
1854 a5d43cac 2022-07-01 thomas if (err)
1855 a5d43cac 2022-07-01 thomas break;
1856 3a0139e8 2022-07-22 thomas if (view->resize) {
1857 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1858 a5d43cac 2022-07-01 thomas if (err)
1859 a5d43cac 2022-07-01 thomas break;
1860 1e37a5c2 2019-05-12 jcs }
1861 e0bcabc5 2023-04-28 thomas if (view->parent) {
1862 e0bcabc5 2023-04-28 thomas if (view->parent->resize) {
1863 e0bcabc5 2023-04-28 thomas err = view->parent->resize(view->parent, 0);
1864 e0bcabc5 2023-04-28 thomas if (err != NULL)
1865 e0bcabc5 2023-04-28 thomas break;
1866 e0bcabc5 2023-04-28 thomas }
1867 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1868 e0bcabc5 2023-04-28 thomas if (err != NULL)
1869 e0bcabc5 2023-04-28 thomas break;
1870 e0bcabc5 2023-04-28 thomas }
1871 e0bcabc5 2023-04-28 thomas err = offset_selection_down(view);
1872 1e37a5c2 2019-05-12 jcs break;
1873 64486692 2022-07-07 thomas case 'S':
1874 53d2bdd3 2022-07-10 thomas view->count = 0;
1875 64486692 2022-07-07 thomas err = switch_split(view);
1876 53d2bdd3 2022-07-10 thomas break;
1877 53d2bdd3 2022-07-10 thomas case '-':
1878 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1879 64486692 2022-07-07 thomas break;
1880 53d2bdd3 2022-07-10 thomas case '+':
1881 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1882 53d2bdd3 2022-07-10 thomas break;
1883 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1884 60493ae3 2019-06-20 stsp break;
1885 60493ae3 2019-06-20 stsp case '/':
1886 07b0611c 2022-06-23 thomas view->count = 0;
1887 60493ae3 2019-06-20 stsp if (view->search_start)
1888 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1889 60493ae3 2019-06-20 stsp else
1890 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1891 1e37a5c2 2019-05-12 jcs break;
1892 b1bf1435 2019-06-21 stsp case 'N':
1893 60493ae3 2019-06-20 stsp case 'n':
1894 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1895 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1896 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1897 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1898 60493ae3 2019-06-20 stsp view->search_next(view);
1899 60493ae3 2019-06-20 stsp } else
1900 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1901 adf4c9e0 2022-07-03 thomas break;
1902 adf4c9e0 2022-07-03 thomas case 'A':
1903 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1904 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1905 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1906 f2d06bef 2023-01-23 thomas } else {
1907 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1908 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1909 f2d06bef 2023-01-23 thomas }
1910 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1911 adf4c9e0 2022-07-03 thomas if (v->reset) {
1912 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1913 adf4c9e0 2022-07-03 thomas if (err)
1914 adf4c9e0 2022-07-03 thomas return err;
1915 adf4c9e0 2022-07-03 thomas }
1916 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1917 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1918 adf4c9e0 2022-07-03 thomas if (err)
1919 adf4c9e0 2022-07-03 thomas return err;
1920 adf4c9e0 2022-07-03 thomas }
1921 adf4c9e0 2022-07-03 thomas }
1922 60493ae3 2019-06-20 stsp break;
1923 b85a3496 2023-04-14 thomas case TOG_KEY_SCRDUMP:
1924 b85a3496 2023-04-14 thomas err = screendump(view);
1925 b85a3496 2023-04-14 thomas break;
1926 1e37a5c2 2019-05-12 jcs default:
1927 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1928 1e37a5c2 2019-05-12 jcs break;
1929 e5a0f69f 2018-08-18 stsp }
1930 e5a0f69f 2018-08-18 stsp
1931 e5a0f69f 2018-08-18 stsp return err;
1932 bcbd79e2 2018-08-19 stsp }
1933 bcbd79e2 2018-08-19 stsp
1934 ef20f542 2022-06-26 thomas static int
1935 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1936 a3404814 2018-09-02 stsp {
1937 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1938 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1939 669b5ffa 2018-10-07 stsp return 0;
1940 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1941 669b5ffa 2018-10-07 stsp return 0;
1942 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1943 a3404814 2018-09-02 stsp return 0;
1944 a3404814 2018-09-02 stsp
1945 669b5ffa 2018-10-07 stsp return view->focussed;
1946 a3404814 2018-09-02 stsp }
1947 a3404814 2018-09-02 stsp
1948 bcbd79e2 2018-08-19 stsp static const struct got_error *
1949 557d3365 2023-04-14 thomas tog_io_close(void)
1950 e5a0f69f 2018-08-18 stsp {
1951 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1952 b85a3496 2023-04-14 thomas
1953 557d3365 2023-04-14 thomas if (tog_io.cin && fclose(tog_io.cin) == EOF)
1954 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cin, GOT_ERR_IO);
1955 557d3365 2023-04-14 thomas if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1956 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cout, GOT_ERR_IO);
1957 557d3365 2023-04-14 thomas if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1958 557d3365 2023-04-14 thomas err = got_ferror(tog_io.f, GOT_ERR_IO);
1959 5b3a801d 2023-04-22 thomas if (tog_io.sdump && fclose(tog_io.sdump) == EOF && err == NULL)
1960 5b3a801d 2023-04-22 thomas err = got_ferror(tog_io.sdump, GOT_ERR_IO);
1961 b85a3496 2023-04-14 thomas
1962 b85a3496 2023-04-14 thomas return err;
1963 b85a3496 2023-04-14 thomas }
1964 b85a3496 2023-04-14 thomas
1965 b85a3496 2023-04-14 thomas static const struct got_error *
1966 557d3365 2023-04-14 thomas view_loop(struct tog_view *view)
1967 b85a3496 2023-04-14 thomas {
1968 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1969 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1970 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1971 64486692 2022-07-07 thomas char *mode;
1972 fd823528 2018-10-22 stsp int fast_refresh = 10;
1973 1a76625f 2018-10-22 stsp int done = 0, errcode;
1974 e5a0f69f 2018-08-18 stsp
1975 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1976 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1977 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1978 64486692 2022-07-07 thomas else
1979 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1980 64486692 2022-07-07 thomas
1981 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1982 1a76625f 2018-10-22 stsp if (errcode)
1983 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1984 1a76625f 2018-10-22 stsp
1985 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1986 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1987 e5a0f69f 2018-08-18 stsp
1988 1004088d 2018-09-29 stsp view->focussed = 1;
1989 878940b7 2018-09-29 stsp err = view->show(view);
1990 0cf4efb1 2018-09-29 stsp if (err)
1991 0cf4efb1 2018-09-29 stsp return err;
1992 0cf4efb1 2018-09-29 stsp update_panels();
1993 0cf4efb1 2018-09-29 stsp doupdate();
1994 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1995 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1996 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1997 557d3365 2023-04-14 thomas if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1998 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1999 fd823528 2018-10-22 stsp
2000 557d3365 2023-04-14 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
2001 e5a0f69f 2018-08-18 stsp if (err)
2002 e5a0f69f 2018-08-18 stsp break;
2003 6ec3d39c 2023-01-23 thomas
2004 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
2005 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
2006 6ec3d39c 2023-01-23 thomas done = 1;
2007 6ec3d39c 2023-01-23 thomas if (done) {
2008 6ec3d39c 2023-01-23 thomas struct tog_view *v;
2009 6ec3d39c 2023-01-23 thomas
2010 6ec3d39c 2023-01-23 thomas /*
2011 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
2012 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
2013 6ec3d39c 2023-01-23 thomas */
2014 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
2015 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
2016 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
2017 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
2018 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
2019 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
2020 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
2021 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
2022 6ec3d39c 2023-01-23 thomas }
2023 6ec3d39c 2023-01-23 thomas }
2024 6ec3d39c 2023-01-23 thomas doupdate();
2025 6ec3d39c 2023-01-23 thomas }
2026 6ec3d39c 2023-01-23 thomas
2027 9970f7fc 2020-12-03 stsp if (view->dying) {
2028 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2029 669b5ffa 2018-10-07 stsp
2030 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2031 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2032 9970f7fc 2020-12-03 stsp entry);
2033 e78dc838 2020-12-04 stsp else if (view->parent)
2034 669b5ffa 2018-10-07 stsp prev = view->parent;
2035 669b5ffa 2018-10-07 stsp
2036 e78dc838 2020-12-04 stsp if (view->parent) {
2037 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2038 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2039 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
2040 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
2041 40236d76 2022-06-23 thomas err = view_resize(view->parent);
2042 40236d76 2022-06-23 thomas if (err)
2043 40236d76 2022-06-23 thomas break;
2044 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
2045 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
2046 e78dc838 2020-12-04 stsp } else
2047 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2048 669b5ffa 2018-10-07 stsp
2049 9970f7fc 2020-12-03 stsp err = view_close(view);
2050 fb59748f 2020-12-05 stsp if (err)
2051 e5a0f69f 2018-08-18 stsp goto done;
2052 669b5ffa 2018-10-07 stsp
2053 e78dc838 2020-12-04 stsp view = NULL;
2054 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2055 e78dc838 2020-12-04 stsp if (v->focussed)
2056 e78dc838 2020-12-04 stsp break;
2057 0cf4efb1 2018-09-29 stsp }
2058 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2059 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2060 e78dc838 2020-12-04 stsp if (prev)
2061 e78dc838 2020-12-04 stsp view = prev;
2062 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2063 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2064 e78dc838 2020-12-04 stsp tog_view_list_head);
2065 e78dc838 2020-12-04 stsp }
2066 e78dc838 2020-12-04 stsp if (view) {
2067 e78dc838 2020-12-04 stsp if (view->focus_child) {
2068 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2069 e78dc838 2020-12-04 stsp view = view->child;
2070 e78dc838 2020-12-04 stsp } else
2071 e78dc838 2020-12-04 stsp view->focussed = 1;
2072 e78dc838 2020-12-04 stsp }
2073 e78dc838 2020-12-04 stsp }
2074 e5a0f69f 2018-08-18 stsp }
2075 bcbd79e2 2018-08-19 stsp if (new_view) {
2076 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2077 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2078 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2079 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2080 86c66b02 2018-10-18 stsp continue;
2081 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2082 86c66b02 2018-10-18 stsp err = view_close(v);
2083 86c66b02 2018-10-18 stsp if (err)
2084 86c66b02 2018-10-18 stsp goto done;
2085 86c66b02 2018-10-18 stsp break;
2086 86c66b02 2018-10-18 stsp }
2087 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2088 fed7eaa8 2018-10-24 stsp view = new_view;
2089 7cd52833 2022-06-23 thomas }
2090 6ec3d39c 2023-01-23 thomas if (view && !done) {
2091 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2092 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2093 e78dc838 2020-12-04 stsp view = view->child;
2094 e78dc838 2020-12-04 stsp } else {
2095 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2096 e78dc838 2020-12-04 stsp view = view->parent;
2097 1a76625f 2018-10-22 stsp }
2098 e78dc838 2020-12-04 stsp show_panel(view->panel);
2099 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2100 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2101 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2102 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2103 669b5ffa 2018-10-07 stsp if (err)
2104 1a76625f 2018-10-22 stsp goto done;
2105 669b5ffa 2018-10-07 stsp }
2106 669b5ffa 2018-10-07 stsp err = view->show(view);
2107 0cf4efb1 2018-09-29 stsp if (err)
2108 1a76625f 2018-10-22 stsp goto done;
2109 669b5ffa 2018-10-07 stsp if (view->child) {
2110 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2111 669b5ffa 2018-10-07 stsp if (err)
2112 1a76625f 2018-10-22 stsp goto done;
2113 669b5ffa 2018-10-07 stsp }
2114 1a76625f 2018-10-22 stsp update_panels();
2115 1a76625f 2018-10-22 stsp doupdate();
2116 0cf4efb1 2018-09-29 stsp }
2117 e5a0f69f 2018-08-18 stsp }
2118 e5a0f69f 2018-08-18 stsp done:
2119 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2120 f2d749db 2022-07-12 thomas const struct got_error *close_err;
2121 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2122 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2123 f2d749db 2022-07-12 thomas close_err = view_close(view);
2124 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
2125 f2d749db 2022-07-12 thomas err = close_err;
2126 e5a0f69f 2018-08-18 stsp }
2127 1a76625f 2018-10-22 stsp
2128 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2129 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2130 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2131 1a76625f 2018-10-22 stsp
2132 e5a0f69f 2018-08-18 stsp return err;
2133 ea5e7bb5 2018-08-01 stsp }
2134 ea5e7bb5 2018-08-01 stsp
2135 4ed7e80c 2018-05-20 stsp __dead static void
2136 9f7d7167 2018-04-29 stsp usage_log(void)
2137 9f7d7167 2018-04-29 stsp {
2138 80ddbec8 2018-04-29 stsp endwin();
2139 c70c5802 2018-08-01 stsp fprintf(stderr,
2140 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2141 9f7d7167 2018-04-29 stsp getprogname());
2142 9f7d7167 2018-04-29 stsp exit(1);
2143 80ddbec8 2018-04-29 stsp }
2144 80ddbec8 2018-04-29 stsp
2145 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2146 80ddbec8 2018-04-29 stsp static const struct got_error *
2147 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2148 963b370f 2018-05-20 stsp {
2149 00dfcb92 2018-06-11 stsp char *vis = NULL;
2150 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2151 963b370f 2018-05-20 stsp
2152 963b370f 2018-05-20 stsp *ws = NULL;
2153 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2154 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2155 00dfcb92 2018-06-11 stsp int vislen;
2156 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2157 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2158 00dfcb92 2018-06-11 stsp
2159 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2160 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2161 00dfcb92 2018-06-11 stsp if (err)
2162 00dfcb92 2018-06-11 stsp return err;
2163 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2164 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2165 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2166 a7f50699 2018-06-11 stsp goto done;
2167 a7f50699 2018-06-11 stsp }
2168 00dfcb92 2018-06-11 stsp }
2169 963b370f 2018-05-20 stsp
2170 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2171 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2172 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2173 a7f50699 2018-06-11 stsp goto done;
2174 a7f50699 2018-06-11 stsp }
2175 963b370f 2018-05-20 stsp
2176 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2177 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2178 a7f50699 2018-06-11 stsp done:
2179 00dfcb92 2018-06-11 stsp free(vis);
2180 963b370f 2018-05-20 stsp if (err) {
2181 963b370f 2018-05-20 stsp free(*ws);
2182 963b370f 2018-05-20 stsp *ws = NULL;
2183 963b370f 2018-05-20 stsp *wlen = 0;
2184 963b370f 2018-05-20 stsp }
2185 963b370f 2018-05-20 stsp return err;
2186 05171be4 2022-06-23 thomas }
2187 05171be4 2022-06-23 thomas
2188 05171be4 2022-06-23 thomas static const struct got_error *
2189 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
2190 05171be4 2022-06-23 thomas {
2191 05171be4 2022-06-23 thomas char *dst;
2192 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
2193 05171be4 2022-06-23 thomas
2194 05171be4 2022-06-23 thomas *ptr = NULL;
2195 05171be4 2022-06-23 thomas n = len = strlen(src);
2196 83316834 2022-06-23 thomas dst = malloc(n + 1);
2197 05171be4 2022-06-23 thomas if (dst == NULL)
2198 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
2199 05171be4 2022-06-23 thomas
2200 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
2201 05171be4 2022-06-23 thomas const char c = src[idx];
2202 05171be4 2022-06-23 thomas
2203 05171be4 2022-06-23 thomas if (c == '\t') {
2204 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
2205 b235ad5d 2022-06-23 thomas char *p;
2206 b235ad5d 2022-06-23 thomas
2207 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
2208 83316834 2022-06-23 thomas if (p == NULL) {
2209 83316834 2022-06-23 thomas free(dst);
2210 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
2211 83316834 2022-06-23 thomas
2212 83316834 2022-06-23 thomas }
2213 83316834 2022-06-23 thomas dst = p;
2214 05171be4 2022-06-23 thomas n += nb;
2215 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2216 05171be4 2022-06-23 thomas sz += nb;
2217 05171be4 2022-06-23 thomas } else
2218 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2219 05171be4 2022-06-23 thomas ++idx;
2220 05171be4 2022-06-23 thomas }
2221 05171be4 2022-06-23 thomas
2222 05171be4 2022-06-23 thomas dst[sz] = '\0';
2223 05171be4 2022-06-23 thomas *ptr = dst;
2224 05171be4 2022-06-23 thomas return NULL;
2225 963b370f 2018-05-20 stsp }
2226 963b370f 2018-05-20 stsp
2227 8d208d34 2022-06-23 thomas /*
2228 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2229 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2230 20181212 2023-06-01 thomas * Return the combined column width of all spanned wide characters in
2231 8d208d34 2022-06-23 thomas * *rcol.
2232 f91a2b48 2022-06-23 thomas */
2233 8d208d34 2022-06-23 thomas static int
2234 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2235 8d208d34 2022-06-23 thomas {
2236 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2237 f91a2b48 2022-06-23 thomas
2238 8d208d34 2022-06-23 thomas if (n == 0) {
2239 8d208d34 2022-06-23 thomas *rcol = cols;
2240 8d208d34 2022-06-23 thomas return off;
2241 8d208d34 2022-06-23 thomas }
2242 f91a2b48 2022-06-23 thomas
2243 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2244 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2245 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2246 8d208d34 2022-06-23 thomas else
2247 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2248 f91a2b48 2022-06-23 thomas
2249 8d208d34 2022-06-23 thomas if (width == -1) {
2250 8d208d34 2022-06-23 thomas width = 1;
2251 8d208d34 2022-06-23 thomas wline[i] = L'.';
2252 f91a2b48 2022-06-23 thomas }
2253 f91a2b48 2022-06-23 thomas
2254 8d208d34 2022-06-23 thomas if (cols + width > n)
2255 8d208d34 2022-06-23 thomas break;
2256 8d208d34 2022-06-23 thomas cols += width;
2257 f91a2b48 2022-06-23 thomas }
2258 f91a2b48 2022-06-23 thomas
2259 8d208d34 2022-06-23 thomas *rcol = cols;
2260 8d208d34 2022-06-23 thomas return i;
2261 f91a2b48 2022-06-23 thomas }
2262 f91a2b48 2022-06-23 thomas
2263 f91a2b48 2022-06-23 thomas /*
2264 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2265 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2266 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2267 f91a2b48 2022-06-23 thomas */
2268 f91a2b48 2022-06-23 thomas static const struct got_error *
2269 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2270 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2271 f91a2b48 2022-06-23 thomas {
2272 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2273 8d208d34 2022-06-23 thomas int cols;
2274 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2275 05171be4 2022-06-23 thomas char *exstr = NULL;
2276 963b370f 2018-05-20 stsp size_t wlen;
2277 8d208d34 2022-06-23 thomas int i, scrollx;
2278 963b370f 2018-05-20 stsp
2279 963b370f 2018-05-20 stsp *wlinep = NULL;
2280 b700b5d6 2018-07-10 stsp *widthp = 0;
2281 963b370f 2018-05-20 stsp
2282 05171be4 2022-06-23 thomas if (expand) {
2283 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2284 05171be4 2022-06-23 thomas if (err)
2285 05171be4 2022-06-23 thomas return err;
2286 05171be4 2022-06-23 thomas }
2287 05171be4 2022-06-23 thomas
2288 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2289 05171be4 2022-06-23 thomas free(exstr);
2290 963b370f 2018-05-20 stsp if (err)
2291 963b370f 2018-05-20 stsp return err;
2292 963b370f 2018-05-20 stsp
2293 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2294 f91a2b48 2022-06-23 thomas
2295 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2296 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2297 3f670bfb 2020-12-10 stsp wlen--;
2298 3f670bfb 2020-12-10 stsp }
2299 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2300 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2301 3f670bfb 2020-12-10 stsp wlen--;
2302 3f670bfb 2020-12-10 stsp }
2303 3f670bfb 2020-12-10 stsp
2304 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2305 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2306 27a741e5 2019-09-11 stsp
2307 b700b5d6 2018-07-10 stsp if (widthp)
2308 b700b5d6 2018-07-10 stsp *widthp = cols;
2309 f91a2b48 2022-06-23 thomas if (scrollxp)
2310 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2311 963b370f 2018-05-20 stsp if (err)
2312 963b370f 2018-05-20 stsp free(wline);
2313 963b370f 2018-05-20 stsp else
2314 963b370f 2018-05-20 stsp *wlinep = wline;
2315 963b370f 2018-05-20 stsp return err;
2316 963b370f 2018-05-20 stsp }
2317 963b370f 2018-05-20 stsp
2318 8b473291 2019-02-21 stsp static const struct got_error*
2319 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2320 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2321 8b473291 2019-02-21 stsp {
2322 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2323 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2324 8b473291 2019-02-21 stsp char *s;
2325 8b473291 2019-02-21 stsp const char *name;
2326 8b473291 2019-02-21 stsp
2327 8b473291 2019-02-21 stsp *refs_str = NULL;
2328 00ddec2f 2023-05-17 thomas
2329 00ddec2f 2023-05-17 thomas if (refs == NULL)
2330 00ddec2f 2023-05-17 thomas return NULL;
2331 8b473291 2019-02-21 stsp
2332 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2333 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2334 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2335 52b5abe1 2019-08-13 stsp int cmp;
2336 52b5abe1 2019-08-13 stsp
2337 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2338 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2339 8b473291 2019-02-21 stsp continue;
2340 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2341 8b473291 2019-02-21 stsp name += 5;
2342 0d095295 2023-06-01 thomas if (strncmp(name, "got/", 4) == 0)
2343 7143d404 2019-03-12 stsp continue;
2344 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2345 8b473291 2019-02-21 stsp name += 6;
2346 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2347 8b473291 2019-02-21 stsp name += 8;
2348 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2349 081e3dc2 2023-06-15 thomas if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
2350 79cc719f 2020-04-24 stsp continue;
2351 79cc719f 2020-04-24 stsp }
2352 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2353 48cae60d 2020-09-22 stsp if (err)
2354 48cae60d 2020-09-22 stsp break;
2355 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2356 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2357 5d844a1e 2019-08-13 stsp if (err) {
2358 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2359 48cae60d 2020-09-22 stsp free(ref_id);
2360 5d844a1e 2019-08-13 stsp break;
2361 48cae60d 2020-09-22 stsp }
2362 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2363 5d844a1e 2019-08-13 stsp err = NULL;
2364 5d844a1e 2019-08-13 stsp tag = NULL;
2365 5d844a1e 2019-08-13 stsp }
2366 52b5abe1 2019-08-13 stsp }
2367 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2368 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2369 48cae60d 2020-09-22 stsp free(ref_id);
2370 52b5abe1 2019-08-13 stsp if (tag)
2371 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2372 52b5abe1 2019-08-13 stsp if (cmp != 0)
2373 52b5abe1 2019-08-13 stsp continue;
2374 8b473291 2019-02-21 stsp s = *refs_str;
2375 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2376 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2377 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2378 8b473291 2019-02-21 stsp free(s);
2379 8b473291 2019-02-21 stsp *refs_str = NULL;
2380 8b473291 2019-02-21 stsp break;
2381 8b473291 2019-02-21 stsp }
2382 8b473291 2019-02-21 stsp free(s);
2383 8b473291 2019-02-21 stsp }
2384 8b473291 2019-02-21 stsp
2385 8b473291 2019-02-21 stsp return err;
2386 8b473291 2019-02-21 stsp }
2387 8b473291 2019-02-21 stsp
2388 963b370f 2018-05-20 stsp static const struct got_error *
2389 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2390 27a741e5 2019-09-11 stsp int col_tab_align)
2391 5813d178 2019-03-09 stsp {
2392 e6b8b890 2020-12-29 naddy char *smallerthan;
2393 5813d178 2019-03-09 stsp
2394 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2395 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2396 5813d178 2019-03-09 stsp author = smallerthan + 1;
2397 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2398 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2399 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2400 5813d178 2019-03-09 stsp }
2401 5813d178 2019-03-09 stsp
2402 5813d178 2019-03-09 stsp static const struct got_error *
2403 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2404 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2405 8fdc79fe 2020-12-01 naddy int author_display_cols)
2406 80ddbec8 2018-04-29 stsp {
2407 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2408 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2409 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2410 61dc16bb 2023-05-17 thomas char *refs_str = NULL;
2411 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2412 5813d178 2019-03-09 stsp char *author = NULL;
2413 cabb4cfd 2023-06-01 thomas wchar_t *wrefstr = NULL, *wlogmsg = NULL, *wauthor = NULL;
2414 cabb4cfd 2023-06-01 thomas int author_width, refstr_width, logmsg_width;
2415 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2416 cabb4cfd 2023-06-01 thomas int col, limit, scrollx, logmsg_x;
2417 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2418 ccb26ccd 2018-11-05 stsp struct tm tm;
2419 45d799e2 2018-12-23 stsp time_t committer_time;
2420 11b20872 2019-11-08 stsp struct tog_color *tc;
2421 9472af95 2023-05-15 thomas struct got_reflist_head *refs;
2422 80ddbec8 2018-04-29 stsp
2423 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2424 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2425 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2426 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2427 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2428 b39d25c7 2018-07-10 stsp
2429 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2430 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2431 b39d25c7 2018-07-10 stsp else
2432 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2433 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2434 11b20872 2019-11-08 stsp if (tc)
2435 11b20872 2019-11-08 stsp wattr_on(view->window,
2436 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2437 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2438 11b20872 2019-11-08 stsp if (tc)
2439 11b20872 2019-11-08 stsp wattr_off(view->window,
2440 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2441 27a741e5 2019-09-11 stsp col = limit;
2442 b39d25c7 2018-07-10 stsp if (col > avail)
2443 b39d25c7 2018-07-10 stsp goto done;
2444 6570a66d 2019-11-08 stsp
2445 6570a66d 2019-11-08 stsp if (avail >= 120) {
2446 6570a66d 2019-11-08 stsp char *id_str;
2447 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2448 6570a66d 2019-11-08 stsp if (err)
2449 6570a66d 2019-11-08 stsp goto done;
2450 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2451 11b20872 2019-11-08 stsp if (tc)
2452 11b20872 2019-11-08 stsp wattr_on(view->window,
2453 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2454 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2455 11b20872 2019-11-08 stsp if (tc)
2456 11b20872 2019-11-08 stsp wattr_off(view->window,
2457 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2458 6570a66d 2019-11-08 stsp free(id_str);
2459 6570a66d 2019-11-08 stsp col += 9;
2460 6570a66d 2019-11-08 stsp if (col > avail)
2461 6570a66d 2019-11-08 stsp goto done;
2462 6570a66d 2019-11-08 stsp }
2463 b39d25c7 2018-07-10 stsp
2464 f69c5a46 2022-07-19 thomas if (s->use_committer)
2465 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2466 f69c5a46 2022-07-19 thomas else
2467 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2468 5813d178 2019-03-09 stsp if (author == NULL) {
2469 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2470 80ddbec8 2018-04-29 stsp goto done;
2471 80ddbec8 2018-04-29 stsp }
2472 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2473 bb737323 2018-05-20 stsp if (err)
2474 bb737323 2018-05-20 stsp goto done;
2475 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2476 11b20872 2019-11-08 stsp if (tc)
2477 11b20872 2019-11-08 stsp wattr_on(view->window,
2478 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2479 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2480 bb737323 2018-05-20 stsp col += author_width;
2481 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2482 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2483 bb737323 2018-05-20 stsp col++;
2484 bb737323 2018-05-20 stsp author_width++;
2485 bb737323 2018-05-20 stsp }
2486 f31d6c3b 2022-09-09 thomas if (tc)
2487 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2488 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2489 9c2eaf34 2018-05-20 stsp if (col > avail)
2490 9c2eaf34 2018-05-20 stsp goto done;
2491 80ddbec8 2018-04-29 stsp
2492 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2493 5943eee2 2019-08-13 stsp if (err)
2494 6d9fbc00 2018-04-29 stsp goto done;
2495 bb737323 2018-05-20 stsp logmsg = logmsg0;
2496 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2497 bb737323 2018-05-20 stsp logmsg++;
2498 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2499 bb737323 2018-05-20 stsp if (newline)
2500 bb737323 2018-05-20 stsp *newline = '\0';
2501 9472af95 2023-05-15 thomas
2502 cabb4cfd 2023-06-01 thomas limit = avail - col;
2503 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2504 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2505 cabb4cfd 2023-06-01 thomas
2506 9472af95 2023-05-15 thomas /* Prepend reference labels to log message if possible .*/
2507 9472af95 2023-05-15 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id);
2508 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, id, s->repo);
2509 61dc16bb 2023-05-17 thomas if (err)
2510 61dc16bb 2023-05-17 thomas goto done;
2511 61dc16bb 2023-05-17 thomas if (refs_str) {
2512 cabb4cfd 2023-06-01 thomas char *rs;
2513 fcfb26c3 2023-05-26 thomas
2514 cabb4cfd 2023-06-01 thomas if (asprintf(&rs, "[%s]", refs_str) == -1) {
2515 9472af95 2023-05-15 thomas err = got_error_from_errno("asprintf");
2516 9472af95 2023-05-15 thomas goto done;
2517 9472af95 2023-05-15 thomas }
2518 cabb4cfd 2023-06-01 thomas err = format_line(&wrefstr, &refstr_width,
2519 cabb4cfd 2023-06-01 thomas &scrollx, rs, view->x, limit, col, 1);
2520 cabb4cfd 2023-06-01 thomas free(rs);
2521 cabb4cfd 2023-06-01 thomas if (err)
2522 cabb4cfd 2023-06-01 thomas goto done;
2523 9472af95 2023-05-15 thomas tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2524 9472af95 2023-05-15 thomas if (tc)
2525 9472af95 2023-05-15 thomas wattr_on(view->window,
2526 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2527 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wrefstr[scrollx]);
2528 9472af95 2023-05-15 thomas if (tc)
2529 9472af95 2023-05-15 thomas wattr_off(view->window,
2530 9472af95 2023-05-15 thomas COLOR_PAIR(tc->colorpair), NULL);
2531 cabb4cfd 2023-06-01 thomas col += MAX(refstr_width, 0);
2532 cabb4cfd 2023-06-01 thomas if (col > avail)
2533 cabb4cfd 2023-06-01 thomas goto done;
2534 cabb4cfd 2023-06-01 thomas
2535 cabb4cfd 2023-06-01 thomas if (col < avail) {
2536 cabb4cfd 2023-06-01 thomas waddch(view->window, ' ');
2537 cabb4cfd 2023-06-01 thomas col++;
2538 cabb4cfd 2023-06-01 thomas }
2539 cabb4cfd 2023-06-01 thomas
2540 cabb4cfd 2023-06-01 thomas if (refstr_width > 0)
2541 cabb4cfd 2023-06-01 thomas logmsg_x = 0;
2542 cabb4cfd 2023-06-01 thomas else {
2543 cabb4cfd 2023-06-01 thomas int unscrolled_refstr_width;
2544 cabb4cfd 2023-06-01 thomas size_t len = wcslen(wrefstr);
2545 cabb4cfd 2023-06-01 thomas
2546 cabb4cfd 2023-06-01 thomas /*
2547 cabb4cfd 2023-06-01 thomas * No need to check for -1 return value here since
2548 cabb4cfd 2023-06-01 thomas * unprintables have been replaced by span_wline().
2549 cabb4cfd 2023-06-01 thomas */
2550 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width = wcswidth(wrefstr, len);
2551 cabb4cfd 2023-06-01 thomas unscrolled_refstr_width += 1; /* trailing space */
2552 cabb4cfd 2023-06-01 thomas logmsg_x = view->x - unscrolled_refstr_width;
2553 cabb4cfd 2023-06-01 thomas }
2554 cabb4cfd 2023-06-01 thomas
2555 cabb4cfd 2023-06-01 thomas limit = avail - col;
2556 cabb4cfd 2023-06-01 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2557 cabb4cfd 2023-06-01 thomas limit--; /* for the border */
2558 9472af95 2023-05-15 thomas } else
2559 cabb4cfd 2023-06-01 thomas logmsg_x = view->x;
2560 cabb4cfd 2023-06-01 thomas
2561 cabb4cfd 2023-06-01 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, logmsg_x,
2562 cabb4cfd 2023-06-01 thomas limit, col, 1);
2563 cabb4cfd 2023-06-01 thomas if (err)
2564 cabb4cfd 2023-06-01 thomas goto done;
2565 cabb4cfd 2023-06-01 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2566 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2567 27a741e5 2019-09-11 stsp while (col < avail) {
2568 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2569 bb737323 2018-05-20 stsp col++;
2570 881b2d3e 2018-04-30 stsp }
2571 80ddbec8 2018-04-29 stsp done:
2572 80ddbec8 2018-04-29 stsp free(logmsg0);
2573 bb737323 2018-05-20 stsp free(wlogmsg);
2574 cabb4cfd 2023-06-01 thomas free(wrefstr);
2575 61dc16bb 2023-05-17 thomas free(refs_str);
2576 5813d178 2019-03-09 stsp free(author);
2577 bb737323 2018-05-20 stsp free(wauthor);
2578 80ddbec8 2018-04-29 stsp free(line);
2579 80ddbec8 2018-04-29 stsp return err;
2580 80ddbec8 2018-04-29 stsp }
2581 26ed57b2 2018-05-19 stsp
2582 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2583 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2584 899d86c2 2018-05-10 stsp struct got_object_id *id)
2585 80ddbec8 2018-04-29 stsp {
2586 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2587 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2588 80ddbec8 2018-04-29 stsp
2589 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2590 80ddbec8 2018-04-29 stsp if (entry == NULL)
2591 899d86c2 2018-05-10 stsp return NULL;
2592 99db9666 2018-05-07 stsp
2593 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2594 d68b9737 2022-09-05 thomas if (dup == NULL) {
2595 d68b9737 2022-09-05 thomas free(entry);
2596 d68b9737 2022-09-05 thomas return NULL;
2597 d68b9737 2022-09-05 thomas }
2598 d68b9737 2022-09-05 thomas
2599 d68b9737 2022-09-05 thomas entry->id = dup;
2600 99db9666 2018-05-07 stsp entry->commit = commit;
2601 899d86c2 2018-05-10 stsp return entry;
2602 99db9666 2018-05-07 stsp }
2603 80ddbec8 2018-04-29 stsp
2604 99db9666 2018-05-07 stsp static void
2605 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2606 99db9666 2018-05-07 stsp {
2607 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2608 99db9666 2018-05-07 stsp
2609 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2610 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2611 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2612 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2613 d68b9737 2022-09-05 thomas free(entry->id);
2614 99db9666 2018-05-07 stsp free(entry);
2615 99db9666 2018-05-07 stsp }
2616 99db9666 2018-05-07 stsp
2617 99db9666 2018-05-07 stsp static void
2618 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2619 99db9666 2018-05-07 stsp {
2620 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2621 99db9666 2018-05-07 stsp pop_commit(commits);
2622 c4972b91 2018-05-07 stsp }
2623 c4972b91 2018-05-07 stsp
2624 c4972b91 2018-05-07 stsp static const struct got_error *
2625 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2626 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2627 13add988 2019-10-15 stsp {
2628 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2629 13add988 2019-10-15 stsp regmatch_t regmatch;
2630 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2631 13add988 2019-10-15 stsp
2632 13add988 2019-10-15 stsp *have_match = 0;
2633 13add988 2019-10-15 stsp
2634 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2635 13add988 2019-10-15 stsp if (err)
2636 13add988 2019-10-15 stsp return err;
2637 13add988 2019-10-15 stsp
2638 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2639 13add988 2019-10-15 stsp if (err)
2640 13add988 2019-10-15 stsp goto done;
2641 13add988 2019-10-15 stsp
2642 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2643 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2644 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2645 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2646 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2647 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2648 13add988 2019-10-15 stsp *have_match = 1;
2649 13add988 2019-10-15 stsp done:
2650 13add988 2019-10-15 stsp free(id_str);
2651 13add988 2019-10-15 stsp free(logmsg);
2652 13add988 2019-10-15 stsp return err;
2653 13add988 2019-10-15 stsp }
2654 13add988 2019-10-15 stsp
2655 13add988 2019-10-15 stsp static const struct got_error *
2656 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2657 c4972b91 2018-05-07 stsp {
2658 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2659 9ba79e04 2018-06-11 stsp
2660 1a76625f 2018-10-22 stsp /*
2661 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2662 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2663 1a76625f 2018-10-22 stsp * while updating the display.
2664 1a76625f 2018-10-22 stsp */
2665 4e0d2870 2020-12-07 naddy do {
2666 7210b715 2022-09-11 thomas struct got_object_id id;
2667 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2668 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2669 7e8004ba 2022-09-11 thomas int limit_match = 0;
2670 1a76625f 2018-10-22 stsp int errcode;
2671 899d86c2 2018-05-10 stsp
2672 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2673 4e0d2870 2020-12-07 naddy NULL, NULL);
2674 7210b715 2022-09-11 thomas if (err)
2675 ecb28ae0 2018-07-16 stsp break;
2676 899d86c2 2018-05-10 stsp
2677 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2678 9ba79e04 2018-06-11 stsp if (err)
2679 9ba79e04 2018-06-11 stsp break;
2680 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2681 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2682 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2683 9ba79e04 2018-06-11 stsp break;
2684 9ba79e04 2018-06-11 stsp }
2685 93e45b7c 2018-09-24 stsp
2686 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2687 1a76625f 2018-10-22 stsp if (errcode) {
2688 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2689 13add988 2019-10-15 stsp "pthread_mutex_lock");
2690 1a76625f 2018-10-22 stsp break;
2691 1a76625f 2018-10-22 stsp }
2692 1a76625f 2018-10-22 stsp
2693 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2694 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2695 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2696 1a76625f 2018-10-22 stsp
2697 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2698 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2699 7e8004ba 2022-09-11 thomas a->limit_regex);
2700 7e8004ba 2022-09-11 thomas if (err)
2701 7e8004ba 2022-09-11 thomas break;
2702 7e8004ba 2022-09-11 thomas
2703 7e8004ba 2022-09-11 thomas if (limit_match) {
2704 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2705 7e8004ba 2022-09-11 thomas
2706 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2707 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2708 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2709 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2710 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2711 7e8004ba 2022-09-11 thomas break;
2712 7e8004ba 2022-09-11 thomas }
2713 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2714 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2715 7e8004ba 2022-09-11 thomas
2716 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2717 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2718 7e8004ba 2022-09-11 thomas matched, entry);
2719 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2720 7e8004ba 2022-09-11 thomas }
2721 7e8004ba 2022-09-11 thomas
2722 7e8004ba 2022-09-11 thomas /*
2723 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2724 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2725 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2726 7e8004ba 2022-09-11 thomas */
2727 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2728 7e8004ba 2022-09-11 thomas }
2729 7e8004ba 2022-09-11 thomas
2730 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2731 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2732 7c1452c1 2020-03-26 stsp int have_match;
2733 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2734 7c1452c1 2020-03-26 stsp if (err)
2735 7c1452c1 2020-03-26 stsp break;
2736 7e8004ba 2022-09-11 thomas
2737 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2738 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2739 7e8004ba 2022-09-11 thomas *a->search_next_done =
2740 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2741 7e8004ba 2022-09-11 thomas } else if (have_match)
2742 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2743 13add988 2019-10-15 stsp }
2744 13add988 2019-10-15 stsp
2745 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2746 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2747 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2748 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2749 7c1452c1 2020-03-26 stsp if (err)
2750 13add988 2019-10-15 stsp break;
2751 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2752 899d86c2 2018-05-10 stsp
2753 9ba79e04 2018-06-11 stsp return err;
2754 0553a4e3 2018-04-30 stsp }
2755 0553a4e3 2018-04-30 stsp
2756 2b779855 2020-12-05 naddy static void
2757 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2758 2b779855 2020-12-05 naddy {
2759 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2760 2b779855 2020-12-05 naddy int ncommits = 0;
2761 2b779855 2020-12-05 naddy
2762 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2763 2b779855 2020-12-05 naddy while (entry) {
2764 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2765 2b779855 2020-12-05 naddy s->selected_entry = entry;
2766 2b779855 2020-12-05 naddy break;
2767 2b779855 2020-12-05 naddy }
2768 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2769 2b779855 2020-12-05 naddy ncommits++;
2770 2b779855 2020-12-05 naddy }
2771 2b779855 2020-12-05 naddy }
2772 2b779855 2020-12-05 naddy
2773 0553a4e3 2018-04-30 stsp static const struct got_error *
2774 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2775 0553a4e3 2018-04-30 stsp {
2776 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2777 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2778 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2779 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2780 60493ae3 2019-06-20 stsp int width;
2781 cabb4cfd 2023-06-01 thomas int ncommits, author_cols = 4, refstr_cols;
2782 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2783 8b473291 2019-02-21 stsp char *refs_str = NULL;
2784 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2785 11b20872 2019-11-08 stsp struct tog_color *tc;
2786 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2787 cabb4cfd 2023-06-01 thomas struct got_reflist_head *refs;
2788 bd3f8225 2022-08-12 thomas
2789 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2790 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2791 0553a4e3 2018-04-30 stsp
2792 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2793 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2794 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2795 1a76625f 2018-10-22 stsp if (err)
2796 ecb28ae0 2018-07-16 stsp return err;
2797 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2798 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2799 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, s->selected_entry->id,
2800 00ddec2f 2023-05-17 thomas s->repo);
2801 00ddec2f 2023-05-17 thomas if (err)
2802 00ddec2f 2023-05-17 thomas goto done;
2803 867c6645 2018-07-10 stsp }
2804 359bfafd 2019-02-22 stsp
2805 557d3365 2023-04-14 thomas if (s->thread_args.commits_needed == 0 && !using_mock_io)
2806 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2807 1a76625f 2018-10-22 stsp
2808 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2809 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2810 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2811 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2812 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2813 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2814 8f4ed634 2020-03-26 stsp goto done;
2815 8f4ed634 2020-03-26 stsp }
2816 8f4ed634 2020-03-26 stsp } else {
2817 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2818 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2819 f9686aa5 2020-03-27 stsp
2820 f9686aa5 2020-03-27 stsp if (view->searching) {
2821 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2822 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2823 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2824 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2825 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2826 f9686aa5 2020-03-27 stsp search_str = "searching...";
2827 f9686aa5 2020-03-27 stsp }
2828 f9686aa5 2020-03-27 stsp
2829 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2830 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2831 7e8004ba 2022-09-11 thomas
2832 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2833 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2834 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2835 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2836 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2837 8f4ed634 2020-03-26 stsp goto done;
2838 8f4ed634 2020-03-26 stsp }
2839 8b473291 2019-02-21 stsp }
2840 1a76625f 2018-10-22 stsp
2841 00580e07 2023-06-01 thomas free(refs_str);
2842 00580e07 2023-06-01 thomas refs_str = NULL;
2843 00580e07 2023-06-01 thomas
2844 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2845 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2846 91198554 2022-06-23 thomas "........................................",
2847 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2848 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2849 1a76625f 2018-10-22 stsp header = NULL;
2850 1a76625f 2018-10-22 stsp goto done;
2851 1a76625f 2018-10-22 stsp }
2852 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2853 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2854 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2855 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2856 1a76625f 2018-10-22 stsp header = NULL;
2857 1a76625f 2018-10-22 stsp goto done;
2858 ecb28ae0 2018-07-16 stsp }
2859 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2860 1a76625f 2018-10-22 stsp if (err)
2861 1a76625f 2018-10-22 stsp goto done;
2862 867c6645 2018-07-10 stsp
2863 2814baeb 2018-08-01 stsp werase(view->window);
2864 867c6645 2018-07-10 stsp
2865 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2866 a3404814 2018-09-02 stsp wstandout(view->window);
2867 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2868 11b20872 2019-11-08 stsp if (tc)
2869 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2870 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2871 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2872 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2873 1a76625f 2018-10-22 stsp width++;
2874 1a76625f 2018-10-22 stsp }
2875 86f4aab9 2022-09-09 thomas if (tc)
2876 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2877 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2878 a3404814 2018-09-02 stsp wstandend(view->window);
2879 ecb28ae0 2018-07-16 stsp free(wline);
2880 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2881 1a76625f 2018-10-22 stsp goto done;
2882 0553a4e3 2018-04-30 stsp
2883 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2884 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2885 5813d178 2019-03-09 stsp ncommits = 0;
2886 05171be4 2022-06-23 thomas view->maxx = 0;
2887 5813d178 2019-03-09 stsp while (entry) {
2888 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2889 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2890 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2891 5813d178 2019-03-09 stsp int width;
2892 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2893 5813d178 2019-03-09 stsp break;
2894 f69c5a46 2022-07-19 thomas if (s->use_committer)
2895 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2896 f69c5a46 2022-07-19 thomas else
2897 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2898 5813d178 2019-03-09 stsp if (author == NULL) {
2899 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2900 5813d178 2019-03-09 stsp goto done;
2901 5813d178 2019-03-09 stsp }
2902 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2903 27a741e5 2019-09-11 stsp date_display_cols);
2904 5813d178 2019-03-09 stsp if (author_cols < width)
2905 5813d178 2019-03-09 stsp author_cols = width;
2906 5813d178 2019-03-09 stsp free(wauthor);
2907 5813d178 2019-03-09 stsp free(author);
2908 ef944b8b 2022-07-21 thomas if (err)
2909 ef944b8b 2022-07-21 thomas goto done;
2910 cabb4cfd 2023-06-01 thomas refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2911 cabb4cfd 2023-06-01 thomas entry->id);
2912 cabb4cfd 2023-06-01 thomas err = build_refs_str(&refs_str, refs, entry->id, s->repo);
2913 cabb4cfd 2023-06-01 thomas if (err)
2914 cabb4cfd 2023-06-01 thomas goto done;
2915 cabb4cfd 2023-06-01 thomas if (refs_str) {
2916 cabb4cfd 2023-06-01 thomas wchar_t *ws;
2917 cabb4cfd 2023-06-01 thomas err = format_line(&ws, &width, NULL, refs_str,
2918 cabb4cfd 2023-06-01 thomas 0, INT_MAX, date_display_cols + author_cols, 0);
2919 cabb4cfd 2023-06-01 thomas free(ws);
2920 00580e07 2023-06-01 thomas free(refs_str);
2921 00580e07 2023-06-01 thomas refs_str = NULL;
2922 cabb4cfd 2023-06-01 thomas if (err)
2923 cabb4cfd 2023-06-01 thomas goto done;
2924 cabb4cfd 2023-06-01 thomas refstr_cols = width + 3; /* account for [ ] + space */
2925 cabb4cfd 2023-06-01 thomas } else
2926 cabb4cfd 2023-06-01 thomas refstr_cols = 0;
2927 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2928 05171be4 2022-06-23 thomas if (err)
2929 05171be4 2022-06-23 thomas goto done;
2930 05171be4 2022-06-23 thomas msg = msg0;
2931 05171be4 2022-06-23 thomas while (*msg == '\n')
2932 05171be4 2022-06-23 thomas ++msg;
2933 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2934 331b1a16 2022-06-23 thomas *eol = '\0';
2935 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2936 cabb4cfd 2023-06-01 thomas date_display_cols + author_cols + refstr_cols, 0);
2937 331b1a16 2022-06-23 thomas if (err)
2938 331b1a16 2022-06-23 thomas goto done;
2939 cabb4cfd 2023-06-01 thomas view->maxx = MAX(view->maxx, width + refstr_cols);
2940 05171be4 2022-06-23 thomas free(msg0);
2941 331b1a16 2022-06-23 thomas free(wmsg);
2942 7ca04879 2019-10-19 stsp ncommits++;
2943 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2944 5813d178 2019-03-09 stsp }
2945 5813d178 2019-03-09 stsp
2946 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2947 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2948 867c6645 2018-07-10 stsp ncommits = 0;
2949 899d86c2 2018-05-10 stsp while (entry) {
2950 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2951 899d86c2 2018-05-10 stsp break;
2952 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2953 2814baeb 2018-08-01 stsp wstandout(view->window);
2954 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2955 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2956 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2957 2814baeb 2018-08-01 stsp wstandend(view->window);
2958 0553a4e3 2018-04-30 stsp if (err)
2959 60493ae3 2019-06-20 stsp goto done;
2960 0553a4e3 2018-04-30 stsp ncommits++;
2961 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2962 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2963 80ddbec8 2018-04-29 stsp }
2964 80ddbec8 2018-04-29 stsp
2965 a5d43cac 2022-07-01 thomas view_border(view);
2966 1a76625f 2018-10-22 stsp done:
2967 1a76625f 2018-10-22 stsp free(id_str);
2968 8b473291 2019-02-21 stsp free(refs_str);
2969 1a76625f 2018-10-22 stsp free(ncommits_str);
2970 1a76625f 2018-10-22 stsp free(header);
2971 80ddbec8 2018-04-29 stsp return err;
2972 9f7d7167 2018-04-29 stsp }
2973 07b55e75 2018-05-10 stsp
2974 07b55e75 2018-05-10 stsp static void
2975 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2976 07b55e75 2018-05-10 stsp {
2977 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2978 07b55e75 2018-05-10 stsp int nscrolled = 0;
2979 07b55e75 2018-05-10 stsp
2980 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2981 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2982 07b55e75 2018-05-10 stsp return;
2983 9f7d7167 2018-04-29 stsp
2984 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2985 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2986 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2987 07b55e75 2018-05-10 stsp if (entry) {
2988 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2989 07b55e75 2018-05-10 stsp nscrolled++;
2990 07b55e75 2018-05-10 stsp }
2991 07b55e75 2018-05-10 stsp }
2992 aa075928 2018-05-10 stsp }
2993 aa075928 2018-05-10 stsp
2994 aa075928 2018-05-10 stsp static const struct got_error *
2995 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2996 aa075928 2018-05-10 stsp {
2997 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2998 5e224a3e 2019-02-22 stsp int errcode;
2999 8a42fca8 2019-02-22 stsp
3000 557d3365 2023-04-14 thomas if (!using_mock_io)
3001 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while loading commits */
3002 aa075928 2018-05-10 stsp
3003 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
3004 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
3005 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
3006 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
3007 7aafa0d1 2019-02-22 stsp if (errcode)
3008 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3009 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3010 7c1452c1 2020-03-26 stsp
3011 7c1452c1 2020-03-26 stsp /*
3012 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
3013 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
3014 7c1452c1 2020-03-26 stsp */
3015 7c1452c1 2020-03-26 stsp if (!wait)
3016 7c1452c1 2020-03-26 stsp break;
3017 7c1452c1 2020-03-26 stsp
3018 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3019 ffe38506 2020-12-01 naddy show_log_view(view);
3020 7c1452c1 2020-03-26 stsp update_panels();
3021 7c1452c1 2020-03-26 stsp doupdate();
3022 7c1452c1 2020-03-26 stsp
3023 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
3024 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
3025 82954512 2020-02-03 stsp if (errcode)
3026 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
3027 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3028 82954512 2020-02-03 stsp
3029 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
3030 ffe38506 2020-12-01 naddy show_log_view(view);
3031 7c1452c1 2020-03-26 stsp update_panels();
3032 7c1452c1 2020-03-26 stsp doupdate();
3033 5e224a3e 2019-02-22 stsp }
3034 5e224a3e 2019-02-22 stsp
3035 5e224a3e 2019-02-22 stsp return NULL;
3036 a5d43cac 2022-07-01 thomas }
3037 a5d43cac 2022-07-01 thomas
3038 a5d43cac 2022-07-01 thomas static const struct got_error *
3039 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
3040 a5d43cac 2022-07-01 thomas {
3041 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
3042 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3043 fe731b51 2022-07-14 thomas
3044 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
3045 fe731b51 2022-07-14 thomas return NULL;
3046 a5d43cac 2022-07-01 thomas
3047 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
3048 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
3049 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3050 a5d43cac 2022-07-01 thomas
3051 a5d43cac 2022-07-01 thomas return err;
3052 5e224a3e 2019-02-22 stsp }
3053 5e224a3e 2019-02-22 stsp
3054 5e224a3e 2019-02-22 stsp static const struct got_error *
3055 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
3056 5e224a3e 2019-02-22 stsp {
3057 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
3058 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
3059 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
3060 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
3061 5e224a3e 2019-02-22 stsp
3062 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
3063 5e224a3e 2019-02-22 stsp return NULL;
3064 5e224a3e 2019-02-22 stsp
3065 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
3066 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
3067 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
3068 08ebd0a9 2019-02-22 stsp /*
3069 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
3070 08ebd0a9 2019-02-22 stsp */
3071 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
3072 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
3073 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3074 5e224a3e 2019-02-22 stsp if (err)
3075 5e224a3e 2019-02-22 stsp return err;
3076 7aafa0d1 2019-02-22 stsp }
3077 b295e71b 2019-02-22 stsp
3078 7aafa0d1 2019-02-22 stsp do {
3079 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
3080 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
3081 88048b54 2019-02-21 stsp break;
3082 88048b54 2019-02-21 stsp
3083 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
3084 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
3085 aa075928 2018-05-10 stsp
3086 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
3087 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
3088 dd0a52c1 2018-05-20 stsp break;
3089 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
3090 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
3091 aa075928 2018-05-10 stsp
3092 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
3093 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
3094 a5d43cac 2022-07-01 thomas else
3095 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
3096 a5d43cac 2022-07-01 thomas
3097 dd0a52c1 2018-05-20 stsp return err;
3098 07b55e75 2018-05-10 stsp }
3099 4a7f7875 2018-05-10 stsp
3100 cd0acaa7 2018-05-20 stsp static const struct got_error *
3101 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
3102 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
3103 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3104 cd0acaa7 2018-05-20 stsp {
3105 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3106 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3107 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3108 cd0acaa7 2018-05-20 stsp
3109 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3110 15a94983 2018-12-23 stsp if (diff_view == NULL)
3111 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3112 ea5e7bb5 2018-08-01 stsp
3113 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3114 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3115 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3116 e5a0f69f 2018-08-18 stsp if (err == NULL)
3117 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3118 cd0acaa7 2018-05-20 stsp return err;
3119 4a7f7875 2018-05-10 stsp }
3120 4a7f7875 2018-05-10 stsp
3121 80ddbec8 2018-04-29 stsp static const struct got_error *
3122 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3123 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3124 9343a5fb 2018-06-23 stsp {
3125 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3126 941e9f74 2019-05-21 stsp
3127 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3128 941e9f74 2019-05-21 stsp if (parent == NULL)
3129 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3130 941e9f74 2019-05-21 stsp
3131 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3132 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3133 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3134 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3135 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3136 941e9f74 2019-05-21 stsp s->tree = subtree;
3137 941e9f74 2019-05-21 stsp s->selected = 0;
3138 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3139 941e9f74 2019-05-21 stsp return NULL;
3140 941e9f74 2019-05-21 stsp }
3141 941e9f74 2019-05-21 stsp
3142 941e9f74 2019-05-21 stsp static const struct got_error *
3143 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3144 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
3145 941e9f74 2019-05-21 stsp {
3146 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3147 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3148 941e9f74 2019-05-21 stsp const char *p;
3149 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3150 9343a5fb 2018-06-23 stsp
3151 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3152 941e9f74 2019-05-21 stsp p = path;
3153 941e9f74 2019-05-21 stsp while (*p) {
3154 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3155 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3156 56e0773d 2019-11-28 stsp char *te_name;
3157 33cbf02b 2020-01-12 stsp
3158 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3159 33cbf02b 2020-01-12 stsp p++;
3160 941e9f74 2019-05-21 stsp
3161 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3162 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3163 941e9f74 2019-05-21 stsp if (slash == NULL)
3164 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3165 33cbf02b 2020-01-12 stsp else
3166 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3167 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3168 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3169 56e0773d 2019-11-28 stsp break;
3170 941e9f74 2019-05-21 stsp }
3171 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3172 56e0773d 2019-11-28 stsp if (te == NULL) {
3173 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3174 56e0773d 2019-11-28 stsp free(te_name);
3175 941e9f74 2019-05-21 stsp break;
3176 941e9f74 2019-05-21 stsp }
3177 56e0773d 2019-11-28 stsp free(te_name);
3178 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3179 941e9f74 2019-05-21 stsp
3180 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3181 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3182 b03c880f 2019-05-21 stsp
3183 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3184 941e9f74 2019-05-21 stsp if (slash)
3185 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3186 941e9f74 2019-05-21 stsp else
3187 941e9f74 2019-05-21 stsp subpath = strdup(path);
3188 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3189 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3190 941e9f74 2019-05-21 stsp break;
3191 941e9f74 2019-05-21 stsp }
3192 941e9f74 2019-05-21 stsp
3193 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
3194 941e9f74 2019-05-21 stsp subpath);
3195 941e9f74 2019-05-21 stsp if (err)
3196 941e9f74 2019-05-21 stsp break;
3197 941e9f74 2019-05-21 stsp
3198 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3199 941e9f74 2019-05-21 stsp free(tree_id);
3200 941e9f74 2019-05-21 stsp if (err)
3201 941e9f74 2019-05-21 stsp break;
3202 941e9f74 2019-05-21 stsp
3203 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3204 941e9f74 2019-05-21 stsp if (err) {
3205 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3206 941e9f74 2019-05-21 stsp break;
3207 941e9f74 2019-05-21 stsp }
3208 941e9f74 2019-05-21 stsp if (slash == NULL)
3209 941e9f74 2019-05-21 stsp break;
3210 941e9f74 2019-05-21 stsp free(subpath);
3211 941e9f74 2019-05-21 stsp subpath = NULL;
3212 941e9f74 2019-05-21 stsp p = slash;
3213 941e9f74 2019-05-21 stsp }
3214 941e9f74 2019-05-21 stsp
3215 941e9f74 2019-05-21 stsp free(subpath);
3216 1a76625f 2018-10-22 stsp return err;
3217 61266923 2020-01-14 stsp }
3218 61266923 2020-01-14 stsp
3219 61266923 2020-01-14 stsp static const struct got_error *
3220 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3221 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3222 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3223 55cccc34 2020-02-20 stsp {
3224 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3225 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3226 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3227 55cccc34 2020-02-20 stsp
3228 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3229 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3230 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3231 55cccc34 2020-02-20 stsp
3232 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3233 bc573f3b 2021-07-10 stsp if (err)
3234 55cccc34 2020-02-20 stsp return err;
3235 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3236 55cccc34 2020-02-20 stsp
3237 55cccc34 2020-02-20 stsp *new_view = tree_view;
3238 55cccc34 2020-02-20 stsp
3239 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3240 55cccc34 2020-02-20 stsp return NULL;
3241 55cccc34 2020-02-20 stsp
3242 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
3243 55cccc34 2020-02-20 stsp }
3244 55cccc34 2020-02-20 stsp
3245 55cccc34 2020-02-20 stsp static const struct got_error *
3246 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3247 61266923 2020-01-14 stsp {
3248 61266923 2020-01-14 stsp sigset_t sigset;
3249 61266923 2020-01-14 stsp int errcode;
3250 61266923 2020-01-14 stsp
3251 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3252 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3253 61266923 2020-01-14 stsp
3254 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3255 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3256 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3257 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3258 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3259 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
3260 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3261 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
3262 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3263 61266923 2020-01-14 stsp
3264 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3265 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3266 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3267 61266923 2020-01-14 stsp
3268 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3269 61266923 2020-01-14 stsp if (errcode)
3270 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3271 61266923 2020-01-14 stsp
3272 61266923 2020-01-14 stsp return NULL;
3273 1a76625f 2018-10-22 stsp }
3274 1a76625f 2018-10-22 stsp
3275 1a76625f 2018-10-22 stsp static void *
3276 1a76625f 2018-10-22 stsp log_thread(void *arg)
3277 1a76625f 2018-10-22 stsp {
3278 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3279 1a76625f 2018-10-22 stsp int errcode = 0;
3280 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3281 1a76625f 2018-10-22 stsp int done = 0;
3282 61266923 2020-01-14 stsp
3283 f2d749db 2022-07-12 thomas /*
3284 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
3285 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
3286 f2d749db 2022-07-12 thomas */
3287 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
3288 f2d749db 2022-07-12 thomas if (errcode) {
3289 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
3290 61266923 2020-01-14 stsp return (void *)err;
3291 f2d749db 2022-07-12 thomas }
3292 1a76625f 2018-10-22 stsp
3293 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
3294 f2d749db 2022-07-12 thomas if (err) {
3295 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3296 f2d749db 2022-07-12 thomas goto done;
3297 f2d749db 2022-07-12 thomas }
3298 f2d749db 2022-07-12 thomas
3299 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3300 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3301 f2d749db 2022-07-12 thomas if (errcode) {
3302 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3303 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3304 f2d749db 2022-07-12 thomas goto done;
3305 f2d749db 2022-07-12 thomas }
3306 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3307 1a76625f 2018-10-22 stsp if (err) {
3308 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3309 f2d749db 2022-07-12 thomas goto done;
3310 1a76625f 2018-10-22 stsp err = NULL;
3311 1a76625f 2018-10-22 stsp done = 1;
3312 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3313 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3314 7e8004ba 2022-09-11 thomas if (a->limit_match)
3315 7e8004ba 2022-09-11 thomas a->commits_needed--;
3316 7e8004ba 2022-09-11 thomas } else
3317 7e8004ba 2022-09-11 thomas a->commits_needed--;
3318 7e8004ba 2022-09-11 thomas }
3319 1a76625f 2018-10-22 stsp
3320 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3321 3abe8080 2019-04-10 stsp if (errcode) {
3322 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3323 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3324 f2d749db 2022-07-12 thomas goto done;
3325 3abe8080 2019-04-10 stsp } else if (*a->quit)
3326 1a76625f 2018-10-22 stsp done = 1;
3327 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3328 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3329 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3330 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3331 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3332 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3333 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3334 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3335 1a76625f 2018-10-22 stsp }
3336 1a76625f 2018-10-22 stsp
3337 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3338 7c1452c1 2020-03-26 stsp if (errcode) {
3339 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3340 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3341 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3342 f2d749db 2022-07-12 thomas goto done;
3343 7c1452c1 2020-03-26 stsp }
3344 7c1452c1 2020-03-26 stsp
3345 1a76625f 2018-10-22 stsp if (done)
3346 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3347 7c1452c1 2020-03-26 stsp else {
3348 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3349 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3350 7c1452c1 2020-03-26 stsp &tog_mutex);
3351 f2d749db 2022-07-12 thomas if (errcode) {
3352 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3353 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3354 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3355 f2d749db 2022-07-12 thomas goto done;
3356 f2d749db 2022-07-12 thomas }
3357 21355643 2020-12-06 stsp if (*a->quit)
3358 21355643 2020-12-06 stsp done = 1;
3359 7c1452c1 2020-03-26 stsp }
3360 1a76625f 2018-10-22 stsp }
3361 1a76625f 2018-10-22 stsp }
3362 3abe8080 2019-04-10 stsp a->log_complete = 1;
3363 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3364 f2d749db 2022-07-12 thomas if (errcode)
3365 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3366 f2d749db 2022-07-12 thomas done:
3367 f2d749db 2022-07-12 thomas if (err) {
3368 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3369 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3370 f2d749db 2022-07-12 thomas }
3371 1a76625f 2018-10-22 stsp return (void *)err;
3372 1a76625f 2018-10-22 stsp }
3373 1a76625f 2018-10-22 stsp
3374 1a76625f 2018-10-22 stsp static const struct got_error *
3375 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3376 1a76625f 2018-10-22 stsp {
3377 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3378 1a76625f 2018-10-22 stsp int errcode;
3379 1a76625f 2018-10-22 stsp
3380 1a76625f 2018-10-22 stsp if (s->thread) {
3381 1a76625f 2018-10-22 stsp s->quit = 1;
3382 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3383 1a76625f 2018-10-22 stsp if (errcode)
3384 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3385 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3386 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3387 1a76625f 2018-10-22 stsp if (errcode)
3388 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3389 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3390 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3391 1a76625f 2018-10-22 stsp if (errcode)
3392 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3393 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3394 1a76625f 2018-10-22 stsp if (errcode)
3395 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3396 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3397 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3398 1a76625f 2018-10-22 stsp }
3399 1a76625f 2018-10-22 stsp
3400 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3401 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3402 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3403 1af5eddf 2022-06-23 thomas }
3404 1af5eddf 2022-06-23 thomas
3405 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3406 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3407 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3408 1af5eddf 2022-06-23 thomas if (err == NULL)
3409 1af5eddf 2022-06-23 thomas err = pack_err;
3410 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3411 1a76625f 2018-10-22 stsp }
3412 1a76625f 2018-10-22 stsp
3413 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3414 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3415 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3416 1a76625f 2018-10-22 stsp }
3417 1a76625f 2018-10-22 stsp
3418 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3419 9343a5fb 2018-06-23 stsp }
3420 9343a5fb 2018-06-23 stsp
3421 9343a5fb 2018-06-23 stsp static const struct got_error *
3422 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3423 1a76625f 2018-10-22 stsp {
3424 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3425 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3426 276b94a1 2020-11-13 naddy int errcode;
3427 1a76625f 2018-10-22 stsp
3428 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3429 276b94a1 2020-11-13 naddy
3430 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3431 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3432 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3433 276b94a1 2020-11-13 naddy
3434 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3435 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3436 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3437 276b94a1 2020-11-13 naddy
3438 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3439 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3440 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3441 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3442 1a76625f 2018-10-22 stsp free(s->start_id);
3443 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3444 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3445 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3446 1a76625f 2018-10-22 stsp return err;
3447 1a76625f 2018-10-22 stsp }
3448 1a76625f 2018-10-22 stsp
3449 7e8004ba 2022-09-11 thomas /*
3450 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3451 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3452 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3453 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3454 7e8004ba 2022-09-11 thomas * works with very slight change.
3455 7e8004ba 2022-09-11 thomas */
3456 1a76625f 2018-10-22 stsp static const struct got_error *
3457 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3458 7e8004ba 2022-09-11 thomas {
3459 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3460 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3461 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3462 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3463 7e8004ba 2022-09-11 thomas char pattern[1024];
3464 7e8004ba 2022-09-11 thomas int ret;
3465 7e8004ba 2022-09-11 thomas
3466 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3467 7e8004ba 2022-09-11 thomas v = view->child;
3468 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3469 7e8004ba 2022-09-11 thomas v = view->parent;
3470 7e8004ba 2022-09-11 thomas
3471 7e8004ba 2022-09-11 thomas /* Get the pattern */
3472 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3473 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3474 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3475 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3476 7e8004ba 2022-09-11 thomas nocbreak();
3477 7e8004ba 2022-09-11 thomas echo();
3478 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3479 7e8004ba 2022-09-11 thomas cbreak();
3480 7e8004ba 2022-09-11 thomas noecho();
3481 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3482 7e8004ba 2022-09-11 thomas if (ret == ERR)
3483 7e8004ba 2022-09-11 thomas return NULL;
3484 7e8004ba 2022-09-11 thomas
3485 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3486 7e8004ba 2022-09-11 thomas /*
3487 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3488 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3489 7e8004ba 2022-09-11 thomas */
3490 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3491 7e8004ba 2022-09-11 thomas return NULL;
3492 7e8004ba 2022-09-11 thomas
3493 7e8004ba 2022-09-11 thomas /*
3494 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3495 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3496 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3497 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3498 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3499 7e8004ba 2022-09-11 thomas * the queue.
3500 7e8004ba 2022-09-11 thomas */
3501 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3502 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3503 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3504 7e8004ba 2022-09-11 thomas s->selected = 0;
3505 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3506 7e8004ba 2022-09-11 thomas
3507 7e8004ba 2022-09-11 thomas return NULL;
3508 7e8004ba 2022-09-11 thomas }
3509 7e8004ba 2022-09-11 thomas
3510 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3511 7e8004ba 2022-09-11 thomas return NULL;
3512 7e8004ba 2022-09-11 thomas
3513 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3514 7e8004ba 2022-09-11 thomas
3515 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3516 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3517 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3518 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3519 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3520 7e8004ba 2022-09-11 thomas
3521 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3522 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3523 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3524 7e8004ba 2022-09-11 thomas
3525 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3526 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3527 7e8004ba 2022-09-11 thomas int have_match = 0;
3528 7e8004ba 2022-09-11 thomas
3529 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3530 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3531 7e8004ba 2022-09-11 thomas if (err)
3532 7e8004ba 2022-09-11 thomas return err;
3533 7e8004ba 2022-09-11 thomas
3534 7e8004ba 2022-09-11 thomas if (have_match) {
3535 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3536 7e8004ba 2022-09-11 thomas
3537 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3538 7e8004ba 2022-09-11 thomas entry->id);
3539 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3540 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3541 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3542 7e8004ba 2022-09-11 thomas break;
3543 7e8004ba 2022-09-11 thomas }
3544 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3545 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3546 7e8004ba 2022-09-11 thomas
3547 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3548 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3549 7e8004ba 2022-09-11 thomas matched, entry);
3550 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3551 7e8004ba 2022-09-11 thomas }
3552 7e8004ba 2022-09-11 thomas }
3553 7e8004ba 2022-09-11 thomas
3554 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3555 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3556 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3557 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3558 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3559 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3560 7e8004ba 2022-09-11 thomas if (err)
3561 7e8004ba 2022-09-11 thomas return err;
3562 7e8004ba 2022-09-11 thomas }
3563 7e8004ba 2022-09-11 thomas
3564 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3565 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3566 7e8004ba 2022-09-11 thomas s->selected = 0;
3567 7e8004ba 2022-09-11 thomas
3568 7e8004ba 2022-09-11 thomas return NULL;
3569 7e8004ba 2022-09-11 thomas }
3570 7e8004ba 2022-09-11 thomas
3571 7e8004ba 2022-09-11 thomas static const struct got_error *
3572 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3573 60493ae3 2019-06-20 stsp {
3574 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3575 60493ae3 2019-06-20 stsp
3576 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3577 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3578 60493ae3 2019-06-20 stsp return NULL;
3579 60493ae3 2019-06-20 stsp }
3580 60493ae3 2019-06-20 stsp
3581 60493ae3 2019-06-20 stsp static const struct got_error *
3582 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3583 60493ae3 2019-06-20 stsp {
3584 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3585 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3586 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3587 60493ae3 2019-06-20 stsp
3588 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3589 f9686aa5 2020-03-27 stsp show_log_view(view);
3590 f9686aa5 2020-03-27 stsp update_panels();
3591 f9686aa5 2020-03-27 stsp doupdate();
3592 f9686aa5 2020-03-27 stsp
3593 96e2b566 2019-07-08 stsp if (s->search_entry) {
3594 13add988 2019-10-15 stsp int errcode, ch;
3595 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3596 13add988 2019-10-15 stsp if (errcode)
3597 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3598 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3599 13add988 2019-10-15 stsp ch = wgetch(view->window);
3600 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3601 13add988 2019-10-15 stsp if (errcode)
3602 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3603 13add988 2019-10-15 stsp "pthread_mutex_lock");
3604 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3605 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3606 678cbce5 2019-07-28 stsp return NULL;
3607 678cbce5 2019-07-28 stsp }
3608 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3609 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3610 96e2b566 2019-07-08 stsp else
3611 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3612 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3613 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3614 df5e841d 2022-06-23 thomas /*
3615 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3616 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3617 1f4d5162 2022-06-23 thomas * might have changed.
3618 df5e841d 2022-06-23 thomas */
3619 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3620 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3621 e7baaec8 2022-09-13 thomas else
3622 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3623 e7baaec8 2022-09-13 thomas entry);
3624 20be8d96 2019-06-21 stsp } else {
3625 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3626 20be8d96 2019-06-21 stsp }
3627 60493ae3 2019-06-20 stsp
3628 60493ae3 2019-06-20 stsp while (1) {
3629 13add988 2019-10-15 stsp int have_match = 0;
3630 13add988 2019-10-15 stsp
3631 60493ae3 2019-06-20 stsp if (entry == NULL) {
3632 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3633 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3634 f9967bca 2020-03-27 stsp view->search_next_done =
3635 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3636 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3637 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3638 f801134a 2019-06-25 stsp return NULL;
3639 60493ae3 2019-06-20 stsp }
3640 96e2b566 2019-07-08 stsp /*
3641 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3642 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3643 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3644 96e2b566 2019-07-08 stsp */
3645 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3646 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3647 60493ae3 2019-06-20 stsp }
3648 60493ae3 2019-06-20 stsp
3649 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3650 13add988 2019-10-15 stsp &view->regex);
3651 5943eee2 2019-08-13 stsp if (err)
3652 13add988 2019-10-15 stsp break;
3653 13add988 2019-10-15 stsp if (have_match) {
3654 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3655 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3656 60493ae3 2019-06-20 stsp break;
3657 60493ae3 2019-06-20 stsp }
3658 13add988 2019-10-15 stsp
3659 96e2b566 2019-07-08 stsp s->search_entry = entry;
3660 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3661 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3662 b1bf1435 2019-06-21 stsp else
3663 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3664 60493ae3 2019-06-20 stsp }
3665 60493ae3 2019-06-20 stsp
3666 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3667 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3668 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3669 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3670 60493ae3 2019-06-20 stsp if (err)
3671 60493ae3 2019-06-20 stsp return err;
3672 ead14cbe 2019-06-21 stsp cur++;
3673 ead14cbe 2019-06-21 stsp }
3674 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3675 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3676 60493ae3 2019-06-20 stsp if (err)
3677 60493ae3 2019-06-20 stsp return err;
3678 ead14cbe 2019-06-21 stsp cur--;
3679 60493ae3 2019-06-20 stsp }
3680 60493ae3 2019-06-20 stsp }
3681 60493ae3 2019-06-20 stsp
3682 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3683 96e2b566 2019-07-08 stsp
3684 60493ae3 2019-06-20 stsp return NULL;
3685 60493ae3 2019-06-20 stsp }
3686 60493ae3 2019-06-20 stsp
3687 60493ae3 2019-06-20 stsp static const struct got_error *
3688 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3689 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3690 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3691 80ddbec8 2018-04-29 stsp {
3692 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3693 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3694 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3695 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3696 1a76625f 2018-10-22 stsp int errcode;
3697 80ddbec8 2018-04-29 stsp
3698 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3699 f135c941 2020-02-20 stsp free(s->in_repo_path);
3700 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3701 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3702 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3703 51b7e1c3 2023-04-14 thomas goto done;
3704 51b7e1c3 2023-04-14 thomas }
3705 f135c941 2020-02-20 stsp }
3706 ecb28ae0 2018-07-16 stsp
3707 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3708 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3709 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3710 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3711 78756c87 2020-11-24 stsp
3712 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3713 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3714 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3715 7e8004ba 2022-09-11 thomas
3716 fb2756b9 2018-08-04 stsp s->repo = repo;
3717 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3718 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3719 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3720 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3721 9cd7cbd1 2020-12-07 stsp goto done;
3722 9cd7cbd1 2020-12-07 stsp }
3723 9cd7cbd1 2020-12-07 stsp }
3724 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3725 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3726 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3727 5036bf37 2018-09-24 stsp goto done;
3728 5036bf37 2018-09-24 stsp }
3729 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3730 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3731 e5a0f69f 2018-08-18 stsp
3732 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3733 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3734 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3735 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3736 11b20872 2019-11-08 stsp if (err)
3737 11b20872 2019-11-08 stsp goto done;
3738 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3739 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3740 11b20872 2019-11-08 stsp if (err) {
3741 11b20872 2019-11-08 stsp free_colors(&s->colors);
3742 11b20872 2019-11-08 stsp goto done;
3743 11b20872 2019-11-08 stsp }
3744 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3745 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3746 11b20872 2019-11-08 stsp if (err) {
3747 11b20872 2019-11-08 stsp free_colors(&s->colors);
3748 11b20872 2019-11-08 stsp goto done;
3749 11b20872 2019-11-08 stsp }
3750 11b20872 2019-11-08 stsp }
3751 11b20872 2019-11-08 stsp
3752 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3753 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3754 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3755 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3756 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3757 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3758 1a76625f 2018-10-22 stsp
3759 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3760 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3761 1af5eddf 2022-06-23 thomas if (err)
3762 1af5eddf 2022-06-23 thomas goto done;
3763 1af5eddf 2022-06-23 thomas }
3764 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3765 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3766 7cd52833 2022-06-23 thomas if (err)
3767 7cd52833 2022-06-23 thomas goto done;
3768 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3769 b672a97a 2020-01-27 stsp !s->log_branches);
3770 1a76625f 2018-10-22 stsp if (err)
3771 1a76625f 2018-10-22 stsp goto done;
3772 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3773 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3774 c5b78334 2020-01-12 stsp if (err)
3775 c5b78334 2020-01-12 stsp goto done;
3776 1a76625f 2018-10-22 stsp
3777 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3778 1a76625f 2018-10-22 stsp if (errcode) {
3779 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3780 1a76625f 2018-10-22 stsp goto done;
3781 1a76625f 2018-10-22 stsp }
3782 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3783 7c1452c1 2020-03-26 stsp if (errcode) {
3784 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3785 7c1452c1 2020-03-26 stsp goto done;
3786 7c1452c1 2020-03-26 stsp }
3787 1a76625f 2018-10-22 stsp
3788 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3789 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3790 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3791 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3792 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3793 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3794 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3795 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3796 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3797 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3798 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3799 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3800 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3801 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3802 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3803 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3804 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3805 ba4f502b 2018-08-04 stsp done:
3806 51b7e1c3 2023-04-14 thomas if (err) {
3807 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3808 51b7e1c3 2023-04-14 thomas close_log_view(view);
3809 51b7e1c3 2023-04-14 thomas view_close(view);
3810 51b7e1c3 2023-04-14 thomas }
3811 ba4f502b 2018-08-04 stsp return err;
3812 ba4f502b 2018-08-04 stsp }
3813 ba4f502b 2018-08-04 stsp
3814 e5a0f69f 2018-08-18 stsp static const struct got_error *
3815 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3816 ba4f502b 2018-08-04 stsp {
3817 f2f6d207 2020-11-24 stsp const struct got_error *err;
3818 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3819 ba4f502b 2018-08-04 stsp
3820 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3821 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3822 2b380cc8 2018-10-24 stsp &s->thread_args);
3823 2b380cc8 2018-10-24 stsp if (errcode)
3824 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3825 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3826 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3827 f2f6d207 2020-11-24 stsp if (err)
3828 f2f6d207 2020-11-24 stsp return err;
3829 f2f6d207 2020-11-24 stsp }
3830 2b380cc8 2018-10-24 stsp }
3831 2b380cc8 2018-10-24 stsp
3832 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3833 b31f89ff 2022-07-01 thomas }
3834 b31f89ff 2022-07-01 thomas
3835 b31f89ff 2022-07-01 thomas static void
3836 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3837 b31f89ff 2022-07-01 thomas {
3838 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3839 b31f89ff 2022-07-01 thomas
3840 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3841 b31f89ff 2022-07-01 thomas return;
3842 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3843 7e8004ba 2022-09-11 thomas view->count = 0;
3844 b31f89ff 2022-07-01 thomas
3845 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3846 b31f89ff 2022-07-01 thomas || home)
3847 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3848 b31f89ff 2022-07-01 thomas
3849 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3850 b31f89ff 2022-07-01 thomas --s->selected;
3851 b31f89ff 2022-07-01 thomas else
3852 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3853 b31f89ff 2022-07-01 thomas
3854 b31f89ff 2022-07-01 thomas select_commit(s);
3855 b31f89ff 2022-07-01 thomas return;
3856 b31f89ff 2022-07-01 thomas }
3857 b31f89ff 2022-07-01 thomas
3858 b31f89ff 2022-07-01 thomas static const struct got_error *
3859 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3860 b31f89ff 2022-07-01 thomas {
3861 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3862 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3863 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3864 b31f89ff 2022-07-01 thomas
3865 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3866 7e8004ba 2022-09-11 thomas return NULL;
3867 7e8004ba 2022-09-11 thomas
3868 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3869 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3870 b31f89ff 2022-07-01 thomas return NULL;
3871 b31f89ff 2022-07-01 thomas
3872 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3873 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3874 b31f89ff 2022-07-01 thomas
3875 7ed048bd 2022-08-12 thomas if (!page) {
3876 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3877 b31f89ff 2022-07-01 thomas ++s->selected;
3878 b31f89ff 2022-07-01 thomas else
3879 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3880 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3881 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3882 7ed048bd 2022-08-12 thomas int n;
3883 7ed048bd 2022-08-12 thomas
3884 7ed048bd 2022-08-12 thomas s->selected = 0;
3885 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3886 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3887 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3888 7ed048bd 2022-08-12 thomas if (entry == NULL)
3889 7ed048bd 2022-08-12 thomas break;
3890 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3891 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3892 7ed048bd 2022-08-12 thomas }
3893 7ed048bd 2022-08-12 thomas if (n > 0)
3894 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3895 b31f89ff 2022-07-01 thomas } else {
3896 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3897 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3898 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3899 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3900 bd3f8225 2022-08-12 thomas else
3901 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3902 b31f89ff 2022-07-01 thomas }
3903 b31f89ff 2022-07-01 thomas if (err)
3904 b31f89ff 2022-07-01 thomas return err;
3905 b31f89ff 2022-07-01 thomas
3906 a5d43cac 2022-07-01 thomas /*
3907 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3908 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3909 a5d43cac 2022-07-01 thomas */
3910 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3911 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3912 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3913 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3914 25100026 2022-08-13 thomas }
3915 a5d43cac 2022-07-01 thomas
3916 b31f89ff 2022-07-01 thomas select_commit(s);
3917 b31f89ff 2022-07-01 thomas
3918 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3919 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3920 b31f89ff 2022-07-01 thomas view->count = 0;
3921 b31f89ff 2022-07-01 thomas
3922 b31f89ff 2022-07-01 thomas return NULL;
3923 e5a0f69f 2018-08-18 stsp }
3924 04cc582a 2018-08-01 stsp
3925 a5d43cac 2022-07-01 thomas static void
3926 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3927 a5d43cac 2022-07-01 thomas {
3928 24415785 2022-07-03 thomas *x = 0;
3929 24415785 2022-07-03 thomas *y = 0;
3930 24415785 2022-07-03 thomas
3931 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3932 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3933 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3934 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3935 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3936 53d2bdd3 2022-07-10 thomas else
3937 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3938 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3939 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3940 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3941 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3942 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3943 53d2bdd3 2022-07-10 thomas else
3944 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3945 53d2bdd3 2022-07-10 thomas }
3946 a5d43cac 2022-07-01 thomas }
3947 a5d43cac 2022-07-01 thomas
3948 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3949 e5a0f69f 2018-08-18 stsp static const struct got_error *
3950 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3951 a5d43cac 2022-07-01 thomas {
3952 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3953 a5d43cac 2022-07-01 thomas
3954 a5d43cac 2022-07-01 thomas view->nlines = y;
3955 64486692 2022-07-07 thomas view->ncols = COLS;
3956 a5d43cac 2022-07-01 thomas err = view_resize(view);
3957 a5d43cac 2022-07-01 thomas if (err)
3958 a5d43cac 2022-07-01 thomas return err;
3959 a5d43cac 2022-07-01 thomas
3960 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3961 a5d43cac 2022-07-01 thomas
3962 a5d43cac 2022-07-01 thomas return err;
3963 07dd3ed3 2022-08-06 thomas }
3964 07dd3ed3 2022-08-06 thomas
3965 07dd3ed3 2022-08-06 thomas static const struct got_error *
3966 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3967 07dd3ed3 2022-08-06 thomas {
3968 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3969 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3970 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3971 25100026 2022-08-13 thomas
3972 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3973 25100026 2022-08-13 thomas return NULL;
3974 07dd3ed3 2022-08-06 thomas
3975 07dd3ed3 2022-08-06 thomas g = view->gline;
3976 07dd3ed3 2022-08-06 thomas view->gline = 0;
3977 07dd3ed3 2022-08-06 thomas
3978 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3979 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3980 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3981 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3982 07dd3ed3 2022-08-06 thomas select_commit(s);
3983 07dd3ed3 2022-08-06 thomas return NULL;
3984 07dd3ed3 2022-08-06 thomas }
3985 07dd3ed3 2022-08-06 thomas
3986 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3987 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3988 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3989 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3990 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3991 07dd3ed3 2022-08-06 thomas if (err)
3992 07dd3ed3 2022-08-06 thomas return err;
3993 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3994 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3995 07dd3ed3 2022-08-06 thomas
3996 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3997 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3998 07dd3ed3 2022-08-06 thomas
3999 07dd3ed3 2022-08-06 thomas select_commit(s);
4000 07dd3ed3 2022-08-06 thomas return NULL;
4001 c72de8ab 2023-02-03 thomas
4002 c72de8ab 2023-02-03 thomas }
4003 c72de8ab 2023-02-03 thomas
4004 c72de8ab 2023-02-03 thomas static void
4005 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
4006 c72de8ab 2023-02-03 thomas {
4007 07dd3ed3 2022-08-06 thomas
4008 c72de8ab 2023-02-03 thomas switch (ch) {
4009 c72de8ab 2023-02-03 thomas case KEY_LEFT:
4010 c72de8ab 2023-02-03 thomas case 'h':
4011 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
4012 c72de8ab 2023-02-03 thomas if (view->x <= 0)
4013 c72de8ab 2023-02-03 thomas view->count = 0;
4014 c72de8ab 2023-02-03 thomas break;
4015 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
4016 c72de8ab 2023-02-03 thomas case 'l':
4017 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
4018 c72de8ab 2023-02-03 thomas view->x += 2;
4019 c72de8ab 2023-02-03 thomas else
4020 c72de8ab 2023-02-03 thomas view->count = 0;
4021 c72de8ab 2023-02-03 thomas break;
4022 c72de8ab 2023-02-03 thomas case '0':
4023 c72de8ab 2023-02-03 thomas view->x = 0;
4024 c72de8ab 2023-02-03 thomas break;
4025 c72de8ab 2023-02-03 thomas case '$':
4026 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
4027 c72de8ab 2023-02-03 thomas view->count = 0;
4028 c72de8ab 2023-02-03 thomas break;
4029 c72de8ab 2023-02-03 thomas default:
4030 c72de8ab 2023-02-03 thomas break;
4031 c72de8ab 2023-02-03 thomas }
4032 a5d43cac 2022-07-01 thomas }
4033 a5d43cac 2022-07-01 thomas
4034 a5d43cac 2022-07-01 thomas static const struct got_error *
4035 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
4036 e5a0f69f 2018-08-18 stsp {
4037 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4038 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
4039 7ed048bd 2022-08-12 thomas int eos, nscroll;
4040 80ddbec8 2018-04-29 stsp
4041 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
4042 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
4043 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
4044 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
4045 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4046 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
4047 fb280deb 2021-08-30 stsp }
4048 c0be8933 2022-08-12 thomas if (err)
4049 c0be8933 2022-08-12 thomas return err;
4050 528dedf3 2021-08-30 stsp }
4051 068ab281 2022-07-01 thomas
4052 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
4053 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
4054 068ab281 2022-07-01 thomas --eos; /* border */
4055 07dd3ed3 2022-08-06 thomas
4056 07dd3ed3 2022-08-06 thomas if (view->gline)
4057 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
4058 068ab281 2022-07-01 thomas
4059 528dedf3 2021-08-30 stsp switch (ch) {
4060 7e8004ba 2022-09-11 thomas case '&':
4061 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
4062 7e8004ba 2022-09-11 thomas break;
4063 1e37a5c2 2019-05-12 jcs case 'q':
4064 1e37a5c2 2019-05-12 jcs s->quit = 1;
4065 05171be4 2022-06-23 thomas break;
4066 05171be4 2022-06-23 thomas case '0':
4067 05171be4 2022-06-23 thomas case '$':
4068 05171be4 2022-06-23 thomas case KEY_RIGHT:
4069 05171be4 2022-06-23 thomas case 'l':
4070 05171be4 2022-06-23 thomas case KEY_LEFT:
4071 05171be4 2022-06-23 thomas case 'h':
4072 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
4073 05171be4 2022-06-23 thomas break;
4074 1e37a5c2 2019-05-12 jcs case 'k':
4075 1e37a5c2 2019-05-12 jcs case KEY_UP:
4076 1e37a5c2 2019-05-12 jcs case '<':
4077 1e37a5c2 2019-05-12 jcs case ',':
4078 f7140bf5 2021-10-17 thomas case CTRL('p'):
4079 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
4080 912a3f79 2021-08-30 j break;
4081 912a3f79 2021-08-30 j case 'g':
4082 aa7a1117 2023-01-09 thomas case '=':
4083 912a3f79 2021-08-30 j case KEY_HOME:
4084 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
4085 07b0611c 2022-06-23 thomas view->count = 0;
4086 1e37a5c2 2019-05-12 jcs break;
4087 70f17a53 2022-06-13 thomas case CTRL('u'):
4088 23427b14 2022-06-23 thomas case 'u':
4089 70f17a53 2022-06-13 thomas nscroll /= 2;
4090 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4091 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4092 a4292ac5 2019-05-12 jcs case CTRL('b'):
4093 1c5e5faa 2022-06-23 thomas case 'b':
4094 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
4095 1e37a5c2 2019-05-12 jcs break;
4096 1e37a5c2 2019-05-12 jcs case 'j':
4097 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4098 1e37a5c2 2019-05-12 jcs case '>':
4099 1e37a5c2 2019-05-12 jcs case '.':
4100 f7140bf5 2021-10-17 thomas case CTRL('n'):
4101 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
4102 912a3f79 2021-08-30 j break;
4103 f69c5a46 2022-07-19 thomas case '@':
4104 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
4105 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4106 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4107 f69c5a46 2022-07-19 thomas break;
4108 912a3f79 2021-08-30 j case 'G':
4109 aa7a1117 2023-01-09 thomas case '*':
4110 912a3f79 2021-08-30 j case KEY_END: {
4111 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4112 912a3f79 2021-08-30 j * traverse them all. */
4113 07b0611c 2022-06-23 thomas view->count = 0;
4114 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4115 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4116 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4117 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4118 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4119 1e37a5c2 2019-05-12 jcs break;
4120 912a3f79 2021-08-30 j }
4121 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4122 23427b14 2022-06-23 thomas case 'd':
4123 70f17a53 2022-06-13 thomas nscroll /= 2;
4124 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4125 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4126 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4127 4c2d69cb 2022-06-23 thomas case 'f':
4128 b31f89ff 2022-07-01 thomas case ' ':
4129 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4130 1e37a5c2 2019-05-12 jcs break;
4131 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4132 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4133 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4134 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4135 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4136 2b779855 2020-12-05 naddy select_commit(s);
4137 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4138 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4139 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4140 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4141 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4142 0bf7f153 2020-12-02 naddy }
4143 1e37a5c2 2019-05-12 jcs break;
4144 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4145 444d5325 2022-07-03 thomas case '\r':
4146 07b0611c 2022-06-23 thomas view->count = 0;
4147 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4148 e5a0f69f 2018-08-18 stsp break;
4149 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4150 1e37a5c2 2019-05-12 jcs break;
4151 1be4947a 2022-07-22 thomas case 'T':
4152 07b0611c 2022-06-23 thomas view->count = 0;
4153 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4154 5036bf37 2018-09-24 stsp break;
4155 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4156 1e37a5c2 2019-05-12 jcs break;
4157 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4158 21355643 2020-12-06 stsp case CTRL('l'):
4159 21355643 2020-12-06 stsp case 'B':
4160 07b0611c 2022-06-23 thomas view->count = 0;
4161 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4162 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4163 1e37a5c2 2019-05-12 jcs break;
4164 21355643 2020-12-06 stsp err = stop_log_thread(s);
4165 74cfe85e 2020-10-20 stsp if (err)
4166 74cfe85e 2020-10-20 stsp return err;
4167 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4168 21355643 2020-12-06 stsp char *parent_path;
4169 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4170 21355643 2020-12-06 stsp if (err)
4171 21355643 2020-12-06 stsp return err;
4172 21355643 2020-12-06 stsp free(s->in_repo_path);
4173 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4174 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4175 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4176 21355643 2020-12-06 stsp struct got_object_id *start_id;
4177 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4178 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4179 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4180 466429a1 2022-11-17 thomas if (err) {
4181 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4182 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4183 466429a1 2022-11-17 thomas return err;
4184 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4185 466429a1 2022-11-17 thomas free(s->head_ref_name);
4186 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4187 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4188 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4189 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4190 466429a1 2022-11-17 thomas if (err)
4191 466429a1 2022-11-17 thomas return err;
4192 466429a1 2022-11-17 thomas }
4193 21355643 2020-12-06 stsp free(s->start_id);
4194 21355643 2020-12-06 stsp s->start_id = start_id;
4195 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4196 21355643 2020-12-06 stsp } else /* 'B' */
4197 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4198 21355643 2020-12-06 stsp
4199 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4200 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4201 bf7e79b3 2022-06-23 thomas if (err)
4202 bf7e79b3 2022-06-23 thomas return err;
4203 bf7e79b3 2022-06-23 thomas }
4204 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4205 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4206 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4207 74cfe85e 2020-10-20 stsp if (err)
4208 21355643 2020-12-06 stsp return err;
4209 51a10b52 2020-12-26 stsp tog_free_refs();
4210 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4211 ca51c541 2020-12-07 stsp if (err)
4212 ca51c541 2020-12-07 stsp return err;
4213 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4214 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4215 d01904d4 2019-06-25 stsp if (err)
4216 d01904d4 2019-06-25 stsp return err;
4217 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4218 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4219 b672a97a 2020-01-27 stsp if (err)
4220 b672a97a 2020-01-27 stsp return err;
4221 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4222 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4223 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4224 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4225 21355643 2020-12-06 stsp s->selected_entry = NULL;
4226 21355643 2020-12-06 stsp s->selected = 0;
4227 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4228 21355643 2020-12-06 stsp s->quit = 0;
4229 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4230 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4231 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4232 94ecf40d 2022-07-22 thomas view->offset = 0;
4233 d01904d4 2019-06-25 stsp break;
4234 1be4947a 2022-07-22 thomas case 'R':
4235 07b0611c 2022-06-23 thomas view->count = 0;
4236 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4237 6458efa5 2020-11-24 stsp break;
4238 1e37a5c2 2019-05-12 jcs default:
4239 07b0611c 2022-06-23 thomas view->count = 0;
4240 1e37a5c2 2019-05-12 jcs break;
4241 899d86c2 2018-05-10 stsp }
4242 e5a0f69f 2018-08-18 stsp
4243 80ddbec8 2018-04-29 stsp return err;
4244 80ddbec8 2018-04-29 stsp }
4245 80ddbec8 2018-04-29 stsp
4246 4ed7e80c 2018-05-20 stsp static const struct got_error *
4247 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4248 c2db6724 2019-01-04 stsp {
4249 c2db6724 2019-01-04 stsp const struct got_error *error;
4250 c2db6724 2019-01-04 stsp
4251 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4252 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4253 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4254 37c06ea4 2019-07-15 stsp #endif
4255 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4256 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4257 c2db6724 2019-01-04 stsp
4258 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4259 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4260 c2db6724 2019-01-04 stsp
4261 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4262 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4263 c2db6724 2019-01-04 stsp
4264 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4265 c2db6724 2019-01-04 stsp if (error != NULL)
4266 c2db6724 2019-01-04 stsp return error;
4267 c2db6724 2019-01-04 stsp
4268 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4269 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4270 c2db6724 2019-01-04 stsp
4271 c2db6724 2019-01-04 stsp return NULL;
4272 c2db6724 2019-01-04 stsp }
4273 c2db6724 2019-01-04 stsp
4274 b85a3496 2023-04-14 thomas static const struct got_error *
4275 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4276 b85a3496 2023-04-14 thomas {
4277 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4278 5b3a801d 2023-04-22 thomas const char *screen_dump_path;
4279 f4086c71 2023-04-22 thomas int in;
4280 b85a3496 2023-04-14 thomas
4281 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4282 fa9bb690 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_TEST_SCRIPT not defined");
4283 b85a3496 2023-04-14 thomas
4284 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4285 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4286 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4287 b85a3496 2023-04-14 thomas test_script_path);
4288 b85a3496 2023-04-14 thomas goto done;
4289 b85a3496 2023-04-14 thomas }
4290 b85a3496 2023-04-14 thomas
4291 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4292 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4293 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4294 f4086c71 2023-04-22 thomas err = got_error_from_errno2("fopen", "/dev/null");
4295 b85a3496 2023-04-14 thomas goto done;
4296 b85a3496 2023-04-14 thomas }
4297 b85a3496 2023-04-14 thomas
4298 f4086c71 2023-04-22 thomas in = dup(fileno(tog_io.cout));
4299 f4086c71 2023-04-22 thomas if (in == -1) {
4300 f4086c71 2023-04-22 thomas err = got_error_from_errno("dup");
4301 f4086c71 2023-04-22 thomas goto done;
4302 f4086c71 2023-04-22 thomas }
4303 f4086c71 2023-04-22 thomas tog_io.cin = fdopen(in, "r");
4304 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4305 f4086c71 2023-04-22 thomas err = got_error_from_errno("fdopen");
4306 f4086c71 2023-04-22 thomas close(in);
4307 b85a3496 2023-04-14 thomas goto done;
4308 b85a3496 2023-04-14 thomas }
4309 b85a3496 2023-04-14 thomas
4310 5b3a801d 2023-04-22 thomas screen_dump_path = getenv("TOG_SCR_DUMP");
4311 5b3a801d 2023-04-22 thomas if (screen_dump_path == NULL || *screen_dump_path == '\0')
4312 5b3a801d 2023-04-22 thomas return got_error_msg(GOT_ERR_IO, "TOG_SCR_DUMP not defined");
4313 5b3a801d 2023-04-22 thomas tog_io.sdump = fopen(screen_dump_path, "wex");
4314 5b3a801d 2023-04-22 thomas if (tog_io.sdump == NULL) {
4315 5b3a801d 2023-04-22 thomas err = got_error_from_errno2("fopen", screen_dump_path);
4316 5b3a801d 2023-04-22 thomas goto done;
4317 5b3a801d 2023-04-22 thomas }
4318 5b3a801d 2023-04-22 thomas
4319 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4320 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4321 b85a3496 2023-04-14 thomas goto done;
4322 b85a3496 2023-04-14 thomas }
4323 b85a3496 2023-04-14 thomas
4324 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4325 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4326 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4327 557d3365 2023-04-14 thomas
4328 557d3365 2023-04-14 thomas using_mock_io = 1;
4329 1134ebde 2023-04-22 thomas
4330 b85a3496 2023-04-14 thomas done:
4331 b85a3496 2023-04-14 thomas if (err)
4332 557d3365 2023-04-14 thomas tog_io_close();
4333 b85a3496 2023-04-14 thomas return err;
4334 b85a3496 2023-04-14 thomas }
4335 b85a3496 2023-04-14 thomas
4336 557d3365 2023-04-14 thomas static void
4337 557d3365 2023-04-14 thomas init_curses(void)
4338 a915003a 2019-02-05 stsp {
4339 296152d1 2022-05-31 thomas /*
4340 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
4341 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
4342 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
4343 296152d1 2022-05-31 thomas */
4344 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
4345 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
4346 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
4347 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
4348 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
4349 296152d1 2022-05-31 thomas
4350 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4351 557d3365 2023-04-14 thomas return;
4352 b85a3496 2023-04-14 thomas
4353 557d3365 2023-04-14 thomas initscr();
4354 557d3365 2023-04-14 thomas
4355 a915003a 2019-02-05 stsp cbreak();
4356 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4357 a915003a 2019-02-05 stsp noecho();
4358 a915003a 2019-02-05 stsp nonl();
4359 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4360 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4361 a915003a 2019-02-05 stsp curs_set(0);
4362 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4363 6d17833f 2019-11-08 stsp start_color();
4364 6d17833f 2019-11-08 stsp use_default_colors();
4365 6d17833f 2019-11-08 stsp }
4366 b85a3496 2023-04-14 thomas
4367 557d3365 2023-04-14 thomas return;
4368 a915003a 2019-02-05 stsp }
4369 a915003a 2019-02-05 stsp
4370 c2db6724 2019-01-04 stsp static const struct got_error *
4371 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4372 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4373 f135c941 2020-02-20 stsp {
4374 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4375 f135c941 2020-02-20 stsp
4376 f135c941 2020-02-20 stsp if (argc == 0) {
4377 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4378 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4379 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4380 f135c941 2020-02-20 stsp return NULL;
4381 f135c941 2020-02-20 stsp }
4382 f135c941 2020-02-20 stsp
4383 f135c941 2020-02-20 stsp if (worktree) {
4384 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4385 bfd61697 2020-11-14 stsp char *p;
4386 f135c941 2020-02-20 stsp
4387 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4388 f135c941 2020-02-20 stsp if (err)
4389 f135c941 2020-02-20 stsp return err;
4390 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4391 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4392 bfd61697 2020-11-14 stsp p) == -1) {
4393 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4394 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4395 f135c941 2020-02-20 stsp }
4396 f135c941 2020-02-20 stsp free(p);
4397 f135c941 2020-02-20 stsp } else
4398 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4399 f135c941 2020-02-20 stsp
4400 f135c941 2020-02-20 stsp return err;
4401 f135c941 2020-02-20 stsp }
4402 f135c941 2020-02-20 stsp
4403 f135c941 2020-02-20 stsp static const struct got_error *
4404 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4405 9f7d7167 2018-04-29 stsp {
4406 1d98034b 2023-04-14 thomas const struct got_error *error;
4407 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4408 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4409 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4410 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4411 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4412 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4413 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4414 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4415 04cc582a 2018-08-01 stsp struct tog_view *view;
4416 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4417 80ddbec8 2018-04-29 stsp
4418 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4419 80ddbec8 2018-04-29 stsp switch (ch) {
4420 b672a97a 2020-01-27 stsp case 'b':
4421 b672a97a 2020-01-27 stsp log_branches = 1;
4422 b672a97a 2020-01-27 stsp break;
4423 80ddbec8 2018-04-29 stsp case 'c':
4424 80ddbec8 2018-04-29 stsp start_commit = optarg;
4425 80ddbec8 2018-04-29 stsp break;
4426 ecb28ae0 2018-07-16 stsp case 'r':
4427 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4428 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4429 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4430 9ba1d308 2019-10-21 stsp optarg);
4431 ecb28ae0 2018-07-16 stsp break;
4432 80ddbec8 2018-04-29 stsp default:
4433 17020d27 2019-03-07 stsp usage_log();
4434 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4435 80ddbec8 2018-04-29 stsp }
4436 80ddbec8 2018-04-29 stsp }
4437 80ddbec8 2018-04-29 stsp
4438 80ddbec8 2018-04-29 stsp argc -= optind;
4439 80ddbec8 2018-04-29 stsp argv += optind;
4440 80ddbec8 2018-04-29 stsp
4441 f135c941 2020-02-20 stsp if (argc > 1)
4442 f135c941 2020-02-20 stsp usage_log();
4443 963f97a1 2019-03-18 stsp
4444 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4445 7cd52833 2022-06-23 thomas if (error != NULL)
4446 7cd52833 2022-06-23 thomas goto done;
4447 7cd52833 2022-06-23 thomas
4448 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4449 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4450 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4451 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4452 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4453 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4454 c156c7a4 2020-12-18 stsp goto done;
4455 a1fbf39a 2019-08-11 stsp if (worktree)
4456 6962eb72 2020-02-20 stsp repo_path =
4457 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4458 a1fbf39a 2019-08-11 stsp else
4459 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4460 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4461 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4462 c156c7a4 2020-12-18 stsp goto done;
4463 c156c7a4 2020-12-18 stsp }
4464 ecb28ae0 2018-07-16 stsp }
4465 ecb28ae0 2018-07-16 stsp
4466 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4467 80ddbec8 2018-04-29 stsp if (error != NULL)
4468 ecb28ae0 2018-07-16 stsp goto done;
4469 80ddbec8 2018-04-29 stsp
4470 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4471 f135c941 2020-02-20 stsp repo, worktree);
4472 f135c941 2020-02-20 stsp if (error)
4473 f135c941 2020-02-20 stsp goto done;
4474 f135c941 2020-02-20 stsp
4475 557d3365 2023-04-14 thomas init_curses();
4476 f135c941 2020-02-20 stsp
4477 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4478 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4479 c02c541e 2019-03-29 stsp if (error)
4480 c02c541e 2019-03-29 stsp goto done;
4481 c02c541e 2019-03-29 stsp
4482 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4483 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4484 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4485 87670572 2020-12-26 naddy if (error)
4486 87670572 2020-12-26 naddy goto done;
4487 87670572 2020-12-26 naddy }
4488 51a10b52 2020-12-26 stsp
4489 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4490 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4491 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4492 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4493 d8f38dc4 2020-12-05 stsp if (error)
4494 d8f38dc4 2020-12-05 stsp goto done;
4495 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4496 d8f38dc4 2020-12-05 stsp } else {
4497 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4498 d8f38dc4 2020-12-05 stsp if (error == NULL)
4499 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4500 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4501 d8f38dc4 2020-12-05 stsp goto done;
4502 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4503 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4504 d8f38dc4 2020-12-05 stsp if (error)
4505 d8f38dc4 2020-12-05 stsp goto done;
4506 d8f38dc4 2020-12-05 stsp }
4507 8b473291 2019-02-21 stsp
4508 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4509 04cc582a 2018-08-01 stsp if (view == NULL) {
4510 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4511 04cc582a 2018-08-01 stsp goto done;
4512 04cc582a 2018-08-01 stsp }
4513 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4514 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4515 ba4f502b 2018-08-04 stsp if (error)
4516 ba4f502b 2018-08-04 stsp goto done;
4517 2fc00ff4 2019-08-31 stsp if (worktree) {
4518 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4519 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4520 2fc00ff4 2019-08-31 stsp worktree = NULL;
4521 2fc00ff4 2019-08-31 stsp }
4522 557d3365 2023-04-14 thomas error = view_loop(view);
4523 ecb28ae0 2018-07-16 stsp done:
4524 f135c941 2020-02-20 stsp free(in_repo_path);
4525 ecb28ae0 2018-07-16 stsp free(repo_path);
4526 ecb28ae0 2018-07-16 stsp free(cwd);
4527 899d86c2 2018-05-10 stsp free(start_id);
4528 d8f38dc4 2020-12-05 stsp free(label);
4529 d8f38dc4 2020-12-05 stsp if (ref)
4530 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4531 1d0f4054 2021-06-17 stsp if (repo) {
4532 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4533 1d0f4054 2021-06-17 stsp if (error == NULL)
4534 1d0f4054 2021-06-17 stsp error = close_err;
4535 1d0f4054 2021-06-17 stsp }
4536 ec142235 2019-03-07 stsp if (worktree)
4537 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4538 7cd52833 2022-06-23 thomas if (pack_fds) {
4539 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4540 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4541 7cd52833 2022-06-23 thomas if (error == NULL)
4542 7cd52833 2022-06-23 thomas error = pack_err;
4543 b85a3496 2023-04-14 thomas }
4544 51a10b52 2020-12-26 stsp tog_free_refs();
4545 80ddbec8 2018-04-29 stsp return error;
4546 9f7d7167 2018-04-29 stsp }
4547 9f7d7167 2018-04-29 stsp
4548 4ed7e80c 2018-05-20 stsp __dead static void
4549 9f7d7167 2018-04-29 stsp usage_diff(void)
4550 9f7d7167 2018-04-29 stsp {
4551 80ddbec8 2018-04-29 stsp endwin();
4552 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4553 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4554 9f7d7167 2018-04-29 stsp exit(1);
4555 b304db33 2018-05-20 stsp }
4556 b304db33 2018-05-20 stsp
4557 6d17833f 2019-11-08 stsp static int
4558 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4559 41605754 2020-11-12 stsp regmatch_t *regmatch)
4560 6d17833f 2019-11-08 stsp {
4561 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4562 6d17833f 2019-11-08 stsp }
4563 6d17833f 2019-11-08 stsp
4564 ef20f542 2022-06-26 thomas static struct tog_color *
4565 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4566 6d17833f 2019-11-08 stsp {
4567 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4568 6d17833f 2019-11-08 stsp
4569 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4570 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4571 6d17833f 2019-11-08 stsp return tc;
4572 6d17833f 2019-11-08 stsp }
4573 6d17833f 2019-11-08 stsp
4574 6d17833f 2019-11-08 stsp return NULL;
4575 6d17833f 2019-11-08 stsp }
4576 6d17833f 2019-11-08 stsp
4577 4ed7e80c 2018-05-20 stsp static const struct got_error *
4578 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4579 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4580 41605754 2020-11-12 stsp {
4581 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4582 666f7b10 2022-06-23 thomas char *exstr = NULL;
4583 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4584 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4585 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4586 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4587 41605754 2020-11-12 stsp
4588 41605754 2020-11-12 stsp *wtotal = 0;
4589 cbea3800 2022-06-23 thomas
4590 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4591 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4592 41605754 2020-11-12 stsp
4593 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4594 666f7b10 2022-06-23 thomas if (err)
4595 666f7b10 2022-06-23 thomas return err;
4596 666f7b10 2022-06-23 thomas
4597 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4598 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4599 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4600 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4601 666f7b10 2022-06-23 thomas goto done;
4602 666f7b10 2022-06-23 thomas }
4603 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4604 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4605 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4606 cbea3800 2022-06-23 thomas goto done;
4607 cbea3800 2022-06-23 thomas }
4608 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4609 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4610 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4611 cbea3800 2022-06-23 thomas goto done;
4612 cbea3800 2022-06-23 thomas }
4613 05171be4 2022-06-23 thomas
4614 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4615 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4616 cbea3800 2022-06-23 thomas col_tab_align, 1);
4617 cbea3800 2022-06-23 thomas if (err)
4618 cbea3800 2022-06-23 thomas goto done;
4619 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4620 05171be4 2022-06-23 thomas if (n) {
4621 cbea3800 2022-06-23 thomas free(wline);
4622 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4623 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4624 cbea3800 2022-06-23 thomas if (err)
4625 cbea3800 2022-06-23 thomas goto done;
4626 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4627 cbea3800 2022-06-23 thomas wlimit -= width;
4628 cbea3800 2022-06-23 thomas *wtotal += width;
4629 41605754 2020-11-12 stsp }
4630 41605754 2020-11-12 stsp
4631 41605754 2020-11-12 stsp if (wlimit > 0) {
4632 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4633 cbea3800 2022-06-23 thomas size_t wlen;
4634 cbea3800 2022-06-23 thomas
4635 cbea3800 2022-06-23 thomas free(wline);
4636 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4637 cbea3800 2022-06-23 thomas col_tab_align, 1);
4638 cbea3800 2022-06-23 thomas if (err)
4639 cbea3800 2022-06-23 thomas goto done;
4640 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4641 cbea3800 2022-06-23 thomas while (i < wlen) {
4642 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4643 cbea3800 2022-06-23 thomas if (width == -1) {
4644 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4645 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4646 cbea3800 2022-06-23 thomas goto done;
4647 cbea3800 2022-06-23 thomas }
4648 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4649 cbea3800 2022-06-23 thomas break;
4650 1c5e5faa 2022-06-23 thomas w += width;
4651 cbea3800 2022-06-23 thomas i++;
4652 41605754 2020-11-12 stsp }
4653 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4654 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4655 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4656 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4657 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4658 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4659 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4660 41605754 2020-11-12 stsp }
4661 41605754 2020-11-12 stsp }
4662 41605754 2020-11-12 stsp
4663 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4664 cbea3800 2022-06-23 thomas free(wline);
4665 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4666 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4667 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4668 cbea3800 2022-06-23 thomas col_tab_align, 1);
4669 cbea3800 2022-06-23 thomas if (err)
4670 cbea3800 2022-06-23 thomas goto done;
4671 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4672 cbea3800 2022-06-23 thomas } else {
4673 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4674 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4675 cbea3800 2022-06-23 thomas if (err)
4676 cbea3800 2022-06-23 thomas goto done;
4677 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4678 cbea3800 2022-06-23 thomas }
4679 cbea3800 2022-06-23 thomas *wtotal += width2;
4680 41605754 2020-11-12 stsp }
4681 cbea3800 2022-06-23 thomas done:
4682 05171be4 2022-06-23 thomas free(wline);
4683 666f7b10 2022-06-23 thomas free(exstr);
4684 cbea3800 2022-06-23 thomas free(seg0);
4685 cbea3800 2022-06-23 thomas free(seg1);
4686 cbea3800 2022-06-23 thomas free(seg2);
4687 cbea3800 2022-06-23 thomas return err;
4688 41605754 2020-11-12 stsp }
4689 07dd3ed3 2022-08-06 thomas
4690 07dd3ed3 2022-08-06 thomas static int
4691 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4692 07dd3ed3 2022-08-06 thomas {
4693 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4694 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4695 07dd3ed3 2022-08-06 thomas
4696 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4697 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4698 fc2737d5 2022-09-15 thomas
4699 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4700 fc2737d5 2022-09-15 thomas selected = first;
4701 fc2737d5 2022-09-15 thomas eof = &s->eof;
4702 fc2737d5 2022-09-15 thomas f = s->f;
4703 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4704 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4705 41605754 2020-11-12 stsp
4706 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4707 07dd3ed3 2022-08-06 thomas selected = first;
4708 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4709 07dd3ed3 2022-08-06 thomas f = s->f;
4710 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4711 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4712 07dd3ed3 2022-08-06 thomas
4713 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4714 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4715 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4716 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4717 07dd3ed3 2022-08-06 thomas } else
4718 07dd3ed3 2022-08-06 thomas return 0;
4719 07dd3ed3 2022-08-06 thomas
4720 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4721 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4722 07dd3ed3 2022-08-06 thomas return 0;
4723 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4724 07dd3ed3 2022-08-06 thomas rewind(f);
4725 07dd3ed3 2022-08-06 thomas *eof = 0;
4726 07dd3ed3 2022-08-06 thomas *first = 1;
4727 07dd3ed3 2022-08-06 thomas *lineno = 0;
4728 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4729 07dd3ed3 2022-08-06 thomas return 0;
4730 07dd3ed3 2022-08-06 thomas }
4731 07dd3ed3 2022-08-06 thomas
4732 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4733 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4734 07dd3ed3 2022-08-06 thomas view->gline = 0;
4735 07dd3ed3 2022-08-06 thomas
4736 07dd3ed3 2022-08-06 thomas return 1;
4737 07dd3ed3 2022-08-06 thomas }
4738 07dd3ed3 2022-08-06 thomas
4739 41605754 2020-11-12 stsp static const struct got_error *
4740 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4741 26ed57b2 2018-05-19 stsp {
4742 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4743 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4744 61e69b96 2018-05-20 stsp const struct got_error *err;
4745 82c78e96 2022-08-06 thomas int nprinted = 0;
4746 b304db33 2018-05-20 stsp char *line;
4747 826082fe 2020-12-10 stsp size_t linesize = 0;
4748 826082fe 2020-12-10 stsp ssize_t linelen;
4749 61e69b96 2018-05-20 stsp wchar_t *wline;
4750 e0b650dd 2018-05-20 stsp int width;
4751 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4752 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4753 fe621944 2020-11-10 stsp off_t line_offset;
4754 26ed57b2 2018-05-19 stsp
4755 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4756 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4757 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4758 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4759 fe621944 2020-11-10 stsp
4760 f7d12f7e 2018-08-01 stsp werase(view->window);
4761 a3404814 2018-09-02 stsp
4762 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4763 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4764 07dd3ed3 2022-08-06 thomas
4765 a3404814 2018-09-02 stsp if (header) {
4766 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4767 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4768 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4769 07dd3ed3 2022-08-06 thomas
4770 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4771 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4772 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4773 f91a2b48 2022-06-23 thomas 0, 0);
4774 135a2da0 2020-11-11 stsp free(line);
4775 135a2da0 2020-11-11 stsp if (err)
4776 a3404814 2018-09-02 stsp return err;
4777 a3404814 2018-09-02 stsp
4778 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4779 a3404814 2018-09-02 stsp wstandout(view->window);
4780 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4781 e54cc94a 2020-11-11 stsp free(wline);
4782 e54cc94a 2020-11-11 stsp wline = NULL;
4783 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4784 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4785 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4786 a3404814 2018-09-02 stsp wstandend(view->window);
4787 26ed57b2 2018-05-19 stsp
4788 a3404814 2018-09-02 stsp if (max_lines <= 1)
4789 a3404814 2018-09-02 stsp return NULL;
4790 a3404814 2018-09-02 stsp max_lines--;
4791 a3404814 2018-09-02 stsp }
4792 a3404814 2018-09-02 stsp
4793 89f1a395 2020-12-01 naddy s->eof = 0;
4794 05171be4 2022-06-23 thomas view->maxx = 0;
4795 826082fe 2020-12-10 stsp line = NULL;
4796 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4797 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4798 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4799 6568f0aa 2022-08-06 thomas
4800 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4801 826082fe 2020-12-10 stsp if (linelen == -1) {
4802 826082fe 2020-12-10 stsp if (feof(s->f)) {
4803 826082fe 2020-12-10 stsp s->eof = 1;
4804 826082fe 2020-12-10 stsp break;
4805 826082fe 2020-12-10 stsp }
4806 826082fe 2020-12-10 stsp free(line);
4807 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4808 61e69b96 2018-05-20 stsp }
4809 05171be4 2022-06-23 thomas
4810 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4811 07dd3ed3 2022-08-06 thomas continue;
4812 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4813 07dd3ed3 2022-08-06 thomas continue;
4814 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4815 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4816 07dd3ed3 2022-08-06 thomas
4817 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4818 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4819 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4820 cbea3800 2022-06-23 thomas if (err) {
4821 cbea3800 2022-06-23 thomas free(line);
4822 cbea3800 2022-06-23 thomas return err;
4823 cbea3800 2022-06-23 thomas }
4824 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4825 cbea3800 2022-06-23 thomas free(wline);
4826 cbea3800 2022-06-23 thomas wline = NULL;
4827 cbea3800 2022-06-23 thomas
4828 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4829 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4830 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4831 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4832 07dd3ed3 2022-08-06 thomas if (attr)
4833 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4834 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4835 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4836 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4837 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4838 41605754 2020-11-12 stsp if (err) {
4839 41605754 2020-11-12 stsp free(line);
4840 41605754 2020-11-12 stsp return err;
4841 41605754 2020-11-12 stsp }
4842 41605754 2020-11-12 stsp } else {
4843 f1c0ec19 2022-06-23 thomas int skip;
4844 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4845 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4846 f1c0ec19 2022-06-23 thomas if (err) {
4847 f1c0ec19 2022-06-23 thomas free(line);
4848 f1c0ec19 2022-06-23 thomas return err;
4849 f1c0ec19 2022-06-23 thomas }
4850 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4851 f1c0ec19 2022-06-23 thomas free(wline);
4852 41605754 2020-11-12 stsp wline = NULL;
4853 41605754 2020-11-12 stsp }
4854 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4855 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4856 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4857 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4858 07dd3ed3 2022-08-06 thomas } else {
4859 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4860 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4861 07dd3ed3 2022-08-06 thomas }
4862 07dd3ed3 2022-08-06 thomas if (attr)
4863 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4864 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4865 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4866 826082fe 2020-12-10 stsp }
4867 826082fe 2020-12-10 stsp free(line);
4868 fe621944 2020-11-10 stsp if (nprinted >= 1)
4869 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4870 89f1a395 2020-12-01 naddy (nprinted - 1);
4871 fe621944 2020-11-10 stsp else
4872 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4873 26ed57b2 2018-05-19 stsp
4874 a5d43cac 2022-07-01 thomas view_border(view);
4875 c3e9aa98 2019-05-13 jcs
4876 89f1a395 2020-12-01 naddy if (s->eof) {
4877 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4878 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4879 c3e9aa98 2019-05-13 jcs nprinted++;
4880 c3e9aa98 2019-05-13 jcs }
4881 c3e9aa98 2019-05-13 jcs
4882 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4883 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4884 c3e9aa98 2019-05-13 jcs if (err) {
4885 c3e9aa98 2019-05-13 jcs return err;
4886 c3e9aa98 2019-05-13 jcs }
4887 26ed57b2 2018-05-19 stsp
4888 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4889 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4890 e54cc94a 2020-11-11 stsp free(wline);
4891 e54cc94a 2020-11-11 stsp wline = NULL;
4892 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4893 c3e9aa98 2019-05-13 jcs }
4894 c3e9aa98 2019-05-13 jcs
4895 26ed57b2 2018-05-19 stsp return NULL;
4896 abd2672a 2018-12-23 stsp }
4897 abd2672a 2018-12-23 stsp
4898 abd2672a 2018-12-23 stsp static char *
4899 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4900 abd2672a 2018-12-23 stsp {
4901 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4902 09867e48 2019-08-13 stsp char *p, *s;
4903 09867e48 2019-08-13 stsp
4904 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4905 09867e48 2019-08-13 stsp if (tm == NULL)
4906 09867e48 2019-08-13 stsp return NULL;
4907 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4908 09867e48 2019-08-13 stsp if (s == NULL)
4909 09867e48 2019-08-13 stsp return NULL;
4910 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4911 abd2672a 2018-12-23 stsp if (p)
4912 abd2672a 2018-12-23 stsp *p = '\0';
4913 abd2672a 2018-12-23 stsp return s;
4914 9f7d7167 2018-04-29 stsp }
4915 9f7d7167 2018-04-29 stsp
4916 4ed7e80c 2018-05-20 stsp static const struct got_error *
4917 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4918 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4919 abd2672a 2018-12-23 stsp {
4920 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4921 fe621944 2020-11-10 stsp
4922 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4923 fe621944 2020-11-10 stsp if (p == NULL)
4924 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4925 82c78e96 2022-08-06 thomas *lines = p;
4926 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4927 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4928 fe621944 2020-11-10 stsp (*nlines)++;
4929 82c78e96 2022-08-06 thomas
4930 fe621944 2020-11-10 stsp return NULL;
4931 fe621944 2020-11-10 stsp }
4932 fe621944 2020-11-10 stsp
4933 fe621944 2020-11-10 stsp static const struct got_error *
4934 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4935 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4936 be97ab03 2023-01-19 thomas {
4937 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4938 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4939 be97ab03 2023-01-19 thomas size_t i, r;
4940 be97ab03 2023-01-19 thomas
4941 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4942 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4943 be97ab03 2023-01-19 thomas
4944 be97ab03 2023-01-19 thomas for (;;) {
4945 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4946 be97ab03 2023-01-19 thomas if (r == 0) {
4947 be97ab03 2023-01-19 thomas if (ferror(src))
4948 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4949 be97ab03 2023-01-19 thomas if (feof(src))
4950 be97ab03 2023-01-19 thomas break;
4951 be97ab03 2023-01-19 thomas }
4952 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4953 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4954 be97ab03 2023-01-19 thomas }
4955 be97ab03 2023-01-19 thomas
4956 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
4957 9382c10a 2023-02-23 thomas return NULL;
4958 9382c10a 2023-02-23 thomas
4959 be97ab03 2023-01-19 thomas /*
4960 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
4961 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
4962 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
4963 be97ab03 2023-01-19 thomas */
4964 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
4965 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
4966 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4967 be97ab03 2023-01-19 thomas
4968 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
4969 9382c10a 2023-02-23 thomas --s_nlines;
4970 9382c10a 2023-02-23 thomas ++s_lines;
4971 9382c10a 2023-02-23 thomas }
4972 9382c10a 2023-02-23 thomas }
4973 be97ab03 2023-01-19 thomas
4974 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4975 be97ab03 2023-01-19 thomas if (p == NULL) {
4976 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4977 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4978 be97ab03 2023-01-19 thomas }
4979 be97ab03 2023-01-19 thomas
4980 be97ab03 2023-01-19 thomas *d_lines = p;
4981 be97ab03 2023-01-19 thomas
4982 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4983 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4984 be97ab03 2023-01-19 thomas
4985 be97ab03 2023-01-19 thomas return NULL;
4986 be97ab03 2023-01-19 thomas }
4987 be97ab03 2023-01-19 thomas
4988 be97ab03 2023-01-19 thomas static const struct got_error *
4989 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4990 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4991 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4992 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4993 fe621944 2020-11-10 stsp {
4994 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4995 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4996 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4997 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4998 45d799e2 2018-12-23 stsp time_t committer_time;
4999 45d799e2 2018-12-23 stsp const char *author, *committer;
5000 8b473291 2019-02-21 stsp char *refs_str = NULL;
5001 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
5002 fe621944 2020-11-10 stsp off_t outoff = 0;
5003 fe621944 2020-11-10 stsp int n;
5004 abd2672a 2018-12-23 stsp
5005 00ddec2f 2023-05-17 thomas err = build_refs_str(&refs_str, refs, commit_id, repo);
5006 00ddec2f 2023-05-17 thomas if (err)
5007 00ddec2f 2023-05-17 thomas return err;
5008 8b473291 2019-02-21 stsp
5009 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5010 abd2672a 2018-12-23 stsp if (err)
5011 abd2672a 2018-12-23 stsp return err;
5012 abd2672a 2018-12-23 stsp
5013 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
5014 15a94983 2018-12-23 stsp if (err) {
5015 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
5016 15a94983 2018-12-23 stsp goto done;
5017 15a94983 2018-12-23 stsp }
5018 abd2672a 2018-12-23 stsp
5019 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
5020 fe621944 2020-11-10 stsp if (err)
5021 fe621944 2020-11-10 stsp goto done;
5022 fe621944 2020-11-10 stsp
5023 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
5024 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
5025 fe621944 2020-11-10 stsp if (n < 0) {
5026 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5027 abd2672a 2018-12-23 stsp goto done;
5028 abd2672a 2018-12-23 stsp }
5029 fe621944 2020-11-10 stsp outoff += n;
5030 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
5031 fe621944 2020-11-10 stsp if (err)
5032 fe621944 2020-11-10 stsp goto done;
5033 fe621944 2020-11-10 stsp
5034 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
5035 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
5036 fe621944 2020-11-10 stsp if (n < 0) {
5037 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
5038 abd2672a 2018-12-23 stsp goto done;
5039 abd2672a 2018-12-23 stsp }
5040 fe621944 2020-11-10 stsp outoff += n;
5041 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
5042 fe621944 2020-11-10 stsp if (err)
5043 fe621944 2020-11-10 stsp goto done;
5044 fe621944 2020-11-10 stsp
5045 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
5046 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
5047 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
5048 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
5049 fe621944 2020-11-10 stsp if (n < 0) {
5050 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5051 fe621944 2020-11-10 stsp goto done;
5052 fe621944 2020-11-10 stsp }
5053 fe621944 2020-11-10 stsp outoff += n;
5054 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5055 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
5056 fe621944 2020-11-10 stsp if (err)
5057 fe621944 2020-11-10 stsp goto done;
5058 abd2672a 2018-12-23 stsp }
5059 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
5060 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
5061 48830929 2023-01-07 thomas if (datestr) {
5062 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
5063 fe621944 2020-11-10 stsp if (n < 0) {
5064 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5065 fe621944 2020-11-10 stsp goto done;
5066 fe621944 2020-11-10 stsp }
5067 fe621944 2020-11-10 stsp outoff += n;
5068 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5069 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
5070 fe621944 2020-11-10 stsp if (err)
5071 fe621944 2020-11-10 stsp goto done;
5072 abd2672a 2018-12-23 stsp }
5073 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
5074 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
5075 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
5076 ac4dc263 2021-09-24 thomas int pn = 1;
5077 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
5078 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
5079 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
5080 ac4dc263 2021-09-24 thomas if (err)
5081 ac4dc263 2021-09-24 thomas goto done;
5082 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
5083 ac4dc263 2021-09-24 thomas if (n < 0) {
5084 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
5085 ac4dc263 2021-09-24 thomas goto done;
5086 ac4dc263 2021-09-24 thomas }
5087 ac4dc263 2021-09-24 thomas outoff += n;
5088 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5089 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
5090 ac4dc263 2021-09-24 thomas if (err)
5091 ac4dc263 2021-09-24 thomas goto done;
5092 ac4dc263 2021-09-24 thomas free(id_str);
5093 ac4dc263 2021-09-24 thomas id_str = NULL;
5094 ac4dc263 2021-09-24 thomas }
5095 ac4dc263 2021-09-24 thomas }
5096 ac4dc263 2021-09-24 thomas
5097 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
5098 5943eee2 2019-08-13 stsp if (err)
5099 5943eee2 2019-08-13 stsp goto done;
5100 fe621944 2020-11-10 stsp s = logmsg;
5101 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5102 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5103 fe621944 2020-11-10 stsp if (n < 0) {
5104 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5105 fe621944 2020-11-10 stsp goto done;
5106 fe621944 2020-11-10 stsp }
5107 fe621944 2020-11-10 stsp outoff += n;
5108 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5109 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5110 fe621944 2020-11-10 stsp if (err)
5111 fe621944 2020-11-10 stsp goto done;
5112 abd2672a 2018-12-23 stsp }
5113 fe621944 2020-11-10 stsp
5114 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5115 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5116 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5117 772fcad5 2023-01-07 thomas
5118 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5119 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5120 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5121 fe621944 2020-11-10 stsp if (n < 0) {
5122 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5123 fe621944 2020-11-10 stsp goto done;
5124 fe621944 2020-11-10 stsp }
5125 fe621944 2020-11-10 stsp outoff += n;
5126 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5127 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5128 fe621944 2020-11-10 stsp if (err)
5129 fe621944 2020-11-10 stsp goto done;
5130 0208f208 2020-05-05 stsp }
5131 fe621944 2020-11-10 stsp
5132 0208f208 2020-05-05 stsp fputc('\n', outfile);
5133 fe621944 2020-11-10 stsp outoff++;
5134 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5135 772fcad5 2023-01-07 thomas if (err)
5136 772fcad5 2023-01-07 thomas goto done;
5137 772fcad5 2023-01-07 thomas
5138 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5139 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5140 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5141 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5142 772fcad5 2023-01-07 thomas if (n < 0) {
5143 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5144 772fcad5 2023-01-07 thomas goto done;
5145 772fcad5 2023-01-07 thomas }
5146 772fcad5 2023-01-07 thomas outoff += n;
5147 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5148 772fcad5 2023-01-07 thomas if (err)
5149 772fcad5 2023-01-07 thomas goto done;
5150 772fcad5 2023-01-07 thomas
5151 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5152 772fcad5 2023-01-07 thomas outoff++;
5153 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5154 abd2672a 2018-12-23 stsp done:
5155 abd2672a 2018-12-23 stsp free(id_str);
5156 5943eee2 2019-08-13 stsp free(logmsg);
5157 8b473291 2019-02-21 stsp free(refs_str);
5158 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5159 7510f233 2020-08-09 stsp if (err) {
5160 82c78e96 2022-08-06 thomas free(*lines);
5161 82c78e96 2022-08-06 thomas *lines = NULL;
5162 ae6a6978 2020-08-09 stsp *nlines = 0;
5163 7510f233 2020-08-09 stsp }
5164 fe621944 2020-11-10 stsp return err;
5165 abd2672a 2018-12-23 stsp }
5166 abd2672a 2018-12-23 stsp
5167 abd2672a 2018-12-23 stsp static const struct got_error *
5168 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5169 26ed57b2 2018-05-19 stsp {
5170 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5171 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5172 15a94983 2018-12-23 stsp int obj_type;
5173 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5174 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5175 fe621944 2020-11-10 stsp
5176 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5177 be97ab03 2023-01-19 thomas
5178 82c78e96 2022-08-06 thomas free(s->lines);
5179 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5180 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5181 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5182 fe621944 2020-11-10 stsp s->nlines = 0;
5183 26ed57b2 2018-05-19 stsp
5184 511a516b 2018-05-19 stsp f = got_opentemp();
5185 48ae06ee 2018-10-18 stsp if (f == NULL) {
5186 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5187 48ae06ee 2018-10-18 stsp goto done;
5188 48ae06ee 2018-10-18 stsp }
5189 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5190 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5191 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5192 be97ab03 2023-01-19 thomas goto done;
5193 be97ab03 2023-01-19 thomas }
5194 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5195 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5196 fb43ecf1 2019-02-11 stsp goto done;
5197 fb43ecf1 2019-02-11 stsp }
5198 48ae06ee 2018-10-18 stsp s->f = f;
5199 26ed57b2 2018-05-19 stsp
5200 15a94983 2018-12-23 stsp if (s->id1)
5201 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5202 15a94983 2018-12-23 stsp else
5203 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5204 15a94983 2018-12-23 stsp if (err)
5205 15a94983 2018-12-23 stsp goto done;
5206 15a94983 2018-12-23 stsp
5207 15a94983 2018-12-23 stsp switch (obj_type) {
5208 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5209 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5210 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5211 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5212 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5213 53d03f97 2023-01-10 thomas s->f);
5214 26ed57b2 2018-05-19 stsp break;
5215 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5216 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5217 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5218 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5219 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5220 26ed57b2 2018-05-19 stsp break;
5221 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5222 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5223 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5224 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5225 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5226 be97ab03 2023-01-19 thomas size_t nlines = 0;
5227 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5228 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5229 be97ab03 2023-01-19 thomas &changed_paths,
5230 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5231 be97ab03 2023-01-19 thomas s->force_text_diff,
5232 be97ab03 2023-01-19 thomas tog_diff_algo
5233 be97ab03 2023-01-19 thomas };
5234 abd2672a 2018-12-23 stsp
5235 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5236 be97ab03 2023-01-19 thomas if (lines == NULL) {
5237 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5238 be97ab03 2023-01-19 thomas goto done;
5239 be97ab03 2023-01-19 thomas }
5240 be97ab03 2023-01-19 thomas
5241 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5242 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5243 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5244 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5245 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5246 be97ab03 2023-01-19 thomas if (err)
5247 be97ab03 2023-01-19 thomas break;
5248 be97ab03 2023-01-19 thomas
5249 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5250 abd2672a 2018-12-23 stsp if (err)
5251 3ffacbe1 2020-02-02 tracey goto done;
5252 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5253 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5254 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5255 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5256 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5257 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5258 f44b1f58 2020-02-02 tracey if (err)
5259 f44b1f58 2020-02-02 tracey goto done;
5260 f44b1f58 2020-02-02 tracey } else {
5261 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5262 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5263 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5264 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5265 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5266 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5267 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5268 f44b1f58 2020-02-02 tracey if (err)
5269 f44b1f58 2020-02-02 tracey goto done;
5270 f5404e4e 2020-02-02 tracey break;
5271 15a087fe 2019-02-21 stsp }
5272 abd2672a 2018-12-23 stsp }
5273 abd2672a 2018-12-23 stsp }
5274 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5275 abd2672a 2018-12-23 stsp
5276 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5277 be97ab03 2023-01-19 thomas lines, nlines);
5278 26ed57b2 2018-05-19 stsp break;
5279 abd2672a 2018-12-23 stsp }
5280 26ed57b2 2018-05-19 stsp default:
5281 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5282 48ae06ee 2018-10-18 stsp break;
5283 26ed57b2 2018-05-19 stsp }
5284 48ae06ee 2018-10-18 stsp done:
5285 be97ab03 2023-01-19 thomas free(lines);
5286 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5287 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5288 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5289 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5290 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5291 48ae06ee 2018-10-18 stsp return err;
5292 48ae06ee 2018-10-18 stsp }
5293 26ed57b2 2018-05-19 stsp
5294 f5215bb9 2019-02-22 stsp static void
5295 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5296 f5215bb9 2019-02-22 stsp {
5297 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5298 f5215bb9 2019-02-22 stsp update_panels();
5299 f5215bb9 2019-02-22 stsp doupdate();
5300 f44b1f58 2020-02-02 tracey }
5301 f44b1f58 2020-02-02 tracey
5302 f44b1f58 2020-02-02 tracey static const struct got_error *
5303 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5304 f44b1f58 2020-02-02 tracey {
5305 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5306 f44b1f58 2020-02-02 tracey
5307 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5308 f44b1f58 2020-02-02 tracey return NULL;
5309 f44b1f58 2020-02-02 tracey }
5310 f44b1f58 2020-02-02 tracey
5311 2a7d3cd7 2022-09-17 thomas static void
5312 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5313 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5314 f44b1f58 2020-02-02 tracey {
5315 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5316 fc2737d5 2022-09-15 thomas
5317 2a7d3cd7 2022-09-17 thomas *f = s->f;
5318 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5319 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5320 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5321 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5322 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5323 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5324 fc2737d5 2022-09-15 thomas }
5325 fc2737d5 2022-09-15 thomas
5326 fc2737d5 2022-09-15 thomas static const struct got_error *
5327 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5328 fc2737d5 2022-09-15 thomas {
5329 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5330 fc2737d5 2022-09-15 thomas FILE *f;
5331 f44b1f58 2020-02-02 tracey int lineno;
5332 78eecffc 2022-06-23 thomas char *line = NULL;
5333 826082fe 2020-12-10 stsp size_t linesize = 0;
5334 826082fe 2020-12-10 stsp ssize_t linelen;
5335 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5336 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5337 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5338 f44b1f58 2020-02-02 tracey
5339 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5340 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5341 2a7d3cd7 2022-09-17 thomas "view search not supported");
5342 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5343 fc2737d5 2022-09-15 thomas &match, &selected);
5344 fc2737d5 2022-09-15 thomas
5345 f44b1f58 2020-02-02 tracey if (!view->searching) {
5346 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5347 f44b1f58 2020-02-02 tracey return NULL;
5348 f44b1f58 2020-02-02 tracey }
5349 f44b1f58 2020-02-02 tracey
5350 fc2737d5 2022-09-15 thomas if (*match) {
5351 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5352 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5353 f44b1f58 2020-02-02 tracey else
5354 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5355 4b3f9dac 2021-12-17 thomas } else
5356 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5357 f44b1f58 2020-02-02 tracey
5358 f44b1f58 2020-02-02 tracey while (1) {
5359 f44b1f58 2020-02-02 tracey off_t offset;
5360 f44b1f58 2020-02-02 tracey
5361 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5362 fc2737d5 2022-09-15 thomas if (*match == 0) {
5363 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5364 f44b1f58 2020-02-02 tracey break;
5365 f44b1f58 2020-02-02 tracey }
5366 f44b1f58 2020-02-02 tracey
5367 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5368 f44b1f58 2020-02-02 tracey lineno = 1;
5369 f44b1f58 2020-02-02 tracey else
5370 fc2737d5 2022-09-15 thomas lineno = nlines;
5371 f44b1f58 2020-02-02 tracey }
5372 f44b1f58 2020-02-02 tracey
5373 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5374 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5375 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5376 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5377 f44b1f58 2020-02-02 tracey free(line);
5378 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5379 f44b1f58 2020-02-02 tracey }
5380 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5381 78eecffc 2022-06-23 thomas if (linelen != -1) {
5382 78eecffc 2022-06-23 thomas char *exstr;
5383 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5384 78eecffc 2022-06-23 thomas if (err)
5385 78eecffc 2022-06-23 thomas break;
5386 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5387 78eecffc 2022-06-23 thomas &view->regmatch)) {
5388 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5389 fc2737d5 2022-09-15 thomas *match = lineno;
5390 78eecffc 2022-06-23 thomas free(exstr);
5391 78eecffc 2022-06-23 thomas break;
5392 78eecffc 2022-06-23 thomas }
5393 78eecffc 2022-06-23 thomas free(exstr);
5394 f44b1f58 2020-02-02 tracey }
5395 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5396 f44b1f58 2020-02-02 tracey lineno++;
5397 f44b1f58 2020-02-02 tracey else
5398 f44b1f58 2020-02-02 tracey lineno--;
5399 f44b1f58 2020-02-02 tracey }
5400 826082fe 2020-12-10 stsp free(line);
5401 f44b1f58 2020-02-02 tracey
5402 fc2737d5 2022-09-15 thomas if (*match) {
5403 fc2737d5 2022-09-15 thomas *first = *match;
5404 fc2737d5 2022-09-15 thomas *selected = 1;
5405 f44b1f58 2020-02-02 tracey }
5406 f44b1f58 2020-02-02 tracey
5407 05171be4 2022-06-23 thomas return err;
5408 f5215bb9 2019-02-22 stsp }
5409 f5215bb9 2019-02-22 stsp
5410 48ae06ee 2018-10-18 stsp static const struct got_error *
5411 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5412 a0f32f33 2022-06-13 thomas {
5413 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5414 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5415 a0f32f33 2022-06-13 thomas
5416 a0f32f33 2022-06-13 thomas free(s->id1);
5417 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5418 a0f32f33 2022-06-13 thomas free(s->id2);
5419 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5420 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5421 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5422 a0f32f33 2022-06-13 thomas s->f = NULL;
5423 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5424 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5425 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5426 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5427 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5428 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5429 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5430 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5431 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5432 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5433 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5434 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5435 82c78e96 2022-08-06 thomas free(s->lines);
5436 82c78e96 2022-08-06 thomas s->lines = NULL;
5437 a0f32f33 2022-06-13 thomas s->nlines = 0;
5438 a0f32f33 2022-06-13 thomas return err;
5439 a0f32f33 2022-06-13 thomas }
5440 a0f32f33 2022-06-13 thomas
5441 a0f32f33 2022-06-13 thomas static const struct got_error *
5442 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5443 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5444 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5445 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5446 48ae06ee 2018-10-18 stsp {
5447 48ae06ee 2018-10-18 stsp const struct got_error *err;
5448 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5449 5dc9f4bc 2018-08-04 stsp
5450 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5451 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5452 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5453 a0f32f33 2022-06-13 thomas
5454 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5455 51b7e1c3 2023-04-14 thomas int type1, type2;
5456 15a94983 2018-12-23 stsp
5457 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5458 51b7e1c3 2023-04-14 thomas if (err)
5459 51b7e1c3 2023-04-14 thomas goto done;
5460 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5461 51b7e1c3 2023-04-14 thomas if (err)
5462 51b7e1c3 2023-04-14 thomas goto done;
5463 51b7e1c3 2023-04-14 thomas
5464 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5465 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5466 51b7e1c3 2023-04-14 thomas goto done;
5467 51b7e1c3 2023-04-14 thomas }
5468 15a94983 2018-12-23 stsp }
5469 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5470 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5471 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5472 f44b1f58 2020-02-02 tracey s->repo = repo;
5473 f44b1f58 2020-02-02 tracey s->id1 = id1;
5474 f44b1f58 2020-02-02 tracey s->id2 = id2;
5475 3dbaef42 2020-11-24 stsp s->label1 = label1;
5476 3dbaef42 2020-11-24 stsp s->label2 = label2;
5477 48ae06ee 2018-10-18 stsp
5478 15a94983 2018-12-23 stsp if (id1) {
5479 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5480 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5481 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5482 51b7e1c3 2023-04-14 thomas goto done;
5483 51b7e1c3 2023-04-14 thomas }
5484 48ae06ee 2018-10-18 stsp } else
5485 5465d566 2020-02-01 tracey s->id1 = NULL;
5486 48ae06ee 2018-10-18 stsp
5487 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5488 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5489 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5490 a0f32f33 2022-06-13 thomas goto done;
5491 48ae06ee 2018-10-18 stsp }
5492 a0f32f33 2022-06-13 thomas
5493 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5494 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5495 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5496 9a1d7435 2022-06-23 thomas goto done;
5497 9a1d7435 2022-06-23 thomas }
5498 9a1d7435 2022-06-23 thomas
5499 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5500 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5501 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5502 a0f32f33 2022-06-13 thomas goto done;
5503 a0f32f33 2022-06-13 thomas }
5504 a0f32f33 2022-06-13 thomas
5505 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5506 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5507 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5508 19a6a6b5 2022-07-01 thomas goto done;
5509 19a6a6b5 2022-07-01 thomas }
5510 19a6a6b5 2022-07-01 thomas
5511 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5512 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5513 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5514 19a6a6b5 2022-07-01 thomas goto done;
5515 19a6a6b5 2022-07-01 thomas }
5516 19a6a6b5 2022-07-01 thomas
5517 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5518 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5519 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5520 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5521 5465d566 2020-02-01 tracey s->repo = repo;
5522 6d17833f 2019-11-08 stsp
5523 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5524 6568f0aa 2022-08-06 thomas int rc;
5525 6d17833f 2019-11-08 stsp
5526 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5527 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5528 6568f0aa 2022-08-06 thomas if (rc != ERR)
5529 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5530 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5531 6568f0aa 2022-08-06 thomas if (rc != ERR)
5532 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5533 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5534 6568f0aa 2022-08-06 thomas if (rc != ERR)
5535 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5536 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5537 6568f0aa 2022-08-06 thomas if (rc != ERR)
5538 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5539 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5540 6568f0aa 2022-08-06 thomas if (rc != ERR)
5541 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5542 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5543 6568f0aa 2022-08-06 thomas if (rc != ERR)
5544 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5545 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5546 6568f0aa 2022-08-06 thomas if (rc != ERR)
5547 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5548 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5549 6568f0aa 2022-08-06 thomas if (rc != ERR)
5550 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5551 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5552 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5553 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5554 a0f32f33 2022-06-13 thomas goto done;
5555 6568f0aa 2022-08-06 thomas }
5556 6d17833f 2019-11-08 stsp }
5557 5dc9f4bc 2018-08-04 stsp
5558 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5559 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5560 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5561 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5562 f5215bb9 2019-02-22 stsp
5563 5465d566 2020-02-01 tracey err = create_diff(s);
5564 48ae06ee 2018-10-18 stsp
5565 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5566 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5567 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5568 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5569 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5570 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5571 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5572 a0f32f33 2022-06-13 thomas done:
5573 51b7e1c3 2023-04-14 thomas if (err) {
5574 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5575 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5576 51b7e1c3 2023-04-14 thomas view_close(view);
5577 51b7e1c3 2023-04-14 thomas }
5578 e5a0f69f 2018-08-18 stsp return err;
5579 5dc9f4bc 2018-08-04 stsp }
5580 5dc9f4bc 2018-08-04 stsp
5581 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5582 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5583 5dc9f4bc 2018-08-04 stsp {
5584 a3404814 2018-09-02 stsp const struct got_error *err;
5585 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5586 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5587 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5588 a3404814 2018-09-02 stsp
5589 a3404814 2018-09-02 stsp if (s->id1) {
5590 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5591 a3404814 2018-09-02 stsp if (err)
5592 a3404814 2018-09-02 stsp return err;
5593 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5594 3dbaef42 2020-11-24 stsp } else
5595 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5596 3dbaef42 2020-11-24 stsp
5597 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5598 a3404814 2018-09-02 stsp if (err)
5599 a3404814 2018-09-02 stsp return err;
5600 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5601 26ed57b2 2018-05-19 stsp
5602 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5603 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5604 a3404814 2018-09-02 stsp free(id_str1);
5605 a3404814 2018-09-02 stsp free(id_str2);
5606 a3404814 2018-09-02 stsp return err;
5607 a3404814 2018-09-02 stsp }
5608 a3404814 2018-09-02 stsp free(id_str1);
5609 a3404814 2018-09-02 stsp free(id_str2);
5610 a3404814 2018-09-02 stsp
5611 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5612 267bb3b8 2021-08-01 stsp free(header);
5613 267bb3b8 2021-08-01 stsp return err;
5614 15a087fe 2019-02-21 stsp }
5615 15a087fe 2019-02-21 stsp
5616 15a087fe 2019-02-21 stsp static const struct got_error *
5617 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5618 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5619 15a087fe 2019-02-21 stsp {
5620 d7a04538 2019-02-21 stsp const struct got_error *err;
5621 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5622 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5623 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5624 15a087fe 2019-02-21 stsp
5625 15a087fe 2019-02-21 stsp free(s->id2);
5626 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5627 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5628 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5629 15a087fe 2019-02-21 stsp
5630 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5631 d7a04538 2019-02-21 stsp if (err)
5632 d7a04538 2019-02-21 stsp return err;
5633 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5634 15a087fe 2019-02-21 stsp free(s->id1);
5635 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5636 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5637 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5638 15a087fe 2019-02-21 stsp return NULL;
5639 0cf4efb1 2018-09-29 stsp }
5640 0cf4efb1 2018-09-29 stsp
5641 0cf4efb1 2018-09-29 stsp static const struct got_error *
5642 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5643 adf4c9e0 2022-07-03 thomas {
5644 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5645 adf4c9e0 2022-07-03 thomas
5646 adf4c9e0 2022-07-03 thomas view->count = 0;
5647 adf4c9e0 2022-07-03 thomas wclear(view->window);
5648 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5649 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5650 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5651 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5652 adf4c9e0 2022-07-03 thomas return create_diff(s);
5653 82c78e96 2022-08-06 thomas }
5654 82c78e96 2022-08-06 thomas
5655 82c78e96 2022-08-06 thomas static void
5656 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5657 82c78e96 2022-08-06 thomas {
5658 82c78e96 2022-08-06 thomas int start, i;
5659 82c78e96 2022-08-06 thomas
5660 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5661 82c78e96 2022-08-06 thomas
5662 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5663 82c78e96 2022-08-06 thomas if (i == 0)
5664 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5665 82c78e96 2022-08-06 thomas if (--i == start)
5666 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5667 82c78e96 2022-08-06 thomas }
5668 82c78e96 2022-08-06 thomas
5669 82c78e96 2022-08-06 thomas s->selected_line = 1;
5670 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5671 adf4c9e0 2022-07-03 thomas }
5672 adf4c9e0 2022-07-03 thomas
5673 82c78e96 2022-08-06 thomas static void
5674 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5675 82c78e96 2022-08-06 thomas {
5676 82c78e96 2022-08-06 thomas int start, i;
5677 82c78e96 2022-08-06 thomas
5678 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5679 82c78e96 2022-08-06 thomas
5680 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5681 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5682 82c78e96 2022-08-06 thomas i = 0;
5683 82c78e96 2022-08-06 thomas if (++i == start)
5684 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5685 82c78e96 2022-08-06 thomas }
5686 82c78e96 2022-08-06 thomas
5687 82c78e96 2022-08-06 thomas s->selected_line = 1;
5688 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5689 82c78e96 2022-08-06 thomas }
5690 82c78e96 2022-08-06 thomas
5691 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5692 4fc71f3b 2022-07-12 thomas int, int, int);
5693 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5694 4fc71f3b 2022-07-12 thomas int, int);
5695 4fc71f3b 2022-07-12 thomas
5696 adf4c9e0 2022-07-03 thomas static const struct got_error *
5697 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5698 e5a0f69f 2018-08-18 stsp {
5699 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5700 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5701 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5702 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5703 826082fe 2020-12-10 stsp char *line = NULL;
5704 826082fe 2020-12-10 stsp size_t linesize = 0;
5705 826082fe 2020-12-10 stsp ssize_t linelen;
5706 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5707 e5a0f69f 2018-08-18 stsp
5708 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5709 82c78e96 2022-08-06 thomas
5710 e5a0f69f 2018-08-18 stsp switch (ch) {
5711 05171be4 2022-06-23 thomas case '0':
5712 05171be4 2022-06-23 thomas case '$':
5713 05171be4 2022-06-23 thomas case KEY_RIGHT:
5714 05171be4 2022-06-23 thomas case 'l':
5715 05171be4 2022-06-23 thomas case KEY_LEFT:
5716 05171be4 2022-06-23 thomas case 'h':
5717 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5718 05171be4 2022-06-23 thomas break;
5719 64453f7e 2020-11-21 stsp case 'a':
5720 3dbaef42 2020-11-24 stsp case 'w':
5721 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5722 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5723 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5724 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5725 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5726 f2d06bef 2023-01-23 thomas }
5727 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5728 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5729 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5730 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5731 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5732 f2d06bef 2023-01-23 thomas }
5733 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5734 912a3f79 2021-08-30 j break;
5735 912a3f79 2021-08-30 j case 'g':
5736 912a3f79 2021-08-30 j case KEY_HOME:
5737 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5738 07b0611c 2022-06-23 thomas view->count = 0;
5739 64453f7e 2020-11-21 stsp break;
5740 912a3f79 2021-08-30 j case 'G':
5741 912a3f79 2021-08-30 j case KEY_END:
5742 07b0611c 2022-06-23 thomas view->count = 0;
5743 912a3f79 2021-08-30 j if (s->eof)
5744 912a3f79 2021-08-30 j break;
5745 912a3f79 2021-08-30 j
5746 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5747 912a3f79 2021-08-30 j s->eof = 1;
5748 912a3f79 2021-08-30 j break;
5749 1e37a5c2 2019-05-12 jcs case 'k':
5750 1e37a5c2 2019-05-12 jcs case KEY_UP:
5751 f7140bf5 2021-10-17 thomas case CTRL('p'):
5752 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5753 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5754 07b0611c 2022-06-23 thomas else
5755 07b0611c 2022-06-23 thomas view->count = 0;
5756 1e37a5c2 2019-05-12 jcs break;
5757 70f17a53 2022-06-13 thomas case CTRL('u'):
5758 23427b14 2022-06-23 thomas case 'u':
5759 70f17a53 2022-06-13 thomas nscroll /= 2;
5760 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5761 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5762 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5763 1c5e5faa 2022-06-23 thomas case 'b':
5764 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5765 07b0611c 2022-06-23 thomas view->count = 0;
5766 26ed57b2 2018-05-19 stsp break;
5767 07b0611c 2022-06-23 thomas }
5768 1e37a5c2 2019-05-12 jcs i = 0;
5769 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5770 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5771 1e37a5c2 2019-05-12 jcs break;
5772 1e37a5c2 2019-05-12 jcs case 'j':
5773 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5774 f7140bf5 2021-10-17 thomas case CTRL('n'):
5775 1e37a5c2 2019-05-12 jcs if (!s->eof)
5776 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5777 07b0611c 2022-06-23 thomas else
5778 07b0611c 2022-06-23 thomas view->count = 0;
5779 1e37a5c2 2019-05-12 jcs break;
5780 70f17a53 2022-06-13 thomas case CTRL('d'):
5781 23427b14 2022-06-23 thomas case 'd':
5782 70f17a53 2022-06-13 thomas nscroll /= 2;
5783 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5784 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5785 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5786 1c5e5faa 2022-06-23 thomas case 'f':
5787 1e37a5c2 2019-05-12 jcs case ' ':
5788 07b0611c 2022-06-23 thomas if (s->eof) {
5789 07b0611c 2022-06-23 thomas view->count = 0;
5790 1e37a5c2 2019-05-12 jcs break;
5791 07b0611c 2022-06-23 thomas }
5792 1e37a5c2 2019-05-12 jcs i = 0;
5793 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5794 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5795 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5796 826082fe 2020-12-10 stsp if (linelen == -1) {
5797 826082fe 2020-12-10 stsp if (feof(s->f)) {
5798 826082fe 2020-12-10 stsp s->eof = 1;
5799 826082fe 2020-12-10 stsp } else
5800 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5801 34bc9ec9 2019-02-22 stsp break;
5802 826082fe 2020-12-10 stsp }
5803 1e37a5c2 2019-05-12 jcs }
5804 826082fe 2020-12-10 stsp free(line);
5805 1e37a5c2 2019-05-12 jcs break;
5806 82c78e96 2022-08-06 thomas case '(':
5807 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5808 82c78e96 2022-08-06 thomas break;
5809 82c78e96 2022-08-06 thomas case ')':
5810 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5811 82c78e96 2022-08-06 thomas break;
5812 82c78e96 2022-08-06 thomas case '{':
5813 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5814 82c78e96 2022-08-06 thomas break;
5815 82c78e96 2022-08-06 thomas case '}':
5816 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5817 82c78e96 2022-08-06 thomas break;
5818 1e37a5c2 2019-05-12 jcs case '[':
5819 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5820 1e37a5c2 2019-05-12 jcs s->diff_context--;
5821 aa61903a 2021-12-31 thomas s->matched_line = 0;
5822 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5823 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5824 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5825 27829c9e 2020-11-21 stsp s->nlines) {
5826 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5827 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5828 27829c9e 2020-11-21 stsp }
5829 07b0611c 2022-06-23 thomas } else
5830 07b0611c 2022-06-23 thomas view->count = 0;
5831 1e37a5c2 2019-05-12 jcs break;
5832 1e37a5c2 2019-05-12 jcs case ']':
5833 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5834 1e37a5c2 2019-05-12 jcs s->diff_context++;
5835 aa61903a 2021-12-31 thomas s->matched_line = 0;
5836 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5837 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5838 07b0611c 2022-06-23 thomas } else
5839 07b0611c 2022-06-23 thomas view->count = 0;
5840 1e37a5c2 2019-05-12 jcs break;
5841 1e37a5c2 2019-05-12 jcs case '<':
5842 1e37a5c2 2019-05-12 jcs case ',':
5843 777aae21 2022-07-20 thomas case 'K':
5844 4fc71f3b 2022-07-12 thomas up = 1;
5845 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5846 4fc71f3b 2022-07-12 thomas case '>':
5847 4fc71f3b 2022-07-12 thomas case '.':
5848 777aae21 2022-07-20 thomas case 'J':
5849 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5850 07b0611c 2022-06-23 thomas view->count = 0;
5851 48ae06ee 2018-10-18 stsp break;
5852 07b0611c 2022-06-23 thomas }
5853 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5854 6524637e 2019-02-21 stsp
5855 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5856 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5857 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5858 15a087fe 2019-02-21 stsp
5859 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5860 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5861 4fc71f3b 2022-07-12 thomas if (err)
5862 4fc71f3b 2022-07-12 thomas break;
5863 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5864 15a087fe 2019-02-21 stsp
5865 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5866 4fc71f3b 2022-07-12 thomas break;
5867 15a087fe 2019-02-21 stsp
5868 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5869 4fc71f3b 2022-07-12 thomas if (err)
5870 4fc71f3b 2022-07-12 thomas break;
5871 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5872 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5873 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5874 5e224a3e 2019-02-22 stsp
5875 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5876 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5877 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5878 4fc71f3b 2022-07-12 thomas
5879 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5880 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5881 4fc71f3b 2022-07-12 thomas if (err)
5882 4fc71f3b 2022-07-12 thomas break;
5883 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5884 5a8b5076 2020-12-05 stsp
5885 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5886 4fc71f3b 2022-07-12 thomas break;
5887 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5888 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5889 4fc71f3b 2022-07-12 thomas bs->selected_line);
5890 4fc71f3b 2022-07-12 thomas if (id == NULL)
5891 4fc71f3b 2022-07-12 thomas break;
5892 15a087fe 2019-02-21 stsp
5893 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5894 4fc71f3b 2022-07-12 thomas break;
5895 15a087fe 2019-02-21 stsp
5896 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5897 4fc71f3b 2022-07-12 thomas if (err)
5898 4fc71f3b 2022-07-12 thomas break;
5899 4fc71f3b 2022-07-12 thomas }
5900 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5901 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5902 aa61903a 2021-12-31 thomas s->matched_line = 0;
5903 05171be4 2022-06-23 thomas view->x = 0;
5904 1e37a5c2 2019-05-12 jcs
5905 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5906 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5907 1e37a5c2 2019-05-12 jcs break;
5908 1e37a5c2 2019-05-12 jcs default:
5909 07b0611c 2022-06-23 thomas view->count = 0;
5910 1e37a5c2 2019-05-12 jcs break;
5911 26ed57b2 2018-05-19 stsp }
5912 e5a0f69f 2018-08-18 stsp
5913 bcbd79e2 2018-08-19 stsp return err;
5914 26ed57b2 2018-05-19 stsp }
5915 26ed57b2 2018-05-19 stsp
5916 4ed7e80c 2018-05-20 stsp static const struct got_error *
5917 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5918 9f7d7167 2018-04-29 stsp {
5919 1d98034b 2023-04-14 thomas const struct got_error *error;
5920 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5921 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5922 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5923 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5924 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5925 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5926 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5927 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5928 3dbaef42 2020-11-24 stsp const char *errstr;
5929 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5930 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5931 70ac5f84 2019-03-28 stsp
5932 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5933 26ed57b2 2018-05-19 stsp switch (ch) {
5934 64453f7e 2020-11-21 stsp case 'a':
5935 64453f7e 2020-11-21 stsp force_text_diff = 1;
5936 3dbaef42 2020-11-24 stsp break;
5937 3dbaef42 2020-11-24 stsp case 'C':
5938 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5939 3dbaef42 2020-11-24 stsp &errstr);
5940 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5941 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5942 8f666e67 2022-02-12 thomas errstr, errstr);
5943 64453f7e 2020-11-21 stsp break;
5944 09b5bff8 2020-02-23 naddy case 'r':
5945 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5946 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5947 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5948 09b5bff8 2020-02-23 naddy optarg);
5949 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5950 09b5bff8 2020-02-23 naddy break;
5951 3dbaef42 2020-11-24 stsp case 'w':
5952 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5953 3dbaef42 2020-11-24 stsp break;
5954 26ed57b2 2018-05-19 stsp default:
5955 17020d27 2019-03-07 stsp usage_diff();
5956 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5957 26ed57b2 2018-05-19 stsp }
5958 26ed57b2 2018-05-19 stsp }
5959 26ed57b2 2018-05-19 stsp
5960 26ed57b2 2018-05-19 stsp argc -= optind;
5961 26ed57b2 2018-05-19 stsp argv += optind;
5962 26ed57b2 2018-05-19 stsp
5963 26ed57b2 2018-05-19 stsp if (argc == 0) {
5964 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5965 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5966 15a94983 2018-12-23 stsp id_str1 = argv[0];
5967 15a94983 2018-12-23 stsp id_str2 = argv[1];
5968 26ed57b2 2018-05-19 stsp } else
5969 26ed57b2 2018-05-19 stsp usage_diff();
5970 eb6600df 2019-01-04 stsp
5971 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5972 7cd52833 2022-06-23 thomas if (error)
5973 7cd52833 2022-06-23 thomas goto done;
5974 7cd52833 2022-06-23 thomas
5975 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5976 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5977 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5978 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5979 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5980 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5981 c156c7a4 2020-12-18 stsp goto done;
5982 a273ac94 2020-02-23 naddy if (worktree)
5983 a273ac94 2020-02-23 naddy repo_path =
5984 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5985 a273ac94 2020-02-23 naddy else
5986 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5987 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5988 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5989 c156c7a4 2020-12-18 stsp goto done;
5990 c156c7a4 2020-12-18 stsp }
5991 a273ac94 2020-02-23 naddy }
5992 a273ac94 2020-02-23 naddy
5993 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5994 eb6600df 2019-01-04 stsp if (error)
5995 eb6600df 2019-01-04 stsp goto done;
5996 26ed57b2 2018-05-19 stsp
5997 557d3365 2023-04-14 thomas init_curses();
5998 a273ac94 2020-02-23 naddy
5999 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6000 51a10b52 2020-12-26 stsp if (error)
6001 51a10b52 2020-12-26 stsp goto done;
6002 51a10b52 2020-12-26 stsp
6003 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6004 26ed57b2 2018-05-19 stsp if (error)
6005 26ed57b2 2018-05-19 stsp goto done;
6006 26ed57b2 2018-05-19 stsp
6007 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
6008 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6009 26ed57b2 2018-05-19 stsp if (error)
6010 26ed57b2 2018-05-19 stsp goto done;
6011 26ed57b2 2018-05-19 stsp
6012 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
6013 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
6014 26ed57b2 2018-05-19 stsp if (error)
6015 26ed57b2 2018-05-19 stsp goto done;
6016 26ed57b2 2018-05-19 stsp
6017 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
6018 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
6019 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6020 ea5e7bb5 2018-08-01 stsp goto done;
6021 ea5e7bb5 2018-08-01 stsp }
6022 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
6023 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
6024 5dc9f4bc 2018-08-04 stsp if (error)
6025 5dc9f4bc 2018-08-04 stsp goto done;
6026 557d3365 2023-04-14 thomas error = view_loop(view);
6027 26ed57b2 2018-05-19 stsp done:
6028 3dbaef42 2020-11-24 stsp free(label1);
6029 3dbaef42 2020-11-24 stsp free(label2);
6030 c02c541e 2019-03-29 stsp free(repo_path);
6031 a273ac94 2020-02-23 naddy free(cwd);
6032 1d0f4054 2021-06-17 stsp if (repo) {
6033 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6034 1d0f4054 2021-06-17 stsp if (error == NULL)
6035 1d0f4054 2021-06-17 stsp error = close_err;
6036 1d0f4054 2021-06-17 stsp }
6037 a273ac94 2020-02-23 naddy if (worktree)
6038 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
6039 7cd52833 2022-06-23 thomas if (pack_fds) {
6040 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6041 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6042 7cd52833 2022-06-23 thomas if (error == NULL)
6043 7cd52833 2022-06-23 thomas error = pack_err;
6044 b85a3496 2023-04-14 thomas }
6045 51a10b52 2020-12-26 stsp tog_free_refs();
6046 26ed57b2 2018-05-19 stsp return error;
6047 9f7d7167 2018-04-29 stsp }
6048 9f7d7167 2018-04-29 stsp
6049 4ed7e80c 2018-05-20 stsp __dead static void
6050 9f7d7167 2018-04-29 stsp usage_blame(void)
6051 9f7d7167 2018-04-29 stsp {
6052 80ddbec8 2018-04-29 stsp endwin();
6053 91198554 2022-06-23 thomas fprintf(stderr,
6054 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
6055 9f7d7167 2018-04-29 stsp getprogname());
6056 9f7d7167 2018-04-29 stsp exit(1);
6057 9f7d7167 2018-04-29 stsp }
6058 84451b3e 2018-07-10 stsp
6059 84451b3e 2018-07-10 stsp struct tog_blame_line {
6060 84451b3e 2018-07-10 stsp int annotated;
6061 84451b3e 2018-07-10 stsp struct got_object_id *id;
6062 84451b3e 2018-07-10 stsp };
6063 9f7d7167 2018-04-29 stsp
6064 4ed7e80c 2018-05-20 stsp static const struct got_error *
6065 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
6066 84451b3e 2018-07-10 stsp {
6067 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6068 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6069 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
6070 84451b3e 2018-07-10 stsp const struct got_error *err;
6071 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
6072 826082fe 2020-12-10 stsp char *line = NULL;
6073 826082fe 2020-12-10 stsp size_t linesize = 0;
6074 826082fe 2020-12-10 stsp ssize_t linelen;
6075 84451b3e 2018-07-10 stsp wchar_t *wline;
6076 27a741e5 2019-09-11 stsp int width;
6077 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
6078 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
6079 ab089a2a 2018-07-12 stsp char *id_str;
6080 11b20872 2019-11-08 stsp struct tog_color *tc;
6081 ab089a2a 2018-07-12 stsp
6082 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
6083 ab089a2a 2018-07-12 stsp if (err)
6084 ab089a2a 2018-07-12 stsp return err;
6085 84451b3e 2018-07-10 stsp
6086 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
6087 f7d12f7e 2018-08-01 stsp werase(view->window);
6088 84451b3e 2018-07-10 stsp
6089 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
6090 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6091 ab089a2a 2018-07-12 stsp free(id_str);
6092 ab089a2a 2018-07-12 stsp return err;
6093 ab089a2a 2018-07-12 stsp }
6094 ab089a2a 2018-07-12 stsp
6095 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6096 ab089a2a 2018-07-12 stsp free(line);
6097 2550e4c3 2018-07-13 stsp line = NULL;
6098 1cae65b4 2019-09-22 stsp if (err)
6099 1cae65b4 2019-09-22 stsp return err;
6100 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6101 a3404814 2018-09-02 stsp wstandout(view->window);
6102 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6103 11b20872 2019-11-08 stsp if (tc)
6104 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6105 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6106 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6107 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6108 11b20872 2019-11-08 stsp if (tc)
6109 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6110 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6111 a3404814 2018-09-02 stsp wstandend(view->window);
6112 2550e4c3 2018-07-13 stsp free(wline);
6113 2550e4c3 2018-07-13 stsp wline = NULL;
6114 ab089a2a 2018-07-12 stsp
6115 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6116 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6117 07dd3ed3 2022-08-06 thomas
6118 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6119 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6120 1134ebde 2023-04-22 thomas int rc;
6121 1134ebde 2023-04-22 thomas
6122 1134ebde 2023-04-22 thomas rc = pthread_cond_wait(&bta->blame_complete, &tog_mutex);
6123 1134ebde 2023-04-22 thomas if (rc)
6124 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_wait");
6125 1134ebde 2023-04-22 thomas tog_io.wait_for_ui = 0;
6126 1134ebde 2023-04-22 thomas }
6127 1134ebde 2023-04-22 thomas
6128 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6129 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6130 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6131 ab089a2a 2018-07-12 stsp free(id_str);
6132 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6133 ab089a2a 2018-07-12 stsp }
6134 ab089a2a 2018-07-12 stsp free(id_str);
6135 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6136 3f60a8ef 2018-07-10 stsp free(line);
6137 2550e4c3 2018-07-13 stsp line = NULL;
6138 3f60a8ef 2018-07-10 stsp if (err)
6139 3f60a8ef 2018-07-10 stsp return err;
6140 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6141 2550e4c3 2018-07-13 stsp free(wline);
6142 2550e4c3 2018-07-13 stsp wline = NULL;
6143 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6144 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6145 3f60a8ef 2018-07-10 stsp
6146 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6147 05171be4 2022-06-23 thomas view->maxx = 0;
6148 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6149 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6150 826082fe 2020-12-10 stsp if (linelen == -1) {
6151 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6152 826082fe 2020-12-10 stsp s->eof = 1;
6153 826082fe 2020-12-10 stsp break;
6154 826082fe 2020-12-10 stsp }
6155 84451b3e 2018-07-10 stsp free(line);
6156 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6157 84451b3e 2018-07-10 stsp }
6158 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6159 07dd3ed3 2022-08-06 thomas continue;
6160 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6161 826082fe 2020-12-10 stsp continue;
6162 cbea3800 2022-06-23 thomas
6163 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6164 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6165 cbea3800 2022-06-23 thomas if (err) {
6166 cbea3800 2022-06-23 thomas free(line);
6167 cbea3800 2022-06-23 thomas return err;
6168 cbea3800 2022-06-23 thomas }
6169 cbea3800 2022-06-23 thomas free(wline);
6170 cbea3800 2022-06-23 thomas wline = NULL;
6171 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6172 84451b3e 2018-07-10 stsp
6173 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6174 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6175 b700b5d6 2018-07-10 stsp
6176 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6177 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6178 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6179 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6180 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6181 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6182 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6183 8d0fe45a 2019-08-12 stsp char *id_str;
6184 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6185 91198554 2022-06-23 thomas blame_line->id);
6186 8d0fe45a 2019-08-12 stsp if (err) {
6187 8d0fe45a 2019-08-12 stsp free(line);
6188 8d0fe45a 2019-08-12 stsp return err;
6189 8d0fe45a 2019-08-12 stsp }
6190 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6191 11b20872 2019-11-08 stsp if (tc)
6192 11b20872 2019-11-08 stsp wattr_on(view->window,
6193 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6194 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6195 11b20872 2019-11-08 stsp if (tc)
6196 11b20872 2019-11-08 stsp wattr_off(view->window,
6197 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6198 8d0fe45a 2019-08-12 stsp free(id_str);
6199 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6200 8d0fe45a 2019-08-12 stsp } else {
6201 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6202 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6203 84451b3e 2018-07-10 stsp }
6204 ee41ec32 2018-07-10 stsp } else {
6205 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6206 ee41ec32 2018-07-10 stsp prev_id = NULL;
6207 ee41ec32 2018-07-10 stsp }
6208 84451b3e 2018-07-10 stsp
6209 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6210 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6211 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6212 27a741e5 2019-09-11 stsp
6213 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6214 41605754 2020-11-12 stsp width = 9;
6215 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6216 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6217 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6218 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6219 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6220 41605754 2020-11-12 stsp if (err) {
6221 41605754 2020-11-12 stsp free(line);
6222 41605754 2020-11-12 stsp return err;
6223 41605754 2020-11-12 stsp }
6224 41605754 2020-11-12 stsp width += 9;
6225 41605754 2020-11-12 stsp } else {
6226 923086fd 2022-06-23 thomas int skip;
6227 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6228 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6229 923086fd 2022-06-23 thomas if (err) {
6230 923086fd 2022-06-23 thomas free(line);
6231 923086fd 2022-06-23 thomas return err;
6232 05171be4 2022-06-23 thomas }
6233 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6234 02bce7e2 2022-06-23 thomas width += 9;
6235 41605754 2020-11-12 stsp free(wline);
6236 41605754 2020-11-12 stsp wline = NULL;
6237 41605754 2020-11-12 stsp }
6238 41605754 2020-11-12 stsp
6239 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6240 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6241 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6242 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6243 84451b3e 2018-07-10 stsp }
6244 826082fe 2020-12-10 stsp free(line);
6245 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6246 84451b3e 2018-07-10 stsp
6247 a5d43cac 2022-07-01 thomas view_border(view);
6248 84451b3e 2018-07-10 stsp
6249 84451b3e 2018-07-10 stsp return NULL;
6250 84451b3e 2018-07-10 stsp }
6251 84451b3e 2018-07-10 stsp
6252 84451b3e 2018-07-10 stsp static const struct got_error *
6253 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6254 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6255 84451b3e 2018-07-10 stsp {
6256 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6257 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6258 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6259 1a76625f 2018-10-22 stsp int errcode;
6260 84451b3e 2018-07-10 stsp
6261 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6262 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6263 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6264 84451b3e 2018-07-10 stsp
6265 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6266 1a76625f 2018-10-22 stsp if (errcode)
6267 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6268 84451b3e 2018-07-10 stsp
6269 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6270 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6271 d68a0a7d 2018-07-10 stsp goto done;
6272 d68a0a7d 2018-07-10 stsp }
6273 d68a0a7d 2018-07-10 stsp
6274 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6275 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6276 d68a0a7d 2018-07-10 stsp
6277 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6278 d68a0a7d 2018-07-10 stsp if (line->annotated)
6279 d68a0a7d 2018-07-10 stsp goto done;
6280 d68a0a7d 2018-07-10 stsp
6281 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6282 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6283 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6284 84451b3e 2018-07-10 stsp goto done;
6285 84451b3e 2018-07-10 stsp }
6286 84451b3e 2018-07-10 stsp line->annotated = 1;
6287 84451b3e 2018-07-10 stsp done:
6288 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6289 1a76625f 2018-10-22 stsp if (errcode)
6290 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6291 84451b3e 2018-07-10 stsp return err;
6292 84451b3e 2018-07-10 stsp }
6293 84451b3e 2018-07-10 stsp
6294 84451b3e 2018-07-10 stsp static void *
6295 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6296 84451b3e 2018-07-10 stsp {
6297 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6298 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6299 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6300 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6301 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6302 00a8878e 2022-07-01 thomas
6303 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6304 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6305 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6306 9117a7b7 2022-07-01 thomas
6307 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6308 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6309 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6310 9117a7b7 2022-07-01 thomas goto done;
6311 9117a7b7 2022-07-01 thomas }
6312 18430de3 2018-07-10 stsp
6313 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6314 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6315 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6316 9117a7b7 2022-07-01 thomas goto done;
6317 9117a7b7 2022-07-01 thomas }
6318 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6319 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6320 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6321 9117a7b7 2022-07-01 thomas goto done;
6322 9117a7b7 2022-07-01 thomas }
6323 9117a7b7 2022-07-01 thomas
6324 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6325 61266923 2020-01-14 stsp if (err)
6326 9117a7b7 2022-07-01 thomas goto done;
6327 61266923 2020-01-14 stsp
6328 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6329 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6330 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6331 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6332 fc06ba56 2019-08-22 stsp err = NULL;
6333 18430de3 2018-07-10 stsp
6334 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6335 9117a7b7 2022-07-01 thomas if (errcode) {
6336 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6337 9117a7b7 2022-07-01 thomas goto done;
6338 9117a7b7 2022-07-01 thomas }
6339 18430de3 2018-07-10 stsp
6340 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6341 1d0f4054 2021-06-17 stsp if (err == NULL)
6342 1d0f4054 2021-06-17 stsp err = close_err;
6343 c9beca56 2018-07-22 stsp ta->repo = NULL;
6344 c9beca56 2018-07-22 stsp *ta->complete = 1;
6345 1134ebde 2023-04-22 thomas
6346 1134ebde 2023-04-22 thomas if (tog_io.wait_for_ui) {
6347 1134ebde 2023-04-22 thomas errcode = pthread_cond_signal(&ta->blame_complete);
6348 1134ebde 2023-04-22 thomas if (errcode && err == NULL)
6349 1134ebde 2023-04-22 thomas err = got_error_set_errno(errcode,
6350 1134ebde 2023-04-22 thomas "pthread_cond_signal");
6351 1134ebde 2023-04-22 thomas }
6352 18430de3 2018-07-10 stsp
6353 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6354 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6355 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6356 18430de3 2018-07-10 stsp
6357 9117a7b7 2022-07-01 thomas done:
6358 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6359 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6360 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6361 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6362 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6363 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6364 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6365 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6366 00a8878e 2022-07-01 thomas
6367 18430de3 2018-07-10 stsp return (void *)err;
6368 84451b3e 2018-07-10 stsp }
6369 84451b3e 2018-07-10 stsp
6370 245d91c1 2018-07-12 stsp static struct got_object_id *
6371 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6372 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6373 245d91c1 2018-07-12 stsp {
6374 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6375 8d0fe45a 2019-08-12 stsp
6376 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6377 8d0fe45a 2019-08-12 stsp return NULL;
6378 b880a918 2018-07-10 stsp
6379 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6380 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6381 4fc71f3b 2022-07-12 thomas return NULL;
6382 4fc71f3b 2022-07-12 thomas
6383 4fc71f3b 2022-07-12 thomas return line->id;
6384 4fc71f3b 2022-07-12 thomas }
6385 4fc71f3b 2022-07-12 thomas
6386 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6387 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6388 4fc71f3b 2022-07-12 thomas int lineno)
6389 4fc71f3b 2022-07-12 thomas {
6390 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6391 4fc71f3b 2022-07-12 thomas
6392 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6393 4fc71f3b 2022-07-12 thomas return NULL;
6394 4fc71f3b 2022-07-12 thomas
6395 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6396 245d91c1 2018-07-12 stsp if (!line->annotated)
6397 245d91c1 2018-07-12 stsp return NULL;
6398 245d91c1 2018-07-12 stsp
6399 245d91c1 2018-07-12 stsp return line->id;
6400 b880a918 2018-07-10 stsp }
6401 245d91c1 2018-07-12 stsp
6402 b880a918 2018-07-10 stsp static const struct got_error *
6403 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6404 a70480e0 2018-06-23 stsp {
6405 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6406 245d91c1 2018-07-12 stsp int i;
6407 245d91c1 2018-07-12 stsp
6408 245d91c1 2018-07-12 stsp if (blame->thread) {
6409 1a76625f 2018-10-22 stsp int errcode;
6410 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6411 1a76625f 2018-10-22 stsp if (errcode)
6412 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6413 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6414 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6415 1a76625f 2018-10-22 stsp if (errcode)
6416 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6417 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6418 1a76625f 2018-10-22 stsp if (errcode)
6419 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6420 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6421 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6422 245d91c1 2018-07-12 stsp err = NULL;
6423 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6424 245d91c1 2018-07-12 stsp }
6425 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6426 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6427 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6428 1d0f4054 2021-06-17 stsp if (err == NULL)
6429 1d0f4054 2021-06-17 stsp err = close_err;
6430 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6431 245d91c1 2018-07-12 stsp }
6432 245d91c1 2018-07-12 stsp if (blame->f) {
6433 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6434 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6435 245d91c1 2018-07-12 stsp blame->f = NULL;
6436 245d91c1 2018-07-12 stsp }
6437 57670559 2018-12-23 stsp if (blame->lines) {
6438 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6439 57670559 2018-12-23 stsp free(blame->lines[i].id);
6440 57670559 2018-12-23 stsp free(blame->lines);
6441 57670559 2018-12-23 stsp blame->lines = NULL;
6442 57670559 2018-12-23 stsp }
6443 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6444 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6445 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6446 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6447 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6448 7cd52833 2022-06-23 thomas if (err == NULL)
6449 7cd52833 2022-06-23 thomas err = pack_err;
6450 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6451 7cd52833 2022-06-23 thomas }
6452 245d91c1 2018-07-12 stsp return err;
6453 245d91c1 2018-07-12 stsp }
6454 245d91c1 2018-07-12 stsp
6455 245d91c1 2018-07-12 stsp static const struct got_error *
6456 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6457 fc06ba56 2019-08-22 stsp {
6458 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6459 fc06ba56 2019-08-22 stsp int *done = arg;
6460 fc06ba56 2019-08-22 stsp int errcode;
6461 fc06ba56 2019-08-22 stsp
6462 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6463 fc06ba56 2019-08-22 stsp if (errcode)
6464 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6465 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6466 fc06ba56 2019-08-22 stsp
6467 fc06ba56 2019-08-22 stsp if (*done)
6468 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6469 fc06ba56 2019-08-22 stsp
6470 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6471 fc06ba56 2019-08-22 stsp if (errcode)
6472 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6473 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6474 fc06ba56 2019-08-22 stsp
6475 fc06ba56 2019-08-22 stsp return err;
6476 fc06ba56 2019-08-22 stsp }
6477 fc06ba56 2019-08-22 stsp
6478 fc06ba56 2019-08-22 stsp static const struct got_error *
6479 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6480 245d91c1 2018-07-12 stsp {
6481 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6482 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6483 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6484 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6485 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6486 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6487 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6488 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6489 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6490 a70480e0 2018-06-23 stsp
6491 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6492 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6493 27d434c2 2018-09-15 stsp if (err)
6494 15a94983 2018-12-23 stsp return err;
6495 f4ae6ddb 2022-07-01 thomas
6496 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6497 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6498 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6499 f4ae6ddb 2022-07-01 thomas goto done;
6500 f4ae6ddb 2022-07-01 thomas }
6501 945f9229 2022-04-16 thomas
6502 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6503 945f9229 2022-04-16 thomas if (err)
6504 945f9229 2022-04-16 thomas goto done;
6505 27d434c2 2018-09-15 stsp
6506 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6507 84451b3e 2018-07-10 stsp if (err)
6508 84451b3e 2018-07-10 stsp goto done;
6509 27d434c2 2018-09-15 stsp
6510 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6511 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6512 84451b3e 2018-07-10 stsp goto done;
6513 84451b3e 2018-07-10 stsp }
6514 a70480e0 2018-06-23 stsp
6515 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6516 a70480e0 2018-06-23 stsp if (err)
6517 a70480e0 2018-06-23 stsp goto done;
6518 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6519 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6520 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6521 84451b3e 2018-07-10 stsp goto done;
6522 84451b3e 2018-07-10 stsp }
6523 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6524 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6525 1fddf795 2021-01-20 stsp if (err)
6526 1fddf795 2021-01-20 stsp goto done;
6527 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6528 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6529 84451b3e 2018-07-10 stsp goto done;
6530 1fddf795 2021-01-20 stsp }
6531 b02560ec 2019-08-19 stsp
6532 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6533 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6534 b02560ec 2019-08-19 stsp blame->nlines--;
6535 a70480e0 2018-06-23 stsp
6536 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6537 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6538 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6539 84451b3e 2018-07-10 stsp goto done;
6540 84451b3e 2018-07-10 stsp }
6541 a70480e0 2018-06-23 stsp
6542 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6543 bd24772e 2018-07-11 stsp if (err)
6544 bd24772e 2018-07-11 stsp goto done;
6545 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6546 7cd52833 2022-06-23 thomas pack_fds);
6547 7cd52833 2022-06-23 thomas if (err)
6548 7cd52833 2022-06-23 thomas goto done;
6549 bd24772e 2018-07-11 stsp
6550 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6551 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6552 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6553 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6554 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6555 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6556 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6557 245d91c1 2018-07-12 stsp goto done;
6558 245d91c1 2018-07-12 stsp }
6559 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6560 245d91c1 2018-07-12 stsp
6561 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6562 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6563 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6564 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6565 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6566 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6567 a5388363 2020-12-01 naddy s->blame_complete = 0;
6568 f5a09613 2020-12-13 naddy
6569 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6570 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6571 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6572 f5a09613 2020-12-13 naddy s->selected_line = 1;
6573 f5a09613 2020-12-13 naddy }
6574 aa61903a 2021-12-31 thomas s->matched_line = 0;
6575 245d91c1 2018-07-12 stsp
6576 245d91c1 2018-07-12 stsp done:
6577 945f9229 2022-04-16 thomas if (commit)
6578 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6579 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6580 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6581 245d91c1 2018-07-12 stsp if (blob)
6582 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6583 27d434c2 2018-09-15 stsp free(obj_id);
6584 245d91c1 2018-07-12 stsp if (err)
6585 245d91c1 2018-07-12 stsp stop_blame(blame);
6586 245d91c1 2018-07-12 stsp return err;
6587 245d91c1 2018-07-12 stsp }
6588 245d91c1 2018-07-12 stsp
6589 245d91c1 2018-07-12 stsp static const struct got_error *
6590 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6591 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6592 245d91c1 2018-07-12 stsp {
6593 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6594 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6595 dbc6a6b6 2018-07-12 stsp
6596 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6597 245d91c1 2018-07-12 stsp
6598 c4843652 2019-08-12 stsp s->path = strdup(path);
6599 c4843652 2019-08-12 stsp if (s->path == NULL)
6600 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6601 c4843652 2019-08-12 stsp
6602 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6603 c4843652 2019-08-12 stsp if (err) {
6604 c4843652 2019-08-12 stsp free(s->path);
6605 7cbe629d 2018-08-04 stsp return err;
6606 c4843652 2019-08-12 stsp }
6607 245d91c1 2018-07-12 stsp
6608 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6609 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6610 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6611 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6612 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6613 fb2756b9 2018-08-04 stsp s->repo = repo;
6614 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6615 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6616 7cbe629d 2018-08-04 stsp
6617 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6618 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6619 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6620 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6621 11b20872 2019-11-08 stsp if (err)
6622 11b20872 2019-11-08 stsp return err;
6623 11b20872 2019-11-08 stsp }
6624 11b20872 2019-11-08 stsp
6625 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6626 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6627 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6628 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6629 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6630 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6631 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6632 1134ebde 2023-04-22 thomas
6633 1134ebde 2023-04-22 thomas if (using_mock_io) {
6634 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6635 1134ebde 2023-04-22 thomas int rc;
6636 1134ebde 2023-04-22 thomas
6637 1134ebde 2023-04-22 thomas rc = pthread_cond_init(&bta->blame_complete, NULL);
6638 1134ebde 2023-04-22 thomas if (rc)
6639 1134ebde 2023-04-22 thomas return got_error_set_errno(rc, "pthread_cond_init");
6640 1134ebde 2023-04-22 thomas }
6641 e5a0f69f 2018-08-18 stsp
6642 a5388363 2020-12-01 naddy return run_blame(view);
6643 7cbe629d 2018-08-04 stsp }
6644 7cbe629d 2018-08-04 stsp
6645 e5a0f69f 2018-08-18 stsp static const struct got_error *
6646 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6647 7cbe629d 2018-08-04 stsp {
6648 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6649 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6650 7cbe629d 2018-08-04 stsp
6651 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6652 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6653 e5a0f69f 2018-08-18 stsp
6654 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6655 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6656 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6657 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6658 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6659 1134ebde 2023-04-22 thomas }
6660 1134ebde 2023-04-22 thomas
6661 1134ebde 2023-04-22 thomas if (using_mock_io) {
6662 1134ebde 2023-04-22 thomas struct tog_blame_thread_args *bta = &s->blame.thread_args;
6663 1134ebde 2023-04-22 thomas int rc;
6664 1134ebde 2023-04-22 thomas
6665 1134ebde 2023-04-22 thomas rc = pthread_cond_destroy(&bta->blame_complete);
6666 1134ebde 2023-04-22 thomas if (rc && err == NULL)
6667 1134ebde 2023-04-22 thomas err = got_error_set_errno(rc, "pthread_cond_destroy");
6668 7cbe629d 2018-08-04 stsp }
6669 e5a0f69f 2018-08-18 stsp
6670 e5a0f69f 2018-08-18 stsp free(s->path);
6671 11b20872 2019-11-08 stsp free_colors(&s->colors);
6672 e5a0f69f 2018-08-18 stsp return err;
6673 7cbe629d 2018-08-04 stsp }
6674 7cbe629d 2018-08-04 stsp
6675 7cbe629d 2018-08-04 stsp static const struct got_error *
6676 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6677 6c4c42e0 2019-06-24 stsp {
6678 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6679 6c4c42e0 2019-06-24 stsp
6680 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6681 6c4c42e0 2019-06-24 stsp return NULL;
6682 2a7d3cd7 2022-09-17 thomas }
6683 2a7d3cd7 2022-09-17 thomas
6684 2a7d3cd7 2022-09-17 thomas static void
6685 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6686 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6687 2a7d3cd7 2022-09-17 thomas {
6688 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6689 2a7d3cd7 2022-09-17 thomas
6690 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6691 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6692 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6693 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6694 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6695 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6696 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6697 6c4c42e0 2019-06-24 stsp }
6698 6c4c42e0 2019-06-24 stsp
6699 6c4c42e0 2019-06-24 stsp static const struct got_error *
6700 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6701 7cbe629d 2018-08-04 stsp {
6702 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6703 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6704 2b380cc8 2018-10-24 stsp int errcode;
6705 2b380cc8 2018-10-24 stsp
6706 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6707 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6708 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6709 2b380cc8 2018-10-24 stsp if (errcode)
6710 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6711 51fe7530 2019-08-19 stsp
6712 557d3365 2023-04-14 thomas if (!using_mock_io)
6713 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6714 2b380cc8 2018-10-24 stsp }
6715 e5a0f69f 2018-08-18 stsp
6716 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6717 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6718 51fe7530 2019-08-19 stsp
6719 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6720 e5a0f69f 2018-08-18 stsp
6721 a5d43cac 2022-07-01 thomas view_border(view);
6722 e5a0f69f 2018-08-18 stsp return err;
6723 e5a0f69f 2018-08-18 stsp }
6724 e5a0f69f 2018-08-18 stsp
6725 e5a0f69f 2018-08-18 stsp static const struct got_error *
6726 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6727 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6728 eaeaa612 2022-07-20 thomas {
6729 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6730 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6731 eaeaa612 2022-07-20 thomas
6732 eaeaa612 2022-07-20 thomas *new_view = NULL;
6733 eaeaa612 2022-07-20 thomas
6734 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6735 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6736 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6737 eaeaa612 2022-07-20 thomas
6738 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6739 eaeaa612 2022-07-20 thomas if (err)
6740 eaeaa612 2022-07-20 thomas view_close(log_view);
6741 eaeaa612 2022-07-20 thomas else
6742 eaeaa612 2022-07-20 thomas *new_view = log_view;
6743 eaeaa612 2022-07-20 thomas
6744 eaeaa612 2022-07-20 thomas return err;
6745 eaeaa612 2022-07-20 thomas }
6746 eaeaa612 2022-07-20 thomas
6747 eaeaa612 2022-07-20 thomas static const struct got_error *
6748 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6749 e5a0f69f 2018-08-18 stsp {
6750 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6751 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6752 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6753 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6754 a5d43cac 2022-07-01 thomas
6755 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6756 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6757 a5d43cac 2022-07-01 thomas --eos; /* border */
6758 7cbe629d 2018-08-04 stsp
6759 e5a0f69f 2018-08-18 stsp switch (ch) {
6760 05171be4 2022-06-23 thomas case '0':
6761 05171be4 2022-06-23 thomas case '$':
6762 05171be4 2022-06-23 thomas case KEY_RIGHT:
6763 05171be4 2022-06-23 thomas case 'l':
6764 05171be4 2022-06-23 thomas case KEY_LEFT:
6765 05171be4 2022-06-23 thomas case 'h':
6766 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6767 05171be4 2022-06-23 thomas break;
6768 1e37a5c2 2019-05-12 jcs case 'q':
6769 1e37a5c2 2019-05-12 jcs s->done = 1;
6770 4deef56f 2021-09-02 naddy break;
6771 4deef56f 2021-09-02 naddy case 'g':
6772 4deef56f 2021-09-02 naddy case KEY_HOME:
6773 4deef56f 2021-09-02 naddy s->selected_line = 1;
6774 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6775 07b0611c 2022-06-23 thomas view->count = 0;
6776 4deef56f 2021-09-02 naddy break;
6777 4deef56f 2021-09-02 naddy case 'G':
6778 4deef56f 2021-09-02 naddy case KEY_END:
6779 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6780 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6781 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6782 4deef56f 2021-09-02 naddy } else {
6783 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6784 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6785 4deef56f 2021-09-02 naddy }
6786 07b0611c 2022-06-23 thomas view->count = 0;
6787 1e37a5c2 2019-05-12 jcs break;
6788 1e37a5c2 2019-05-12 jcs case 'k':
6789 1e37a5c2 2019-05-12 jcs case KEY_UP:
6790 f7140bf5 2021-10-17 thomas case CTRL('p'):
6791 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6792 1e37a5c2 2019-05-12 jcs s->selected_line--;
6793 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6794 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6795 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6796 07b0611c 2022-06-23 thomas else
6797 07b0611c 2022-06-23 thomas view->count = 0;
6798 1e37a5c2 2019-05-12 jcs break;
6799 70f17a53 2022-06-13 thomas case CTRL('u'):
6800 23427b14 2022-06-23 thomas case 'u':
6801 70f17a53 2022-06-13 thomas nscroll /= 2;
6802 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6803 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6804 ea025d1d 2020-02-22 naddy case CTRL('b'):
6805 1c5e5faa 2022-06-23 thomas case 'b':
6806 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6807 07b0611c 2022-06-23 thomas if (view->count > 1)
6808 07b0611c 2022-06-23 thomas nscroll += nscroll;
6809 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6810 07b0611c 2022-06-23 thomas view->count = 0;
6811 e5a0f69f 2018-08-18 stsp break;
6812 1e37a5c2 2019-05-12 jcs }
6813 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6814 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6815 1e37a5c2 2019-05-12 jcs else
6816 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6817 1e37a5c2 2019-05-12 jcs break;
6818 1e37a5c2 2019-05-12 jcs case 'j':
6819 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6820 f7140bf5 2021-10-17 thomas case CTRL('n'):
6821 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6822 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6823 1e37a5c2 2019-05-12 jcs s->selected_line++;
6824 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6825 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6826 07b0611c 2022-06-23 thomas else
6827 07b0611c 2022-06-23 thomas view->count = 0;
6828 1e37a5c2 2019-05-12 jcs break;
6829 1c5e5faa 2022-06-23 thomas case 'c':
6830 1e37a5c2 2019-05-12 jcs case 'p': {
6831 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6832 07b0611c 2022-06-23 thomas
6833 07b0611c 2022-06-23 thomas view->count = 0;
6834 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6835 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6836 1e37a5c2 2019-05-12 jcs if (id == NULL)
6837 e5a0f69f 2018-08-18 stsp break;
6838 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6839 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6840 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6841 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6842 1e37a5c2 2019-05-12 jcs int obj_type;
6843 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6844 1e37a5c2 2019-05-12 jcs s->repo, id);
6845 e5a0f69f 2018-08-18 stsp if (err)
6846 e5a0f69f 2018-08-18 stsp break;
6847 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6848 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6849 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6850 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6851 e5a0f69f 2018-08-18 stsp break;
6852 e5a0f69f 2018-08-18 stsp }
6853 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6854 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6855 ec242592 2022-04-22 thomas s->repo, &pid->id);
6856 945f9229 2022-04-16 thomas if (err)
6857 945f9229 2022-04-16 thomas break;
6858 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6859 945f9229 2022-04-16 thomas pcommit, s->path);
6860 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6861 e5a0f69f 2018-08-18 stsp if (err) {
6862 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6863 1e37a5c2 2019-05-12 jcs err = NULL;
6864 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6865 e5a0f69f 2018-08-18 stsp break;
6866 e5a0f69f 2018-08-18 stsp }
6867 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6868 1e37a5c2 2019-05-12 jcs blob_id);
6869 1e37a5c2 2019-05-12 jcs free(blob_id);
6870 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6871 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6872 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6873 e5a0f69f 2018-08-18 stsp break;
6874 1e37a5c2 2019-05-12 jcs }
6875 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6876 ec242592 2022-04-22 thomas &pid->id);
6877 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6878 1e37a5c2 2019-05-12 jcs } else {
6879 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6880 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6881 1e37a5c2 2019-05-12 jcs break;
6882 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6883 1e37a5c2 2019-05-12 jcs id);
6884 1e37a5c2 2019-05-12 jcs }
6885 1e37a5c2 2019-05-12 jcs if (err)
6886 e5a0f69f 2018-08-18 stsp break;
6887 1e37a5c2 2019-05-12 jcs s->done = 1;
6888 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6889 1e37a5c2 2019-05-12 jcs s->done = 0;
6890 1e37a5c2 2019-05-12 jcs if (thread_err)
6891 1e37a5c2 2019-05-12 jcs break;
6892 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6893 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6894 a5388363 2020-12-01 naddy err = run_blame(view);
6895 1e37a5c2 2019-05-12 jcs if (err)
6896 1e37a5c2 2019-05-12 jcs break;
6897 1e37a5c2 2019-05-12 jcs break;
6898 1e37a5c2 2019-05-12 jcs }
6899 1c5e5faa 2022-06-23 thomas case 'C': {
6900 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6901 07b0611c 2022-06-23 thomas
6902 07b0611c 2022-06-23 thomas view->count = 0;
6903 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6904 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6905 1e37a5c2 2019-05-12 jcs break;
6906 1e37a5c2 2019-05-12 jcs s->done = 1;
6907 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6908 1e37a5c2 2019-05-12 jcs s->done = 0;
6909 1e37a5c2 2019-05-12 jcs if (thread_err)
6910 1e37a5c2 2019-05-12 jcs break;
6911 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6912 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6913 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6914 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6915 a5388363 2020-12-01 naddy err = run_blame(view);
6916 1e37a5c2 2019-05-12 jcs if (err)
6917 1e37a5c2 2019-05-12 jcs break;
6918 1e37a5c2 2019-05-12 jcs break;
6919 1e37a5c2 2019-05-12 jcs }
6920 2a31b33b 2022-07-23 thomas case 'L':
6921 eaeaa612 2022-07-20 thomas view->count = 0;
6922 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6923 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6924 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6925 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6926 eaeaa612 2022-07-20 thomas break;
6927 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6928 1e37a5c2 2019-05-12 jcs case '\r': {
6929 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6930 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6931 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6932 07b0611c 2022-06-23 thomas
6933 07b0611c 2022-06-23 thomas view->count = 0;
6934 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6935 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6936 1e37a5c2 2019-05-12 jcs if (id == NULL)
6937 1e37a5c2 2019-05-12 jcs break;
6938 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6939 1e37a5c2 2019-05-12 jcs if (err)
6940 1e37a5c2 2019-05-12 jcs break;
6941 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6942 4fc71f3b 2022-07-12 thomas if (*new_view) {
6943 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6944 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6945 4fc71f3b 2022-07-12 thomas if (err)
6946 4fc71f3b 2022-07-12 thomas break;
6947 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6948 4fc71f3b 2022-07-12 thomas } else {
6949 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6950 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6951 a5d43cac 2022-07-01 thomas
6952 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6953 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6954 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6955 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6956 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6957 4fc71f3b 2022-07-12 thomas break;
6958 4fc71f3b 2022-07-12 thomas }
6959 15a94983 2018-12-23 stsp }
6960 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6961 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6962 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6963 1e37a5c2 2019-05-12 jcs if (err) {
6964 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6965 1e37a5c2 2019-05-12 jcs break;
6966 1e37a5c2 2019-05-12 jcs }
6967 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6968 4fc71f3b 2022-07-12 thomas s->selected_line;
6969 4fc71f3b 2022-07-12 thomas if (*new_view)
6970 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6971 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6972 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6973 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6974 a5d43cac 2022-07-01 thomas if (err)
6975 a5d43cac 2022-07-01 thomas break;
6976 a5d43cac 2022-07-01 thomas }
6977 a5d43cac 2022-07-01 thomas
6978 e78dc838 2020-12-04 stsp view->focussed = 0;
6979 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6980 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6981 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6982 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6983 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6984 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6985 1e37a5c2 2019-05-12 jcs if (err)
6986 34bc9ec9 2019-02-22 stsp break;
6987 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6988 40236d76 2022-06-23 thomas if (err)
6989 40236d76 2022-06-23 thomas break;
6990 e78dc838 2020-12-04 stsp view->focus_child = 1;
6991 1e37a5c2 2019-05-12 jcs } else
6992 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6993 1e37a5c2 2019-05-12 jcs if (err)
6994 e5a0f69f 2018-08-18 stsp break;
6995 1e37a5c2 2019-05-12 jcs break;
6996 1e37a5c2 2019-05-12 jcs }
6997 70f17a53 2022-06-13 thomas case CTRL('d'):
6998 23427b14 2022-06-23 thomas case 'd':
6999 70f17a53 2022-06-13 thomas nscroll /= 2;
7000 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7001 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7002 ea025d1d 2020-02-22 naddy case CTRL('f'):
7003 1c5e5faa 2022-06-23 thomas case 'f':
7004 1e37a5c2 2019-05-12 jcs case ' ':
7005 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7006 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
7007 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
7008 07b0611c 2022-06-23 thomas view->count = 0;
7009 e5a0f69f 2018-08-18 stsp break;
7010 1e37a5c2 2019-05-12 jcs }
7011 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
7012 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
7013 70f17a53 2022-06-13 thomas s->selected_line +=
7014 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
7015 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
7016 1e37a5c2 2019-05-12 jcs }
7017 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
7018 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
7019 1e37a5c2 2019-05-12 jcs else
7020 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
7021 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
7022 1e37a5c2 2019-05-12 jcs break;
7023 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7024 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
7025 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
7026 1e37a5c2 2019-05-12 jcs view->nlines - 2);
7027 1e37a5c2 2019-05-12 jcs }
7028 1e37a5c2 2019-05-12 jcs break;
7029 1e37a5c2 2019-05-12 jcs default:
7030 07b0611c 2022-06-23 thomas view->count = 0;
7031 1e37a5c2 2019-05-12 jcs break;
7032 a70480e0 2018-06-23 stsp }
7033 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
7034 adf4c9e0 2022-07-03 thomas }
7035 adf4c9e0 2022-07-03 thomas
7036 adf4c9e0 2022-07-03 thomas static const struct got_error *
7037 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
7038 adf4c9e0 2022-07-03 thomas {
7039 adf4c9e0 2022-07-03 thomas const struct got_error *err;
7040 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
7041 adf4c9e0 2022-07-03 thomas
7042 adf4c9e0 2022-07-03 thomas view->count = 0;
7043 adf4c9e0 2022-07-03 thomas s->done = 1;
7044 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
7045 adf4c9e0 2022-07-03 thomas s->done = 0;
7046 adf4c9e0 2022-07-03 thomas if (err)
7047 adf4c9e0 2022-07-03 thomas return err;
7048 adf4c9e0 2022-07-03 thomas return run_blame(view);
7049 a70480e0 2018-06-23 stsp }
7050 a70480e0 2018-06-23 stsp
7051 a70480e0 2018-06-23 stsp static const struct got_error *
7052 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
7053 9f7d7167 2018-04-29 stsp {
7054 1d98034b 2023-04-14 thomas const struct got_error *error;
7055 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
7056 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
7057 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7058 0587e10c 2020-07-23 stsp char *link_target = NULL;
7059 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7060 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7061 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
7062 a70480e0 2018-06-23 stsp int ch;
7063 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
7064 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7065 a70480e0 2018-06-23 stsp
7066 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7067 a70480e0 2018-06-23 stsp switch (ch) {
7068 a70480e0 2018-06-23 stsp case 'c':
7069 a70480e0 2018-06-23 stsp commit_id_str = optarg;
7070 a70480e0 2018-06-23 stsp break;
7071 69069811 2018-08-02 stsp case 'r':
7072 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
7073 69069811 2018-08-02 stsp if (repo_path == NULL)
7074 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7075 9ba1d308 2019-10-21 stsp optarg);
7076 69069811 2018-08-02 stsp break;
7077 a70480e0 2018-06-23 stsp default:
7078 17020d27 2019-03-07 stsp usage_blame();
7079 a70480e0 2018-06-23 stsp /* NOTREACHED */
7080 a70480e0 2018-06-23 stsp }
7081 a70480e0 2018-06-23 stsp }
7082 a70480e0 2018-06-23 stsp
7083 a70480e0 2018-06-23 stsp argc -= optind;
7084 a70480e0 2018-06-23 stsp argv += optind;
7085 a70480e0 2018-06-23 stsp
7086 f135c941 2020-02-20 stsp if (argc != 1)
7087 a70480e0 2018-06-23 stsp usage_blame();
7088 6962eb72 2020-02-20 stsp
7089 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7090 7cd52833 2022-06-23 thomas if (error != NULL)
7091 7cd52833 2022-06-23 thomas goto done;
7092 7cd52833 2022-06-23 thomas
7093 69069811 2018-08-02 stsp if (repo_path == NULL) {
7094 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7095 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7096 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7097 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7098 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7099 c156c7a4 2020-12-18 stsp goto done;
7100 f135c941 2020-02-20 stsp if (worktree)
7101 eb41ed75 2019-02-05 stsp repo_path =
7102 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7103 f135c941 2020-02-20 stsp else
7104 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
7105 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7106 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7107 c156c7a4 2020-12-18 stsp goto done;
7108 c156c7a4 2020-12-18 stsp }
7109 f135c941 2020-02-20 stsp }
7110 a915003a 2019-02-05 stsp
7111 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7112 c02c541e 2019-03-29 stsp if (error != NULL)
7113 8e94dd5b 2019-01-04 stsp goto done;
7114 69069811 2018-08-02 stsp
7115 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
7116 f135c941 2020-02-20 stsp worktree);
7117 c02c541e 2019-03-29 stsp if (error)
7118 92205607 2019-01-04 stsp goto done;
7119 69069811 2018-08-02 stsp
7120 557d3365 2023-04-14 thomas init_curses();
7121 f135c941 2020-02-20 stsp
7122 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7123 51a10b52 2020-12-26 stsp if (error)
7124 51a10b52 2020-12-26 stsp goto done;
7125 51a10b52 2020-12-26 stsp
7126 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7127 eb41ed75 2019-02-05 stsp if (error)
7128 69069811 2018-08-02 stsp goto done;
7129 a70480e0 2018-06-23 stsp
7130 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
7131 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
7132 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7133 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7134 a70480e0 2018-06-23 stsp if (error != NULL)
7135 66b4983c 2018-06-23 stsp goto done;
7136 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7137 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7138 a70480e0 2018-06-23 stsp } else {
7139 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7140 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7141 a70480e0 2018-06-23 stsp }
7142 a19e88aa 2018-06-23 stsp if (error != NULL)
7143 e1cd8fed 2018-08-01 stsp goto done;
7144 0587e10c 2020-07-23 stsp
7145 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7146 945f9229 2022-04-16 thomas if (error)
7147 945f9229 2022-04-16 thomas goto done;
7148 945f9229 2022-04-16 thomas
7149 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7150 945f9229 2022-04-16 thomas commit, repo);
7151 7cbe629d 2018-08-04 stsp if (error)
7152 7cbe629d 2018-08-04 stsp goto done;
7153 0587e10c 2020-07-23 stsp
7154 4334634c 2023-04-22 thomas view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7155 4334634c 2023-04-22 thomas if (view == NULL) {
7156 4334634c 2023-04-22 thomas error = got_error_from_errno("view_open");
7157 4334634c 2023-04-22 thomas goto done;
7158 4334634c 2023-04-22 thomas }
7159 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7160 78756c87 2020-11-24 stsp commit_id, repo);
7161 4334634c 2023-04-22 thomas if (error != NULL) {
7162 4334634c 2023-04-22 thomas if (view->close == NULL)
7163 4334634c 2023-04-22 thomas close_blame_view(view);
7164 4334634c 2023-04-22 thomas view_close(view);
7165 0587e10c 2020-07-23 stsp goto done;
7166 4334634c 2023-04-22 thomas }
7167 12314ad4 2019-08-31 stsp if (worktree) {
7168 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7169 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7170 12314ad4 2019-08-31 stsp worktree = NULL;
7171 12314ad4 2019-08-31 stsp }
7172 557d3365 2023-04-14 thomas error = view_loop(view);
7173 a70480e0 2018-06-23 stsp done:
7174 69069811 2018-08-02 stsp free(repo_path);
7175 f135c941 2020-02-20 stsp free(in_repo_path);
7176 0587e10c 2020-07-23 stsp free(link_target);
7177 69069811 2018-08-02 stsp free(cwd);
7178 a70480e0 2018-06-23 stsp free(commit_id);
7179 945f9229 2022-04-16 thomas if (commit)
7180 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7181 eb41ed75 2019-02-05 stsp if (worktree)
7182 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7183 1d0f4054 2021-06-17 stsp if (repo) {
7184 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7185 1d0f4054 2021-06-17 stsp if (error == NULL)
7186 1d0f4054 2021-06-17 stsp error = close_err;
7187 1d0f4054 2021-06-17 stsp }
7188 7cd52833 2022-06-23 thomas if (pack_fds) {
7189 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7190 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7191 7cd52833 2022-06-23 thomas if (error == NULL)
7192 7cd52833 2022-06-23 thomas error = pack_err;
7193 7cd52833 2022-06-23 thomas }
7194 51a10b52 2020-12-26 stsp tog_free_refs();
7195 a70480e0 2018-06-23 stsp return error;
7196 ffd1d5e5 2018-06-23 stsp }
7197 ffd1d5e5 2018-06-23 stsp
7198 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7199 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7200 ffd1d5e5 2018-06-23 stsp {
7201 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7202 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7203 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7204 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7205 86f4aab9 2022-09-09 thomas char *index = NULL;
7206 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7207 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7208 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7209 ffd1d5e5 2018-06-23 stsp
7210 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7211 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7212 a5d43cac 2022-07-01 thomas --limit; /* border */
7213 ffd1d5e5 2018-06-23 stsp
7214 f7d12f7e 2018-08-01 stsp werase(view->window);
7215 ffd1d5e5 2018-06-23 stsp
7216 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7217 ffd1d5e5 2018-06-23 stsp return NULL;
7218 ffd1d5e5 2018-06-23 stsp
7219 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7220 f91a2b48 2022-06-23 thomas 0, 0);
7221 ffd1d5e5 2018-06-23 stsp if (err)
7222 ffd1d5e5 2018-06-23 stsp return err;
7223 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7224 a3404814 2018-09-02 stsp wstandout(view->window);
7225 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7226 11b20872 2019-11-08 stsp if (tc)
7227 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7228 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7229 86f4aab9 2022-09-09 thomas free(wline);
7230 86f4aab9 2022-09-09 thomas wline = NULL;
7231 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7232 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7233 11b20872 2019-11-08 stsp if (tc)
7234 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7235 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7236 a3404814 2018-09-02 stsp wstandend(view->window);
7237 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7238 86f4aab9 2022-09-09 thomas return NULL;
7239 07dd3ed3 2022-08-06 thomas
7240 6f5f393a 2022-08-12 thomas i += s->selected;
7241 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7242 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7243 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7244 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7245 07dd3ed3 2022-08-06 thomas }
7246 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7247 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7248 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7249 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7250 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7251 86f4aab9 2022-09-09 thomas free(index);
7252 ce52c690 2018-06-23 stsp if (err)
7253 ce52c690 2018-06-23 stsp return err;
7254 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7255 2550e4c3 2018-07-13 stsp free(wline);
7256 2550e4c3 2018-07-13 stsp wline = NULL;
7257 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7258 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7259 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7260 ffd1d5e5 2018-06-23 stsp return NULL;
7261 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7262 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7263 a1eca9bb 2018-06-23 stsp return NULL;
7264 ffd1d5e5 2018-06-23 stsp
7265 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7266 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7267 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7268 0cf4efb1 2018-09-29 stsp if (view->focussed)
7269 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7270 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7271 ffd1d5e5 2018-06-23 stsp }
7272 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7273 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7274 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7275 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7276 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7277 ffd1d5e5 2018-06-23 stsp return NULL;
7278 ffd1d5e5 2018-06-23 stsp n = 1;
7279 ffd1d5e5 2018-06-23 stsp } else {
7280 ffd1d5e5 2018-06-23 stsp n = 0;
7281 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7282 ffd1d5e5 2018-06-23 stsp }
7283 ffd1d5e5 2018-06-23 stsp
7284 c72de8ab 2023-02-03 thomas view->maxx = 0;
7285 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7286 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7287 848d6979 2019-08-12 stsp const char *modestr = "";
7288 56e0773d 2019-11-28 stsp mode_t mode;
7289 1d13200f 2018-07-12 stsp
7290 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7291 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7292 56e0773d 2019-11-28 stsp
7293 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7294 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7295 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7296 1d13200f 2018-07-12 stsp if (err)
7297 638f9024 2019-05-13 stsp return got_error_from_errno(
7298 230a42bd 2019-05-11 jcs "got_object_id_str");
7299 1d13200f 2018-07-12 stsp }
7300 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7301 63c5ca5d 2019-08-24 stsp modestr = "$";
7302 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7303 0d6c6ee3 2020-05-20 stsp int i;
7304 0d6c6ee3 2020-05-20 stsp
7305 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7306 d86d3b18 2020-12-01 naddy te, s->repo);
7307 0d6c6ee3 2020-05-20 stsp if (err) {
7308 0d6c6ee3 2020-05-20 stsp free(id_str);
7309 0d6c6ee3 2020-05-20 stsp return err;
7310 0d6c6ee3 2020-05-20 stsp }
7311 cbb35fac 2023-06-25 thomas for (i = 0; link_target[i] != '\0'; i++) {
7312 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7313 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7314 0d6c6ee3 2020-05-20 stsp }
7315 848d6979 2019-08-12 stsp modestr = "@";
7316 0d6c6ee3 2020-05-20 stsp }
7317 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7318 848d6979 2019-08-12 stsp modestr = "/";
7319 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7320 848d6979 2019-08-12 stsp modestr = "*";
7321 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7322 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7323 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7324 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7325 1d13200f 2018-07-12 stsp free(id_str);
7326 0d6c6ee3 2020-05-20 stsp free(link_target);
7327 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7328 1d13200f 2018-07-12 stsp }
7329 1d13200f 2018-07-12 stsp free(id_str);
7330 0d6c6ee3 2020-05-20 stsp free(link_target);
7331 c72de8ab 2023-02-03 thomas
7332 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7333 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7334 ffd1d5e5 2018-06-23 stsp if (err) {
7335 ffd1d5e5 2018-06-23 stsp free(line);
7336 ffd1d5e5 2018-06-23 stsp break;
7337 ffd1d5e5 2018-06-23 stsp }
7338 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7339 c72de8ab 2023-02-03 thomas free(wline);
7340 c72de8ab 2023-02-03 thomas wline = NULL;
7341 c72de8ab 2023-02-03 thomas
7342 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7343 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7344 c72de8ab 2023-02-03 thomas if (err) {
7345 c72de8ab 2023-02-03 thomas free(line);
7346 c72de8ab 2023-02-03 thomas break;
7347 c72de8ab 2023-02-03 thomas }
7348 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7349 0cf4efb1 2018-09-29 stsp if (view->focussed)
7350 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7351 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7352 ffd1d5e5 2018-06-23 stsp }
7353 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7354 f26dddb7 2019-11-08 stsp if (tc)
7355 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7356 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7357 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7358 f26dddb7 2019-11-08 stsp if (tc)
7359 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7360 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7361 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7362 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7363 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7364 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7365 ffd1d5e5 2018-06-23 stsp free(line);
7366 2550e4c3 2018-07-13 stsp free(wline);
7367 2550e4c3 2018-07-13 stsp wline = NULL;
7368 ffd1d5e5 2018-06-23 stsp n++;
7369 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7370 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7371 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7372 ffd1d5e5 2018-06-23 stsp break;
7373 ffd1d5e5 2018-06-23 stsp }
7374 ffd1d5e5 2018-06-23 stsp
7375 ffd1d5e5 2018-06-23 stsp return err;
7376 ffd1d5e5 2018-06-23 stsp }
7377 ffd1d5e5 2018-06-23 stsp
7378 ffd1d5e5 2018-06-23 stsp static void
7379 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7380 ffd1d5e5 2018-06-23 stsp {
7381 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7382 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7383 fa86c4bf 2020-11-29 stsp int i = 0;
7384 ffd1d5e5 2018-06-23 stsp
7385 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7386 ffd1d5e5 2018-06-23 stsp return;
7387 ffd1d5e5 2018-06-23 stsp
7388 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7389 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7390 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7391 fa86c4bf 2020-11-29 stsp if (!isroot)
7392 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7393 fa86c4bf 2020-11-29 stsp break;
7394 fa86c4bf 2020-11-29 stsp }
7395 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7396 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7397 ffd1d5e5 2018-06-23 stsp }
7398 ffd1d5e5 2018-06-23 stsp }
7399 ffd1d5e5 2018-06-23 stsp
7400 a5d43cac 2022-07-01 thomas static const struct got_error *
7401 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7402 ffd1d5e5 2018-06-23 stsp {
7403 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7404 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7405 ffd1d5e5 2018-06-23 stsp int n = 0;
7406 ffd1d5e5 2018-06-23 stsp
7407 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7408 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7409 694d3271 2020-12-01 naddy s->first_displayed_entry);
7410 694d3271 2020-12-01 naddy else
7411 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7412 56e0773d 2019-11-28 stsp
7413 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7414 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7415 07dd3ed3 2022-08-06 thomas if (last) {
7416 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7417 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7418 07dd3ed3 2022-08-06 thomas }
7419 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7420 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7421 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7422 768394f3 2019-01-24 stsp }
7423 ffd1d5e5 2018-06-23 stsp }
7424 a5d43cac 2022-07-01 thomas
7425 a5d43cac 2022-07-01 thomas return NULL;
7426 ffd1d5e5 2018-06-23 stsp }
7427 ffd1d5e5 2018-06-23 stsp
7428 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7429 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7430 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7431 ffd1d5e5 2018-06-23 stsp {
7432 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7433 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7434 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7435 ffd1d5e5 2018-06-23 stsp
7436 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7437 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7438 56e0773d 2019-11-28 stsp + 1 /* slash */;
7439 ce52c690 2018-06-23 stsp if (te)
7440 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7441 ce52c690 2018-06-23 stsp
7442 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7443 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7444 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7445 ffd1d5e5 2018-06-23 stsp
7446 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7447 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7448 d9765a41 2018-06-23 stsp while (pt) {
7449 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7450 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7451 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7452 cb2ebc8a 2018-06-23 stsp goto done;
7453 cb2ebc8a 2018-06-23 stsp }
7454 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7455 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7456 cb2ebc8a 2018-06-23 stsp goto done;
7457 cb2ebc8a 2018-06-23 stsp }
7458 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7459 ffd1d5e5 2018-06-23 stsp }
7460 ce52c690 2018-06-23 stsp if (te) {
7461 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7462 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7463 ce52c690 2018-06-23 stsp goto done;
7464 ce52c690 2018-06-23 stsp }
7465 cb2ebc8a 2018-06-23 stsp }
7466 ce52c690 2018-06-23 stsp done:
7467 ce52c690 2018-06-23 stsp if (err) {
7468 ce52c690 2018-06-23 stsp free(*path);
7469 ce52c690 2018-06-23 stsp *path = NULL;
7470 ce52c690 2018-06-23 stsp }
7471 ce52c690 2018-06-23 stsp return err;
7472 ce52c690 2018-06-23 stsp }
7473 ce52c690 2018-06-23 stsp
7474 ce52c690 2018-06-23 stsp static const struct got_error *
7475 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7476 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7477 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7478 ce52c690 2018-06-23 stsp {
7479 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7480 ce52c690 2018-06-23 stsp char *path;
7481 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7482 a0de39f3 2019-08-09 stsp
7483 a0de39f3 2019-08-09 stsp *new_view = NULL;
7484 69efd4c4 2018-07-18 stsp
7485 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7486 ce52c690 2018-06-23 stsp if (err)
7487 ce52c690 2018-06-23 stsp return err;
7488 ffd1d5e5 2018-06-23 stsp
7489 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7490 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7491 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7492 83ce39e3 2019-08-12 stsp goto done;
7493 83ce39e3 2019-08-12 stsp }
7494 cdf1ee82 2018-08-01 stsp
7495 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7496 e5a0f69f 2018-08-18 stsp if (err) {
7497 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7498 fc06ba56 2019-08-22 stsp err = NULL;
7499 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7500 e5a0f69f 2018-08-18 stsp } else
7501 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7502 83ce39e3 2019-08-12 stsp done:
7503 83ce39e3 2019-08-12 stsp free(path);
7504 69efd4c4 2018-07-18 stsp return err;
7505 69efd4c4 2018-07-18 stsp }
7506 69efd4c4 2018-07-18 stsp
7507 69efd4c4 2018-07-18 stsp static const struct got_error *
7508 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7509 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7510 69efd4c4 2018-07-18 stsp {
7511 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7512 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7513 69efd4c4 2018-07-18 stsp char *path;
7514 a0de39f3 2019-08-09 stsp
7515 a0de39f3 2019-08-09 stsp *new_view = NULL;
7516 69efd4c4 2018-07-18 stsp
7517 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7518 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7519 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7520 e5a0f69f 2018-08-18 stsp
7521 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7522 69efd4c4 2018-07-18 stsp if (err)
7523 69efd4c4 2018-07-18 stsp return err;
7524 69efd4c4 2018-07-18 stsp
7525 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7526 4e97c21c 2020-12-06 stsp path, 0);
7527 ba4f502b 2018-08-04 stsp if (err)
7528 e5a0f69f 2018-08-18 stsp view_close(log_view);
7529 e5a0f69f 2018-08-18 stsp else
7530 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7531 cb2ebc8a 2018-06-23 stsp free(path);
7532 cb2ebc8a 2018-06-23 stsp return err;
7533 ffd1d5e5 2018-06-23 stsp }
7534 ffd1d5e5 2018-06-23 stsp
7535 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7536 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7537 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7538 ffd1d5e5 2018-06-23 stsp {
7539 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7540 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7541 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7542 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7543 ffd1d5e5 2018-06-23 stsp
7544 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7545 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7546 bc573f3b 2021-07-10 stsp
7547 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7548 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7549 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7550 51b7e1c3 2023-04-14 thomas goto done;
7551 51b7e1c3 2023-04-14 thomas }
7552 ffd1d5e5 2018-06-23 stsp
7553 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7554 bc573f3b 2021-07-10 stsp if (err)
7555 bc573f3b 2021-07-10 stsp goto done;
7556 bc573f3b 2021-07-10 stsp
7557 bc573f3b 2021-07-10 stsp /*
7558 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7559 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7560 bc573f3b 2021-07-10 stsp * closed on demand.
7561 bc573f3b 2021-07-10 stsp */
7562 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7563 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7564 bc573f3b 2021-07-10 stsp if (err)
7565 bc573f3b 2021-07-10 stsp goto done;
7566 bc573f3b 2021-07-10 stsp s->tree = s->root;
7567 bc573f3b 2021-07-10 stsp
7568 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7569 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7570 ffd1d5e5 2018-06-23 stsp goto done;
7571 ffd1d5e5 2018-06-23 stsp
7572 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7573 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7574 ffd1d5e5 2018-06-23 stsp goto done;
7575 ffd1d5e5 2018-06-23 stsp }
7576 ffd1d5e5 2018-06-23 stsp
7577 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7578 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7579 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7580 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7581 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7582 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7583 9cd7cbd1 2020-12-07 stsp goto done;
7584 9cd7cbd1 2020-12-07 stsp }
7585 9cd7cbd1 2020-12-07 stsp }
7586 fb2756b9 2018-08-04 stsp s->repo = repo;
7587 c0b01bdb 2019-11-08 stsp
7588 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7589 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7590 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7591 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7592 c0b01bdb 2019-11-08 stsp if (err)
7593 c0b01bdb 2019-11-08 stsp goto done;
7594 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7595 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7596 bc573f3b 2021-07-10 stsp if (err)
7597 c0b01bdb 2019-11-08 stsp goto done;
7598 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7599 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7600 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7601 bc573f3b 2021-07-10 stsp if (err)
7602 c0b01bdb 2019-11-08 stsp goto done;
7603 e5a0f69f 2018-08-18 stsp
7604 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7605 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7606 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7607 bc573f3b 2021-07-10 stsp if (err)
7608 11b20872 2019-11-08 stsp goto done;
7609 11b20872 2019-11-08 stsp
7610 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7611 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7612 bc573f3b 2021-07-10 stsp if (err)
7613 c0b01bdb 2019-11-08 stsp goto done;
7614 c0b01bdb 2019-11-08 stsp }
7615 c0b01bdb 2019-11-08 stsp
7616 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7617 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7618 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7619 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7620 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7621 ad80ab7b 2018-08-04 stsp done:
7622 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7623 bc573f3b 2021-07-10 stsp if (commit)
7624 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7625 51b7e1c3 2023-04-14 thomas if (err) {
7626 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7627 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7628 51b7e1c3 2023-04-14 thomas view_close(view);
7629 51b7e1c3 2023-04-14 thomas }
7630 ad80ab7b 2018-08-04 stsp return err;
7631 ad80ab7b 2018-08-04 stsp }
7632 ad80ab7b 2018-08-04 stsp
7633 e5a0f69f 2018-08-18 stsp static const struct got_error *
7634 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7635 ad80ab7b 2018-08-04 stsp {
7636 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7637 ad80ab7b 2018-08-04 stsp
7638 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7639 fb2756b9 2018-08-04 stsp free(s->tree_label);
7640 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7641 6484ec90 2018-09-29 stsp free(s->commit_id);
7642 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7643 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7644 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7645 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7646 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7647 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7648 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7649 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7650 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7651 ad80ab7b 2018-08-04 stsp free(parent);
7652 ad80ab7b 2018-08-04 stsp
7653 ad80ab7b 2018-08-04 stsp }
7654 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7655 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7656 bc573f3b 2021-07-10 stsp if (s->root)
7657 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7658 7c32bd05 2019-06-22 stsp return NULL;
7659 7c32bd05 2019-06-22 stsp }
7660 7c32bd05 2019-06-22 stsp
7661 7c32bd05 2019-06-22 stsp static const struct got_error *
7662 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7663 7c32bd05 2019-06-22 stsp {
7664 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7665 7c32bd05 2019-06-22 stsp
7666 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7667 7c32bd05 2019-06-22 stsp return NULL;
7668 7c32bd05 2019-06-22 stsp }
7669 7c32bd05 2019-06-22 stsp
7670 7c32bd05 2019-06-22 stsp static int
7671 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7672 7c32bd05 2019-06-22 stsp {
7673 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7674 7c32bd05 2019-06-22 stsp
7675 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7676 56e0773d 2019-11-28 stsp 0) == 0;
7677 7c32bd05 2019-06-22 stsp }
7678 7c32bd05 2019-06-22 stsp
7679 7c32bd05 2019-06-22 stsp static const struct got_error *
7680 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7681 7c32bd05 2019-06-22 stsp {
7682 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7683 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7684 7c32bd05 2019-06-22 stsp
7685 7c32bd05 2019-06-22 stsp if (!view->searching) {
7686 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7687 7c32bd05 2019-06-22 stsp return NULL;
7688 7c32bd05 2019-06-22 stsp }
7689 7c32bd05 2019-06-22 stsp
7690 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7691 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7692 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7693 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7694 56e0773d 2019-11-28 stsp s->selected_entry);
7695 7c32bd05 2019-06-22 stsp else
7696 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7697 56e0773d 2019-11-28 stsp } else {
7698 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7699 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7700 56e0773d 2019-11-28 stsp else
7701 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7702 56e0773d 2019-11-28 stsp s->selected_entry);
7703 7c32bd05 2019-06-22 stsp }
7704 7c32bd05 2019-06-22 stsp } else {
7705 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7706 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7707 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7708 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7709 56e0773d 2019-11-28 stsp else
7710 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7711 7c32bd05 2019-06-22 stsp }
7712 7c32bd05 2019-06-22 stsp
7713 7c32bd05 2019-06-22 stsp while (1) {
7714 56e0773d 2019-11-28 stsp if (te == NULL) {
7715 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7716 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7717 ac66afb8 2019-06-24 stsp return NULL;
7718 ac66afb8 2019-06-24 stsp }
7719 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7720 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7721 56e0773d 2019-11-28 stsp else
7722 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7723 7c32bd05 2019-06-22 stsp }
7724 7c32bd05 2019-06-22 stsp
7725 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7726 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7727 56e0773d 2019-11-28 stsp s->matched_entry = te;
7728 7c32bd05 2019-06-22 stsp break;
7729 7c32bd05 2019-06-22 stsp }
7730 7c32bd05 2019-06-22 stsp
7731 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7732 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7733 56e0773d 2019-11-28 stsp else
7734 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7735 7c32bd05 2019-06-22 stsp }
7736 e5a0f69f 2018-08-18 stsp
7737 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7738 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7739 7c32bd05 2019-06-22 stsp s->selected = 0;
7740 7c32bd05 2019-06-22 stsp }
7741 7c32bd05 2019-06-22 stsp
7742 e5a0f69f 2018-08-18 stsp return NULL;
7743 ad80ab7b 2018-08-04 stsp }
7744 ad80ab7b 2018-08-04 stsp
7745 ad80ab7b 2018-08-04 stsp static const struct got_error *
7746 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7747 ad80ab7b 2018-08-04 stsp {
7748 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7749 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7750 e5a0f69f 2018-08-18 stsp char *parent_path;
7751 ad80ab7b 2018-08-04 stsp
7752 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7753 e5a0f69f 2018-08-18 stsp if (err)
7754 e5a0f69f 2018-08-18 stsp return err;
7755 ffd1d5e5 2018-06-23 stsp
7756 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7757 e5a0f69f 2018-08-18 stsp free(parent_path);
7758 669b5ffa 2018-10-07 stsp
7759 a5d43cac 2022-07-01 thomas view_border(view);
7760 e5a0f69f 2018-08-18 stsp return err;
7761 07dd3ed3 2022-08-06 thomas }
7762 07dd3ed3 2022-08-06 thomas
7763 07dd3ed3 2022-08-06 thomas static const struct got_error *
7764 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7765 07dd3ed3 2022-08-06 thomas {
7766 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7767 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7768 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7769 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7770 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7771 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7772 07dd3ed3 2022-08-06 thomas
7773 07dd3ed3 2022-08-06 thomas g = view->gline;
7774 07dd3ed3 2022-08-06 thomas view->gline = 0;
7775 07dd3ed3 2022-08-06 thomas
7776 07dd3ed3 2022-08-06 thomas if (g == 0)
7777 07dd3ed3 2022-08-06 thomas g = 1;
7778 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7779 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7780 07dd3ed3 2022-08-06 thomas
7781 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7782 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7783 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7784 07dd3ed3 2022-08-06 thomas
7785 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7786 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7787 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7788 07dd3ed3 2022-08-06 thomas }
7789 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7790 07dd3ed3 2022-08-06 thomas last += off;
7791 07dd3ed3 2022-08-06 thomas
7792 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7793 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7794 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7795 07dd3ed3 2022-08-06 thomas }
7796 07dd3ed3 2022-08-06 thomas
7797 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7798 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7799 07dd3ed3 2022-08-06 thomas i += off;
7800 07dd3ed3 2022-08-06 thomas }
7801 07dd3ed3 2022-08-06 thomas
7802 07dd3ed3 2022-08-06 thomas if (i < g) {
7803 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7804 07dd3ed3 2022-08-06 thomas if (err)
7805 07dd3ed3 2022-08-06 thomas return err;
7806 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7807 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7808 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7809 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7810 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7811 07dd3ed3 2022-08-06 thomas first += off;
7812 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7813 07dd3ed3 2022-08-06 thomas }
7814 07dd3ed3 2022-08-06 thomas } else if (i > g)
7815 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7816 07dd3ed3 2022-08-06 thomas
7817 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7818 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7819 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7820 07dd3ed3 2022-08-06 thomas
7821 07dd3ed3 2022-08-06 thomas return NULL;
7822 e5a0f69f 2018-08-18 stsp }
7823 ce52c690 2018-06-23 stsp
7824 e5a0f69f 2018-08-18 stsp static const struct got_error *
7825 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7826 e5a0f69f 2018-08-18 stsp {
7827 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7828 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7829 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7830 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7831 ffd1d5e5 2018-06-23 stsp
7832 07dd3ed3 2022-08-06 thomas if (view->gline)
7833 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7834 07dd3ed3 2022-08-06 thomas
7835 e5a0f69f 2018-08-18 stsp switch (ch) {
7836 c72de8ab 2023-02-03 thomas case '0':
7837 c72de8ab 2023-02-03 thomas case '$':
7838 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7839 c72de8ab 2023-02-03 thomas case 'l':
7840 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7841 c72de8ab 2023-02-03 thomas case 'h':
7842 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7843 c72de8ab 2023-02-03 thomas break;
7844 1e37a5c2 2019-05-12 jcs case 'i':
7845 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7846 07b0611c 2022-06-23 thomas view->count = 0;
7847 1e37a5c2 2019-05-12 jcs break;
7848 1be4947a 2022-07-22 thomas case 'L':
7849 07b0611c 2022-06-23 thomas view->count = 0;
7850 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7851 ffd1d5e5 2018-06-23 stsp break;
7852 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7853 152c1c93 2020-11-29 stsp break;
7854 1be4947a 2022-07-22 thomas case 'R':
7855 07b0611c 2022-06-23 thomas view->count = 0;
7856 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7857 e4526bf5 2021-09-03 naddy break;
7858 e4526bf5 2021-09-03 naddy case 'g':
7859 aa7a1117 2023-01-09 thomas case '=':
7860 e4526bf5 2021-09-03 naddy case KEY_HOME:
7861 e4526bf5 2021-09-03 naddy s->selected = 0;
7862 07b0611c 2022-06-23 thomas view->count = 0;
7863 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7864 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7865 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7866 e4526bf5 2021-09-03 naddy else
7867 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7868 1e37a5c2 2019-05-12 jcs break;
7869 e4526bf5 2021-09-03 naddy case 'G':
7870 aa7a1117 2023-01-09 thomas case '*':
7871 a5d43cac 2022-07-01 thomas case KEY_END: {
7872 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7873 a5d43cac 2022-07-01 thomas
7874 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7875 a5d43cac 2022-07-01 thomas --eos; /* border */
7876 e4526bf5 2021-09-03 naddy s->selected = 0;
7877 07b0611c 2022-06-23 thomas view->count = 0;
7878 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7879 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7880 e4526bf5 2021-09-03 naddy if (te == NULL) {
7881 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7882 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7883 e4526bf5 2021-09-03 naddy n++;
7884 e4526bf5 2021-09-03 naddy }
7885 e4526bf5 2021-09-03 naddy break;
7886 e4526bf5 2021-09-03 naddy }
7887 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7888 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7889 e4526bf5 2021-09-03 naddy }
7890 e4526bf5 2021-09-03 naddy if (n > 0)
7891 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7892 e4526bf5 2021-09-03 naddy break;
7893 a5d43cac 2022-07-01 thomas }
7894 1e37a5c2 2019-05-12 jcs case 'k':
7895 1e37a5c2 2019-05-12 jcs case KEY_UP:
7896 f7140bf5 2021-10-17 thomas case CTRL('p'):
7897 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7898 1e37a5c2 2019-05-12 jcs s->selected--;
7899 fa86c4bf 2020-11-29 stsp break;
7900 1e37a5c2 2019-05-12 jcs }
7901 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7902 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7903 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7904 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7905 07b0611c 2022-06-23 thomas view->count = 0;
7906 1e37a5c2 2019-05-12 jcs break;
7907 70f17a53 2022-06-13 thomas case CTRL('u'):
7908 23427b14 2022-06-23 thomas case 'u':
7909 70f17a53 2022-06-13 thomas nscroll /= 2;
7910 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7911 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7912 ea025d1d 2020-02-22 naddy case CTRL('b'):
7913 1c5e5faa 2022-06-23 thomas case 'b':
7914 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7915 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7916 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7917 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7918 fa86c4bf 2020-11-29 stsp } else {
7919 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7920 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7921 fa86c4bf 2020-11-29 stsp }
7922 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7923 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7924 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7925 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7926 07b0611c 2022-06-23 thomas view->count = 0;
7927 1e37a5c2 2019-05-12 jcs break;
7928 1e37a5c2 2019-05-12 jcs case 'j':
7929 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7930 f7140bf5 2021-10-17 thomas case CTRL('n'):
7931 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7932 1e37a5c2 2019-05-12 jcs s->selected++;
7933 1e37a5c2 2019-05-12 jcs break;
7934 1e37a5c2 2019-05-12 jcs }
7935 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7936 07b0611c 2022-06-23 thomas == NULL) {
7937 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7938 07b0611c 2022-06-23 thomas view->count = 0;
7939 1e37a5c2 2019-05-12 jcs break;
7940 07b0611c 2022-06-23 thomas }
7941 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7942 1e37a5c2 2019-05-12 jcs break;
7943 70f17a53 2022-06-13 thomas case CTRL('d'):
7944 23427b14 2022-06-23 thomas case 'd':
7945 70f17a53 2022-06-13 thomas nscroll /= 2;
7946 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7947 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7948 ea025d1d 2020-02-22 naddy case CTRL('f'):
7949 1c5e5faa 2022-06-23 thomas case 'f':
7950 4c2d69cb 2022-06-23 thomas case ' ':
7951 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7952 1e37a5c2 2019-05-12 jcs == NULL) {
7953 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7954 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7955 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7956 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7957 07b0611c 2022-06-23 thomas else
7958 07b0611c 2022-06-23 thomas view->count = 0;
7959 1e37a5c2 2019-05-12 jcs break;
7960 1e37a5c2 2019-05-12 jcs }
7961 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7962 1e37a5c2 2019-05-12 jcs break;
7963 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7964 1e37a5c2 2019-05-12 jcs case '\r':
7965 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7966 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7967 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7968 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7969 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7970 07b0611c 2022-06-23 thomas view->count = 0;
7971 1e37a5c2 2019-05-12 jcs break;
7972 07b0611c 2022-06-23 thomas }
7973 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7974 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7975 1e37a5c2 2019-05-12 jcs entry);
7976 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7977 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7978 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7979 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7980 1e37a5c2 2019-05-12 jcs s->selected_entry =
7981 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7982 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7983 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7984 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7985 a5d43cac 2022-07-01 thomas if (err)
7986 a5d43cac 2022-07-01 thomas break;
7987 a5d43cac 2022-07-01 thomas }
7988 1e37a5c2 2019-05-12 jcs free(parent);
7989 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7990 56e0773d 2019-11-28 stsp s->selected_entry))) {
7991 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7992 07b0611c 2022-06-23 thomas view->count = 0;
7993 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7994 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7995 1e37a5c2 2019-05-12 jcs if (err)
7996 1e37a5c2 2019-05-12 jcs break;
7997 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7998 941e9f74 2019-05-21 stsp if (err) {
7999 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
8000 1e37a5c2 2019-05-12 jcs break;
8001 1e37a5c2 2019-05-12 jcs }
8002 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
8003 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
8004 1e37a5c2 2019-05-12 jcs break;
8005 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
8006 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
8007 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
8008 07b0611c 2022-06-23 thomas view->count = 0;
8009 1e37a5c2 2019-05-12 jcs break;
8010 1e37a5c2 2019-05-12 jcs default:
8011 07b0611c 2022-06-23 thomas view->count = 0;
8012 1e37a5c2 2019-05-12 jcs break;
8013 ffd1d5e5 2018-06-23 stsp }
8014 e5a0f69f 2018-08-18 stsp
8015 ffd1d5e5 2018-06-23 stsp return err;
8016 9f7d7167 2018-04-29 stsp }
8017 9f7d7167 2018-04-29 stsp
8018 ffd1d5e5 2018-06-23 stsp __dead static void
8019 ffd1d5e5 2018-06-23 stsp usage_tree(void)
8020 ffd1d5e5 2018-06-23 stsp {
8021 ffd1d5e5 2018-06-23 stsp endwin();
8022 91198554 2022-06-23 thomas fprintf(stderr,
8023 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
8024 ffd1d5e5 2018-06-23 stsp getprogname());
8025 ffd1d5e5 2018-06-23 stsp exit(1);
8026 ffd1d5e5 2018-06-23 stsp }
8027 ffd1d5e5 2018-06-23 stsp
8028 ffd1d5e5 2018-06-23 stsp static const struct got_error *
8029 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
8030 ffd1d5e5 2018-06-23 stsp {
8031 1d98034b 2023-04-14 thomas const struct got_error *error;
8032 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
8033 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
8034 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8035 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
8036 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8037 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
8038 4e97c21c 2020-12-06 stsp char *label = NULL;
8039 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
8040 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
8041 ffd1d5e5 2018-06-23 stsp int ch;
8042 5221c383 2018-08-01 stsp struct tog_view *view;
8043 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8044 70ac5f84 2019-03-28 stsp
8045 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
8046 ffd1d5e5 2018-06-23 stsp switch (ch) {
8047 ffd1d5e5 2018-06-23 stsp case 'c':
8048 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
8049 74283ab8 2020-02-07 stsp break;
8050 74283ab8 2020-02-07 stsp case 'r':
8051 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
8052 74283ab8 2020-02-07 stsp if (repo_path == NULL)
8053 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
8054 74283ab8 2020-02-07 stsp optarg);
8055 ffd1d5e5 2018-06-23 stsp break;
8056 ffd1d5e5 2018-06-23 stsp default:
8057 e99e2d15 2020-11-24 naddy usage_tree();
8058 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
8059 ffd1d5e5 2018-06-23 stsp }
8060 ffd1d5e5 2018-06-23 stsp }
8061 ffd1d5e5 2018-06-23 stsp
8062 ffd1d5e5 2018-06-23 stsp argc -= optind;
8063 ffd1d5e5 2018-06-23 stsp argv += optind;
8064 ffd1d5e5 2018-06-23 stsp
8065 55cccc34 2020-02-20 stsp if (argc > 1)
8066 e99e2d15 2020-11-24 naddy usage_tree();
8067 7cd52833 2022-06-23 thomas
8068 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8069 7cd52833 2022-06-23 thomas if (error != NULL)
8070 7cd52833 2022-06-23 thomas goto done;
8071 74283ab8 2020-02-07 stsp
8072 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
8073 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8074 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8075 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8076 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8077 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8078 c156c7a4 2020-12-18 stsp goto done;
8079 55cccc34 2020-02-20 stsp if (worktree)
8080 52185f70 2019-02-05 stsp repo_path =
8081 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
8082 55cccc34 2020-02-20 stsp else
8083 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
8084 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8085 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8086 c156c7a4 2020-12-18 stsp goto done;
8087 c156c7a4 2020-12-18 stsp }
8088 55cccc34 2020-02-20 stsp }
8089 a915003a 2019-02-05 stsp
8090 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8091 c02c541e 2019-03-29 stsp if (error != NULL)
8092 52185f70 2019-02-05 stsp goto done;
8093 d188b9a6 2019-01-04 stsp
8094 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8095 55cccc34 2020-02-20 stsp repo, worktree);
8096 55cccc34 2020-02-20 stsp if (error)
8097 55cccc34 2020-02-20 stsp goto done;
8098 55cccc34 2020-02-20 stsp
8099 557d3365 2023-04-14 thomas init_curses();
8100 55cccc34 2020-02-20 stsp
8101 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8102 c02c541e 2019-03-29 stsp if (error)
8103 52185f70 2019-02-05 stsp goto done;
8104 ffd1d5e5 2018-06-23 stsp
8105 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8106 51a10b52 2020-12-26 stsp if (error)
8107 51a10b52 2020-12-26 stsp goto done;
8108 51a10b52 2020-12-26 stsp
8109 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
8110 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
8111 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
8112 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8113 4e97c21c 2020-12-06 stsp if (error)
8114 4e97c21c 2020-12-06 stsp goto done;
8115 4e97c21c 2020-12-06 stsp head_ref_name = label;
8116 4e97c21c 2020-12-06 stsp } else {
8117 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
8118 4e97c21c 2020-12-06 stsp if (error == NULL)
8119 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
8120 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
8121 4e97c21c 2020-12-06 stsp goto done;
8122 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
8123 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8124 4e97c21c 2020-12-06 stsp if (error)
8125 4e97c21c 2020-12-06 stsp goto done;
8126 4e97c21c 2020-12-06 stsp }
8127 ffd1d5e5 2018-06-23 stsp
8128 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8129 945f9229 2022-04-16 thomas if (error)
8130 945f9229 2022-04-16 thomas goto done;
8131 945f9229 2022-04-16 thomas
8132 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8133 5221c383 2018-08-01 stsp if (view == NULL) {
8134 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8135 5221c383 2018-08-01 stsp goto done;
8136 5221c383 2018-08-01 stsp }
8137 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8138 ad80ab7b 2018-08-04 stsp if (error)
8139 ad80ab7b 2018-08-04 stsp goto done;
8140 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8141 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8142 d91faf3b 2020-12-01 naddy in_repo_path);
8143 55cccc34 2020-02-20 stsp if (error)
8144 55cccc34 2020-02-20 stsp goto done;
8145 55cccc34 2020-02-20 stsp }
8146 55cccc34 2020-02-20 stsp
8147 55cccc34 2020-02-20 stsp if (worktree) {
8148 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8149 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8150 55cccc34 2020-02-20 stsp worktree = NULL;
8151 55cccc34 2020-02-20 stsp }
8152 557d3365 2023-04-14 thomas error = view_loop(view);
8153 ffd1d5e5 2018-06-23 stsp done:
8154 52185f70 2019-02-05 stsp free(repo_path);
8155 e4a0e26d 2020-02-20 stsp free(cwd);
8156 ffd1d5e5 2018-06-23 stsp free(commit_id);
8157 4e97c21c 2020-12-06 stsp free(label);
8158 486cd271 2020-12-06 stsp if (ref)
8159 486cd271 2020-12-06 stsp got_ref_close(ref);
8160 1d0f4054 2021-06-17 stsp if (repo) {
8161 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8162 1d0f4054 2021-06-17 stsp if (error == NULL)
8163 1d0f4054 2021-06-17 stsp error = close_err;
8164 1d0f4054 2021-06-17 stsp }
8165 7cd52833 2022-06-23 thomas if (pack_fds) {
8166 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8167 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8168 7cd52833 2022-06-23 thomas if (error == NULL)
8169 7cd52833 2022-06-23 thomas error = pack_err;
8170 b85a3496 2023-04-14 thomas }
8171 51a10b52 2020-12-26 stsp tog_free_refs();
8172 ffd1d5e5 2018-06-23 stsp return error;
8173 6458efa5 2020-11-24 stsp }
8174 6458efa5 2020-11-24 stsp
8175 6458efa5 2020-11-24 stsp static const struct got_error *
8176 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8177 6458efa5 2020-11-24 stsp {
8178 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8179 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8180 6458efa5 2020-11-24 stsp
8181 6458efa5 2020-11-24 stsp s->nrefs = 0;
8182 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8183 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8184 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8185 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8186 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8187 6458efa5 2020-11-24 stsp continue;
8188 6458efa5 2020-11-24 stsp
8189 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8190 6458efa5 2020-11-24 stsp if (re == NULL)
8191 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8192 6458efa5 2020-11-24 stsp
8193 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8194 8924d611 2020-12-26 stsp if (re->ref == NULL)
8195 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8196 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8197 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8198 6458efa5 2020-11-24 stsp }
8199 6458efa5 2020-11-24 stsp
8200 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8201 6458efa5 2020-11-24 stsp return NULL;
8202 6458efa5 2020-11-24 stsp }
8203 6458efa5 2020-11-24 stsp
8204 ef20f542 2022-06-26 thomas static void
8205 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8206 6458efa5 2020-11-24 stsp {
8207 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8208 6458efa5 2020-11-24 stsp
8209 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8210 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8211 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8212 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8213 6458efa5 2020-11-24 stsp free(re);
8214 6458efa5 2020-11-24 stsp }
8215 6458efa5 2020-11-24 stsp }
8216 6458efa5 2020-11-24 stsp
8217 6458efa5 2020-11-24 stsp static const struct got_error *
8218 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8219 6458efa5 2020-11-24 stsp {
8220 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8221 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8222 6458efa5 2020-11-24 stsp
8223 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8224 6458efa5 2020-11-24 stsp s->repo = repo;
8225 6458efa5 2020-11-24 stsp
8226 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8227 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8228 6458efa5 2020-11-24 stsp
8229 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8230 6458efa5 2020-11-24 stsp if (err)
8231 51b7e1c3 2023-04-14 thomas goto done;
8232 34ba6917 2020-11-30 stsp
8233 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8234 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8235 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8236 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8237 6458efa5 2020-11-24 stsp if (err)
8238 6458efa5 2020-11-24 stsp goto done;
8239 6458efa5 2020-11-24 stsp
8240 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8241 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8242 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8243 6458efa5 2020-11-24 stsp if (err)
8244 6458efa5 2020-11-24 stsp goto done;
8245 6458efa5 2020-11-24 stsp
8246 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8247 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8248 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8249 2183bbf6 2022-01-23 thomas if (err)
8250 2183bbf6 2022-01-23 thomas goto done;
8251 2183bbf6 2022-01-23 thomas
8252 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8253 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8254 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8255 6458efa5 2020-11-24 stsp if (err)
8256 6458efa5 2020-11-24 stsp goto done;
8257 6458efa5 2020-11-24 stsp }
8258 6458efa5 2020-11-24 stsp
8259 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8260 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8261 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8262 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8263 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8264 6458efa5 2020-11-24 stsp done:
8265 51b7e1c3 2023-04-14 thomas if (err) {
8266 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8267 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8268 51b7e1c3 2023-04-14 thomas view_close(view);
8269 51b7e1c3 2023-04-14 thomas }
8270 6458efa5 2020-11-24 stsp return err;
8271 6458efa5 2020-11-24 stsp }
8272 6458efa5 2020-11-24 stsp
8273 6458efa5 2020-11-24 stsp static const struct got_error *
8274 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8275 6458efa5 2020-11-24 stsp {
8276 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8277 6458efa5 2020-11-24 stsp
8278 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8279 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8280 6458efa5 2020-11-24 stsp
8281 6458efa5 2020-11-24 stsp return NULL;
8282 9f7d7167 2018-04-29 stsp }
8283 ce5b7c56 2019-07-09 stsp
8284 6458efa5 2020-11-24 stsp static const struct got_error *
8285 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8286 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8287 6458efa5 2020-11-24 stsp {
8288 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8289 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8290 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8291 6458efa5 2020-11-24 stsp int obj_type;
8292 6458efa5 2020-11-24 stsp
8293 c42c9805 2020-11-24 stsp *commit_id = NULL;
8294 6458efa5 2020-11-24 stsp
8295 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8296 6458efa5 2020-11-24 stsp if (err)
8297 6458efa5 2020-11-24 stsp return err;
8298 6458efa5 2020-11-24 stsp
8299 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8300 6458efa5 2020-11-24 stsp if (err)
8301 6458efa5 2020-11-24 stsp goto done;
8302 6458efa5 2020-11-24 stsp
8303 6458efa5 2020-11-24 stsp switch (obj_type) {
8304 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8305 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8306 6458efa5 2020-11-24 stsp break;
8307 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8308 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8309 6458efa5 2020-11-24 stsp if (err)
8310 6458efa5 2020-11-24 stsp goto done;
8311 c42c9805 2020-11-24 stsp free(obj_id);
8312 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8313 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8314 c42c9805 2020-11-24 stsp if (err)
8315 6458efa5 2020-11-24 stsp goto done;
8316 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8317 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8318 c42c9805 2020-11-24 stsp goto done;
8319 c42c9805 2020-11-24 stsp }
8320 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8321 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8322 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8323 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8324 c42c9805 2020-11-24 stsp goto done;
8325 c42c9805 2020-11-24 stsp }
8326 6458efa5 2020-11-24 stsp break;
8327 6458efa5 2020-11-24 stsp default:
8328 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8329 c42c9805 2020-11-24 stsp break;
8330 6458efa5 2020-11-24 stsp }
8331 6458efa5 2020-11-24 stsp
8332 c42c9805 2020-11-24 stsp done:
8333 c42c9805 2020-11-24 stsp if (tag)
8334 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8335 c42c9805 2020-11-24 stsp if (err) {
8336 c42c9805 2020-11-24 stsp free(*commit_id);
8337 c42c9805 2020-11-24 stsp *commit_id = NULL;
8338 c42c9805 2020-11-24 stsp }
8339 c42c9805 2020-11-24 stsp return err;
8340 c42c9805 2020-11-24 stsp }
8341 c42c9805 2020-11-24 stsp
8342 c42c9805 2020-11-24 stsp static const struct got_error *
8343 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8344 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8345 c42c9805 2020-11-24 stsp {
8346 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8347 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8348 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8349 c42c9805 2020-11-24 stsp
8350 c42c9805 2020-11-24 stsp *new_view = NULL;
8351 c42c9805 2020-11-24 stsp
8352 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8353 c42c9805 2020-11-24 stsp if (err) {
8354 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8355 c42c9805 2020-11-24 stsp return err;
8356 c42c9805 2020-11-24 stsp else
8357 c42c9805 2020-11-24 stsp return NULL;
8358 c42c9805 2020-11-24 stsp }
8359 c42c9805 2020-11-24 stsp
8360 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8361 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8362 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8363 6458efa5 2020-11-24 stsp goto done;
8364 6458efa5 2020-11-24 stsp }
8365 6458efa5 2020-11-24 stsp
8366 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8367 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8368 6458efa5 2020-11-24 stsp done:
8369 6458efa5 2020-11-24 stsp if (err)
8370 6458efa5 2020-11-24 stsp view_close(log_view);
8371 6458efa5 2020-11-24 stsp else
8372 6458efa5 2020-11-24 stsp *new_view = log_view;
8373 c42c9805 2020-11-24 stsp free(commit_id);
8374 6458efa5 2020-11-24 stsp return err;
8375 6458efa5 2020-11-24 stsp }
8376 6458efa5 2020-11-24 stsp
8377 ce5b7c56 2019-07-09 stsp static void
8378 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8379 6458efa5 2020-11-24 stsp {
8380 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8381 34ba6917 2020-11-30 stsp int i = 0;
8382 6458efa5 2020-11-24 stsp
8383 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8384 6458efa5 2020-11-24 stsp return;
8385 6458efa5 2020-11-24 stsp
8386 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8387 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8388 34ba6917 2020-11-30 stsp if (re == NULL)
8389 34ba6917 2020-11-30 stsp break;
8390 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8391 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8392 6458efa5 2020-11-24 stsp }
8393 6458efa5 2020-11-24 stsp }
8394 6458efa5 2020-11-24 stsp
8395 a5d43cac 2022-07-01 thomas static const struct got_error *
8396 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8397 6458efa5 2020-11-24 stsp {
8398 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8399 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8400 6458efa5 2020-11-24 stsp int n = 0;
8401 6458efa5 2020-11-24 stsp
8402 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8403 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8404 6458efa5 2020-11-24 stsp else
8405 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8406 6458efa5 2020-11-24 stsp
8407 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8408 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8409 07dd3ed3 2022-08-06 thomas if (last) {
8410 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8411 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8412 07dd3ed3 2022-08-06 thomas }
8413 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8414 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8415 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8416 6458efa5 2020-11-24 stsp }
8417 6458efa5 2020-11-24 stsp }
8418 a5d43cac 2022-07-01 thomas
8419 a5d43cac 2022-07-01 thomas return NULL;
8420 6458efa5 2020-11-24 stsp }
8421 6458efa5 2020-11-24 stsp
8422 6458efa5 2020-11-24 stsp static const struct got_error *
8423 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8424 6458efa5 2020-11-24 stsp {
8425 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8426 6458efa5 2020-11-24 stsp
8427 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8428 6458efa5 2020-11-24 stsp return NULL;
8429 6458efa5 2020-11-24 stsp }
8430 6458efa5 2020-11-24 stsp
8431 6458efa5 2020-11-24 stsp static int
8432 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8433 6458efa5 2020-11-24 stsp {
8434 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8435 6458efa5 2020-11-24 stsp
8436 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8437 6458efa5 2020-11-24 stsp 0) == 0;
8438 6458efa5 2020-11-24 stsp }
8439 6458efa5 2020-11-24 stsp
8440 6458efa5 2020-11-24 stsp static const struct got_error *
8441 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8442 6458efa5 2020-11-24 stsp {
8443 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8444 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8445 6458efa5 2020-11-24 stsp
8446 6458efa5 2020-11-24 stsp if (!view->searching) {
8447 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8448 6458efa5 2020-11-24 stsp return NULL;
8449 6458efa5 2020-11-24 stsp }
8450 6458efa5 2020-11-24 stsp
8451 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8452 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8453 6458efa5 2020-11-24 stsp if (s->selected_entry)
8454 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8455 6458efa5 2020-11-24 stsp else
8456 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8457 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8458 6458efa5 2020-11-24 stsp } else {
8459 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8460 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8461 6458efa5 2020-11-24 stsp else
8462 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8463 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8464 6458efa5 2020-11-24 stsp }
8465 6458efa5 2020-11-24 stsp } else {
8466 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8467 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8468 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8469 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8470 6458efa5 2020-11-24 stsp else
8471 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8472 6458efa5 2020-11-24 stsp }
8473 6458efa5 2020-11-24 stsp
8474 6458efa5 2020-11-24 stsp while (1) {
8475 6458efa5 2020-11-24 stsp if (re == NULL) {
8476 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8477 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8478 6458efa5 2020-11-24 stsp return NULL;
8479 6458efa5 2020-11-24 stsp }
8480 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8481 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8482 6458efa5 2020-11-24 stsp else
8483 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8484 6458efa5 2020-11-24 stsp }
8485 6458efa5 2020-11-24 stsp
8486 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8487 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8488 6458efa5 2020-11-24 stsp s->matched_entry = re;
8489 6458efa5 2020-11-24 stsp break;
8490 6458efa5 2020-11-24 stsp }
8491 6458efa5 2020-11-24 stsp
8492 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8493 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8494 6458efa5 2020-11-24 stsp else
8495 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8496 6458efa5 2020-11-24 stsp }
8497 6458efa5 2020-11-24 stsp
8498 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8499 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8500 6458efa5 2020-11-24 stsp s->selected = 0;
8501 6458efa5 2020-11-24 stsp }
8502 6458efa5 2020-11-24 stsp
8503 6458efa5 2020-11-24 stsp return NULL;
8504 6458efa5 2020-11-24 stsp }
8505 6458efa5 2020-11-24 stsp
8506 6458efa5 2020-11-24 stsp static const struct got_error *
8507 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8508 6458efa5 2020-11-24 stsp {
8509 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8510 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8512 6458efa5 2020-11-24 stsp char *line = NULL;
8513 6458efa5 2020-11-24 stsp wchar_t *wline;
8514 6458efa5 2020-11-24 stsp struct tog_color *tc;
8515 66a70b97 2023-02-03 thomas int width, n, scrollx;
8516 6458efa5 2020-11-24 stsp int limit = view->nlines;
8517 6458efa5 2020-11-24 stsp
8518 6458efa5 2020-11-24 stsp werase(view->window);
8519 6458efa5 2020-11-24 stsp
8520 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8521 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8522 a5d43cac 2022-07-01 thomas --limit; /* border */
8523 6458efa5 2020-11-24 stsp
8524 6458efa5 2020-11-24 stsp if (limit == 0)
8525 6458efa5 2020-11-24 stsp return NULL;
8526 6458efa5 2020-11-24 stsp
8527 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8528 6458efa5 2020-11-24 stsp
8529 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8530 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8531 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8532 6458efa5 2020-11-24 stsp
8533 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8534 6458efa5 2020-11-24 stsp if (err) {
8535 6458efa5 2020-11-24 stsp free(line);
8536 6458efa5 2020-11-24 stsp return err;
8537 6458efa5 2020-11-24 stsp }
8538 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8539 6458efa5 2020-11-24 stsp wstandout(view->window);
8540 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8541 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8542 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8543 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8544 6458efa5 2020-11-24 stsp wstandend(view->window);
8545 6458efa5 2020-11-24 stsp free(wline);
8546 6458efa5 2020-11-24 stsp wline = NULL;
8547 6458efa5 2020-11-24 stsp free(line);
8548 6458efa5 2020-11-24 stsp line = NULL;
8549 6458efa5 2020-11-24 stsp if (--limit <= 0)
8550 6458efa5 2020-11-24 stsp return NULL;
8551 6458efa5 2020-11-24 stsp
8552 6458efa5 2020-11-24 stsp n = 0;
8553 66a70b97 2023-02-03 thomas view->maxx = 0;
8554 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8555 6458efa5 2020-11-24 stsp char *line = NULL;
8556 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8557 6458efa5 2020-11-24 stsp
8558 84227eb1 2022-06-23 thomas if (s->show_date) {
8559 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8560 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8561 84227eb1 2022-06-23 thomas struct got_object_id *id;
8562 84227eb1 2022-06-23 thomas struct tm tm;
8563 84227eb1 2022-06-23 thomas time_t t;
8564 84227eb1 2022-06-23 thomas
8565 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8566 84227eb1 2022-06-23 thomas if (err)
8567 84227eb1 2022-06-23 thomas return err;
8568 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8569 84227eb1 2022-06-23 thomas if (err) {
8570 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8571 84227eb1 2022-06-23 thomas free(id);
8572 84227eb1 2022-06-23 thomas return err;
8573 84227eb1 2022-06-23 thomas }
8574 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8575 84227eb1 2022-06-23 thomas id);
8576 84227eb1 2022-06-23 thomas if (err) {
8577 84227eb1 2022-06-23 thomas free(id);
8578 84227eb1 2022-06-23 thomas return err;
8579 84227eb1 2022-06-23 thomas }
8580 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8581 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8582 84227eb1 2022-06-23 thomas } else {
8583 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8584 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8585 84227eb1 2022-06-23 thomas }
8586 84227eb1 2022-06-23 thomas free(id);
8587 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8588 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8589 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8590 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8591 84227eb1 2022-06-23 thomas }
8592 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8593 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8594 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8595 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8596 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8597 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8598 6458efa5 2020-11-24 stsp struct got_object_id *id;
8599 6458efa5 2020-11-24 stsp char *id_str;
8600 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8601 6458efa5 2020-11-24 stsp if (err)
8602 6458efa5 2020-11-24 stsp return err;
8603 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8604 6458efa5 2020-11-24 stsp if (err) {
8605 6458efa5 2020-11-24 stsp free(id);
8606 6458efa5 2020-11-24 stsp return err;
8607 6458efa5 2020-11-24 stsp }
8608 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8609 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8610 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8611 6458efa5 2020-11-24 stsp free(id);
8612 6458efa5 2020-11-24 stsp free(id_str);
8613 6458efa5 2020-11-24 stsp return err;
8614 6458efa5 2020-11-24 stsp }
8615 6458efa5 2020-11-24 stsp free(id);
8616 6458efa5 2020-11-24 stsp free(id_str);
8617 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8618 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8619 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8620 6458efa5 2020-11-24 stsp
8621 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8622 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8623 6458efa5 2020-11-24 stsp if (err) {
8624 6458efa5 2020-11-24 stsp free(line);
8625 6458efa5 2020-11-24 stsp return err;
8626 6458efa5 2020-11-24 stsp }
8627 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8628 66a70b97 2023-02-03 thomas free(wline);
8629 66a70b97 2023-02-03 thomas wline = NULL;
8630 66a70b97 2023-02-03 thomas
8631 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8632 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8633 66a70b97 2023-02-03 thomas if (err) {
8634 66a70b97 2023-02-03 thomas free(line);
8635 66a70b97 2023-02-03 thomas return err;
8636 66a70b97 2023-02-03 thomas }
8637 6458efa5 2020-11-24 stsp if (n == s->selected) {
8638 6458efa5 2020-11-24 stsp if (view->focussed)
8639 6458efa5 2020-11-24 stsp wstandout(view->window);
8640 6458efa5 2020-11-24 stsp s->selected_entry = re;
8641 6458efa5 2020-11-24 stsp }
8642 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8643 6458efa5 2020-11-24 stsp if (tc)
8644 6458efa5 2020-11-24 stsp wattr_on(view->window,
8645 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8646 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8647 6458efa5 2020-11-24 stsp if (tc)
8648 6458efa5 2020-11-24 stsp wattr_off(view->window,
8649 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8650 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8651 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8652 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8653 6458efa5 2020-11-24 stsp wstandend(view->window);
8654 6458efa5 2020-11-24 stsp free(line);
8655 6458efa5 2020-11-24 stsp free(wline);
8656 6458efa5 2020-11-24 stsp wline = NULL;
8657 6458efa5 2020-11-24 stsp n++;
8658 6458efa5 2020-11-24 stsp s->ndisplayed++;
8659 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8660 6458efa5 2020-11-24 stsp
8661 6458efa5 2020-11-24 stsp limit--;
8662 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8663 6458efa5 2020-11-24 stsp }
8664 6458efa5 2020-11-24 stsp
8665 a5d43cac 2022-07-01 thomas view_border(view);
8666 6458efa5 2020-11-24 stsp return err;
8667 6458efa5 2020-11-24 stsp }
8668 6458efa5 2020-11-24 stsp
8669 6458efa5 2020-11-24 stsp static const struct got_error *
8670 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8671 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8672 c42c9805 2020-11-24 stsp {
8673 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8674 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8675 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8676 c42c9805 2020-11-24 stsp
8677 c42c9805 2020-11-24 stsp *new_view = NULL;
8678 c42c9805 2020-11-24 stsp
8679 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8680 c42c9805 2020-11-24 stsp if (err) {
8681 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8682 c42c9805 2020-11-24 stsp return err;
8683 c42c9805 2020-11-24 stsp else
8684 c42c9805 2020-11-24 stsp return NULL;
8685 c42c9805 2020-11-24 stsp }
8686 c42c9805 2020-11-24 stsp
8687 c42c9805 2020-11-24 stsp
8688 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8689 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8690 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8691 c42c9805 2020-11-24 stsp goto done;
8692 c42c9805 2020-11-24 stsp }
8693 c42c9805 2020-11-24 stsp
8694 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8695 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8696 c42c9805 2020-11-24 stsp if (err)
8697 c42c9805 2020-11-24 stsp goto done;
8698 c42c9805 2020-11-24 stsp
8699 c42c9805 2020-11-24 stsp *new_view = tree_view;
8700 c42c9805 2020-11-24 stsp done:
8701 c42c9805 2020-11-24 stsp free(commit_id);
8702 c42c9805 2020-11-24 stsp return err;
8703 07dd3ed3 2022-08-06 thomas }
8704 07dd3ed3 2022-08-06 thomas
8705 07dd3ed3 2022-08-06 thomas static const struct got_error *
8706 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8707 07dd3ed3 2022-08-06 thomas {
8708 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8709 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8710 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8711 07dd3ed3 2022-08-06 thomas
8712 07dd3ed3 2022-08-06 thomas g = view->gline;
8713 07dd3ed3 2022-08-06 thomas view->gline = 0;
8714 07dd3ed3 2022-08-06 thomas
8715 07dd3ed3 2022-08-06 thomas if (g == 0)
8716 07dd3ed3 2022-08-06 thomas g = 1;
8717 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8718 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8719 07dd3ed3 2022-08-06 thomas
8720 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8721 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8722 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8723 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8724 07dd3ed3 2022-08-06 thomas return NULL;
8725 07dd3ed3 2022-08-06 thomas }
8726 07dd3ed3 2022-08-06 thomas
8727 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8728 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8729 07dd3ed3 2022-08-06 thomas if (err)
8730 07dd3ed3 2022-08-06 thomas return err;
8731 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8732 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8733 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8734 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8735 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8736 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8737 07dd3ed3 2022-08-06 thomas
8738 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8739 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8740 07dd3ed3 2022-08-06 thomas
8741 07dd3ed3 2022-08-06 thomas return NULL;
8742 07dd3ed3 2022-08-06 thomas
8743 c42c9805 2020-11-24 stsp }
8744 07dd3ed3 2022-08-06 thomas
8745 c42c9805 2020-11-24 stsp static const struct got_error *
8746 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8747 6458efa5 2020-11-24 stsp {
8748 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8749 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8750 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8751 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8752 6458efa5 2020-11-24 stsp
8753 07dd3ed3 2022-08-06 thomas if (view->gline)
8754 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8755 07dd3ed3 2022-08-06 thomas
8756 6458efa5 2020-11-24 stsp switch (ch) {
8757 66a70b97 2023-02-03 thomas case '0':
8758 66a70b97 2023-02-03 thomas case '$':
8759 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8760 66a70b97 2023-02-03 thomas case 'l':
8761 66a70b97 2023-02-03 thomas case KEY_LEFT:
8762 66a70b97 2023-02-03 thomas case 'h':
8763 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8764 66a70b97 2023-02-03 thomas break;
8765 6458efa5 2020-11-24 stsp case 'i':
8766 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8767 07b0611c 2022-06-23 thomas view->count = 0;
8768 3bfadbd4 2021-11-20 thomas break;
8769 84227eb1 2022-06-23 thomas case 'm':
8770 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8771 07b0611c 2022-06-23 thomas view->count = 0;
8772 84227eb1 2022-06-23 thomas break;
8773 98182bd0 2021-11-20 thomas case 'o':
8774 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8775 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8776 07b0611c 2022-06-23 thomas view->count = 0;
8777 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8778 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8779 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8780 c591440f 2021-11-20 thomas if (err)
8781 c591440f 2021-11-20 thomas break;
8782 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8783 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8784 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8785 3bfadbd4 2021-11-20 thomas if (err)
8786 3bfadbd4 2021-11-20 thomas break;
8787 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8788 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8789 6458efa5 2020-11-24 stsp break;
8790 6458efa5 2020-11-24 stsp case KEY_ENTER:
8791 6458efa5 2020-11-24 stsp case '\r':
8792 07b0611c 2022-06-23 thomas view->count = 0;
8793 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8794 a5d43cac 2022-07-01 thomas break;
8795 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8796 6458efa5 2020-11-24 stsp break;
8797 1be4947a 2022-07-22 thomas case 'T':
8798 07b0611c 2022-06-23 thomas view->count = 0;
8799 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8800 c42c9805 2020-11-24 stsp break;
8801 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8802 c42c9805 2020-11-24 stsp break;
8803 e4526bf5 2021-09-03 naddy case 'g':
8804 aa7a1117 2023-01-09 thomas case '=':
8805 e4526bf5 2021-09-03 naddy case KEY_HOME:
8806 e4526bf5 2021-09-03 naddy s->selected = 0;
8807 07b0611c 2022-06-23 thomas view->count = 0;
8808 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8809 e4526bf5 2021-09-03 naddy break;
8810 e4526bf5 2021-09-03 naddy case 'G':
8811 aa7a1117 2023-01-09 thomas case '*':
8812 a5d43cac 2022-07-01 thomas case KEY_END: {
8813 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8814 a5d43cac 2022-07-01 thomas
8815 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8816 a5d43cac 2022-07-01 thomas --eos; /* border */
8817 e4526bf5 2021-09-03 naddy s->selected = 0;
8818 07b0611c 2022-06-23 thomas view->count = 0;
8819 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8820 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8821 e4526bf5 2021-09-03 naddy if (re == NULL)
8822 e4526bf5 2021-09-03 naddy break;
8823 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8824 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8825 e4526bf5 2021-09-03 naddy }
8826 e4526bf5 2021-09-03 naddy if (n > 0)
8827 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8828 e4526bf5 2021-09-03 naddy break;
8829 a5d43cac 2022-07-01 thomas }
8830 6458efa5 2020-11-24 stsp case 'k':
8831 6458efa5 2020-11-24 stsp case KEY_UP:
8832 f7140bf5 2021-10-17 thomas case CTRL('p'):
8833 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8834 6458efa5 2020-11-24 stsp s->selected--;
8835 6458efa5 2020-11-24 stsp break;
8836 34ba6917 2020-11-30 stsp }
8837 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8838 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8839 07b0611c 2022-06-23 thomas view->count = 0;
8840 6458efa5 2020-11-24 stsp break;
8841 70f17a53 2022-06-13 thomas case CTRL('u'):
8842 23427b14 2022-06-23 thomas case 'u':
8843 70f17a53 2022-06-13 thomas nscroll /= 2;
8844 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8845 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8846 6458efa5 2020-11-24 stsp case CTRL('b'):
8847 1c5e5faa 2022-06-23 thomas case 'b':
8848 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8849 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8850 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8851 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8852 07b0611c 2022-06-23 thomas view->count = 0;
8853 6458efa5 2020-11-24 stsp break;
8854 6458efa5 2020-11-24 stsp case 'j':
8855 6458efa5 2020-11-24 stsp case KEY_DOWN:
8856 f7140bf5 2021-10-17 thomas case CTRL('n'):
8857 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8858 6458efa5 2020-11-24 stsp s->selected++;
8859 6458efa5 2020-11-24 stsp break;
8860 6458efa5 2020-11-24 stsp }
8861 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8862 6458efa5 2020-11-24 stsp /* can't scroll any further */
8863 07b0611c 2022-06-23 thomas view->count = 0;
8864 6458efa5 2020-11-24 stsp break;
8865 07b0611c 2022-06-23 thomas }
8866 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8867 6458efa5 2020-11-24 stsp break;
8868 70f17a53 2022-06-13 thomas case CTRL('d'):
8869 23427b14 2022-06-23 thomas case 'd':
8870 70f17a53 2022-06-13 thomas nscroll /= 2;
8871 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8872 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8873 6458efa5 2020-11-24 stsp case CTRL('f'):
8874 1c5e5faa 2022-06-23 thomas case 'f':
8875 4c2d69cb 2022-06-23 thomas case ' ':
8876 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8877 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8878 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8879 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8880 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8881 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8882 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8883 07b0611c 2022-06-23 thomas view->count = 0;
8884 6458efa5 2020-11-24 stsp break;
8885 6458efa5 2020-11-24 stsp }
8886 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8887 6458efa5 2020-11-24 stsp break;
8888 6458efa5 2020-11-24 stsp case CTRL('l'):
8889 07b0611c 2022-06-23 thomas view->count = 0;
8890 8924d611 2020-12-26 stsp tog_free_refs();
8891 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8892 8924d611 2020-12-26 stsp if (err)
8893 8924d611 2020-12-26 stsp break;
8894 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8895 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8896 6458efa5 2020-11-24 stsp break;
8897 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8898 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8899 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8900 6458efa5 2020-11-24 stsp break;
8901 6458efa5 2020-11-24 stsp default:
8902 07b0611c 2022-06-23 thomas view->count = 0;
8903 6458efa5 2020-11-24 stsp break;
8904 6458efa5 2020-11-24 stsp }
8905 6458efa5 2020-11-24 stsp
8906 6458efa5 2020-11-24 stsp return err;
8907 6458efa5 2020-11-24 stsp }
8908 6458efa5 2020-11-24 stsp
8909 6458efa5 2020-11-24 stsp __dead static void
8910 6458efa5 2020-11-24 stsp usage_ref(void)
8911 6458efa5 2020-11-24 stsp {
8912 6458efa5 2020-11-24 stsp endwin();
8913 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8914 6458efa5 2020-11-24 stsp getprogname());
8915 6458efa5 2020-11-24 stsp exit(1);
8916 6458efa5 2020-11-24 stsp }
8917 6458efa5 2020-11-24 stsp
8918 6458efa5 2020-11-24 stsp static const struct got_error *
8919 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8920 6458efa5 2020-11-24 stsp {
8921 1d98034b 2023-04-14 thomas const struct got_error *error;
8922 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8923 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8924 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8925 6458efa5 2020-11-24 stsp int ch;
8926 6458efa5 2020-11-24 stsp struct tog_view *view;
8927 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8928 6458efa5 2020-11-24 stsp
8929 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8930 6458efa5 2020-11-24 stsp switch (ch) {
8931 6458efa5 2020-11-24 stsp case 'r':
8932 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8933 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8934 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8935 6458efa5 2020-11-24 stsp optarg);
8936 6458efa5 2020-11-24 stsp break;
8937 6458efa5 2020-11-24 stsp default:
8938 e99e2d15 2020-11-24 naddy usage_ref();
8939 6458efa5 2020-11-24 stsp /* NOTREACHED */
8940 6458efa5 2020-11-24 stsp }
8941 6458efa5 2020-11-24 stsp }
8942 6458efa5 2020-11-24 stsp
8943 6458efa5 2020-11-24 stsp argc -= optind;
8944 6458efa5 2020-11-24 stsp argv += optind;
8945 6458efa5 2020-11-24 stsp
8946 6458efa5 2020-11-24 stsp if (argc > 1)
8947 e99e2d15 2020-11-24 naddy usage_ref();
8948 7cd52833 2022-06-23 thomas
8949 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8950 7cd52833 2022-06-23 thomas if (error != NULL)
8951 7cd52833 2022-06-23 thomas goto done;
8952 6458efa5 2020-11-24 stsp
8953 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8954 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8955 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8956 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8957 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8958 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8959 c156c7a4 2020-12-18 stsp goto done;
8960 6458efa5 2020-11-24 stsp if (worktree)
8961 6458efa5 2020-11-24 stsp repo_path =
8962 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8963 6458efa5 2020-11-24 stsp else
8964 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8965 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8966 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8967 c156c7a4 2020-12-18 stsp goto done;
8968 c156c7a4 2020-12-18 stsp }
8969 6458efa5 2020-11-24 stsp }
8970 6458efa5 2020-11-24 stsp
8971 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8972 6458efa5 2020-11-24 stsp if (error != NULL)
8973 6458efa5 2020-11-24 stsp goto done;
8974 6458efa5 2020-11-24 stsp
8975 557d3365 2023-04-14 thomas init_curses();
8976 6458efa5 2020-11-24 stsp
8977 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8978 51a10b52 2020-12-26 stsp if (error)
8979 51a10b52 2020-12-26 stsp goto done;
8980 51a10b52 2020-12-26 stsp
8981 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8982 6458efa5 2020-11-24 stsp if (error)
8983 6458efa5 2020-11-24 stsp goto done;
8984 6458efa5 2020-11-24 stsp
8985 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8986 6458efa5 2020-11-24 stsp if (view == NULL) {
8987 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8988 6458efa5 2020-11-24 stsp goto done;
8989 6458efa5 2020-11-24 stsp }
8990 6458efa5 2020-11-24 stsp
8991 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8992 6458efa5 2020-11-24 stsp if (error)
8993 6458efa5 2020-11-24 stsp goto done;
8994 6458efa5 2020-11-24 stsp
8995 6458efa5 2020-11-24 stsp if (worktree) {
8996 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8997 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8998 6458efa5 2020-11-24 stsp worktree = NULL;
8999 6458efa5 2020-11-24 stsp }
9000 557d3365 2023-04-14 thomas error = view_loop(view);
9001 6458efa5 2020-11-24 stsp done:
9002 6458efa5 2020-11-24 stsp free(repo_path);
9003 6458efa5 2020-11-24 stsp free(cwd);
9004 1d0f4054 2021-06-17 stsp if (repo) {
9005 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9006 1d0f4054 2021-06-17 stsp if (close_err)
9007 1d0f4054 2021-06-17 stsp error = close_err;
9008 1d0f4054 2021-06-17 stsp }
9009 7cd52833 2022-06-23 thomas if (pack_fds) {
9010 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9011 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9012 7cd52833 2022-06-23 thomas if (error == NULL)
9013 7cd52833 2022-06-23 thomas error = pack_err;
9014 b85a3496 2023-04-14 thomas }
9015 51a10b52 2020-12-26 stsp tog_free_refs();
9016 6458efa5 2020-11-24 stsp return error;
9017 6458efa5 2020-11-24 stsp }
9018 6458efa5 2020-11-24 stsp
9019 fc2737d5 2022-09-15 thomas static const struct got_error*
9020 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
9021 fc2737d5 2022-09-15 thomas const char *str)
9022 fc2737d5 2022-09-15 thomas {
9023 fc2737d5 2022-09-15 thomas size_t len;
9024 fc2737d5 2022-09-15 thomas
9025 fc2737d5 2022-09-15 thomas if (win == NULL)
9026 fc2737d5 2022-09-15 thomas win = stdscr;
9027 fc2737d5 2022-09-15 thomas
9028 fc2737d5 2022-09-15 thomas len = strlen(str);
9029 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
9030 fc2737d5 2022-09-15 thomas
9031 fc2737d5 2022-09-15 thomas if (focus)
9032 fc2737d5 2022-09-15 thomas wstandout(win);
9033 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
9034 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
9035 fc2737d5 2022-09-15 thomas if (focus)
9036 fc2737d5 2022-09-15 thomas wstandend(win);
9037 fc2737d5 2022-09-15 thomas
9038 fc2737d5 2022-09-15 thomas return NULL;
9039 fc2737d5 2022-09-15 thomas }
9040 fc2737d5 2022-09-15 thomas
9041 2a31b33b 2022-07-23 thomas static const struct got_error *
9042 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
9043 fc2737d5 2022-09-15 thomas {
9044 fc2737d5 2022-09-15 thomas off_t *p;
9045 fc2737d5 2022-09-15 thomas
9046 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
9047 fc2737d5 2022-09-15 thomas if (p == NULL) {
9048 fc2737d5 2022-09-15 thomas free(*line_offsets);
9049 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
9050 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
9051 fc2737d5 2022-09-15 thomas }
9052 fc2737d5 2022-09-15 thomas
9053 fc2737d5 2022-09-15 thomas *line_offsets = p;
9054 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
9055 fc2737d5 2022-09-15 thomas ++(*nlines);
9056 fc2737d5 2022-09-15 thomas return NULL;
9057 fc2737d5 2022-09-15 thomas }
9058 fc2737d5 2022-09-15 thomas
9059 fc2737d5 2022-09-15 thomas static const struct got_error *
9060 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
9061 fc2737d5 2022-09-15 thomas {
9062 fc2737d5 2022-09-15 thomas *ret = 0;
9063 fc2737d5 2022-09-15 thomas
9064 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
9065 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9066 fc2737d5 2022-09-15 thomas size_t len = 1;
9067 fc2737d5 2022-09-15 thomas
9068 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
9069 fc2737d5 2022-09-15 thomas continue;
9070 fc2737d5 2022-09-15 thomas
9071 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9072 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9073 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9074 fc2737d5 2022-09-15 thomas
9075 fc2737d5 2022-09-15 thomas len += strlen(t);
9076 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
9077 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
9078 fc2737d5 2022-09-15 thomas free(t0);
9079 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
9080 fc2737d5 2022-09-15 thomas }
9081 fc2737d5 2022-09-15 thomas
9082 fc2737d5 2022-09-15 thomas return NULL;
9083 fc2737d5 2022-09-15 thomas }
9084 fc2737d5 2022-09-15 thomas
9085 fc2737d5 2022-09-15 thomas /*
9086 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
9087 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
9088 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
9089 fc2737d5 2022-09-15 thomas */
9090 fc2737d5 2022-09-15 thomas static const struct got_error *
9091 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
9092 fc2737d5 2022-09-15 thomas {
9093 fc2737d5 2022-09-15 thomas int n, len = width;
9094 fc2737d5 2022-09-15 thomas
9095 fc2737d5 2022-09-15 thomas if (km->keys) {
9096 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
9097 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
9098 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
9099 30e77f6a 2022-09-18 thomas };
9100 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
9101 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
9102 fc2737d5 2022-09-15 thomas
9103 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
9104 fc2737d5 2022-09-15 thomas
9105 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
9106 fc2737d5 2022-09-15 thomas if (t0 == NULL)
9107 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
9108 fc2737d5 2022-09-15 thomas
9109 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
9110 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
9111 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
9112 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
9113 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
9114 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
9115 fc2737d5 2022-09-15 thomas if (n < 0) {
9116 fc2737d5 2022-09-15 thomas free(t0);
9117 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9118 fc2737d5 2022-09-15 thomas }
9119 fc2737d5 2022-09-15 thomas first = 0;
9120 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
9121 fc2737d5 2022-09-15 thomas *off += n;
9122 fc2737d5 2022-09-15 thomas }
9123 fc2737d5 2022-09-15 thomas free(t0);
9124 fc2737d5 2022-09-15 thomas }
9125 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9126 fc2737d5 2022-09-15 thomas if (n < 0)
9127 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9128 fc2737d5 2022-09-15 thomas *off += n;
9129 fc2737d5 2022-09-15 thomas
9130 fc2737d5 2022-09-15 thomas return NULL;
9131 fc2737d5 2022-09-15 thomas }
9132 fc2737d5 2022-09-15 thomas
9133 fc2737d5 2022-09-15 thomas static const struct got_error *
9134 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9135 fc2737d5 2022-09-15 thomas {
9136 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9137 fc2737d5 2022-09-15 thomas off_t off = 0;
9138 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9139 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9140 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9141 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9142 fc2737d5 2022-09-15 thomas GENERATE_HELP
9143 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9144 fc2737d5 2022-09-15 thomas #undef KEY_
9145 fc2737d5 2022-09-15 thomas };
9146 fc2737d5 2022-09-15 thomas
9147 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9148 fc2737d5 2022-09-15 thomas if (err)
9149 fc2737d5 2022-09-15 thomas return err;
9150 fc2737d5 2022-09-15 thomas
9151 fc2737d5 2022-09-15 thomas n = nitems(km);
9152 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9153 fc2737d5 2022-09-15 thomas if (err)
9154 fc2737d5 2022-09-15 thomas return err;
9155 fc2737d5 2022-09-15 thomas
9156 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9157 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9158 fc2737d5 2022-09-15 thomas show = s->all;
9159 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9160 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9161 fc2737d5 2022-09-15 thomas show = 1;
9162 fc2737d5 2022-09-15 thomas }
9163 fc2737d5 2022-09-15 thomas if (show) {
9164 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9165 fc2737d5 2022-09-15 thomas if (err)
9166 fc2737d5 2022-09-15 thomas return err;
9167 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9168 fc2737d5 2022-09-15 thomas if (err)
9169 fc2737d5 2022-09-15 thomas return err;
9170 fc2737d5 2022-09-15 thomas }
9171 fc2737d5 2022-09-15 thomas }
9172 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9173 fc2737d5 2022-09-15 thomas ++off;
9174 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9175 fc2737d5 2022-09-15 thomas return err;
9176 fc2737d5 2022-09-15 thomas }
9177 fc2737d5 2022-09-15 thomas
9178 fc2737d5 2022-09-15 thomas static const struct got_error *
9179 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9180 fc2737d5 2022-09-15 thomas {
9181 fc2737d5 2022-09-15 thomas FILE *f;
9182 fc2737d5 2022-09-15 thomas const struct got_error *err;
9183 fc2737d5 2022-09-15 thomas
9184 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9185 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9186 fc2737d5 2022-09-15 thomas s->nlines = 0;
9187 fc2737d5 2022-09-15 thomas
9188 fc2737d5 2022-09-15 thomas f = got_opentemp();
9189 fc2737d5 2022-09-15 thomas if (f == NULL)
9190 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9191 fc2737d5 2022-09-15 thomas s->f = f;
9192 fc2737d5 2022-09-15 thomas
9193 fc2737d5 2022-09-15 thomas err = format_help(s);
9194 fc2737d5 2022-09-15 thomas if (err)
9195 fc2737d5 2022-09-15 thomas return err;
9196 fc2737d5 2022-09-15 thomas
9197 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9198 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9199 fc2737d5 2022-09-15 thomas
9200 fc2737d5 2022-09-15 thomas return NULL;
9201 fc2737d5 2022-09-15 thomas }
9202 fc2737d5 2022-09-15 thomas
9203 fc2737d5 2022-09-15 thomas static const struct got_error *
9204 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9205 fc2737d5 2022-09-15 thomas {
9206 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9207 fc2737d5 2022-09-15 thomas return NULL;
9208 fc2737d5 2022-09-15 thomas }
9209 fc2737d5 2022-09-15 thomas
9210 2a7d3cd7 2022-09-17 thomas static void
9211 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9212 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9213 2a7d3cd7 2022-09-17 thomas {
9214 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9215 2a7d3cd7 2022-09-17 thomas
9216 2a7d3cd7 2022-09-17 thomas *f = s->f;
9217 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9218 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9219 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9220 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9221 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9222 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9223 2a7d3cd7 2022-09-17 thomas }
9224 2a7d3cd7 2022-09-17 thomas
9225 fc2737d5 2022-09-15 thomas static const struct got_error *
9226 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9227 fc2737d5 2022-09-15 thomas {
9228 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9229 fc2737d5 2022-09-15 thomas const struct got_error *err;
9230 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9231 fc2737d5 2022-09-15 thomas wchar_t *wline;
9232 fc2737d5 2022-09-15 thomas char *line;
9233 fc2737d5 2022-09-15 thomas ssize_t linelen;
9234 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9235 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9236 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9237 fc2737d5 2022-09-15 thomas
9238 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9239 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9240 fc2737d5 2022-09-15 thomas
9241 fc2737d5 2022-09-15 thomas s->lineno = 0;
9242 fc2737d5 2022-09-15 thomas rewind(s->f);
9243 fc2737d5 2022-09-15 thomas werase(view->window);
9244 fc2737d5 2022-09-15 thomas
9245 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9246 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9247 fc2737d5 2022-09-15 thomas
9248 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9249 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9250 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9251 fc2737d5 2022-09-15 thomas if (err)
9252 fc2737d5 2022-09-15 thomas return err;
9253 fc2737d5 2022-09-15 thomas if (eos <= 1)
9254 fc2737d5 2022-09-15 thomas return NULL;
9255 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9256 fc2737d5 2022-09-15 thomas eos -= 2;
9257 fc2737d5 2022-09-15 thomas
9258 fc2737d5 2022-09-15 thomas s->eof = 0;
9259 fc2737d5 2022-09-15 thomas view->maxx = 0;
9260 fc2737d5 2022-09-15 thomas line = NULL;
9261 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9262 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9263 fc2737d5 2022-09-15 thomas
9264 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9265 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9266 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9267 fc2737d5 2022-09-15 thomas free(line);
9268 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9269 fc2737d5 2022-09-15 thomas }
9270 fc2737d5 2022-09-15 thomas s->eof = 1;
9271 fc2737d5 2022-09-15 thomas break;
9272 fc2737d5 2022-09-15 thomas }
9273 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9274 fc2737d5 2022-09-15 thomas continue;
9275 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9276 fc2737d5 2022-09-15 thomas continue;
9277 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9278 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9279 fc2737d5 2022-09-15 thomas
9280 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9281 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9282 fc2737d5 2022-09-15 thomas if (err) {
9283 fc2737d5 2022-09-15 thomas free(line);
9284 fc2737d5 2022-09-15 thomas return err;
9285 fc2737d5 2022-09-15 thomas }
9286 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9287 fc2737d5 2022-09-15 thomas free(wline);
9288 fc2737d5 2022-09-15 thomas wline = NULL;
9289 fc2737d5 2022-09-15 thomas
9290 fc2737d5 2022-09-15 thomas if (attr)
9291 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9292 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9293 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9294 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9295 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9296 fc2737d5 2022-09-15 thomas if (err) {
9297 fc2737d5 2022-09-15 thomas free(line);
9298 fc2737d5 2022-09-15 thomas return err;
9299 fc2737d5 2022-09-15 thomas }
9300 fc2737d5 2022-09-15 thomas } else {
9301 fc2737d5 2022-09-15 thomas int skip;
9302 fc2737d5 2022-09-15 thomas
9303 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9304 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9305 fc2737d5 2022-09-15 thomas if (err) {
9306 fc2737d5 2022-09-15 thomas free(line);
9307 fc2737d5 2022-09-15 thomas return err;
9308 fc2737d5 2022-09-15 thomas }
9309 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9310 fc2737d5 2022-09-15 thomas free(wline);
9311 fc2737d5 2022-09-15 thomas wline = NULL;
9312 fc2737d5 2022-09-15 thomas }
9313 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9314 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9315 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9316 fc2737d5 2022-09-15 thomas } else {
9317 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9318 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9319 fc2737d5 2022-09-15 thomas }
9320 fc2737d5 2022-09-15 thomas if (attr)
9321 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9322 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9323 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9324 fc2737d5 2022-09-15 thomas }
9325 fc2737d5 2022-09-15 thomas free(line);
9326 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9327 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9328 fc2737d5 2022-09-15 thomas else
9329 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9330 fc2737d5 2022-09-15 thomas
9331 fc2737d5 2022-09-15 thomas view_border(view);
9332 fc2737d5 2022-09-15 thomas
9333 fc2737d5 2022-09-15 thomas if (s->eof) {
9334 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9335 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9336 fc2737d5 2022-09-15 thomas view->ncols - 1);
9337 fc2737d5 2022-09-15 thomas if (rc == ERR)
9338 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9339 fc2737d5 2022-09-15 thomas } else {
9340 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9341 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9342 fc2737d5 2022-09-15 thomas wstandout(view->window);
9343 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9344 fc2737d5 2022-09-15 thomas view->ncols - 1);
9345 fc2737d5 2022-09-15 thomas if (rc == ERR)
9346 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9347 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9348 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9349 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9350 fc2737d5 2022-09-15 thomas if (rc == ERR)
9351 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9352 fc2737d5 2022-09-15 thomas }
9353 fc2737d5 2022-09-15 thomas wstandend(view->window);
9354 fc2737d5 2022-09-15 thomas }
9355 fc2737d5 2022-09-15 thomas
9356 fc2737d5 2022-09-15 thomas return NULL;
9357 fc2737d5 2022-09-15 thomas }
9358 fc2737d5 2022-09-15 thomas
9359 fc2737d5 2022-09-15 thomas static const struct got_error *
9360 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9361 fc2737d5 2022-09-15 thomas {
9362 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9363 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9364 fc2737d5 2022-09-15 thomas char *line = NULL;
9365 fc2737d5 2022-09-15 thomas ssize_t linelen;
9366 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9367 fc2737d5 2022-09-15 thomas int eos, nscroll;
9368 fc2737d5 2022-09-15 thomas
9369 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9370 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9371 fc2737d5 2022-09-15 thomas --eos; /* border */
9372 fc2737d5 2022-09-15 thomas
9373 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9374 fc2737d5 2022-09-15 thomas
9375 fc2737d5 2022-09-15 thomas switch (ch) {
9376 fc2737d5 2022-09-15 thomas case '0':
9377 fc2737d5 2022-09-15 thomas case '$':
9378 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9379 fc2737d5 2022-09-15 thomas case 'l':
9380 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9381 fc2737d5 2022-09-15 thomas case 'h':
9382 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9383 fc2737d5 2022-09-15 thomas break;
9384 fc2737d5 2022-09-15 thomas case 'g':
9385 fc2737d5 2022-09-15 thomas case KEY_HOME:
9386 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9387 fc2737d5 2022-09-15 thomas view->count = 0;
9388 fc2737d5 2022-09-15 thomas break;
9389 fc2737d5 2022-09-15 thomas case 'G':
9390 fc2737d5 2022-09-15 thomas case KEY_END:
9391 fc2737d5 2022-09-15 thomas view->count = 0;
9392 fc2737d5 2022-09-15 thomas if (s->eof)
9393 fc2737d5 2022-09-15 thomas break;
9394 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9395 fc2737d5 2022-09-15 thomas s->eof = 1;
9396 fc2737d5 2022-09-15 thomas break;
9397 fc2737d5 2022-09-15 thomas case 'k':
9398 fc2737d5 2022-09-15 thomas case KEY_UP:
9399 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9400 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9401 fc2737d5 2022-09-15 thomas else
9402 fc2737d5 2022-09-15 thomas view->count = 0;
9403 fc2737d5 2022-09-15 thomas break;
9404 fc2737d5 2022-09-15 thomas case CTRL('u'):
9405 fc2737d5 2022-09-15 thomas case 'u':
9406 fc2737d5 2022-09-15 thomas nscroll /= 2;
9407 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9408 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9409 fc2737d5 2022-09-15 thomas case CTRL('b'):
9410 fc2737d5 2022-09-15 thomas case 'b':
9411 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9412 fc2737d5 2022-09-15 thomas view->count = 0;
9413 fc2737d5 2022-09-15 thomas break;
9414 fc2737d5 2022-09-15 thomas }
9415 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9416 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9417 fc2737d5 2022-09-15 thomas break;
9418 fc2737d5 2022-09-15 thomas case 'j':
9419 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9420 fc2737d5 2022-09-15 thomas case CTRL('n'):
9421 fc2737d5 2022-09-15 thomas if (!s->eof)
9422 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9423 fc2737d5 2022-09-15 thomas else
9424 fc2737d5 2022-09-15 thomas view->count = 0;
9425 fc2737d5 2022-09-15 thomas break;
9426 fc2737d5 2022-09-15 thomas case CTRL('d'):
9427 fc2737d5 2022-09-15 thomas case 'd':
9428 fc2737d5 2022-09-15 thomas nscroll /= 2;
9429 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9430 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9431 fc2737d5 2022-09-15 thomas case CTRL('f'):
9432 fc2737d5 2022-09-15 thomas case 'f':
9433 fc2737d5 2022-09-15 thomas case ' ':
9434 fc2737d5 2022-09-15 thomas if (s->eof) {
9435 fc2737d5 2022-09-15 thomas view->count = 0;
9436 fc2737d5 2022-09-15 thomas break;
9437 fc2737d5 2022-09-15 thomas }
9438 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9439 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9440 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9441 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9442 fc2737d5 2022-09-15 thomas if (feof(s->f))
9443 fc2737d5 2022-09-15 thomas s->eof = 1;
9444 fc2737d5 2022-09-15 thomas else
9445 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9446 fc2737d5 2022-09-15 thomas break;
9447 fc2737d5 2022-09-15 thomas }
9448 fc2737d5 2022-09-15 thomas }
9449 fc2737d5 2022-09-15 thomas free(line);
9450 fc2737d5 2022-09-15 thomas break;
9451 fc2737d5 2022-09-15 thomas default:
9452 fc2737d5 2022-09-15 thomas view->count = 0;
9453 fc2737d5 2022-09-15 thomas break;
9454 fc2737d5 2022-09-15 thomas }
9455 fc2737d5 2022-09-15 thomas
9456 fc2737d5 2022-09-15 thomas return err;
9457 fc2737d5 2022-09-15 thomas }
9458 fc2737d5 2022-09-15 thomas
9459 fc2737d5 2022-09-15 thomas static const struct got_error *
9460 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9461 fc2737d5 2022-09-15 thomas {
9462 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9463 fc2737d5 2022-09-15 thomas
9464 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9465 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9466 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9467 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9468 fc2737d5 2022-09-15 thomas
9469 fc2737d5 2022-09-15 thomas return NULL;
9470 fc2737d5 2022-09-15 thomas }
9471 fc2737d5 2022-09-15 thomas
9472 fc2737d5 2022-09-15 thomas static const struct got_error *
9473 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9474 fc2737d5 2022-09-15 thomas {
9475 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9476 fc2737d5 2022-09-15 thomas
9477 fc2737d5 2022-09-15 thomas
9478 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9479 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9480 fc2737d5 2022-09-15 thomas
9481 fc2737d5 2022-09-15 thomas wclear(view->window);
9482 fc2737d5 2022-09-15 thomas view->count = 0;
9483 fc2737d5 2022-09-15 thomas view->x = 0;
9484 fc2737d5 2022-09-15 thomas s->all = !s->all;
9485 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9486 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9487 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9488 fc2737d5 2022-09-15 thomas
9489 fc2737d5 2022-09-15 thomas return create_help(s);
9490 fc2737d5 2022-09-15 thomas }
9491 fc2737d5 2022-09-15 thomas
9492 fc2737d5 2022-09-15 thomas static const struct got_error *
9493 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9494 fc2737d5 2022-09-15 thomas {
9495 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9496 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9497 fc2737d5 2022-09-15 thomas
9498 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9499 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9500 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9501 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9502 fc2737d5 2022-09-15 thomas
9503 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9504 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9505 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9506 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9507 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9508 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9509 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9510 fc2737d5 2022-09-15 thomas
9511 fc2737d5 2022-09-15 thomas err = create_help(s);
9512 fc2737d5 2022-09-15 thomas return err;
9513 fc2737d5 2022-09-15 thomas }
9514 fc2737d5 2022-09-15 thomas
9515 fc2737d5 2022-09-15 thomas static const struct got_error *
9516 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9517 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9518 2a31b33b 2022-07-23 thomas {
9519 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9520 2a31b33b 2022-07-23 thomas
9521 2a31b33b 2022-07-23 thomas *new_view = NULL;
9522 2a31b33b 2022-07-23 thomas
9523 2a31b33b 2022-07-23 thomas switch (request) {
9524 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9525 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9526 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9527 2a31b33b 2022-07-23 thomas
9528 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9529 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9530 2a31b33b 2022-07-23 thomas view, s->repo);
9531 2a31b33b 2022-07-23 thomas } else
9532 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9533 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9534 2a31b33b 2022-07-23 thomas break;
9535 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9536 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9537 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9538 2a31b33b 2022-07-23 thomas
9539 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9540 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9541 2a31b33b 2022-07-23 thomas s->repo);
9542 2a31b33b 2022-07-23 thomas } else
9543 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9544 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9545 2a31b33b 2022-07-23 thomas break;
9546 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9547 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9548 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9549 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9550 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9551 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9552 2a31b33b 2022-07-23 thomas &view->state.tree);
9553 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9554 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9555 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9556 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9557 2a31b33b 2022-07-23 thomas else
9558 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9559 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9560 2a31b33b 2022-07-23 thomas break;
9561 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9562 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9563 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9564 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9565 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9566 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9567 2a31b33b 2022-07-23 thomas view->state.log.repo);
9568 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9569 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9570 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9571 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9572 2a31b33b 2022-07-23 thomas else
9573 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9574 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9575 2a31b33b 2022-07-23 thomas break;
9576 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9577 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9578 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9579 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9580 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9581 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9582 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9583 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9584 2a31b33b 2022-07-23 thomas else
9585 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9586 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9587 2a31b33b 2022-07-23 thomas if (err)
9588 2a31b33b 2022-07-23 thomas view_close(*new_view);
9589 2a31b33b 2022-07-23 thomas break;
9590 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9591 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9592 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9593 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9594 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9595 fc2737d5 2022-09-15 thomas if (err)
9596 fc2737d5 2022-09-15 thomas view_close(*new_view);
9597 fc2737d5 2022-09-15 thomas break;
9598 2a31b33b 2022-07-23 thomas default:
9599 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9600 2a31b33b 2022-07-23 thomas }
9601 2a31b33b 2022-07-23 thomas
9602 2a31b33b 2022-07-23 thomas return err;
9603 2a31b33b 2022-07-23 thomas }
9604 2a31b33b 2022-07-23 thomas
9605 a5d43cac 2022-07-01 thomas /*
9606 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9607 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9608 a5d43cac 2022-07-01 thomas */
9609 6458efa5 2020-11-24 stsp static void
9610 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9611 a5d43cac 2022-07-01 thomas {
9612 a5d43cac 2022-07-01 thomas switch (view->type) {
9613 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9614 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9615 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9616 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9617 a5d43cac 2022-07-01 thomas 1);
9618 a5d43cac 2022-07-01 thomas break;
9619 a5d43cac 2022-07-01 thomas }
9620 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9621 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9622 a5d43cac 2022-07-01 thomas else
9623 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9624 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9625 a5d43cac 2022-07-01 thomas break;
9626 a5d43cac 2022-07-01 thomas }
9627 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9628 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9629 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9630 a5d43cac 2022-07-01 thomas break;
9631 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9632 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9633 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9634 a5d43cac 2022-07-01 thomas break;
9635 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9636 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9637 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9638 a5d43cac 2022-07-01 thomas break;
9639 a5d43cac 2022-07-01 thomas default:
9640 a5d43cac 2022-07-01 thomas break;
9641 a5d43cac 2022-07-01 thomas }
9642 a5d43cac 2022-07-01 thomas
9643 a5d43cac 2022-07-01 thomas view->offset = 0;
9644 a5d43cac 2022-07-01 thomas }
9645 a5d43cac 2022-07-01 thomas
9646 a5d43cac 2022-07-01 thomas /*
9647 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9648 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9649 a5d43cac 2022-07-01 thomas */
9650 a5d43cac 2022-07-01 thomas static const struct got_error *
9651 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9652 a5d43cac 2022-07-01 thomas {
9653 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9654 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9655 a5d43cac 2022-07-01 thomas int *selected = NULL;
9656 a5d43cac 2022-07-01 thomas int header, offset;
9657 a5d43cac 2022-07-01 thomas
9658 a5d43cac 2022-07-01 thomas switch (view->type) {
9659 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9660 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9661 a5d43cac 2022-07-01 thomas header = 3;
9662 a5d43cac 2022-07-01 thomas scrolld = NULL;
9663 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9664 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9665 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9666 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9667 a5d43cac 2022-07-01 thomas view->offset = offset;
9668 a5d43cac 2022-07-01 thomas }
9669 a5d43cac 2022-07-01 thomas break;
9670 a5d43cac 2022-07-01 thomas }
9671 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9672 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9673 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9674 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9675 a5d43cac 2022-07-01 thomas selected = &s->selected;
9676 a5d43cac 2022-07-01 thomas break;
9677 a5d43cac 2022-07-01 thomas }
9678 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9679 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9680 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9681 a5d43cac 2022-07-01 thomas header = 3;
9682 a5d43cac 2022-07-01 thomas selected = &s->selected;
9683 a5d43cac 2022-07-01 thomas break;
9684 a5d43cac 2022-07-01 thomas }
9685 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9686 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9687 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9688 a5d43cac 2022-07-01 thomas header = 5;
9689 a5d43cac 2022-07-01 thomas selected = &s->selected;
9690 a5d43cac 2022-07-01 thomas break;
9691 a5d43cac 2022-07-01 thomas }
9692 a5d43cac 2022-07-01 thomas default:
9693 a5d43cac 2022-07-01 thomas selected = NULL;
9694 a5d43cac 2022-07-01 thomas scrolld = NULL;
9695 a5d43cac 2022-07-01 thomas header = 0;
9696 a5d43cac 2022-07-01 thomas break;
9697 a5d43cac 2022-07-01 thomas }
9698 a5d43cac 2022-07-01 thomas
9699 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9700 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9701 a5d43cac 2022-07-01 thomas view->offset = offset;
9702 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9703 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9704 a5d43cac 2022-07-01 thomas *selected -= offset;
9705 a5d43cac 2022-07-01 thomas }
9706 a5d43cac 2022-07-01 thomas }
9707 a5d43cac 2022-07-01 thomas
9708 a5d43cac 2022-07-01 thomas return err;
9709 a5d43cac 2022-07-01 thomas }
9710 a5d43cac 2022-07-01 thomas
9711 a5d43cac 2022-07-01 thomas static void
9712 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9713 ce5b7c56 2019-07-09 stsp {
9714 6059809a 2020-12-17 stsp size_t i;
9715 9f7d7167 2018-04-29 stsp
9716 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9717 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9718 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9719 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9720 ce5b7c56 2019-07-09 stsp }
9721 6879ba42 2020-10-01 naddy fputc('\n', fp);
9722 ce5b7c56 2019-07-09 stsp }
9723 ce5b7c56 2019-07-09 stsp
9724 4ed7e80c 2018-05-20 stsp __dead static void
9725 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9726 9f7d7167 2018-04-29 stsp {
9727 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9728 6879ba42 2020-10-01 naddy
9729 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9730 6879ba42 2020-10-01 naddy getprogname());
9731 6879ba42 2020-10-01 naddy if (hflag) {
9732 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9733 6879ba42 2020-10-01 naddy list_commands(fp);
9734 ee85c5e8 2020-02-29 stsp }
9735 6879ba42 2020-10-01 naddy exit(status);
9736 9f7d7167 2018-04-29 stsp }
9737 9f7d7167 2018-04-29 stsp
9738 c2301be8 2018-04-30 stsp static char **
9739 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9740 c2301be8 2018-04-30 stsp {
9741 ee85c5e8 2020-02-29 stsp va_list ap;
9742 c2301be8 2018-04-30 stsp char **argv;
9743 ee85c5e8 2020-02-29 stsp int i;
9744 c2301be8 2018-04-30 stsp
9745 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9746 ee85c5e8 2020-02-29 stsp
9747 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9748 c2301be8 2018-04-30 stsp if (argv == NULL)
9749 c2301be8 2018-04-30 stsp err(1, "calloc");
9750 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9751 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9752 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9753 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9754 c2301be8 2018-04-30 stsp }
9755 c2301be8 2018-04-30 stsp
9756 ee85c5e8 2020-02-29 stsp va_end(ap);
9757 c2301be8 2018-04-30 stsp return argv;
9758 ee85c5e8 2020-02-29 stsp }
9759 ee85c5e8 2020-02-29 stsp
9760 ee85c5e8 2020-02-29 stsp /*
9761 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9762 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9763 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9764 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9765 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9766 ee85c5e8 2020-02-29 stsp */
9767 ee85c5e8 2020-02-29 stsp static const struct got_error *
9768 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9769 ee85c5e8 2020-02-29 stsp {
9770 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9771 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9772 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9773 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9774 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9775 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9776 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9777 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9778 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9779 84de9106 2020-12-26 stsp
9780 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9781 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9782 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9783 ee85c5e8 2020-02-29 stsp
9784 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9785 7cd52833 2022-06-23 thomas if (error != NULL)
9786 7cd52833 2022-06-23 thomas goto done;
9787 7cd52833 2022-06-23 thomas
9788 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9789 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9790 ee85c5e8 2020-02-29 stsp goto done;
9791 ee85c5e8 2020-02-29 stsp
9792 ee85c5e8 2020-02-29 stsp if (worktree)
9793 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9794 ee85c5e8 2020-02-29 stsp else
9795 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9796 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9797 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9798 ee85c5e8 2020-02-29 stsp goto done;
9799 ee85c5e8 2020-02-29 stsp }
9800 ee85c5e8 2020-02-29 stsp
9801 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9802 ee85c5e8 2020-02-29 stsp if (error != NULL)
9803 ee85c5e8 2020-02-29 stsp goto done;
9804 ee85c5e8 2020-02-29 stsp
9805 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9806 ee85c5e8 2020-02-29 stsp repo, worktree);
9807 ee85c5e8 2020-02-29 stsp if (error)
9808 ee85c5e8 2020-02-29 stsp goto done;
9809 ee85c5e8 2020-02-29 stsp
9810 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9811 84de9106 2020-12-26 stsp if (error)
9812 84de9106 2020-12-26 stsp goto done;
9813 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9814 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9815 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9816 ee85c5e8 2020-02-29 stsp if (error)
9817 ee85c5e8 2020-02-29 stsp goto done;
9818 ee85c5e8 2020-02-29 stsp
9819 ee85c5e8 2020-02-29 stsp if (worktree) {
9820 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9821 ee85c5e8 2020-02-29 stsp worktree = NULL;
9822 ee85c5e8 2020-02-29 stsp }
9823 ee85c5e8 2020-02-29 stsp
9824 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9825 945f9229 2022-04-16 thomas if (error)
9826 945f9229 2022-04-16 thomas goto done;
9827 945f9229 2022-04-16 thomas
9828 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9829 ee85c5e8 2020-02-29 stsp if (error) {
9830 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9831 ee85c5e8 2020-02-29 stsp goto done;
9832 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9833 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9834 6879ba42 2020-10-01 naddy usage(1, 1);
9835 ee85c5e8 2020-02-29 stsp /* not reached */
9836 ee85c5e8 2020-02-29 stsp }
9837 ee85c5e8 2020-02-29 stsp
9838 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9839 ee85c5e8 2020-02-29 stsp if (error)
9840 ee85c5e8 2020-02-29 stsp goto done;
9841 ee85c5e8 2020-02-29 stsp
9842 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9843 ee85c5e8 2020-02-29 stsp argc = 4;
9844 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9845 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9846 ee85c5e8 2020-02-29 stsp done:
9847 1d0f4054 2021-06-17 stsp if (repo) {
9848 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9849 1d0f4054 2021-06-17 stsp if (error == NULL)
9850 1d0f4054 2021-06-17 stsp error = close_err;
9851 1d0f4054 2021-06-17 stsp }
9852 945f9229 2022-04-16 thomas if (commit)
9853 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9854 ee85c5e8 2020-02-29 stsp if (worktree)
9855 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9856 7cd52833 2022-06-23 thomas if (pack_fds) {
9857 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9858 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9859 7cd52833 2022-06-23 thomas if (error == NULL)
9860 7cd52833 2022-06-23 thomas error = pack_err;
9861 7cd52833 2022-06-23 thomas }
9862 ee85c5e8 2020-02-29 stsp free(id);
9863 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9864 ee85c5e8 2020-02-29 stsp free(commit_id);
9865 ee85c5e8 2020-02-29 stsp free(cwd);
9866 ee85c5e8 2020-02-29 stsp free(repo_path);
9867 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9868 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9869 ee85c5e8 2020-02-29 stsp int i;
9870 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9871 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9872 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9873 ee85c5e8 2020-02-29 stsp }
9874 87670572 2020-12-26 naddy tog_free_refs();
9875 ee85c5e8 2020-02-29 stsp return error;
9876 c2301be8 2018-04-30 stsp }
9877 c2301be8 2018-04-30 stsp
9878 9f7d7167 2018-04-29 stsp int
9879 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9880 9f7d7167 2018-04-29 stsp {
9881 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
9882 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9883 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9884 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9885 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9886 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9887 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9888 83cd27f8 2020-01-13 stsp };
9889 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9890 557d3365 2023-04-14 thomas const char *test_script_path;
9891 a0abeae5 2023-02-17 thomas
9892 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
9893 a0abeae5 2023-02-17 thomas
9894 557d3365 2023-04-14 thomas /*
9895 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
9896 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
9897 557d3365 2023-04-14 thomas */
9898 fa9bb690 2023-04-22 thomas test_script_path = getenv("TOG_TEST_SCRIPT");
9899 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
9900 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
9901 557d3365 2023-04-14 thomas if (error) {
9902 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9903 557d3365 2023-04-14 thomas return 1;
9904 557d3365 2023-04-14 thomas }
9905 5ffbe221 2023-04-22 thomas } else if (!isatty(STDIN_FILENO))
9906 5ffbe221 2023-04-22 thomas errx(1, "standard input is not a tty");
9907 557d3365 2023-04-14 thomas
9908 557d3365 2023-04-14 thomas #if !defined(PROFILE)
9909 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9910 a0abeae5 2023-02-17 thomas NULL) == -1)
9911 a0abeae5 2023-02-17 thomas err(1, "pledge");
9912 a0abeae5 2023-02-17 thomas #endif
9913 9f7d7167 2018-04-29 stsp
9914 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9915 9f7d7167 2018-04-29 stsp switch (ch) {
9916 9f7d7167 2018-04-29 stsp case 'h':
9917 9f7d7167 2018-04-29 stsp hflag = 1;
9918 9f7d7167 2018-04-29 stsp break;
9919 53ccebc2 2019-07-30 stsp case 'V':
9920 53ccebc2 2019-07-30 stsp Vflag = 1;
9921 53ccebc2 2019-07-30 stsp break;
9922 9f7d7167 2018-04-29 stsp default:
9923 6879ba42 2020-10-01 naddy usage(hflag, 1);
9924 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9925 9f7d7167 2018-04-29 stsp }
9926 9f7d7167 2018-04-29 stsp }
9927 9f7d7167 2018-04-29 stsp
9928 9f7d7167 2018-04-29 stsp argc -= optind;
9929 9f7d7167 2018-04-29 stsp argv += optind;
9930 9814e6a3 2020-09-27 naddy optind = 1;
9931 c2301be8 2018-04-30 stsp optreset = 1;
9932 9f7d7167 2018-04-29 stsp
9933 53ccebc2 2019-07-30 stsp if (Vflag) {
9934 53ccebc2 2019-07-30 stsp got_version_print_str();
9935 6879ba42 2020-10-01 naddy return 0;
9936 53ccebc2 2019-07-30 stsp }
9937 4010e238 2020-12-04 stsp
9938 c2301be8 2018-04-30 stsp if (argc == 0) {
9939 f29d3e89 2018-06-23 stsp if (hflag)
9940 6879ba42 2020-10-01 naddy usage(hflag, 0);
9941 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9942 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9943 c2301be8 2018-04-30 stsp argc = 1;
9944 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9945 c2301be8 2018-04-30 stsp } else {
9946 6059809a 2020-12-17 stsp size_t i;
9947 9f7d7167 2018-04-29 stsp
9948 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9949 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9950 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9951 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9952 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9953 9f7d7167 2018-04-29 stsp break;
9954 9f7d7167 2018-04-29 stsp }
9955 9f7d7167 2018-04-29 stsp }
9956 ee85c5e8 2020-02-29 stsp }
9957 3642c4c6 2019-07-09 stsp
9958 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9959 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9960 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9961 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9962 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9963 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9964 adf4c9e0 2022-07-03 thomas }
9965 adf4c9e0 2022-07-03 thomas
9966 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9967 ee85c5e8 2020-02-29 stsp if (argc != 1)
9968 6879ba42 2020-10-01 naddy usage(0, 1);
9969 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9970 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9971 ee85c5e8 2020-02-29 stsp } else {
9972 ee85c5e8 2020-02-29 stsp if (hflag)
9973 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9974 ee85c5e8 2020-02-29 stsp else
9975 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9976 9f7d7167 2018-04-29 stsp }
9977 9f7d7167 2018-04-29 stsp
9978 1d98034b 2023-04-14 thomas if (using_mock_io) {
9979 1d98034b 2023-04-14 thomas io_err = tog_io_close();
9980 1d98034b 2023-04-14 thomas if (error == NULL)
9981 1d98034b 2023-04-14 thomas error = io_err;
9982 1d98034b 2023-04-14 thomas }
9983 9f7d7167 2018-04-29 stsp endwin();
9984 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9985 a2f4a359 2020-02-28 stsp int i;
9986 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9987 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9988 a2f4a359 2020-02-28 stsp free(cmd_argv);
9989 a2f4a359 2020-02-28 stsp }
9990 a2f4a359 2020-02-28 stsp
9991 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9992 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9993 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9994 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9995 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9996 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9997 9f7d7167 2018-04-29 stsp return 0;
9998 9f7d7167 2018-04-29 stsp }